From: Arnaud Lacombe <al@sigfpe.info>
[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 "mutt.h"
17 #include "ascii.h"
18 #include "mutt_sasl.h"
19 #include "imap_private.h"
20 #include "auth.h"
21
22 #include "lib/mem.h"
23 #include "lib/intl.h"
24 #include "lib/debug.h"
25
26 #include <sasl/sasl.h>
27 #include <sasl/saslutil.h>
28
29 /* imap_auth_sasl: Default authenticator if available. */
30 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
31 {
32   sasl_conn_t *saslconn;
33   sasl_interact_t *interaction = NULL;
34   int rc, irc;
35   char buf[HUGE_STRING];
36   const char *mech;
37
38   const char *pc = NULL;
39   unsigned int len, olen;
40   unsigned char client_start;
41
42   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
43     debug_print (1, ("Error allocating SASL connection.\n"));
44     return IMAP_AUTH_FAILURE;
45   }
46
47   rc = SASL_FAIL;
48
49   /* If the user hasn't specified a method, use any available */
50   if (!method) {
51     method = idata->capstr;
52
53     /* hack for SASL ANONYMOUS support:
54      * 1. Fetch username. If it's "" or "anonymous" then
55      * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
56      * 3. if sasl_client_start fails, fall through... */
57
58     if (mutt_account_getuser (&idata->conn->account))
59       return IMAP_AUTH_FAILURE;
60
61     if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
62         (!idata->conn->account.user[0] ||
63          !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
64       rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
65                               &mech);
66   }
67
68   if (rc != SASL_OK && rc != SASL_CONTINUE)
69     do {
70       rc = sasl_client_start (saslconn, method, &interaction,
71                               &pc, &olen, &mech);
72       if (rc == SASL_INTERACT)
73         mutt_sasl_interact (interaction);
74     }
75     while (rc == SASL_INTERACT);
76
77   client_start = (olen > 0);
78
79   if (rc != SASL_OK && rc != SASL_CONTINUE) {
80     if (method)
81       debug_print (2, ("%s unavailable\n", method));
82     else
83       debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
84     /* SASL doesn't support LOGIN, so fall back */
85
86     return IMAP_AUTH_UNAVAIL;
87   }
88
89   mutt_message (_("Authenticating (%s)..."), mech);
90
91   snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
92   imap_cmd_start (idata, buf);
93   irc = IMAP_CMD_CONTINUE;
94
95   /* looping protocol */
96   while (rc == SASL_CONTINUE || olen > 0) {
97     do
98       irc = imap_cmd_step (idata);
99     while (irc == IMAP_CMD_CONTINUE);
100
101     if (method && irc == IMAP_CMD_NO) {
102       debug_print (2, ("%s failed\n", method));
103       sasl_dispose (&saslconn);
104       return IMAP_AUTH_UNAVAIL;
105     }
106
107     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
108       goto bail;
109
110     if (irc == IMAP_CMD_RESPOND) {
111       if (sasl_decode64
112           (idata->cmd.buf + 2, str_len (idata->cmd.buf + 2), buf,
113            LONG_STRING - 1,
114                          &len) != SASL_OK) {
115         debug_print (1, ("error base64-decoding server response.\n"));
116         goto bail;
117       }
118     }
119
120     /* client-start is only available with the SASL-IR extension, but
121      * SASL 2.1 seems to want to use it regardless, at least for DIGEST
122      * fast reauth. Override if the server sent an initial continuation */
123     if (!client_start || buf[0]) {
124       do {
125         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
126         if (rc == SASL_INTERACT)
127           mutt_sasl_interact (interaction);
128       }
129       while (rc == SASL_INTERACT);
130     }
131     else
132       client_start = 0;
133
134     /* send out response, or line break if none needed */
135     if (olen) {
136       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
137         debug_print (1, ("error base64-encoding client response.\n"));
138         goto bail;
139       }
140     }
141
142     if (irc == IMAP_CMD_RESPOND) {
143       strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
144       mutt_socket_write (idata->conn, buf);
145     }
146
147     /* If SASL has errored out, send an abort string to the server */
148     if (rc < 0) {
149       mutt_socket_write (idata->conn, "*\r\n");
150       debug_print (1, ("sasl_client_step error %d\n", rc));
151     }
152
153     olen = 0;
154   }
155
156   while (irc != IMAP_CMD_OK)
157     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
158       break;
159
160   if (rc != SASL_OK)
161     goto bail;
162
163   if (imap_code (idata->cmd.buf)) {
164     mutt_sasl_setup_conn (idata->conn, saslconn);
165     return IMAP_AUTH_SUCCESS;
166   }
167
168 bail:
169   mutt_error _("SASL authentication failed.");
170   mutt_sleep (2);
171   sasl_dispose (&saslconn);
172
173   return IMAP_AUTH_FAILURE;
174 }