2 * Copyright (C) 2000-3 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /* SASL login/authentication code */
26 #include "mutt_sasl.h"
27 #include "imap_private.h"
31 #include <sasl/sasl.h>
32 #include <sasl/saslutil.h>
38 /* imap_auth_sasl: Default authenticator if available. */
39 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
41 sasl_conn_t *saslconn;
42 sasl_interact_t *interaction = NULL;
44 char buf[HUGE_STRING];
48 const char *pc = NULL;
52 unsigned int len, olen;
53 unsigned char client_start;
55 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
56 dprint (1, (debugfile,
57 "imap_auth_sasl: Error allocating SASL connection.\n"));
58 return IMAP_AUTH_FAILURE;
63 /* If the user hasn't specified a method, use any available */
65 method = idata->capstr;
67 /* hack for SASL ANONYMOUS support:
68 * 1. Fetch username. If it's "" or "anonymous" then
69 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
70 * 3. if sasl_client_start fails, fall through... */
72 if (mutt_account_getuser (&idata->conn->account))
73 return IMAP_AUTH_FAILURE;
75 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
76 (!idata->conn->account.user[0] ||
77 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
79 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
83 sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
88 if (rc != SASL_OK && rc != SASL_CONTINUE)
91 rc = sasl_client_start (saslconn, method, &interaction,
94 rc = sasl_client_start (saslconn, method, NULL, &interaction,
97 if (rc == SASL_INTERACT)
98 mutt_sasl_interact (interaction);
100 while (rc == SASL_INTERACT);
102 client_start = (olen > 0);
104 if (rc != SASL_OK && rc != SASL_CONTINUE) {
106 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
110 "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
111 /* SASL doesn't support LOGIN, so fall back */
113 return IMAP_AUTH_UNAVAIL;
116 mutt_message (_("Authenticating (%s)..."), mech);
118 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
119 imap_cmd_start (idata, buf);
120 irc = IMAP_CMD_CONTINUE;
122 /* looping protocol */
123 while (rc == SASL_CONTINUE || olen > 0) {
125 irc = imap_cmd_step (idata);
126 while (irc == IMAP_CMD_CONTINUE);
128 if (method && irc == IMAP_CMD_NO) {
129 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
130 sasl_dispose (&saslconn);
131 return IMAP_AUTH_UNAVAIL;
134 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
137 if (irc == IMAP_CMD_RESPOND) {
140 (idata->cmd.buf + 2, strlen (idata->cmd.buf + 2), buf,
143 if (sasl_decode64 (idata->cmd.buf + 2, strlen (idata->cmd.buf + 2), buf,
148 "imap_auth_sasl: error base64-decoding server response.\n"));
155 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
156 if (rc == SASL_INTERACT)
157 mutt_sasl_interact (interaction);
159 while (rc == SASL_INTERACT);
164 /* send out response, or line break if none needed */
166 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
169 "imap_auth_sasl: error base64-encoding client response.\n"));
173 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
180 if (irc == IMAP_CMD_RESPOND) {
181 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
182 mutt_socket_write (idata->conn, buf);
185 /* If SASL has errored out, send an abort string to the server */
187 mutt_socket_write (idata->conn, "*\r\n");
189 (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
195 while (irc != IMAP_CMD_OK)
196 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
202 if (imap_code (idata->cmd.buf)) {
203 mutt_sasl_setup_conn (idata->conn, saslconn);
204 return IMAP_AUTH_SUCCESS;
208 mutt_error _("SASL authentication failed.");
210 sasl_dispose (&saslconn);
212 return IMAP_AUTH_FAILURE;