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) { dummy(x-1); }
36 #include "gsasl_exim.h"
38 /* Authenticator-specific options. */
39 /* I did have server_*_condition options for various mechanisms, but since
40 we only ever handle one mechanism at a time, I didn't see the point in keeping
41 that. In case someone sees a point, I've left the condition_check() API
43 optionlist auth_gsasl_options[] = {
44 { "server_channelbinding", opt_bool,
45 (void *)(offsetof(auth_gsasl_options_block, server_channelbinding)) },
46 { "server_hostname", opt_stringptr,
47 (void *)(offsetof(auth_gsasl_options_block, server_hostname)) },
48 { "server_mech", opt_stringptr,
49 (void *)(offsetof(auth_gsasl_options_block, server_mech)) },
50 { "server_password", opt_stringptr,
51 (void *)(offsetof(auth_gsasl_options_block, server_password)) },
52 { "server_realm", opt_stringptr,
53 (void *)(offsetof(auth_gsasl_options_block, server_realm)) },
54 { "server_scram_iter", opt_stringptr,
55 (void *)(offsetof(auth_gsasl_options_block, server_scram_iter)) },
56 { "server_scram_salt", opt_stringptr,
57 (void *)(offsetof(auth_gsasl_options_block, server_scram_salt)) },
58 { "server_service", opt_stringptr,
59 (void *)(offsetof(auth_gsasl_options_block, server_service)) }
61 /* GSASL_SCRAM_SALTED_PASSWORD documented only for client, so not implementing
62 hooks to avoid cleartext passwords in the Exim server. */
64 int auth_gsasl_options_count =
65 sizeof(auth_gsasl_options)/sizeof(optionlist);
67 /* Defaults for the authenticator-specific options. */
68 auth_gsasl_options_block auth_gsasl_option_defaults = {
69 US"smtp", /* server_service */
70 US"$primary_hostname", /* server_hostname */
71 NULL, /* server_realm */
72 NULL, /* server_mech */
73 NULL, /* server_password */
74 NULL, /* server_scram_iter */
75 NULL, /* server_scram_salt */
76 FALSE /* server_channelbinding */
79 /* "Globals" for managing the gsasl interface. */
81 static Gsasl *gsasl_ctx = NULL;
83 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop);
85 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
87 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
89 static BOOL sasl_error_should_defer = FALSE;
90 static Gsasl_property callback_loop = 0;
91 static BOOL checked_server_condition = FALSE;
93 enum { CURRENTLY_SERVER = 1, CURRENTLY_CLIENT = 2 };
95 struct callback_exim_state {
96 auth_instance *ablock;
101 /*************************************************
102 * Initialization entry point *
103 *************************************************/
105 /* Called for each instance, after its options have been read, to
106 enable consistency checks to be done, or anything else that needs
110 auth_gsasl_init(auth_instance *ablock)
114 auth_gsasl_options_block *ob =
115 (auth_gsasl_options_block *)(ablock->options_block);
117 /* As per existing Cyrus glue, use the authenticator's public name as
118 the default for the mechanism name; we don't handle multiple mechanisms
119 in one authenticator, but the same driver can be used multiple times. */
121 if (ob->server_mech == NULL)
122 ob->server_mech = string_copy(ablock->public_name);
124 /* Can get multiple session contexts from one library context, so just
125 initialise the once. */
126 if (gsasl_ctx == NULL) {
127 rc = gsasl_init(&gsasl_ctx);
128 if (rc != GSASL_OK) {
129 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
130 "couldn't initialise GNU SASL library: %s (%s)",
131 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
133 gsasl_callback_set(gsasl_ctx, main_callback);
136 /* We don't need this except to log it for debugging. */
137 rc = gsasl_server_mechlist(gsasl_ctx, &p);
139 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
140 "failed to retrieve list of mechanisms: %s (%s)",
141 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
142 HDEBUG(D_auth) debug_printf("GNU SASL supports: %s\n", p);
144 supported = gsasl_client_support_p(gsasl_ctx, (const char *)ob->server_mech);
146 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
147 "GNU SASL does not support mechanism \"%s\"",
148 ablock->name, ob->server_mech);
150 if ((ablock->server_condition == NULL) &&
151 (streqic(ob->server_mech, US"EXTERNAL") ||
152 streqic(ob->server_mech, US"ANONYMOUS") ||
153 streqic(ob->server_mech, US"PLAIN") ||
154 streqic(ob->server_mech, US"LOGIN")))
155 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
156 "Need server_condition for %s mechanism",
157 ablock->name, ob->server_mech);
159 /* This does *not* scale to new SASL mechanisms. Need a better way to ask
160 which properties will be needed. */
161 if ((ob->server_realm == NULL) &&
162 streqic(ob->server_mech, US"DIGEST-MD5"))
163 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
164 "Need server_realm for %s mechanism",
165 ablock->name, ob->server_mech);
167 /* At present, for mechanisms we don't panic on absence of server_condition;
168 need to figure out the most generically correct approach to deciding when
169 it's critical and when it isn't. Eg, for simple validation (PLAIN mechanism,
170 etc) it clearly is critical.
172 So don't activate without server_condition, this might be relaxed in the future.
174 if (ablock->server_condition != NULL) ablock->server = TRUE;
175 ablock->client = FALSE;
179 /* GNU SASL uses one top-level callback, registered at library level.
180 We dispatch to client and server functions instead. */
183 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
186 struct callback_exim_state *cb_state =
187 (struct callback_exim_state *)gsasl_session_hook_get(sctx);
190 debug_printf("GNU SASL Callback entered, prop=%d (loop prop=%d)\n",
191 prop, callback_loop);
193 if (cb_state == NULL) {
194 HDEBUG(D_auth) debug_printf(" not from our server/client processing.\n");
195 return GSASL_NO_CALLBACK;
198 if (callback_loop > 0) {
199 /* Most likely is that we were asked for property foo, and to
200 expand the string we asked for property bar to put into an auth
201 variable, but property bar is not supplied for this mechanism. */
203 debug_printf("Loop, asked for property %d while handling property %d\n",
204 prop, callback_loop);
205 return GSASL_NO_CALLBACK;
207 callback_loop = prop;
209 if (cb_state->currently == CURRENTLY_CLIENT)
210 rc = client_callback(ctx, sctx, prop, cb_state->ablock);
211 else if (cb_state->currently == CURRENTLY_SERVER)
212 rc = server_callback(ctx, sctx, prop, cb_state->ablock);
214 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
215 "unhandled callback state, bug in Exim", cb_state->ablock->name);
224 /*************************************************
225 * Server entry point *
226 *************************************************/
228 /* For interface, see auths/README */
231 auth_gsasl_server(auth_instance *ablock, uschar *initial_data)
234 char *to_send, *received;
235 Gsasl_session *sctx = NULL;
236 auth_gsasl_options_block *ob =
237 (auth_gsasl_options_block *)(ablock->options_block);
238 struct callback_exim_state cb_state;
239 int rc, auth_result, exim_error, exim_error_override;
242 debug_printf("GNU SASL: initialising session for %s, mechanism %s.\n",
243 ablock->name, ob->server_mech);
245 rc = gsasl_server_start(gsasl_ctx, (const char *)ob->server_mech, &sctx);
246 if (rc != GSASL_OK) {
247 auth_defer_msg = string_sprintf("GNU SASL: session start failure: %s (%s)",
248 gsasl_strerror_name(rc), gsasl_strerror(rc));
249 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
252 /* Hereafter: gsasl_finish(sctx) please */
254 gsasl_session_hook_set(sctx, (void *)ablock);
255 cb_state.ablock = ablock;
256 cb_state.currently = CURRENTLY_SERVER;
257 gsasl_session_hook_set(sctx, (void *)&cb_state);
259 tmps = CS expand_string(ob->server_service);
260 gsasl_property_set(sctx, GSASL_SERVICE, tmps);
261 tmps = CS expand_string(ob->server_hostname);
262 gsasl_property_set(sctx, GSASL_HOSTNAME, tmps);
263 if (ob->server_realm) {
264 tmps = CS expand_string(ob->server_realm);
266 gsasl_property_set(sctx, GSASL_REALM, tmps);
269 /* We don't support protection layers. */
270 gsasl_property_set(sctx, GSASL_QOPS, "qop-auth");
272 if (tls_channelbinding_b64) {
273 /* Some auth mechanisms can ensure that both sides are talking withing the
274 same security context; for TLS, this means that even if a bad certificate
275 has been accepted, they remain MitM-proof because both sides must be within
276 the same negotiated session; if someone is terminating one sesson and
277 proxying data on within a second, authentication will fail.
279 We might not have this available, depending upon TLS implementation,
280 ciphersuite, phase of moon ...
282 If we do, it results in extra SASL mechanisms being available; here,
283 Exim's one-mechanism-per-authenticator potentially causes problems.
284 It depends upon how GNU SASL will implement the PLUS variants of GS2
285 and whether it automatically mandates a switch to the bound PLUS
286 if the data is available. Since default-on, despite being more secure,
287 would then result in mechanism name changes on a library update, we
288 have little choice but to default it off and let the admin choose to
291 if (ob->server_channelbinding) {
292 HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
294 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE,
295 (const char *) tls_channelbinding_b64);
298 debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
303 debug_printf("Auth %s: no channel-binding data available\n",
308 checked_server_condition = FALSE;
310 received = CS initial_data;
312 exim_error = exim_error_override = OK;
315 rc = gsasl_step64(sctx, received, &to_send);
320 goto STOP_INTERACTION;
323 case GSASL_NEEDS_MORE:
326 case GSASL_AUTHENTICATION_ERROR:
327 case GSASL_INTEGRITY_ERROR:
328 case GSASL_NO_AUTHID:
329 case GSASL_NO_ANONYMOUS_TOKEN:
330 case GSASL_NO_AUTHZID:
331 case GSASL_NO_PASSWORD:
332 case GSASL_NO_PASSCODE:
334 case GSASL_BASE64_ERROR:
335 HDEBUG(D_auth) debug_printf("GNU SASL permanent error: %s (%s)\n",
336 gsasl_strerror_name(rc), gsasl_strerror(rc));
337 log_write(0, LOG_REJECT, "%s authenticator (%s):\n "
338 "GNU SASL permanent failure: %s (%s)",
339 ablock->name, ob->server_mech,
340 gsasl_strerror_name(rc), gsasl_strerror(rc));
341 if (rc == GSASL_BASE64_ERROR)
342 exim_error_override = BAD64;
343 goto STOP_INTERACTION;
346 auth_defer_msg = string_sprintf("GNU SASL temporary error: %s (%s)",
347 gsasl_strerror_name(rc), gsasl_strerror(rc));
348 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
349 exim_error_override = DEFER;
350 goto STOP_INTERACTION;
353 if ((rc == GSASL_NEEDS_MORE) ||
354 (to_send && *to_send))
356 auth_get_no64_data((uschar **)&received, (uschar *)to_send);
364 break; /* handles * cancelled check */
366 } while (rc == GSASL_NEEDS_MORE);
373 /* Can return: OK DEFER FAIL CANCELLED BAD64 UNEXPECTED */
375 if (exim_error != OK)
378 if (auth_result != GSASL_OK) {
379 HDEBUG(D_auth) debug_printf("authentication returned %s (%s)\n",
380 gsasl_strerror_name(auth_result), gsasl_strerror(auth_result));
381 if (exim_error_override != OK)
382 return exim_error_override; /* might be DEFER */
383 if (sasl_error_should_defer) /* overriding auth failure SASL error */
388 /* Auth succeeded, check server_condition unless already done in callback */
389 return checked_server_condition ? OK : auth_check_serv_cond(ablock);
392 /* returns the GSASL status of expanding the Exim string given */
394 condition_check(auth_instance *ablock, uschar *label, uschar *condition_string)
398 exim_rc = auth_check_some_cond(ablock, label, condition_string, FAIL);
402 } else if (exim_rc == DEFER) {
403 sasl_error_should_defer = TRUE;
404 return GSASL_AUTHENTICATION_ERROR;
405 } else if (exim_rc == FAIL) {
406 return GSASL_AUTHENTICATION_ERROR;
409 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
410 "Unhandled return from checking %s: %d",
411 ablock->name, label, exim_rc);
413 return GSASL_AUTHENTICATION_ERROR;
417 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
421 int cbrc = GSASL_NO_CALLBACK;
423 auth_gsasl_options_block *ob =
424 (auth_gsasl_options_block *)(ablock->options_block);
427 debug_printf("GNU SASL callback %d for %s/%s as server\n",
428 prop, ablock->name, ablock->public_name);
430 for (i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
434 case GSASL_VALIDATE_SIMPLE:
435 /* GSASL_AUTHID, GSASL_AUTHZID, and GSASL_PASSWORD */
436 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
437 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
438 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
439 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
440 propval = (uschar *) gsasl_property_fast(sctx, GSASL_PASSWORD);
441 auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
443 for (i = 1; i <= 3; ++i)
444 expand_nlength[i] = Ustrlen(expand_nstring[i]);
446 cbrc = condition_check(ablock, US"server_condition", ablock->server_condition);
447 checked_server_condition = TRUE;
450 case GSASL_VALIDATE_EXTERNAL:
451 if (ablock->server_condition == NULL) {
452 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate EXTERNAL.\n");
453 cbrc = GSASL_AUTHENTICATION_ERROR;
456 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
457 /* We always set $auth1, even if only to empty string. */
458 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
459 expand_nlength[1] = Ustrlen(expand_nstring[1]);
462 cbrc = condition_check(ablock,
463 US"server_condition (EXTERNAL)", ablock->server_condition);
464 checked_server_condition = TRUE;
467 case GSASL_VALIDATE_ANONYMOUS:
468 if (ablock->server_condition == NULL) {
469 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate ANONYMOUS.\n");
470 cbrc = GSASL_AUTHENTICATION_ERROR;
473 propval = (uschar *) gsasl_property_fast(sctx, GSASL_ANONYMOUS_TOKEN);
474 /* We always set $auth1, even if only to empty string. */
475 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
476 expand_nlength[1] = Ustrlen(expand_nstring[1]);
479 cbrc = condition_check(ablock,
480 US"server_condition (ANONYMOUS)", ablock->server_condition);
481 checked_server_condition = TRUE;
484 case GSASL_VALIDATE_GSSAPI:
485 /* GSASL_AUTHZID and GSASL_GSSAPI_DISPLAY_NAME
486 The display-name is authenticated as part of GSS, the authzid is claimed
487 by the SASL integration after authentication; protected against tampering
488 (if the SASL mechanism supports that, which Kerberos does) but is
489 unverified, same as normal for other mechanisms.
491 First coding, we had these values swapped, but for consistency and prior
492 to the first release of Exim with this authenticator, they've been
493 switched to match the ordering of GSASL_VALIDATE_SIMPLE. */
494 propval = (uschar *) gsasl_property_fast(sctx, GSASL_GSSAPI_DISPLAY_NAME);
495 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
496 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
497 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
499 for (i = 1; i <= 2; ++i)
500 expand_nlength[i] = Ustrlen(expand_nstring[i]);
502 /* In this one case, it perhaps makes sense to default back open?
503 But for consistency, let's just mandate server_condition here too. */
504 cbrc = condition_check(ablock,
505 US"server_condition (GSSAPI family)", ablock->server_condition);
506 checked_server_condition = TRUE;
510 /* DIGEST-MD5: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
511 CRAM-MD5: GSASL_AUTHID
512 PLAIN: GSASL_AUTHID and GSASL_AUTHZID
515 if (ob->server_scram_iter) {
516 tmps = CS expand_string(ob->server_scram_iter);
517 gsasl_property_set(sctx, GSASL_SCRAM_ITER, tmps);
519 if (ob->server_scram_salt) {
520 tmps = CS expand_string(ob->server_scram_salt);
521 gsasl_property_set(sctx, GSASL_SCRAM_SALT, tmps);
523 /* Asking for GSASL_AUTHZID calls back into us if we use
524 gsasl_property_get(), thus the use of gsasl_property_fast().
525 Do we really want to hardcode limits per mechanism? What happens when
526 a new mechanism is added to the library. It *shouldn't* result in us
527 needing to add more glue, since avoiding that is a large part of the
529 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHID);
530 auth_vars[0] = expand_nstring[1] = propval ? propval : US"";
531 propval = (uschar *) gsasl_property_fast(sctx, GSASL_AUTHZID);
532 auth_vars[1] = expand_nstring[2] = propval ? propval : US"";
533 propval = (uschar *) gsasl_property_fast(sctx, GSASL_REALM);
534 auth_vars[2] = expand_nstring[3] = propval ? propval : US"";
536 for (i = 1; i <= 3; ++i)
537 expand_nlength[i] = Ustrlen(expand_nstring[i]);
539 tmps = CS expand_string(ob->server_password);
541 sasl_error_should_defer = expand_string_forcedfail ? FALSE : TRUE;
542 HDEBUG(D_auth) debug_printf("server_password expansion failed, so "
543 "can't tell GNU SASL library the password for %s\n", auth_vars[0]);
544 return GSASL_AUTHENTICATION_ERROR;
546 gsasl_property_set(sctx, GSASL_PASSWORD, tmps);
547 /* This is inadequate; don't think Exim's store stacks are geared
548 for memory wiping, so expanding strings will leave stuff laying around.
549 But no need to compound the problem, so get rid of the one we can. */
550 memset(tmps, '\0', strlen(tmps));
555 HDEBUG(D_auth) debug_printf("Unrecognised callback: %d\n", prop);
556 cbrc = GSASL_NO_CALLBACK;
559 HDEBUG(D_auth) debug_printf("Returning %s (%s)\n",
560 gsasl_strerror_name(cbrc), gsasl_strerror(cbrc));
566 /*************************************************
567 * Client entry point *
568 *************************************************/
570 /* For interface, see auths/README */
574 auth_instance *ablock, /* authenticator block */
575 smtp_inblock *inblock, /* connection inblock */
576 smtp_outblock *outblock, /* connection outblock */
577 int timeout, /* command timeout */
578 uschar *buffer, /* buffer for reading response */
579 int buffsize) /* size of buffer */
582 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
583 /* NOT IMPLEMENTED */
588 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
590 int cbrc = GSASL_NO_CALLBACK;
592 debug_printf("GNU SASL callback %d for %s/%s as client\n",
593 prop, ablock->name, ablock->public_name);
596 debug_printf("Client side NOT IMPLEMENTED: you should not see this!\n");
601 /*************************************************
603 *************************************************/
606 auth_gsasl_version_report(FILE *f)
609 runtime = gsasl_check_version(NULL);
610 fprintf(f, "Library version: GNU SASL: Compile: %s\n"
612 GSASL_VERSION, runtime);
615 #endif /* AUTH_GSASL */
617 /* End of gsasl_exim.c */