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,
30 /* do authentication, using named method or any available if method is NULL */
31 imap_auth_res_t (*authenticate) (IMAP_DATA * idata, const char *method);
32 /* name of authentication method supported, NULL means variable. If this
33 * is not null, authenticate may ignore the second parameter. */
37 /* imap_auth_sasl: Default authenticator if available. */
38 static imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
40 sasl_conn_t *saslconn;
41 sasl_interact_t *interaction = NULL;
43 char buf[HUGE_STRING];
46 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 return IMAP_AUTH_FAILURE;
56 /* If the user hasn't specified a method, use any available */
58 method = idata->capstr;
60 /* hack for SASL ANONYMOUS support:
61 * 1. Fetch username. If it's "" or "anonymous" then
62 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
63 * 3. if sasl_client_start fails, fall through... */
65 if (mutt_account_getuser (&idata->conn->account))
66 return IMAP_AUTH_FAILURE;
68 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
69 (!idata->conn->account.user[0] ||
70 !m_strncmp(idata->conn->account.user, "anonymous", 9)))
71 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
75 if (rc != SASL_OK && rc != SASL_CONTINUE)
77 rc = sasl_client_start (saslconn, method, &interaction,
79 if (rc == SASL_INTERACT)
80 mutt_sasl_interact (interaction);
82 while (rc == SASL_INTERACT);
84 client_start = (olen > 0);
86 if (rc != SASL_OK && rc != SASL_CONTINUE) {
87 /* SASL doesn't support LOGIN, so fall back */
88 return IMAP_AUTH_UNAVAIL;
91 mutt_message (_("Authenticating (%s)..."), mech);
93 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
94 imap_cmd_start (idata, buf);
95 irc = IMAP_CMD_CONTINUE;
97 /* looping protocol */
98 while (rc == SASL_CONTINUE || olen > 0) {
100 irc = imap_cmd_step (idata);
101 while (irc == IMAP_CMD_CONTINUE);
103 if (method && irc == IMAP_CMD_NO) {
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) {
112 if (sasl_decode64(idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf,
113 LONG_STRING - 1, &len) != SASL_OK) {
118 /* client-start is only available with the SASL-IR extension, but
119 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
120 * fast reauth. Override if the server sent an initial continuation */
121 if (!client_start || buf[0]) {
123 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
124 if (rc == SASL_INTERACT)
125 mutt_sasl_interact (interaction);
127 while (rc == SASL_INTERACT);
132 /* send out response, or line break if none needed */
134 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
139 if (irc == IMAP_CMD_RESPOND) {
140 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
141 mutt_socket_write (idata->conn, buf);
144 /* If SASL has errored out, send an abort string to the server */
146 mutt_socket_write (idata->conn, "*\r\n");
152 while (irc != IMAP_CMD_OK)
153 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
159 if (imap_code (idata->cmd.buf)) {
160 mutt_sasl_setup_conn (idata->conn, saslconn);
161 return IMAP_AUTH_SUCCESS;
165 mutt_error _("SASL authentication failed.");
167 sasl_dispose (&saslconn);
169 return IMAP_AUTH_FAILURE;
171 /* imap_auth_login: Plain LOGIN support */
172 static imap_auth_res_t imap_auth_login(IMAP_DATA *idata, const char *method)
174 char q_user[STRING], q_pass[STRING];
178 if (mutt_bit_isset (idata->capabilities, LOGINDISABLED)) {
179 mutt_message _("LOGIN disabled on this server.");
181 return IMAP_AUTH_UNAVAIL;
184 if (mutt_account_getlogin (&idata->conn->account))
185 return IMAP_AUTH_FAILURE;
186 if (mutt_account_getpass (&idata->conn->account))
187 return IMAP_AUTH_FAILURE;
189 mutt_message _("Logging in...");
191 imap_quote_string(q_user, sizeof(q_user), idata->conn->account.login);
192 imap_quote_string(q_pass, sizeof(q_pass), idata->conn->account.pass);
194 snprintf(buf, sizeof(buf), "LOGIN %s %s", q_user, q_pass);
195 rc = imap_exec(idata, buf, IMAP_CMD_FAIL_OK | IMAP_CMD_PASS);
198 return IMAP_AUTH_SUCCESS;
200 mutt_error _("Login failed.");
203 return IMAP_AUTH_FAILURE;
205 static imap_auth_t imap_authenticators[] = {
206 {imap_auth_sasl, NULL},
207 {imap_auth_login, "login"},
211 /* imap_authenticate: Attempt to authenticate using either user-specified
212 * authentication method if specified, or any. */
213 int imap_authenticate (IMAP_DATA * idata)
215 imap_auth_t *authenticator;
221 if (ImapAuthenticators && *ImapAuthenticators) {
222 /* Try user-specified list of authentication methods */
223 methods = m_strdup(ImapAuthenticators);
225 for (method = methods; method; method = delim) {
226 delim = strchr (method, ':');
232 authenticator = imap_authenticators;
234 while (authenticator->authenticate) {
235 if (!authenticator->method ||
236 !ascii_strcasecmp (authenticator->method, method))
237 if ((r = authenticator->authenticate (idata, method)) !=
250 /* Fall back to default: any authenticator */
251 authenticator = imap_authenticators;
253 while (authenticator->authenticate) {
255 authenticator->authenticate (idata, NULL)) != IMAP_AUTH_UNAVAIL)
261 if (r == IMAP_AUTH_UNAVAIL) {
262 mutt_error (_("No authenticators available"));