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