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 */
22 #include "mutt_sasl.h"
23 #include "imap_private.h"
27 #include <sasl/sasl.h>
28 #include <sasl/saslutil.h>
34 /* imap_auth_sasl: Default authenticator if available. */
35 imap_auth_res_t imap_auth_sasl (IMAP_DATA* idata, const char* method)
37 sasl_conn_t* saslconn;
38 sasl_interact_t* interaction = NULL;
40 char buf[HUGE_STRING];
43 const char *pc = NULL;
47 unsigned int len, olen;
48 unsigned char client_start;
50 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0)
52 dprint (1, (debugfile,
53 "imap_auth_sasl: Error allocating SASL connection.\n"));
54 return IMAP_AUTH_FAILURE;
59 /* If the user hasn't specified a method, use any available */
62 method = idata->capstr;
64 /* hack for SASL ANONYMOUS support:
65 * 1. Fetch username. If it's "" or "anonymous" then
66 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
67 * 3. if sasl_client_start fails, fall through... */
69 if (mutt_account_getuser (&idata->conn->account))
70 return IMAP_AUTH_FAILURE;
72 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
73 (!idata->conn->account.user[0] ||
74 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
76 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
79 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
84 if (rc != SASL_OK && rc != SASL_CONTINUE)
88 rc = sasl_client_start (saslconn, method, &interaction,
91 rc = sasl_client_start (saslconn, method, NULL, &interaction,
94 if (rc == SASL_INTERACT)
95 mutt_sasl_interact (interaction);
97 while (rc == SASL_INTERACT);
99 client_start = (olen > 0);
101 if (rc != SASL_OK && rc != SASL_CONTINUE)
104 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
106 dprint (1, (debugfile, "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
107 /* SASL doesn't support LOGIN, so fall back */
109 return IMAP_AUTH_UNAVAIL;
112 mutt_message (_("Authenticating (%s)..."), mech);
114 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
115 imap_cmd_start (idata, buf);
116 irc = IMAP_CMD_CONTINUE;
118 /* looping protocol */
119 while (rc == SASL_CONTINUE)
122 irc = imap_cmd_step (idata);
123 while (irc == IMAP_CMD_CONTINUE);
125 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
128 if (irc == IMAP_CMD_RESPOND)
131 if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf, LONG_STRING-1,
133 if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf,
137 dprint (1, (debugfile, "imap_auth_sasl: error base64-decoding server response.\n"));
146 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
147 if (rc == SASL_INTERACT)
148 mutt_sasl_interact (interaction);
150 while (rc == SASL_INTERACT);
155 /* send out response, or line break if none needed */
158 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
160 dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
164 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
171 if (irc == IMAP_CMD_RESPOND)
173 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
174 mutt_socket_write (idata->conn, buf);
177 /* If SASL has errored out, send an abort string to the server */
180 mutt_socket_write (idata->conn, "*\r\n");
181 dprint (1, (debugfile, "imap_auth_sasl: sasl_client_step error %d\n",rc));
185 while (irc != IMAP_CMD_OK)
186 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
192 if (imap_code (idata->cmd.buf))
194 mutt_sasl_setup_conn (idata->conn, saslconn);
195 return IMAP_AUTH_SUCCESS;
199 mutt_error _("SASL authentication failed.");
201 sasl_dispose (&saslconn);
203 return IMAP_AUTH_FAILURE;