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 */
12 #include <lib-lib/lib-lib.h>
15 #include "mutt_sasl.h"
16 #include "imap_private.h"
19 #include <sasl/sasl.h>
20 #include <sasl/saslutil.h>
22 /* imap_auth_sasl: Default authenticator if available. */
23 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
25 sasl_conn_t *saslconn;
26 sasl_interact_t *interaction = NULL;
28 char buf[HUGE_STRING];
31 const char *pc = NULL;
32 unsigned int len, olen;
33 unsigned char client_start;
35 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
36 return IMAP_AUTH_FAILURE;
41 /* If the user hasn't specified a method, use any available */
43 method = idata->capstr;
45 /* hack for SASL ANONYMOUS support:
46 * 1. Fetch username. If it's "" or "anonymous" then
47 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
48 * 3. if sasl_client_start fails, fall through... */
50 if (mutt_account_getuser (&idata->conn->account))
51 return IMAP_AUTH_FAILURE;
53 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
54 (!idata->conn->account.user[0] ||
55 !m_strncmp(idata->conn->account.user, "anonymous", 9)))
56 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
60 if (rc != SASL_OK && rc != SASL_CONTINUE)
62 rc = sasl_client_start (saslconn, method, &interaction,
64 if (rc == SASL_INTERACT)
65 mutt_sasl_interact (interaction);
67 while (rc == SASL_INTERACT);
69 client_start = (olen > 0);
71 if (rc != SASL_OK && rc != SASL_CONTINUE) {
72 /* SASL doesn't support LOGIN, so fall back */
73 return IMAP_AUTH_UNAVAIL;
76 mutt_message (_("Authenticating (%s)..."), mech);
78 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
79 imap_cmd_start (idata, buf);
80 irc = IMAP_CMD_CONTINUE;
82 /* looping protocol */
83 while (rc == SASL_CONTINUE || olen > 0) {
85 irc = imap_cmd_step (idata);
86 while (irc == IMAP_CMD_CONTINUE);
88 if (method && irc == IMAP_CMD_NO) {
89 sasl_dispose (&saslconn);
90 return IMAP_AUTH_UNAVAIL;
93 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
96 if (irc == IMAP_CMD_RESPOND) {
97 if (sasl_decode64(idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf,
98 LONG_STRING - 1, &len) != SASL_OK) {
103 /* client-start is only available with the SASL-IR extension, but
104 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
105 * fast reauth. Override if the server sent an initial continuation */
106 if (!client_start || buf[0]) {
108 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
109 if (rc == SASL_INTERACT)
110 mutt_sasl_interact (interaction);
112 while (rc == SASL_INTERACT);
117 /* send out response, or line break if none needed */
119 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
124 if (irc == IMAP_CMD_RESPOND) {
125 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
126 mutt_socket_write (idata->conn, buf);
129 /* If SASL has errored out, send an abort string to the server */
131 mutt_socket_write (idata->conn, "*\r\n");
137 while (irc != IMAP_CMD_OK)
138 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
144 if (imap_code (idata->cmd.buf)) {
145 mutt_sasl_setup_conn (idata->conn, saslconn);
146 return IMAP_AUTH_SUCCESS;
150 mutt_error _("SASL authentication failed.");
152 sasl_dispose (&saslconn);
154 return IMAP_AUTH_FAILURE;