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/lib-lib.h>
19 #include "mutt_sasl.h"
20 #include "imap_private.h"
23 #include <sasl/sasl.h>
24 #include <sasl/saslutil.h>
26 /* imap_auth_sasl: Default authenticator if available. */
27 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
29 sasl_conn_t *saslconn;
30 sasl_interact_t *interaction = NULL;
32 char buf[HUGE_STRING];
35 const char *pc = NULL;
36 unsigned int len, olen;
37 unsigned char client_start;
39 if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
40 return IMAP_AUTH_FAILURE;
45 /* If the user hasn't specified a method, use any available */
47 method = idata->capstr;
49 /* hack for SASL ANONYMOUS support:
50 * 1. Fetch username. If it's "" or "anonymous" then
51 * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
52 * 3. if sasl_client_start fails, fall through... */
54 if (mutt_account_getuser (&idata->conn->account))
55 return IMAP_AUTH_FAILURE;
57 if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
58 (!idata->conn->account.user[0] ||
59 !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
60 rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
64 if (rc != SASL_OK && rc != SASL_CONTINUE)
66 rc = sasl_client_start (saslconn, method, &interaction,
68 if (rc == SASL_INTERACT)
69 mutt_sasl_interact (interaction);
71 while (rc == SASL_INTERACT);
73 client_start = (olen > 0);
75 if (rc != SASL_OK && rc != SASL_CONTINUE) {
76 /* SASL doesn't support LOGIN, so fall back */
77 return IMAP_AUTH_UNAVAIL;
80 mutt_message (_("Authenticating (%s)..."), mech);
82 snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
83 imap_cmd_start (idata, buf);
84 irc = IMAP_CMD_CONTINUE;
86 /* looping protocol */
87 while (rc == SASL_CONTINUE || olen > 0) {
89 irc = imap_cmd_step (idata);
90 while (irc == IMAP_CMD_CONTINUE);
92 if (method && irc == IMAP_CMD_NO) {
93 sasl_dispose (&saslconn);
94 return IMAP_AUTH_UNAVAIL;
97 if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
100 if (irc == IMAP_CMD_RESPOND) {
101 if (sasl_decode64(idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf,
102 LONG_STRING - 1, &len) != SASL_OK) {
107 /* client-start is only available with the SASL-IR extension, but
108 * SASL 2.1 seems to want to use it regardless, at least for DIGEST
109 * fast reauth. Override if the server sent an initial continuation */
110 if (!client_start || buf[0]) {
112 rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
113 if (rc == SASL_INTERACT)
114 mutt_sasl_interact (interaction);
116 while (rc == SASL_INTERACT);
121 /* send out response, or line break if none needed */
123 if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
128 if (irc == IMAP_CMD_RESPOND) {
129 m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
130 mutt_socket_write (idata->conn, buf);
133 /* If SASL has errored out, send an abort string to the server */
135 mutt_socket_write (idata->conn, "*\r\n");
141 while (irc != IMAP_CMD_OK)
142 if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
148 if (imap_code (idata->cmd.buf)) {
149 mutt_sasl_setup_conn (idata->conn, saslconn);
150 return IMAP_AUTH_SUCCESS;
154 mutt_error _("SASL authentication failed.");
156 sasl_dispose (&saslconn);
158 return IMAP_AUTH_FAILURE;