Rocco Rutte:
[apps/madmutt.git] / pop / pop_auth.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-2001 Vsevolod Volkov <vvv@mutt.org.ua>
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 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "mx.h"
16 #include "md5.h"
17 #include "pop.h"
18
19 #include "lib/mem.h"
20 #include "lib/intl.h"
21 #include "lib/debug.h"
22
23 #include <string.h>
24 #include <unistd.h>
25
26 #ifdef USE_SASL
27 #ifdef USE_SASL2
28 #include <sasl/sasl.h>
29 #include <sasl/saslutil.h>
30 #else
31 #include <sasl.h>
32 #include <saslutil.h>
33 #endif
34
35 #include "mutt_sasl.h"
36 #endif
37
38 #ifdef USE_SASL
39 /* SASL authenticator */
40 static pop_auth_res_t pop_auth_sasl (POP_DATA * pop_data, const char *method)
41 {
42   sasl_conn_t *saslconn;
43   sasl_interact_t *interaction = NULL;
44   int rc;
45   char buf[LONG_STRING];
46   char inbuf[LONG_STRING];
47   const char *mech;
48
49 #ifdef USE_SASL2
50   const char *pc = NULL;
51 #else
52   char *pc = NULL;
53 #endif
54   unsigned int len, olen;
55   unsigned char client_start;
56
57   if (mutt_sasl_client_new (pop_data->conn, &saslconn) < 0) {
58     debug_print (1, ("Error allocating SASL connection.\n"));
59     return POP_A_FAILURE;
60   }
61
62   if (!method)
63     method = pop_data->auth_list;
64
65   FOREVER {
66 #ifdef USE_SASL2
67     rc =
68       sasl_client_start (saslconn, method, &interaction, &pc, &olen, &mech);
69 #else
70     rc = sasl_client_start (saslconn, method, NULL,
71                             &interaction, &pc, &olen, &mech);
72 #endif
73     if (rc != SASL_INTERACT)
74       break;
75     mutt_sasl_interact (interaction);
76   }
77
78   if (rc != SASL_OK && rc != SASL_CONTINUE) {
79     debug_print (1, ("Failure starting authentication exchange. No shared mechanisms?\n"));
80
81     /* SASL doesn't support suggested mechanisms, so fall back */
82     return POP_A_UNAVAIL;
83   }
84
85   client_start = (olen > 0);
86
87   mutt_message _("Authenticating (SASL)...");
88
89   snprintf (buf, sizeof (buf), "AUTH %s", mech);
90   olen = strlen (buf);
91
92   /* looping protocol */
93   FOREVER {
94     strfcpy (buf + olen, "\r\n", sizeof (buf) - olen);
95     mutt_socket_write (pop_data->conn, buf);
96     if (mutt_socket_readln (inbuf, sizeof (inbuf), pop_data->conn) < 0) {
97       sasl_dispose (&saslconn);
98       pop_data->status = POP_DISCONNECTED;
99       return POP_A_SOCKET;
100     }
101
102     if (rc != SASL_CONTINUE)
103       break;
104
105 #ifdef USE_SASL2
106     if (!str_ncmp (inbuf, "+ ", 2)
107         && sasl_decode64 (inbuf, strlen (inbuf), buf, LONG_STRING - 1,
108                           &len) != SASL_OK)
109 #else
110     if (!str_ncmp (inbuf, "+ ", 2)
111         && sasl_decode64 (inbuf, strlen (inbuf), buf, &len) != SASL_OK)
112 #endif
113     {
114       debug_print (1, ("error base64-decoding server response.\n"));
115       goto bail;
116     }
117
118     if (!client_start)
119       FOREVER {
120       rc = sasl_client_step (saslconn, buf, len, &interaction, &pc, &olen);
121       if (rc != SASL_INTERACT)
122         break;
123       mutt_sasl_interact (interaction);
124       }
125     else
126       client_start = 0;
127
128     if (rc != SASL_CONTINUE && (olen == 0 || rc != SASL_OK))
129       break;
130
131     /* send out response, or line break if none needed */
132     if (pc) {
133       if (sasl_encode64 (pc, olen, buf, sizeof (buf), &olen) != SASL_OK) {
134         debug_print (1, ("error base64-encoding client response.\n"));
135         goto bail;
136       }
137
138       /* sasl_client_st(art|ep) allocate pc with malloc, expect me to 
139        * free it */
140 #ifndef USE_SASL2
141       FREE (&pc);
142 #endif
143     }
144   }
145
146   if (rc != SASL_OK)
147     goto bail;
148
149   if (!str_ncmp (inbuf, "+OK", 3)) {
150     mutt_sasl_setup_conn (pop_data->conn, saslconn);
151     return POP_A_SUCCESS;
152   }
153
154 bail:
155   sasl_dispose (&saslconn);
156
157   /* terminate SASL sessoin if the last responce is not +OK nor -ERR */
158   if (!str_ncmp (inbuf, "+ ", 2)) {
159     snprintf (buf, sizeof (buf), "*\r\n");
160     if (pop_query (pop_data, buf, sizeof (buf)) == PQ_NOT_CONNECTED)
161       return POP_A_SOCKET;
162   }
163
164   mutt_error _("SASL authentication failed.");
165
166   mutt_sleep (2);
167
168   return POP_A_FAILURE;
169 }
170 #endif
171
172 /* Get the server timestamp for APOP authentication */
173 void pop_apop_timestamp (POP_DATA * pop_data, char *buf)
174 {
175   char *p1, *p2;
176
177   FREE (&pop_data->timestamp);
178
179   if ((p1 = strchr (buf, '<')) && (p2 = strchr (p1, '>'))) {
180     p2[1] = '\0';
181     pop_data->timestamp = str_dup (p1);
182   }
183 }
184
185 /* APOP authenticator */
186 static pop_auth_res_t pop_auth_apop (POP_DATA * pop_data, const char *method)
187 {
188   MD5_CTX mdContext;
189   unsigned char digest[16];
190   char hash[33];
191   char buf[LONG_STRING];
192   int i;
193
194   if (!pop_data->timestamp)
195     return POP_A_UNAVAIL;
196
197   mutt_message _("Authenticating (APOP)...");
198
199   /* Compute the authentication hash to send to the server */
200   MD5Init (&mdContext);
201   MD5Update (&mdContext, (unsigned char *) pop_data->timestamp,
202              strlen (pop_data->timestamp));
203   MD5Update (&mdContext, (unsigned char *) pop_data->conn->account.pass,
204              strlen (pop_data->conn->account.pass));
205   MD5Final (digest, &mdContext);
206
207   for (i = 0; i < sizeof (digest); i++)
208     sprintf (hash + 2 * i, "%02x", digest[i]);
209
210   /* Send APOP command to server */
211   snprintf (buf, sizeof (buf), "APOP %s %s\r\n", pop_data->conn->account.user,
212             hash);
213
214   switch (pop_query (pop_data, buf, sizeof (buf))) {
215   case PQ_OK:
216     return POP_A_SUCCESS;
217   case PQ_NOT_CONNECTED:
218     return POP_A_SOCKET;
219   }
220
221   mutt_error _("APOP authentication failed.");
222
223   mutt_sleep (2);
224
225   return POP_A_FAILURE;
226 }
227
228 /* USER authenticator */
229 static pop_auth_res_t pop_auth_user (POP_DATA * pop_data, const char *method)
230 {
231   char buf[LONG_STRING];
232   pop_query_status ret;
233
234   if (pop_data->cmd_user == CMD_NOT_AVAILABLE)
235     return POP_A_UNAVAIL;
236
237   mutt_message _("Logging in...");
238
239   snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user);
240   ret = pop_query (pop_data, buf, sizeof (buf));
241
242   if (pop_data->cmd_user == CMD_UNKNOWN) {
243     if (ret == PQ_OK) {
244       pop_data->cmd_user = CMD_AVAILABLE;
245
246       debug_print (1, ("set USER capability\n"));
247     }
248
249     if (ret == PQ_ERR) {
250       pop_data->cmd_user = CMD_NOT_AVAILABLE;
251
252       debug_print (1, ("unset USER capability\n"));
253       snprintf (pop_data->err_msg, sizeof (pop_data->err_msg),
254                 _("Command USER is not supported by server."));
255     }
256   }
257
258   if (ret == PQ_OK) {
259     snprintf (buf, sizeof (buf), "PASS %s\r\n", pop_data->conn->account.pass);
260     ret = pop_query_d (pop_data, buf, sizeof (buf),
261 #ifdef DEBUG
262     /* don't print the password unless we're at the ungodly debugging level */
263     DebugLevel < M_SOCK_LOG_FULL ? "PASS *\r\n" :
264 #endif
265     NULL);
266   }
267
268   switch (ret) {
269   case PQ_OK:
270     return POP_A_SUCCESS;
271   case PQ_NOT_CONNECTED:
272     return POP_A_SOCKET;
273   }
274
275   mutt_error ("%s %s", _("Login failed."), pop_data->err_msg);
276   mutt_sleep (2);
277
278   return POP_A_FAILURE;
279 }
280
281 static pop_auth_t pop_authenticators[] = {
282 #ifdef USE_SASL
283   {pop_auth_sasl, NULL},
284 #endif
285   {pop_auth_apop, "apop"},
286   {pop_auth_user, "user"},
287   {NULL}
288 };
289
290 /*
291  * Authentication
292  *  0 - successful,
293  * -1 - conection lost,
294  * -2 - login failed,
295  * -3 - authentication canceled.
296 */
297 pop_query_status pop_authenticate (POP_DATA * pop_data)
298 {
299   ACCOUNT *acct = &pop_data->conn->account;
300   pop_auth_t *authenticator;
301   char *methods;
302   char *comma;
303   char *method;
304   int attempts = 0;
305   int ret = POP_A_UNAVAIL;
306
307   if (mutt_account_getuser (acct) || !acct->user[0] ||
308       mutt_account_getpass (acct) || !acct->pass[0])
309     return PFD_FUNCT_ERROR;
310
311   if (PopAuthenticators && *PopAuthenticators) {
312     /* Try user-specified list of authentication methods */
313     methods = str_dup (PopAuthenticators);
314     method = methods;
315
316     while (method) {
317       comma = strchr (method, ':');
318       if (comma)
319         *comma++ = '\0';
320       debug_print (2, ("Trying method %s\n", method));
321       authenticator = pop_authenticators;
322
323       while (authenticator->authenticate) {
324         if (!authenticator->method ||
325             !ascii_strcasecmp (authenticator->method, method)) {
326           ret = authenticator->authenticate (pop_data, method);
327           if (ret == POP_A_SOCKET)
328             switch (pop_connect (pop_data)) {
329             case PQ_OK:
330               {
331                 ret = authenticator->authenticate (pop_data, method);
332                 break;
333               }
334             case PQ_ERR:
335               ret = POP_A_FAILURE;
336             }
337
338           if (ret != POP_A_UNAVAIL)
339             attempts++;
340           if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
341               (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL))) {
342             comma = NULL;
343             break;
344           }
345         }
346         authenticator++;
347       }
348
349       method = comma;
350     }
351
352     FREE (&methods);
353   }
354   else {
355     /* Fall back to default: any authenticator */
356     debug_print (2, ("Using any available method.\n"));
357     authenticator = pop_authenticators;
358
359     while (authenticator->authenticate) {
360       ret = authenticator->authenticate (pop_data, authenticator->method);
361       if (ret == POP_A_SOCKET)
362         switch (pop_connect (pop_data)) {
363         case PQ_OK:
364           {
365             ret =
366               authenticator->authenticate (pop_data, authenticator->method);
367             break;
368           }
369         case PQ_ERR:
370           ret = POP_A_FAILURE;
371         }
372
373       if (ret != POP_A_UNAVAIL)
374         attempts++;
375       if (ret == POP_A_SUCCESS || ret == POP_A_SOCKET ||
376           (ret == POP_A_FAILURE && !option (OPTPOPAUTHTRYALL)))
377         break;
378
379       authenticator++;
380     }
381   }
382
383   switch (ret) {
384   case POP_A_SUCCESS:
385     return PQ_OK;
386   case POP_A_SOCKET:
387     return PQ_NOT_CONNECTED;
388   case POP_A_UNAVAIL:
389     if (!attempts)
390       mutt_error (_("No authenticators available"));
391   }
392
393   return PQ_ERR;
394 }