create mod_ssl.
[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 || mod_ssl.force_tls)) {
248     if (mod_ssl.force_tls)
249       pop_data->use_stls = 2;
250     if (pop_data->use_stls == 0) {
251       pop_data->use_stls = 1;
252       if (mod_ssl.starttls)
253         pop_data->use_stls = 2;
254     }
255     if (pop_data->use_stls == 2) {
256       m_strcpy(buf, sizeof(buf), "STLS\r\n");
257       ret = pop_query (pop_data, buf, sizeof (buf));
258       if (ret == PQ_NOT_CONNECTED)
259         goto err_conn;
260       if (ret != PQ_OK) {
261         mutt_error ("%s", pop_data->err_msg);
262         mutt_sleep (2);
263       }
264       else if (mutt_ssl_starttls (pop_data->conn))
265       {
266         mutt_error (_("Could not negotiate TLS connection"));
267         mutt_sleep (2);
268         return PQ_ERR;
269       }
270       else {
271         /* recheck capabilities after STLS completes */
272         ret = pop_capabilities (pop_data, 1);
273         if (ret == PQ_NOT_CONNECTED)
274           goto err_conn;
275         if (ret == PQ_ERR) {
276           mutt_sleep (2);
277           return PQ_ERR;
278         }
279       }
280     }
281   }
282
283   if (mod_ssl.force_tls && !pop_data->conn->ssf) {
284     mutt_error _("Encrypted connection unavailable");
285     mutt_sleep (1);
286     return -2;
287   }
288
289   ret = pop_authenticate (pop_data);
290   if (ret == PQ_NOT_CONNECTED)
291     goto err_conn;
292   if (ret == PFD_FUNCT_ERROR)
293     mutt_clear_error ();
294   if (ret != PQ_OK)
295     return ret;
296
297   /* recheck capabilities after authentication */
298   ret = pop_capabilities (pop_data, 2);
299   if (ret == PQ_NOT_CONNECTED)
300     goto err_conn;
301   if (ret == PQ_ERR) {
302     mutt_sleep (2);
303     return PQ_ERR;
304   }
305
306   /* get total size of mailbox */
307   m_strcpy(buf, sizeof(buf), "STAT\r\n");
308   ret = pop_query (pop_data, buf, sizeof (buf));
309   if (ret == PQ_NOT_CONNECTED)
310     goto err_conn;
311   if (ret == PQ_ERR) {
312     mutt_error ("%s", pop_data->err_msg);
313     mutt_sleep (2);
314     return ret;
315   }
316
317   sscanf (buf, "+OK %u %u", &n, &size);
318   pop_data->size = size;
319   return PQ_OK;
320
321 err_conn:
322   pop_data->status = POP_DISCONNECTED;
323   mutt_error _("Server closed connection!");
324
325   mutt_sleep (2);
326   return PQ_NOT_CONNECTED;
327 }
328
329 /* logout from POP server */
330 void pop_logout (CONTEXT * ctx)
331 {
332   pop_query_status ret = 0;
333   char buf[LONG_STRING];
334   POP_DATA *pop_data = (POP_DATA *) ctx->data;
335
336   if (pop_data->status == POP_CONNECTED) {
337     mutt_message _("Closing connection to POP server...");
338
339     if (ctx->readonly) {
340       m_strcpy(buf, sizeof(buf), "RSET\r\n");
341       ret = pop_query (pop_data, buf, sizeof (buf));
342     }
343
344     if (ret != PQ_NOT_CONNECTED) {
345       m_strcpy(buf, sizeof(buf), "QUIT\r\n");
346       pop_query (pop_data, buf, sizeof (buf));
347     }
348
349     mutt_clear_error ();
350   }
351
352   pop_data->status = POP_DISCONNECTED;
353   return;
354 }
355
356 /*
357  * Send data from buffer and receive answer to the same buffer
358  *  0 - successful,
359  * -1 - conection lost,
360  * -2 - invalid command or execution error.
361 */
362 pop_query_status pop_query (POP_DATA * pop_data, char *buf, size_t buflen)
363 {
364   char *c;
365
366   if (pop_data->status != POP_CONNECTED)
367     return PQ_NOT_CONNECTED;
368
369   mutt_socket_write(pop_data->conn, buf);
370
371   c = strpbrk (buf, " \r\n");
372   *c = '\0';
373   snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
374
375   if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
376     pop_data->status = POP_DISCONNECTED;
377     return PQ_NOT_CONNECTED;
378   }
379   if (!m_strncmp(buf, "+OK", 3))
380     return PQ_OK;
381
382   pop_error (pop_data, buf);
383   return PQ_ERR;
384 }
385
386 /*
387  * This function calls  funct(*line, *data)  for each received line,
388  * funct(NULL, *data)  if  rewind(*data)  needs, exits when fail or done.
389  * Returned codes:
390  *  0 - successful,
391  * -1 - conection lost,
392  * -2 - invalid command or execution error,
393  * -3 - error in funct(*line, *data)
394  */
395 pop_query_status pop_fetch_data (POP_DATA * pop_data, const char *query, progress_t* bar,
396                     int (*funct) (char *, void *), void *data)
397 {
398   char buf[LONG_STRING];
399   char *inbuf;
400   char *p;
401   pop_query_status ret;
402   int chunk = 0;
403   long pos = 0;
404   size_t lenbuf = 0;
405
406   m_strcpy(buf, sizeof(buf), query);
407   ret = pop_query (pop_data, buf, sizeof (buf));
408   if (ret != PQ_OK)
409     return ret;
410
411   inbuf = p_new(char, sizeof(buf));
412
413   for (;;) {
414     chunk =
415       mutt_socket_readln(buf, sizeof (buf), pop_data->conn);
416     if (chunk < 0) {
417       pop_data->status = POP_DISCONNECTED;
418       ret = PQ_NOT_CONNECTED;
419       break;
420     }
421
422     p = buf;
423     if (!lenbuf && buf[0] == '.') {
424       if (buf[1] != '.')
425         break;
426       p++;
427     }
428
429     m_strcpy(inbuf + lenbuf,sizeof(buf), p);
430     pos += chunk;
431
432     if (chunk >= ssizeof(buf)) {
433       lenbuf += strlen (p);
434     } else {
435       if (bar)
436         mutt_progress_bar (bar, pos);
437       if (ret == 0 && funct (inbuf, data) < 0)
438         ret = PFD_FUNCT_ERROR;
439       lenbuf = 0;
440     }
441
442     p_realloc(&inbuf, lenbuf + sizeof(buf));
443   }
444
445   p_delete(&inbuf);
446   return ret;
447 }
448
449 /* find message with this UIDL and set refno */
450 static int check_uidl (char *line, void *data)
451 {
452   int i, idx;
453   CONTEXT *ctx = (CONTEXT *)data;
454
455   sscanf (line, "%u %s", &idx, line);
456   for (i = 0; i < ctx->msgcount; i++) {
457     if (!m_strcmp(ctx->hdrs[i]->data, line)) {
458       ctx->hdrs[i]->refno = idx;
459       break;
460     }
461   }
462
463   return 0;
464 }
465
466 /* reconnect and verify indexes if connection was lost */
467 pop_query_status pop_reconnect (CONTEXT * ctx)
468 {
469   pop_query_status ret;
470   POP_DATA *pop_data = (POP_DATA *) ctx->data;
471   progress_t bar;
472
473   if (pop_data->status == POP_CONNECTED)
474     return PQ_OK;
475   if (pop_data->status == POP_BYE)
476     return PQ_NOT_CONNECTED;
477
478   for (;;) {
479     mutt_socket_close (pop_data->conn);
480
481     ret = pop_open_connection (pop_data);
482     if (ret == PQ_OK) {
483       int i;
484
485       bar.msg = _("Verifying message indexes...");
486       bar.size = 0;
487       mutt_progress_bar (&bar, 0);
488
489       for (i = 0; i < ctx->msgcount; i++)
490         ctx->hdrs[i]->refno = -1;
491
492       ret = pop_fetch_data(pop_data, "UIDL\r\n", &bar, check_uidl, ctx);
493       if (ret == PQ_ERR) {
494         mutt_error ("%s", pop_data->err_msg);
495         mutt_sleep (2);
496       }
497     }
498     if (ret == PQ_OK)
499       return PQ_OK;
500
501     pop_logout (ctx);
502
503     if (ret == PQ_ERR)
504       return PQ_NOT_CONNECTED;
505
506     if (query_quadoption (OPT_POPRECONNECT,
507                           _("Connection lost. Reconnect to POP server?")) !=
508         M_YES)
509       return PQ_NOT_CONNECTED;
510   }
511 }