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 */
18 #include "mutt_sasl.h"
19 #include "imap_private.h"
24 #include "lib/debug.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];
44 const char *pc = NULL;
48 unsigned int len, olen;
49 unsigned char client_start;
51 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
52 debug_print (1, ("Error allocating SASL connection.\n"));
53 return IMAP_AUTH_FAILURE;
58 /* If the user hasn't specified a method, use any available */
60 method = idata->capstr;
62 /* hack for SASL ANONYMOUS support:
63 * 1. Fetch username. If it's "" or "anonymous" then
64 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
65 * 3. if sasl_client_start fails, fall through... */
67 if (mutt_account_getuser (&idata->conn->account))
68 return IMAP_AUTH_FAILURE;
70 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
71 (!idata->conn->account.user[0] ||
72 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
74 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
78 sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
83 if (rc != SASL_OK && rc != SASL_CONTINUE)
86 rc = sasl_client_start (saslconn, method, &interaction,
89 rc = sasl_client_start (saslconn, method, NULL, &interaction,
92 if (rc == SASL_INTERACT)
93 mutt_sasl_interact (interaction);
95 while (rc == SASL_INTERACT);
97 client_start = (olen > 0);
99 if (rc != SASL_OK && rc != SASL_CONTINUE) {
101 debug_print (2, ("%s unavailable\n", method));
103 debug_print (1, ("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 debug_print (2, ("%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, str_len (idata->cmd.buf + 2), buf,
136 if (sasl_decode64 (idata->cmd.buf + 2, str_len (idata->cmd.buf + 2), buf,
139 debug_print (1, ("error base64-decoding server response.\n"));
144 /* client-start is only available with the SASL-IR extension, but
145 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
146 * fast reauth. Override if the server sent an initial continuation */
147 if (!client_start || buf[0]) {
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) {
161 debug_print (1, ("error base64-encoding client response.\n"));
165 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
172 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 */
179 mutt_socket_write (idata->conn, "*\r\n");
180 debug_print (1, ("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;