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