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];
47 const char *pc = NULL;
51 unsigned int len, olen;
52 unsigned char client_start;
54 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 */
66 method = idata->capstr;
68 /* hack for SASL ANONYMOUS support:
69 * 1. Fetch username. If it's "" or "anonymous" then
70 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
71 * 3. if sasl_client_start fails, fall through... */
73 if (mutt_account_getuser (&idata->conn->account))
74 return IMAP_AUTH_FAILURE;
76 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
77 (!idata->conn->account.user[0] ||
78 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
80 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
83 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
88 if (rc != SASL_OK && rc != SASL_CONTINUE)
92 rc = sasl_client_start (saslconn, method, &interaction,
95 rc = sasl_client_start (saslconn, method, NULL, &interaction,
98 if (rc == SASL_INTERACT)
99 mutt_sasl_interact (interaction);
101 while (rc == SASL_INTERACT);
103 client_start = (olen > 0);
105 if (rc != SASL_OK && rc != SASL_CONTINUE)
108 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
110 dprint (1, (debugfile, "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)
126 irc = imap_cmd_step (idata);
127 while (irc == IMAP_CMD_CONTINUE);
129 if (method && irc == IMAP_CMD_NO)
131 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
132 sasl_dispose (&saslconn);
133 return IMAP_AUTH_UNAVAIL;
136 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
139 if (irc == IMAP_CMD_RESPOND)
142 if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf, LONG_STRING-1,
144 if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf,
148 dprint (1, (debugfile, "imap_auth_sasl: error base64-decoding server response.\n"));
157 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
158 if (rc == SASL_INTERACT)
159 mutt_sasl_interact (interaction);
161 while (rc == SASL_INTERACT);
166 /* send out response, or line break if none needed */
169 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
171 dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
175 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
182 if (irc == IMAP_CMD_RESPOND)
184 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
185 mutt_socket_write (idata->conn, buf);
188 /* If SASL has errored out, send an abort string to the server */
191 mutt_socket_write (idata->conn, "*\r\n");
192 dprint (1, (debugfile, "imap_auth_sasl: sasl_client_step error %d\n",rc));
198 while (irc != IMAP_CMD_OK)
199 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
205 if (imap_code (idata->cmd.buf))
207 mutt_sasl_setup_conn (idata->conn, saslconn);
208 return IMAP_AUTH_SUCCESS;
212 mutt_error _("SASL authentication failed.");
214 sasl_dispose (&saslconn);
216 return IMAP_AUTH_FAILURE;