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 */
17 #include "mutt_sasl.h"
18 #include "imap_private.h"
22 #include <sasl/sasl.h>
23 #include <sasl/saslutil.h>
29 /* imap_auth_sasl: Default authenticator if available. */
30 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
32 sasl_conn_t *saslconn;
33 sasl_interact_t *interaction = NULL;
35 char buf[HUGE_STRING];
39 const char *pc = NULL;
43 unsigned int len, olen;
44 unsigned char client_start;
46 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 */
56 method = idata->capstr;
58 /* hack for SASL ANONYMOUS support:
59 * 1. Fetch username. If it's "" or "anonymous" then
60 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
61 * 3. if sasl_client_start fails, fall through... */
63 if (mutt_account_getuser (&idata->conn->account))
64 return IMAP_AUTH_FAILURE;
66 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
67 (!idata->conn->account.user[0] ||
68 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
70 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
74 sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
79 if (rc != SASL_OK && rc != SASL_CONTINUE)
82 rc = sasl_client_start (saslconn, method, &interaction,
85 rc = sasl_client_start (saslconn, method, NULL, &interaction,
88 if (rc == SASL_INTERACT)
89 mutt_sasl_interact (interaction);
91 while (rc == SASL_INTERACT);
93 client_start = (olen > 0);
95 if (rc != SASL_OK && rc != SASL_CONTINUE) {
97 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
101 "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
102 /* SASL doesn't support LOGIN, so fall back */
104 return IMAP_AUTH_UNAVAIL;
107 mutt_message (_("Authenticating (%s)..."), mech);
109 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
110 imap_cmd_start (idata, buf);
111 irc = IMAP_CMD_CONTINUE;
113 /* looping protocol */
114 while (rc == SASL_CONTINUE || olen > 0) {
116 irc = imap_cmd_step (idata);
117 while (irc == IMAP_CMD_CONTINUE);
119 if (method && irc == IMAP_CMD_NO) {
120 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
121 sasl_dispose (&saslconn);
122 return IMAP_AUTH_UNAVAIL;
125 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
128 if (irc == IMAP_CMD_RESPOND) {
131 (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
134 if (sasl_decode64 (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
139 "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 */
157 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
160 "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) {
172 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
173 mutt_socket_write (idata->conn, buf);
176 /* If SASL has errored out, send an abort string to the server */
178 mutt_socket_write (idata->conn, "*\r\n");
180 (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
186 while (irc != IMAP_CMD_OK)
187 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
193 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;