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"
24 #include <sasl/sasl.h>
25 #include <sasl/saslutil.h>
31 /* imap_auth_sasl: Default authenticator if available. */
32 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
34 sasl_conn_t *saslconn;
35 sasl_interact_t *interaction = NULL;
37 char buf[HUGE_STRING];
41 const char *pc = NULL;
45 unsigned int len, olen;
46 unsigned char client_start;
48 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
49 dprint (1, (debugfile,
50 "imap_auth_sasl: Error allocating SASL connection.\n"));
51 return IMAP_AUTH_FAILURE;
56 /* If the user hasn't specified a method, use any available */
58 method = idata->capstr;
60 /* hack for SASL ANONYMOUS support:
61 * 1. Fetch username. If it's "" or "anonymous" then
62 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
63 * 3. if sasl_client_start fails, fall through... */
65 if (mutt_account_getuser (&idata->conn->account))
66 return IMAP_AUTH_FAILURE;
68 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
69 (!idata->conn->account.user[0] ||
70 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
72 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
76 sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
81 if (rc != SASL_OK && rc != SASL_CONTINUE)
84 rc = sasl_client_start (saslconn, method, &interaction,
87 rc = sasl_client_start (saslconn, method, NULL, &interaction,
90 if (rc == SASL_INTERACT)
91 mutt_sasl_interact (interaction);
93 while (rc == SASL_INTERACT);
95 client_start = (olen > 0);
97 if (rc != SASL_OK && rc != SASL_CONTINUE) {
99 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
103 "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
104 /* SASL doesn't support LOGIN, so fall back */
106 return IMAP_AUTH_UNAVAIL;
109 mutt_message (_("Authenticating (%s)..."), mech);
111 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
112 imap_cmd_start (idata, buf);
113 irc = IMAP_CMD_CONTINUE;
115 /* looping protocol */
116 while (rc == SASL_CONTINUE || olen > 0) {
118 irc = imap_cmd_step (idata);
119 while (irc == IMAP_CMD_CONTINUE);
121 if (method && irc == IMAP_CMD_NO) {
122 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
123 sasl_dispose (&saslconn);
124 return IMAP_AUTH_UNAVAIL;
127 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
130 if (irc == IMAP_CMD_RESPOND) {
133 (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
136 if (sasl_decode64 (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
141 "imap_auth_sasl: error base64-decoding server response.\n"));
148 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
149 if (rc == SASL_INTERACT)
150 mutt_sasl_interact (interaction);
152 while (rc == SASL_INTERACT);
157 /* send out response, or line break if none needed */
159 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
162 "imap_auth_sasl: error base64-encoding client response.\n"));
166 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
173 if (irc == IMAP_CMD_RESPOND) {
174 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
175 mutt_socket_write (idata->conn, buf);
178 /* If SASL has errored out, send an abort string to the server */
180 mutt_socket_write (idata->conn, "*\r\n");
182 (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
188 while (irc != IMAP_CMD_OK)
189 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
195 if (imap_code (idata->cmd.buf)) {
196 mutt_sasl_setup_conn (idata->conn, saslconn);
197 return IMAP_AUTH_SUCCESS;
201 mutt_error _("SASL authentication failed.");
203 sasl_dispose (&saslconn);
205 return IMAP_AUTH_FAILURE;