2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-2003 Vsevolod Volkov <vvv@mutt.org.ua>
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.
19 # include "mutt_ssl.h"
24 #include "lib/debug.h"
30 /* given an POP mailbox name, return host, port, username and password */
31 int pop_parse_path (const char *path, ACCOUNT * acct)
39 acct->port = POP_PORT;
40 acct->type = M_ACCT_TYPE_POP;
42 c = safe_strdup (path);
43 url_parse_ciss (&url, c);
45 if (url.scheme == U_POP || url.scheme == U_POPS) {
46 if (url.scheme == U_POPS) {
47 acct->flags |= M_ACCT_SSL;
48 acct->port = POP_SSL_PORT;
51 if ((!url.path || !*url.path) && mutt_account_fromurl (acct, &url) == 0)
59 /* Copy error message to err_msg buffer */
60 void pop_error (POP_DATA * pop_data, char *msg)
64 t = strchr (pop_data->err_msg, '\0');
67 if (!safe_strncmp (msg, "-ERR ", 5)) {
75 strfcpy (t, c, sizeof (pop_data->err_msg) - strlen (pop_data->err_msg));
76 mutt_remove_trailing_ws (pop_data->err_msg);
79 /* Parse CAPA output */
80 static int fetch_capa (char *line, void *data)
82 POP_DATA *pop_data = (POP_DATA *) data;
85 if (!ascii_strncasecmp (line, "SASL", 4)) {
86 FREE (&pop_data->auth_list);
89 pop_data->auth_list = safe_strdup (c);
92 else if (!ascii_strncasecmp (line, "STLS", 4))
93 pop_data->cmd_stls = CMD_AVAILABLE;
95 else if (!ascii_strncasecmp (line, "USER", 4))
96 pop_data->cmd_user = CMD_AVAILABLE;
98 else if (!ascii_strncasecmp (line, "UIDL", 4))
99 pop_data->cmd_uidl = CMD_AVAILABLE;
101 else if (!ascii_strncasecmp (line, "TOP", 3))
102 pop_data->cmd_top = CMD_AVAILABLE;
107 /* Fetch list of the authentication mechanisms */
108 static int fetch_auth (char *line, void *data)
110 POP_DATA *pop_data = (POP_DATA *) data;
112 if (!pop_data->auth_list) {
113 pop_data->auth_list = safe_malloc (strlen (line) + 1);
114 *pop_data->auth_list = '\0';
117 safe_realloc (&pop_data->auth_list,
118 strlen (pop_data->auth_list) + strlen (line) + 2);
119 strcat (pop_data->auth_list, " "); /* __STRCAT_CHECKED__ */
121 strcat (pop_data->auth_list, line); /* __STRCAT_CHECKED__ */
129 * -1 - conection lost,
130 * -2 - execution error.
132 static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode)
134 char buf[LONG_STRING];
136 /* don't check capabilities on reconnect */
137 if (pop_data->capabilities)
140 /* init capabilities */
142 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
143 pop_data->cmd_stls = CMD_NOT_AVAILABLE;
144 pop_data->cmd_user = CMD_NOT_AVAILABLE;
145 pop_data->cmd_uidl = CMD_NOT_AVAILABLE;
146 pop_data->cmd_top = CMD_NOT_AVAILABLE;
147 pop_data->resp_codes = 0;
148 pop_data->expire = 1;
149 pop_data->login_delay = 0;
150 FREE (&pop_data->auth_list);
153 /* Execute CAPA command */
154 if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) {
155 strfcpy (buf, "CAPA\r\n", sizeof (buf));
156 switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) {
159 pop_data->cmd_capa = CMD_AVAILABLE;
164 pop_data->cmd_capa = CMD_NOT_AVAILABLE;
167 case PQ_NOT_CONNECTED:
168 return PQ_NOT_CONNECTED;
172 /* CAPA not supported, use defaults */
173 if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) {
174 pop_data->cmd_user = CMD_UNKNOWN;
175 pop_data->cmd_uidl = CMD_UNKNOWN;
176 pop_data->cmd_top = CMD_UNKNOWN;
178 strfcpy (buf, "AUTH\r\n", sizeof (buf));
179 if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED)
180 return PQ_NOT_CONNECTED;
183 /* Check capabilities */
187 if (!pop_data->expire)
188 msg = _("Unable to leave messages on server.");
189 if (pop_data->cmd_top == CMD_NOT_AVAILABLE)
190 msg = _("Command TOP is not supported by server.");
191 if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE)
192 msg = _("Command UIDL is not supported by server.");
193 if (msg && pop_data->cmd_capa != CMD_AVAILABLE) {
197 pop_data->capabilities = 1;
206 * -1 - conection lost,
207 * -2 - invalid response.
209 pop_query_status pop_connect (POP_DATA * pop_data)
211 char buf[LONG_STRING];
213 pop_data->status = POP_NONE;
214 if (mutt_socket_open (pop_data->conn) < 0 ||
215 mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) {
216 mutt_error (_("Error connecting to server: %s"),
217 pop_data->conn->account.host);
218 return PQ_NOT_CONNECTED;
221 pop_data->status = POP_CONNECTED;
223 if (safe_strncmp (buf, "+OK", 3)) {
224 *pop_data->err_msg = '\0';
225 pop_error (pop_data, buf);
226 mutt_error ("%s", pop_data->err_msg);
230 pop_apop_timestamp (pop_data, buf);
236 * Open connection and authenticate
238 * -1 - conection lost,
239 * -2 - invalid command or execution error,
240 * -3 - authentication canceled.
242 pop_query_status pop_open_connection (POP_DATA * pop_data)
244 pop_query_status ret;
245 unsigned int n, size;
246 char buf[LONG_STRING];
248 ret = pop_connect (pop_data);
254 ret = pop_capabilities (pop_data, 0);
255 if (ret == PQ_NOT_CONNECTED)
262 #if (defined(USE_SSL) || defined(USE_GNUTLS)) && !defined(USE_NSS)
263 /* Attempt STLS if available and desired. */
264 if (pop_data->cmd_stls && !pop_data->conn->ssf) {
265 if (pop_data->use_stls == 0) {
266 ret = query_quadoption (OPT_SSLSTARTTLS,
267 _("Secure connection with TLS?"));
270 pop_data->use_stls = 1;
272 pop_data->use_stls = 2;
274 if (pop_data->use_stls == 2) {
275 strfcpy (buf, "STLS\r\n", sizeof (buf));
276 ret = pop_query (pop_data, buf, sizeof (buf));
277 if (ret == PQ_NOT_CONNECTED)
280 mutt_error ("%s", pop_data->err_msg);
284 else if (mutt_ssl_starttls (pop_data->conn))
286 else if (mutt_gnutls_starttls (pop_data->conn))
289 mutt_error (_("Could not negotiate TLS connection"));
294 /* recheck capabilities after STLS completes */
295 ret = pop_capabilities (pop_data, 1);
296 if (ret == PQ_NOT_CONNECTED)
307 ret = pop_authenticate (pop_data);
308 if (ret == PQ_NOT_CONNECTED)
310 if (ret == PFD_FUNCT_ERROR)
315 /* recheck capabilities after authentication */
316 ret = pop_capabilities (pop_data, 2);
317 if (ret == PQ_NOT_CONNECTED)
324 /* get total size of mailbox */
325 strfcpy (buf, "STAT\r\n", sizeof (buf));
326 ret = pop_query (pop_data, buf, sizeof (buf));
327 if (ret == PQ_NOT_CONNECTED)
330 mutt_error ("%s", pop_data->err_msg);
335 sscanf (buf, "+OK %u %u", &n, &size);
336 pop_data->size = size;
340 pop_data->status = POP_DISCONNECTED;
341 mutt_error _("Server closed connection!");
344 return PQ_NOT_CONNECTED;
347 /* logout from POP server */
348 void pop_logout (CONTEXT * ctx)
350 pop_query_status ret = 0;
351 char buf[LONG_STRING];
352 POP_DATA *pop_data = (POP_DATA *) ctx->data;
354 if (pop_data->status == POP_CONNECTED) {
355 mutt_message _("Closing connection to POP server...");
358 strfcpy (buf, "RSET\r\n", sizeof (buf));
359 ret = pop_query (pop_data, buf, sizeof (buf));
362 if (ret != PQ_NOT_CONNECTED) {
363 strfcpy (buf, "QUIT\r\n", sizeof (buf));
364 pop_query (pop_data, buf, sizeof (buf));
370 pop_data->status = POP_DISCONNECTED;
375 * Send data from buffer and receive answer to the same buffer
377 * -1 - conection lost,
378 * -2 - invalid command or execution error.
380 pop_query_status pop_query_d (POP_DATA * pop_data, char *buf, size_t buflen, char *msg)
382 int dbg = M_SOCK_LOG_CMD;
385 if (pop_data->status != POP_CONNECTED)
386 return PQ_NOT_CONNECTED;
389 /* print msg instaed of real command */
391 dbg = M_SOCK_LOG_FULL;
392 debug_print (M_SOCK_LOG_CMD, ("> %s", msg));
396 mutt_socket_write_d (pop_data->conn, buf, dbg);
398 c = strpbrk (buf, " \r\n");
400 snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), "%s: ", buf);
402 if (mutt_socket_readln (buf, buflen, pop_data->conn) < 0) {
403 pop_data->status = POP_DISCONNECTED;
404 return PQ_NOT_CONNECTED;
406 if (!safe_strncmp (buf, "+OK", 3))
409 pop_error (pop_data, buf);
414 * This function calls funct(*line, *data) for each received line,
415 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
418 * -1 - conection lost,
419 * -2 - invalid command or execution error,
420 * -3 - error in funct(*line, *data)
422 pop_query_status pop_fetch_data (POP_DATA * pop_data, char *query, char *msg,
423 int (*funct) (char *, void *), void *data)
425 char buf[LONG_STRING];
428 pop_query_status ret;
432 strfcpy (buf, query, sizeof (buf));
433 ret = pop_query (pop_data, buf, sizeof (buf));
437 inbuf = safe_malloc (sizeof (buf));
441 mutt_socket_readln_d (buf, sizeof (buf), pop_data->conn,
444 pop_data->status = POP_DISCONNECTED;
445 ret = PQ_NOT_CONNECTED;
450 if (!lenbuf && buf[0] == '.') {
456 strfcpy (inbuf + lenbuf, p, sizeof (buf));
458 if (chunk >= sizeof (buf)) {
459 lenbuf += strlen (p);
463 if (msg && ReadInc && (line % ReadInc == 0))
464 mutt_message ("%s %d", msg, line);
465 if (ret == 0 && funct (inbuf, data) < 0)
466 ret = PFD_FUNCT_ERROR;
470 safe_realloc (&inbuf, lenbuf + sizeof (buf));
477 /* find message with this UIDL and set refno */
478 static int check_uidl (char *line, void *data)
482 CONTEXT *ctx = (CONTEXT *) data;
484 sscanf (line, "%u %s", &index, line);
485 for (i = 0; i < ctx->msgcount; i++) {
486 if (!safe_strcmp (ctx->hdrs[i]->data, line)) {
487 ctx->hdrs[i]->refno = index;
495 /* reconnect and verify indexes if connection was lost */
496 pop_query_status pop_reconnect (CONTEXT * ctx)
498 pop_query_status ret;
499 POP_DATA *pop_data = (POP_DATA *) ctx->data;
501 if (pop_data->status == POP_CONNECTED)
503 if (pop_data->status == POP_BYE)
504 return PQ_NOT_CONNECTED;
507 mutt_socket_close (pop_data->conn);
509 ret = pop_open_connection (pop_data);
511 char *msg = _("Verifying message indexes...");
514 for (i = 0; i < ctx->msgcount; i++)
515 ctx->hdrs[i]->refno = -1;
519 ret = pop_fetch_data (pop_data, "UIDL\r\n", msg, check_uidl, ctx);
521 mutt_error ("%s", pop_data->err_msg);
531 return PQ_NOT_CONNECTED;
533 if (query_quadoption (OPT_POPRECONNECT,
534 _("Connection lost. Reconnect to POP server?")) !=
536 return PQ_NOT_CONNECTED;