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 dummy2(int x) { dummy2(x-1); }
33 static void dummy(int x) { dummy2(x-1); }
37 #include "gsasl_exim.h"
39 /* Authenticator-specific options. */
40 /* I did have server_*_condition options for various mechanisms, but since
41 we only ever handle one mechanism at a time, I didn't see the point in keeping
42 that. In case someone sees a point, I've left the condition_check() API
44 optionlist auth_gsasl_options[] = {
45 { "server_channelbinding", opt_bool,
46 (void *)(offsetof(auth_gsasl_options_block, server_channelbinding)) },
47 { "server_hostname", opt_stringptr,
48 (void *)(offsetof(auth_gsasl_options_block, server_hostname)) },
49 { "server_mech", opt_stringptr,
50 (void *)(offsetof(auth_gsasl_options_block, server_mech)) },
51 { "server_password", opt_stringptr,
52 (void *)(offsetof(auth_gsasl_options_block, server_password)) },
53 { "server_realm", opt_stringptr,
54 (void *)(offsetof(auth_gsasl_options_block, server_realm)) },
55 { "server_scram_iter", opt_stringptr,
56 (void *)(offsetof(auth_gsasl_options_block, server_scram_iter)) },
57 { "server_scram_salt", opt_stringptr,
58 (void *)(offsetof(auth_gsasl_options_block, server_scram_salt)) },
59 { "server_service", opt_stringptr,
60 (void *)(offsetof(auth_gsasl_options_block, server_service)) }
62 /* GSASL_SCRAM_SALTED_PASSWORD documented only for client, so not implementing
63 hooks to avoid cleartext passwords in the Exim server. */
65 int auth_gsasl_options_count =
66 sizeof(auth_gsasl_options)/sizeof(optionlist);
68 /* Defaults for the authenticator-specific options. */
69 auth_gsasl_options_block auth_gsasl_option_defaults = {
70 US"smtp", /* server_service */
71 US"$primary_hostname", /* server_hostname */
72 NULL, /* server_realm */
73 NULL, /* server_mech */
74 NULL, /* server_password */
75 NULL, /* server_scram_iter */
76 NULL, /* server_scram_salt */
77 FALSE /* server_channelbinding */
80 /* "Globals" for managing the gsasl interface. */
82 static Gsasl *gsasl_ctx = NULL;
84 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop);
86 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
88 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
90 static BOOL sasl_error_should_defer = FALSE;
91 static Gsasl_property callback_loop = 0;
92 static BOOL checked_server_condition = FALSE;
94 enum { CURRENTLY_SERVER = 1, CURRENTLY_CLIENT = 2 };
96 struct callback_exim_state {
97 auth_instance *ablock;
102 /*************************************************
103 * Initialization entry point *
104 *************************************************/
106 /* Called for each instance, after its options have been read, to
107 enable consistency checks to be done, or anything else that needs
111 auth_gsasl_init(auth_instance *ablock)
115 auth_gsasl_options_block *ob =
116 (auth_gsasl_options_block *)(ablock->options_block);
118 /* As per existing Cyrus glue, use the authenticator's public name as
119 the default for the mechanism name; we don't handle multiple mechanisms
120 in one authenticator, but the same driver can be used multiple times. */
122 if (ob->server_mech == NULL)
123 ob->server_mech = string_copy(ablock->public_name);
125 /* Can get multiple session contexts from one library context, so just
126 initialise the once. */
127 if (gsasl_ctx == NULL) {
128 rc = gsasl_init(&gsasl_ctx);
129 if (rc != GSASL_OK) {
130 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
131 "couldn't initialise GNU SASL library: %s (%s)",
132 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
134 gsasl_callback_set(gsasl_ctx, main_callback);
137 /* We don't need this except to log it for debugging. */
138 rc = gsasl_server_mechlist(gsasl_ctx, &p);
140 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
141 "failed to retrieve list of mechanisms: %s (%s)",
142 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
143 HDEBUG(D_auth) debug_printf("GNU SASL supports: %s\n", p);
145 supported = gsasl_client_support_p(gsasl_ctx, (const char *)ob->server_mech);
147 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
148 "GNU SASL does not support mechanism \"%s\"",
149 ablock->name, ob->server_mech);
151 if ((ablock->server_condition == NULL) &&
152 (streqic(ob->server_mech, US"EXTERNAL") ||
153 streqic(ob->server_mech, US"ANONYMOUS") ||
154 streqic(ob->server_mech, US"PLAIN") ||
155 streqic(ob->server_mech, US"LOGIN")))
156 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
157 "Need server_condition for %s mechanism",
158 ablock->name, ob->server_mech);
160 /* This does *not* scale to new SASL mechanisms. Need a better way to ask
161 which properties will be needed. */
162 if ((ob->server_realm == NULL) &&
163 streqic(ob->server_mech, US"DIGEST-MD5"))
164 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
165 "Need server_realm for %s mechanism",
166 ablock->name, ob->server_mech);
168 /* At present, for mechanisms we don't panic on absence of server_condition;
169 need to figure out the most generically correct approach to deciding when
170 it's critical and when it isn't. Eg, for simple validation (PLAIN mechanism,
171 etc) it clearly is critical.
173 So don't activate without server_condition, this might be relaxed in the future.
175 if (ablock->server_condition != NULL) ablock->server = TRUE;
176 ablock->client = FALSE;
180 /* GNU SASL uses one top-level callback, registered at library level.
181 We dispatch to client and server functions instead. */
184 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
187 struct callback_exim_state *cb_state =
188 (struct callback_exim_state *)gsasl_session_hook_get(sctx);
191 debug_printf("GNU SASL Callback entered, prop=%d (loop prop=%d)\n",
192 prop, callback_loop);
194 if (cb_state == NULL) {
195 HDEBUG(D_auth) debug_printf(" not from our server/client processing.\n");
196 return GSASL_NO_CALLBACK;
199 if (callback_loop > 0) {
200 /* Most likely is that we were asked for property foo, and to
201 expand the string we asked for property bar to put into an auth
202 variable, but property bar is not supplied for this mechanism. */
204 debug_printf("Loop, asked for property %d while handling property %d\n",
205 prop, callback_loop);
206 return GSASL_NO_CALLBACK;
208 callback_loop = prop;
210 if (cb_state->currently == CURRENTLY_CLIENT)
211 rc = client_callback(ctx, sctx, prop, cb_state->ablock);
212 else if (cb_state->currently == CURRENTLY_SERVER)
213 rc = server_callback(ctx, sctx, prop, cb_state->ablock);
215 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
216 "unhandled callback state, bug in Exim", cb_state->ablock->name);
225 /*************************************************
226 * Server entry point *
227 *************************************************/
229 /* For interface, see auths/README */
232 auth_gsasl_server(auth_instance *ablock, uschar *initial_data)
235 char *to_send, *received;
236 Gsasl_session *sctx = NULL;
237 auth_gsasl_options_block *ob =
238 (auth_gsasl_options_block *)(ablock->options_block);
239 struct callback_exim_state cb_state;
240 int rc, auth_result, exim_error, exim_error_override;
243 debug_printf("GNU SASL: initialising session for %s, mechanism %s.\n",
244 ablock->name, ob->server_mech);
246 rc = gsasl_server_start(gsasl_ctx, (const char *)ob->server_mech, &sctx);
247 if (rc != GSASL_OK) {
248 auth_defer_msg = string_sprintf("GNU SASL: session start failure: %s (%s)",
249 gsasl_strerror_name(rc), gsasl_strerror(rc));
250 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
253 /* Hereafter: gsasl_finish(sctx) please */
255 gsasl_session_hook_set(sctx, (void *)ablock);
256 cb_state.ablock = ablock;
257 cb_state.currently = CURRENTLY_SERVER;
258 gsasl_session_hook_set(sctx, (void *)&cb_state);
260 tmps = CS expand_string(ob->server_service);
261 gsasl_property_set(sctx, GSASL_SERVICE, tmps);
262 tmps = CS expand_string(ob->server_hostname);
263 gsasl_property_set(sctx, GSASL_HOSTNAME, tmps);
264 if (ob->server_realm) {
265 tmps = CS expand_string(ob->server_realm);
267 gsasl_property_set(sctx, GSASL_REALM, tmps);
270 /* We don't support protection layers. */
271 gsasl_property_set(sctx, GSASL_QOPS, "qop-auth");
273 if (tls_channelbinding_b64) {
274 /* Some auth mechanisms can ensure that both sides are talking withing the
275 same security context; for TLS, this means that even if a bad certificate
276 has been accepted, they remain MitM-proof because both sides must be within
277 the same negotiated session; if someone is terminating one sesson and
278 proxying data on within a second, authentication will fail.
280 We might not have this available, depending upon TLS implementation,
281 ciphersuite, phase of moon ...
283 If we do, it results in extra SASL mechanisms being available; here,
284 Exim's one-mechanism-per-authenticator potentially causes problems.
285 It depends upon how GNU SASL will implement the PLUS variants of GS2
286 and whether it automatically mandates a switch to the bound PLUS
287 if the data is available. Since default-on, despite being more secure,
288 would then result in mechanism name changes on a library update, we
289 have little choice but to default it off and let the admin choose to
292 if (ob->server_channelbinding) {
293 HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
295 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE,
296 (const char *) tls_channelbinding_b64);
299 debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
304 debug_printf("Auth %s: no channel-binding data available\n",
309 checked_server_condition = FALSE;
311 received = CS initial_data;
313 exim_error = exim_error_override = OK;
316 rc = gsasl_step64(sctx, received, &to_send);
321 goto STOP_INTERACTION;
324 case GSASL_NEEDS_MORE:
327 case GSASL_AUTHENTICATION_ERROR:
328 case GSASL_INTEGRITY_ERROR:
329 case GSASL_NO_AUTHID:
330 case GSASL_NO_ANONYMOUS_TOKEN:
331 case GSASL_NO_AUTHZID:
332 case GSASL_NO_PASSWORD:
333 case GSASL_NO_PASSCODE:
335 case GSASL_BASE64_ERROR:
336 HDEBUG(D_auth) debug_printf("GNU SASL permanent error: %s (%s)\n",
337 gsasl_strerror_name(rc), gsasl_strerror(rc));
338 log_write(0, LOG_REJECT, "%s authenticator (%s):\n "
339 "GNU SASL permanent failure: %s (%s)",
340 ablock->name, ob->server_mech,
341 gsasl_strerror_name(rc), gsasl_strerror(rc));
342 if (rc == GSASL_BASE64_ERROR)
343 exim_error_override = BAD64;
344 goto STOP_INTERACTION;
347 auth_defer_msg = string_sprintf("GNU SASL temporary error: %s (%s)",
348 gsasl_strerror_name(rc), gsasl_strerror(rc));
349 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
350 exim_error_override = DEFER;
351 goto STOP_INTERACTION;
354 if ((rc == GSASL_NEEDS_MORE) ||
355 (to_send && *to_send))
357 auth_get_no64_data((uschar **)&received, (uschar *)to_send);
365 break; /* handles * cancelled check */
367 } while (rc == GSASL_NEEDS_MORE);
374 /* Can return: OK DEFER FAIL CANCELLED BAD64 UNEXPECTED */
376 if (exim_error != OK)
379 if (auth_result != GSASL_OK) {
380 HDEBUG(D_auth) debug_printf("authentication returned %s (%s)\n",
381 gsasl_strerror_name(auth_result), gsasl_strerror(auth_result));
382 if (exim_error_override != OK)
383 return exim_error_override; /* might be DEFER */
384 if (sasl_error_should_defer) /* overriding auth failure SASL error */
389 /* Auth succeeded, check server_condition unless already done in callback */
390 return checked_server_condition ? OK : auth_check_serv_cond(ablock);
393 /* returns the GSASL status of expanding the Exim string given */
395 condition_check(auth_instance *ablock, uschar *label, uschar *condition_string)
399 exim_rc = auth_check_some_cond(ablock, label, condition_string, FAIL);
403 } else if (exim_rc == DEFER) {
404 sasl_error_should_defer = TRUE;
405 return GSASL_AUTHENTICATION_ERROR;
406 } else if (exim_rc == FAIL) {
407 return GSASL_AUTHENTICATION_ERROR;
410 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
411 "Unhandled return from checking %s: %d",
412 ablock->name, label, exim_rc);
414 return GSASL_AUTHENTICATION_ERROR;
418 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
422 int cbrc = GSASL_NO_CALLBACK;
424 auth_gsasl_options_block *ob =
425 (auth_gsasl_options_block *)(ablock->options_block);
428 debug_printf("GNU SASL callback %d for %s/%s as server\n",
429 prop, ablock->name, ablock->public_name);
431 for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
435 case GSASL_VALIDATE_SIMPLE:
436 /* GSASL_AUTHID, GSASL_AUTHZID, and GSASL_PASSWORD */
437 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
438 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
439 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
440 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
441 propval = (uschar *) gsasl_property_fast(sctx, GSASL_PASSWORD);
442 auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
444 for (i = 1; i <= 3; ++i)
445 expand_nlength[i] = Ustrlen(expand_nstring[i]);
447 cbrc = condition_check(ablock, US"server_condition", ablock->server_condition);
448 checked_server_condition = TRUE;
451 case GSASL_VALIDATE_EXTERNAL:
452 if (ablock->server_condition == NULL) {
453 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate EXTERNAL.\n");
454 cbrc = GSASL_AUTHENTICATION_ERROR;
457 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
458 /* We always set $auth1, even if only to empty string. */
459 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
460 expand_nlength[1] = Ustrlen(expand_nstring[1]);
463 cbrc = condition_check(ablock,
464 US"server_condition (EXTERNAL)", ablock->server_condition);
465 checked_server_condition = TRUE;
468 case GSASL_VALIDATE_ANONYMOUS:
469 if (ablock->server_condition == NULL) {
470 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate ANONYMOUS.\n");
471 cbrc = GSASL_AUTHENTICATION_ERROR;
474 propval = (uschar *) gsasl_property_fast(sctx, GSASL_ANONYMOUS_TOKEN);
475 /* We always set $auth1, even if only to empty string. */
476 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
477 expand_nlength[1] = Ustrlen(expand_nstring[1]);
480 cbrc = condition_check(ablock,
481 US"server_condition (ANONYMOUS)", ablock->server_condition);
482 checked_server_condition = TRUE;
485 case GSASL_VALIDATE_GSSAPI:
486 /* GSASL_AUTHZID and GSASL_GSSAPI_DISPLAY_NAME
487 The display-name is authenticated as part of GSS, the authzid is claimed
488 by the SASL integration after authentication; protected against tampering
489 (if the SASL mechanism supports that, which Kerberos does) but is
490 unverified, same as normal for other mechanisms.
492 First coding, we had these values swapped, but for consistency and prior
493 to the first release of Exim with this authenticator, they've been
494 switched to match the ordering of GSASL_VALIDATE_SIMPLE. */
495 propval = (uschar *) gsasl_property_fast(sctx, GSASL_GSSAPI_DISPLAY_NAME);
496 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
497 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
498 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
500 for (i = 1; i <= 2; ++i)
501 expand_nlength[i] = Ustrlen(expand_nstring[i]);
503 /* In this one case, it perhaps makes sense to default back open?
504 But for consistency, let's just mandate server_condition here too. */
505 cbrc = condition_check(ablock,
506 US"server_condition (GSSAPI family)", ablock->server_condition);
507 checked_server_condition = TRUE;
511 /* DIGEST-MD5: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
512 CRAM-MD5: GSASL_AUTHID
513 PLAIN: GSASL_AUTHID and GSASL_AUTHZID
516 if (ob->server_scram_iter) {
517 tmps = CS expand_string(ob->server_scram_iter);
518 gsasl_property_set(sctx, GSASL_SCRAM_ITER, tmps);
520 if (ob->server_scram_salt) {
521 tmps = CS expand_string(ob->server_scram_salt);
522 gsasl_property_set(sctx, GSASL_SCRAM_SALT, tmps);
524 /* Asking for GSASL_AUTHZID calls back into us if we use
525 gsasl_property_get(), thus the use of gsasl_property_fast().
526 Do we really want to hardcode limits per mechanism? What happens when
527 a new mechanism is added to the library. It *shouldn't* result in us
528 needing to add more glue, since avoiding that is a large part of the
530 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
531 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
532 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
533 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
534 propval = (uschar *) gsasl_property_fast(sctx, GSASL_REALM);
535 auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
537 for (i = 1; i <= 3; ++i)
538 expand_nlength[i] = Ustrlen(expand_nstring[i]);
540 tmps = CS expand_string(ob->server_password);
542 sasl_error_should_defer = expand_string_forcedfail ? FALSE : TRUE;
543 HDEBUG(D_auth) debug_printf("server_password expansion failed, so "
544 "can't tell GNU SASL library the password for %s\n", auth_vars[0]);
545 return GSASL_AUTHENTICATION_ERROR;
547 gsasl_property_set(sctx, GSASL_PASSWORD, tmps);
548 /* This is inadequate; don't think Exim's store stacks are geared
549 for memory wiping, so expanding strings will leave stuff laying around.
550 But no need to compound the problem, so get rid of the one we can. */
551 memset(tmps, '\0', strlen(tmps));
556 HDEBUG(D_auth) debug_printf("Unrecognised callback: %d\n", prop);
557 cbrc = GSASL_NO_CALLBACK;
560 HDEBUG(D_auth) debug_printf("Returning %s (%s)\n",
561 gsasl_strerror_name(cbrc), gsasl_strerror(cbrc));
567 /*************************************************
568 * Client entry point *
569 *************************************************/
571 /* For interface, see auths/README */
575 auth_instance *ablock, /* authenticator block */
576 smtp_inblock *inblock, /* connection inblock */
577 smtp_outblock *outblock, /* connection outblock */
578 int timeout, /* command timeout */
579 uschar *buffer, /* buffer for reading response */
580 int buffsize) /* size of buffer */
583 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
584 /* NOT IMPLEMENTED */
589 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
591 int cbrc = GSASL_NO_CALLBACK;
593 debug_printf("GNU SASL callback %d for %s/%s as client\n",
594 prop, ablock->name, ablock->public_name);
597 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
602 /*************************************************
604 *************************************************/
607 auth_gsasl_version_report(FILE *f)
610 runtime = gsasl_check_version(NULL);
611 fprintf(f, "Library version: GNU SASL: Compile: %s\n"
613 GSASL_VERSION, runtime);
616 #endif /* AUTH_GSASL */
618 /* End of gsasl_exim.c */