523f7c69a8a505fd2b469533f9cb2c28a3372066
[exim.git] / src / src / auths / heimdal_gssapi.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 /* Copyright (c) Twitter Inc 2012
9 Author: Phil Pennock <pdp@exim.org> */
10 /* Copyright (c) Phil Pennock 2012 */
11
12 /* Interface to Heimdal library for GSSAPI authentication. */
13
14 /* Naming and rationale
15
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
19 Kerberos.
20
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.
26
27 Without rename, we could add an option for GS2 support in the future.
28 */
29
30 /* Sources
31
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; however,
38 after multiple attempts I failed to get that to work and instead switched to
39 gsskrb5_register_acceptor_identity().
40 */
41
42 #include "../exim.h"
43
44 #ifndef AUTH_HEIMDAL_GSSAPI
45 /* dummy function to satisfy compilers when we link in an "empty" file. */
46 static void dummy(int x);
47 static void dummy2(int x) { dummy(x-1); }
48 static void dummy(int x) { dummy2(x-1); }
49 #else
50
51 #include <gssapi/gssapi.h>
52 #include <gssapi/gssapi_krb5.h>
53
54 /* for the _init debugging */
55 #include <krb5.h>
56
57 #include "heimdal_gssapi.h"
58
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_service", opt_stringptr,
66 (void *)(offsetof(auth_heimdal_gssapi_options_block, server_service)) }
67 };
68
69 int auth_heimdal_gssapi_options_count =
70 sizeof(auth_heimdal_gssapi_options)/sizeof(optionlist);
71
72 /* Defaults for the authenticator-specific options. */
73 auth_heimdal_gssapi_options_block auth_heimdal_gssapi_option_defaults = {
74 US"$primary_hostname", /* server_hostname */
75 NULL, /* server_keytab */
76 US"smtp", /* server_service */
77 };
78
79
80 #ifdef MACRO_PREDEF
81
82 /* Dummy values */
83 void auth_heimdal_gssapi_init(auth_instance *ablock) {}
84 int auth_heimdal_gssapi_server(auth_instance *ablock, uschar *data) {return 0;}
85 int auth_heimdal_gssapi_client(auth_instance *ablock, void * sx,
86 int timeout, uschar *buffer, int buffsize) {return 0;}
87 void auth_heimdal_gssapi_version_report(FILE *f) {}
88
89 #else /*!MACRO_PREDEF*/
90
91
92
93 /* "Globals" for managing the heimdal_gssapi interface. */
94
95 /* Utility functions */
96 static void
97 exim_heimdal_error_debug(const char *, krb5_context, krb5_error_code);
98 static int
99 exim_gssapi_error_defer(rmark, OM_uint32, OM_uint32, const char *, ...)
100 PRINTF_FUNCTION(4, 5);
101
102 #define EmptyBuf(buf) do { buf.value = NULL; buf.length = 0; } while (0)
103
104
105 /*************************************************
106 * Initialization entry point *
107 *************************************************/
108
109 /* Called for each instance, after its options have been read, to
110 enable consistency checks to be done, or anything else that needs
111 to be set up. */
112
113 /* Heimdal provides a GSSAPI extension method for setting the keytab;
114 in the init, we mostly just use raw krb5 methods so that we can report
115 the keytab contents, for -D+auth debugging. */
116
117 void
118 auth_heimdal_gssapi_init(auth_instance *ablock)
119 {
120 krb5_context context;
121 krb5_keytab keytab;
122 krb5_kt_cursor cursor;
123 krb5_keytab_entry entry;
124 krb5_error_code krc;
125 char *principal, *enctype_s;
126 const char *k_keytab_typed_name = NULL;
127 auth_heimdal_gssapi_options_block *ob =
128 (auth_heimdal_gssapi_options_block *)(ablock->options_block);
129
130 ablock->server = FALSE;
131 ablock->client = FALSE;
132
133 if (!ob->server_service || !*ob->server_service)
134 {
135 HDEBUG(D_auth) debug_printf("heimdal: missing server_service\n");
136 return;
137 }
138
139 krc = krb5_init_context(&context);
140 if (krc != 0)
141 {
142 int kerr = errno;
143 HDEBUG(D_auth) debug_printf("heimdal: failed to initialise krb5 context: %s\n",
144 strerror(kerr));
145 return;
146 }
147
148 if (ob->server_keytab)
149 {
150 k_keytab_typed_name = CCS string_sprintf("file:%s", expand_string(ob->server_keytab));
151 HDEBUG(D_auth) debug_printf("heimdal: using keytab %s\n", k_keytab_typed_name);
152 krc = krb5_kt_resolve(context, k_keytab_typed_name, &keytab);
153 if (krc)
154 {
155 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_resolve", context, krc);
156 return;
157 }
158 }
159 else
160 {
161 HDEBUG(D_auth) debug_printf("heimdal: using system default keytab\n");
162 krc = krb5_kt_default(context, &keytab);
163 if (krc)
164 {
165 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_default", context, krc);
166 return;
167 }
168 }
169
170 HDEBUG(D_auth)
171 {
172 /* http://www.h5l.org/manual/HEAD/krb5/krb5_keytab_intro.html */
173 krc = krb5_kt_start_seq_get(context, keytab, &cursor);
174 if (krc)
175 exim_heimdal_error_debug("krb5_kt_start_seq_get", context, krc);
176 else
177 {
178 while ((krc = krb5_kt_next_entry(context, keytab, &entry, &cursor)) == 0)
179 {
180 principal = enctype_s = NULL;
181 krb5_unparse_name(context, entry.principal, &principal);
182 krb5_enctype_to_string(context, entry.keyblock.keytype, &enctype_s);
183 debug_printf("heimdal: keytab principal: %s vno=%d type=%s\n",
184 principal ? principal : "??",
185 entry.vno,
186 enctype_s ? enctype_s : "??");
187 free(principal);
188 free(enctype_s);
189 krb5_kt_free_entry(context, &entry);
190 }
191 krc = krb5_kt_end_seq_get(context, keytab, &cursor);
192 if (krc)
193 exim_heimdal_error_debug("krb5_kt_end_seq_get", context, krc);
194 }
195 }
196
197 krc = krb5_kt_close(context, keytab);
198 if (krc)
199 HDEBUG(D_auth) exim_heimdal_error_debug("krb5_kt_close", context, krc);
200
201 krb5_free_context(context);
202
203 ablock->server = TRUE;
204 }
205
206
207 static void
208 exim_heimdal_error_debug(const char *label,
209 krb5_context context, krb5_error_code err)
210 {
211 const char *kerrsc;
212 kerrsc = krb5_get_error_message(context, err);
213 debug_printf("heimdal %s: %s\n", label, kerrsc ? kerrsc : "unknown error");
214 krb5_free_error_message(context, kerrsc);
215 }
216
217 /*************************************************
218 * Server entry point *
219 *************************************************/
220
221 /* For interface, see auths/README */
222
223 /* GSSAPI notes:
224 OM_uint32: portable type for unsigned int32
225 gss_buffer_desc / *gss_buffer_t: hold/point-to size_t .length & void *value
226 -- all strings/etc passed in should go through one of these
227 -- when allocated by gssapi, release with gss_release_buffer()
228 */
229
230 int
231 auth_heimdal_gssapi_server(auth_instance *ablock, uschar *initial_data)
232 {
233 gss_name_t gclient = GSS_C_NO_NAME;
234 gss_name_t gserver = GSS_C_NO_NAME;
235 gss_cred_id_t gcred = GSS_C_NO_CREDENTIAL;
236 gss_ctx_id_t gcontext = GSS_C_NO_CONTEXT;
237 uschar *ex_server_str;
238 gss_buffer_desc gbufdesc = GSS_C_EMPTY_BUFFER;
239 gss_buffer_desc gbufdesc_in = GSS_C_EMPTY_BUFFER;
240 gss_buffer_desc gbufdesc_out = GSS_C_EMPTY_BUFFER;
241 gss_OID mech_type;
242 OM_uint32 maj_stat, min_stat;
243 int step, error_out;
244 uschar *tmp1, *tmp2, *from_client;
245 auth_heimdal_gssapi_options_block *ob =
246 (auth_heimdal_gssapi_options_block *)(ablock->options_block);
247 BOOL handled_empty_ir;
248 rmark store_reset_point;
249 uschar *keytab;
250 uschar sasl_config[4];
251 uschar requested_qop;
252
253 store_reset_point = store_mark();
254
255 HDEBUG(D_auth)
256 debug_printf("heimdal: initialising auth context for %s\n", ablock->name);
257
258 /* Construct our gss_name_t gserver describing ourselves */
259 tmp1 = expand_string(ob->server_service);
260 tmp2 = expand_string(ob->server_hostname);
261 ex_server_str = string_sprintf("%s@%s", tmp1, tmp2);
262 gbufdesc.value = (void *) ex_server_str;
263 gbufdesc.length = Ustrlen(ex_server_str);
264 maj_stat = gss_import_name(&min_stat,
265 &gbufdesc, GSS_C_NT_HOSTBASED_SERVICE, &gserver);
266 if (GSS_ERROR(maj_stat))
267 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
268 "gss_import_name(%s)", CS gbufdesc.value);
269
270 /* Use a specific keytab, if specified */
271 if (ob->server_keytab)
272 {
273 keytab = expand_string(ob->server_keytab);
274 maj_stat = gsskrb5_register_acceptor_identity(CCS keytab);
275 if (GSS_ERROR(maj_stat))
276 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
277 "registering keytab \"%s\"", keytab);
278 HDEBUG(D_auth)
279 debug_printf("heimdal: using keytab \"%s\"\n", keytab);
280 }
281
282 /* Acquire our credentials */
283 maj_stat = gss_acquire_cred(&min_stat,
284 gserver, /* desired name */
285 0, /* time */
286 GSS_C_NULL_OID_SET, /* desired mechs */
287 GSS_C_ACCEPT, /* cred usage */
288 &gcred, /* handle */
289 NULL /* actual mechs */,
290 NULL /* time rec */);
291 if (GSS_ERROR(maj_stat))
292 return exim_gssapi_error_defer(store_reset_point, maj_stat, min_stat,
293 "gss_acquire_cred(%s)", ex_server_str);
294
295 maj_stat = gss_release_name(&min_stat, &gserver);
296
297 HDEBUG(D_auth) debug_printf("heimdal: have server credentials.\n");
298
299 /* Loop talking to client */
300 step = 0;
301 from_client = initial_data;
302 handled_empty_ir = FALSE;
303 error_out = OK;
304
305 /* buffer sizes: auth_get_data() uses big_buffer, which we grow per
306 GSSAPI RFC in _init, if needed, to meet the SHOULD size of 64KB.
307 (big_buffer starts life at the MUST size of 16KB). */
308
309 /* step values
310 0: getting initial data from client to feed into GSSAPI
311 1: iterating for as long as GSS_S_CONTINUE_NEEDED
312 2: GSS_S_COMPLETE, SASL wrapping for authz and qop to send to client
313 3: unpick final auth message from client
314 4: break/finish (non-step)
315 */
316 while (step < 4)
317 switch (step)
318 {
319 case 0:
320 if (!from_client || *from_client == '\0')
321 {
322 if (handled_empty_ir)
323 {
324 HDEBUG(D_auth) debug_printf("gssapi: repeated empty input, grr.\n");
325 error_out = BAD64;
326 goto ERROR_OUT;
327 }
328 else
329 {
330 HDEBUG(D_auth) debug_printf("gssapi: missing initial response, nudging.\n");
331 error_out = auth_get_data(&from_client, US"", 0);
332 if (error_out != OK)
333 goto ERROR_OUT;
334 handled_empty_ir = TRUE;
335 continue;
336 }
337 }
338 /* We should now have the opening data from the client, base64-encoded. */
339 step += 1;
340 HDEBUG(D_auth) debug_printf("heimdal: have initial client data\n");
341 break;
342
343 case 1:
344 gbufdesc_in.length = b64decode(from_client, USS &gbufdesc_in.value);
345 if (gclient)
346 {
347 maj_stat = gss_release_name(&min_stat, &gclient);
348 gclient = GSS_C_NO_NAME;
349 }
350 maj_stat = gss_accept_sec_context(&min_stat,
351 &gcontext, /* context handle */
352 gcred, /* acceptor cred handle */
353 &gbufdesc_in, /* input from client */
354 GSS_C_NO_CHANNEL_BINDINGS, /* XXX fixme: use the channel bindings from GnuTLS */
355 &gclient, /* client identifier */
356 &mech_type, /* mechanism in use */
357 &gbufdesc_out, /* output to send to client */
358 NULL, /* return flags */
359 NULL, /* time rec */
360 NULL /* delegated cred_handle */
361 );
362 if (GSS_ERROR(maj_stat))
363 {
364 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
365 "gss_accept_sec_context()");
366 error_out = FAIL;
367 goto ERROR_OUT;
368 }
369 if (gbufdesc_out.length != 0)
370 {
371 error_out = auth_get_data(&from_client,
372 gbufdesc_out.value, gbufdesc_out.length);
373 if (error_out != OK)
374 goto ERROR_OUT;
375
376 gss_release_buffer(&min_stat, &gbufdesc_out);
377 EmptyBuf(gbufdesc_out);
378 }
379 if (maj_stat == GSS_S_COMPLETE)
380 {
381 step += 1;
382 HDEBUG(D_auth) debug_printf("heimdal: GSS complete\n");
383 }
384 else
385 HDEBUG(D_auth) debug_printf("heimdal: need more data\n");
386 break;
387
388 case 2:
389 memset(sasl_config, 0xFF, 4);
390 /* draft-ietf-sasl-gssapi-06.txt defines bitmasks for first octet
391 0x01 No security layer
392 0x02 Integrity protection
393 0x04 Confidentiality protection
394
395 The remaining three octets are the maximum buffer size for wrapped
396 content. */
397 sasl_config[0] = 0x01; /* Exim does not wrap/unwrap SASL layers after auth */
398 gbufdesc.value = (void *) sasl_config;
399 gbufdesc.length = 4;
400 maj_stat = gss_wrap(&min_stat,
401 gcontext,
402 0, /* conf_req_flag: integrity only */
403 GSS_C_QOP_DEFAULT, /* qop requested */
404 &gbufdesc, /* message to protect */
405 NULL, /* conf_state: no confidentiality applied */
406 &gbufdesc_out /* output buffer */
407 );
408 if (GSS_ERROR(maj_stat))
409 {
410 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
411 "gss_wrap(SASL state after auth)");
412 error_out = FAIL;
413 goto ERROR_OUT;
414 }
415
416 HDEBUG(D_auth) debug_printf("heimdal SASL: requesting QOP with no security layers\n");
417
418 error_out = auth_get_data(&from_client,
419 gbufdesc_out.value, gbufdesc_out.length);
420 if (error_out != OK)
421 goto ERROR_OUT;
422
423 gss_release_buffer(&min_stat, &gbufdesc_out);
424 EmptyBuf(gbufdesc_out);
425 step += 1;
426 break;
427
428 case 3:
429 gbufdesc_in.length = b64decode(from_client, USS &gbufdesc_in.value);
430 maj_stat = gss_unwrap(&min_stat,
431 gcontext,
432 &gbufdesc_in, /* data from client */
433 &gbufdesc_out, /* results */
434 NULL, /* conf state */
435 NULL /* qop state */
436 );
437 if (GSS_ERROR(maj_stat))
438 {
439 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
440 "gss_unwrap(final SASL message from client)");
441 error_out = FAIL;
442 goto ERROR_OUT;
443 }
444 if (gbufdesc_out.length < 4)
445 {
446 HDEBUG(D_auth)
447 debug_printf("gssapi: final message too short; "
448 "need flags, buf sizes and optional authzid\n");
449 error_out = FAIL;
450 goto ERROR_OUT;
451 }
452
453 requested_qop = (CS gbufdesc_out.value)[0];
454 if ((requested_qop & 0x01) == 0)
455 {
456 HDEBUG(D_auth)
457 debug_printf("gssapi: client requested security layers (%x)\n",
458 (unsigned int) requested_qop);
459 error_out = FAIL;
460 goto ERROR_OUT;
461 }
462
463 for (int i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
464 expand_nmax = 0;
465
466 /* Identifiers:
467 The SASL provided identifier is an unverified authzid.
468 GSSAPI provides us with a verified identifier, but it might be empty
469 for some clients.
470 */
471
472 /* $auth2 is authzid requested at SASL layer */
473 if (gbufdesc_out.length > 4)
474 {
475 expand_nlength[2] = gbufdesc_out.length - 4;
476 auth_vars[1] = expand_nstring[2] =
477 string_copyn((US gbufdesc_out.value) + 4, expand_nlength[2]);
478 expand_nmax = 2;
479 }
480
481 gss_release_buffer(&min_stat, &gbufdesc_out);
482 EmptyBuf(gbufdesc_out);
483
484 /* $auth1 is GSSAPI display name */
485 maj_stat = gss_display_name(&min_stat,
486 gclient,
487 &gbufdesc_out,
488 &mech_type);
489 if (GSS_ERROR(maj_stat))
490 {
491 auth_vars[1] = expand_nstring[2] = NULL;
492 expand_nmax = 0;
493 exim_gssapi_error_defer(NULL, maj_stat, min_stat,
494 "gss_display_name(client identifier)");
495 error_out = FAIL;
496 goto ERROR_OUT;
497 }
498
499 expand_nlength[1] = gbufdesc_out.length;
500 auth_vars[0] = expand_nstring[1] =
501 string_copyn(gbufdesc_out.value, gbufdesc_out.length);
502
503 if (expand_nmax == 0) /* should be: authzid was empty */
504 {
505 expand_nmax = 2;
506 expand_nlength[2] = expand_nlength[1];
507 auth_vars[1] = expand_nstring[2] = string_copyn(expand_nstring[1], expand_nlength[1]);
508 HDEBUG(D_auth)
509 debug_printf("heimdal SASL: empty authzid, set to dup of GSSAPI display name\n");
510 }
511
512 HDEBUG(D_auth)
513 debug_printf("heimdal SASL: happy with client request\n"
514 " auth1 (verified GSSAPI display-name): \"%s\"\n"
515 " auth2 (unverified SASL requested authzid): \"%s\"\n",
516 auth_vars[0], auth_vars[1]);
517
518 step += 1;
519 break;
520
521 } /* switch */
522 /* while step */
523
524
525 ERROR_OUT:
526 maj_stat = gss_release_cred(&min_stat, &gcred);
527 if (gclient)
528 {
529 gss_release_name(&min_stat, &gclient);
530 gclient = GSS_C_NO_NAME;
531 }
532 if (gbufdesc_out.length)
533 {
534 gss_release_buffer(&min_stat, &gbufdesc_out);
535 EmptyBuf(gbufdesc_out);
536 }
537 if (gcontext != GSS_C_NO_CONTEXT)
538 gss_delete_sec_context(&min_stat, &gcontext, GSS_C_NO_BUFFER);
539
540 store_reset(store_reset_point);
541
542 if (error_out != OK)
543 return error_out;
544
545 /* Auth succeeded, check server_condition */
546 return auth_check_serv_cond(ablock);
547 }
548
549
550 static int
551 exim_gssapi_error_defer(rmark store_reset_point,
552 OM_uint32 major, OM_uint32 minor,
553 const char *format, ...)
554 {
555 va_list ap;
556 OM_uint32 maj_stat, min_stat;
557 OM_uint32 msgcontext = 0;
558 gss_buffer_desc status_string;
559 gstring * g;
560
561 HDEBUG(D_auth)
562 {
563 va_start(ap, format);
564 g = string_vformat(NULL, SVFMT_EXTEND|SVFMT_REBUFFER, format, ap);
565 va_end(ap);
566 }
567
568 auth_defer_msg = NULL;
569
570 do {
571 maj_stat = gss_display_status(&min_stat,
572 major, GSS_C_GSS_CODE, GSS_C_NO_OID, &msgcontext, &status_string);
573
574 if (!auth_defer_msg)
575 auth_defer_msg = string_copy(US status_string.value);
576
577 HDEBUG(D_auth) debug_printf("heimdal %s: %.*s\n",
578 string_from_gstring(g), (int)status_string.length,
579 CS status_string.value);
580 gss_release_buffer(&min_stat, &status_string);
581
582 } while (msgcontext != 0);
583
584 if (store_reset_point)
585 store_reset(store_reset_point);
586 return DEFER;
587 }
588
589
590 /*************************************************
591 * Client entry point *
592 *************************************************/
593
594 /* For interface, see auths/README */
595
596 int
597 auth_heimdal_gssapi_client(
598 auth_instance *ablock, /* authenticator block */
599 void * sx, /* connection */
600 int timeout, /* command timeout */
601 uschar *buffer, /* buffer for reading response */
602 int buffsize) /* size of buffer */
603 {
604 HDEBUG(D_auth)
605 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
606 /* NOT IMPLEMENTED */
607 return FAIL;
608 }
609
610 /*************************************************
611 * Diagnostic API *
612 *************************************************/
613
614 void
615 auth_heimdal_gssapi_version_report(FILE *f)
616 {
617 /* No build-time constants available unless we link against libraries at
618 build-time and export the result as a string into a header ourselves. */
619 fprintf(f, "Library version: Heimdal: Runtime: %s\n"
620 " Build Info: %s\n",
621 heimdal_version, heimdal_long_version);
622 }
623
624 #endif /*!MACRO_PREDEF*/
625 #endif /* AUTH_HEIMDAL_GSSAPI */
626
627 /* End of heimdal_gssapi.c */