master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 * RFC1734 POP3 Authentication
24 * RFC1939 POP3 protocol
25 * RFC2195 CRAM-MD5 authentication
26 * RFC2384 POP URL Scheme
27 * RFC2449 POP3 Extension Mechanism
28 * RFC2595 Using TLS with IMAP, POP3 and ACAP
29 * RFC2831 DIGEST-MD5 authentication
30 * RFC4422 Simple Authentication and Security Layer (SASL)
31 * RFC4616 PLAIN authentication
32 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
33 * RFC5034 POP3 SASL Authentication Mechanism
34 * RFC6749 OAuth 2.0 Authorization Framework
35 * RFC8314 Use of TLS for Email Submission and Access
36 * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
37 *
38 ***************************************************************************/
39#include "curl_setup.h"
40#include "urldata.h"
41#include "pop3.h"
43#ifndef CURL_DISABLE_POP3
45#ifdef HAVE_NETINET_IN_H
46#include <netinet/in.h>
47#endif
48#ifdef HAVE_ARPA_INET_H
49#include <arpa/inet.h>
50#endif
51#ifdef HAVE_NETDB_H
52#include <netdb.h>
53#endif
54#ifdef __VMS
55#include <in.h>
56#include <inet.h>
57#endif
59#include "sendf.h"
60#include "curl_trc.h"
61#include "progress.h"
62#include "transfer.h"
63#include "escape.h"
64#include "pingpong.h"
65#include "vtls/vtls.h"
66#include "cfilters.h"
67#include "connect.h"
68#include "select.h"
69#include "url.h"
70#include "bufref.h"
71#include "curl_sasl.h"
72#include "curl_md5.h"
73#include "curlx/strdup.h"
75/* Authentication type flags */
76#define POP3_TYPE_CLEARTEXT (1 << 0)
77#define POP3_TYPE_APOP (1 << 1)
78#define POP3_TYPE_SASL (1 << 2)
80/* Authentication type values */
81#define POP3_TYPE_NONE 0
82#define POP3_TYPE_ANY (POP3_TYPE_CLEARTEXT | POP3_TYPE_APOP | POP3_TYPE_SASL)
84/* This is the 5-bytes End-Of-Body marker for POP3 */
85#define POP3_EOB "\x0d\x0a\x2e\x0d\x0a"
86#define POP3_EOB_LEN 5
88/* meta key for storing protocol meta at easy handle */
89#define CURL_META_POP3_EASY "meta:proto:pop3:easy"
90/* meta key for storing protocol meta at connection */
91#define CURL_META_POP3_CONN "meta:proto:pop3:conn"
93/*
94 * POP3 easy handle state
95 */
96struct POP3 {
97 curl_pp_transfer transfer;
98 char *id; /* Message ID */
99 char *custom; /* Custom Request */
100};
102/*
103 * POP3 connection state
104 */
105typedef enum {
106 POP3_STOP, /* do nothing state, stops the state machine */
107 POP3_SERVERGREET, /* waiting for the initial greeting immediately after
108 a connect */
109 POP3_CAPA,
110 POP3_STARTTLS,
111 POP3_UPGRADETLS, /* asynchronously upgrade the connection to SSL/TLS
112 (multi mode only) */
113 POP3_AUTH,
114 POP3_APOP,
115 POP3_USER,
116 POP3_PASS,
117 POP3_COMMAND,
118 POP3_QUIT,
119 POP3_LAST /* never used */
120} pop3state;
122struct pop3_conn {
123 struct pingpong pp;
124 pop3state state; /* Always use pop3.c:state() to change state! */
125 size_t eob; /* Number of bytes of the EOB (End Of Body) that
126 have been received so far */
127 size_t strip; /* Number of bytes from the start to ignore as
128 non-body */
129 struct SASL sasl; /* SASL-related storage */
130 char *apoptimestamp; /* APOP timestamp from the server greeting */
131 unsigned char authtypes; /* Accepted authentication types */
132 unsigned char preftype; /* Preferred authentication type */
133 BIT(ssldone); /* Is connect() over SSL done? */
134 BIT(tls_supported); /* StartTLS capability supported by server */
135};
137struct pop3_cmd {
138 const char *name;
139 unsigned short nlen;
140 BIT(multiline); /* response is multi-line with last '.' line */
141 BIT(multiline_with_args); /* is multi-line when command has args */
142};
144static const struct pop3_cmd pop3cmds[] = {
145 { "APOP", 4, FALSE, FALSE },
146 { "AUTH", 4, FALSE, FALSE },
147 { "CAPA", 4, TRUE, TRUE },
148 { "DELE", 4, FALSE, FALSE },
149 { "LIST", 4, TRUE, FALSE },
150 { "MSG", 3, TRUE, TRUE },
151 { "NOOP", 4, FALSE, FALSE },
152 { "PASS", 4, FALSE, FALSE },
153 { "QUIT", 4, FALSE, FALSE },
154 { "RETR", 4, TRUE, TRUE },
155 { "RSET", 4, FALSE, FALSE },
156 { "STAT", 4, FALSE, FALSE },
157 { "STLS", 4, FALSE, FALSE },
158 { "TOP", 3, TRUE, TRUE },
159 { "UIDL", 4, TRUE, FALSE },
160 { "USER", 4, FALSE, FALSE },
161 { "UTF8", 4, FALSE, FALSE },
162 { "XTND", 4, TRUE, TRUE },
163};
165/***********************************************************************
166 *
167 * pop3_parse_url_options()
168 *
169 * Parse the URL login options.
170 */
171static CURLcode pop3_parse_url_options(struct connectdata *conn)
172{
173 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
174 CURLcode result = CURLE_OK;
175 const char *ptr = conn->options;
177 if(!pop3c)
178 return CURLE_FAILED_INIT;
180 while(!result && ptr && *ptr) {
181 const char *key = ptr;
182 const char *value;
184 while(*ptr && *ptr != '=')
185 ptr++;
187 value = ptr + 1;
189 while(*ptr && *ptr != ';')
190 ptr++;
192 if(curl_strnequal(key, "AUTH=", 5)) {
193 result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
194 value, ptr - value);
196 if(result && curl_strnequal(value, "+APOP", ptr - value)) {
197 pop3c->preftype = POP3_TYPE_APOP;
198 pop3c->sasl.prefmech = SASL_AUTH_NONE;
199 result = CURLE_OK;
200 }
201 }
202 else
203 result = CURLE_URL_MALFORMAT;
205 if(*ptr == ';')
206 ptr++;
207 }
209 if(pop3c->preftype != POP3_TYPE_APOP)
210 switch(pop3c->sasl.prefmech) {
211 case SASL_AUTH_NONE:
212 pop3c->preftype = POP3_TYPE_NONE;
213 break;
214 case SASL_AUTH_DEFAULT:
215 pop3c->preftype = POP3_TYPE_ANY;
216 break;
217 default:
218 pop3c->preftype = POP3_TYPE_SASL;
219 break;
220 }
222 return result;
223}
225/***********************************************************************
226 *
227 * pop3_parse_url_path()
228 *
229 * Parse the URL path into separate path components.
230 */
231static CURLcode pop3_parse_url_path(struct Curl_easy *data)
232{
233 /* The POP3 struct is already initialized in pop3_connect() */
234 struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
235 const char *path = &data->state.up.path[1]; /* skip leading path */
237 if(!pop3)
238 return CURLE_FAILED_INIT;
239 /* URL decode the path for the message ID */
240 return Curl_urldecode(path, 0, &pop3->id, NULL, REJECT_CTRL);
241}
243/***********************************************************************
244 *
245 * pop3_parse_custom_request()
246 *
247 * Parse the custom request.
248 */
249static CURLcode pop3_parse_custom_request(struct Curl_easy *data)
250{
251 CURLcode result = CURLE_OK;
252 struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
253 const char *custom = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
255 if(!pop3)
256 return CURLE_FAILED_INIT;
257 /* URL decode the custom request */
258 if(custom)
259 result = Curl_urldecode(custom, 0, &pop3->custom, NULL, REJECT_CTRL);
261 return result;
262}
264/* Return iff a command is defined as "multi-line" (RFC 1939),
265 * has a response terminated by a last line with a '.'.
266 */
267static bool pop3_is_multiline(const char *cmdline)
268{
269 size_t i;
270 for(i = 0; i < CURL_ARRAYSIZE(pop3cmds); ++i) {
271 if(curl_strnequal(pop3cmds[i].name, cmdline, pop3cmds[i].nlen)) {
272 if(!cmdline[pop3cmds[i].nlen])
273 return (bool)pop3cmds[i].multiline;
274 else if(cmdline[pop3cmds[i].nlen] == ' ')
275 return (bool)pop3cmds[i].multiline_with_args;
276 }
277 }
278 /* Unknown command, assume multi-line for backward compatibility with
279 * earlier curl versions that only could do multi-line responses. */
280 return TRUE;
281}
283/***********************************************************************
284 *
285 * pop3_endofresp()
286 *
287 * Checks for an ending POP3 status code at the start of the given string, but
288 * also detects the APOP timestamp from the server greeting and various
289 * capabilities from the CAPA response including the supported authentication
290 * types and allowed SASL mechanisms.
291 */
292static bool pop3_endofresp(struct Curl_easy *data, struct connectdata *conn,
293 const char *line, size_t len, int *resp)
294{
295 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
296 (void)data;
297 DEBUGASSERT(pop3c);
298 if(!pop3c) /* internal error */
299 return TRUE;
301 /* Do we have an error response? */
302 if(len >= 4 && !memcmp("-ERR", line, 4)) {
303 *resp = '-';
305 return TRUE;
306 }
308 /* Are we processing CAPA command responses? */
309 if(pop3c->state == POP3_CAPA) {
310 /* Do we have the terminating line? Per RFC 2449 this is a line
311 containing only a single dot */
312 if((len == 3 && line[0] == '.' && line[1] == '\r') ||
313 (len == 2 && line[0] == '.' && line[1] == '\n'))
314 /* Treat the response as a success */
315 *resp = '+';
316 else
317 /* Treat the response as an untagged continuation */
318 *resp = '*';
320 return TRUE;
321 }
323 /* Do we have a success response? */
324 if(len >= 3 && !memcmp("+OK", line, 3)) {
325 *resp = '+';
327 return TRUE;
328 }
330 /* Do we have a continuation response? */
331 if(len >= 1 && line[0] == '+') {
332 *resp = '*';
334 return TRUE;
335 }
337 return FALSE; /* Nothing for us */
338}
340/***********************************************************************
341 *
342 * pop3_get_message()
343 *
344 * Gets the authentication message from the response buffer.
345 */
346static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out)
347{
348 struct pop3_conn *pop3c =
349 Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
350 char *message;
351 size_t len;
353 if(!pop3c)
354 return CURLE_FAILED_INIT;
355 message = curlx_dyn_ptr(&pop3c->pp.recvbuf);
356 len = pop3c->pp.nfinal;
357 if(len > 2) {
358 /* Find the start of the message */
359 len -= 2;
360 for(message += 2; ISBLANK(*message); message++, len--)
361 ;
363 /* Find the end of the message */
364 while(len--)
365 if(!ISBLANK(message[len]) && !ISNEWLINE(message[len]))
366 break;
368 /* Terminate the message */
369 message[++len] = '\0';
370 Curl_bufref_set(out, message, len, NULL);
371 }
372 else
373 /* junk input => zero length output */
374 Curl_bufref_set(out, "", 0, NULL);
376 return CURLE_OK;
377}
379/***********************************************************************
380 *
381 * pop3_state()
382 *
383 * This is the ONLY way to change POP3 state!
384 */
385static void pop3_state(struct Curl_easy *data, pop3state newstate)
386{
387 struct pop3_conn *pop3c =
388 Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
389 if(pop3c) {
390#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
391 /* for debug purposes */
392 static const char * const names[] = {
393 "STOP",
394 "SERVERGREET",
395 "CAPA",
396 "STARTTLS",
397 "UPGRADETLS",
398 "AUTH",
399 "APOP",
400 "USER",
401 "PASS",
402 "COMMAND",
403 "QUIT",
404 /* LAST */
405 };
407 if(pop3c->state != newstate)
408 infof(data, "POP3 %p state change from %s to %s",
409 (void *)pop3c, names[pop3c->state], names[newstate]);
410#endif
412 pop3c->state = newstate;
413 }
414}
416/***********************************************************************
417 *
418 * pop3_perform_capa()
419 *
420 * Sends the CAPA command in order to obtain a list of server side supported
421 * capabilities.
422 */
423static CURLcode pop3_perform_capa(struct Curl_easy *data,
424 struct connectdata *conn)
425{
426 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
427 CURLcode result = CURLE_OK;
429 if(!pop3c)
430 return CURLE_FAILED_INIT;
432 pop3c->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
433 pop3c->sasl.authused = SASL_AUTH_NONE; /* Clear the auth. mechanism used */
434 pop3c->tls_supported = FALSE; /* Clear the TLS capability */
436 /* Send the CAPA command */
437 result = Curl_pp_sendf(data, &pop3c->pp, "%s", "CAPA");
439 if(!result)
440 pop3_state(data, POP3_CAPA);
442 return result;
443}
445/***********************************************************************
446 *
447 * pop3_perform_starttls()
448 *
449 * Sends the STLS command to start the upgrade to TLS.
450 */
451static CURLcode pop3_perform_starttls(struct Curl_easy *data,
452 struct connectdata *conn)
453{
454 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
455 CURLcode result;
457 if(!pop3c)
458 return CURLE_FAILED_INIT;
460 /* Send the STLS command */
461 result = Curl_pp_sendf(data, &pop3c->pp, "%s", "STLS");
462 if(!result)
463 pop3_state(data, POP3_STARTTLS);
465 return result;
466}
468/***********************************************************************
469 *
470 * pop3_perform_upgrade_tls()
471 *
472 * Performs the upgrade to TLS.
473 */
474static CURLcode pop3_perform_upgrade_tls(struct Curl_easy *data,
475 struct connectdata *conn)
476{
477#ifdef USE_SSL
478 /* Start the SSL connection */
479 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
480 CURLcode result;
481 bool ssldone = FALSE;
483 if(!pop3c)
484 return CURLE_FAILED_INIT;
486 if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
487 result = Curl_ssl_cfilter_add(
488 data, Curl_conn_get_origin(conn, FIRSTSOCKET), conn, FIRSTSOCKET);
489 if(result)
490 goto out;
491 /* Change the connection handler */
492 conn->scheme = &Curl_scheme_pop3s;
493 }
495 DEBUGASSERT(!pop3c->ssldone);
496 result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
497 DEBUGF(infof(data, "pop3_perform_upgrade_tls, connect -> %d, %d",
498 (int)result, ssldone));
499 if(!result && ssldone) {
500 pop3c->ssldone = ssldone;
501 /* perform CAPA now, changes pop3c->state out of POP3_UPGRADETLS */
502 result = pop3_perform_capa(data, conn);
503 }
504out:
505 return result;
506#else
507 (void)data;
508 (void)conn;
509 return CURLE_NOT_BUILT_IN;
510#endif
511}
513/***********************************************************************
514 *
515 * pop3_perform_user()
516 *
517 * Sends a clear text USER command to authenticate with.
518 */
519static CURLcode pop3_perform_user(struct Curl_easy *data,
520 struct connectdata *conn)
521{
522 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
523 CURLcode result = CURLE_OK;
525 if(!pop3c)
526 return CURLE_FAILED_INIT;
528 /* Check we have a username and password to authenticate with and end the
529 connect phase if we do not */
530 if(!conn->creds) {
531 pop3_state(data, POP3_STOP);
533 return result;
534 }
536 /* Send the USER command */
537 result = Curl_pp_sendf(data, &pop3c->pp, "USER %s",
538 Curl_creds_user(conn->creds));
539 if(!result)
540 pop3_state(data, POP3_USER);
542 return result;
543}
545#ifndef CURL_DISABLE_DIGEST_AUTH
546/***********************************************************************
547 *
548 * pop3_perform_apop()
549 *
550 * Sends an APOP command to authenticate with.
551 */
552static CURLcode pop3_perform_apop(struct Curl_easy *data,
553 struct connectdata *conn)
554{
555 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
556 CURLcode result = CURLE_OK;
557 size_t i;
558 struct MD5_context *ctxt;
559 unsigned char digest[MD5_DIGEST_LEN];
560 char secret[(2 * MD5_DIGEST_LEN) + 1];
562 if(!pop3c)
563 return CURLE_FAILED_INIT;
565 /* Check we have a username and password to authenticate with and end the
566 connect phase if we do not */
567 if(!data->state.creds) {
568 pop3_state(data, POP3_STOP);
570 return result;
571 }
573 /* Create the digest */
574 ctxt = Curl_MD5_init(&Curl_DIGEST_MD5);
575 if(!ctxt)
576 return CURLE_OUT_OF_MEMORY;
578 Curl_MD5_update(ctxt, (const unsigned char *)pop3c->apoptimestamp,
579 curlx_uztoui(strlen(pop3c->apoptimestamp)));
581 Curl_MD5_update(ctxt, (const unsigned char *)Curl_creds_passwd(conn->creds),
582 curlx_uztoui(strlen(Curl_creds_passwd(conn->creds))));
584 /* Finalise the digest */
585 Curl_MD5_final(ctxt, digest);
587 /* Convert the calculated 16 octet digest into a 32-byte hex string */
588 for(i = 0; i < MD5_DIGEST_LEN; i++)
589 curl_msnprintf(&secret[2 * i], 3, "%02x", digest[i]);
591 result = Curl_pp_sendf(data, &pop3c->pp, "APOP %s %s",
592 Curl_creds_user(conn->creds), secret);
594 if(!result)
595 pop3_state(data, POP3_APOP);
597 return result;
598}
599#endif
601/***********************************************************************
602 *
603 * pop3_perform_auth()
604 *
605 * Sends an AUTH command allowing the client to login with the given SASL
606 * authentication mechanism.
607 */
608static CURLcode pop3_perform_auth(struct Curl_easy *data,
609 const char *mech,
610 const struct bufref *initresp)
611{
612 struct pop3_conn *pop3c =
613 Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
614 CURLcode result = CURLE_OK;
615 const char *ir = Curl_bufref_ptr(initresp);
617 if(!pop3c)
618 return CURLE_FAILED_INIT;
620 if(ir) { /* AUTH <mech> ...<crlf> */
621 /* Send the AUTH command with the initial response */
622 result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s %s",
623 mech, *ir ? ir : "=");
624 }
625 else {
626 /* Send the AUTH command */
627 result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s", mech);
628 }
630 return result;
631}
633/***********************************************************************
634 *
635 * pop3_continue_auth()
636 *
637 * Sends SASL continuation data.
638 */
639static CURLcode pop3_continue_auth(struct Curl_easy *data,
640 const char *mech,
641 const struct bufref *resp)
642{
643 struct pop3_conn *pop3c =
644 Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
646 (void)mech;
647 if(!pop3c)
648 return CURLE_FAILED_INIT;
650 return Curl_pp_sendf(data, &pop3c->pp, "%s", Curl_bufref_ptr(resp));
651}
653/***********************************************************************
654 *
655 * pop3_cancel_auth()
656 *
657 * Sends SASL cancellation.
658 */
659static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech)
660{
661 struct pop3_conn *pop3c =
662 Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
664 (void)mech;
665 if(!pop3c)
666 return CURLE_FAILED_INIT;
668 return Curl_pp_sendf(data, &pop3c->pp, "*");
669}
671/***********************************************************************
672 *
673 * pop3_perform_authentication()
674 *
675 * Initiates the authentication sequence, with the appropriate SASL
676 * authentication mechanism, falling back to APOP and clear text should a
677 * common mechanism not be available between the client and server.
678 */
679static CURLcode pop3_perform_authentication(struct Curl_easy *data,
680 struct connectdata *conn)
681{
682 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
683 CURLcode result = CURLE_OK;
684 saslprogress progress = SASL_IDLE;
686 if(!pop3c)
687 return CURLE_FAILED_INIT;
689 /* Check we have enough data to authenticate with and end the
690 connect phase if we do not */
691 if(!Curl_sasl_can_authenticate(&pop3c->sasl, data)) {
692 pop3_state(data, POP3_STOP);
693 return result;
694 }
696 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) {
697 /* Calculate the SASL login details */
698 result = Curl_sasl_start(&pop3c->sasl, data, FALSE, &progress);
700 if(!result && progress == SASL_INPROGRESS)
701 pop3_state(data, POP3_AUTH);
702 }
704 if(!result && progress == SASL_IDLE) {
705#ifndef CURL_DISABLE_DIGEST_AUTH
706 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
707 /* Perform APOP authentication */
708 result = pop3_perform_apop(data, conn);
709 else
710#endif
711 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
712 /* Perform clear text authentication */
713 result = pop3_perform_user(data, conn);
714 else
715 result = Curl_sasl_is_blocked(&pop3c->sasl, data);
716 }
718 return result;
719}
721/***********************************************************************
722 *
723 * pop3_perform_command()
724 *
725 * Sends a POP3 based command.
726 */
727static CURLcode pop3_perform_command(struct Curl_easy *data)
728{
729 CURLcode result = CURLE_OK;
730 struct connectdata *conn = data->conn;
731 struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
732 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
733 const char *command = NULL;
735 if(!pop3 || !pop3c)
736 return CURLE_FAILED_INIT;
738 /* Calculate the default command */
739 if(pop3->id[0] == '\0' || data->set.list_only) {
740 command = "LIST";
742 if(pop3->id[0] != '\0')
743 /* Message specific LIST so skip the BODY transfer */
744 pop3->transfer = PPTRANSFER_INFO;
745 }
746 else
747 command = "RETR";
749 if(pop3->custom && pop3->custom[0] != '\0')
750 command = pop3->custom;
752 /* Send the command */
753 if(pop3->id[0] != '\0')
754 result = Curl_pp_sendf(data, &pop3c->pp, "%s %s", command, pop3->id);
755 else
756 result = Curl_pp_sendf(data, &pop3c->pp, "%s", command);
758 if(!result) {
759 pop3_state(data, POP3_COMMAND);
760 data->req.no_body = !pop3_is_multiline(command);
761 }
763 return result;
764}
766/***********************************************************************
767 *
768 * pop3_perform_quit()
769 *
770 * Performs the quit action prior to sclose() be called.
771 */
772static CURLcode pop3_perform_quit(struct Curl_easy *data,
773 struct connectdata *conn)
774{
775 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
776 CURLcode result;
778 if(!pop3c)
779 return CURLE_FAILED_INIT;
781 /* Send the QUIT command */
782 result = Curl_pp_sendf(data, &pop3c->pp, "%s", "QUIT");
783 if(!result)
784 pop3_state(data, POP3_QUIT);
786 return result;
787}
789/* For the initial server greeting */
790static CURLcode pop3_state_servergreet_resp(struct Curl_easy *data,
791 int pop3code,
792 pop3state instate)
793{
794 CURLcode result = CURLE_OK;
795 struct connectdata *conn = data->conn;
796 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
797 const char *line;
798 size_t len;
800 (void)instate;
801 if(!pop3c)
802 return CURLE_FAILED_INIT;
804 line = curlx_dyn_ptr(&pop3c->pp.recvbuf);
805 len = pop3c->pp.nfinal;
807 if(pop3code != '+') {
808 failf(data, "Got unexpected pop3-server response");
809 result = CURLE_WEIRD_SERVER_REPLY;
810 }
811 else if(len > 3) {
812 /* Does the server support APOP authentication? */
813 const char *lt;
814 const char *gt = NULL;
816 /* Look for the APOP timestamp */
817 lt = memchr(line, '<', len);
818 if(lt)
819 /* search the remainder for '>' */
820 gt = memchr(lt, '>', len - (lt - line));
821 if(gt) {
822 /* the length of the timestamp, including the brackets */
823 size_t timestamplen = gt - lt + 1;
824 const char *at = memchr(lt, '@', timestamplen);
825 /* If the timestamp does not contain '@' it is not (as required by
826 RFC-1939) conformant to the RFC-822 message id syntax, and we
827 therefore do not use APOP authentication. */
828 if(at) {
829 /* dupe the timestamp */
830 pop3c->apoptimestamp = curlx_memdup0(lt, timestamplen);
831 if(!pop3c->apoptimestamp)
832 return CURLE_OUT_OF_MEMORY;
833 /* Store the APOP capability */
834 pop3c->authtypes |= POP3_TYPE_APOP;
835 }
836 }
838 if(!result)
839 result = pop3_perform_capa(data, conn);
840 }
842 return result;
843}
845/* For CAPA responses */
846static CURLcode pop3_state_capa_resp(struct Curl_easy *data, int pop3code,
847 pop3state instate)
848{
849 CURLcode result = CURLE_OK;
850 struct connectdata *conn = data->conn;
851 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
852 const char *line;
853 size_t len;
855 (void)instate;
856 if(!pop3c)
857 return CURLE_FAILED_INIT;
859 line = curlx_dyn_ptr(&pop3c->pp.recvbuf);
860 len = pop3c->pp.nfinal;
862 /* Do we have an untagged continuation response? */
863 if(pop3code == '*') {
864 /* Does the server support the STLS capability? */
865 if(len >= 4 && curl_strnequal(line, "STLS", 4))
866 pop3c->tls_supported = TRUE;
868 /* Does the server support clear text authentication? */
869 else if(len >= 4 && curl_strnequal(line, "USER", 4))
870 pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
872 /* Does the server support SASL based authentication? */
873 else if(len >= 5 && curl_strnequal(line, "SASL ", 5)) {
874 pop3c->authtypes |= POP3_TYPE_SASL;
876 /* Advance past the SASL keyword */
877 line += 5;
878 len -= 5;
880 /* Loop through the data line */
881 for(;;) {
882 size_t llen;
883 size_t wordlen = 0;
884 unsigned short mechbit;
886 while(len && (ISBLANK(*line) || ISNEWLINE(*line))) {
887 line++;
888 len--;
889 }
891 if(!len)
892 break;
894 /* Extract the word */
895 while(wordlen < len && !ISBLANK(line[wordlen]) &&
896 !ISNEWLINE(line[wordlen]))
897 wordlen++;
899 /* Test the word for a matching authentication mechanism */
900 mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
901 if(mechbit && llen == wordlen)
902 pop3c->sasl.authmechs |= mechbit;
904 line += wordlen;
905 len -= wordlen;
906 }
907 }
908 }
909 else {
910 /* Clear text is supported when CAPA is not recognised */
911 if(pop3code != '+')
912 pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
914 if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET))
915 result = pop3_perform_authentication(data, conn);
916 else if(pop3code == '+' && pop3c->tls_supported)
917 /* Switch to TLS connection now */
918 result = pop3_perform_starttls(data, conn);
919 else if(data->set.use_ssl <= CURLUSESSL_TRY)
920 /* Fallback and carry on with authentication */
921 result = pop3_perform_authentication(data, conn);
922 else {
923 failf(data, "STLS not supported.");
924 result = CURLE_USE_SSL_FAILED;
925 }
926 }
928 return result;
929}
931/* For STARTTLS responses */
932static CURLcode pop3_state_starttls_resp(struct Curl_easy *data,
933 struct connectdata *conn,
934 int pop3code,
935 pop3state instate)
936{
937 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
938 CURLcode result = CURLE_OK;
939 (void)instate;
941 if(!pop3c)
942 return CURLE_FAILED_INIT;
944 /* Pipelining in response is forbidden. */
945 if(pop3c->pp.overflow)
946 return CURLE_WEIRD_SERVER_REPLY;
948 if(pop3code != '+') {
949 if(data->set.use_ssl != CURLUSESSL_TRY) {
950 failf(data, "STARTTLS denied");
951 result = CURLE_USE_SSL_FAILED;
952 }
953 else
954 result = pop3_perform_authentication(data, conn);
955 }
956 else
957 pop3_state(data, POP3_UPGRADETLS);
959 return result;
960}
962/* For SASL authentication responses */
963static CURLcode pop3_state_auth_resp(struct Curl_easy *data,
964 int pop3code,
965 pop3state instate)
966{
967 CURLcode result = CURLE_OK;
968 struct connectdata *conn = data->conn;
969 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
970 saslprogress progress;
972 (void)instate;
973 if(!pop3c)
974 return CURLE_FAILED_INIT;
976 result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress);
977 if(!result)
978 switch(progress) {
979 case SASL_DONE:
980 pop3_state(data, POP3_STOP); /* Authenticated */
981 break;
982 case SASL_IDLE: /* No mechanism left after cancellation */
983#ifndef CURL_DISABLE_DIGEST_AUTH
984 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
985 /* Perform APOP authentication */
986 result = pop3_perform_apop(data, conn);
987 else
988#endif
989 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
990 /* Perform clear text authentication */
991 result = pop3_perform_user(data, conn);
992 else {
993 failf(data, "Authentication cancelled");
994 result = CURLE_LOGIN_DENIED;
995 }
996 break;
997 default:
998 break;
999 }
1001 return result;
1002}
1004#ifndef CURL_DISABLE_DIGEST_AUTH
1005/* For APOP responses */
1006static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code,
1007 pop3state instate)
1008{
1009 CURLcode result = CURLE_OK;
1010 (void)instate;
1012 if(pop3code != '+') {
1013 failf(data, "Authentication failed: %d", pop3code);
1014 result = CURLE_LOGIN_DENIED;
1015 }
1016 else
1017 /* End of connect phase */
1018 pop3_state(data, POP3_STOP);
1020 return result;
1021}
1022#endif
1024/* For USER responses */
1025static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code,
1026 pop3state instate)
1027{
1028 CURLcode result = CURLE_OK;
1029 struct connectdata *conn = data->conn;
1030 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1031 (void)instate;
1033 if(!pop3c)
1034 return CURLE_FAILED_INIT;
1036 if(pop3code != '+') {
1037 failf(data, "Access denied. %c", pop3code);
1038 result = CURLE_LOGIN_DENIED;
1039 }
1040 else
1041 /* Send the PASS command */
1042 result = Curl_pp_sendf(data, &pop3c->pp, "PASS %s",
1043 Curl_creds_passwd(conn->creds));
1044 if(!result)
1045 pop3_state(data, POP3_PASS);
1047 return result;
1048}
1050/* For PASS responses */
1051static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code,
1052 pop3state instate)
1053{
1054 CURLcode result = CURLE_OK;
1055 (void)instate;
1057 if(pop3code != '+') {
1058 failf(data, "Access denied. %c", pop3code);
1059 result = CURLE_LOGIN_DENIED;
1060 }
1061 else
1062 /* End of connect phase */
1063 pop3_state(data, POP3_STOP);
1065 return result;
1066}
1068/***********************************************************************
1069 *
1070 * pop3_write()
1071 *
1072 * This function scans the body after the end-of-body and writes everything
1073 * until the end is found.
1074 */
1075static CURLcode pop3_write(struct Curl_easy *data, const char *str,
1076 size_t nread, bool is_eos)
1077{
1078 /* This code could be made into a special function in the handler struct */
1079 CURLcode result = CURLE_OK;
1080 struct connectdata *conn = data->conn;
1081 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1082 bool strip_dot = FALSE;
1083 size_t last = 0;
1084 size_t i;
1085 (void)is_eos;
1087 if(!pop3c)
1088 return CURLE_FAILED_INIT;
1090 /* Search through the buffer looking for the end-of-body marker which is
1091 5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
1092 the eob so the server will have prefixed it with an extra dot which we
1093 need to strip out. Additionally the marker could of course be spread out
1094 over 5 different data chunks. */
1095 for(i = 0; i < nread; i++) {
1096 size_t prev = pop3c->eob;
1098 switch(str[i]) {
1099 case 0x0d:
1100 if(pop3c->eob == 0) {
1101 pop3c->eob++;
1103 if(i) {
1104 /* Write out the body part that did not match */
1105 result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
1106 i - last);
1108 if(result)
1109 return result;
1111 last = i;
1112 }
1113 }
1114 else if(pop3c->eob == 3)
1115 pop3c->eob++;
1116 else
1117 /* If the character match was not at position 0 or 3 then restart the
1118 pattern matching */
1119 pop3c->eob = 1;
1120 break;
1122 case 0x0a:
1123 if(pop3c->eob == 1 || pop3c->eob == 4)
1124 pop3c->eob++;
1125 else
1126 /* If the character match was not at position 1 or 4 then start the
1127 search again */
1128 pop3c->eob = 0;
1129 break;
1131 case 0x2e:
1132 if(pop3c->eob == 2)
1133 pop3c->eob++;
1134 else if(pop3c->eob == 3) {
1135 /* We have an extra dot after the CRLF which we need to strip off */
1136 strip_dot = TRUE;
1137 pop3c->eob = 0;
1138 }
1139 else
1140 /* If the character match was not at position 2 then start the search
1141 again */
1142 pop3c->eob = 0;
1143 break;
1145 default:
1146 pop3c->eob = 0;
1147 break;
1148 }
1150 /* Did we have a partial match which has subsequently failed? */
1151 if(prev && prev >= pop3c->eob) {
1152 /* Strip can only be non-zero for the first mismatch after CRLF and
1153 then both prev and strip are equal and nothing will be output below */
1154 while(prev && pop3c->strip) {
1155 prev--;
1156 pop3c->strip--;
1157 }
1159 if(prev) {
1160 /* If the partial match was the CRLF and dot then only write the CRLF
1161 as the server would have inserted the dot */
1162 if(strip_dot && prev - 1 > 0) {
1163 result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB,
1164 prev - 1);
1165 }
1166 else if(!strip_dot) {
1167 result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB,
1168 prev);
1169 }
1170 else {
1171 result = CURLE_OK;
1172 }
1174 if(result)
1175 return result;
1177 last = i;
1178 strip_dot = FALSE;
1179 }
1180 }
1181 }
1183 if(pop3c->eob == POP3_EOB_LEN) {
1184 /* We have a full match so the transfer is done, however we must transfer
1185 the CRLF at the start of the EOB as this is considered to be part of the
1186 message as per RFC-1939, sect. 3 */
1187 result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB, 2);
1189 CURL_REQ_CLEAR_RECV(data);
1190 pop3c->eob = 0;
1192 return result;
1193 }
1195 if(pop3c->eob)
1196 /* While EOB is matching nothing should be output */
1197 return CURLE_OK;
1199 if(nread - last) {
1200 result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
1201 nread - last);
1202 }
1204 return result;
1205}
1207/* For command responses */
1208static CURLcode pop3_state_command_resp(struct Curl_easy *data,
1209 int pop3code,
1210 pop3state instate)
1211{
1212 CURLcode result = CURLE_OK;
1213 struct connectdata *conn = data->conn;
1214 struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1215 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1216 struct pingpong *pp;
1218 (void)instate;
1219 if(!pop3 || !pop3c)
1220 return CURLE_FAILED_INIT;
1222 pp = &pop3c->pp;
1223 if(pop3code != '+') {
1224 pop3_state(data, POP3_STOP);
1225 return CURLE_WEIRD_SERVER_REPLY;
1226 }
1228 /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
1229 EOB string so count this is two matching bytes. This is necessary to make
1230 the code detect the EOB if the only data than comes now is %2e CR LF like
1231 when there is no body to return. */
1232 pop3c->eob = 2;
1234 /* Since this initial CR LF pair is not part of the actual body, we set
1235 the strip counter here so that these bytes will not be delivered. */
1236 pop3c->strip = 2;
1238 if(pop3->transfer == PPTRANSFER_BODY) {
1239 /* POP3 download */
1240 Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
1242 if(pp->overflow) {
1243 /* The recv buffer contains data that is actually body content so send
1244 it as such. Note that there may even be additional "headers" after
1245 the body */
1247 /* keep only the overflow */
1248 curlx_dyn_tail(&pp->recvbuf, pp->overflow);
1249 pp->nfinal = 0; /* done */
1251 if(!data->req.no_body) {
1252 result = pop3_write(data, curlx_dyn_ptr(&pp->recvbuf),
1253 curlx_dyn_len(&pp->recvbuf), FALSE);
1254 if(result)
1255 return result;
1256 }
1258 /* reset the buffer */
1259 curlx_dyn_reset(&pp->recvbuf);
1260 pp->overflow = 0;
1261 }
1262 }
1263 else
1264 pp->overflow = 0;
1266 /* End of DO phase */
1267 pop3_state(data, POP3_STOP);
1269 return result;
1270}
1272static CURLcode pop3_statemachine(struct Curl_easy *data,
1273 struct connectdata *conn)
1274{
1275 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1276 CURLcode result = CURLE_OK;
1277 int pop3code;
1278 struct pingpong *pp;
1279 size_t nread = 0;
1280 (void)data;
1282 if(!pop3c)
1283 return CURLE_FAILED_INIT;
1285 pp = &pop3c->pp;
1286 /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
1287upgrade_tls:
1288 if(pop3c->state == POP3_UPGRADETLS) {
1289 result = pop3_perform_upgrade_tls(data, conn);
1290 if(result || (pop3c->state == POP3_UPGRADETLS))
1291 return result;
1292 }
1294 /* Flush any data that needs to be sent */
1295 if(pp->sendleft)
1296 return Curl_pp_flushsend(data, pp);
1298 do {
1299 /* Read the response from the server */
1300 result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &pop3code, &nread);
1301 if(result)
1302 return result;
1304 if(!pop3code)
1305 break;
1307 /* We have now received a full POP3 server response */
1308 switch(pop3c->state) {
1309 case POP3_SERVERGREET:
1310 result = pop3_state_servergreet_resp(data, pop3code, pop3c->state);
1311 break;
1313 case POP3_CAPA:
1314 result = pop3_state_capa_resp(data, pop3code, pop3c->state);
1315 break;
1317 case POP3_STARTTLS:
1318 result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state);
1319 /* During UPGRADETLS, leave the read loop as we need to connect
1320 * (e.g. TLS handshake) before we continue sending/receiving. */
1321 if(!result && (pop3c->state == POP3_UPGRADETLS))
1322 goto upgrade_tls;
1323 break;
1325 case POP3_AUTH:
1326 result = pop3_state_auth_resp(data, pop3code, pop3c->state);
1327 break;
1329#ifndef CURL_DISABLE_DIGEST_AUTH
1330 case POP3_APOP:
1331 result = pop3_state_apop_resp(data, pop3code, pop3c->state);
1332 break;
1333#endif
1335 case POP3_USER:
1336 result = pop3_state_user_resp(data, pop3code, pop3c->state);
1337 break;
1339 case POP3_PASS:
1340 result = pop3_state_pass_resp(data, pop3code, pop3c->state);
1341 break;
1343 case POP3_COMMAND:
1344 result = pop3_state_command_resp(data, pop3code, pop3c->state);
1345 break;
1347 case POP3_QUIT:
1348 pop3_state(data, POP3_STOP);
1349 break;
1351 default:
1352 /* internal error */
1353 pop3_state(data, POP3_STOP);
1354 break;
1355 }
1356 } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
1358 return result;
1359}
1361/* Called repeatedly until done from multi.c */
1362static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done)
1363{
1364 CURLcode result = CURLE_OK;
1365 struct connectdata *conn = data->conn;
1366 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1368 if(!pop3c)
1369 return CURLE_FAILED_INIT;
1370 result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE);
1371 *done = (pop3c->state == POP3_STOP);
1373 return result;
1374}
1376static CURLcode pop3_block_statemach(struct Curl_easy *data,
1377 struct connectdata *conn,
1378 bool disconnecting)
1379{
1380 CURLcode result = CURLE_OK;
1381 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1383 if(!pop3c)
1384 return CURLE_FAILED_INIT;
1386 while(pop3c->state != POP3_STOP && !result)
1387 result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting);
1389 return result;
1390}
1392/* For the POP3 "protocol connect" and "doing" phases only */
1393static CURLcode pop3_pollset(struct Curl_easy *data,
1394 struct easy_pollset *ps)
1395{
1396 struct pop3_conn *pop3c =
1397 Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN);
1398 return pop3c ? Curl_pp_pollset(data, &pop3c->pp, ps) : CURLE_OK;
1399}
1401/* SASL parameters for the pop3 protocol */
1402static const struct SASLproto saslpop3 = {
1403 "pop", /* The service name */
1404 pop3_perform_auth, /* Send authentication command */
1405 pop3_continue_auth, /* Send authentication continuation */
1406 pop3_cancel_auth, /* Send authentication cancellation */
1407 pop3_get_message, /* Get SASL response message */
1408 255 - 8, /* Max line len - strlen("AUTH ") - 1 space - CRLF */
1409 '*', /* Code received when continuation is expected */
1410 '+', /* Code to receive upon authentication success */
1411 SASL_AUTH_DEFAULT, /* Default mechanisms */
1412 SASL_FLAG_BASE64 /* Configuration flags */
1413};
1415/***********************************************************************
1416 *
1417 * pop3_connect()
1418 *
1419 * This function should do everything that is to be considered a part of the
1420 * connection phase.
1421 *
1422 * The variable 'done' points to will be TRUE if the protocol-layer connect
1423 * phase is done when this function returns, or FALSE if not.
1424 */
1425static CURLcode pop3_connect(struct Curl_easy *data, bool *done)
1426{
1427 CURLcode result = CURLE_OK;
1428 struct connectdata *conn = data->conn;
1429 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1430 struct pingpong *pp = pop3c ? &pop3c->pp : NULL;
1432 *done = FALSE; /* default to not done yet */
1433 if(!pop3c)
1434 return CURLE_FAILED_INIT;
1436 PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp);
1438 /* Set the default preferred authentication type and mechanism */
1439 pop3c->preftype = POP3_TYPE_ANY;
1440 Curl_sasl_init(&pop3c->sasl, data, &saslpop3);
1442 /* Initialize the pingpong layer */
1443 Curl_pp_init(pp, Curl_pgrs_now(data));
1445 /* Parse the URL options */
1446 result = pop3_parse_url_options(conn);
1447 if(result)
1448 return result;
1450 /* Start off waiting for the server greeting response */
1451 pop3_state(data, POP3_SERVERGREET);
1453 result = pop3_multi_statemach(data, done);
1455 return result;
1456}
1458/***********************************************************************
1459 *
1460 * pop3_done()
1461 *
1462 * The DONE function. This does what needs to be done after a single DO has
1463 * performed.
1464 *
1465 * Input argument is already checked for validity.
1466 */
1467static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
1468 bool premature)
1469{
1470 CURLcode result = CURLE_OK;
1471 struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1473 (void)premature;
1475 if(!pop3)
1476 return CURLE_OK;
1478 if(status) {
1479 CURL_TRC_M(data, "POP3 done with bad status");
1480 connclose(data->conn);
1481 result = status; /* use the already set error code */
1482 }
1484 /* Cleanup our per-request based variables */
1485 curlx_safefree(pop3->id);
1486 curlx_safefree(pop3->custom);
1488 /* Clear the transfer mode for the next request */
1489 pop3->transfer = PPTRANSFER_BODY;
1491 return result;
1492}
1494/***********************************************************************
1495 *
1496 * pop3_perform()
1497 *
1498 * This is the actual DO function for POP3. Get a message/listing according to
1499 * the options previously setup.
1500 */
1501static CURLcode pop3_perform(struct Curl_easy *data, bool *connected,
1502 bool *dophase_done)
1503{
1504 /* This is POP3 and no proxy */
1505 CURLcode result = CURLE_OK;
1506 struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY);
1508 if(!pop3)
1509 return CURLE_FAILED_INIT;
1511 DEBUGF(infof(data, "DO phase starts"));
1513 /* Start the first command in the DO phase, may alter data->req.no_body */
1514 result = pop3_perform_command(data);
1515 if(result)
1516 return result;
1518 if(data->req.no_body)
1519 /* Requested no body means no transfer */
1520 pop3->transfer = PPTRANSFER_INFO;
1522 *dophase_done = FALSE; /* not done yet */
1524 /* Run the state-machine */
1525 result = pop3_multi_statemach(data, dophase_done);
1526 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
1528 if(*dophase_done)
1529 DEBUGF(infof(data, "DO phase is complete"));
1531 return result;
1532}
1534/* Call this when the DO phase has completed */
1535static CURLcode pop3_dophase_done(struct Curl_easy *data, bool connected)
1536{
1537 (void)data;
1538 (void)connected;
1540 return CURLE_OK;
1541}
1543/***********************************************************************
1544 *
1545 * pop3_regular_transfer()
1546 *
1547 * The input argument is already checked for validity.
1548 *
1549 * Performs all commands done before a regular transfer between a local and a
1550 * remote host.
1551 */
1552static CURLcode pop3_regular_transfer(struct Curl_easy *data,
1553 bool *dophase_done)
1554{
1555 CURLcode result = CURLE_OK;
1556 bool connected = FALSE;
1558 /* Make sure size is unknown at this point */
1559 data->req.size = -1;
1561 /* Set the progress data */
1562 Curl_pgrsReset(data);
1564 /* Carry out the perform */
1565 result = pop3_perform(data, &connected, dophase_done);
1567 /* Perform post DO phase operations if necessary */
1568 if(!result && *dophase_done)
1569 result = pop3_dophase_done(data, connected);
1571 return result;
1572}
1574/***********************************************************************
1575 *
1576 * pop3_do()
1577 *
1578 * This function is registered as 'curl_do' function. It decodes the path
1579 * parts etc as a wrapper to the actual DO function (pop3_perform).
1580 *
1581 * The input argument is already checked for validity.
1582 */
1583static CURLcode pop3_do(struct Curl_easy *data, bool *done)
1584{
1585 CURLcode result = CURLE_OK;
1586 *done = FALSE; /* default to false */
1588 /* Parse the URL path */
1589 result = pop3_parse_url_path(data);
1590 if(result)
1591 return result;
1593 /* Parse the custom request */
1594 result = pop3_parse_custom_request(data);
1595 if(result)
1596 return result;
1598 result = pop3_regular_transfer(data, done);
1600 return result;
1601}
1603/***********************************************************************
1604 *
1605 * pop3_disconnect()
1606 *
1607 * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
1608 * resources. BLOCKING.
1609 */
1610static CURLcode pop3_disconnect(struct Curl_easy *data,
1611 struct connectdata *conn, bool dead_connection)
1612{
1613 struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN);
1614 (void)data;
1616 if(!pop3c)
1617 return CURLE_FAILED_INIT;
1619 /* We cannot send quit unconditionally. If this connection is stale or
1620 bad in any way, sending quit and waiting around here will make the
1621 disconnect wait in vain and cause more problems than we need to. */
1623 if(!dead_connection && conn->bits.protoconnstart &&
1624 !Curl_pp_needs_flush(data, &pop3c->pp) &&
1625 !pop3_perform_quit(data, conn))
1626 (void)pop3_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
1628 /* Disconnect from the server */
1629 Curl_pp_disconnect(&pop3c->pp);
1631 /* Cleanup our connection based variables */
1632 curlx_safefree(pop3c->apoptimestamp);
1634 return CURLE_OK;
1635}
1637/* Called from multi.c while DOing */
1638static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done)
1639{
1640 CURLcode result = pop3_multi_statemach(data, dophase_done);
1642 if(result)
1643 DEBUGF(infof(data, "DO phase failed"));
1644 else if(*dophase_done) {
1645 result = pop3_dophase_done(data, FALSE /* not connected */);
1647 DEBUGF(infof(data, "DO phase is complete"));
1648 }
1650 return result;
1651}
1653static void pop3_easy_dtor(const void *key, size_t klen, void *entry)
1654{
1655 struct POP3 *pop3 = entry;
1656 (void)key;
1657 (void)klen;
1658 DEBUGASSERT(pop3);
1659 /* Cleanup our per-request based variables */
1660 curlx_safefree(pop3->id);
1661 curlx_safefree(pop3->custom);
1662 curlx_free(pop3);
1663}
1665static void pop3_conn_dtor(const void *key, size_t klen, void *entry)
1666{
1667 struct pop3_conn *pop3c = entry;
1668 (void)key;
1669 (void)klen;
1670 DEBUGASSERT(pop3c);
1671 Curl_pp_disconnect(&pop3c->pp);
1672 curlx_safefree(pop3c->apoptimestamp);
1673 curlx_free(pop3c);
1674}
1676static CURLcode pop3_setup_connection(struct Curl_easy *data,
1677 struct connectdata *conn)
1678{
1679 struct pop3_conn *pop3c;
1680 struct POP3 *pop3 = curlx_calloc(1, sizeof(*pop3));
1681 if(!pop3 ||
1682 Curl_meta_set(data, CURL_META_POP3_EASY, pop3, pop3_easy_dtor))
1683 return CURLE_OUT_OF_MEMORY;
1685 pop3c = curlx_calloc(1, sizeof(*pop3c));
1686 if(!pop3c ||
1687 Curl_conn_meta_set(conn, CURL_META_POP3_CONN, pop3c, pop3_conn_dtor))
1688 return CURLE_OUT_OF_MEMORY;
1690 return CURLE_OK;
1691}
1693/*
1694 * POP3 protocol.
1695 */
1696const struct Curl_protocol Curl_protocol_pop3 = {
1697 pop3_setup_connection, /* setup_connection */
1698 ZERO_NULL, /* setup_filters */
1699 pop3_do, /* do_it */
1700 pop3_done, /* done */
1701 ZERO_NULL, /* do_more */
1702 pop3_connect, /* connect_it */
1703 pop3_multi_statemach, /* connecting */
1704 pop3_doing, /* doing */
1705 pop3_pollset, /* proto_pollset */
1706 pop3_pollset, /* doing_pollset */
1707 ZERO_NULL, /* domore_pollset */
1708 ZERO_NULL, /* perform_pollset */
1709 pop3_disconnect, /* disconnect */
1710 pop3_write, /* write_resp */
1711 ZERO_NULL, /* write_resp_hd */
1712 ZERO_NULL, /* connection_is_dead */
1713 ZERO_NULL, /* attach connection */
1714 ZERO_NULL, /* follow */
1715};
1717#endif /* CURL_DISABLE_POP3 */
Diagnostics4 git calls · 338 ms
CallStatusSizeTime0338 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=20 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20013 KiB316 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=316 git.response.bytes=12952 git.response.cache=MISS git.response.duration_ms=316 git.fetch.wants=04affc406e6e8df5ae9669a400825240f012be80 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=12952 git.pkt.payload.max=12931 git.sideband.band1.bytes=12930 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2cea699feefad
- Request
- 6b3a78d8…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format