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> */
11 /* Interface to Heimdal SASL library for GSSAPI authentication. */
13 /* Naming and rationale
15 Sensibly, this integration would be deferred to a SASL library, but none
16 of them appear to offer keytab file selection interfaces in their APIs. It
17 might be that this driver only requires minor modification to work with MIT
20 Heimdal provides a number of interfaces for various forms of authentication.
21 As GS2 does not appear to provide keytab control interfaces either, we may
22 end up supporting that too. It's possible that we could trivially expand to
23 support NTLM support via Heimdal, etc. Rather than try to be too generic
24 immediately, this driver is directly only supporting GSSAPI.
26 Without rename, we could add an option for GS2 support in the future.
31 * mailcheck-imap (Perl, client-side, written by me years ago)
32 * gsasl driver (GPL, server-side)
33 * heimdal sources and man-pages, plus http://www.h5l.org/manual/
34 * FreeBSD man-pages (very informative!)
35 * http://www.ggf.org/documents/GFD.24.pdf confirming GSS_KRB5_REGISTER_ACCEPTOR_IDENTITY_X
36 semantics, that found by browsing Heimdal source to find how to set the keytab
42 #ifndef AUTH_HEIMDAL_GSSAPI
43 /* dummy function to satisfy compilers when we link in an "empty" file. */
44 static void dummy(int x) { dummy(x-1); }
47 #include <gssapi/gssapi.h>
48 #include <gssapi/gssapi_krb5.h>
50 /* for the _init debugging */
53 /* Because __gss_krb5_register_acceptor_identity_x_oid_desc is internal */
56 #include "heimdal_gssapi.h"
58 /* Authenticator-specific options. */
59 optionlist auth_heimdal_gssapi_options[] = {
60 { "server_hostname", opt_stringptr,
61 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_hostname)) },
62 { "server_keytab", opt_stringptr,
63 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_keytab)) },
64 { "server_realm", opt_stringptr,
65 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_realm)) },
66 { "server_service", opt_stringptr,
67 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_service)) }
70 int auth_heimdal_gssapi_options_count =
71 sizeof(auth_heimdal_gssapi_options)/sizeof(optionlist);
73 /* Defaults for the authenticator-specific options. */
74 auth_heimdal_gssapi_options_block auth_heimdal_gssapi_option_defaults = {
75 US"$primary_hostname", /* server_hostname */
76 NULL, /* server_keytab */
77 NULL, /* server_realm */
78 US"smtp", /* server_service */
81 /* "Globals" for managing the heimdal_gssapi interface. */
83 /* hack around unavailable __gss_krb5_register_acceptor_identity_x_oid_desc
85 from heimdal lib/gssapi/krb5/external.c */
86 gss_OID_desc exim_register_keytab_OID = {6, rk_UNCONST("\x2a\x85\x70\x2b\x0d\x05")};
88 /* Utility functions */
90 exim_heimdal_error_debug(const char *, krb5_context, krb5_error_code);
92 exim_gssapi_error_defer(uschar *, OM_uint32, OM_uint32, const char *, ...)
93 PRINTF_FUNCTION(4, 5);
95 #define EmptyBuf(buf) do { buf.value = NULL; buf.length = 0; } while (0)
98 /*************************************************
99 * Initialization entry point *
100 *************************************************/
102 /* Called for each instance, after its options have been read, to
103 enable consistency checks to be done, or anything else that needs
106 /* Heimdal provides a GSSAPI extension method (via an OID) for setting the
107 keytab; in the init, we mostly just use raw krb5 methods so that we can report
108 the keytab contents, for -D+auth debugging. */
111 auth_heimdal_gssapi_init(auth_instance *ablock)
113 krb5_context context;
115 krb5_kt_cursor cursor;
116 krb5_keytab_entry entry;
118 char *principal, *enctype_s;
119 const char *k_keytab_typed_name = NULL;
120 auth_heimdal_gssapi_options_block *ob =
121 (auth_heimdal_gssapi_options_block *)(ablock->options_block);
123 ablock->server = FALSE;
124 ablock->client = FALSE;
126 if (!ob->server_service || !*ob->server_service) {
127 HDEBUG(D_auth) debug_printf("heimdal: missing server_service\n");
131 krc = krb5_init_context(&context);
134 HDEBUG(D_auth) debug_printf("heimdal: failed to initialise krb5 context: %s\n",
139 if (ob->server_keytab) {
140 k_keytab_typed_name = CCS string_sprintf("file:%s", expand_string(ob->server_keytab));
141 HDEBUG(D_auth) debug_printf("heimdal: using keytab %s\n", k_keytab_typed_name);
142 krc = krb5_kt_resolve(context, k_keytab_typed_name, &keytab);
144 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_resolve", context, krc);
148 HDEBUG(D_auth) debug_printf("heimdal: using system default keytab\n");
149 krc = krb5_kt_default(context, &keytab);
151 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_default", context, krc);
157 /* http://www.h5l.org/manual/HEAD/krb5/krb5_keytab_intro.html */
158 krc = krb5_kt_start_seq_get(context, keytab, &cursor);
160 exim_heimdal_error_debug("krb5_kt_start_seq_get", context, krc);
162 while ((krc = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0) {
163 principal = enctype_s = NULL;
164 krb5_unparse_name(context, entry.principal, &principal);
165 krb5_enctype_to_string(context, entry.keyblock.keytype, &enctype_s);
166 debug_printf("heimdal: keytab principal: %s vno=%d type=%s\n",
167 principal ? principal : "??",
169 enctype_s ? enctype_s : "??");
172 krb5_kt_free_entry(context, &entry);
174 krc = krb5_kt_end_seq_get(context, keytab, &cursor);
176 exim_heimdal_error_debug("krb5_kt_end_seq_get", context, krc);
180 krc = krb5_kt_close(context, keytab);
182 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_close", context, krc);
184 krb5_free_context(context);
186 /* RFC 4121 section 5.2, SHOULD support 64K input buffers */
187 if (big_buffer_size < (64 * 1024)) {
189 big_buffer_size = 64 * 1024;
190 newbuf = store_malloc(big_buffer_size);
191 store_free(big_buffer);
195 ablock->server = TRUE;
200 exim_heimdal_error_debug(const char *label,
201 krb5_context context, krb5_error_code err)
204 kerrsc = krb5_get_error_message(context, err);
205 debug_printf("heimdal %s: %s\n", label, kerrsc ? kerrsc : "unknown error");
206 krb5_free_error_message(context, kerrsc);
209 /*************************************************
210 * Server entry point *
211 *************************************************/
213 /* For interface, see auths/README */
216 OM_uint32: portable type for unsigned int32
217 gss_buffer_desc / *gss_buffer_t: hold/point-to size_t .length & void *value
218 -- all strings/etc passed in should go through one of these
219 -- when allocated by gssapi, release with gss_release_buffer()
223 auth_heimdal_gssapi_server(auth_instance *ablock, uschar *initial_data)
225 gss_name_t gclient = GSS_C_NO_NAME;
226 gss_name_t gserver = GSS_C_NO_NAME;
227 gss_cred_id_t gcred = GSS_C_NO_CREDENTIAL;
228 gss_ctx_id_t gcontext = GSS_C_NO_CONTEXT;
229 uschar *ex_server_str;
230 gss_buffer_desc gbufdesc = GSS_C_EMPTY_BUFFER;
231 gss_buffer_desc gbufdesc_in = GSS_C_EMPTY_BUFFER;
232 gss_buffer_desc gbufdesc_out = GSS_C_EMPTY_BUFFER;
234 OM_uint32 maj_stat, min_stat;
235 int step, error_out, i;
236 uschar *tmp1, *tmp2, *from_client;
237 auth_heimdal_gssapi_options_block *ob =
238 (auth_heimdal_gssapi_options_block *)(ablock->options_block);
239 BOOL handled_empty_ir;
240 uschar *store_reset_point;
241 uschar sasl_config[4];
242 uschar requested_qop;
244 store_reset_point = store_get(0);
247 debug_printf("heimdal: initialising auth context for %s\n", ablock->name);
249 /* Construct our gss_name_t gserver describing ourselves */
250 tmp1 = expand_string(ob->server_service);
251 tmp2 = expand_string(ob->server_hostname);
252 ex_server_str = string_sprintf("%s@%s", tmp1, tmp2);
253 gbufdesc.value = (void *) ex_server_str;
254 gbufdesc.length = Ustrlen(ex_server_str);
255 maj_stat = gss_import_name(&min_stat,
256 &gbufdesc, GSS_C_NT_HOSTBASED_SERVICE, &gserver);
257 if (GSS_ERROR(maj_stat))
258 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
259 "gss_import_name(%s)", CS gbufdesc.value);
261 /* Use a specific keytab, if specified */
262 if (ob->server_keytab) {
263 gbufdesc.value = (void *) string_sprintf("file:%s", expand_string(ob->server_keytab));
264 gbufdesc.length = strlen(CS gbufdesc.value);
265 maj_stat = gss_set_sec_context_option(&min_stat,
266 &gcontext, /* create new security context */
267 &exim_register_keytab_OID, /* GSS_KRB5_REGISTER_ACCEPTOR_IDENTITY_X */
269 if (GSS_ERROR(maj_stat))
270 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
271 "registering keytab \"%s\"", CS gbufdesc.value);
274 /* Acquire our credentials */
275 maj_stat = gss_acquire_cred(&min_stat,
276 gserver, /* desired name */
278 GSS_C_NULL_OID_SET, /* desired mechs */
279 GSS_C_ACCEPT, /* cred usage */
281 NULL /* actual mechs */,
282 NULL /* time rec */);
283 if (GSS_ERROR(maj_stat))
284 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
285 "gss_acquire_cred(%s)", ex_server_str);
287 maj_stat = gss_release_name(&min_stat, &gserver);
289 /* Loop talking to client */
291 from_client = initial_data;
292 handled_empty_ir = FALSE;
295 /* buffer sizes: auth_get_data() uses big_buffer, which we grow per
296 GSSAPI RFC in _init, if needed, to meet the SHOULD size of 64KB.
297 (big_buffer starts life at the MUST size of 16KB). */
300 0: getting initial data from client to feed into GSSAPI
301 1: iterating for as long as GSS_S_CONTINUE_NEEDED
302 2: GSS_S_COMPLETE, SASL wrapping for authz and qop to send to client
303 3: unpick final auth message from client
304 4: break/finish (non-step)
309 if (!from_client || *from_client == '\0') {
310 if (handled_empty_ir) {
311 HDEBUG(D_auth) debug_printf("gssapi: repeated empty input, grr.\n");
315 HDEBUG(D_auth) debug_printf("gssapi: missing initial response, nudging.\n");
316 error_out = auth_get_data(&from_client, US"", 0);
322 /* We should now have the opening data from the client, base64-encoded. */
327 gbufdesc_in.length = auth_b64decode(from_client, USS &gbufdesc_in.value);
329 maj_stat = gss_release_name(&min_stat, &gclient);
330 gclient = GSS_C_NO_NAME;
332 maj_stat = gss_accept_sec_context(&min_stat,
333 &gcontext, /* context handle */
334 gcred, /* acceptor cred handle */
335 &gbufdesc_in, /* input from client */
336 GSS_C_NO_CHANNEL_BINDINGS, /* XXX fixme: use the channel bindings from GnuTLS */
337 &gclient, /* client identifier */
338 &mech_type, /* mechanism in use */
339 &gbufdesc_out, /* output to send to client */
340 NULL, /* return flags */
342 NULL /* delegated cred_handle */
344 if (GSS_ERROR(maj_stat)) {
345 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
346 "gss_accept_sec_context()");
350 if (&gbufdesc_out.length != 0) {
351 error_out = auth_get_data(&from_client,
352 gbufdesc_out.value, gbufdesc_out.length);
356 gss_release_buffer(&min_stat, &gbufdesc_out);
357 EmptyBuf(gbufdesc_out);
359 if (maj_stat == GSS_S_COMPLETE)
364 memset(sasl_config, 0xFF, 4);
365 /* draft-ietf-sasl-gssapi-06.txt defines bitmasks for first octet
366 0x01 No security layer
367 0x02 Integrity protection
368 0x04 Confidentiality protection
370 The remaining three octets are the maximum buffer size for wrappe
372 sasl_config[0] = 0x01; /* Exim does not wrap/unwrap SASL layers after auth */
373 gbufdesc.value = (void *) sasl_config;
375 maj_stat = gss_wrap(&min_stat,
377 0, /* conf_req_flag: integrity only */
378 GSS_C_QOP_DEFAULT, /* qop requested */
379 &gbufdesc, /* message to protect */
380 NULL, /* conf_state: no confidentiality applied */
381 &gbufdesc_out /* output buffer */
383 if (GSS_ERROR(maj_stat)) {
384 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
385 "gss_wrap(SASL state after auth)");
389 error_out = auth_get_data(&from_client,
390 gbufdesc_out.value, gbufdesc_out.length);
394 gss_release_buffer(&min_stat, &gbufdesc_out);
395 EmptyBuf(gbufdesc_out);
400 gbufdesc_in.length = auth_b64decode(from_client, USS &gbufdesc_in.value);
401 maj_stat = gss_unwrap(&min_stat,
403 &gbufdesc_in, /* data from client */
404 &gbufdesc_out, /* results */
405 NULL, /* conf state */
408 if (GSS_ERROR(maj_stat)) {
409 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
410 "gss_unwrap(final SASL message from client)");
414 if (gbufdesc_out.length < 5) {
416 debug_printf("gssapi: final message too short; "
417 "need flags, buf sizes and authzid\n");
422 requested_qop = (CS gbufdesc_out.value)[0];
423 if ((requested_qop & 0x01) == 0) {
425 debug_printf("gssapi: client requested security layers (%x)\n",
426 (unsigned int) requested_qop);
431 for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
435 The SASL provided identifier is an unverified authzid.
436 GSSAPI provides us with a verified identifier.
439 /* $auth2 is authzid requested at SASL layer */
440 expand_nlength[2] = gbufdesc_out.length - 4;
441 auth_vars[1] = expand_nstring[2] =
442 string_copyn((US gbufdesc_out.value) + 4, expand_nlength[2]);
445 gss_release_buffer(&min_stat, &gbufdesc_out);
446 EmptyBuf(gbufdesc_out);
448 /* $auth1 is GSSAPI display name */
449 maj_stat = gss_display_name(&min_stat,
453 if (GSS_ERROR(maj_stat)) {
454 auth_vars[1] = expand_nstring[2] = NULL;
456 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
457 "gss_display_name(client identifier)");
462 expand_nlength[1] = gbufdesc_out.length;
463 auth_vars[0] = expand_nstring[1] =
464 string_copyn(gbufdesc_out.value, gbufdesc_out.length);
474 maj_stat = gss_release_cred(&min_stat, &gcred);
476 gss_release_name(&min_stat, &gclient);
477 gclient = GSS_C_NO_NAME;
479 if (gbufdesc_out.length) {
480 gss_release_buffer(&min_stat, &gbufdesc_out);
481 EmptyBuf(gbufdesc_out);
483 if (gcontext != GSS_C_NO_CONTEXT) {
484 gss_delete_sec_context(&min_stat, &gcontext, GSS_C_NO_BUFFER);
487 store_reset(store_reset_point);
492 /* Auth succeeded, check server_condition */
493 return auth_check_serv_cond(ablock);
498 exim_gssapi_error_defer(uschar *store_reset_point,
499 OM_uint32 major, OM_uint32 minor,
500 const char *format, ...)
503 uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
504 OM_uint32 maj_stat, min_stat;
505 OM_uint32 msgcontext = 0;
506 gss_buffer_desc status_string;
508 va_start(ap, format);
509 if (!string_vformat(buffer, sizeof(buffer), format, ap))
510 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
511 "exim_gssapi_error_defer expansion larger than %d",
515 auth_defer_msg = NULL;
518 maj_stat = gss_display_status(&min_stat,
519 major, GSS_C_GSS_CODE, GSS_C_NO_OID,
520 &msgcontext, &status_string);
522 if (auth_defer_msg == NULL) {
523 auth_defer_msg = string_copy(US status_string.value);
526 HDEBUG(D_auth) debug_printf("heimdal %s: %.*s\n",
527 buffer, (int)status_string.length, CS status_string.value);
528 gss_release_buffer(&min_stat, &status_string);
530 } while (msgcontext != 0);
532 if (store_reset_point)
533 store_reset(store_reset_point);
538 /*************************************************
539 * Client entry point *
540 *************************************************/
542 /* For interface, see auths/README */
545 auth_heimdal_gssapi_client(
546 auth_instance *ablock, /* authenticator block */
547 smtp_inblock *inblock, /* connection inblock */
548 smtp_outblock *outblock, /* connection outblock */
549 int timeout, /* command timeout */
550 uschar *buffer, /* buffer for reading response */
551 int buffsize) /* size of buffer */
554 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
555 /* NOT IMPLEMENTED */
559 /*************************************************
561 *************************************************/
564 auth_heimdal_gssapi_version_report(FILE *f)
566 /* No build-time constants available unless we link against libraries at
567 build-time and export the result as a string into a header ourselves. */
568 fprintf(f, "Library version: Heimdal: Runtime: %s\n"
570 heimdal_version, heimdal_long_version);
573 #endif /* AUTH_HEIMDAL_GSSAPI */
575 /* End of heimdal_gssapi.c */