5e72d0cd42372a35f3a4480ed2611b4ad971fc96
[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 PQ_ERR:
165       {
166         pop_data->cmd_capa = CMD_NOT_AVAILABLE;
167         break;
168       }
169     case PQ_NOT_CONNECTED:
170       return PQ_NOT_CONNECTED;
171     }
172   }
173
174   /* CAPA not supported, use defaults */
175   if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
176     pop_data->cmd_user = CMD_UNKNOWN;
177     pop_data->cmd_uidl = CMD_UNKNOWN;
178     pop_data->cmd_top = CMD_UNKNOWN;
179
180     strfcpy (buf, "AUTH\r\n", sizeof (buf));
181     if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
182       return PQ_NOT_CONNECTED;
183   }
184
185   /* Check capabilities */
186   if (mode == 2) {
187     char *msg = NULL;
188
189     if (!pop_data->expire)
190       msg = _("Unable to leave messages on server.");
191     if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
192       msg = _("Command TOP is not supported by server.");
193     if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
194       msg = _("Command UIDL is not supported by server.");
195     if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
196       mutt_error (msg);
197       return PQ_ERR;
198     }
199     pop_data->capabilities = 1;
200   }
201
202   return PQ_OK;
203 }
204
205 /*
206  * Open connection
207  *  0 - successful,
208  * -1 - conection lost,
209  * -2 - invalid response.
210 */
211 pop_query_status pop_connect (POP_DATA * pop_data)
212 {
213   char buf[LONG_STRING];
214
215   pop_data->status = POP_NONE;
216   if (mutt_socket_open (pop_data->conn) < 0 ||
217       mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
218     mutt_error (_("Error connecting to server: %s"),
219                 pop_data->conn->account.host);
220     return PQ_NOT_CONNECTED;
221   }
222
223   pop_data->status = POP_CONNECTED;
224
225   if (str_ncmp (buf, "+OK", 3)) {
226     *pop_data->err_msg = '\0';
227     pop_error (pop_data, buf);
228     mutt_error ("%s", pop_data->err_msg);
229     return PQ_ERR;
230   }
231
232   pop_apop_timestamp (pop_data, buf);
233
234   return PQ_OK;
235 }
236
237 /*
238  * Open connection and authenticate
239  *  0 - successful,
240  * -1 - conection lost,
241  * -2 - invalid command or execution error,
242  * -3 - authentication canceled.
243 */
244 pop_query_status pop_open_connection (POP_DATA * pop_data)
245 {
246   pop_query_status ret;
247   unsigned int n, size;
248   char buf[LONG_STRING];
249
250   ret = pop_connect (pop_data);
251   if (ret != PQ_OK) {
252     mutt_sleep (2);
253     return ret;
254   }
255
256   ret = pop_capabilities (pop_data, 0);
257   if (ret == PQ_NOT_CONNECTED)
258     goto err_conn;
259   if (ret == PQ_ERR) {
260     mutt_sleep (2);
261     return PQ_ERR;
262   }
263
264 #if (defined(USE_SSL) || defined(USE_GNUTLS))
265   /* Attempt STLS if available and desired. */
266   if (!pop_data->conn->ssf && (pop_data->cmd_stls || option(OPTSSLFORCETLS))) {
267     if (option (OPTSSLFORCETLS))
268       pop_data->use_stls = 2;
269     if (pop_data->use_stls == 0) {
270       ret = query_quadoption (OPT_SSLSTARTTLS,
271                               _("Secure connection with TLS?"));
272       if (ret == -1)
273         return PQ_ERR;
274       pop_data->use_stls = 1;
275       if (ret == M_YES)
276         pop_data->use_stls = 2;
277     }
278     if (pop_data->use_stls == 2) {
279       strfcpy (buf, "STLS\r\n", sizeof (buf));
280       ret = pop_query (pop_data, buf, sizeof (buf));
281       if (ret == PQ_NOT_CONNECTED)
282         goto err_conn;
283       if (ret != PQ_OK) {
284         mutt_error ("%s", pop_data->err_msg);
285         mutt_sleep (2);
286       }
287 #ifdef USE_SSL
288       else if (mutt_ssl_starttls (pop_data->conn))
289 #elif USE_GNUTLS
290       else if (mutt_gnutls_starttls (pop_data->conn))
291 #endif
292       {
293         mutt_error (_("Could not negotiate TLS connection"));
294         mutt_sleep (2);
295         return PQ_ERR;
296       }
297       else {
298         /* recheck capabilities after STLS completes */
299         ret = pop_capabilities (pop_data, 1);
300         if (ret == PQ_NOT_CONNECTED)
301           goto err_conn;
302         if (ret == PQ_ERR) {
303           mutt_sleep (2);
304           return PQ_ERR;
305         }
306       }
307     }
308   }
309
310   if (option(OPTSSLFORCETLS) && !pop_data->conn->ssf) {
311     mutt_error _("Encrypted connection unavailable");
312     mutt_sleep (1);
313     return -2;
314   }
315 #endif
316
317   ret = pop_authenticate (pop_data);
318   if (ret == PQ_NOT_CONNECTED)
319     goto err_conn;
320   if (ret == PFD_FUNCT_ERROR)
321     mutt_clear_error ();
322   if (ret != PQ_OK)
323     return ret;
324
325   /* recheck capabilities after authentication */
326   ret = pop_capabilities (pop_data, 2);
327   if (ret == PQ_NOT_CONNECTED)
328     goto err_conn;
329   if (ret == PQ_ERR) {
330     mutt_sleep (2);
331     return PQ_ERR;
332   }
333
334   /* get total size of mailbox */
335   strfcpy (buf, "STAT\r\n", sizeof (buf));
336   ret = pop_query (pop_data, buf, sizeof (buf));
337   if (ret == PQ_NOT_CONNECTED)
338     goto err_conn;
339   if (ret == PQ_ERR) {
340     mutt_error ("%s", pop_data->err_msg);
341     mutt_sleep (2);
342     return ret;
343   }
344
345   sscanf (buf, "+OK %u %u", &n, &size);
346   pop_data->size = size;
347   return PQ_OK;
348
349 err_conn:
350   pop_data->status = POP_DISCONNECTED;
351   mutt_error _("Server closed connection!");
352
353   mutt_sleep (2);
354   return PQ_NOT_CONNECTED;
355 }
356
357 /* logout from POP server */
358 void pop_logout (CONTEXT * ctx)
359 {
360   pop_query_status ret = 0;
361   char buf[LONG_STRING];
362   POP_DATA *pop_data = (POP_DATA *) ctx->data;
363
364   if (pop_data->status == POP_CONNECTED) {
365     mutt_message _("Closing connection to POP server...");
366
367     if (ctx->readonly) {
368       strfcpy (buf, "RSET\r\n", sizeof (buf));
369       ret = pop_query (pop_data, buf, sizeof (buf));
370     }
371
372     if (ret != PQ_NOT_CONNECTED) {
373       strfcpy (buf, "QUIT\r\n", sizeof (buf));
374       pop_query (pop_data, buf, sizeof (buf));
375     }
376
377     mutt_clear_error ();
378   }
379
380   pop_data->status = POP_DISCONNECTED;
381   return;
382 }
383
384 /*
385  * Send data from buffer and receive answer to the same buffer
386  *  0 - successful,
387  * -1 - conection lost,
388  * -2 - invalid command or execution error.
389 */
390 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
391 {
392   int dbg = M_SOCK_LOG_CMD;
393   char *c;
394
395   if (pop_data->status != POP_CONNECTED)
396     return PQ_NOT_CONNECTED;
397
398 #ifdef DEBUG
399   /* print msg instaed of real command */
400   if (msg) {
401     dbg = M_SOCK_LOG_FULL;
402     debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
403   }
404 #endif
405
406   mutt_socket_write_d (pop_data->conn, buf, dbg);
407
408   c = strpbrk (buf, " \r\n");
409   *c = '\0';
410   snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
411
412   if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
413     pop_data->status = POP_DISCONNECTED;
414     return PQ_NOT_CONNECTED;
415   }
416   if (!str_ncmp (buf, "+OK", 3))
417     return PQ_OK;
418
419   pop_error (pop_data, buf);
420   return PQ_ERR;
421 }
422
423 /*
424  * This function calls  funct(*line, *data)  for each received line,
425  * funct(NULL, *data)  if  rewind(*data)  needs, exits when fail or done.
426  * Returned codes:
427  *  0 - successful,
428  * -1 - conection lost,
429  * -2 - invalid command or execution error,
430  * -3 - error in funct(*line, *data)
431  */
432 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
433                     int (*funct) (char *, void *), void *data)
434 {
435   char buf[LONG_STRING];
436   char *inbuf;
437   char *p;
438   pop_query_status ret;
439   int chunk, line = 0;
440   size_t lenbuf = 0;
441
442   strfcpy (buf, query, sizeof (buf));
443   ret = pop_query (pop_data, buf, sizeof (buf));
444   if (ret != PQ_OK)
445     return ret;
446
447   inbuf = mem_malloc (sizeof (buf));
448
449   FOREVER {
450     chunk =
451       mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
452                             M_SOCK_LOG_HDR);
453     if (chunk < 0) {
454       pop_data->status = POP_DISCONNECTED;
455       ret = PQ_NOT_CONNECTED;
456       break;
457     }
458
459     p = buf;
460     if (!lenbuf && buf[0] == '.') {
461       if (buf[1] != '.')
462         break;
463       p++;
464     }
465
466     strfcpy (inbuf + lenbuf, p, sizeof (buf));
467
468     if (chunk >= sizeof (buf)) {
469       lenbuf += strlen (p);
470     }
471     else {
472       line++;
473       if (msg && ReadInc && (line % ReadInc == 0))
474         mutt_message ("%s %d", msg, line);
475       if (ret == 0 && funct (inbuf, data) < 0)
476         ret = PFD_FUNCT_ERROR;
477       lenbuf = 0;
478     }
479
480     mem_realloc (&inbuf, lenbuf + sizeof (buf));
481   }
482
483   mem_free (&inbuf);
484   return ret;
485 }
486
487 /* find message with this UIDL and set refno */
488 static int check_uidl (char *line, void *data)
489 {
490   int i;
491   unsigned int index;
492   CONTEXT *ctx = (CONTEXT *) data;
493
494   sscanf (line, "%u %s", &index, line);
495   for (i = 0; i < ctx->msgcount; i++) {
496     if (!str_cmp (ctx->hdrs[i]->data, line)) {
497       ctx->hdrs[i]->refno = index;
498       break;
499     }
500   }
501
502   return 0;
503 }
504
505 /* reconnect and verify indexes if connection was lost */
506 pop_query_status pop_reconnect (CONTEXT * ctx)
507 {
508   pop_query_status ret;
509   POP_DATA *pop_data = (POP_DATA *) ctx->data;
510
511   if (pop_data->status == POP_CONNECTED)
512     return PQ_OK;
513   if (pop_data->status == POP_BYE)
514     return PQ_NOT_CONNECTED;
515
516   FOREVER {
517     mutt_socket_close (pop_data->conn);
518
519     ret = pop_open_connection (pop_data);
520     if (ret == PQ_OK) {
521       char *msg = _("Verifying message indexes...");
522       int i;
523
524       for (i = 0; i < ctx->msgcount; i++)
525         ctx->hdrs[i]->refno = -1;
526
527       mutt_message (msg);
528
529       ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
530       if (ret == PQ_ERR) {
531         mutt_error ("%s", pop_data->err_msg);
532         mutt_sleep (2);
533       }
534     }
535     if (ret == PQ_OK)
536       return PQ_OK;
537
538     pop_logout (ctx);
539
540     if (ret == PQ_ERR)
541       return PQ_NOT_CONNECTED;
542
543     if (query_quadoption (OPT_POPRECONNECT,
544                           _("Connection lost. Reconnect to POP server?")) !=
545         M_YES)
546       return PQ_NOT_CONNECTED;
547   }
548 }