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