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