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 */
16 #include <lib-lib/mem.h>
17 #include <lib-lib/ascii.h>
20 #include "mutt_sasl.h"
21 #include "imap_private.h"
24 #include <lib-lib/macros.h>
25 #include "lib/debug.h"
27 #include <sasl/sasl.h>
28 #include <sasl/saslutil.h>
30 /* imap_auth_sasl: Default authenticator if available. */
31 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
33 sasl_conn_t *saslconn;
34 sasl_interact_t *interaction = NULL;
36 char buf[HUGE_STRING];
39 const char *pc = NULL;
40 unsigned int len, olen;
41 unsigned char client_start;
43 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
44 debug_print (1, ("Error allocating SASL connection.\n"));
45 return IMAP_AUTH_FAILURE;
50 /* If the user hasn't specified a method, use any available */
52 method = idata->capstr;
54 /* hack for SASL ANONYMOUS support:
55 * 1. Fetch username. If it's "" or "anonymous" then
56 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
57 * 3. if sasl_client_start fails, fall through... */
59 if (mutt_account_getuser (&idata->conn->account))
60 return IMAP_AUTH_FAILURE;
62 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
63 (!idata->conn->account.user[0] ||
64 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
65 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
69 if (rc != SASL_OK && rc != SASL_CONTINUE)
71 rc = sasl_client_start (saslconn, method, &interaction,
73 if (rc == SASL_INTERACT)
74 mutt_sasl_interact (interaction);
76 while (rc == SASL_INTERACT);
78 client_start = (olen > 0);
80 if (rc != SASL_OK && rc != SASL_CONTINUE) {
82 debug_print (2, ("%s unavailable\n", method));
84 debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
85 /* SASL doesn't support LOGIN, so fall back */
87 return IMAP_AUTH_UNAVAIL;
90 mutt_message (_("Authenticating (%s)..."), mech);
92 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
93 imap_cmd_start (idata, buf);
94 irc = IMAP_CMD_CONTINUE;
96 /* looping protocol */
97 while (rc == SASL_CONTINUE || olen > 0) {
99 irc = imap_cmd_step (idata);
100 while (irc == IMAP_CMD_CONTINUE);
102 if (method && irc == IMAP_CMD_NO) {
103 debug_print (2, ("%s failed\n", method));
104 sasl_dispose (&saslconn);
105 return IMAP_AUTH_UNAVAIL;
108 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
111 if (irc == IMAP_CMD_RESPOND) {
113 (idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf,
116 debug_print (1, ("error base64-decoding server response.\n"));
121 /* client-start is only available with the SASL-IR extension, but
122 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
123 * fast reauth. Override if the server sent an initial continuation */
124 if (!client_start || buf[0]) {
126 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
127 if (rc == SASL_INTERACT)
128 mutt_sasl_interact (interaction);
130 while (rc == SASL_INTERACT);
135 /* send out response, or line break if none needed */
137 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
138 debug_print (1, ("error base64-encoding client response.\n"));
143 if (irc == IMAP_CMD_RESPOND) {
144 strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
145 mutt_socket_write (idata->conn, buf);
148 /* If SASL has errored out, send an abort string to the server */
150 mutt_socket_write (idata->conn, "*\r\n");
151 debug_print (1, ("sasl_client_step error %d\n", rc));
157 while (irc != IMAP_CMD_OK)
158 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
164 if (imap_code (idata->cmd.buf)) {
165 mutt_sasl_setup_conn (idata->conn, saslconn);
166 return IMAP_AUTH_SUCCESS;
170 mutt_error _("SASL authentication failed.");
172 sasl_dispose (&saslconn);
174 return IMAP_AUTH_FAILURE;