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