1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2019-2020 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
9 /* Copyright (c) Twitter Inc 2012
10 Author: Phil Pennock <pdp@exim.org> */
11 /* Copyright (c) Phil Pennock 2012 */
13 /* Interface to GNU SASL library for generic authentication. */
17 GNU SASL does not provide authentication data itself, so we have to expose
18 that decision to configuration. For some mechanisms, we need to act much
19 like plaintext. For others, we only need to be able to provide some
20 evaluated data on demand. There's no abstracted way (ie, without hardcoding
21 knowledge of authenticators here) to know which need what properties; we
22 can't query a session or the library for "we will need these for mechanism X".
24 So: we always require server_condition, even if sometimes it will just be
25 set as "yes". We do provide a number of other hooks, which might not make
26 sense in all contexts. For some, we can do checks at init time.
32 /* dummy function to satisfy compilers when we link in an "empty" file. */
33 static void dummy(int x);
34 static void dummy2(int x) { dummy(x-1); }
35 static void dummy(int x) { dummy2(x-1); }
39 #include "gsasl_exim.h"
42 #if GSASL_VERSION_MINOR >= 10
43 # define EXIM_GSASL_HAVE_SCRAM_SHA_256
44 # define EXIM_GSASL_SCRAM_S_KEY
46 #elif GSASL_VERSION_MINOR == 9
47 # define EXIM_GSASL_HAVE_SCRAM_SHA_256
49 # if GSASL_VERSION_PATCH >= 1
50 # define EXIM_GSASL_SCRAM_S_KEY
52 # if GSASL_VERSION_PATCH < 2
53 # define CHANNELBIND_HACK
57 # define CHANNELBIND_HACK
61 /* Authenticator-specific options. */
62 /* I did have server_*_condition options for various mechanisms, but since
63 we only ever handle one mechanism at a time, I didn't see the point in keeping
64 that. In case someone sees a point, I've left the condition_check() API
66 #define LOFF(field) OPT_OFF(auth_gsasl_options_block, field)
68 optionlist auth_gsasl_options[] = {
69 { "client_authz", opt_stringptr, LOFF(client_authz) },
70 { "client_channelbinding", opt_bool, LOFF(client_channelbinding) },
71 { "client_password", opt_stringptr, LOFF(client_password) },
72 { "client_spassword", opt_stringptr, LOFF(client_spassword) },
73 { "client_username", opt_stringptr, LOFF(client_username) },
75 { "server_channelbinding", opt_bool, LOFF(server_channelbinding) },
76 { "server_hostname", opt_stringptr, LOFF(server_hostname) },
77 #ifdef EXIM_GSASL_SCRAM_S_KEY
78 { "server_key", opt_stringptr, LOFF(server_key) },
80 { "server_mech", opt_stringptr, LOFF(server_mech) },
81 { "server_password", opt_stringptr, LOFF(server_password) },
82 { "server_realm", opt_stringptr, LOFF(server_realm) },
83 { "server_scram_iter", opt_stringptr, LOFF(server_scram_iter) },
84 { "server_scram_salt", opt_stringptr, LOFF(server_scram_salt) },
85 #ifdef EXIM_GSASL_SCRAM_S_KEY
86 { "server_skey", opt_stringptr, LOFF(server_s_key) },
88 { "server_service", opt_stringptr, LOFF(server_service) }
91 int auth_gsasl_options_count =
92 sizeof(auth_gsasl_options)/sizeof(optionlist);
94 /* Defaults for the authenticator-specific options. */
95 auth_gsasl_options_block auth_gsasl_option_defaults = {
96 .server_service = US"smtp",
97 .server_hostname = US"$primary_hostname",
98 .server_scram_iter = US"4096",
99 /* all others zero/null */
104 # include "../macro_predef.h"
107 void auth_gsasl_init(auth_instance *ablock) {}
108 int auth_gsasl_server(auth_instance *ablock, uschar *data) {return 0;}
109 int auth_gsasl_client(auth_instance *ablock, void * sx,
110 int timeout, uschar *buffer, int buffsize) {return 0;}
111 void auth_gsasl_version_report(FILE *f) {}
114 auth_gsasl_macros(void)
116 # ifdef EXIM_GSASL_HAVE_SCRAM_SHA_256
117 builtin_macro_create(US"_HAVE_AUTH_GSASL_SCRAM_SHA_256");
119 # ifdef EXIM_GSASL_SCRAM_S_KEY
120 builtin_macro_create(US"_HAVE_AUTH_GSASL_SCRAM_S_KEY");
124 #else /*!MACRO_PREDEF*/
128 /* "Globals" for managing the gsasl interface. */
130 static Gsasl *gsasl_ctx = NULL;
132 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop);
134 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
136 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock);
138 static BOOL sasl_error_should_defer = FALSE;
139 static Gsasl_property callback_loop = 0;
140 static BOOL checked_server_condition = FALSE;
142 enum { CURRENTLY_SERVER = 1, CURRENTLY_CLIENT = 2 };
144 struct callback_exim_state {
145 auth_instance *ablock;
150 /*************************************************
151 * Initialization entry point *
152 *************************************************/
154 /* Called for each instance, after its options have been read, to
155 enable consistency checks to be done, or anything else that needs
159 auth_gsasl_init(auth_instance *ablock)
161 static char * once = NULL;
163 auth_gsasl_options_block *ob =
164 (auth_gsasl_options_block *)(ablock->options_block);
166 /* As per existing Cyrus glue, use the authenticator's public name as
167 the default for the mechanism name; we don't handle multiple mechanisms
168 in one authenticator, but the same driver can be used multiple times. */
170 if (!ob->server_mech)
171 ob->server_mech = string_copy(ablock->public_name);
173 /* Can get multiple session contexts from one library context, so just
174 initialise the once. */
178 if ((rc = gsasl_init(&gsasl_ctx)) != GSASL_OK)
179 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
180 "couldn't initialise GNU SASL library: %s (%s)",
181 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
183 gsasl_callback_set(gsasl_ctx, main_callback);
186 /* We don't need this except to log it for debugging. */
188 HDEBUG(D_auth) if (!once)
190 if ((rc = gsasl_server_mechlist(gsasl_ctx, &once)) != GSASL_OK)
191 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
192 "failed to retrieve list of mechanisms: %s (%s)",
193 ablock->name, gsasl_strerror_name(rc), gsasl_strerror(rc));
195 debug_printf("GNU SASL supports: %s\n", once);
198 if (!gsasl_client_support_p(gsasl_ctx, CCS ob->server_mech))
199 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
200 "GNU SASL does not support mechanism \"%s\"",
201 ablock->name, ob->server_mech);
203 ablock->server = TRUE;
205 if ( !ablock->server_condition
206 && ( streqic(ob->server_mech, US"EXTERNAL")
207 || streqic(ob->server_mech, US"ANONYMOUS")
208 || streqic(ob->server_mech, US"PLAIN")
209 || streqic(ob->server_mech, US"LOGIN")
212 ablock->server = FALSE;
213 HDEBUG(D_auth) debug_printf("%s authenticator: "
214 "Need server_condition for %s mechanism\n",
215 ablock->name, ob->server_mech);
218 /* This does *not* scale to new SASL mechanisms. Need a better way to ask
219 which properties will be needed. */
221 if ( !ob->server_realm
222 && streqic(ob->server_mech, US"DIGEST-MD5"))
224 ablock->server = FALSE;
225 HDEBUG(D_auth) debug_printf("%s authenticator: "
226 "Need server_realm for %s mechanism\n",
227 ablock->name, ob->server_mech);
230 /* At present, for mechanisms we don't panic on absence of server_condition;
231 need to figure out the most generically correct approach to deciding when
232 it's critical and when it isn't. Eg, for simple validation (PLAIN mechanism,
233 etc) it clearly is critical.
236 ablock->client = ob->client_username && ob->client_password;
240 /* GNU SASL uses one top-level callback, registered at library level.
241 We dispatch to client and server functions instead. */
244 main_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop)
247 struct callback_exim_state *cb_state =
248 (struct callback_exim_state *)gsasl_session_hook_get(sctx);
252 HDEBUG(D_auth) debug_printf("gsasl callback (%d) not from our server/client processing\n", prop);
253 #ifdef CHANNELBIND_HACK
254 if (prop == GSASL_CB_TLS_UNIQUE)
257 if ((s = gsasl_callback_hook_get(ctx)))
259 HDEBUG(D_auth) debug_printf("GSASL_CB_TLS_UNIQUE from ctx hook\n");
260 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CS s);
264 HDEBUG(D_auth) debug_printf("GSASL_CB_TLS_UNIQUE! dummy for now\n");
265 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, "");
270 return GSASL_NO_CALLBACK;
274 debug_printf("GNU SASL Callback entered, prop=%d (loop prop=%d)\n",
275 prop, callback_loop);
277 if (callback_loop > 0)
279 /* Most likely is that we were asked for property FOO, and to
280 expand the string we asked for property BAR to put into an auth
281 variable, but property BAR is not supplied for this mechanism. */
283 debug_printf("Loop, asked for property %d while handling property %d\n",
284 prop, callback_loop);
285 return GSASL_NO_CALLBACK;
287 callback_loop = prop;
289 if (cb_state->currently == CURRENTLY_CLIENT)
290 rc = client_callback(ctx, sctx, prop, cb_state->ablock);
291 else if (cb_state->currently == CURRENTLY_SERVER)
292 rc = server_callback(ctx, sctx, prop, cb_state->ablock);
294 log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
295 "unhandled callback state, bug in Exim", cb_state->ablock->name);
303 /*************************************************
304 * Debug service function *
305 *************************************************/
306 static const uschar *
307 gsasl_prop_code_to_name(Gsasl_property prop)
311 case GSASL_AUTHID: return US"AUTHID";
312 case GSASL_AUTHZID: return US"AUTHZID";
313 case GSASL_PASSWORD: return US"PASSWORD";
314 case GSASL_ANONYMOUS_TOKEN: return US"ANONYMOUS_TOKEN";
315 case GSASL_SERVICE: return US"SERVICE";
316 case GSASL_HOSTNAME: return US"HOSTNAME";
317 case GSASL_GSSAPI_DISPLAY_NAME: return US"GSSAPI_DISPLAY_NAME";
318 case GSASL_PASSCODE: return US"PASSCODE";
319 case GSASL_SUGGESTED_PIN: return US"SUGGESTED_PIN";
320 case GSASL_PIN: return US"PIN";
321 case GSASL_REALM: return US"REALM";
322 case GSASL_DIGEST_MD5_HASHED_PASSWORD: return US"DIGEST_MD5_HASHED_PASSWORD";
323 case GSASL_QOPS: return US"QOPS";
324 case GSASL_QOP: return US"QOP";
325 case GSASL_SCRAM_ITER: return US"SCRAM_ITER";
326 case GSASL_SCRAM_SALT: return US"SCRAM_SALT";
327 case GSASL_SCRAM_SALTED_PASSWORD: return US"SCRAM_SALTED_PASSWORD";
328 #ifdef EXIM_GSASL_SCRAM_S_KEY
329 case GSASL_SCRAM_STOREDKEY: return US"SCRAM_STOREDKEY";
330 case GSASL_SCRAM_SERVERKEY: return US"SCRAM_SERVERKEY";
332 case GSASL_CB_TLS_UNIQUE: return US"CB_TLS_UNIQUE";
333 case GSASL_SAML20_IDP_IDENTIFIER: return US"SAML20_IDP_IDENTIFIER";
334 case GSASL_SAML20_REDIRECT_URL: return US"SAML20_REDIRECT_URL";
335 case GSASL_OPENID20_REDIRECT_URL: return US"OPENID20_REDIRECT_URL";
336 case GSASL_OPENID20_OUTCOME_DATA: return US"OPENID20_OUTCOME_DATA";
337 case GSASL_SAML20_AUTHENTICATE_IN_BROWSER: return US"SAML20_AUTHENTICATE_IN_BROWSER";
338 case GSASL_OPENID20_AUTHENTICATE_IN_BROWSER: return US"OPENID20_AUTHENTICATE_IN_BROWSER";
339 #ifdef EXIM_GSASL_SCRAM_S_KEY
340 case GSASL_SCRAM_CLIENTKEY: return US"SCRAM_CLIENTKEY";
342 case GSASL_VALIDATE_SIMPLE: return US"VALIDATE_SIMPLE";
343 case GSASL_VALIDATE_EXTERNAL: return US"VALIDATE_EXTERNAL";
344 case GSASL_VALIDATE_ANONYMOUS: return US"VALIDATE_ANONYMOUS";
345 case GSASL_VALIDATE_GSSAPI: return US"VALIDATE_GSSAPI";
346 case GSASL_VALIDATE_SECURID: return US"VALIDATE_SECURID";
347 case GSASL_VALIDATE_SAML20: return US"VALIDATE_SAML20";
348 case GSASL_VALIDATE_OPENID20: return US"VALIDATE_OPENID20";
350 return CUS string_sprintf("(unknown prop: %d)", (int)prop);
353 /*************************************************
354 * Server entry point *
355 *************************************************/
357 /* For interface, see auths/README */
360 auth_gsasl_server(auth_instance *ablock, uschar *initial_data)
363 char *to_send, *received;
364 Gsasl_session *sctx = NULL;
365 auth_gsasl_options_block *ob =
366 (auth_gsasl_options_block *)(ablock->options_block);
367 struct callback_exim_state cb_state;
368 int rc, auth_result, exim_error, exim_error_override;
371 debug_printf("GNU SASL: initialising session for %s, mechanism %s\n",
372 ablock->name, ob->server_mech);
375 if (tls_in.channelbinding && ob->server_channelbinding)
377 # ifndef DISABLE_TLS_RESUME
378 if (!tls_in.ext_master_secret && tls_in.resumption == RESUME_USED)
379 { /* per RFC 7677 section 4 */
380 HDEBUG(D_auth) debug_printf(
381 "channel binding not usable on resumed TLS without extended-master-secret");
385 # ifdef CHANNELBIND_HACK
386 /* This is a gross hack to get around the library before 1.9.2
387 a) requiring that c-b was already set, at the _start() call, and
388 b) caching a b64'd version of the binding then which it never updates. */
390 gsasl_callback_hook_set(gsasl_ctx, tls_in.channelbinding);
395 if ((rc = gsasl_server_start(gsasl_ctx, CCS ob->server_mech, &sctx)) != GSASL_OK)
397 auth_defer_msg = string_sprintf("GNU SASL: session start failure: %s (%s)",
398 gsasl_strerror_name(rc), gsasl_strerror(rc));
399 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
402 /* Hereafter: gsasl_finish(sctx) please */
404 cb_state.ablock = ablock;
405 cb_state.currently = CURRENTLY_SERVER;
406 gsasl_session_hook_set(sctx, &cb_state);
408 tmps = CS expand_string(ob->server_service);
409 gsasl_property_set(sctx, GSASL_SERVICE, tmps);
410 tmps = CS expand_string(ob->server_hostname);
411 gsasl_property_set(sctx, GSASL_HOSTNAME, tmps);
412 if (ob->server_realm)
414 tmps = CS expand_string(ob->server_realm);
416 gsasl_property_set(sctx, GSASL_REALM, tmps);
418 /* We don't support protection layers. */
419 gsasl_property_set(sctx, GSASL_QOPS, "qop-auth");
422 if (tls_in.channelbinding)
424 /* Some auth mechanisms can ensure that both sides are talking withing the
425 same security context; for TLS, this means that even if a bad certificate
426 has been accepted, they remain MitM-proof because both sides must be within
427 the same negotiated session; if someone is terminating one session and
428 proxying data on within a second, authentication will fail.
430 We might not have this available, depending upon TLS implementation,
431 ciphersuite, phase of moon ...
433 If we do, it results in extra SASL mechanisms being available; here,
434 Exim's one-mechanism-per-authenticator potentially causes problems.
435 It depends upon how GNU SASL will implement the PLUS variants of GS2
436 and whether it automatically mandates a switch to the bound PLUS
437 if the data is available. Since default-on, despite being more secure,
438 would then result in mechanism name changes on a library update, we
439 have little choice but to default it off and let the admin choose to
442 Earlier library versions need this set early, during the _start() call,
443 so we had to misuse gsasl_callback_hook_set/get() as a data transfer
444 mech for the callback done at that time to get the bind-data. More recently
445 the callback is done (if needed) during the first gsasl_stop(). We know
446 the bind-data here so can set it (and should not get a callback).
448 if (ob->server_channelbinding)
450 HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
452 # ifndef CHANNELBIND_HACK
453 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CCS tls_in.channelbinding);
458 debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
463 debug_printf("Auth %s: no channel-binding data available\n",
467 checked_server_condition = FALSE;
469 received = CS initial_data;
471 exim_error = exim_error_override = OK;
474 switch (rc = gsasl_step64(sctx, received, &to_send))
478 goto STOP_INTERACTION;
481 case GSASL_NEEDS_MORE:
484 case GSASL_AUTHENTICATION_ERROR:
485 case GSASL_INTEGRITY_ERROR:
486 case GSASL_NO_AUTHID:
487 case GSASL_NO_ANONYMOUS_TOKEN:
488 case GSASL_NO_AUTHZID:
489 case GSASL_NO_PASSWORD:
490 case GSASL_NO_PASSCODE:
492 case GSASL_BASE64_ERROR:
493 HDEBUG(D_auth) debug_printf("GNU SASL permanent error: %s (%s)\n",
494 gsasl_strerror_name(rc), gsasl_strerror(rc));
495 log_write(0, LOG_REJECT, "%s authenticator (%s):\n "
496 "GNU SASL permanent failure: %s (%s)",
497 ablock->name, ob->server_mech,
498 gsasl_strerror_name(rc), gsasl_strerror(rc));
499 if (rc == GSASL_BASE64_ERROR)
500 exim_error_override = BAD64;
501 goto STOP_INTERACTION;
504 auth_defer_msg = string_sprintf("GNU SASL temporary error: %s (%s)",
505 gsasl_strerror_name(rc), gsasl_strerror(rc));
506 HDEBUG(D_auth) debug_printf("%s\n", auth_defer_msg);
507 exim_error_override = DEFER;
508 goto STOP_INTERACTION;
511 /*XXX having our caller send the final smtp "235" is unfortunate; wastes a roundtrip */
512 if ((rc == GSASL_NEEDS_MORE) || (to_send && *to_send))
513 exim_error = auth_get_no64_data(USS &received, US to_send);
522 break; /* handles * cancelled check */
524 } while (rc == GSASL_NEEDS_MORE);
532 if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_ITER)))
533 debug_printf(" - itercnt: '%s'\n", s);
534 if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_SALT)))
535 debug_printf(" - salt: '%s'\n", s);
536 #ifdef EXIM_GSASL_SCRAM_S_KEY
537 if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_SERVERKEY)))
538 debug_printf(" - ServerKey: '%s'\n", s);
539 if ((s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_STOREDKEY)))
540 debug_printf(" - StoredKey: '%s'\n", s);
546 /* Can return: OK DEFER FAIL CANCELLED BAD64 UNEXPECTED */
548 if (exim_error != OK)
551 if (auth_result != GSASL_OK)
553 HDEBUG(D_auth) debug_printf("authentication returned %s (%s)\n",
554 gsasl_strerror_name(auth_result), gsasl_strerror(auth_result));
555 if (exim_error_override != OK)
556 return exim_error_override; /* might be DEFER */
557 if (sasl_error_should_defer) /* overriding auth failure SASL error */
562 /* Auth succeeded, check server_condition unless already done in callback */
563 return checked_server_condition ? OK : auth_check_serv_cond(ablock);
567 /* returns the GSASL status of expanding the Exim string given */
569 condition_check(auth_instance *ablock, uschar *label, uschar *condition_string)
571 int exim_rc = auth_check_some_cond(ablock, label, condition_string, FAIL);
574 case OK: return GSASL_OK;
575 case DEFER: sasl_error_should_defer = TRUE;
576 return GSASL_AUTHENTICATION_ERROR;
577 case FAIL: return GSASL_AUTHENTICATION_ERROR;
578 default: log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s authenticator: "
579 "Unhandled return from checking %s: %d",
580 ablock->name, label, exim_rc);
584 return GSASL_AUTHENTICATION_ERROR;
589 set_exim_authvar_from_prop(Gsasl_session * sctx, Gsasl_property prop)
591 uschar * propval = US gsasl_property_fast(sctx, prop);
592 int i = expand_nmax, j = i + 1;
593 propval = propval ? string_copy(propval) : US"";
594 auth_vars[i] = expand_nstring[j] = propval;
595 expand_nlength[j] = Ustrlen(propval);
600 set_exim_authvars_from_a_az_r_props(Gsasl_session * sctx)
602 if (expand_nmax > 0 ) return;
604 /* Asking for GSASL_AUTHZID calls back into us if we use
605 gsasl_property_get(), thus the use of gsasl_property_fast().
606 Do we really want to hardcode limits per mechanism? What happens when
607 a new mechanism is added to the library. It *shouldn't* result in us
608 needing to add more glue, since avoiding that is a large part of the
611 set_exim_authvar_from_prop(sctx, GSASL_AUTHID);
612 set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
613 set_exim_authvar_from_prop(sctx, GSASL_REALM);
618 prop_from_option(Gsasl_session * sctx, Gsasl_property prop,
619 const uschar * option)
621 HDEBUG(D_auth) debug_printf(" %s\n", gsasl_prop_code_to_name(prop));
624 set_exim_authvars_from_a_az_r_props(sctx);
625 option = expand_cstring(option);
626 HDEBUG(D_auth) debug_printf(" '%s'\n", option);
628 gsasl_property_set(sctx, prop, CCS option);
631 HDEBUG(D_auth) debug_printf(" option not set\n");
632 return GSASL_NO_CALLBACK;
636 server_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop,
637 auth_instance *ablock)
641 int cbrc = GSASL_NO_CALLBACK;
642 auth_gsasl_options_block *ob =
643 (auth_gsasl_options_block *)(ablock->options_block);
645 HDEBUG(D_auth) debug_printf("GNU SASL callback %s for %s/%s as server\n",
646 gsasl_prop_code_to_name(prop), ablock->name, ablock->public_name);
648 for (int i = 0; i < AUTH_VARS; i++) auth_vars[i] = NULL;
653 case GSASL_VALIDATE_SIMPLE:
654 /* GSASL_AUTHID, GSASL_AUTHZID, and GSASL_PASSWORD */
655 set_exim_authvar_from_prop(sctx, GSASL_AUTHID);
656 set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
657 set_exim_authvar_from_prop(sctx, GSASL_PASSWORD);
659 cbrc = condition_check(ablock, US"server_condition", ablock->server_condition);
660 checked_server_condition = TRUE;
663 case GSASL_VALIDATE_EXTERNAL:
664 if (!ablock->server_condition)
666 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate EXTERNAL\n");
667 cbrc = GSASL_AUTHENTICATION_ERROR;
670 set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
672 cbrc = condition_check(ablock,
673 US"server_condition (EXTERNAL)", ablock->server_condition);
674 checked_server_condition = TRUE;
677 case GSASL_VALIDATE_ANONYMOUS:
678 if (!ablock->server_condition)
680 HDEBUG(D_auth) debug_printf("No server_condition supplied, to validate ANONYMOUS\n");
681 cbrc = GSASL_AUTHENTICATION_ERROR;
684 set_exim_authvar_from_prop(sctx, GSASL_ANONYMOUS_TOKEN);
686 cbrc = condition_check(ablock,
687 US"server_condition (ANONYMOUS)", ablock->server_condition);
688 checked_server_condition = TRUE;
691 case GSASL_VALIDATE_GSSAPI:
692 /* GSASL_AUTHZID and GSASL_GSSAPI_DISPLAY_NAME
693 The display-name is authenticated as part of GSS, the authzid is claimed
694 by the SASL integration after authentication; protected against tampering
695 (if the SASL mechanism supports that, which Kerberos does) but is
696 unverified, same as normal for other mechanisms.
697 First coding, we had these values swapped, but for consistency and prior
698 to the first release of Exim with this authenticator, they've been
699 switched to match the ordering of GSASL_VALIDATE_SIMPLE. */
701 set_exim_authvar_from_prop(sctx, GSASL_GSSAPI_DISPLAY_NAME);
702 set_exim_authvar_from_prop(sctx, GSASL_AUTHZID);
704 /* In this one case, it perhaps makes sense to default back open?
705 But for consistency, let's just mandate server_condition here too. */
707 cbrc = condition_check(ablock,
708 US"server_condition (GSSAPI family)", ablock->server_condition);
709 checked_server_condition = TRUE;
712 case GSASL_SCRAM_ITER:
713 cbrc = prop_from_option(sctx, prop, ob->server_scram_iter);
716 case GSASL_SCRAM_SALT:
717 cbrc = prop_from_option(sctx, prop, ob->server_scram_salt);
720 #ifdef EXIM_GSASL_SCRAM_S_KEY
721 case GSASL_SCRAM_STOREDKEY:
722 cbrc = prop_from_option(sctx, prop, ob->server_s_key);
725 case GSASL_SCRAM_SERVERKEY:
726 cbrc = prop_from_option(sctx, prop, ob->server_key);
731 /* SCRAM-*: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
732 DIGEST-MD5: GSASL_AUTHID, GSASL_AUTHZID and GSASL_REALM
733 CRAM-MD5: GSASL_AUTHID
734 PLAIN: GSASL_AUTHID and GSASL_AUTHZID
737 set_exim_authvars_from_a_az_r_props(sctx);
739 if (!(s = ob->server_password))
741 HDEBUG(D_auth) debug_printf("option not set\n");
744 if (!(tmps = CS expand_string(s)))
746 sasl_error_should_defer = !f.expand_string_forcedfail;
747 HDEBUG(D_auth) debug_printf("server_password expansion failed, so "
748 "can't tell GNU SASL library the password for %s\n", auth_vars[0]);
749 return GSASL_AUTHENTICATION_ERROR;
751 HDEBUG(D_auth) debug_printf(" set\n");
752 gsasl_property_set(sctx, GSASL_PASSWORD, tmps);
754 /* This is inadequate; don't think Exim's store stacks are geared
755 for memory wiping, so expanding strings will leave stuff laying around.
756 But no need to compound the problem, so get rid of the one we can. */
758 memset(tmps, '\0', strlen(tmps));
763 HDEBUG(D_auth) debug_printf(" Unrecognised callback: %d\n", prop);
764 cbrc = GSASL_NO_CALLBACK;
767 HDEBUG(D_auth) debug_printf("Returning %s (%s)\n",
768 gsasl_strerror_name(cbrc), gsasl_strerror(cbrc));
774 /******************************************************************************/
776 #define PROP_OPTIONAL BIT(0)
779 set_client_prop(Gsasl_session * sctx, Gsasl_property prop, uschar * val,
780 unsigned flags, uschar * buffer, int buffsize)
785 if (!val) return !!(flags & PROP_OPTIONAL);
786 if (!(s = expand_string(val)) || !(flags & PROP_OPTIONAL) && !*s)
788 string_format(buffer, buffsize, "%s", expand_string_message);
793 HDEBUG(D_auth) debug_printf("%s: set %s = '%s'\n", __FUNCTION__,
794 gsasl_prop_code_to_name(prop), s);
795 gsasl_property_set(sctx, prop, CS s);
801 /*************************************************
802 * Client entry point *
803 *************************************************/
805 /* For interface, see auths/README */
809 auth_instance *ablock, /* authenticator block */
810 void * sx, /* connection */
811 int timeout, /* command timeout */
812 uschar *buffer, /* buffer for reading response */
813 int buffsize) /* size of buffer */
815 auth_gsasl_options_block *ob =
816 (auth_gsasl_options_block *)(ablock->options_block);
817 Gsasl_session * sctx = NULL;
818 struct callback_exim_state cb_state;
821 int rc, yield = FAIL;
824 debug_printf("GNU SASL: initialising session for %s, mechanism %s\n",
825 ablock->name, ob->server_mech);
830 if (tls_out.channelbinding && ob->client_channelbinding)
832 # ifndef DISABLE_TLS_RESUME
833 if (!tls_out.ext_master_secret && tls_out.resumption == RESUME_USED)
834 { /* per RFC 7677 section 4 */
835 string_format(buffer, buffsize, "%s",
836 "channel binding not usable on resumed TLS without extended-master-secret");
840 # ifdef CHANNELBIND_HACK
841 /* This is a gross hack to get around the library before 1.9.2
842 a) requiring that c-b was already set, at the _start() call, and
843 b) caching a b64'd version of the binding then which it never updates. */
845 gsasl_callback_hook_set(gsasl_ctx, tls_out.channelbinding);
850 if ((rc = gsasl_client_start(gsasl_ctx, CCS ob->server_mech, &sctx)) != GSASL_OK)
852 string_format(buffer, buffsize, "GNU SASL: session start failure: %s (%s)",
853 gsasl_strerror_name(rc), gsasl_strerror(rc));
854 HDEBUG(D_auth) debug_printf("%s\n", buffer);
858 cb_state.ablock = ablock;
859 cb_state.currently = CURRENTLY_CLIENT;
860 gsasl_session_hook_set(sctx, &cb_state);
864 if ( !set_client_prop(sctx, GSASL_SCRAM_SALTED_PASSWORD, ob->client_spassword,
867 !set_client_prop(sctx, GSASL_PASSWORD, ob->client_password,
869 || !set_client_prop(sctx, GSASL_AUTHID, ob->client_username,
871 || !set_client_prop(sctx, GSASL_AUTHZID, ob->client_authz,
872 PROP_OPTIONAL, buffer, buffsize)
877 if (tls_out.channelbinding)
878 if (ob->client_channelbinding)
880 HDEBUG(D_auth) debug_printf("Auth %s: Enabling channel-binding\n",
882 # ifndef CHANNELBIND_HACK
883 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CCS tls_out.channelbinding);
888 debug_printf("Auth %s: Not enabling channel-binding (data available)\n",
892 /* Run the SASL conversation with the server */
899 rc = gsasl_step64(sctx, CS s, CSS &outstr);
902 ? smtp_write_command(sx, SCMD_FLUSH,
903 outstr ? "AUTH %s %s\r\n" : "AUTH %s\r\n",
904 ablock->public_name, outstr) <= 0
906 ? smtp_write_command(sx, SCMD_FLUSH, "%s\r\n", outstr) <= 0
908 if (outstr && *outstr) free(outstr);
916 if (rc != GSASL_NEEDS_MORE)
920 string_format(buffer, buffsize, "gsasl: %s", gsasl_strerror(rc));
924 /* expecting a final 2xx from the server, accepting the AUTH */
926 if (smtp_read_response(sx, buffer, buffsize, '2', timeout))
928 break; /* from SASL sequence loop */
931 /* 2xx or 3xx response is acceptable. If 2xx, no further input */
933 if (!smtp_read_response(sx, buffer, buffsize, '3', timeout))
934 if (errno == 0 && buffer[0] == '2')
947 const uschar * s = CUS gsasl_property_fast(sctx, GSASL_SCRAM_SALTED_PASSWORD);
948 if (s) debug_printf(" - SaltedPassword: '%s'\n", s);
956 client_callback(Gsasl *ctx, Gsasl_session *sctx, Gsasl_property prop, auth_instance *ablock)
958 HDEBUG(D_auth) debug_printf("GNU SASL callback %s for %s/%s as client\n",
959 gsasl_prop_code_to_name(prop), ablock->name, ablock->public_name);
962 case GSASL_CB_TLS_UNIQUE: /*XXX should never get called for this */
964 debug_printf(" filling in\n");
965 gsasl_property_set(sctx, GSASL_CB_TLS_UNIQUE, CCS tls_out.channelbinding);
969 debug_printf(" not providing one\n");
972 return GSASL_NO_CALLBACK;
975 /*************************************************
977 *************************************************/
980 auth_gsasl_version_report(FILE *f)
983 runtime = gsasl_check_version(NULL);
984 fprintf(f, "Library version: GNU SASL: Compile: %s\n"
986 GSASL_VERSION, runtime);
992 void auth_gsasl_macros(void) {}
994 #endif /*!MACRO_PREDEF*/
995 #endif /* AUTH_GSASL */
997 /* End of gsasl_exim.c */