2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
5 * Copyright (C) 1999-2001 Brendan Cully <brendan@kublai.com>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 /* IMAP login/authentication code */
14 #include <lib-lib/lib-lib.h>
16 #include <sasl/sasl.h>
17 #include <sasl/saslutil.h>
20 #include "mutt_sasl.h"
21 #include "imap_private.h"
24 IMAP_AUTH_SUCCESS = 0,
29 /* imap_auth_sasl: Default authenticator if available. */
30 static int imap_auth_sasl(IMAP_DATA * idata, const char *method)
32 sasl_conn_t *saslconn;
33 sasl_interact_t *interaction = NULL;
35 char buf[HUGE_STRING];
38 const char *pc = NULL;
39 unsigned int len, olen;
40 unsigned char client_start;
42 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
43 return IMAP_AUTH_FAILURE;
48 /* If the user hasn't specified a method, use any available */
50 method = idata->capstr;
52 /* hack for SASL ANONYMOUS support:
53 * 1. Fetch username. If it's "" or "anonymous" then
54 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
55 * 3. if sasl_client_start fails, fall through... */
57 if (mutt_account_getuser (&idata->conn->account))
58 return IMAP_AUTH_FAILURE;
60 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
61 (!idata->conn->account.user[0] ||
62 !m_strncmp(idata->conn->account.user, "anonymous", 9)))
63 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
67 if (rc != SASL_OK && rc != SASL_CONTINUE)
69 rc = sasl_client_start (saslconn, method, &interaction,
71 if (rc == SASL_INTERACT)
72 mutt_sasl_interact (interaction);
74 while (rc == SASL_INTERACT);
76 client_start = (olen > 0);
78 if (rc != SASL_OK && rc != SASL_CONTINUE) {
79 /* SASL doesn't support LOGIN, so fall back */
80 return IMAP_AUTH_UNAVAIL;
83 mutt_message (_("Authenticating (%s)..."), mech);
85 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
86 imap_cmd_start (idata, buf);
87 irc = IMAP_CMD_CONTINUE;
89 /* looping protocol */
90 while (rc == SASL_CONTINUE || olen > 0) {
92 irc = imap_cmd_step (idata);
93 while (irc == IMAP_CMD_CONTINUE);
95 if (method && irc == IMAP_CMD_NO) {
96 sasl_dispose (&saslconn);
97 return IMAP_AUTH_UNAVAIL;
100 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
103 if (irc == IMAP_CMD_RESPOND) {
104 if (sasl_decode64(idata->cmd.buf.data + 2, idata->cmd.buf.len - 2, buf,
105 LONG_STRING - 1, &len) != SASL_OK)
111 /* client-start is only available with the SASL-IR extension, but
112 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
113 * fast reauth. Override if the server sent an initial continuation */
114 if (!client_start || buf[0]) {
116 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
117 if (rc == SASL_INTERACT)
118 mutt_sasl_interact (interaction);
120 while (rc == SASL_INTERACT);
125 /* send out response, or line break if none needed */
127 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
132 if (irc == IMAP_CMD_RESPOND) {
133 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
134 mutt_socket_write (idata->conn, buf);
137 /* If SASL has errored out, send an abort string to the server */
139 mutt_socket_write (idata->conn, "*\r\n");
145 while (irc != IMAP_CMD_OK)
146 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
152 if (imap_code(idata->cmd.buf.data)) {
153 mutt_sasl_setup_conn (idata->conn, saslconn);
154 return IMAP_AUTH_SUCCESS;
158 mutt_error _("SASL authentication failed.");
160 sasl_dispose (&saslconn);
162 return IMAP_AUTH_FAILURE;
165 int imap_authenticate (IMAP_DATA * idata)
169 if (!m_strisempty(ImapAuthenticators)) {
173 for (p = ImapAuthenticators;; p = q) {
179 q = strchrnul(p, ':');
180 m_strncpy(buf, sizeof(buf), p, q - p);
182 if ((r = imap_auth_sasl(idata, buf)) != IMAP_AUTH_UNAVAIL) {
187 if ((r = imap_auth_sasl(idata, NULL)) != IMAP_AUTH_UNAVAIL) {
192 mutt_error (_("No authenticators available"));