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>
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;
43 unsigned int len, olen;
44 unsigned char client_start;
46 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
47 debug_print (1, ("Error allocating SASL connection.\n"));
48 return IMAP_AUTH_FAILURE;
53 /* If the user hasn't specified a method, use any available */
55 method = idata->capstr;
57 /* hack for SASL ANONYMOUS support:
58 * 1. Fetch username. If it's "" or "anonymous" then
59 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
60 * 3. if sasl_client_start fails, fall through... */
62 if (mutt_account_getuser (&idata->conn->account))
63 return IMAP_AUTH_FAILURE;
65 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
66 (!idata->conn->account.user[0] ||
67 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
69 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
74 if (rc != SASL_OK && rc != SASL_CONTINUE)
77 rc = sasl_client_start (saslconn, method, &interaction,
80 if (rc == SASL_INTERACT)
81 mutt_sasl_interact (interaction);
83 while (rc == SASL_INTERACT);
85 client_start = (olen > 0);
87 if (rc != SASL_OK && rc != SASL_CONTINUE) {
89 debug_print (2, ("%s unavailable\n", method));
91 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
92 /* SASL doesn't support LOGIN, so fall back */
94 return IMAP_AUTH_UNAVAIL;
97 mutt_message (_("Authenticating (%s)..."), mech);
99 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
100 imap_cmd_start (idata, buf);
101 irc = IMAP_CMD_CONTINUE;
103 /* looping protocol */
104 while (rc == SASL_CONTINUE || olen > 0) {
106 irc = imap_cmd_step (idata);
107 while (irc == IMAP_CMD_CONTINUE);
109 if (method && irc == IMAP_CMD_NO) {
110 debug_print (2, ("%s failed\n", method));
111 sasl_dispose (&saslconn);
112 return IMAP_AUTH_UNAVAIL;
115 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
118 if (irc == IMAP_CMD_RESPOND) {
121 (idata->cmd.buf + 2, str_len (idata->cmd.buf + 2), buf,
125 debug_print (1, ("error base64-decoding server response.\n"));
130 /* client-start is only available with the SASL-IR extension, but
131 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
132 * fast reauth. Override if the server sent an initial continuation */
133 if (!client_start || buf[0]) {
135 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
136 if (rc == SASL_INTERACT)
137 mutt_sasl_interact (interaction);
139 while (rc == SASL_INTERACT);
144 /* send out response, or line break if none needed */
146 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
147 debug_print (1, ("error base64-encoding client response.\n"));
151 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
158 if (irc == IMAP_CMD_RESPOND) {
159 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
160 mutt_socket_write (idata->conn, buf);
163 /* If SASL has errored out, send an abort string to the server */
165 mutt_socket_write (idata->conn, "*\r\n");
166 debug_print (1, ("sasl_client_step error %d\n", rc));
172 while (irc != IMAP_CMD_OK)
173 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
179 if (imap_code (idata->cmd.buf)) {
180 mutt_sasl_setup_conn (idata->conn, saslconn);
181 return IMAP_AUTH_SUCCESS;
185 mutt_error _("SASL authentication failed.");
187 sasl_dispose (&saslconn);
189 return IMAP_AUTH_FAILURE;