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"
25 #include <sasl/sasl.h>
26 #include <sasl/saslutil.h>
32 /* imap_auth_sasl: Default authenticator if available. */
33 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
35 sasl_conn_t *saslconn;
36 sasl_interact_t *interaction = NULL;
38 char buf[HUGE_STRING];
42 const char *pc = NULL;
46 unsigned int len, olen;
47 unsigned char client_start;
49 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
50 dprint (1, (debugfile,
51 "imap_auth_sasl: Error allocating SASL connection.\n"));
52 return IMAP_AUTH_FAILURE;
57 /* If the user hasn't specified a method, use any available */
59 method = idata->capstr;
61 /* hack for SASL ANONYMOUS support:
62 * 1. Fetch username. If it's "" or "anonymous" then
63 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
64 * 3. if sasl_client_start fails, fall through... */
66 if (mutt_account_getuser (&idata->conn->account))
67 return IMAP_AUTH_FAILURE;
69 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
70 (!idata->conn->account.user[0] ||
71 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
73 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
77 sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
82 if (rc != SASL_OK && rc != SASL_CONTINUE)
85 rc = sasl_client_start (saslconn, method, &interaction,
88 rc = sasl_client_start (saslconn, method, NULL, &interaction,
91 if (rc == SASL_INTERACT)
92 mutt_sasl_interact (interaction);
94 while (rc == SASL_INTERACT);
96 client_start = (olen > 0);
98 if (rc != SASL_OK && rc != SASL_CONTINUE) {
100 dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
104 "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
105 /* SASL doesn't support LOGIN, so fall back */
107 return IMAP_AUTH_UNAVAIL;
110 mutt_message (_("Authenticating (%s)..."), mech);
112 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
113 imap_cmd_start (idata, buf);
114 irc = IMAP_CMD_CONTINUE;
116 /* looping protocol */
117 while (rc == SASL_CONTINUE || olen > 0) {
119 irc = imap_cmd_step (idata);
120 while (irc == IMAP_CMD_CONTINUE);
122 if (method && irc == IMAP_CMD_NO) {
123 dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
124 sasl_dispose (&saslconn);
125 return IMAP_AUTH_UNAVAIL;
128 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
131 if (irc == IMAP_CMD_RESPOND) {
134 (idata->cmd.buf + 2, safe_strlen (idata->cmd.buf + 2), buf,
137 if (sasl_decode64 (idata->cmd.buf + 2, safe_strlen (idata->cmd.buf + 2), buf,
142 "imap_auth_sasl: error base64-decoding server response.\n"));
149 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
150 if (rc == SASL_INTERACT)
151 mutt_sasl_interact (interaction);
153 while (rc == SASL_INTERACT);
158 /* send out response, or line break if none needed */
160 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
163 "imap_auth_sasl: error base64-encoding client response.\n"));
167 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
174 if (irc == IMAP_CMD_RESPOND) {
175 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
176 mutt_socket_write (idata->conn, buf);
179 /* If SASL has errored out, send an abort string to the server */
181 mutt_socket_write (idata->conn, "*\r\n");
183 (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
189 while (irc != IMAP_CMD_OK)
190 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
196 if (imap_code (idata->cmd.buf)) {
197 mutt_sasl_setup_conn (idata->conn, saslconn);
198 return IMAP_AUTH_SUCCESS;
202 mutt_error _("SASL authentication failed.");
204 sasl_dispose (&saslconn);
206 return IMAP_AUTH_FAILURE;