1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * RFC1734 POP3 Authentication
22 * RFC1939 POP3 protocol
23 * RFC2195 CRAM-MD5 authentication
24 * RFC2384 POP URL Scheme
25 * RFC2449 POP3 Extension Mechanism
26 * RFC2595 Using TLS with IMAP, POP3 and ACAP
27 * RFC2831 DIGEST-MD5 authentication
28 * RFC4422 Simple Authentication and Security Layer (SASL)
29 * RFC4616 PLAIN authentication
30 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
31 * RFC5034 POP3 SASL Authentication Mechanism
32 * RFC6749 OAuth 2.0 Authorization Framework
33 * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
35 ***************************************************************************/
37 #include "curl_setup.h"
39 #ifndef CURL_DISABLE_POP3
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
48 #include <sys/utsname.h>
58 #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
60 #define in_addr_t unsigned long
63 #include <curl/curl.h>
70 #include "http.h" /* for HTTP proxy tunnel stuff */
74 #include "strtoofft.h"
76 #include "vtls/vtls.h"
83 #include "curl_sasl.h"
86 #include "curl_printf.h"
87 #include "curl_memory.h"
88 /* The last #include file should be: */
91 /* Local API functions */
92 static CURLcode pop3_regular_transfer(struct connectdata *conn, bool *done);
93 static CURLcode pop3_do(struct connectdata *conn, bool *done);
94 static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
96 static CURLcode pop3_connect(struct connectdata *conn, bool *done);
97 static CURLcode pop3_disconnect(struct connectdata *conn, bool dead);
98 static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done);
99 static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
101 static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done);
102 static CURLcode pop3_setup_connection(struct connectdata *conn);
103 static CURLcode pop3_parse_url_options(struct connectdata *conn);
104 static CURLcode pop3_parse_url_path(struct connectdata *conn);
105 static CURLcode pop3_parse_custom_request(struct connectdata *conn);
106 static CURLcode pop3_perform_auth(struct connectdata *conn, const char *mech,
107 const char *initresp);
108 static CURLcode pop3_continue_auth(struct connectdata *conn, const char *resp);
109 static void pop3_get_message(char *buffer, char** outptr);
112 * POP3 protocol handler.
115 const struct Curl_handler Curl_handler_pop3 = {
117 pop3_setup_connection, /* setup_connection */
119 pop3_done, /* done */
120 ZERO_NULL, /* do_more */
121 pop3_connect, /* connect_it */
122 pop3_multi_statemach, /* connecting */
123 pop3_doing, /* doing */
124 pop3_getsock, /* proto_getsock */
125 pop3_getsock, /* doing_getsock */
126 ZERO_NULL, /* domore_getsock */
127 ZERO_NULL, /* perform_getsock */
128 pop3_disconnect, /* disconnect */
129 ZERO_NULL, /* readwrite */
130 PORT_POP3, /* defport */
131 CURLPROTO_POP3, /* protocol */
132 PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY /* flags */
137 * POP3S protocol handler.
140 const struct Curl_handler Curl_handler_pop3s = {
141 "POP3S", /* scheme */
142 pop3_setup_connection, /* setup_connection */
144 pop3_done, /* done */
145 ZERO_NULL, /* do_more */
146 pop3_connect, /* connect_it */
147 pop3_multi_statemach, /* connecting */
148 pop3_doing, /* doing */
149 pop3_getsock, /* proto_getsock */
150 pop3_getsock, /* doing_getsock */
151 ZERO_NULL, /* domore_getsock */
152 ZERO_NULL, /* perform_getsock */
153 pop3_disconnect, /* disconnect */
154 ZERO_NULL, /* readwrite */
155 PORT_POP3S, /* defport */
156 CURLPROTO_POP3S, /* protocol */
157 PROTOPT_CLOSEACTION | PROTOPT_SSL
158 | PROTOPT_NOURLQUERY /* flags */
162 #ifndef CURL_DISABLE_HTTP
164 * HTTP-proxyed POP3 protocol handler.
167 static const struct Curl_handler Curl_handler_pop3_proxy = {
169 Curl_http_setup_conn, /* setup_connection */
170 Curl_http, /* do_it */
171 Curl_http_done, /* done */
172 ZERO_NULL, /* do_more */
173 ZERO_NULL, /* connect_it */
174 ZERO_NULL, /* connecting */
175 ZERO_NULL, /* doing */
176 ZERO_NULL, /* proto_getsock */
177 ZERO_NULL, /* doing_getsock */
178 ZERO_NULL, /* domore_getsock */
179 ZERO_NULL, /* perform_getsock */
180 ZERO_NULL, /* disconnect */
181 ZERO_NULL, /* readwrite */
182 PORT_POP3, /* defport */
183 CURLPROTO_HTTP, /* protocol */
184 PROTOPT_NONE /* flags */
189 * HTTP-proxyed POP3S protocol handler.
192 static const struct Curl_handler Curl_handler_pop3s_proxy = {
193 "POP3S", /* scheme */
194 Curl_http_setup_conn, /* setup_connection */
195 Curl_http, /* do_it */
196 Curl_http_done, /* done */
197 ZERO_NULL, /* do_more */
198 ZERO_NULL, /* connect_it */
199 ZERO_NULL, /* connecting */
200 ZERO_NULL, /* doing */
201 ZERO_NULL, /* proto_getsock */
202 ZERO_NULL, /* doing_getsock */
203 ZERO_NULL, /* domore_getsock */
204 ZERO_NULL, /* perform_getsock */
205 ZERO_NULL, /* disconnect */
206 ZERO_NULL, /* readwrite */
207 PORT_POP3S, /* defport */
208 CURLPROTO_HTTP, /* protocol */
209 PROTOPT_NONE /* flags */
214 /* SASL parameters for the pop3 protocol */
215 static const struct SASLproto saslpop3 = {
216 "pop", /* The service name */
217 '+', /* Code received when continuation is expected */
218 '+', /* Code to receive upon authentication success */
219 255 - 8, /* Maximum initial response length (no max) */
220 pop3_perform_auth, /* Send authentication command */
221 pop3_continue_auth, /* Send authentication continuation */
222 pop3_get_message /* Get SASL response message */
226 static void pop3_to_pop3s(struct connectdata *conn)
228 conn->handler = &Curl_handler_pop3s;
231 #define pop3_to_pop3s(x) Curl_nop_stmt
234 /***********************************************************************
238 * Checks for an ending POP3 status code at the start of the given string, but
239 * also detects the APOP timestamp from the server greeting and various
240 * capabilities from the CAPA response including the supported authentication
241 * types and allowed SASL mechanisms.
243 static bool pop3_endofresp(struct connectdata *conn, char *line, size_t len,
246 struct pop3_conn *pop3c = &conn->proto.pop3c;
248 /* Do we have an error response? */
249 if(len >= 4 && !memcmp("-ERR", line, 4)) {
255 /* Are we processing CAPA command responses? */
256 if(pop3c->state == POP3_CAPA) {
257 /* Do we have the terminating line? */
258 if(len >= 1 && !memcmp(line, ".", 1))
266 /* Do we have a command or continuation response? */
267 if((len >= 3 && !memcmp("+OK", line, 3)) ||
268 (len >= 1 && !memcmp("+", line, 1))) {
274 return FALSE; /* Nothing for us */
277 /***********************************************************************
281 * Gets the authentication message from the response buffer.
283 static void pop3_get_message(char *buffer, char** outptr)
286 char* message = NULL;
288 /* Find the start of the message */
289 for(message = buffer + 2; *message == ' ' || *message == '\t'; message++)
292 /* Find the end of the message */
293 for(len = strlen(message); len--;)
294 if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
295 message[len] != '\t')
298 /* Terminate the message */
306 /***********************************************************************
310 * This is the ONLY way to change POP3 state!
312 static void state(struct connectdata *conn, pop3state newstate)
314 struct pop3_conn *pop3c = &conn->proto.pop3c;
315 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
316 /* for debug purposes */
317 static const char * const names[] = {
332 if(pop3c->state != newstate)
333 infof(conn->data, "POP3 %p state change from %s to %s\n",
334 (void *)pop3c, names[pop3c->state], names[newstate]);
337 pop3c->state = newstate;
340 /***********************************************************************
342 * pop3_perform_capa()
344 * Sends the CAPA command in order to obtain a list of server side supported
347 static CURLcode pop3_perform_capa(struct connectdata *conn)
349 CURLcode result = CURLE_OK;
350 struct pop3_conn *pop3c = &conn->proto.pop3c;
352 pop3c->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
353 pop3c->sasl.authused = SASL_AUTH_NONE; /* Clear the auth. mechanism used */
354 pop3c->tls_supported = FALSE; /* Clear the TLS capability */
356 /* Send the CAPA command */
357 result = Curl_pp_sendf(&pop3c->pp, "%s", "CAPA");
360 state(conn, POP3_CAPA);
365 /***********************************************************************
367 * pop3_perform_starttls()
369 * Sends the STLS command to start the upgrade to TLS.
371 static CURLcode pop3_perform_starttls(struct connectdata *conn)
373 CURLcode result = CURLE_OK;
375 /* Send the STLS command */
376 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", "STLS");
379 state(conn, POP3_STARTTLS);
384 /***********************************************************************
386 * pop3_perform_upgrade_tls()
388 * Performs the upgrade to TLS.
390 static CURLcode pop3_perform_upgrade_tls(struct connectdata *conn)
392 CURLcode result = CURLE_OK;
393 struct pop3_conn *pop3c = &conn->proto.pop3c;
395 /* Start the SSL connection */
396 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &pop3c->ssldone);
399 if(pop3c->state != POP3_UPGRADETLS)
400 state(conn, POP3_UPGRADETLS);
404 result = pop3_perform_capa(conn);
411 /***********************************************************************
413 * pop3_perform_user()
415 * Sends a clear text USER command to authenticate with.
417 static CURLcode pop3_perform_user(struct connectdata *conn)
419 CURLcode result = CURLE_OK;
421 /* Check we have a username and password to authenticate with and end the
422 connect phase if we don't */
423 if(!conn->bits.user_passwd) {
424 state(conn, POP3_STOP);
429 /* Send the USER command */
430 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "USER %s",
431 conn->user ? conn->user : "");
433 state(conn, POP3_USER);
438 #ifndef CURL_DISABLE_CRYPTO_AUTH
439 /***********************************************************************
441 * pop3_perform_apop()
443 * Sends an APOP command to authenticate with.
445 static CURLcode pop3_perform_apop(struct connectdata *conn)
447 CURLcode result = CURLE_OK;
448 struct pop3_conn *pop3c = &conn->proto.pop3c;
451 unsigned char digest[MD5_DIGEST_LEN];
452 char secret[2 * MD5_DIGEST_LEN + 1];
454 /* Check we have a username and password to authenticate with and end the
455 connect phase if we don't */
456 if(!conn->bits.user_passwd) {
457 state(conn, POP3_STOP);
462 /* Create the digest */
463 ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
465 return CURLE_OUT_OF_MEMORY;
467 Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp,
468 curlx_uztoui(strlen(pop3c->apoptimestamp)));
470 Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd,
471 curlx_uztoui(strlen(conn->passwd)));
473 /* Finalise the digest */
474 Curl_MD5_final(ctxt, digest);
476 /* Convert the calculated 16 octet digest into a 32 byte hex string */
477 for(i = 0; i < MD5_DIGEST_LEN; i++)
478 snprintf(&secret[2 * i], 3, "%02x", digest[i]);
480 result = Curl_pp_sendf(&pop3c->pp, "APOP %s %s", conn->user, secret);
483 state(conn, POP3_APOP);
489 /***********************************************************************
491 * pop3_perform_auth()
493 * Sends an AUTH command allowing the client to login with the given SASL
494 * authentication mechanism.
496 static CURLcode pop3_perform_auth(struct connectdata *conn,
498 const char *initresp)
500 CURLcode result = CURLE_OK;
501 struct pop3_conn *pop3c = &conn->proto.pop3c;
503 if(initresp) { /* AUTH <mech> ...<crlf> */
504 /* Send the AUTH command with the initial response */
505 result = Curl_pp_sendf(&pop3c->pp, "AUTH %s %s", mech, initresp);
508 /* Send the AUTH command */
509 result = Curl_pp_sendf(&pop3c->pp, "AUTH %s", mech);
515 /***********************************************************************
517 * pop3_continue_auth()
519 * Sends SASL continuation data or cancellation.
521 static CURLcode pop3_continue_auth(struct connectdata *conn,
524 struct pop3_conn *pop3c = &conn->proto.pop3c;
526 return Curl_pp_sendf(&pop3c->pp, "%s", resp);
529 /***********************************************************************
531 * pop3_perform_authentication()
533 * Initiates the authentication sequence, with the appropriate SASL
534 * authentication mechanism, falling back to APOP and clear text should a
535 * common mechanism not be available between the client and server.
537 static CURLcode pop3_perform_authentication(struct connectdata *conn)
539 CURLcode result = CURLE_OK;
540 struct pop3_conn *pop3c = &conn->proto.pop3c;
541 saslprogress progress = SASL_IDLE;
543 /* Check we have enough data to authenticate with and end the
544 connect phase if we don't */
545 if(!Curl_sasl_can_authenticate(&pop3c->sasl, conn)) {
546 state(conn, POP3_STOP);
550 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) {
551 /* Calculate the SASL login details */
552 result = Curl_sasl_start(&pop3c->sasl, conn, FALSE, &progress);
555 if(progress == SASL_INPROGRESS)
556 state(conn, POP3_AUTH);
559 if(!result && progress == SASL_IDLE) {
560 #ifndef CURL_DISABLE_CRYPTO_AUTH
561 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
562 /* Perform APOP authentication */
563 result = pop3_perform_apop(conn);
566 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
567 /* Perform clear text authentication */
568 result = pop3_perform_user(conn);
570 /* Other mechanisms not supported */
571 infof(conn->data, "No known authentication mechanisms supported!\n");
572 result = CURLE_LOGIN_DENIED;
579 /***********************************************************************
581 * pop3_perform_command()
583 * Sends a POP3 based command.
585 static CURLcode pop3_perform_command(struct connectdata *conn)
587 CURLcode result = CURLE_OK;
588 struct SessionHandle *data = conn->data;
589 struct POP3 *pop3 = data->req.protop;
590 const char *command = NULL;
592 /* Calculate the default command */
593 if(pop3->id[0] == '\0' || conn->data->set.ftp_list_only) {
596 if(pop3->id[0] != '\0')
597 /* Message specific LIST so skip the BODY transfer */
598 pop3->transfer = FTPTRANSFER_INFO;
603 /* Send the command */
604 if(pop3->id[0] != '\0')
605 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s %s",
606 (pop3->custom && pop3->custom[0] != '\0' ?
607 pop3->custom : command), pop3->id);
609 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s",
610 (pop3->custom && pop3->custom[0] != '\0' ?
611 pop3->custom : command));
614 state(conn, POP3_COMMAND);
619 /***********************************************************************
621 * pop3_perform_quit()
623 * Performs the quit action prior to sclose() be called.
625 static CURLcode pop3_perform_quit(struct connectdata *conn)
627 CURLcode result = CURLE_OK;
629 /* Send the QUIT command */
630 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", "QUIT");
633 state(conn, POP3_QUIT);
638 /* For the initial server greeting */
639 static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
643 CURLcode result = CURLE_OK;
644 struct SessionHandle *data = conn->data;
645 struct pop3_conn *pop3c = &conn->proto.pop3c;
646 const char *line = data->state.buffer;
647 size_t len = strlen(line);
650 (void)instate; /* no use for this yet */
652 if(pop3code != '+') {
653 failf(data, "Got unexpected pop3-server response");
654 result = CURLE_FTP_WEIRD_SERVER_REPLY;
657 /* Does the server support APOP authentication? */
658 if(len >= 4 && line[len - 2] == '>') {
659 /* Look for the APOP timestamp */
660 for(i = 3; i < len - 2; ++i) {
662 /* Calculate the length of the timestamp */
663 size_t timestamplen = len - 1 - i;
667 /* Allocate some memory for the timestamp */
668 pop3c->apoptimestamp = (char *)calloc(1, timestamplen + 1);
670 if(!pop3c->apoptimestamp)
673 /* Copy the timestamp */
674 memcpy(pop3c->apoptimestamp, line + i, timestamplen);
675 pop3c->apoptimestamp[timestamplen] = '\0';
677 /* Store the APOP capability */
678 pop3c->authtypes |= POP3_TYPE_APOP;
684 result = pop3_perform_capa(conn);
690 /* For CAPA responses */
691 static CURLcode pop3_state_capa_resp(struct connectdata *conn, int pop3code,
694 CURLcode result = CURLE_OK;
695 struct SessionHandle *data = conn->data;
696 struct pop3_conn *pop3c = &conn->proto.pop3c;
697 const char *line = data->state.buffer;
698 size_t len = strlen(line);
701 (void)instate; /* no use for this yet */
703 /* Do we have a untagged response? */
704 if(pop3code == '*') {
705 /* Does the server support the STLS capability? */
706 if(len >= 4 && !memcmp(line, "STLS", 4))
707 pop3c->tls_supported = TRUE;
709 /* Does the server support clear text authentication? */
710 else if(len >= 4 && !memcmp(line, "USER", 4))
711 pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
713 /* Does the server support SASL based authentication? */
714 else if(len >= 5 && !memcmp(line, "SASL ", 5)) {
715 pop3c->authtypes |= POP3_TYPE_SASL;
717 /* Advance past the SASL keyword */
721 /* Loop through the data line */
724 unsigned int mechbit;
727 (*line == ' ' || *line == '\t' ||
728 *line == '\r' || *line == '\n')) {
737 /* Extract the word */
738 for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
739 line[wordlen] != '\t' && line[wordlen] != '\r' &&
740 line[wordlen] != '\n';)
743 /* Test the word for a matching authentication mechanism */
744 if((mechbit = Curl_sasl_decode_mech(line, wordlen, &llen)) &&
746 pop3c->sasl.authmechs |= mechbit;
753 else if(pop3code == '+') {
754 if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
755 /* We don't have a SSL/TLS connection yet, but SSL is requested */
756 if(pop3c->tls_supported)
757 /* Switch to TLS connection now */
758 result = pop3_perform_starttls(conn);
759 else if(data->set.use_ssl == CURLUSESSL_TRY)
760 /* Fallback and carry on with authentication */
761 result = pop3_perform_authentication(conn);
763 failf(data, "STLS not supported.");
764 result = CURLE_USE_SSL_FAILED;
768 result = pop3_perform_authentication(conn);
771 /* Clear text is supported when CAPA isn't recognised */
772 pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
774 result = pop3_perform_authentication(conn);
780 /* For STARTTLS responses */
781 static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
785 CURLcode result = CURLE_OK;
786 struct SessionHandle *data = conn->data;
788 (void)instate; /* no use for this yet */
790 if(pop3code != '+') {
791 if(data->set.use_ssl != CURLUSESSL_TRY) {
792 failf(data, "STARTTLS denied. %c", pop3code);
793 result = CURLE_USE_SSL_FAILED;
796 result = pop3_perform_authentication(conn);
799 result = pop3_perform_upgrade_tls(conn);
804 /* For SASL authentication responses */
805 static CURLcode pop3_state_auth_resp(struct connectdata *conn,
809 CURLcode result = CURLE_OK;
810 struct SessionHandle *data = conn->data;
811 struct pop3_conn *pop3c = &conn->proto.pop3c;
812 saslprogress progress;
814 (void)instate; /* no use for this yet */
816 result = Curl_sasl_continue(&pop3c->sasl, conn, pop3code, &progress);
820 state(conn, POP3_STOP); /* Authenticated */
822 case SASL_IDLE: /* No mechanism left after cancellation */
823 #ifndef CURL_DISABLE_CRYPTO_AUTH
824 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
825 /* Perform APOP authentication */
826 result = pop3_perform_apop(conn);
829 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
830 /* Perform clear text authentication */
831 result = pop3_perform_user(conn);
833 failf(data, "Authentication cancelled");
834 result = CURLE_LOGIN_DENIED;
844 #ifndef CURL_DISABLE_CRYPTO_AUTH
845 /* For APOP responses */
846 static CURLcode pop3_state_apop_resp(struct connectdata *conn, int pop3code,
849 CURLcode result = CURLE_OK;
850 struct SessionHandle *data = conn->data;
852 (void)instate; /* no use for this yet */
854 if(pop3code != '+') {
855 failf(data, "Authentication failed: %d", pop3code);
856 result = CURLE_LOGIN_DENIED;
859 /* End of connect phase */
860 state(conn, POP3_STOP);
866 /* For USER responses */
867 static CURLcode pop3_state_user_resp(struct connectdata *conn, int pop3code,
870 CURLcode result = CURLE_OK;
871 struct SessionHandle *data = conn->data;
873 (void)instate; /* no use for this yet */
875 if(pop3code != '+') {
876 failf(data, "Access denied. %c", pop3code);
877 result = CURLE_LOGIN_DENIED;
880 /* Send the PASS command */
881 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "PASS %s",
882 conn->passwd ? conn->passwd : "");
884 state(conn, POP3_PASS);
889 /* For PASS responses */
890 static CURLcode pop3_state_pass_resp(struct connectdata *conn, int pop3code,
893 CURLcode result = CURLE_OK;
894 struct SessionHandle *data = conn->data;
896 (void)instate; /* no use for this yet */
898 if(pop3code != '+') {
899 failf(data, "Access denied. %c", pop3code);
900 result = CURLE_LOGIN_DENIED;
903 /* End of connect phase */
904 state(conn, POP3_STOP);
909 /* For command responses */
910 static CURLcode pop3_state_command_resp(struct connectdata *conn,
914 CURLcode result = CURLE_OK;
915 struct SessionHandle *data = conn->data;
916 struct POP3 *pop3 = data->req.protop;
917 struct pop3_conn *pop3c = &conn->proto.pop3c;
918 struct pingpong *pp = &pop3c->pp;
920 (void)instate; /* no use for this yet */
922 if(pop3code != '+') {
923 state(conn, POP3_STOP);
924 return CURLE_RECV_ERROR;
927 /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
928 EOB string so count this is two matching bytes. This is necessary to make
929 the code detect the EOB if the only data than comes now is %2e CR LF like
930 when there is no body to return. */
933 /* But since this initial CR LF pair is not part of the actual body, we set
934 the strip counter here so that these bytes won't be delivered. */
937 if(pop3->transfer == FTPTRANSFER_BODY) {
939 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
942 /* The header "cache" contains a bunch of data that is actually body
943 content so send it as such. Note that there may even be additional
944 "headers" after the body */
946 if(!data->set.opt_no_body) {
947 result = Curl_pop3_write(conn, pp->cache, pp->cache_size);
953 Curl_safefree(pp->cache);
955 /* Reset the cache size */
960 /* End of DO phase */
961 state(conn, POP3_STOP);
966 static CURLcode pop3_statemach_act(struct connectdata *conn)
968 CURLcode result = CURLE_OK;
969 curl_socket_t sock = conn->sock[FIRSTSOCKET];
971 struct pop3_conn *pop3c = &conn->proto.pop3c;
972 struct pingpong *pp = &pop3c->pp;
975 /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
976 if(pop3c->state == POP3_UPGRADETLS)
977 return pop3_perform_upgrade_tls(conn);
979 /* Flush any data that needs to be sent */
981 return Curl_pp_flushsend(pp);
984 /* Read the response from the server */
985 result = Curl_pp_readresp(sock, pp, &pop3code, &nread);
992 /* We have now received a full POP3 server response */
993 switch(pop3c->state) {
994 case POP3_SERVERGREET:
995 result = pop3_state_servergreet_resp(conn, pop3code, pop3c->state);
999 result = pop3_state_capa_resp(conn, pop3code, pop3c->state);
1003 result = pop3_state_starttls_resp(conn, pop3code, pop3c->state);
1007 result = pop3_state_auth_resp(conn, pop3code, pop3c->state);
1010 #ifndef CURL_DISABLE_CRYPTO_AUTH
1012 result = pop3_state_apop_resp(conn, pop3code, pop3c->state);
1017 result = pop3_state_user_resp(conn, pop3code, pop3c->state);
1021 result = pop3_state_pass_resp(conn, pop3code, pop3c->state);
1025 result = pop3_state_command_resp(conn, pop3code, pop3c->state);
1029 /* fallthrough, just stop! */
1031 /* internal error */
1032 state(conn, POP3_STOP);
1035 } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
1040 /* Called repeatedly until done from multi.c */
1041 static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done)
1043 CURLcode result = CURLE_OK;
1044 struct pop3_conn *pop3c = &conn->proto.pop3c;
1046 if((conn->handler->flags & PROTOPT_SSL) && !pop3c->ssldone) {
1047 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &pop3c->ssldone);
1048 if(result || !pop3c->ssldone)
1052 result = Curl_pp_statemach(&pop3c->pp, FALSE);
1053 *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
1058 static CURLcode pop3_block_statemach(struct connectdata *conn)
1060 CURLcode result = CURLE_OK;
1061 struct pop3_conn *pop3c = &conn->proto.pop3c;
1063 while(pop3c->state != POP3_STOP && !result)
1064 result = Curl_pp_statemach(&pop3c->pp, TRUE);
1069 /* Allocate and initialize the POP3 struct for the current SessionHandle if
1071 static CURLcode pop3_init(struct connectdata *conn)
1073 CURLcode result = CURLE_OK;
1074 struct SessionHandle *data = conn->data;
1077 pop3 = data->req.protop = calloc(sizeof(struct POP3), 1);
1079 result = CURLE_OUT_OF_MEMORY;
1084 /* For the POP3 "protocol connect" and "doing" phases only */
1085 static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
1088 return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks);
1091 /***********************************************************************
1095 * This function should do everything that is to be considered a part of the
1098 * The variable 'done' points to will be TRUE if the protocol-layer connect
1099 * phase is done when this function returns, or FALSE if not.
1101 static CURLcode pop3_connect(struct connectdata *conn, bool *done)
1103 CURLcode result = CURLE_OK;
1104 struct pop3_conn *pop3c = &conn->proto.pop3c;
1105 struct pingpong *pp = &pop3c->pp;
1107 *done = FALSE; /* default to not done yet */
1109 /* We always support persistent connections in POP3 */
1110 connkeep(conn, "POP3 default");
1112 /* Set the default response time-out */
1113 pp->response_time = RESP_TIMEOUT;
1114 pp->statemach_act = pop3_statemach_act;
1115 pp->endofresp = pop3_endofresp;
1118 /* Set the default preferred authentication type and mechanism */
1119 pop3c->preftype = POP3_TYPE_ANY;
1120 Curl_sasl_init(&pop3c->sasl, &saslpop3);
1122 /* Initialise the pingpong layer */
1125 /* Parse the URL options */
1126 result = pop3_parse_url_options(conn);
1130 /* Start off waiting for the server greeting response */
1131 state(conn, POP3_SERVERGREET);
1133 result = pop3_multi_statemach(conn, done);
1138 /***********************************************************************
1142 * The DONE function. This does what needs to be done after a single DO has
1145 * Input argument is already checked for validity.
1147 static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
1150 CURLcode result = CURLE_OK;
1151 struct SessionHandle *data = conn->data;
1152 struct POP3 *pop3 = data->req.protop;
1157 /* When the easy handle is removed from the multi interface while libcurl
1158 is still trying to resolve the host name, the POP3 struct is not yet
1159 initialized. However, the removal action calls Curl_done() which in
1160 turn calls this function, so we simply return success. */
1164 connclose(conn, "POP3 done with bad status");
1165 result = status; /* use the already set error code */
1168 /* Cleanup our per-request based variables */
1169 Curl_safefree(pop3->id);
1170 Curl_safefree(pop3->custom);
1172 /* Clear the transfer mode for the next request */
1173 pop3->transfer = FTPTRANSFER_BODY;
1178 /***********************************************************************
1182 * This is the actual DO function for POP3. Get a message/listing according to
1183 * the options previously setup.
1185 static CURLcode pop3_perform(struct connectdata *conn, bool *connected,
1188 /* This is POP3 and no proxy */
1189 CURLcode result = CURLE_OK;
1190 struct POP3 *pop3 = conn->data->req.protop;
1192 DEBUGF(infof(conn->data, "DO phase starts\n"));
1194 if(conn->data->set.opt_no_body) {
1195 /* Requested no body means no transfer */
1196 pop3->transfer = FTPTRANSFER_INFO;
1199 *dophase_done = FALSE; /* not done yet */
1201 /* Start the first command in the DO phase */
1202 result = pop3_perform_command(conn);
1206 /* Run the state-machine */
1207 result = pop3_multi_statemach(conn, dophase_done);
1209 *connected = conn->bits.tcpconnect[FIRSTSOCKET];
1212 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1217 /***********************************************************************
1221 * This function is registered as 'curl_do' function. It decodes the path
1222 * parts etc as a wrapper to the actual DO function (pop3_perform).
1224 * The input argument is already checked for validity.
1226 static CURLcode pop3_do(struct connectdata *conn, bool *done)
1228 CURLcode result = CURLE_OK;
1230 *done = FALSE; /* default to false */
1232 /* Parse the URL path */
1233 result = pop3_parse_url_path(conn);
1237 /* Parse the custom request */
1238 result = pop3_parse_custom_request(conn);
1242 result = pop3_regular_transfer(conn, done);
1247 /***********************************************************************
1251 * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
1252 * resources. BLOCKING.
1254 static CURLcode pop3_disconnect(struct connectdata *conn, bool dead_connection)
1256 struct pop3_conn *pop3c = &conn->proto.pop3c;
1258 /* We cannot send quit unconditionally. If this connection is stale or
1259 bad in any way, sending quit and waiting around here will make the
1260 disconnect wait in vain and cause more problems than we need to. */
1262 /* The POP3 session may or may not have been allocated/setup at this
1264 if(!dead_connection && pop3c->pp.conn && pop3c->pp.conn->bits.protoconnstart)
1265 if(!pop3_perform_quit(conn))
1266 (void)pop3_block_statemach(conn); /* ignore errors on QUIT */
1268 /* Disconnect from the server */
1269 Curl_pp_disconnect(&pop3c->pp);
1271 /* Cleanup the SASL module */
1272 Curl_sasl_cleanup(conn, pop3c->sasl.authused);
1274 /* Cleanup our connection based variables */
1275 Curl_safefree(pop3c->apoptimestamp);
1280 /* Call this when the DO phase has completed */
1281 static CURLcode pop3_dophase_done(struct connectdata *conn, bool connected)
1289 /* Called from multi.c while DOing */
1290 static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done)
1292 CURLcode result = pop3_multi_statemach(conn, dophase_done);
1295 DEBUGF(infof(conn->data, "DO phase failed\n"));
1296 else if(*dophase_done) {
1297 result = pop3_dophase_done(conn, FALSE /* not connected */);
1299 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1305 /***********************************************************************
1307 * pop3_regular_transfer()
1309 * The input argument is already checked for validity.
1311 * Performs all commands done before a regular transfer between a local and a
1314 static CURLcode pop3_regular_transfer(struct connectdata *conn,
1317 CURLcode result = CURLE_OK;
1318 bool connected = FALSE;
1319 struct SessionHandle *data = conn->data;
1321 /* Make sure size is unknown at this point */
1322 data->req.size = -1;
1324 /* Set the progress data */
1325 Curl_pgrsSetUploadCounter(data, 0);
1326 Curl_pgrsSetDownloadCounter(data, 0);
1327 Curl_pgrsSetUploadSize(data, -1);
1328 Curl_pgrsSetDownloadSize(data, -1);
1330 /* Carry out the perform */
1331 result = pop3_perform(conn, &connected, dophase_done);
1333 /* Perform post DO phase operations if necessary */
1334 if(!result && *dophase_done)
1335 result = pop3_dophase_done(conn, connected);
1340 static CURLcode pop3_setup_connection(struct connectdata *conn)
1342 struct SessionHandle *data = conn->data;
1344 /* Initialise the POP3 layer */
1345 CURLcode result = pop3_init(conn);
1349 if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
1350 /* Unless we have asked to tunnel POP3 operations through the proxy, we
1351 switch and use HTTP operations only */
1352 #ifndef CURL_DISABLE_HTTP
1353 if(conn->handler == &Curl_handler_pop3)
1354 conn->handler = &Curl_handler_pop3_proxy;
1357 conn->handler = &Curl_handler_pop3s_proxy;
1359 failf(data, "POP3S not supported!");
1360 return CURLE_UNSUPPORTED_PROTOCOL;
1364 /* set it up as an HTTP connection instead */
1365 return conn->handler->setup_connection(conn);
1367 failf(data, "POP3 over http proxy requires HTTP support built-in!");
1368 return CURLE_UNSUPPORTED_PROTOCOL;
1372 data->state.path++; /* don't include the initial slash */
1377 /***********************************************************************
1379 * pop3_parse_url_options()
1381 * Parse the URL login options.
1383 static CURLcode pop3_parse_url_options(struct connectdata *conn)
1385 CURLcode result = CURLE_OK;
1386 struct pop3_conn *pop3c = &conn->proto.pop3c;
1387 const char *ptr = conn->options;
1389 pop3c->sasl.resetprefs = TRUE;
1391 while(!result && ptr && *ptr) {
1392 const char *key = ptr;
1395 while(*ptr && *ptr != '=')
1400 while(*ptr && *ptr != ';')
1403 if(strnequal(key, "AUTH=", 5)) {
1404 result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
1405 value, ptr - value);
1407 if(result && strnequal(value, "+APOP", ptr - value)) {
1408 pop3c->preftype = POP3_TYPE_APOP;
1409 pop3c->sasl.prefmech = SASL_AUTH_NONE;
1414 result = CURLE_URL_MALFORMAT;
1420 if(pop3c->preftype != POP3_TYPE_APOP)
1421 switch(pop3c->sasl.prefmech) {
1422 case SASL_AUTH_NONE:
1423 pop3c->preftype = POP3_TYPE_NONE;
1425 case SASL_AUTH_DEFAULT:
1426 pop3c->preftype = POP3_TYPE_ANY;
1429 pop3c->preftype = POP3_TYPE_SASL;
1436 /***********************************************************************
1438 * pop3_parse_url_path()
1440 * Parse the URL path into separate path components.
1442 static CURLcode pop3_parse_url_path(struct connectdata *conn)
1444 /* The POP3 struct is already initialised in pop3_connect() */
1445 struct SessionHandle *data = conn->data;
1446 struct POP3 *pop3 = data->req.protop;
1447 const char *path = data->state.path;
1449 /* URL decode the path for the message ID */
1450 return Curl_urldecode(data, path, 0, &pop3->id, NULL, TRUE);
1453 /***********************************************************************
1455 * pop3_parse_custom_request()
1457 * Parse the custom request.
1459 static CURLcode pop3_parse_custom_request(struct connectdata *conn)
1461 CURLcode result = CURLE_OK;
1462 struct SessionHandle *data = conn->data;
1463 struct POP3 *pop3 = data->req.protop;
1464 const char *custom = data->set.str[STRING_CUSTOMREQUEST];
1466 /* URL decode the custom request */
1468 result = Curl_urldecode(data, custom, 0, &pop3->custom, NULL, TRUE);
1473 /***********************************************************************
1477 * This function scans the body after the end-of-body and writes everything
1478 * until the end is found.
1480 CURLcode Curl_pop3_write(struct connectdata *conn, char *str, size_t nread)
1482 /* This code could be made into a special function in the handler struct */
1483 CURLcode result = CURLE_OK;
1484 struct SessionHandle *data = conn->data;
1485 struct SingleRequest *k = &data->req;
1487 struct pop3_conn *pop3c = &conn->proto.pop3c;
1488 bool strip_dot = FALSE;
1492 /* Search through the buffer looking for the end-of-body marker which is
1493 5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
1494 the eob so the server will have prefixed it with an extra dot which we
1495 need to strip out. Additionally the marker could of course be spread out
1496 over 5 different data chunks. */
1497 for(i = 0; i < nread; i++) {
1498 size_t prev = pop3c->eob;
1502 if(pop3c->eob == 0) {
1506 /* Write out the body part that didn't match */
1507 result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
1516 else if(pop3c->eob == 3)
1519 /* If the character match wasn't at position 0 or 3 then restart the
1525 if(pop3c->eob == 1 || pop3c->eob == 4)
1528 /* If the character match wasn't at position 1 or 4 then start the
1536 else if(pop3c->eob == 3) {
1537 /* We have an extra dot after the CRLF which we need to strip off */
1542 /* If the character match wasn't at position 2 then start the search
1552 /* Did we have a partial match which has subsequently failed? */
1553 if(prev && prev >= pop3c->eob) {
1554 /* Strip can only be non-zero for the very first mismatch after CRLF
1555 and then both prev and strip are equal and nothing will be output
1557 while(prev && pop3c->strip) {
1563 /* If the partial match was the CRLF and dot then only write the CRLF
1564 as the server would have inserted the dot */
1565 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char*)POP3_EOB,
1566 strip_dot ? prev - 1 : prev);
1577 if(pop3c->eob == POP3_EOB_LEN) {
1578 /* We have a full match so the transfer is done, however we must transfer
1579 the CRLF at the start of the EOB as this is considered to be part of the
1580 message as per RFC-1939, sect. 3 */
1581 result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)POP3_EOB, 2);
1583 k->keepon &= ~KEEP_RECV;
1590 /* While EOB is matching nothing should be output */
1594 result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
1601 #endif /* CURL_DISABLE_POP3 */