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