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 #ifdef USE_SASL2
47   const char *pc = NULL;
48 #else
49   char* pc = NULL;
50 #endif
51   unsigned int len, olen;
52   unsigned char client_start;
53
54   if (mutt_sasl_client_new (idata->conn, &saslconn) < 0)
55   {
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   {
66     method = idata->capstr;
67
68     /* hack for SASL ANONYMOUS support:
69      * 1. Fetch username. If it's "" or "anonymous" then
70      * 2. attempt sasl_client_start with only "AUTH=ANONYMOUS" capability
71      * 3. if sasl_client_start fails, fall through... */
72
73     if (mutt_account_getuser (&idata->conn->account))
74       return IMAP_AUTH_FAILURE;
75
76     if (mutt_bit_isset (idata->capabilities, AUTH_ANON) &&
77         (!idata->conn->account.user[0] ||
78          !ascii_strncmp (idata->conn->account.user, "anonymous", 9)))
79 #ifdef USE_SASL2
80       rc = sasl_client_start (saslconn, "AUTH=ANONYMOUS", NULL, &pc, &olen, 
81                               &mech);
82 #else
83       rc = 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     {
91 #ifdef USE_SASL2
92       rc = sasl_client_start (saslconn, method, &interaction,
93         &pc, &olen, &mech);
94 #else
95       rc = sasl_client_start (saslconn, method, NULL, &interaction,
96         &pc, &olen, &mech);
97 #endif
98       if (rc == SASL_INTERACT)
99         mutt_sasl_interact (interaction);
100     }
101     while (rc == SASL_INTERACT);
102
103   client_start = (olen > 0);
104
105   if (rc != SASL_OK && rc != SASL_CONTINUE)
106   {
107     if (method)
108       dprint (2, (debugfile, "imap_auth_sasl: %s unavailable\n", method));
109     else
110       dprint (1, (debugfile, "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   {
125     do
126       irc = imap_cmd_step (idata);
127     while (irc == IMAP_CMD_CONTINUE);
128
129     if (method && irc == IMAP_CMD_NO)
130     {
131       dprint (2, (debugfile, "imap_auth_sasl: %s failed\n", method));
132       sasl_dispose (&saslconn);
133       return IMAP_AUTH_UNAVAIL;
134     }
135
136     if (irc == IMAP_CMD_BAD || irc == IMAP_CMD_NO)
137       goto bail;
138
139     if (irc == IMAP_CMD_RESPOND)
140     {
141 #ifdef USE_SASL2
142       if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf, LONG_STRING-1,
143 #else
144       if (sasl_decode64 (idata->cmd.buf+2, strlen (idata->cmd.buf+2), buf,
145 #endif
146                          &len) != SASL_OK)
147       {
148         dprint (1, (debugfile, "imap_auth_sasl: error base64-decoding server response.\n"));
149         goto bail;
150       }
151     }
152
153     if (!client_start)
154     {
155       do
156       {
157         rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
158         if (rc == SASL_INTERACT)
159           mutt_sasl_interact (interaction);
160       }
161       while (rc == SASL_INTERACT);
162     }
163     else
164       client_start = 0;
165
166     /* send out response, or line break if none needed */
167     if (olen)
168     {
169       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK)
170       {
171         dprint (1, (debugfile, "imap_auth_sasl: error base64-encoding client response.\n"));
172         goto bail;
173       }
174
175       /* sasl_client_st(art|ep) allocate pc with malloc, expect me to 
176        * free it */
177 #ifndef USE_SASL2
178       FREE (&pc);
179 #endif
180     }
181     
182     if (irc == IMAP_CMD_RESPOND)
183     {
184       strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
185       mutt_socket_write (idata->conn, buf);
186     }
187
188     /* If SASL has errored out, send an abort string to the server */
189     if (rc < 0)
190     {
191       mutt_socket_write (idata->conn, "*\r\n");
192       dprint (1, (debugfile, "imap_auth_sasl: sasl_client_step error %d\n",rc));
193     }
194           
195     olen = 0;
196   }
197
198   while (irc != IMAP_CMD_OK)
199     if ((irc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
200       break;
201
202   if (rc != SASL_OK)
203     goto bail;
204
205   if (imap_code (idata->cmd.buf))
206   {
207     mutt_sasl_setup_conn (idata->conn, saslconn);
208     return IMAP_AUTH_SUCCESS;
209   }
210
211  bail:
212   mutt_error _("SASL authentication failed.");
213   mutt_sleep(2);
214   sasl_dispose (&saslconn);
215
216   return IMAP_AUTH_FAILURE;
217 }