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