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