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