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