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"));
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) {
158 debug_print (1, ("error base64-encoding client response.\n"));
162 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
169 if (irc == IMAP_CMD_RESPOND) {
170 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
171 mutt_socket_write (idata->conn, buf);
174 /* If SASL has errored out, send an abort string to the server */
176 mutt_socket_write (idata->conn, "*\r\n");
177 debug_print (1, ("sasl_client_step error %d\n", rc));
183 while (irc != IMAP_CMD_OK)
184 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
190 if (imap_code (idata->cmd.buf)) {
191 mutt_sasl_setup_conn (idata->conn, saslconn);
192 return IMAP_AUTH_SUCCESS;
196 mutt_error _("SASL authentication failed.");
198 sasl_dispose (&saslconn);
200 return IMAP_AUTH_FAILURE;