2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-3 Brendan Cully <brendan@kublai.com>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* SASL login/authentication code */
18 #include "mutt_sasl.h"
19 #include "imap_private.h"
24 #include "lib/debug.h"
26 #include <sasl/sasl.h>
27 #include <sasl/saslutil.h>
29 /* imap_auth_sasl: Default authenticator if available. */
30 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
32 sasl_conn_t *saslconn;
33 sasl_interact_t *interaction = NULL;
35 char buf[HUGE_STRING];
38 const char *pc = NULL;
39 unsigned int len, olen;
40 unsigned char client_start;
42 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
43 debug_print (1, ("Error allocating SASL connection.\n"));
44 return IMAP_AUTH_FAILURE;
49 /* If the user hasn't specified a method, use any available */
51 method = idata->capstr;
53 /* hack for SASL ANONYMOUS support:
54 * 1. Fetch username. If it's "" or "anonymous" then
55 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
56 * 3. if sasl_client_start fails, fall through... */
58 if (mutt_account_getuser (&idata->conn->account))
59 return IMAP_AUTH_FAILURE;
61 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
62 (!idata->conn->account.user[0] ||
63 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
64 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
68 if (rc != SASL_OK && rc != SASL_CONTINUE)
70 rc = sasl_client_start (saslconn, method, &interaction,
72 if (rc == SASL_INTERACT)
73 mutt_sasl_interact (interaction);
75 while (rc == SASL_INTERACT);
77 client_start = (olen > 0);
79 if (rc != SASL_OK && rc != SASL_CONTINUE) {
81 debug_print (2, ("%s unavailable\n", method));
83 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
84 /* SASL doesn't support LOGIN, so fall back */
86 return IMAP_AUTH_UNAVAIL;
89 mutt_message (_("Authenticating (%s)..."), mech);
91 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
92 imap_cmd_start (idata, buf);
93 irc = IMAP_CMD_CONTINUE;
95 /* looping protocol */
96 while (rc == SASL_CONTINUE || olen > 0) {
98 irc = imap_cmd_step (idata);
99 while (irc == IMAP_CMD_CONTINUE);
101 if (method && irc == IMAP_CMD_NO) {
102 debug_print (2, ("%s failed\n", method));
103 sasl_dispose (&saslconn);
104 return IMAP_AUTH_UNAVAIL;
107 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
110 if (irc == IMAP_CMD_RESPOND) {
112 (idata->cmd.buf + 2, str_len (idata->cmd.buf + 2), buf,
115 debug_print (1, ("error base64-decoding server response.\n"));
120 /* client-start is only available with the SASL-IR extension, but
121 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
122 * fast reauth. Override if the server sent an initial continuation */
123 if (!client_start || buf[0]) {
125 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
126 if (rc == SASL_INTERACT)
127 mutt_sasl_interact (interaction);
129 while (rc == SASL_INTERACT);
134 /* send out response, or line break if none needed */
136 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
137 debug_print (1, ("error base64-encoding client response.\n"));
142 if (irc == IMAP_CMD_RESPOND) {
143 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
144 mutt_socket_write (idata->conn, buf);
147 /* If SASL has errored out, send an abort string to the server */
149 mutt_socket_write (idata->conn, "*\r\n");
150 debug_print (1, ("sasl_client_step error %d\n", rc));
156 while (irc != IMAP_CMD_OK)
157 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
163 if (imap_code (idata->cmd.buf)) {
164 mutt_sasl_setup_conn (idata->conn, saslconn);
165 return IMAP_AUTH_SUCCESS;
169 mutt_error _("SASL authentication failed.");
171 sasl_dispose (&saslconn);
173 return IMAP_AUTH_FAILURE;