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