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 GNU SASL library for generic authentication. */
16 GNU SASL does not provide authentication data itself, so we have to expose
17 that decision to configuration. For some mechanisms, we need to act much
18 like plaintext. For others, we only need to be able to provide some
19 evaluated data on demand. There's no abstracted way (ie, without hardcoding
20 knowledge of authenticators here) to know which need what properties; we
21 can't query a session or the library for "we will need these for mechanism X".
23 So: we always require server_condition, even if sometimes it will just be
24 set as "yes". We do provide a number of other hooks, which might not make
25 sense in all contexts. For some, we can do checks at init time.
31 /* dummy function to satisfy compilers when we link in an "empty" file. */
32 static void dummy(int x);
33 static void dummy2(int x) { dummy(x-1); }
34 static void dummy(int x) { dummy2(x-1); }
38 #include "gsasl_exim.h"
40 /* Authenticator-specific options. */
41 /* I did have server_*_condition options for various mechanisms, but since
42 we only ever handle one mechanism at a time, I didn't see the point in keeping
43 that. In case someone sees a point, I've left the condition_check() API
45 optionlist auth_gsasl_options[] = {
46 { "server_channelbinding", opt_bool,
47 (void *)(offsetof(auth_gsasl_options_block, server_channelbinding)) },
48 { "server_hostname", opt_stringptr,
49 (void *)(offsetof(auth_gsasl_options_block, server_hostname)) },
50 { "server_mech", opt_stringptr,
51 (void *)(offsetof(auth_gsasl_options_block, server_mech)) },
52 { "server_password", opt_stringptr,
53 (void *)(offsetof(auth_gsasl_options_block, server_password)) },
54 { "server_realm", opt_stringptr,
55 (void *)(offsetof(auth_gsasl_options_block, server_realm)) },
56 { "server_scram_iter", opt_stringptr,
57 (void *)(offsetof(auth_gsasl_options_block, server_scram_iter)) },
58 { "server_scram_salt", opt_stringptr,
59 (void *)(offsetof(auth_gsasl_options_block, server_scram_salt)) },
60 { "server_service", opt_stringptr,
61 (void *)(offsetof(auth_gsasl_options_block, server_service)) }
63 /* GSASL_SCRAM_SALTED_PASSWORD documented only for client, so not implementing
64 hooks to avoid cleartext passwords in the Exim server. */
66 int auth_gsasl_options_count =
67 sizeof(auth_gsasl_options)/sizeof(optionlist);
69 /* Defaults for the authenticator-specific options. */
70 auth_gsasl_options_block auth_gsasl_option_defaults = {
71 US"smtp", /* server_service */
72 US"$primary_hostname", /* server_hostname */
73 NULL, /* server_realm */
74 NULL, /* server_mech */
75 NULL, /* server_password */
76 NULL, /* server_scram_iter */
77 NULL, /* server_scram_salt */
78 FALSE /* server_channelbinding */
85 void auth_gsasl_init(auth_instance *ablock) {}
86 int auth_gsasl_server(auth_instance *ablock, uschar *data) {return 0;}
87 int auth_gsasl_client(auth_instance *ablock, smtp_inblock *inblock,
88 smtp_outblock *outblock, int timeout, uschar *buffer, int buffsize) {return 0;}
90 #else /*!MACRO_PREDEF*/
94 /* "Globals" for managing the gsasl interface. */
96 static Gsasl *gsasl_ctx = NULL;
98 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop);
100 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
102 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
104 static BOOL sasl_error_should_defer = FALSE;
105 static Gsasl_property callback_loop = 0;
106 static BOOL checked_server_condition = FALSE;
108 enum { CURRENTLY_SERVER = 1, CURRENTLY_CLIENT = 2 };
110 struct callback_exim_state {
111 auth_instance *ablock;
116 /*************************************************
117 * Initialization entry point *
118 *************************************************/
120 /* Called for each instance, after its options have been read, to
121 enable consistency checks to be done, or anything else that needs
125 auth_gsasl_init(auth_instance *ablock)
129 auth_gsasl_options_block *ob =
130 (auth_gsasl_options_block *)(ablock->options_block);
132 /* As per existing Cyrus glue, use the authenticator's public name as
133 the default for the mechanism name; we don't handle multiple mechanisms
134 in one authenticator, but the same driver can be used multiple times. */
136 if (ob->server_mech == NULL)
137 ob->server_mech = string_copy(ablock->public_name);
139 /* Can get multiple session contexts from one library context, so just
140 initialise the once. */
141 if (gsasl_ctx == NULL) {
142 rc = gsasl_init(&gsasl_ctx);
143 if (rc != GSASL_OK) {
144 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
145 "couldn't initialise GNU SASL library: %s (%s)",
146 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
148 gsasl_callback_set(gsasl_ctx, main_callback);
151 /* We don't need this except to log it for debugging. */
152 rc = gsasl_server_mechlist(gsasl_ctx, &p);
154 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
155 "failed to retrieve list of mechanisms: %s (%s)",
156 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
157 HDEBUG(D_auth) debug_printf("GNU SASL supports: %s\n", p);
159 supported = gsasl_client_support_p(gsasl_ctx, (const char *)ob->server_mech);
161 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
162 "GNU SASL does not support mechanism \"%s\"",
163 ablock->name, ob->server_mech);
165 if ((ablock->server_condition == NULL) &&
166 (streqic(ob->server_mech, US"EXTERNAL") ||
167 streqic(ob->server_mech, US"ANONYMOUS") ||
168 streqic(ob->server_mech, US"PLAIN") ||
169 streqic(ob->server_mech, US"LOGIN")))
170 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
171 "Need server_condition for %s mechanism",
172 ablock->name, ob->server_mech);
174 /* This does *not* scale to new SASL mechanisms. Need a better way to ask
175 which properties will be needed. */
176 if ((ob->server_realm == NULL) &&
177 streqic(ob->server_mech, US"DIGEST-MD5"))
178 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
179 "Need server_realm for %s mechanism",
180 ablock->name, ob->server_mech);
182 /* At present, for mechanisms we don't panic on absence of server_condition;
183 need to figure out the most generically correct approach to deciding when
184 it's critical and when it isn't. Eg, for simple validation (PLAIN mechanism,
185 etc) it clearly is critical.
187 So don't activate without server_condition, this might be relaxed in the future.
189 if (ablock->server_condition != NULL) ablock->server = TRUE;
190 ablock->client = FALSE;
194 /* GNU SASL uses one top-level callback, registered at library level.
195 We dispatch to client and server functions instead. */
198 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
201 struct callback_exim_state *cb_state =
202 (struct callback_exim_state *)gsasl_session_hook_get(sctx);
205 debug_printf("GNU SASL Callback entered, prop=%d (loop prop=%d)\n",
206 prop, callback_loop);
208 if (cb_state == NULL) {
209 HDEBUG(D_auth) debug_printf(" not from our server/client processing.\n");
210 return GSASL_NO_CALLBACK;
213 if (callback_loop > 0) {
214 /* Most likely is that we were asked for property foo, and to
215 expand the string we asked for property bar to put into an auth
216 variable, but property bar is not supplied for this mechanism. */
218 debug_printf("Loop, asked for property %d while handling property %d\n",
219 prop, callback_loop);
220 return GSASL_NO_CALLBACK;
222 callback_loop = prop;
224 if (cb_state->currently == CURRENTLY_CLIENT)
225 rc = client_callback(ctx, sctx, prop, cb_state->ablock);
226 else if (cb_state->currently == CURRENTLY_SERVER)
227 rc = server_callback(ctx, sctx, prop, cb_state->ablock);
229 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
230 "unhandled callback state, bug in Exim", cb_state->ablock->name);
239 /*************************************************
240 * Server entry point *
241 *************************************************/
243 /* For interface, see auths/README */
246 auth_gsasl_server(auth_instance *ablock, uschar *initial_data)
249 char *to_send, *received;
250 Gsasl_session *sctx = NULL;
251 auth_gsasl_options_block *ob =
252 (auth_gsasl_options_block *)(ablock->options_block);
253 struct callback_exim_state cb_state;
254 int rc, auth_result, exim_error, exim_error_override;
257 debug_printf("GNU SASL: initialising session for %s, mechanism %s.\n",
258 ablock->name, ob->server_mech);
260 rc = gsasl_server_start(gsasl_ctx, (const char *)ob->server_mech, &sctx);
261 if (rc != GSASL_OK) {
262 auth_defer_msg = string_sprintf("GNU SASL: session start failure: %s (%s)",
263 gsasl_strerror_name(rc), gsasl_strerror(rc));
264 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
267 /* Hereafter: gsasl_finish(sctx) please */
269 gsasl_session_hook_set(sctx, (void *)ablock);
270 cb_state.ablock = ablock;
271 cb_state.currently = CURRENTLY_SERVER;
272 gsasl_session_hook_set(sctx, (void *)&cb_state);
274 tmps = CS expand_string(ob->server_service);
275 gsasl_property_set(sctx, GSASL_SERVICE, tmps);
276 tmps = CS expand_string(ob->server_hostname);
277 gsasl_property_set(sctx, GSASL_HOSTNAME, tmps);
278 if (ob->server_realm) {
279 tmps = CS expand_string(ob->server_realm);
281 gsasl_property_set(sctx, GSASL_REALM, tmps);
284 /* We don't support protection layers. */
285 gsasl_property_set(sctx, GSASL_QOPS, "qop-auth");
287 if (tls_channelbinding_b64) {
288 /* Some auth mechanisms can ensure that both sides are talking withing the
289 same security context; for TLS, this means that even if a bad certificate
290 has been accepted, they remain MitM-proof because both sides must be within
291 the same negotiated session; if someone is terminating one session and
292 proxying data on within a second, authentication will fail.
294 We might not have this available, depending upon TLS implementation,
295 ciphersuite, phase of moon ...
297 If we do, it results in extra SASL mechanisms being available; here,
298 Exim's one-mechanism-per-authenticator potentially causes problems.
299 It depends upon how GNU SASL will implement the PLUS variants of GS2
300 and whether it automatically mandates a switch to the bound PLUS
301 if the data is available. Since default-on, despite being more secure,
302 would then result in mechanism name changes on a library update, we
303 have little choice but to default it off and let the admin choose to
306 if (ob->server_channelbinding) {
307 HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
309 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE,
310 (const char *) tls_channelbinding_b64);
313 debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
318 debug_printf("Auth %s: no channel-binding data available\n",
323 checked_server_condition = FALSE;
325 received = CS initial_data;
327 exim_error = exim_error_override = OK;
330 rc = gsasl_step64(sctx, received, &to_send);
335 goto STOP_INTERACTION;
338 case GSASL_NEEDS_MORE:
341 case GSASL_AUTHENTICATION_ERROR:
342 case GSASL_INTEGRITY_ERROR:
343 case GSASL_NO_AUTHID:
344 case GSASL_NO_ANONYMOUS_TOKEN:
345 case GSASL_NO_AUTHZID:
346 case GSASL_NO_PASSWORD:
347 case GSASL_NO_PASSCODE:
349 case GSASL_BASE64_ERROR:
350 HDEBUG(D_auth) debug_printf("GNU SASL permanent error: %s (%s)\n",
351 gsasl_strerror_name(rc), gsasl_strerror(rc));
352 log_write(0, LOG_REJECT, "%s authenticator (%s):\n "
353 "GNU SASL permanent failure: %s (%s)",
354 ablock->name, ob->server_mech,
355 gsasl_strerror_name(rc), gsasl_strerror(rc));
356 if (rc == GSASL_BASE64_ERROR)
357 exim_error_override = BAD64;
358 goto STOP_INTERACTION;
361 auth_defer_msg = string_sprintf("GNU SASL temporary error: %s (%s)",
362 gsasl_strerror_name(rc), gsasl_strerror(rc));
363 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
364 exim_error_override = DEFER;
365 goto STOP_INTERACTION;
368 if ((rc == GSASL_NEEDS_MORE) ||
369 (to_send && *to_send))
371 auth_get_no64_data((uschar **)&received, (uschar *)to_send);
379 break; /* handles * cancelled check */
381 } while (rc == GSASL_NEEDS_MORE);
388 /* Can return: OK DEFER FAIL CANCELLED BAD64 UNEXPECTED */
390 if (exim_error != OK)
393 if (auth_result != GSASL_OK) {
394 HDEBUG(D_auth) debug_printf("authentication returned %s (%s)\n",
395 gsasl_strerror_name(auth_result), gsasl_strerror(auth_result));
396 if (exim_error_override != OK)
397 return exim_error_override; /* might be DEFER */
398 if (sasl_error_should_defer) /* overriding auth failure SASL error */
403 /* Auth succeeded, check server_condition unless already done in callback */
404 return checked_server_condition ? OK : auth_check_serv_cond(ablock);
407 /* returns the GSASL status of expanding the Exim string given */
409 condition_check(auth_instance *ablock, uschar *label, uschar *condition_string)
413 exim_rc = auth_check_some_cond(ablock, label, condition_string, FAIL);
417 } else if (exim_rc == DEFER) {
418 sasl_error_should_defer = TRUE;
419 return GSASL_AUTHENTICATION_ERROR;
420 } else if (exim_rc == FAIL) {
421 return GSASL_AUTHENTICATION_ERROR;
424 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
425 "Unhandled return from checking %s: %d",
426 ablock->name, label, exim_rc);
428 return GSASL_AUTHENTICATION_ERROR;
432 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
436 int cbrc = GSASL_NO_CALLBACK;
438 auth_gsasl_options_block *ob =
439 (auth_gsasl_options_block *)(ablock->options_block);
442 debug_printf("GNU SASL callback %d for %s/%s as server\n",
443 prop, ablock->name, ablock->public_name);
445 for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
449 case GSASL_VALIDATE_SIMPLE:
450 /* GSASL_AUTHID, GSASL_AUTHZID, and GSASL_PASSWORD */
451 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
452 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
453 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
454 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
455 propval = (uschar *) gsasl_property_fast(sctx, GSASL_PASSWORD);
456 auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
458 for (i = 1; i <= 3; ++i)
459 expand_nlength[i] = Ustrlen(expand_nstring[i]);
461 cbrc = condition_check(ablock, US"server_condition", ablock->server_condition);
462 checked_server_condition = TRUE;
465 case GSASL_VALIDATE_EXTERNAL:
466 if (ablock->server_condition == NULL) {
467 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate EXTERNAL.\n");
468 cbrc = GSASL_AUTHENTICATION_ERROR;
471 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
472 /* We always set $auth1, even if only to empty string. */
473 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
474 expand_nlength[1] = Ustrlen(expand_nstring[1]);
477 cbrc = condition_check(ablock,
478 US"server_condition (EXTERNAL)", ablock->server_condition);
479 checked_server_condition = TRUE;
482 case GSASL_VALIDATE_ANONYMOUS:
483 if (ablock->server_condition == NULL) {
484 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate ANONYMOUS.\n");
485 cbrc = GSASL_AUTHENTICATION_ERROR;
488 propval = (uschar *) gsasl_property_fast(sctx, GSASL_ANONYMOUS_TOKEN);
489 /* We always set $auth1, even if only to empty string. */
490 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
491 expand_nlength[1] = Ustrlen(expand_nstring[1]);
494 cbrc = condition_check(ablock,
495 US"server_condition (ANONYMOUS)", ablock->server_condition);
496 checked_server_condition = TRUE;
499 case GSASL_VALIDATE_GSSAPI:
500 /* GSASL_AUTHZID and GSASL_GSSAPI_DISPLAY_NAME
501 The display-name is authenticated as part of GSS, the authzid is claimed
502 by the SASL integration after authentication; protected against tampering
503 (if the SASL mechanism supports that, which Kerberos does) but is
504 unverified, same as normal for other mechanisms.
506 First coding, we had these values swapped, but for consistency and prior
507 to the first release of Exim with this authenticator, they've been
508 switched to match the ordering of GSASL_VALIDATE_SIMPLE. */
509 propval = (uschar *) gsasl_property_fast(sctx, GSASL_GSSAPI_DISPLAY_NAME);
510 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
511 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
512 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
514 for (i = 1; i <= 2; ++i)
515 expand_nlength[i] = Ustrlen(expand_nstring[i]);
517 /* In this one case, it perhaps makes sense to default back open?
518 But for consistency, let's just mandate server_condition here too. */
519 cbrc = condition_check(ablock,
520 US"server_condition (GSSAPI family)", ablock->server_condition);
521 checked_server_condition = TRUE;
525 /* DIGEST-MD5: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
526 CRAM-MD5: GSASL_AUTHID
527 PLAIN: GSASL_AUTHID and GSASL_AUTHZID
530 if (ob->server_scram_iter) {
531 tmps = CS expand_string(ob->server_scram_iter);
532 gsasl_property_set(sctx, GSASL_SCRAM_ITER, tmps);
534 if (ob->server_scram_salt) {
535 tmps = CS expand_string(ob->server_scram_salt);
536 gsasl_property_set(sctx, GSASL_SCRAM_SALT, tmps);
538 /* Asking for GSASL_AUTHZID calls back into us if we use
539 gsasl_property_get(), thus the use of gsasl_property_fast().
540 Do we really want to hardcode limits per mechanism? What happens when
541 a new mechanism is added to the library. It *shouldn't* result in us
542 needing to add more glue, since avoiding that is a large part of the
544 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
545 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
546 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
547 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
548 propval = (uschar *) gsasl_property_fast(sctx, GSASL_REALM);
549 auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
551 for (i = 1; i <= 3; ++i)
552 expand_nlength[i] = Ustrlen(expand_nstring[i]);
554 tmps = CS expand_string(ob->server_password);
556 sasl_error_should_defer = expand_string_forcedfail ? FALSE : TRUE;
557 HDEBUG(D_auth) debug_printf("server_password expansion failed, so "
558 "can't tell GNU SASL library the password for %s\n", auth_vars[0]);
559 return GSASL_AUTHENTICATION_ERROR;
561 gsasl_property_set(sctx, GSASL_PASSWORD, tmps);
562 /* This is inadequate; don't think Exim's store stacks are geared
563 for memory wiping, so expanding strings will leave stuff laying around.
564 But no need to compound the problem, so get rid of the one we can. */
565 memset(tmps, '\0', strlen(tmps));
570 HDEBUG(D_auth) debug_printf("Unrecognised callback: %d\n", prop);
571 cbrc = GSASL_NO_CALLBACK;
574 HDEBUG(D_auth) debug_printf("Returning %s (%s)\n",
575 gsasl_strerror_name(cbrc), gsasl_strerror(cbrc));
581 /*************************************************
582 * Client entry point *
583 *************************************************/
585 /* For interface, see auths/README */
589 auth_instance *ablock, /* authenticator block */
590 smtp_inblock *inblock, /* connection inblock */
591 smtp_outblock *outblock, /* connection outblock */
592 int timeout, /* command timeout */
593 uschar *buffer, /* buffer for reading response */
594 int buffsize) /* size of buffer */
597 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
598 /* NOT IMPLEMENTED */
603 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
605 int cbrc = GSASL_NO_CALLBACK;
607 debug_printf("GNU SASL callback %d for %s/%s as client\n",
608 prop, ablock->name, ablock->public_name);
611 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
616 /*************************************************
618 *************************************************/
621 auth_gsasl_version_report(FILE *f)
624 runtime = gsasl_check_version(NULL);
625 fprintf(f, "Library version: GNU SASL: Compile: %s\n"
627 GSASL_VERSION, runtime);
630 #endif /*!MACRO_PREDEF*/
631 #endif /* AUTH_GSASL */
633 /* End of gsasl_exim.c */