prepare the ssl module.
[apps/madmutt.git] / pop / pop_lib.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-2003 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 #include <lib-mx/mx.h>
12
13 #include "mutt.h"
14 #include "pop.h"
15
16 /* given an POP mailbox name, return host, port, username and password */
17 int pop_parse_path (const char *path, ACCOUNT * act)
18 {
19   ciss_url_t url;
20   char *c;
21   int ret = -1;
22
23   /* Defaults */
24   act->flags = 0;
25   act->port = POP_PORT;
26   act->type = M_ACCT_TYPE_POP;
27
28   c = m_strdup(path);
29   url_parse_ciss (&url, c);
30
31   if (url.scheme == U_POP || url.scheme == U_POPS) {
32     if (url.scheme == U_POPS) {
33       act->flags |= M_ACCT_SSL;
34       act->port = POP_SSL_PORT;
35     }
36
37     if ((!url.path || !*url.path) && mutt_account_fromurl (act, &url) == 0)
38       ret = 0;
39   }
40
41   p_delete(&c);
42   return ret;
43 }
44
45 /* Copy error message to err_msg buffer */
46 void pop_error (POP_DATA * pop_data, char *msg)
47 {
48   char *t, *c, *c2;
49
50   t = strchr (pop_data->err_msg, '\0');
51   c = msg;
52
53   if (!m_strncmp(msg, "-ERR ", 5)) {
54     c2 = vskipspaces(msg + 5);
55     if (*c2)
56       c = c2;
57   }
58
59   m_strcpy(t, sizeof(pop_data->err_msg) - strlen(pop_data->err_msg), c);
60   m_strrtrim(pop_data->err_msg);
61 }
62
63 /* Parse CAPA output */
64 static int fetch_capa (char *line, void *data)
65 {
66   POP_DATA *pop_data = (POP_DATA *) data;
67   char *c;
68
69   if (!ascii_strncasecmp (line, "SASL", 4)) {
70     p_delete(&pop_data->auth_list);
71     c = vskipspaces(line + 4);
72     pop_data->auth_list = m_strdup(c);
73   }
74
75   else if (!ascii_strncasecmp (line, "STLS", 4))
76     pop_data->cmd_stls = CMD_AVAILABLE;
77
78   else if (!ascii_strncasecmp (line, "USER", 4))
79     pop_data->cmd_user = CMD_AVAILABLE;
80
81   else if (!ascii_strncasecmp (line, "UIDL", 4))
82     pop_data->cmd_uidl = CMD_AVAILABLE;
83
84   else if (!ascii_strncasecmp (line, "TOP", 3))
85     pop_data->cmd_top = CMD_AVAILABLE;
86
87   return 0;
88 }
89
90 /* Fetch list of the authentication mechanisms */
91 static int fetch_auth (char *line, void *data)
92 {
93   POP_DATA *pop_data = (POP_DATA *) data;
94   ssize_t auth_list_len;
95
96   if (!pop_data->auth_list) {
97     auth_list_len = m_strlen(line) + 1;
98     pop_data->auth_list = p_new(char, auth_list_len);
99   } else {
100     auth_list_len = m_strlen(pop_data->auth_list) + m_strlen(line) + 2;
101     p_realloc(&pop_data->auth_list, auth_list_len);
102     m_strcat(pop_data->auth_list, auth_list_len, " ");
103   }
104   m_strcat(pop_data->auth_list, auth_list_len, line);
105
106   return 0;
107 }
108
109 /*
110  * Get capabilities
111  *  0 - successful,
112  * -1 - conection lost,
113  * -2 - execution error.
114 */
115 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
116 {
117   char buf[LONG_STRING];
118
119   /* don't check capabilities on reconnect */
120   if (pop_data->capabilities)
121     return 0;
122
123   /* init capabilities */
124   if (mode == 0) {
125     pop_data->cmd_capa = CMD_NOT_AVAILABLE;
126     pop_data->cmd_stls = CMD_NOT_AVAILABLE;
127     pop_data->cmd_user = CMD_NOT_AVAILABLE;
128     pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
129     pop_data->cmd_top = CMD_NOT_AVAILABLE;
130     pop_data->resp_codes = 0;
131     pop_data->expire = 1;
132     pop_data->login_delay = 0;
133     p_delete(&pop_data->auth_list);
134   }
135
136   /* Execute CAPA command */
137   if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
138     m_strcpy(buf, sizeof(buf), "CAPA\r\n");
139     switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
140     case PQ_OK:
141       {
142         pop_data->cmd_capa = CMD_AVAILABLE;
143         break;
144       }
145     case PFD_FUNCT_ERROR:
146     case PQ_ERR:
147       {
148         pop_data->cmd_capa = CMD_NOT_AVAILABLE;
149         break;
150       }
151     case PQ_NOT_CONNECTED:
152       return PQ_NOT_CONNECTED;
153     }
154   }
155
156   /* CAPA not supported, use defaults */
157   if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
158     pop_data->cmd_user = CMD_UNKNOWN;
159     pop_data->cmd_uidl = CMD_UNKNOWN;
160     pop_data->cmd_top = CMD_UNKNOWN;
161
162     m_strcpy(buf, sizeof(buf), "AUTH\r\n");
163     if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
164       return PQ_NOT_CONNECTED;
165   }
166
167   /* Check capabilities */
168   if (mode == 2) {
169     char *msg = NULL;
170
171     if (!pop_data->expire)
172       msg = _("Unable to leave messages on server.");
173     if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
174       msg = _("Command TOP is not supported by server.");
175     if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
176       msg = _("Command UIDL is not supported by server.");
177     if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
178       mutt_error (msg);
179       return PQ_ERR;
180     }
181     pop_data->capabilities = 1;
182   }
183
184   return PQ_OK;
185 }
186
187 /*
188  * Open connection
189  *  0 - successful,
190  * -1 - conection lost,
191  * -2 - invalid response.
192 */
193 pop_query_status pop_connect (POP_DATA * pop_data)
194 {
195   char buf[LONG_STRING];
196
197   pop_data->status = POP_NONE;
198   if (mutt_socket_open (pop_data->conn) < 0 ||
199       mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
200     mutt_error (_("Error connecting to server: %s"),
201                 pop_data->conn->account.host);
202     return PQ_NOT_CONNECTED;
203   }
204
205   pop_data->status = POP_CONNECTED;
206
207   if (m_strncmp(buf, "+OK", 3)) {
208     *pop_data->err_msg = '\0';
209     pop_error (pop_data, buf);
210     mutt_error ("%s", pop_data->err_msg);
211     return PQ_ERR;
212   }
213
214   pop_apop_timestamp (pop_data, buf);
215
216   return PQ_OK;
217 }
218
219 /*
220  * Open connection and authenticate
221  *  0 - successful,
222  * -1 - conection lost,
223  * -2 - invalid command or execution error,
224  * -3 - authentication canceled.
225 */
226 pop_query_status pop_open_connection (POP_DATA * pop_data)
227 {
228   pop_query_status ret;
229   unsigned int n, size;
230   char buf[LONG_STRING];
231
232   ret = pop_connect (pop_data);
233   if (ret != PQ_OK) {
234     mutt_sleep (2);
235     return ret;
236   }
237
238   ret = pop_capabilities (pop_data, 0);
239   if (ret == PQ_NOT_CONNECTED)
240     goto err_conn;
241   if (ret == PQ_ERR) {
242     mutt_sleep (2);
243     return PQ_ERR;
244   }
245
246   /* Attempt STLS if available and desired. */
247   if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
248     if (option (OPTSSLFORCETLS))
249       pop_data->use_stls = 2;
250     if (pop_data->use_stls == 0) {
251       ret = query_quadoption (OPT_SSLSTARTTLS,
252                               _("Secure connection with TLS?"));
253       if (ret == -1)
254         return PQ_ERR;
255       pop_data->use_stls = 1;
256       if (ret == M_YES)
257         pop_data->use_stls = 2;
258     }
259     if (pop_data->use_stls == 2) {
260       m_strcpy(buf, sizeof(buf), "STLS\r\n");
261       ret = pop_query (pop_data, buf, sizeof (buf));
262       if (ret == PQ_NOT_CONNECTED)
263         goto err_conn;
264       if (ret != PQ_OK) {
265         mutt_error ("%s", pop_data->err_msg);
266         mutt_sleep (2);
267       }
268       else if (mutt_ssl_starttls (pop_data->conn))
269       {
270         mutt_error (_("Could not negotiate TLS connection"));
271         mutt_sleep (2);
272         return PQ_ERR;
273       }
274       else {
275         /* recheck capabilities after STLS completes */
276         ret = pop_capabilities (pop_data, 1);
277         if (ret == PQ_NOT_CONNECTED)
278           goto err_conn;
279         if (ret == PQ_ERR) {
280           mutt_sleep (2);
281           return PQ_ERR;
282         }
283       }
284     }
285   }
286
287   if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
288     mutt_error _("Encrypted connection unavailable");
289     mutt_sleep (1);
290     return -2;
291   }
292
293   ret = pop_authenticate (pop_data);
294   if (ret == PQ_NOT_CONNECTED)
295     goto err_conn;
296   if (ret == PFD_FUNCT_ERROR)
297     mutt_clear_error ();
298   if (ret != PQ_OK)
299     return ret;
300
301   /* recheck capabilities after authentication */
302   ret = pop_capabilities (pop_data, 2);
303   if (ret == PQ_NOT_CONNECTED)
304     goto err_conn;
305   if (ret == PQ_ERR) {
306     mutt_sleep (2);
307     return PQ_ERR;
308   }
309
310   /* get total size of mailbox */
311   m_strcpy(buf, sizeof(buf), "STAT\r\n");
312   ret = pop_query (pop_data, buf, sizeof (buf));
313   if (ret == PQ_NOT_CONNECTED)
314     goto err_conn;
315   if (ret == PQ_ERR) {
316     mutt_error ("%s", pop_data->err_msg);
317     mutt_sleep (2);
318     return ret;
319   }
320
321   sscanf (buf, "+OK %u %u", &n, &size);
322   pop_data->size = size;
323   return PQ_OK;
324
325 err_conn:
326   pop_data->status = POP_DISCONNECTED;
327   mutt_error _("Server closed connection!");
328
329   mutt_sleep (2);
330   return PQ_NOT_CONNECTED;
331 }
332
333 /* logout from POP server */
334 void pop_logout (CONTEXT * ctx)
335 {
336   pop_query_status ret = 0;
337   char buf[LONG_STRING];
338   POP_DATA *pop_data = (POP_DATA *) ctx->data;
339
340   if (pop_data->status == POP_CONNECTED) {
341     mutt_message _("Closing connection to POP server...");
342
343     if (ctx->readonly) {
344       m_strcpy(buf, sizeof(buf), "RSET\r\n");
345       ret = pop_query (pop_data, buf, sizeof (buf));
346     }
347
348     if (ret != PQ_NOT_CONNECTED) {
349       m_strcpy(buf, sizeof(buf), "QUIT\r\n");
350       pop_query (pop_data, buf, sizeof (buf));
351     }
352
353     mutt_clear_error ();
354   }
355
356   pop_data->status = POP_DISCONNECTED;
357   return;
358 }
359
360 /*
361  * Send data from buffer and receive answer to the same buffer
362  *  0 - successful,
363  * -1 - conection lost,
364  * -2 - invalid command or execution error.
365 */
366 pop_query_status pop_query (POP_DATA * pop_data, char *buf, size_t buflen)
367 {
368   char *c;
369
370   if (pop_data->status != POP_CONNECTED)
371     return PQ_NOT_CONNECTED;
372
373   mutt_socket_write(pop_data->conn, buf);
374
375   c = strpbrk (buf, " \r\n");
376   *c = '\0';
377   snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
378
379   if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
380     pop_data->status = POP_DISCONNECTED;
381     return PQ_NOT_CONNECTED;
382   }
383   if (!m_strncmp(buf, "+OK", 3))
384     return PQ_OK;
385
386   pop_error (pop_data, buf);
387   return PQ_ERR;
388 }
389
390 /*
391  * This function calls  funct(*line, *data)  for each received line,
392  * funct(NULL, *data)  if  rewind(*data)  needs, exits when fail or done.
393  * Returned codes:
394  *  0 - successful,
395  * -1 - conection lost,
396  * -2 - invalid command or execution error,
397  * -3 - error in funct(*line, *data)
398  */
399 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
400                     int (*funct) (char *, void *), void *data)
401 {
402   char buf[LONG_STRING];
403   char *inbuf;
404   char *p;
405   pop_query_status ret;
406   int chunk = 0;
407   long pos = 0;
408   size_t lenbuf = 0;
409
410   m_strcpy(buf, sizeof(buf), query);
411   ret = pop_query (pop_data, buf, sizeof (buf));
412   if (ret != PQ_OK)
413     return ret;
414
415   inbuf = p_new(char, sizeof(buf));
416
417   for (;;) {
418     chunk =
419       mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
420     if (chunk < 0) {
421       pop_data->status = POP_DISCONNECTED;
422       ret = PQ_NOT_CONNECTED;
423       break;
424     }
425
426     p = buf;
427     if (!lenbuf && buf[0] == '.') {
428       if (buf[1] != '.')
429         break;
430       p++;
431     }
432
433     m_strcpy(inbuf + lenbuf,sizeof(buf), p);
434     pos += chunk;
435
436     if (chunk >= ssizeof(buf)) {
437       lenbuf += strlen (p);
438     } else {
439       if (bar)
440         mutt_progress_bar (bar, pos);
441       if (ret == 0 && funct (inbuf, data) < 0)
442         ret = PFD_FUNCT_ERROR;
443       lenbuf = 0;
444     }
445
446     p_realloc(&inbuf, lenbuf + sizeof(buf));
447   }
448
449   p_delete(&inbuf);
450   return ret;
451 }
452
453 /* find message with this UIDL and set refno */
454 static int check_uidl (char *line, void *data)
455 {
456   int i, idx;
457   CONTEXT *ctx = (CONTEXT *)data;
458
459   sscanf (line, "%u %s", &idx, line);
460   for (i = 0; i < ctx->msgcount; i++) {
461     if (!m_strcmp(ctx->hdrs[i]->data, line)) {
462       ctx->hdrs[i]->refno = idx;
463       break;
464     }
465   }
466
467   return 0;
468 }
469
470 /* reconnect and verify indexes if connection was lost */
471 pop_query_status pop_reconnect (CONTEXT * ctx)
472 {
473   pop_query_status ret;
474   POP_DATA *pop_data = (POP_DATA *) ctx->data;
475   progress_t bar;
476
477   if (pop_data->status == POP_CONNECTED)
478     return PQ_OK;
479   if (pop_data->status == POP_BYE)
480     return PQ_NOT_CONNECTED;
481
482   for (;;) {
483     mutt_socket_close (pop_data->conn);
484
485     ret = pop_open_connection (pop_data);
486     if (ret == PQ_OK) {
487       int i;
488
489       bar.msg = _("Verifying message indexes...");
490       bar.size = 0;
491       mutt_progress_bar (&bar, 0);
492
493       for (i = 0; i < ctx->msgcount; i++)
494         ctx->hdrs[i]->refno = -1;
495
496       ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
497       if (ret == PQ_ERR) {
498         mutt_error ("%s", pop_data->err_msg);
499         mutt_sleep (2);
500       }
501     }
502     if (ret == PQ_OK)
503       return PQ_OK;
504
505     pop_logout (ctx);
506
507     if (ret == PQ_ERR)
508       return PQ_NOT_CONNECTED;
509
510     if (query_quadoption (OPT_POPRECONNECT,
511                           _("Connection lost. Reconnect to POP server?")) !=
512         M_YES)
513       return PQ_NOT_CONNECTED;
514   }
515 }