2 * Copyright (C) 2000-6 Brendan Cully <brendan@kublai.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 /* SASL login/authentication code */
26 #include "mutt_sasl.h"
27 #include "imap_private.h"
30 #include <sasl/sasl.h>
31 #include <sasl/saslutil.h>
33 /* imap_auth_sasl: Default authenticator if available. */
34 imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method)
36 sasl_conn_t* saslconn;
37 sasl_interact_t* interaction = NULL;
39 char buf[HUGE_STRING];
41 const char *pc = NULL;
42 unsigned int len, olen;
43 unsigned char client_start;
45 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0)
47 dprint (1, (debugfile,
48 "imap_auth_sasl: Error allocating SASL connection.\n"));
49 return IMAP_AUTH_FAILURE;
54 /* If the user hasn't specified a method, use any available */
57 method = idata->capstr;
59 /* hack for SASL ANONYMOUS support:
60 * 1. Fetch username. If it's "" or "anonymous" then
61 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
62 * 3. if sasl_client_start fails, fall through... */
64 if (mutt_account_getuser (&idata->conn->account))
65 return IMAP_AUTH_FAILURE;
67 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
68 (!idata->conn->account.user[0] ||
69 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
70 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
74 if (rc != SASL_OK && rc != SASL_CONTINUE)
77 rc = sasl_client_start (saslconn, method, &interaction,
79 if (rc == SASL_INTERACT)
80 mutt_sasl_interact (interaction);
82 while (rc == SASL_INTERACT);
84 client_start = (olen > 0);
86 if (rc != SASL_OK && rc != SASL_CONTINUE)
89 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
91 dprint (1, (debugfile, "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
92 /* SASL doesn't support LOGIN, so fall back */
94 return IMAP_AUTH_UNAVAIL;
97 mutt_message (_("Authenticating (%s)..."), mech);
99 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
100 if (mutt_bit_isset (idata->capabilities, SASL_IR) && client_start)
102 len = mutt_strlen (buf);
104 if (sasl_encode64 (pc, olen, buf + len, sizeof (buf) - len, &olen) != SASL_OK)
106 dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
109 client_start = olen = 0;
111 imap_cmd_start (idata, buf);
112 irc = IMAP_CMD_CONTINUE;
114 /* looping protocol */
115 while (rc == SASL_CONTINUE || olen > 0)
118 irc = imap_cmd_step (idata);
119 while (irc == IMAP_CMD_CONTINUE);
121 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
124 if (irc == IMAP_CMD_RESPOND)
126 /* Exchange incorrectly returns +\r\n instead of + \r\n */
127 if (idata->buf[1] == '\0')
132 else if (sasl_decode64 (idata->buf+2, strlen (idata->buf+2), buf,
133 LONG_STRING-1, &len) != SASL_OK)
135 dprint (1, (debugfile, "imap_auth_sasl: error base64-decoding server response.\n"));
140 /* client-start is only available with the SASL-IR extension, but
141 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
142 * fast reauth. Override if the server sent an initial continuation */
143 if (!client_start || buf[0])
147 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
148 if (rc == SASL_INTERACT)
149 mutt_sasl_interact (interaction);
151 while (rc == SASL_INTERACT);
156 /* send out response, or line break if none needed */
159 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
161 dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
166 if (irc == IMAP_CMD_RESPOND)
168 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
169 mutt_socket_write (idata->conn, buf);
172 /* If SASL has errored out, send an abort string to the server */
175 mutt_socket_write (idata->conn, "*\r\n");
176 dprint (1, (debugfile, "imap_auth_sasl: sasl_client_step error %d\n",rc));
182 while (irc != IMAP_CMD_OK)
183 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
189 if (imap_code (idata->buf))
191 mutt_sasl_setup_conn (idata->conn, saslconn);
192 return IMAP_AUTH_SUCCESS;
196 sasl_dispose (&saslconn);
200 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
201 return IMAP_AUTH_UNAVAIL;
204 mutt_error _("SASL authentication failed.");
207 return IMAP_AUTH_FAILURE;