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"
23 #include "lib/debug.h"
26 #include <sasl/sasl.h>
27 #include <sasl/saslutil.h>
33 /* imap_auth_sasl: Default authenticator if available. */
34 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
36 sasl_conn_t *saslconn;
37 sasl_interact_t *interaction = NULL;
39 char buf[HUGE_STRING];
43 const char *pc = NULL;
47 unsigned int len, olen;
48 unsigned char client_start;
50 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
51 debug_print (1, ("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 debug_print (2, ("%s unavailable\n", method));
102 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
103 /* SASL doesn't support LOGIN, so fall back */
105 return IMAP_AUTH_UNAVAIL;
108 mutt_message (_("Authenticating (%s)..."), mech);
110 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
111 imap_cmd_start (idata, buf);
112 irc = IMAP_CMD_CONTINUE;
114 /* looping protocol */
115 while (rc == SASL_CONTINUE || olen > 0) {
117 irc = imap_cmd_step (idata);
118 while (irc == IMAP_CMD_CONTINUE);
120 if (method && irc == IMAP_CMD_NO) {
121 debug_print (2, ("%s failed\n", method));
122 sasl_dispose (&saslconn);
123 return IMAP_AUTH_UNAVAIL;
126 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
129 if (irc == IMAP_CMD_RESPOND) {
132 (idata->cmd.buf + 2, safe_strlen (idata->cmd.buf + 2), buf,
135 if (sasl_decode64 (idata->cmd.buf + 2, safe_strlen (idata->cmd.buf + 2), buf,
138 debug_print (1, ("error base64-decoding server response.\n"));
145 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
146 if (rc == SASL_INTERACT)
147 mutt_sasl_interact (interaction);
149 while (rc == SASL_INTERACT);
154 /* send out response, or line break if none needed */
156 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
157 debug_print (1, ("error base64-encoding client response.\n"));
161 /* sasl_client_st(art|ep) allocate pc with malloc, expect me to
168 if (irc == IMAP_CMD_RESPOND) {
169 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
170 mutt_socket_write (idata->conn, buf);
173 /* If SASL has errored out, send an abort string to the server */
175 mutt_socket_write (idata->conn, "*\r\n");
176 debug_print (1, ("sasl_client_step error %d\n", rc));
182 while (irc != IMAP_CMD_OK)
183 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
189 if (imap_code (idata->cmd.buf)) {
190 mutt_sasl_setup_conn (idata->conn, saslconn);
191 return IMAP_AUTH_SUCCESS;
195 mutt_error _("SASL authentication failed.");
197 sasl_dispose (&saslconn);
199 return IMAP_AUTH_FAILURE;