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 */
16 #include <lib-lib/mem.h>
20 #include "mutt_sasl.h"
21 #include "imap_private.h"
26 #include "lib/debug.h"
28 #include <sasl/sasl.h>
29 #include <sasl/saslutil.h>
31 /* imap_auth_sasl: Default authenticator if available. */
32 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
34 sasl_conn_t *saslconn;
35 sasl_interact_t *interaction = NULL;
37 char buf[HUGE_STRING];
40 const char *pc = NULL;
41 unsigned int len, olen;
42 unsigned char client_start;
44 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
45 debug_print (1, ("Error allocating SASL connection.\n"));
46 return IMAP_AUTH_FAILURE;
51 /* If the user hasn't specified a method, use any available */
53 method = idata->capstr;
55 /* hack for SASL ANONYMOUS support:
56 * 1. Fetch username. If it's "" or "anonymous" then
57 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
58 * 3. if sasl_client_start fails, fall through... */
60 if (mutt_account_getuser (&idata->conn->account))
61 return IMAP_AUTH_FAILURE;
63 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
64 (!idata->conn->account.user[0] ||
65 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
66 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
70 if (rc != SASL_OK && rc != SASL_CONTINUE)
72 rc = sasl_client_start (saslconn, method, &interaction,
74 if (rc == SASL_INTERACT)
75 mutt_sasl_interact (interaction);
77 while (rc == SASL_INTERACT);
79 client_start = (olen > 0);
81 if (rc != SASL_OK && rc != SASL_CONTINUE) {
83 debug_print (2, ("%s unavailable\n", method));
85 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
86 /* SASL doesn't support LOGIN, so fall back */
88 return IMAP_AUTH_UNAVAIL;
91 mutt_message (_("Authenticating (%s)..."), mech);
93 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
94 imap_cmd_start (idata, buf);
95 irc = IMAP_CMD_CONTINUE;
97 /* looping protocol */
98 while (rc == SASL_CONTINUE || olen > 0) {
100 irc = imap_cmd_step (idata);
101 while (irc == IMAP_CMD_CONTINUE);
103 if (method && irc == IMAP_CMD_NO) {
104 debug_print (2, ("%s failed\n", method));
105 sasl_dispose (&saslconn);
106 return IMAP_AUTH_UNAVAIL;
109 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
112 if (irc == IMAP_CMD_RESPOND) {
114 (idata->cmd.buf + 2, str_len (idata->cmd.buf + 2), buf,
117 debug_print (1, ("error base64-decoding server response.\n"));
122 /* client-start is only available with the SASL-IR extension, but
123 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
124 * fast reauth. Override if the server sent an initial continuation */
125 if (!client_start || buf[0]) {
127 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
128 if (rc == SASL_INTERACT)
129 mutt_sasl_interact (interaction);
131 while (rc == SASL_INTERACT);
136 /* send out response, or line break if none needed */
138 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
139 debug_print (1, ("error base64-encoding client response.\n"));
144 if (irc == IMAP_CMD_RESPOND) {
145 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
146 mutt_socket_write (idata->conn, buf);
149 /* If SASL has errored out, send an abort string to the server */
151 mutt_socket_write (idata->conn, "*\r\n");
152 debug_print (1, ("sasl_client_step error %d\n", rc));
158 while (irc != IMAP_CMD_OK)
159 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
165 if (imap_code (idata->cmd.buf)) {
166 mutt_sasl_setup_conn (idata->conn, saslconn);
167 return IMAP_AUTH_SUCCESS;
171 mutt_error _("SASL authentication failed.");
173 sasl_dispose (&saslconn);
175 return IMAP_AUTH_FAILURE;