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