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