Andreas Krennmair:
[apps/madmutt.git] / imap / auth_sasl.c
1 /*
2  * Copyright (C) 2000-3 Brendan Cully <brendan@kublai.com>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /* SASL login/authentication code */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "mutt.h"
26 #include "mutt_sasl.h"
27 #include "imap_private.h"
28 #include "auth.h"
29
30 #ifdef USE_SASL2
31 #include <sasl/sasl.h>
32 #include <sasl/saslutil.h>
33 #else
34 #include <sasl.h>
35 #include <saslutil.h>
36 #endif
37
38 /* imap_auth_sasl: Default authenticator if available. */
39 imap_auth_res_t imap_auth_sasl (IMAP_DATA * idata, const char *method)
40 {
41   sasl_conn_t *saslconn;
42   sasl_interact_t *interaction = NULL;
43   int rc, irc;
44   char buf[HUGE_STRING];
45   const char *mech;
46
47 #ifdef USE_SASL2
48   const char *pc = NULL;
49 #else
50   char *pc = NULL;
51 #endif
52   unsigned int len, olen;
53   unsigned char client_start;
54
55   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0) {
56     dprint (1, (debugfile,
57                 "imap_auth_sasl: Error allocating SASL connection.\n"));
58     return IMAP_AUTH_FAILURE;
59   }
60
61   rc = SASL_FAIL;
62
63   /* If the user hasn't specified a method, use any available */
64   if (!method) {
65     method = idata->capstr;
66
67     /* hack for SASL ANONYMOUS support:
68      * 1. Fetch username. If it's "" or "anonymous" then
69      * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
70      * 3. if sasl_client_start fails, fall through... */
71
72     if (mutt_account_getuser (&idata->conn->account))
73       return IMAP_AUTH_FAILURE;
74
75     if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
76         (!idata->conn->account.user[0] ||
77          !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
78 #ifdef USE_SASL2
79       rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen,
80                               &mech);
81 #else
82       rc =
83         sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, NULL, &pc, &olen,
84                            &mech);
85 #endif
86   }
87
88   if (rc != SASL_OK && rc != SASL_CONTINUE)
89     do {
90 #ifdef USE_SASL2
91       rc = sasl_client_start (saslconn, method, &interaction,
92                               &pc, &olen, &mech);
93 #else
94       rc = sasl_client_start (saslconn, method, NULL, &interaction,
95                               &pc, &olen, &mech);
96 #endif
97       if (rc == SASL_INTERACT)
98         mutt_sasl_interact (interaction);
99     }
100     while (rc == SASL_INTERACT);
101
102   client_start = (olen > 0);
103
104   if (rc != SASL_OK && rc != SASL_CONTINUE) {
105     if (method)
106       dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
107     else
108       dprint (1,
109               (debugfile,
110                "imap_auth_sasl: Failure starting authentication exchange. No shared mechanisms?\n"));
111     /* SASL doesn't support LOGIN, so fall back */
112
113     return IMAP_AUTH_UNAVAIL;
114   }
115
116   mutt_message (_("Authenticating (%s)..."), mech);
117
118   snprintf (buf, sizeof (buf), "AUTHENTICATE %s", mech);
119   imap_cmd_start (idata, buf);
120   irc = IMAP_CMD_CONTINUE;
121
122   /* looping protocol */
123   while (rc == SASL_CONTINUE || olen > 0) {
124     do
125       irc = imap_cmd_step (idata);
126     while (irc == IMAP_CMD_CONTINUE);
127
128     if (method && irc == IMAP_CMD_NO) {
129       dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
130       sasl_dispose (&saslconn);
131       return IMAP_AUTH_UNAVAIL;
132     }
133
134     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
135       goto bail;
136
137     if (irc == IMAP_CMD_RESPOND) {
138 #ifdef USE_SASL2
139       if (sasl_decode64
140           (idata->cmd.buf + 2, strlen (idata->cmd.buf + 2), buf,
141            LONG_STRING - 1,
142 #else
143       if (sasl_decode64 (idata->cmd.buf + 2, strlen (idata->cmd.buf + 2), buf,
144 #endif
145                          &len) != SASL_OK) {
146         dprint (1,
147                 (debugfile,
148                  "imap_auth_sasl: error base64-decoding server response.\n"));
149         goto bail;
150       }
151     }
152
153     if (!client_start) {
154       do {
155         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
156         if (rc == SASL_INTERACT)
157           mutt_sasl_interact (interaction);
158       }
159       while (rc == SASL_INTERACT);
160     }
161     else
162       client_start = 0;
163
164     /* send out response, or line break if none needed */
165     if (olen) {
166       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
167         dprint (1,
168                 (debugfile,
169                  "imap_auth_sasl: error base64-encoding client response.\n"));
170         goto bail;
171       }
172
173       /* sasl_client_st(art|ep) allocate pc with malloc, expect me to 
174        * free it */
175 #ifndef USE_SASL2
176       FREE (&pc);
177 #endif
178     }
179
180     if (irc == IMAP_CMD_RESPOND) {
181       strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
182       mutt_socket_write (idata->conn, buf);
183     }
184
185     /* If SASL has errored out, send an abort string to the server */
186     if (rc < 0) {
187       mutt_socket_write (idata->conn, "*\r\n");
188       dprint (1,
189               (debugfile, "imap_auth_sasl: sasl_client_step error %d\n", rc));
190     }
191
192     olen = 0;
193   }
194
195   while (irc != IMAP_CMD_OK)
196     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
197       break;
198
199   if (rc != SASL_OK)
200     goto bail;
201
202   if (imap_code (idata->cmd.buf)) {
203     mutt_sasl_setup_conn (idata->conn, saslconn);
204     return IMAP_AUTH_SUCCESS;
205   }
206
207 bail:
208   mutt_error _("SASL authentication failed.");
209   mutt_sleep (2);
210   sasl_dispose (&saslconn);
211
212   return IMAP_AUTH_FAILURE;
213 }