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