67452b9a09b3292f702da1d8e415cefc6197a0ef
[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
24 #ifdef USE_SASL2
25 #include <sasl/sasl.h>
26 #include <sasl/saslutil.h>
27 #else
28 #include <sasl.h>
29 #include <saslutil.h>
30 #endif
31
32 /* imap_auth_sasl: Default authenticator if available. */
33 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
34 {
35   sasl_conn_t *saslconn;
36   sasl_interact_t *interaction = NULL;
37   int rc, irc;
38   char buf[HUGE_STRING];
39   const char *mech;
40
41 #ifdef USE_SASL2
42   const char *pc = NULL;
43 #else
44   char *pc = NULL;
45 #endif
46   unsigned int len, olen;
47   unsigned char client_start;
48
49   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
50     dprint (1, (debugfile,
51                 "imap_auth_sasl: 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       dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
101     else
102       dprint (1,
103               (debugfile,
104                "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
105     /* SASL doesn't support LOGIN, so fall back */
106
107     return IMAP_AUTH_UNAVAIL;
108   }
109
110   mutt_message (_("Authenticating (%s)..."), mech);
111
112   snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
113   imap_cmd_start (idata, buf);
114   irc = IMAP_CMD_CONTINUE;
115
116   /* looping protocol */
117   while (rc == SASL_CONTINUE || olen > 0) {
118     do
119       irc = imap_cmd_step (idata);
120     while (irc == IMAP_CMD_CONTINUE);
121
122     if (method && irc == IMAP_CMD_NO) {
123       dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
124       sasl_dispose (&saslconn);
125       return IMAP_AUTH_UNAVAIL;
126     }
127
128     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
129       goto bail;
130
131     if (irc == IMAP_CMD_RESPOND) {
132 #ifdef USE_SASL2
133       if (sasl_decode64
134           (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
135            LONG_STRING - 1,
136 #else
137       if (sasl_decode64 (idata->cmd.buf + 2, mutt_strlen (idata->cmd.buf + 2), buf,
138 #endif
139                          &len) != SASL_OK) {
140         dprint (1,
141                 (debugfile,
142                  "imap_auth_sasl: error base64-decoding server response.\n"));
143         goto bail;
144       }
145     }
146
147     if (!client_start) {
148       do {
149         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
150         if (rc == SASL_INTERACT)
151           mutt_sasl_interact (interaction);
152       }
153       while (rc == SASL_INTERACT);
154     }
155     else
156       client_start = 0;
157
158     /* send out response, or line break if none needed */
159     if (olen) {
160       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
161         dprint (1,
162                 (debugfile,
163                  "imap_auth_sasl: error base64-encoding client response.\n"));
164         goto bail;
165       }
166
167       /* sasl_client_st(art|ep) allocate pc with malloc, expect me to 
168        * free it */
169 #ifndef USE_SASL2
170       FREE (&pc);
171 #endif
172     }
173
174     if (irc == IMAP_CMD_RESPOND) {
175       strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
176       mutt_socket_write (idata->conn, buf);
177     }
178
179     /* If SASL has errored out, send an abort string to the server */
180     if (rc < 0) {
181       mutt_socket_write (idata->conn, "*\r\n");
182       dprint (1,
183               (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
184     }
185
186     olen = 0;
187   }
188
189   while (irc != IMAP_CMD_OK)
190     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
191       break;
192
193   if (rc != SASL_OK)
194     goto bail;
195
196   if (imap_code (idata->cmd.buf)) {
197     mutt_sasl_setup_conn (idata->conn, saslconn);
198     return IMAP_AUTH_SUCCESS;
199   }
200
201 bail:
202   mutt_error _("SASL authentication failed.");
203   mutt_sleep (2);
204   sasl_dispose (&saslconn);
205
206   return IMAP_AUTH_FAILURE;
207 }