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