blob: 53510a21044e49595e71c2cc1b26c8310e7b6c8a [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07008 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
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.
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 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070021 * RFC1734 POP3 Authentication
Kristian Monsen5ab50182010-05-14 18:53:44 +010022 * RFC1939 POP3 protocol
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023 * RFC2195 CRAM-MD5 authentication
Kristian Monsen5ab50182010-05-14 18:53:44 +010024 * RFC2384 POP URL Scheme
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070025 * RFC2449 POP3 Extension Mechanism
Kristian Monsen5ab50182010-05-14 18:53:44 +010026 * RFC2595 Using TLS with IMAP, POP3 and ACAP
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070027 * 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>
Kristian Monsen5ab50182010-05-14 18:53:44 +010034 *
35 ***************************************************************************/
36
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070037#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010038
39#ifndef CURL_DISABLE_POP3
Kristian Monsen5ab50182010-05-14 18:53:44 +010040
Kristian Monsen5ab50182010-05-14 18:53:44 +010041#ifdef HAVE_NETINET_IN_H
42#include <netinet/in.h>
43#endif
44#ifdef HAVE_ARPA_INET_H
45#include <arpa/inet.h>
46#endif
47#ifdef HAVE_UTSNAME_H
48#include <sys/utsname.h>
49#endif
50#ifdef HAVE_NETDB_H
51#include <netdb.h>
52#endif
53#ifdef __VMS
54#include <in.h>
55#include <inet.h>
56#endif
57
58#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
59#undef in_addr_t
60#define in_addr_t unsigned long
61#endif
62
63#include <curl/curl.h>
64#include "urldata.h"
65#include "sendf.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010066#include "hostip.h"
67#include "progress.h"
68#include "transfer.h"
69#include "escape.h"
70#include "http.h" /* for HTTP proxy tunnel stuff */
71#include "socks.h"
72#include "pop3.h"
73
74#include "strtoofft.h"
75#include "strequal.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070076#include "vtls/vtls.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010077#include "connect.h"
78#include "strerror.h"
79#include "select.h"
80#include "multiif.h"
81#include "url.h"
82#include "rawstr.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070083#include "curl_sasl.h"
84#include "curl_md5.h"
85#include "warnless.h"
86#include "curl_printf.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010087#include "curl_memory.h"
88/* The last #include file should be: */
89#include "memdebug.h"
90
91/* Local API functions */
Kristian Monsen5ab50182010-05-14 18:53:44 +010092static CURLcode pop3_regular_transfer(struct connectdata *conn, bool *done);
93static CURLcode pop3_do(struct connectdata *conn, bool *done);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070094static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
95 bool premature);
Kristian Monsen5ab50182010-05-14 18:53:44 +010096static CURLcode pop3_connect(struct connectdata *conn, bool *done);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070097static CURLcode pop3_disconnect(struct connectdata *conn, bool dead);
Kristian Monsen5ab50182010-05-14 18:53:44 +010098static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070099static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100100 int numsocks);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700101static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done);
102static CURLcode pop3_setup_connection(struct connectdata *conn);
103static CURLcode pop3_parse_url_options(struct connectdata *conn);
104static CURLcode pop3_parse_url_path(struct connectdata *conn);
105static CURLcode pop3_parse_custom_request(struct connectdata *conn);
106static CURLcode pop3_perform_auth(struct connectdata *conn, const char *mech,
107 const char *initresp);
108static CURLcode pop3_continue_auth(struct connectdata *conn, const char *resp);
109static void pop3_get_message(char *buffer, char** outptr);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100110
111/*
112 * POP3 protocol handler.
113 */
114
115const struct Curl_handler Curl_handler_pop3 = {
116 "POP3", /* scheme */
117 pop3_setup_connection, /* setup_connection */
118 pop3_do, /* do_it */
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 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700126 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100127 ZERO_NULL, /* perform_getsock */
128 pop3_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700129 ZERO_NULL, /* readwrite */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100130 PORT_POP3, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700131 CURLPROTO_POP3, /* protocol */
132 PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100133};
134
Kristian Monsen5ab50182010-05-14 18:53:44 +0100135#ifdef USE_SSL
136/*
137 * POP3S protocol handler.
138 */
139
140const struct Curl_handler Curl_handler_pop3s = {
141 "POP3S", /* scheme */
142 pop3_setup_connection, /* setup_connection */
143 pop3_do, /* do_it */
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 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700151 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100152 ZERO_NULL, /* perform_getsock */
153 pop3_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700154 ZERO_NULL, /* readwrite */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100155 PORT_POP3S, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700156 CURLPROTO_POP3S, /* protocol */
157 PROTOPT_CLOSEACTION | PROTOPT_SSL
158 | PROTOPT_NOURLQUERY /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100159};
160#endif
161
162#ifndef CURL_DISABLE_HTTP
163/*
164 * HTTP-proxyed POP3 protocol handler.
165 */
166
167static const struct Curl_handler Curl_handler_pop3_proxy = {
168 "POP3", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700169 Curl_http_setup_conn, /* setup_connection */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100170 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 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700178 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100179 ZERO_NULL, /* perform_getsock */
180 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700181 ZERO_NULL, /* readwrite */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100182 PORT_POP3, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700183 CURLPROTO_HTTP, /* protocol */
184 PROTOPT_NONE /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100185};
186
Kristian Monsen5ab50182010-05-14 18:53:44 +0100187#ifdef USE_SSL
188/*
189 * HTTP-proxyed POP3S protocol handler.
190 */
191
192static const struct Curl_handler Curl_handler_pop3s_proxy = {
193 "POP3S", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700194 Curl_http_setup_conn, /* setup_connection */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100195 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 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700203 ZERO_NULL, /* domore_getsock */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100204 ZERO_NULL, /* perform_getsock */
205 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700206 ZERO_NULL, /* readwrite */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100207 PORT_POP3S, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700208 CURLPROTO_HTTP, /* protocol */
209 PROTOPT_NONE /* flags */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100210};
211#endif
212#endif
213
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700214/* SASL parameters for the pop3 protocol */
215static 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 */
223};
Kristian Monsen5ab50182010-05-14 18:53:44 +0100224
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700225#ifdef USE_SSL
226static void pop3_to_pop3s(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100227{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700228 conn->handler = &Curl_handler_pop3s;
229}
230#else
231#define pop3_to_pop3s(x) Curl_nop_stmt
232#endif
Kristian Monsen5ab50182010-05-14 18:53:44 +0100233
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700234/***********************************************************************
235 *
236 * pop3_endofresp()
237 *
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.
242 */
243static bool pop3_endofresp(struct connectdata *conn, char *line, size_t len,
244 int *resp)
245{
246 struct pop3_conn *pop3c = &conn->proto.pop3c;
247
248 /* Do we have an error response? */
249 if(len >= 4 && !memcmp("-ERR", line, 4)) {
250 *resp = '-';
251
Kristian Monsen5ab50182010-05-14 18:53:44 +0100252 return TRUE;
253 }
254
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700255 /* 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))
259 *resp = '+';
260 else
261 *resp = '*';
262
263 return TRUE;
264 }
265
266 /* Do we have a command or continuation response? */
267 if((len >= 3 && !memcmp("+OK", line, 3)) ||
268 (len >= 1 && !memcmp("+", line, 1))) {
269 *resp = '+';
270
271 return TRUE;
272 }
273
274 return FALSE; /* Nothing for us */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100275}
276
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700277/***********************************************************************
278 *
279 * pop3_get_message()
280 *
281 * Gets the authentication message from the response buffer.
282 */
283static void pop3_get_message(char *buffer, char** outptr)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100284{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700285 size_t len = 0;
286 char* message = NULL;
287
288 /* Find the start of the message */
289 for(message = buffer + 2; *message == ' ' || *message == '\t'; message++)
290 ;
291
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')
296 break;
297
298 /* Terminate the message */
299 if(++len) {
300 message[len] = '\0';
301 }
302
303 *outptr = message;
304}
305
306/***********************************************************************
307 *
308 * state()
309 *
310 * This is the ONLY way to change POP3 state!
311 */
312static void state(struct connectdata *conn, pop3state newstate)
313{
314 struct pop3_conn *pop3c = &conn->proto.pop3c;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100315#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
316 /* for debug purposes */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700317 static const char * const names[] = {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100318 "STOP",
319 "SERVERGREET",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700320 "CAPA",
321 "STARTTLS",
322 "UPGRADETLS",
323 "AUTH",
324 "APOP",
Kristian Monsen5ab50182010-05-14 18:53:44 +0100325 "USER",
326 "PASS",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700327 "COMMAND",
Kristian Monsen5ab50182010-05-14 18:53:44 +0100328 "QUIT",
329 /* LAST */
330 };
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700331
Kristian Monsen5ab50182010-05-14 18:53:44 +0100332 if(pop3c->state != newstate)
333 infof(conn->data, "POP3 %p state change from %s to %s\n",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700334 (void *)pop3c, names[pop3c->state], names[newstate]);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100335#endif
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700336
Kristian Monsen5ab50182010-05-14 18:53:44 +0100337 pop3c->state = newstate;
338}
339
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700340/***********************************************************************
341 *
342 * pop3_perform_capa()
343 *
344 * Sends the CAPA command in order to obtain a list of server side supported
345 * capabilities.
346 */
347static CURLcode pop3_perform_capa(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100348{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700349 CURLcode result = CURLE_OK;
350 struct pop3_conn *pop3c = &conn->proto.pop3c;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100351
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700352 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 */
355
356 /* Send the CAPA command */
357 result = Curl_pp_sendf(&pop3c->pp, "%s", "CAPA");
358
359 if(!result)
360 state(conn, POP3_CAPA);
361
362 return result;
363}
364
365/***********************************************************************
366 *
367 * pop3_perform_starttls()
368 *
369 * Sends the STLS command to start the upgrade to TLS.
370 */
371static CURLcode pop3_perform_starttls(struct connectdata *conn)
372{
373 CURLcode result = CURLE_OK;
374
375 /* Send the STLS command */
376 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", "STLS");
377
378 if(!result)
379 state(conn, POP3_STARTTLS);
380
381 return result;
382}
383
384/***********************************************************************
385 *
386 * pop3_perform_upgrade_tls()
387 *
388 * Performs the upgrade to TLS.
389 */
390static CURLcode pop3_perform_upgrade_tls(struct connectdata *conn)
391{
392 CURLcode result = CURLE_OK;
393 struct pop3_conn *pop3c = &conn->proto.pop3c;
394
395 /* Start the SSL connection */
396 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &pop3c->ssldone);
397
398 if(!result) {
399 if(pop3c->state != POP3_UPGRADETLS)
400 state(conn, POP3_UPGRADETLS);
401
402 if(pop3c->ssldone) {
403 pop3_to_pop3s(conn);
404 result = pop3_perform_capa(conn);
405 }
406 }
407
408 return result;
409}
410
411/***********************************************************************
412 *
413 * pop3_perform_user()
414 *
415 * Sends a clear text USER command to authenticate with.
416 */
417static CURLcode pop3_perform_user(struct connectdata *conn)
418{
419 CURLcode result = CURLE_OK;
420
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);
425
Kristian Monsen5ab50182010-05-14 18:53:44 +0100426 return result;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700427 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100428
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700429 /* Send the USER command */
430 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "USER %s",
431 conn->user ? conn->user : "");
432 if(!result)
433 state(conn, POP3_USER);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100434
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700435 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100436}
437
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700438#ifndef CURL_DISABLE_CRYPTO_AUTH
439/***********************************************************************
440 *
441 * pop3_perform_apop()
442 *
443 * Sends an APOP command to authenticate with.
444 */
445static CURLcode pop3_perform_apop(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100446{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700447 CURLcode result = CURLE_OK;
448 struct pop3_conn *pop3c = &conn->proto.pop3c;
449 size_t i;
450 MD5_context *ctxt;
451 unsigned char digest[MD5_DIGEST_LEN];
452 char secret[2 * MD5_DIGEST_LEN + 1];
453
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);
458
459 return result;
460 }
461
462 /* Create the digest */
463 ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
464 if(!ctxt)
465 return CURLE_OUT_OF_MEMORY;
466
467 Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp,
468 curlx_uztoui(strlen(pop3c->apoptimestamp)));
469
470 Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd,
471 curlx_uztoui(strlen(conn->passwd)));
472
473 /* Finalise the digest */
474 Curl_MD5_final(ctxt, digest);
475
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]);
479
480 result = Curl_pp_sendf(&pop3c->pp, "APOP %s %s", conn->user, secret);
481
482 if(!result)
483 state(conn, POP3_APOP);
484
485 return result;
486}
487#endif
488
489/***********************************************************************
490 *
491 * pop3_perform_auth()
492 *
493 * Sends an AUTH command allowing the client to login with the given SASL
494 * authentication mechanism.
495 */
496static CURLcode pop3_perform_auth(struct connectdata *conn,
497 const char *mech,
498 const char *initresp)
499{
500 CURLcode result = CURLE_OK;
501 struct pop3_conn *pop3c = &conn->proto.pop3c;
502
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);
506 }
507 else {
508 /* Send the AUTH command */
509 result = Curl_pp_sendf(&pop3c->pp, "AUTH %s", mech);
510 }
511
512 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100513}
514
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700515/***********************************************************************
516 *
517 * pop3_continue_auth()
518 *
519 * Sends SASL continuation data or cancellation.
520 */
521static CURLcode pop3_continue_auth(struct connectdata *conn,
522 const char *resp)
523{
524 struct pop3_conn *pop3c = &conn->proto.pop3c;
525
526 return Curl_pp_sendf(&pop3c->pp, "%s", resp);
527}
528
529/***********************************************************************
530 *
531 * pop3_perform_authentication()
532 *
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.
536 */
537static CURLcode pop3_perform_authentication(struct connectdata *conn)
538{
539 CURLcode result = CURLE_OK;
540 struct pop3_conn *pop3c = &conn->proto.pop3c;
541 saslprogress progress = SASL_IDLE;
542
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);
547 return result;
548 }
549
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);
553
554 if(!result)
555 if(progress == SASL_INPROGRESS)
556 state(conn, POP3_AUTH);
557 }
558
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);
564 else
565#endif
566 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
567 /* Perform clear text authentication */
568 result = pop3_perform_user(conn);
569 else {
570 /* Other mechanisms not supported */
571 infof(conn->data, "No known authentication mechanisms supported!\n");
572 result = CURLE_LOGIN_DENIED;
573 }
574 }
575
576 return result;
577}
578
579/***********************************************************************
580 *
581 * pop3_perform_command()
582 *
583 * Sends a POP3 based command.
584 */
585static CURLcode pop3_perform_command(struct connectdata *conn)
586{
587 CURLcode result = CURLE_OK;
588 struct SessionHandle *data = conn->data;
589 struct POP3 *pop3 = data->req.protop;
590 const char *command = NULL;
591
592 /* Calculate the default command */
593 if(pop3->id[0] == '\0' || conn->data->set.ftp_list_only) {
594 command = "LIST";
595
596 if(pop3->id[0] != '\0')
597 /* Message specific LIST so skip the BODY transfer */
598 pop3->transfer = FTPTRANSFER_INFO;
599 }
600 else
601 command = "RETR";
602
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);
608 else
609 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s",
610 (pop3->custom && pop3->custom[0] != '\0' ?
611 pop3->custom : command));
612
613 if(!result)
614 state(conn, POP3_COMMAND);
615
616 return result;
617}
618
619/***********************************************************************
620 *
621 * pop3_perform_quit()
622 *
623 * Performs the quit action prior to sclose() be called.
624 */
625static CURLcode pop3_perform_quit(struct connectdata *conn)
626{
627 CURLcode result = CURLE_OK;
628
629 /* Send the QUIT command */
630 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", "QUIT");
631
632 if(!result)
633 state(conn, POP3_QUIT);
634
635 return result;
636}
637
638/* For the initial server greeting */
639static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
640 int pop3code,
641 pop3state instate)
642{
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);
648 size_t i;
649
650 (void)instate; /* no use for this yet */
651
652 if(pop3code != '+') {
653 failf(data, "Got unexpected pop3-server response");
654 result = CURLE_FTP_WEIRD_SERVER_REPLY;
655 }
656 else {
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) {
661 if(line[i] == '<') {
662 /* Calculate the length of the timestamp */
663 size_t timestamplen = len - 1 - i;
664 if(!timestamplen)
665 break;
666
667 /* Allocate some memory for the timestamp */
668 pop3c->apoptimestamp = (char *)calloc(1, timestamplen + 1);
669
670 if(!pop3c->apoptimestamp)
671 break;
672
673 /* Copy the timestamp */
674 memcpy(pop3c->apoptimestamp, line + i, timestamplen);
675 pop3c->apoptimestamp[timestamplen] = '\0';
676
677 /* Store the APOP capability */
678 pop3c->authtypes |= POP3_TYPE_APOP;
679 break;
680 }
681 }
682 }
683
684 result = pop3_perform_capa(conn);
685 }
686
687 return result;
688}
689
690/* For CAPA responses */
691static CURLcode pop3_state_capa_resp(struct connectdata *conn, int pop3code,
692 pop3state instate)
693{
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);
699 size_t wordlen;
700
701 (void)instate; /* no use for this yet */
702
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;
708
709 /* Does the server support clear text authentication? */
710 else if(len >= 4 && !memcmp(line, "USER", 4))
711 pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
712
713 /* Does the server support SASL based authentication? */
714 else if(len >= 5 && !memcmp(line, "SASL ", 5)) {
715 pop3c->authtypes |= POP3_TYPE_SASL;
716
717 /* Advance past the SASL keyword */
718 line += 5;
719 len -= 5;
720
721 /* Loop through the data line */
722 for(;;) {
723 size_t llen;
724 unsigned int mechbit;
725
726 while(len &&
727 (*line == ' ' || *line == '\t' ||
728 *line == '\r' || *line == '\n')) {
729
730 line++;
731 len--;
732 }
733
734 if(!len)
735 break;
736
737 /* Extract the word */
738 for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
739 line[wordlen] != '\t' && line[wordlen] != '\r' &&
740 line[wordlen] != '\n';)
741 wordlen++;
742
743 /* Test the word for a matching authentication mechanism */
744 if((mechbit = Curl_sasl_decode_mech(line, wordlen, &llen)) &&
745 llen == wordlen)
746 pop3c->sasl.authmechs |= mechbit;
747
748 line += wordlen;
749 len -= wordlen;
750 }
751 }
752 }
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);
762 else {
763 failf(data, "STLS not supported.");
764 result = CURLE_USE_SSL_FAILED;
765 }
766 }
767 else
768 result = pop3_perform_authentication(conn);
769 }
770 else {
771 /* Clear text is supported when CAPA isn't recognised */
772 pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
773
774 result = pop3_perform_authentication(conn);
775 }
776
777 return result;
778}
779
780/* For STARTTLS responses */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100781static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
782 int pop3code,
783 pop3state instate)
784{
785 CURLcode result = CURLE_OK;
786 struct SessionHandle *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700787
Kristian Monsen5ab50182010-05-14 18:53:44 +0100788 (void)instate; /* no use for this yet */
789
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700790 if(pop3code != '+') {
791 if(data->set.use_ssl != CURLUSESSL_TRY) {
792 failf(data, "STARTTLS denied. %c", pop3code);
793 result = CURLE_USE_SSL_FAILED;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100794 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700795 else
796 result = pop3_perform_authentication(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100797 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700798 else
799 result = pop3_perform_upgrade_tls(conn);
800
Kristian Monsen5ab50182010-05-14 18:53:44 +0100801 return result;
802}
803
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700804/* For SASL authentication responses */
805static CURLcode pop3_state_auth_resp(struct connectdata *conn,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100806 int pop3code,
807 pop3state instate)
808{
809 CURLcode result = CURLE_OK;
810 struct SessionHandle *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700811 struct pop3_conn *pop3c = &conn->proto.pop3c;
812 saslprogress progress;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100813
814 (void)instate; /* no use for this yet */
815
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700816 result = Curl_sasl_continue(&pop3c->sasl, conn, pop3code, &progress);
817 if(!result)
818 switch(progress) {
819 case SASL_DONE:
820 state(conn, POP3_STOP); /* Authenticated */
821 break;
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);
827 else
828#endif
829 if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
830 /* Perform clear text authentication */
831 result = pop3_perform_user(conn);
832 else {
833 failf(data, "Authentication cancelled");
834 result = CURLE_LOGIN_DENIED;
835 }
836 break;
837 default:
838 break;
839 }
840
841 return result;
842}
843
844#ifndef CURL_DISABLE_CRYPTO_AUTH
845/* For APOP responses */
846static CURLcode pop3_state_apop_resp(struct connectdata *conn, int pop3code,
847 pop3state instate)
848{
849 CURLcode result = CURLE_OK;
850 struct SessionHandle *data = conn->data;
851
852 (void)instate; /* no use for this yet */
853
854 if(pop3code != '+') {
855 failf(data, "Authentication failed: %d", pop3code);
856 result = CURLE_LOGIN_DENIED;
857 }
858 else
859 /* End of connect phase */
860 state(conn, POP3_STOP);
861
862 return result;
863}
864#endif
865
866/* For USER responses */
867static CURLcode pop3_state_user_resp(struct connectdata *conn, int pop3code,
868 pop3state instate)
869{
870 CURLcode result = CURLE_OK;
871 struct SessionHandle *data = conn->data;
872
873 (void)instate; /* no use for this yet */
874
875 if(pop3code != '+') {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100876 failf(data, "Access denied. %c", pop3code);
877 result = CURLE_LOGIN_DENIED;
878 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700879 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700880 /* Send the PASS command */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700881 result = Curl_pp_sendf(&conn->proto.pop3c.pp, "PASS %s",
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700882 conn->passwd ? conn->passwd : "");
883 if(!result)
884 state(conn, POP3_PASS);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100885
Kristian Monsen5ab50182010-05-14 18:53:44 +0100886 return result;
887}
888
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700889/* For PASS responses */
890static CURLcode pop3_state_pass_resp(struct connectdata *conn, int pop3code,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100891 pop3state instate)
892{
893 CURLcode result = CURLE_OK;
894 struct SessionHandle *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700895
Kristian Monsen5ab50182010-05-14 18:53:44 +0100896 (void)instate; /* no use for this yet */
897
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700898 if(pop3code != '+') {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100899 failf(data, "Access denied. %c", pop3code);
900 result = CURLE_LOGIN_DENIED;
901 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700902 else
903 /* End of connect phase */
904 state(conn, POP3_STOP);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100905
Kristian Monsen5ab50182010-05-14 18:53:44 +0100906 return result;
907}
908
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700909/* For command responses */
910static CURLcode pop3_state_command_resp(struct connectdata *conn,
911 int pop3code,
912 pop3state instate)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100913{
914 CURLcode result = CURLE_OK;
915 struct SessionHandle *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700916 struct POP3 *pop3 = data->req.protop;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100917 struct pop3_conn *pop3c = &conn->proto.pop3c;
918 struct pingpong *pp = &pop3c->pp;
919
920 (void)instate; /* no use for this yet */
921
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700922 if(pop3code != '+') {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100923 state(conn, POP3_STOP);
924 return CURLE_RECV_ERROR;
925 }
926
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700927 /* 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. */
931 pop3c->eob = 2;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100932
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700933 /* 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. */
935 pop3c->strip = 2;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100936
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700937 if(pop3->transfer == FTPTRANSFER_BODY) {
938 /* POP3 download */
939 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100940
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700941 if(pp->cache) {
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 */
945
946 if(!data->set.opt_no_body) {
947 result = Curl_pop3_write(conn, pp->cache, pp->cache_size);
948 if(result)
949 return result;
950 }
951
952 /* Free the cache */
953 Curl_safefree(pp->cache);
954
955 /* Reset the cache size */
956 pp->cache_size = 0;
957 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100958 }
959
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700960 /* End of DO phase */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100961 state(conn, POP3_STOP);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100962
Kristian Monsen5ab50182010-05-14 18:53:44 +0100963 return result;
964}
965
966static CURLcode pop3_statemach_act(struct connectdata *conn)
967{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700968 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100969 curl_socket_t sock = conn->sock[FIRSTSOCKET];
Kristian Monsen5ab50182010-05-14 18:53:44 +0100970 int pop3code;
971 struct pop3_conn *pop3c = &conn->proto.pop3c;
972 struct pingpong *pp = &pop3c->pp;
973 size_t nread = 0;
974
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700975 /* 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);
978
979 /* Flush any data that needs to be sent */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100980 if(pp->sendleft)
981 return Curl_pp_flushsend(pp);
982
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700983 do {
984 /* Read the response from the server */
985 result = Curl_pp_readresp(sock, pp, &pop3code, &nread);
986 if(result)
987 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100988
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700989 if(!pop3code)
990 break;
991
992 /* We have now received a full POP3 server response */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100993 switch(pop3c->state) {
994 case POP3_SERVERGREET:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700995 result = pop3_state_servergreet_resp(conn, pop3code, pop3c->state);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100996 break;
997
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700998 case POP3_CAPA:
999 result = pop3_state_capa_resp(conn, pop3code, pop3c->state);
1000 break;
1001
1002 case POP3_STARTTLS:
1003 result = pop3_state_starttls_resp(conn, pop3code, pop3c->state);
1004 break;
1005
1006 case POP3_AUTH:
1007 result = pop3_state_auth_resp(conn, pop3code, pop3c->state);
1008 break;
1009
1010#ifndef CURL_DISABLE_CRYPTO_AUTH
1011 case POP3_APOP:
1012 result = pop3_state_apop_resp(conn, pop3code, pop3c->state);
1013 break;
1014#endif
1015
Kristian Monsen5ab50182010-05-14 18:53:44 +01001016 case POP3_USER:
1017 result = pop3_state_user_resp(conn, pop3code, pop3c->state);
1018 break;
1019
1020 case POP3_PASS:
1021 result = pop3_state_pass_resp(conn, pop3code, pop3c->state);
1022 break;
1023
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001024 case POP3_COMMAND:
1025 result = pop3_state_command_resp(conn, pop3code, pop3c->state);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001026 break;
1027
1028 case POP3_QUIT:
1029 /* fallthrough, just stop! */
1030 default:
1031 /* internal error */
1032 state(conn, POP3_STOP);
1033 break;
1034 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001035 } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
1036
Kristian Monsen5ab50182010-05-14 18:53:44 +01001037 return result;
1038}
1039
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001040/* Called repeatedly until done from multi.c */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001041static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done)
1042{
Kristian Monsen5ab50182010-05-14 18:53:44 +01001043 CURLcode result = CURLE_OK;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001044 struct pop3_conn *pop3c = &conn->proto.pop3c;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001045
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001046 if((conn->handler->flags & PROTOPT_SSL) && !pop3c->ssldone) {
1047 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &pop3c->ssldone);
1048 if(result || !pop3c->ssldone)
1049 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001050 }
1051
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001052 result = Curl_pp_statemach(&pop3c->pp, FALSE);
1053 *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
1054
Kristian Monsen5ab50182010-05-14 18:53:44 +01001055 return result;
1056}
1057
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001058static CURLcode pop3_block_statemach(struct connectdata *conn)
1059{
1060 CURLcode result = CURLE_OK;
1061 struct pop3_conn *pop3c = &conn->proto.pop3c;
1062
1063 while(pop3c->state != POP3_STOP && !result)
1064 result = Curl_pp_statemach(&pop3c->pp, TRUE);
1065
1066 return result;
1067}
1068
1069/* Allocate and initialize the POP3 struct for the current SessionHandle if
1070 required */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001071static CURLcode pop3_init(struct connectdata *conn)
1072{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001073 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001074 struct SessionHandle *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001075 struct POP3 *pop3;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001076
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001077 pop3 = data->req.protop = calloc(sizeof(struct POP3), 1);
1078 if(!pop3)
1079 result = CURLE_OUT_OF_MEMORY;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001080
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001081 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001082}
1083
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001084/* For the POP3 "protocol connect" and "doing" phases only */
1085static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
1086 int numsocks)
1087{
1088 return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks);
1089}
1090
1091/***********************************************************************
1092 *
1093 * pop3_connect()
1094 *
1095 * This function should do everything that is to be considered a part of the
1096 * connection phase.
Kristian Monsen5ab50182010-05-14 18:53:44 +01001097 *
1098 * The variable 'done' points to will be TRUE if the protocol-layer connect
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001099 * phase is done when this function returns, or FALSE if not.
Kristian Monsen5ab50182010-05-14 18:53:44 +01001100 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001101static CURLcode pop3_connect(struct connectdata *conn, bool *done)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001102{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001103 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001104 struct pop3_conn *pop3c = &conn->proto.pop3c;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001105 struct pingpong *pp = &pop3c->pp;
1106
1107 *done = FALSE; /* default to not done yet */
1108
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001109 /* We always support persistent connections in POP3 */
1110 connkeep(conn, "POP3 default");
Kristian Monsen5ab50182010-05-14 18:53:44 +01001111
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001112 /* Set the default response time-out */
1113 pp->response_time = RESP_TIMEOUT;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001114 pp->statemach_act = pop3_statemach_act;
1115 pp->endofresp = pop3_endofresp;
1116 pp->conn = conn;
1117
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001118 /* Set the default preferred authentication type and mechanism */
1119 pop3c->preftype = POP3_TYPE_ANY;
1120 Curl_sasl_init(&pop3c->sasl, &saslpop3);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001121
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001122 /* Initialise the pingpong layer */
1123 Curl_pp_init(pp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001124
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001125 /* Parse the URL options */
1126 result = pop3_parse_url_options(conn);
1127 if(result)
1128 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001129
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001130 /* Start off waiting for the server greeting response */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001131 state(conn, POP3_SERVERGREET);
1132
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001133 result = pop3_multi_statemach(conn, done);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001134
1135 return result;
1136}
1137
1138/***********************************************************************
1139 *
1140 * pop3_done()
1141 *
1142 * The DONE function. This does what needs to be done after a single DO has
1143 * performed.
1144 *
1145 * Input argument is already checked for validity.
1146 */
1147static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
1148 bool premature)
1149{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001150 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001151 struct SessionHandle *data = conn->data;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001152 struct POP3 *pop3 = data->req.protop;
1153
Kristian Monsen5ab50182010-05-14 18:53:44 +01001154 (void)premature;
1155
1156 if(!pop3)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001157 /* 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. */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001161 return CURLE_OK;
1162
1163 if(status) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001164 connclose(conn, "POP3 done with bad status");
1165 result = status; /* use the already set error code */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001166 }
1167
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001168 /* Cleanup our per-request based variables */
1169 Curl_safefree(pop3->id);
1170 Curl_safefree(pop3->custom);
1171
1172 /* Clear the transfer mode for the next request */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001173 pop3->transfer = FTPTRANSFER_BODY;
1174
1175 return result;
1176}
1177
1178/***********************************************************************
1179 *
1180 * pop3_perform()
1181 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001182 * This is the actual DO function for POP3. Get a message/listing according to
Kristian Monsen5ab50182010-05-14 18:53:44 +01001183 * the options previously setup.
1184 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001185static CURLcode pop3_perform(struct connectdata *conn, bool *connected,
1186 bool *dophase_done)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001187{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001188 /* This is POP3 and no proxy */
1189 CURLcode result = CURLE_OK;
1190 struct POP3 *pop3 = conn->data->req.protop;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001191
1192 DEBUGF(infof(conn->data, "DO phase starts\n"));
1193
1194 if(conn->data->set.opt_no_body) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001195 /* Requested no body means no transfer */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001196 pop3->transfer = FTPTRANSFER_INFO;
1197 }
1198
1199 *dophase_done = FALSE; /* not done yet */
1200
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001201 /* Start the first command in the DO phase */
1202 result = pop3_perform_command(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001203 if(result)
1204 return result;
1205
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001206 /* Run the state-machine */
1207 result = pop3_multi_statemach(conn, dophase_done);
1208
1209 *connected = conn->bits.tcpconnect[FIRSTSOCKET];
Kristian Monsen5ab50182010-05-14 18:53:44 +01001210
1211 if(*dophase_done)
1212 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1213
1214 return result;
1215}
1216
1217/***********************************************************************
1218 *
1219 * pop3_do()
1220 *
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).
1223 *
1224 * The input argument is already checked for validity.
1225 */
1226static CURLcode pop3_do(struct connectdata *conn, bool *done)
1227{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001228 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001229
1230 *done = FALSE; /* default to false */
1231
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001232 /* Parse the URL path */
1233 result = pop3_parse_url_path(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001234 if(result)
1235 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001236
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001237 /* Parse the custom request */
1238 result = pop3_parse_custom_request(conn);
1239 if(result)
1240 return result;
1241
1242 result = pop3_regular_transfer(conn, done);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001243
1244 return result;
1245}
1246
1247/***********************************************************************
1248 *
1249 * pop3_disconnect()
1250 *
1251 * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
1252 * resources. BLOCKING.
1253 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001254static CURLcode pop3_disconnect(struct connectdata *conn, bool dead_connection)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001255{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001256 struct pop3_conn *pop3c = &conn->proto.pop3c;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001257
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
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001260 disconnect wait in vain and cause more problems than we need to. */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001261
1262 /* The POP3 session may or may not have been allocated/setup at this
1263 point! */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001264 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 */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001267
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001268 /* Disconnect from the server */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001269 Curl_pp_disconnect(&pop3c->pp);
1270
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001271 /* Cleanup the SASL module */
1272 Curl_sasl_cleanup(conn, pop3c->sasl.authused);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001273
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001274 /* Cleanup our connection based variables */
1275 Curl_safefree(pop3c->apoptimestamp);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001276
1277 return CURLE_OK;
1278}
1279
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001280/* Call this when the DO phase has completed */
1281static CURLcode pop3_dophase_done(struct connectdata *conn, bool connected)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001282{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001283 (void)conn;
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001284 (void)connected;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001285
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001286 return CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001287}
1288
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001289/* Called from multi.c while DOing */
1290static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001291{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001292 CURLcode result = pop3_multi_statemach(conn, dophase_done);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001293
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001294 if(result)
1295 DEBUGF(infof(conn->data, "DO phase failed\n"));
1296 else if(*dophase_done) {
Kristian Monsen5ab50182010-05-14 18:53:44 +01001297 result = pop3_dophase_done(conn, FALSE /* not connected */);
1298
1299 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1300 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001301
Kristian Monsen5ab50182010-05-14 18:53:44 +01001302 return result;
1303}
1304
1305/***********************************************************************
1306 *
1307 * pop3_regular_transfer()
1308 *
1309 * The input argument is already checked for validity.
1310 *
1311 * Performs all commands done before a regular transfer between a local and a
1312 * remote host.
Kristian Monsen5ab50182010-05-14 18:53:44 +01001313 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001314static CURLcode pop3_regular_transfer(struct connectdata *conn,
1315 bool *dophase_done)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001316{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001317 CURLcode result = CURLE_OK;
1318 bool connected = FALSE;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001319 struct SessionHandle *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001320
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001321 /* Make sure size is unknown at this point */
1322 data->req.size = -1;
1323
1324 /* Set the progress data */
Kristian Monsen5ab50182010-05-14 18:53:44 +01001325 Curl_pgrsSetUploadCounter(data, 0);
1326 Curl_pgrsSetDownloadCounter(data, 0);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001327 Curl_pgrsSetUploadSize(data, -1);
1328 Curl_pgrsSetDownloadSize(data, -1);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001329
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001330 /* Carry out the perform */
1331 result = pop3_perform(conn, &connected, dophase_done);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001332
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001333 /* Perform post DO phase operations if necessary */
1334 if(!result && *dophase_done)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001335 result = pop3_dophase_done(conn, connected);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001336
1337 return result;
1338}
1339
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001340static CURLcode pop3_setup_connection(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001341{
1342 struct SessionHandle *data = conn->data;
1343
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001344 /* Initialise the POP3 layer */
1345 CURLcode result = pop3_init(conn);
1346 if(result)
1347 return result;
1348
Kristian Monsen5ab50182010-05-14 18:53:44 +01001349 if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001350 /* Unless we have asked to tunnel POP3 operations through the proxy, we
Kristian Monsen5ab50182010-05-14 18:53:44 +01001351 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;
1355 else {
1356#ifdef USE_SSL
1357 conn->handler = &Curl_handler_pop3s_proxy;
1358#else
1359 failf(data, "POP3S not supported!");
1360 return CURLE_UNSUPPORTED_PROTOCOL;
1361#endif
1362 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001363
1364 /* set it up as an HTTP connection instead */
1365 return conn->handler->setup_connection(conn);
Kristian Monsen5ab50182010-05-14 18:53:44 +01001366#else
1367 failf(data, "POP3 over http proxy requires HTTP support built-in!");
1368 return CURLE_UNSUPPORTED_PROTOCOL;
1369#endif
1370 }
1371
1372 data->state.path++; /* don't include the initial slash */
1373
1374 return CURLE_OK;
1375}
1376
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001377/***********************************************************************
1378 *
1379 * pop3_parse_url_options()
1380 *
1381 * Parse the URL login options.
1382 */
1383static CURLcode pop3_parse_url_options(struct connectdata *conn)
1384{
1385 CURLcode result = CURLE_OK;
1386 struct pop3_conn *pop3c = &conn->proto.pop3c;
1387 const char *ptr = conn->options;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001388
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001389 pop3c->sasl.resetprefs = TRUE;
1390
1391 while(!result && ptr && *ptr) {
1392 const char *key = ptr;
1393 const char *value;
1394
1395 while(*ptr && *ptr != '=')
1396 ptr++;
1397
1398 value = ptr + 1;
1399
1400 while(*ptr && *ptr != ';')
1401 ptr++;
1402
1403 if(strnequal(key, "AUTH=", 5)) {
1404 result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
1405 value, ptr - value);
1406
1407 if(result && strnequal(value, "+APOP", ptr - value)) {
1408 pop3c->preftype = POP3_TYPE_APOP;
1409 pop3c->sasl.prefmech = SASL_AUTH_NONE;
1410 result = CURLE_OK;
1411 }
1412 }
1413 else
1414 result = CURLE_URL_MALFORMAT;
1415
1416 if(*ptr == ';')
1417 ptr++;
1418 }
1419
1420 if(pop3c->preftype != POP3_TYPE_APOP)
1421 switch(pop3c->sasl.prefmech) {
1422 case SASL_AUTH_NONE:
1423 pop3c->preftype = POP3_TYPE_NONE;
1424 break;
1425 case SASL_AUTH_DEFAULT:
1426 pop3c->preftype = POP3_TYPE_ANY;
1427 break;
1428 default:
1429 pop3c->preftype = POP3_TYPE_SASL;
1430 break;
1431 }
1432
1433 return result;
1434}
1435
1436/***********************************************************************
1437 *
1438 * pop3_parse_url_path()
1439 *
1440 * Parse the URL path into separate path components.
1441 */
1442static CURLcode pop3_parse_url_path(struct connectdata *conn)
1443{
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;
1448
1449 /* URL decode the path for the message ID */
1450 return Curl_urldecode(data, path, 0, &pop3->id, NULL, TRUE);
1451}
1452
1453/***********************************************************************
1454 *
1455 * pop3_parse_custom_request()
1456 *
1457 * Parse the custom request.
1458 */
1459static CURLcode pop3_parse_custom_request(struct connectdata *conn)
1460{
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];
1465
1466 /* URL decode the custom request */
1467 if(custom)
1468 result = Curl_urldecode(data, custom, 0, &pop3->custom, NULL, TRUE);
1469
1470 return result;
1471}
1472
1473/***********************************************************************
1474 *
1475 * Curl_pop3_write()
1476 *
Kristian Monsen5ab50182010-05-14 18:53:44 +01001477 * This function scans the body after the end-of-body and writes everything
1478 * until the end is found.
1479 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001480CURLcode Curl_pop3_write(struct connectdata *conn, char *str, size_t nread)
Kristian Monsen5ab50182010-05-14 18:53:44 +01001481{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001482 /* This code could be made into a special function in the handler struct */
1483 CURLcode result = CURLE_OK;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001484 struct SessionHandle *data = conn->data;
1485 struct SingleRequest *k = &data->req;
1486
Kristian Monsen5ab50182010-05-14 18:53:44 +01001487 struct pop3_conn *pop3c = &conn->proto.pop3c;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001488 bool strip_dot = FALSE;
1489 size_t last = 0;
1490 size_t i;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001491
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001492 /* 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;
1499
1500 switch(str[i]) {
1501 case 0x0d:
1502 if(pop3c->eob == 0) {
1503 pop3c->eob++;
1504
1505 if(i) {
1506 /* Write out the body part that didn't match */
1507 result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
1508 i - last);
1509
1510 if(result)
1511 return result;
1512
1513 last = i;
1514 }
1515 }
1516 else if(pop3c->eob == 3)
1517 pop3c->eob++;
1518 else
1519 /* If the character match wasn't at position 0 or 3 then restart the
1520 pattern matching */
1521 pop3c->eob = 1;
1522 break;
1523
1524 case 0x0a:
1525 if(pop3c->eob == 1 || pop3c->eob == 4)
1526 pop3c->eob++;
1527 else
1528 /* If the character match wasn't at position 1 or 4 then start the
1529 search again */
1530 pop3c->eob = 0;
1531 break;
1532
1533 case 0x2e:
1534 if(pop3c->eob == 2)
1535 pop3c->eob++;
1536 else if(pop3c->eob == 3) {
1537 /* We have an extra dot after the CRLF which we need to strip off */
1538 strip_dot = TRUE;
1539 pop3c->eob = 0;
1540 }
1541 else
1542 /* If the character match wasn't at position 2 then start the search
1543 again */
1544 pop3c->eob = 0;
1545 break;
1546
1547 default:
Kristian Monsen5ab50182010-05-14 18:53:44 +01001548 pop3c->eob = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001549 break;
1550 }
1551
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
1556 below */
1557 while(prev && pop3c->strip) {
1558 prev--;
1559 pop3c->strip--;
1560 }
1561
1562 if(prev) {
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);
1567
1568 if(result)
1569 return result;
1570
1571 last = i;
1572 strip_dot = FALSE;
1573 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001574 }
1575 }
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001576
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);
1582
1583 k->keepon &= ~KEEP_RECV;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001584 pop3c->eob = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001585
1586 return result;
Kristian Monsen5ab50182010-05-14 18:53:44 +01001587 }
1588
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001589 if(pop3c->eob)
1590 /* While EOB is matching nothing should be output */
1591 return CURLE_OK;
1592
1593 if(nread - last) {
1594 result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
1595 nread - last);
1596 }
Kristian Monsen5ab50182010-05-14 18:53:44 +01001597
1598 return result;
1599}
1600
1601#endif /* CURL_DISABLE_POP3 */