1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2012 */
6 /* See the file NOTICE for conditions of use and distribution. */
8 /* Copyright (c) Twitter Inc 2012
9 Author: Phil Pennock <pdp@exim.org> */
10 /* Copyright (c) Phil Pennock 2012 */
12 /* Interface to Heimdal SASL library for GSSAPI authentication. */
14 /* Naming and rationale
16 Sensibly, this integration would be deferred to a SASL library, but none
17 of them appear to offer keytab file selection interfaces in their APIs. It
18 might be that this driver only requires minor modification to work with MIT
21 Heimdal provides a number of interfaces for various forms of authentication.
22 As GS2 does not appear to provide keytab control interfaces either, we may
23 end up supporting that too. It's possible that we could trivially expand to
24 support NTLM support via Heimdal, etc. Rather than try to be too generic
25 immediately, this driver is directly only supporting GSSAPI.
27 Without rename, we could add an option for GS2 support in the future.
32 * mailcheck-imap (Perl, client-side, written by me years ago)
33 * gsasl driver (GPL, server-side)
34 * heimdal sources and man-pages, plus http://www.h5l.org/manual/
35 * FreeBSD man-pages (very informative!)
36 * http://www.ggf.org/documents/GFD.24.pdf confirming GSS_KRB5_REGISTER_ACCEPTOR_IDENTITY_X
37 semantics, that found by browsing Heimdal source to find how to set the keytab
43 #ifndef AUTH_HEIMDAL_GSSAPI
44 /* dummy function to satisfy compilers when we link in an "empty" file. */
45 static void dummy(int x) { dummy(x-1); }
48 #include <gssapi/gssapi.h>
49 #include <gssapi/gssapi_krb5.h>
51 /* for the _init debugging */
54 /* Because __gss_krb5_register_acceptor_identity_x_oid_desc is internal */
57 #include "heimdal_gssapi.h"
59 /* Authenticator-specific options. */
60 optionlist auth_heimdal_gssapi_options[] = {
61 { "server_hostname", opt_stringptr,
62 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_hostname)) },
63 { "server_keytab", opt_stringptr,
64 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_keytab)) },
65 { "server_realm", opt_stringptr,
66 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_realm)) },
67 { "server_service", opt_stringptr,
68 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_service)) }
71 int auth_heimdal_gssapi_options_count =
72 sizeof(auth_heimdal_gssapi_options)/sizeof(optionlist);
74 /* Defaults for the authenticator-specific options. */
75 auth_heimdal_gssapi_options_block auth_heimdal_gssapi_option_defaults = {
76 US"$primary_hostname", /* server_hostname */
77 NULL, /* server_keytab */
78 NULL, /* server_realm */
79 US"smtp", /* server_service */
82 /* "Globals" for managing the heimdal_gssapi interface. */
84 /* Utility functions */
86 exim_heimdal_error_debug(const char *, krb5_context, krb5_error_code);
88 exim_gssapi_error_defer(uschar *, OM_uint32, OM_uint32, const char *, ...)
89 PRINTF_FUNCTION(4, 5);
91 #define EmptyBuf(buf) do { buf.value = NULL; buf.length = 0; } while (0)
94 /*************************************************
95 * Initialization entry point *
96 *************************************************/
98 /* Called for each instance, after its options have been read, to
99 enable consistency checks to be done, or anything else that needs
102 /* Heimdal provides a GSSAPI extension method (via an OID) for setting the
103 keytab; in the init, we mostly just use raw krb5 methods so that we can report
104 the keytab contents, for -D+auth debugging. */
107 auth_heimdal_gssapi_init(auth_instance *ablock)
109 krb5_context context;
111 krb5_kt_cursor cursor;
112 krb5_keytab_entry entry;
114 char *principal, *enctype_s;
115 const char *k_keytab_typed_name = NULL;
116 auth_heimdal_gssapi_options_block *ob =
117 (auth_heimdal_gssapi_options_block *)(ablock->options_block);
119 ablock->server = FALSE;
120 ablock->client = FALSE;
122 if (!ob->server_service || !*ob->server_service) {
123 HDEBUG(D_auth) debug_printf("heimdal: missing server_service\n");
127 krc = krb5_init_context(&context);
130 HDEBUG(D_auth) debug_printf("heimdal: failed to initialise krb5 context: %s\n",
135 if (ob->server_keytab) {
136 k_keytab_typed_name = CCS string_sprintf("file:%s", expand_string(ob->server_keytab));
137 HDEBUG(D_auth) debug_printf("heimdal: using keytab %s\n", k_keytab_typed_name);
138 krc = krb5_kt_resolve(context, k_keytab_typed_name, &keytab);
140 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_resolve", context, krc);
144 HDEBUG(D_auth) debug_printf("heimdal: using system default keytab\n");
145 krc = krb5_kt_default(context, &keytab);
147 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_default", context, krc);
153 /* http://www.h5l.org/manual/HEAD/krb5/krb5_keytab_intro.html */
154 krc = krb5_kt_start_seq_get(context, keytab, &cursor);
156 exim_heimdal_error_debug("krb5_kt_start_seq_get", context, krc);
158 while ((krc = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
159 principal = enctype_s = NULL;
160 krb5_unparse_name(context, entry.principal, &principal);
161 krb5_enctype_to_string(context, entry.keyblock.keytype, &enctype_s);
162 debug_printf("heimdal: keytab principal: %s vno=%d type=%s\n",
163 principal ? principal : "??",
165 enctype_s ? enctype_s : "??");
168 krb5_kt_free_entry(context, &entry);
170 krc = krb5_kt_end_seq_get(context, keytab, &cursor);
172 exim_heimdal_error_debug("krb5_kt_end_seq_get", context, krc);
176 krc = krb5_kt_close(context, keytab);
178 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_close", context, krc);
180 krb5_free_context(context);
182 /* RFC 4121 section 5.2, SHOULD support 64K input buffers */
183 if (big_buffer_size < (64 * 1024)) {
185 big_buffer_size = 64 * 1024;
186 newbuf = store_malloc(big_buffer_size);
187 store_free(big_buffer);
191 ablock->server = TRUE;
196 exim_heimdal_error_debug(const char *label,
197 krb5_context context, krb5_error_code err)
200 kerrsc = krb5_get_error_message(context, err);
201 debug_printf("heimdal %s: %s\n", label, kerrsc ? kerrsc : "unknown error");
202 krb5_free_error_message(context, kerrsc);
205 /*************************************************
206 * Server entry point *
207 *************************************************/
209 /* For interface, see auths/README */
212 OM_uint32: portable type for unsigned int32
213 gss_buffer_desc / *gss_buffer_t: hold/point-to size_t .length & void *value
214 -- all strings/etc passed in should go through one of these
215 -- when allocated by gssapi, release with gss_release_buffer()
219 auth_heimdal_gssapi_server(auth_instance *ablock, uschar *initial_data)
221 gss_name_t gclient = GSS_C_NO_NAME;
222 gss_name_t gserver = GSS_C_NO_NAME;
223 gss_cred_id_t gcred = GSS_C_NO_CREDENTIAL;
224 gss_ctx_id_t gcontext = GSS_C_NO_CONTEXT;
225 uschar *ex_server_str;
226 gss_buffer_desc gbufdesc = GSS_C_EMPTY_BUFFER;
227 gss_buffer_desc gbufdesc_in = GSS_C_EMPTY_BUFFER;
228 gss_buffer_desc gbufdesc_out = GSS_C_EMPTY_BUFFER;
230 OM_uint32 maj_stat, min_stat;
231 int step, error_out, i;
232 uschar *tmp1, *tmp2, *from_client;
233 auth_heimdal_gssapi_options_block *ob =
234 (auth_heimdal_gssapi_options_block *)(ablock->options_block);
235 BOOL handled_empty_ir;
236 uschar *store_reset_point;
238 uschar sasl_config[4];
239 uschar requested_qop;
241 store_reset_point = store_get(0);
244 debug_printf("heimdal: initialising auth context for %s\n", ablock->name);
246 /* Construct our gss_name_t gserver describing ourselves */
247 tmp1 = expand_string(ob->server_service);
248 tmp2 = expand_string(ob->server_hostname);
249 ex_server_str = string_sprintf("%s@%s", tmp1, tmp2);
250 gbufdesc.value = (void *) ex_server_str;
251 gbufdesc.length = Ustrlen(ex_server_str);
252 maj_stat = gss_import_name(&min_stat,
253 &gbufdesc, GSS_C_NT_HOSTBASED_SERVICE, &gserver);
254 if (GSS_ERROR(maj_stat))
255 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
256 "gss_import_name(%s)", CS gbufdesc.value);
258 /* Use a specific keytab, if specified */
259 if (ob->server_keytab) {
260 keytab = expand_string(ob->server_keytab);
261 maj_stat = gsskrb5_register_acceptor_identity(CCS keytab);
262 if (GSS_ERROR(maj_stat))
263 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
264 "registering keytab \"%s\"", keytab);
266 debug_printf("heimdal: using keytab \"%s\"\n", keytab);
269 /* Acquire our credentials */
270 maj_stat = gss_acquire_cred(&min_stat,
271 gserver, /* desired name */
273 GSS_C_NULL_OID_SET, /* desired mechs */
274 GSS_C_ACCEPT, /* cred usage */
276 NULL /* actual mechs */,
277 NULL /* time rec */);
278 if (GSS_ERROR(maj_stat))
279 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
280 "gss_acquire_cred(%s)", ex_server_str);
282 maj_stat = gss_release_name(&min_stat, &gserver);
284 HDEBUG(D_auth) debug_printf("heimdal: have server credentials.\n");
286 /* Loop talking to client */
288 from_client = initial_data;
289 handled_empty_ir = FALSE;
292 /* buffer sizes: auth_get_data() uses big_buffer, which we grow per
293 GSSAPI RFC in _init, if needed, to meet the SHOULD size of 64KB.
294 (big_buffer starts life at the MUST size of 16KB). */
297 0: getting initial data from client to feed into GSSAPI
298 1: iterating for as long as GSS_S_CONTINUE_NEEDED
299 2: GSS_S_COMPLETE, SASL wrapping for authz and qop to send to client
300 3: unpick final auth message from client
301 4: break/finish (non-step)
306 if (!from_client || *from_client == '\0') {
307 if (handled_empty_ir) {
308 HDEBUG(D_auth) debug_printf("gssapi: repeated empty input, grr.\n");
312 HDEBUG(D_auth) debug_printf("gssapi: missing initial response, nudging.\n");
313 error_out = auth_get_data(&from_client, US"", 0);
319 /* We should now have the opening data from the client, base64-encoded. */
321 HDEBUG(D_auth) debug_printf("heimdal: have initial client data\n");
325 gbufdesc_in.length = auth_b64decode(from_client, USS &gbufdesc_in.value);
327 maj_stat = gss_release_name(&min_stat, &gclient);
328 gclient = GSS_C_NO_NAME;
330 maj_stat = gss_accept_sec_context(&min_stat,
331 &gcontext, /* context handle */
332 gcred, /* acceptor cred handle */
333 &gbufdesc_in, /* input from client */
334 GSS_C_NO_CHANNEL_BINDINGS, /* XXX fixme: use the channel bindings from GnuTLS */
335 &gclient, /* client identifier */
336 &mech_type, /* mechanism in use */
337 &gbufdesc_out, /* output to send to client */
338 NULL, /* return flags */
340 NULL /* delegated cred_handle */
342 if (GSS_ERROR(maj_stat)) {
343 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
344 "gss_accept_sec_context()");
348 if (&gbufdesc_out.length != 0) {
349 error_out = auth_get_data(&from_client,
350 gbufdesc_out.value, gbufdesc_out.length);
354 gss_release_buffer(&min_stat, &gbufdesc_out);
355 EmptyBuf(gbufdesc_out);
357 if (maj_stat == GSS_S_COMPLETE) {
359 HDEBUG(D_auth) debug_printf("heimdal: GSS complete\n");
361 HDEBUG(D_auth) debug_printf("heimdal: need more data\n");
366 memset(sasl_config, 0xFF, 4);
367 /* draft-ietf-sasl-gssapi-06.txt defines bitmasks for first octet
368 0x01 No security layer
369 0x02 Integrity protection
370 0x04 Confidentiality protection
372 The remaining three octets are the maximum buffer size for wrappe
374 sasl_config[0] = 0x01; /* Exim does not wrap/unwrap SASL layers after auth */
375 gbufdesc.value = (void *) sasl_config;
377 maj_stat = gss_wrap(&min_stat,
379 0, /* conf_req_flag: integrity only */
380 GSS_C_QOP_DEFAULT, /* qop requested */
381 &gbufdesc, /* message to protect */
382 NULL, /* conf_state: no confidentiality applied */
383 &gbufdesc_out /* output buffer */
385 if (GSS_ERROR(maj_stat)) {
386 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
387 "gss_wrap(SASL state after auth)");
392 HDEBUG(D_auth) debug_printf("heimdal SASL: requesting QOP with no security layers\n");
394 error_out = auth_get_data(&from_client,
395 gbufdesc_out.value, gbufdesc_out.length);
399 gss_release_buffer(&min_stat, &gbufdesc_out);
400 EmptyBuf(gbufdesc_out);
405 gbufdesc_in.length = auth_b64decode(from_client, USS &gbufdesc_in.value);
406 maj_stat = gss_unwrap(&min_stat,
408 &gbufdesc_in, /* data from client */
409 &gbufdesc_out, /* results */
410 NULL, /* conf state */
413 if (GSS_ERROR(maj_stat)) {
414 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
415 "gss_unwrap(final SASL message from client)");
419 if (gbufdesc_out.length < 5) {
421 debug_printf("gssapi: final message too short; "
422 "need flags, buf sizes and authzid\n");
427 requested_qop = (CS gbufdesc_out.value)[0];
428 if ((requested_qop & 0x01) == 0) {
430 debug_printf("gssapi: client requested security layers (%x)\n",
431 (unsigned int) requested_qop);
436 for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
440 The SASL provided identifier is an unverified authzid.
441 GSSAPI provides us with a verified identifier.
444 /* $auth2 is authzid requested at SASL layer */
445 expand_nlength[2] = gbufdesc_out.length - 4;
446 auth_vars[1] = expand_nstring[2] =
447 string_copyn((US gbufdesc_out.value) + 4, expand_nlength[2]);
450 gss_release_buffer(&min_stat, &gbufdesc_out);
451 EmptyBuf(gbufdesc_out);
453 /* $auth1 is GSSAPI display name */
454 maj_stat = gss_display_name(&min_stat,
458 if (GSS_ERROR(maj_stat)) {
459 auth_vars[1] = expand_nstring[2] = NULL;
461 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
462 "gss_display_name(client identifier)");
467 expand_nlength[1] = gbufdesc_out.length;
468 auth_vars[0] = expand_nstring[1] =
469 string_copyn(gbufdesc_out.value, gbufdesc_out.length);
472 debug_printf("heimdal SASL: happy with client request\n"
473 " auth1 (verified GSSAPI display-name): \"%s\"\n"
474 " auth2 (unverified SASL requested authzid): \"%s\"\n",
475 auth_vars[0], auth_vars[1]);
485 maj_stat = gss_release_cred(&min_stat, &gcred);
487 gss_release_name(&min_stat, &gclient);
488 gclient = GSS_C_NO_NAME;
490 if (gbufdesc_out.length) {
491 gss_release_buffer(&min_stat, &gbufdesc_out);
492 EmptyBuf(gbufdesc_out);
494 if (gcontext != GSS_C_NO_CONTEXT) {
495 gss_delete_sec_context(&min_stat, &gcontext, GSS_C_NO_BUFFER);
498 store_reset(store_reset_point);
503 /* Auth succeeded, check server_condition */
504 return auth_check_serv_cond(ablock);
509 exim_gssapi_error_defer(uschar *store_reset_point,
510 OM_uint32 major, OM_uint32 minor,
511 const char *format, ...)
514 uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
515 OM_uint32 maj_stat, min_stat;
516 OM_uint32 msgcontext = 0;
517 gss_buffer_desc status_string;
519 va_start(ap, format);
520 if (!string_vformat(buffer, sizeof(buffer), format, ap))
521 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
522 "exim_gssapi_error_defer expansion larger than %d",
526 auth_defer_msg = NULL;
529 maj_stat = gss_display_status(&min_stat,
530 major, GSS_C_GSS_CODE, GSS_C_NO_OID,
531 &msgcontext, &status_string);
533 if (auth_defer_msg == NULL) {
534 auth_defer_msg = string_copy(US status_string.value);
537 HDEBUG(D_auth) debug_printf("heimdal %s: %.*s\n",
538 buffer, (int)status_string.length, CS status_string.value);
539 gss_release_buffer(&min_stat, &status_string);
541 } while (msgcontext != 0);
543 if (store_reset_point)
544 store_reset(store_reset_point);
549 /*************************************************
550 * Client entry point *
551 *************************************************/
553 /* For interface, see auths/README */
556 auth_heimdal_gssapi_client(
557 auth_instance *ablock, /* authenticator block */
558 smtp_inblock *inblock, /* connection inblock */
559 smtp_outblock *outblock, /* connection outblock */
560 int timeout, /* command timeout */
561 uschar *buffer, /* buffer for reading response */
562 int buffsize) /* size of buffer */
565 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
566 /* NOT IMPLEMENTED */
570 /*************************************************
572 *************************************************/
575 auth_heimdal_gssapi_version_report(FILE *f)
577 /* No build-time constants available unless we link against libraries at
578 build-time and export the result as a string into a header ourselves. */
579 fprintf(f, "Library version: Heimdal: Runtime: %s\n"
581 heimdal_version, heimdal_long_version);
584 #endif /* AUTH_HEIMDAL_GSSAPI */
586 /* End of heimdal_gssapi.c */