drop the old string API fully.
[apps/madmutt.git] / imap / auth_sasl.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-3 Brendan Cully <brendan@kublai.com>
4  *
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.
8  */
9
10 /* SASL login/authentication code */
11
12 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include <lib-lib/mem.h>
17 #include <lib-lib/ascii.h>
18
19 #include "mutt.h"
20 #include "mutt_sasl.h"
21 #include "imap_private.h"
22 #include "auth.h"
23
24 #include <lib-lib/macros.h>
25 #include "lib/debug.h"
26
27 #include <sasl/sasl.h>
28 #include <sasl/saslutil.h>
29
30 /* imap_auth_sasl: Default authenticator if available. */
31 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
32 {
33   sasl_conn_t *saslconn;
34   sasl_interact_t *interaction = NULL;
35   int rc, irc;
36   char buf[HUGE_STRING];
37   const char *mech;
38
39   const char *pc = NULL;
40   unsigned int len, olen;
41   unsigned char client_start;
42
43   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
44     debug_print (1, ("Error allocating SASL connection.\n"));
45     return IMAP_AUTH_FAILURE;
46   }
47
48   rc = SASL_FAIL;
49
50   /* If the user hasn't specified a method, use any available */
51   if (!method) {
52     method = idata->capstr;
53
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... */
58
59     if (mutt_account_getuser (&idata->conn->account))
60       return IMAP_AUTH_FAILURE;
61
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,
66                               &mech);
67   }
68
69   if (rc != SASL_OK && rc != SASL_CONTINUE)
70     do {
71       rc = sasl_client_start (saslconn, method, &interaction,
72                               &pc, &olen, &mech);
73       if (rc == SASL_INTERACT)
74         mutt_sasl_interact (interaction);
75     }
76     while (rc == SASL_INTERACT);
77
78   client_start = (olen > 0);
79
80   if (rc != SASL_OK && rc != SASL_CONTINUE) {
81     if (method)
82       debug_print (2, ("%s unavailable\n", method));
83     else
84       debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
85     /* SASL doesn't support LOGIN, so fall back */
86
87     return IMAP_AUTH_UNAVAIL;
88   }
89
90   mutt_message (_("Authenticating (%s)..."), mech);
91
92   snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
93   imap_cmd_start (idata, buf);
94   irc = IMAP_CMD_CONTINUE;
95
96   /* looping protocol */
97   while (rc == SASL_CONTINUE || olen > 0) {
98     do
99       irc = imap_cmd_step (idata);
100     while (irc == IMAP_CMD_CONTINUE);
101
102     if (method && irc == IMAP_CMD_NO) {
103       debug_print (2, ("%s failed\n", method));
104       sasl_dispose (&saslconn);
105       return IMAP_AUTH_UNAVAIL;
106     }
107
108     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
109       goto bail;
110
111     if (irc == IMAP_CMD_RESPOND) {
112       if (sasl_decode64
113           (idata->cmd.buf + 2, m_strlen(idata->cmd.buf + 2), buf,
114            LONG_STRING - 1,
115                          &len) != SASL_OK) {
116         debug_print (1, ("error base64-decoding server response.\n"));
117         goto bail;
118       }
119     }
120
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]) {
125       do {
126         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
127         if (rc == SASL_INTERACT)
128           mutt_sasl_interact (interaction);
129       }
130       while (rc == SASL_INTERACT);
131     }
132     else
133       client_start = 0;
134
135     /* send out response, or line break if none needed */
136     if (olen) {
137       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
138         debug_print (1, ("error base64-encoding client response.\n"));
139         goto bail;
140       }
141     }
142
143     if (irc == IMAP_CMD_RESPOND) {
144       m_strcpy(buf + olen, sizeof(buf) - olen, "\r\n");
145       mutt_socket_write (idata->conn, buf);
146     }
147
148     /* If SASL has errored out, send an abort string to the server */
149     if (rc < 0) {
150       mutt_socket_write (idata->conn, "*\r\n");
151       debug_print (1, ("sasl_client_step error %d\n", rc));
152     }
153
154     olen = 0;
155   }
156
157   while (irc != IMAP_CMD_OK)
158     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
159       break;
160
161   if (rc != SASL_OK)
162     goto bail;
163
164   if (imap_code (idata->cmd.buf)) {
165     mutt_sasl_setup_conn (idata->conn, saslconn);
166     return IMAP_AUTH_SUCCESS;
167   }
168
169 bail:
170   mutt_error _("SASL authentication failed.");
171   mutt_sleep (2);
172   sasl_dispose (&saslconn);
173
174   return IMAP_AUTH_FAILURE;
175 }