[164] | 1 | /***************************************************************************
|
---|
| 2 | * _ _ ____ _
|
---|
| 3 | * Project ___| | | | _ \| |
|
---|
| 4 | * / __| | | | |_) | |
|
---|
| 5 | * | (__| |_| | _ <| |___
|
---|
| 6 | * \___|\___/|_| \_\_____|
|
---|
| 7 | *
|
---|
| 8 | * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
---|
| 9 | *
|
---|
| 10 | * This software is licensed as described in the file COPYING, which
|
---|
| 11 | * you should have received as part of this distribution. The terms
|
---|
| 12 | * are also available at https://curl.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 | *
|
---|
| 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>
|
---|
| 34 | *
|
---|
| 35 | ***************************************************************************/
|
---|
| 36 |
|
---|
| 37 | #include "curl_setup.h"
|
---|
| 38 |
|
---|
| 39 | #ifndef CURL_DISABLE_POP3
|
---|
| 40 |
|
---|
| 41 | #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"
|
---|
| 66 | #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"
|
---|
| 76 | #include "vtls/vtls.h"
|
---|
| 77 | #include "connect.h"
|
---|
| 78 | #include "strerror.h"
|
---|
| 79 | #include "select.h"
|
---|
| 80 | #include "multiif.h"
|
---|
| 81 | #include "url.h"
|
---|
| 82 | #include "rawstr.h"
|
---|
| 83 | #include "curl_sasl.h"
|
---|
| 84 | #include "curl_md5.h"
|
---|
| 85 | #include "warnless.h"
|
---|
| 86 | #include "curl_printf.h"
|
---|
| 87 | #include "curl_memory.h"
|
---|
| 88 | /* The last #include file should be: */
|
---|
| 89 | #include "memdebug.h"
|
---|
| 90 |
|
---|
| 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,
|
---|
| 95 | bool premature);
|
---|
| 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,
|
---|
| 100 | int numsocks);
|
---|
| 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);
|
---|
| 110 |
|
---|
| 111 | /*
|
---|
| 112 | * POP3 protocol handler.
|
---|
| 113 | */
|
---|
| 114 |
|
---|
| 115 | const 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 */
|
---|
| 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 */
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | #ifdef USE_SSL
|
---|
| 136 | /*
|
---|
| 137 | * POP3S protocol handler.
|
---|
| 138 | */
|
---|
| 139 |
|
---|
| 140 | const 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 */
|
---|
| 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 */
|
---|
| 159 | };
|
---|
| 160 | #endif
|
---|
| 161 |
|
---|
| 162 | #ifndef CURL_DISABLE_HTTP
|
---|
| 163 | /*
|
---|
| 164 | * HTTP-proxyed POP3 protocol handler.
|
---|
| 165 | */
|
---|
| 166 |
|
---|
| 167 | static const struct Curl_handler Curl_handler_pop3_proxy = {
|
---|
| 168 | "POP3", /* scheme */
|
---|
| 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 */
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | #ifdef USE_SSL
|
---|
| 188 | /*
|
---|
| 189 | * HTTP-proxyed POP3S protocol handler.
|
---|
| 190 | */
|
---|
| 191 |
|
---|
| 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 */
|
---|
| 210 | };
|
---|
| 211 | #endif
|
---|
| 212 | #endif
|
---|
| 213 |
|
---|
| 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 */
|
---|
| 223 | };
|
---|
| 224 |
|
---|
| 225 | #ifdef USE_SSL
|
---|
| 226 | static void pop3_to_pop3s(struct connectdata *conn)
|
---|
| 227 | {
|
---|
| 228 | conn->handler = &Curl_handler_pop3s;
|
---|
| 229 | }
|
---|
| 230 | #else
|
---|
| 231 | #define pop3_to_pop3s(x) Curl_nop_stmt
|
---|
| 232 | #endif
|
---|
| 233 |
|
---|
| 234 | /***********************************************************************
|
---|
| 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 | */
|
---|
| 243 | static 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 |
|
---|
| 252 | return TRUE;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 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))
|
---|
| 259 | /* Treat the response as a success */
|
---|
| 260 | *resp = '+';
|
---|
| 261 | else
|
---|
| 262 | /* Treat the response as an untagged continuation */
|
---|
| 263 | *resp = '*';
|
---|
| 264 |
|
---|
| 265 | return TRUE;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /* Do we have a success response? */
|
---|
| 269 | if(len >= 3 && !memcmp("+OK", line, 3)) {
|
---|
| 270 | *resp = '+';
|
---|
| 271 |
|
---|
| 272 | return TRUE;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /* Do we have a continuation response? */
|
---|
| 276 | if(len >= 1 && !memcmp("+", line, 1)) {
|
---|
| 277 | *resp = '*';
|
---|
| 278 |
|
---|
| 279 | return TRUE;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | return FALSE; /* Nothing for us */
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | /***********************************************************************
|
---|
| 286 | *
|
---|
| 287 | * pop3_get_message()
|
---|
| 288 | *
|
---|
| 289 | * Gets the authentication message from the response buffer.
|
---|
| 290 | */
|
---|
| 291 | static void pop3_get_message(char *buffer, char** outptr)
|
---|
| 292 | {
|
---|
| 293 | size_t len = 0;
|
---|
| 294 | char* message = NULL;
|
---|
| 295 |
|
---|
| 296 | /* Find the start of the message */
|
---|
| 297 | for(message = buffer + 2; *message == ' ' || *message == '\t'; message++)
|
---|
| 298 | ;
|
---|
| 299 |
|
---|
| 300 | /* Find the end of the message */
|
---|
| 301 | for(len = strlen(message); len--;)
|
---|
| 302 | if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
|
---|
| 303 | message[len] != '\t')
|
---|
| 304 | break;
|
---|
| 305 |
|
---|
| 306 | /* Terminate the message */
|
---|
| 307 | if(++len) {
|
---|
| 308 | message[len] = '\0';
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | *outptr = message;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /***********************************************************************
|
---|
| 315 | *
|
---|
| 316 | * state()
|
---|
| 317 | *
|
---|
| 318 | * This is the ONLY way to change POP3 state!
|
---|
| 319 | */
|
---|
| 320 | static void state(struct connectdata *conn, pop3state newstate)
|
---|
| 321 | {
|
---|
| 322 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 323 | #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
---|
| 324 | /* for debug purposes */
|
---|
| 325 | static const char * const names[] = {
|
---|
| 326 | "STOP",
|
---|
| 327 | "SERVERGREET",
|
---|
| 328 | "CAPA",
|
---|
| 329 | "STARTTLS",
|
---|
| 330 | "UPGRADETLS",
|
---|
| 331 | "AUTH",
|
---|
| 332 | "APOP",
|
---|
| 333 | "USER",
|
---|
| 334 | "PASS",
|
---|
| 335 | "COMMAND",
|
---|
| 336 | "QUIT",
|
---|
| 337 | /* LAST */
|
---|
| 338 | };
|
---|
| 339 |
|
---|
| 340 | if(pop3c->state != newstate)
|
---|
| 341 | infof(conn->data, "POP3 %p state change from %s to %s\n",
|
---|
| 342 | (void *)pop3c, names[pop3c->state], names[newstate]);
|
---|
| 343 | #endif
|
---|
| 344 |
|
---|
| 345 | pop3c->state = newstate;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | /***********************************************************************
|
---|
| 349 | *
|
---|
| 350 | * pop3_perform_capa()
|
---|
| 351 | *
|
---|
| 352 | * Sends the CAPA command in order to obtain a list of server side supported
|
---|
| 353 | * capabilities.
|
---|
| 354 | */
|
---|
| 355 | static CURLcode pop3_perform_capa(struct connectdata *conn)
|
---|
| 356 | {
|
---|
| 357 | CURLcode result = CURLE_OK;
|
---|
| 358 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 359 |
|
---|
| 360 | pop3c->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
|
---|
| 361 | pop3c->sasl.authused = SASL_AUTH_NONE; /* Clear the auth. mechanism used */
|
---|
| 362 | pop3c->tls_supported = FALSE; /* Clear the TLS capability */
|
---|
| 363 |
|
---|
| 364 | /* Send the CAPA command */
|
---|
| 365 | result = Curl_pp_sendf(&pop3c->pp, "%s", "CAPA");
|
---|
| 366 |
|
---|
| 367 | if(!result)
|
---|
| 368 | state(conn, POP3_CAPA);
|
---|
| 369 |
|
---|
| 370 | return result;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | /***********************************************************************
|
---|
| 374 | *
|
---|
| 375 | * pop3_perform_starttls()
|
---|
| 376 | *
|
---|
| 377 | * Sends the STLS command to start the upgrade to TLS.
|
---|
| 378 | */
|
---|
| 379 | static CURLcode pop3_perform_starttls(struct connectdata *conn)
|
---|
| 380 | {
|
---|
| 381 | CURLcode result = CURLE_OK;
|
---|
| 382 |
|
---|
| 383 | /* Send the STLS command */
|
---|
| 384 | result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", "STLS");
|
---|
| 385 |
|
---|
| 386 | if(!result)
|
---|
| 387 | state(conn, POP3_STARTTLS);
|
---|
| 388 |
|
---|
| 389 | return result;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | /***********************************************************************
|
---|
| 393 | *
|
---|
| 394 | * pop3_perform_upgrade_tls()
|
---|
| 395 | *
|
---|
| 396 | * Performs the upgrade to TLS.
|
---|
| 397 | */
|
---|
| 398 | static CURLcode pop3_perform_upgrade_tls(struct connectdata *conn)
|
---|
| 399 | {
|
---|
| 400 | CURLcode result = CURLE_OK;
|
---|
| 401 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 402 |
|
---|
| 403 | /* Start the SSL connection */
|
---|
| 404 | result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &pop3c->ssldone);
|
---|
| 405 |
|
---|
| 406 | if(!result) {
|
---|
| 407 | if(pop3c->state != POP3_UPGRADETLS)
|
---|
| 408 | state(conn, POP3_UPGRADETLS);
|
---|
| 409 |
|
---|
| 410 | if(pop3c->ssldone) {
|
---|
| 411 | pop3_to_pop3s(conn);
|
---|
| 412 | result = pop3_perform_capa(conn);
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | return result;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /***********************************************************************
|
---|
| 420 | *
|
---|
| 421 | * pop3_perform_user()
|
---|
| 422 | *
|
---|
| 423 | * Sends a clear text USER command to authenticate with.
|
---|
| 424 | */
|
---|
| 425 | static CURLcode pop3_perform_user(struct connectdata *conn)
|
---|
| 426 | {
|
---|
| 427 | CURLcode result = CURLE_OK;
|
---|
| 428 |
|
---|
| 429 | /* Check we have a username and password to authenticate with and end the
|
---|
| 430 | connect phase if we don't */
|
---|
| 431 | if(!conn->bits.user_passwd) {
|
---|
| 432 | state(conn, POP3_STOP);
|
---|
| 433 |
|
---|
| 434 | return result;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /* Send the USER command */
|
---|
| 438 | result = Curl_pp_sendf(&conn->proto.pop3c.pp, "USER %s",
|
---|
| 439 | conn->user ? conn->user : "");
|
---|
| 440 | if(!result)
|
---|
| 441 | state(conn, POP3_USER);
|
---|
| 442 |
|
---|
| 443 | return result;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | #ifndef CURL_DISABLE_CRYPTO_AUTH
|
---|
| 447 | /***********************************************************************
|
---|
| 448 | *
|
---|
| 449 | * pop3_perform_apop()
|
---|
| 450 | *
|
---|
| 451 | * Sends an APOP command to authenticate with.
|
---|
| 452 | */
|
---|
| 453 | static CURLcode pop3_perform_apop(struct connectdata *conn)
|
---|
| 454 | {
|
---|
| 455 | CURLcode result = CURLE_OK;
|
---|
| 456 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 457 | size_t i;
|
---|
| 458 | MD5_context *ctxt;
|
---|
| 459 | unsigned char digest[MD5_DIGEST_LEN];
|
---|
| 460 | char secret[2 * MD5_DIGEST_LEN + 1];
|
---|
| 461 |
|
---|
| 462 | /* Check we have a username and password to authenticate with and end the
|
---|
| 463 | connect phase if we don't */
|
---|
| 464 | if(!conn->bits.user_passwd) {
|
---|
| 465 | state(conn, POP3_STOP);
|
---|
| 466 |
|
---|
| 467 | return result;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | /* Create the digest */
|
---|
| 471 | ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
|
---|
| 472 | if(!ctxt)
|
---|
| 473 | return CURLE_OUT_OF_MEMORY;
|
---|
| 474 |
|
---|
| 475 | Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp,
|
---|
| 476 | curlx_uztoui(strlen(pop3c->apoptimestamp)));
|
---|
| 477 |
|
---|
| 478 | Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd,
|
---|
| 479 | curlx_uztoui(strlen(conn->passwd)));
|
---|
| 480 |
|
---|
| 481 | /* Finalise the digest */
|
---|
| 482 | Curl_MD5_final(ctxt, digest);
|
---|
| 483 |
|
---|
| 484 | /* Convert the calculated 16 octet digest into a 32 byte hex string */
|
---|
| 485 | for(i = 0; i < MD5_DIGEST_LEN; i++)
|
---|
| 486 | snprintf(&secret[2 * i], 3, "%02x", digest[i]);
|
---|
| 487 |
|
---|
| 488 | result = Curl_pp_sendf(&pop3c->pp, "APOP %s %s", conn->user, secret);
|
---|
| 489 |
|
---|
| 490 | if(!result)
|
---|
| 491 | state(conn, POP3_APOP);
|
---|
| 492 |
|
---|
| 493 | return result;
|
---|
| 494 | }
|
---|
| 495 | #endif
|
---|
| 496 |
|
---|
| 497 | /***********************************************************************
|
---|
| 498 | *
|
---|
| 499 | * pop3_perform_auth()
|
---|
| 500 | *
|
---|
| 501 | * Sends an AUTH command allowing the client to login with the given SASL
|
---|
| 502 | * authentication mechanism.
|
---|
| 503 | */
|
---|
| 504 | static CURLcode pop3_perform_auth(struct connectdata *conn,
|
---|
| 505 | const char *mech,
|
---|
| 506 | const char *initresp)
|
---|
| 507 | {
|
---|
| 508 | CURLcode result = CURLE_OK;
|
---|
| 509 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 510 |
|
---|
| 511 | if(initresp) { /* AUTH <mech> ...<crlf> */
|
---|
| 512 | /* Send the AUTH command with the initial response */
|
---|
| 513 | result = Curl_pp_sendf(&pop3c->pp, "AUTH %s %s", mech, initresp);
|
---|
| 514 | }
|
---|
| 515 | else {
|
---|
| 516 | /* Send the AUTH command */
|
---|
| 517 | result = Curl_pp_sendf(&pop3c->pp, "AUTH %s", mech);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | return result;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | /***********************************************************************
|
---|
| 524 | *
|
---|
| 525 | * pop3_continue_auth()
|
---|
| 526 | *
|
---|
| 527 | * Sends SASL continuation data or cancellation.
|
---|
| 528 | */
|
---|
| 529 | static CURLcode pop3_continue_auth(struct connectdata *conn,
|
---|
| 530 | const char *resp)
|
---|
| 531 | {
|
---|
| 532 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 533 |
|
---|
| 534 | return Curl_pp_sendf(&pop3c->pp, "%s", resp);
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | /***********************************************************************
|
---|
| 538 | *
|
---|
| 539 | * pop3_perform_authentication()
|
---|
| 540 | *
|
---|
| 541 | * Initiates the authentication sequence, with the appropriate SASL
|
---|
| 542 | * authentication mechanism, falling back to APOP and clear text should a
|
---|
| 543 | * common mechanism not be available between the client and server.
|
---|
| 544 | */
|
---|
| 545 | static CURLcode pop3_perform_authentication(struct connectdata *conn)
|
---|
| 546 | {
|
---|
| 547 | CURLcode result = CURLE_OK;
|
---|
| 548 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 549 | saslprogress progress = SASL_IDLE;
|
---|
| 550 |
|
---|
| 551 | /* Check we have enough data to authenticate with and end the
|
---|
| 552 | connect phase if we don't */
|
---|
| 553 | if(!Curl_sasl_can_authenticate(&pop3c->sasl, conn)) {
|
---|
| 554 | state(conn, POP3_STOP);
|
---|
| 555 | return result;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) {
|
---|
| 559 | /* Calculate the SASL login details */
|
---|
| 560 | result = Curl_sasl_start(&pop3c->sasl, conn, FALSE, &progress);
|
---|
| 561 |
|
---|
| 562 | if(!result)
|
---|
| 563 | if(progress == SASL_INPROGRESS)
|
---|
| 564 | state(conn, POP3_AUTH);
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | if(!result && progress == SASL_IDLE) {
|
---|
| 568 | #ifndef CURL_DISABLE_CRYPTO_AUTH
|
---|
| 569 | if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
|
---|
| 570 | /* Perform APOP authentication */
|
---|
| 571 | result = pop3_perform_apop(conn);
|
---|
| 572 | else
|
---|
| 573 | #endif
|
---|
| 574 | if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
|
---|
| 575 | /* Perform clear text authentication */
|
---|
| 576 | result = pop3_perform_user(conn);
|
---|
| 577 | else {
|
---|
| 578 | /* Other mechanisms not supported */
|
---|
| 579 | infof(conn->data, "No known authentication mechanisms supported!\n");
|
---|
| 580 | result = CURLE_LOGIN_DENIED;
|
---|
| 581 | }
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | return result;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | /***********************************************************************
|
---|
| 588 | *
|
---|
| 589 | * pop3_perform_command()
|
---|
| 590 | *
|
---|
| 591 | * Sends a POP3 based command.
|
---|
| 592 | */
|
---|
| 593 | static CURLcode pop3_perform_command(struct connectdata *conn)
|
---|
| 594 | {
|
---|
| 595 | CURLcode result = CURLE_OK;
|
---|
| 596 | struct SessionHandle *data = conn->data;
|
---|
| 597 | struct POP3 *pop3 = data->req.protop;
|
---|
| 598 | const char *command = NULL;
|
---|
| 599 |
|
---|
| 600 | /* Calculate the default command */
|
---|
| 601 | if(pop3->id[0] == '\0' || conn->data->set.ftp_list_only) {
|
---|
| 602 | command = "LIST";
|
---|
| 603 |
|
---|
| 604 | if(pop3->id[0] != '\0')
|
---|
| 605 | /* Message specific LIST so skip the BODY transfer */
|
---|
| 606 | pop3->transfer = FTPTRANSFER_INFO;
|
---|
| 607 | }
|
---|
| 608 | else
|
---|
| 609 | command = "RETR";
|
---|
| 610 |
|
---|
| 611 | /* Send the command */
|
---|
| 612 | if(pop3->id[0] != '\0')
|
---|
| 613 | result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s %s",
|
---|
| 614 | (pop3->custom && pop3->custom[0] != '\0' ?
|
---|
| 615 | pop3->custom : command), pop3->id);
|
---|
| 616 | else
|
---|
| 617 | result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s",
|
---|
| 618 | (pop3->custom && pop3->custom[0] != '\0' ?
|
---|
| 619 | pop3->custom : command));
|
---|
| 620 |
|
---|
| 621 | if(!result)
|
---|
| 622 | state(conn, POP3_COMMAND);
|
---|
| 623 |
|
---|
| 624 | return result;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | /***********************************************************************
|
---|
| 628 | *
|
---|
| 629 | * pop3_perform_quit()
|
---|
| 630 | *
|
---|
| 631 | * Performs the quit action prior to sclose() be called.
|
---|
| 632 | */
|
---|
| 633 | static CURLcode pop3_perform_quit(struct connectdata *conn)
|
---|
| 634 | {
|
---|
| 635 | CURLcode result = CURLE_OK;
|
---|
| 636 |
|
---|
| 637 | /* Send the QUIT command */
|
---|
| 638 | result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", "QUIT");
|
---|
| 639 |
|
---|
| 640 | if(!result)
|
---|
| 641 | state(conn, POP3_QUIT);
|
---|
| 642 |
|
---|
| 643 | return result;
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | /* For the initial server greeting */
|
---|
| 647 | static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
|
---|
| 648 | int pop3code,
|
---|
| 649 | pop3state instate)
|
---|
| 650 | {
|
---|
| 651 | CURLcode result = CURLE_OK;
|
---|
| 652 | struct SessionHandle *data = conn->data;
|
---|
| 653 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 654 | const char *line = data->state.buffer;
|
---|
| 655 | size_t len = strlen(line);
|
---|
| 656 | size_t i;
|
---|
| 657 |
|
---|
| 658 | (void)instate; /* no use for this yet */
|
---|
| 659 |
|
---|
| 660 | if(pop3code != '+') {
|
---|
| 661 | failf(data, "Got unexpected pop3-server response");
|
---|
| 662 | result = CURLE_FTP_WEIRD_SERVER_REPLY;
|
---|
| 663 | }
|
---|
| 664 | else {
|
---|
| 665 | /* Does the server support APOP authentication? */
|
---|
| 666 | if(len >= 4 && line[len - 2] == '>') {
|
---|
| 667 | /* Look for the APOP timestamp */
|
---|
| 668 | for(i = 3; i < len - 2; ++i) {
|
---|
| 669 | if(line[i] == '<') {
|
---|
| 670 | /* Calculate the length of the timestamp */
|
---|
| 671 | size_t timestamplen = len - 1 - i;
|
---|
| 672 | if(!timestamplen)
|
---|
| 673 | break;
|
---|
| 674 |
|
---|
| 675 | /* Allocate some memory for the timestamp */
|
---|
| 676 | pop3c->apoptimestamp = (char *)calloc(1, timestamplen + 1);
|
---|
| 677 |
|
---|
| 678 | if(!pop3c->apoptimestamp)
|
---|
| 679 | break;
|
---|
| 680 |
|
---|
| 681 | /* Copy the timestamp */
|
---|
| 682 | memcpy(pop3c->apoptimestamp, line + i, timestamplen);
|
---|
| 683 | pop3c->apoptimestamp[timestamplen] = '\0';
|
---|
| 684 |
|
---|
| 685 | /* Store the APOP capability */
|
---|
| 686 | pop3c->authtypes |= POP3_TYPE_APOP;
|
---|
| 687 | break;
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 | result = pop3_perform_capa(conn);
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | return result;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | /* For CAPA responses */
|
---|
| 699 | static CURLcode pop3_state_capa_resp(struct connectdata *conn, int pop3code,
|
---|
| 700 | pop3state instate)
|
---|
| 701 | {
|
---|
| 702 | CURLcode result = CURLE_OK;
|
---|
| 703 | struct SessionHandle *data = conn->data;
|
---|
| 704 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 705 | const char *line = data->state.buffer;
|
---|
| 706 | size_t len = strlen(line);
|
---|
| 707 | size_t wordlen;
|
---|
| 708 |
|
---|
| 709 | (void)instate; /* no use for this yet */
|
---|
| 710 |
|
---|
| 711 | /* Do we have a untagged continuation response? */
|
---|
| 712 | if(pop3code == '*') {
|
---|
| 713 | /* Does the server support the STLS capability? */
|
---|
| 714 | if(len >= 4 && !memcmp(line, "STLS", 4))
|
---|
| 715 | pop3c->tls_supported = TRUE;
|
---|
| 716 |
|
---|
| 717 | /* Does the server support clear text authentication? */
|
---|
| 718 | else if(len >= 4 && !memcmp(line, "USER", 4))
|
---|
| 719 | pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
|
---|
| 720 |
|
---|
| 721 | /* Does the server support SASL based authentication? */
|
---|
| 722 | else if(len >= 5 && !memcmp(line, "SASL ", 5)) {
|
---|
| 723 | pop3c->authtypes |= POP3_TYPE_SASL;
|
---|
| 724 |
|
---|
| 725 | /* Advance past the SASL keyword */
|
---|
| 726 | line += 5;
|
---|
| 727 | len -= 5;
|
---|
| 728 |
|
---|
| 729 | /* Loop through the data line */
|
---|
| 730 | for(;;) {
|
---|
| 731 | size_t llen;
|
---|
| 732 | unsigned int mechbit;
|
---|
| 733 |
|
---|
| 734 | while(len &&
|
---|
| 735 | (*line == ' ' || *line == '\t' ||
|
---|
| 736 | *line == '\r' || *line == '\n')) {
|
---|
| 737 |
|
---|
| 738 | line++;
|
---|
| 739 | len--;
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | if(!len)
|
---|
| 743 | break;
|
---|
| 744 |
|
---|
| 745 | /* Extract the word */
|
---|
| 746 | for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
|
---|
| 747 | line[wordlen] != '\t' && line[wordlen] != '\r' &&
|
---|
| 748 | line[wordlen] != '\n';)
|
---|
| 749 | wordlen++;
|
---|
| 750 |
|
---|
| 751 | /* Test the word for a matching authentication mechanism */
|
---|
| 752 | if((mechbit = Curl_sasl_decode_mech(line, wordlen, &llen)) &&
|
---|
| 753 | llen == wordlen)
|
---|
| 754 | pop3c->sasl.authmechs |= mechbit;
|
---|
| 755 |
|
---|
| 756 | line += wordlen;
|
---|
| 757 | len -= wordlen;
|
---|
| 758 | }
|
---|
| 759 | }
|
---|
| 760 | }
|
---|
| 761 | else if(pop3code == '+') {
|
---|
| 762 | if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
|
---|
| 763 | /* We don't have a SSL/TLS connection yet, but SSL is requested */
|
---|
| 764 | if(pop3c->tls_supported)
|
---|
| 765 | /* Switch to TLS connection now */
|
---|
| 766 | result = pop3_perform_starttls(conn);
|
---|
| 767 | else if(data->set.use_ssl == CURLUSESSL_TRY)
|
---|
| 768 | /* Fallback and carry on with authentication */
|
---|
| 769 | result = pop3_perform_authentication(conn);
|
---|
| 770 | else {
|
---|
| 771 | failf(data, "STLS not supported.");
|
---|
| 772 | result = CURLE_USE_SSL_FAILED;
|
---|
| 773 | }
|
---|
| 774 | }
|
---|
| 775 | else
|
---|
| 776 | result = pop3_perform_authentication(conn);
|
---|
| 777 | }
|
---|
| 778 | else {
|
---|
| 779 | /* Clear text is supported when CAPA isn't recognised */
|
---|
| 780 | pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
|
---|
| 781 |
|
---|
| 782 | result = pop3_perform_authentication(conn);
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | return result;
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | /* For STARTTLS responses */
|
---|
| 789 | static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
|
---|
| 790 | int pop3code,
|
---|
| 791 | pop3state instate)
|
---|
| 792 | {
|
---|
| 793 | CURLcode result = CURLE_OK;
|
---|
| 794 | struct SessionHandle *data = conn->data;
|
---|
| 795 |
|
---|
| 796 | (void)instate; /* no use for this yet */
|
---|
| 797 |
|
---|
| 798 | if(pop3code != '+') {
|
---|
| 799 | if(data->set.use_ssl != CURLUSESSL_TRY) {
|
---|
| 800 | failf(data, "STARTTLS denied. %c", pop3code);
|
---|
| 801 | result = CURLE_USE_SSL_FAILED;
|
---|
| 802 | }
|
---|
| 803 | else
|
---|
| 804 | result = pop3_perform_authentication(conn);
|
---|
| 805 | }
|
---|
| 806 | else
|
---|
| 807 | result = pop3_perform_upgrade_tls(conn);
|
---|
| 808 |
|
---|
| 809 | return result;
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | /* For SASL authentication responses */
|
---|
| 813 | static CURLcode pop3_state_auth_resp(struct connectdata *conn,
|
---|
| 814 | int pop3code,
|
---|
| 815 | pop3state instate)
|
---|
| 816 | {
|
---|
| 817 | CURLcode result = CURLE_OK;
|
---|
| 818 | struct SessionHandle *data = conn->data;
|
---|
| 819 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 820 | saslprogress progress;
|
---|
| 821 |
|
---|
| 822 | (void)instate; /* no use for this yet */
|
---|
| 823 |
|
---|
| 824 | result = Curl_sasl_continue(&pop3c->sasl, conn, pop3code, &progress);
|
---|
| 825 | if(!result)
|
---|
| 826 | switch(progress) {
|
---|
| 827 | case SASL_DONE:
|
---|
| 828 | state(conn, POP3_STOP); /* Authenticated */
|
---|
| 829 | break;
|
---|
| 830 | case SASL_IDLE: /* No mechanism left after cancellation */
|
---|
| 831 | #ifndef CURL_DISABLE_CRYPTO_AUTH
|
---|
| 832 | if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
|
---|
| 833 | /* Perform APOP authentication */
|
---|
| 834 | result = pop3_perform_apop(conn);
|
---|
| 835 | else
|
---|
| 836 | #endif
|
---|
| 837 | if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
|
---|
| 838 | /* Perform clear text authentication */
|
---|
| 839 | result = pop3_perform_user(conn);
|
---|
| 840 | else {
|
---|
| 841 | failf(data, "Authentication cancelled");
|
---|
| 842 | result = CURLE_LOGIN_DENIED;
|
---|
| 843 | }
|
---|
| 844 | break;
|
---|
| 845 | default:
|
---|
| 846 | break;
|
---|
| 847 | }
|
---|
| 848 |
|
---|
| 849 | return result;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | #ifndef CURL_DISABLE_CRYPTO_AUTH
|
---|
| 853 | /* For APOP responses */
|
---|
| 854 | static CURLcode pop3_state_apop_resp(struct connectdata *conn, int pop3code,
|
---|
| 855 | pop3state instate)
|
---|
| 856 | {
|
---|
| 857 | CURLcode result = CURLE_OK;
|
---|
| 858 | struct SessionHandle *data = conn->data;
|
---|
| 859 |
|
---|
| 860 | (void)instate; /* no use for this yet */
|
---|
| 861 |
|
---|
| 862 | if(pop3code != '+') {
|
---|
| 863 | failf(data, "Authentication failed: %d", pop3code);
|
---|
| 864 | result = CURLE_LOGIN_DENIED;
|
---|
| 865 | }
|
---|
| 866 | else
|
---|
| 867 | /* End of connect phase */
|
---|
| 868 | state(conn, POP3_STOP);
|
---|
| 869 |
|
---|
| 870 | return result;
|
---|
| 871 | }
|
---|
| 872 | #endif
|
---|
| 873 |
|
---|
| 874 | /* For USER responses */
|
---|
| 875 | static CURLcode pop3_state_user_resp(struct connectdata *conn, int pop3code,
|
---|
| 876 | pop3state instate)
|
---|
| 877 | {
|
---|
| 878 | CURLcode result = CURLE_OK;
|
---|
| 879 | struct SessionHandle *data = conn->data;
|
---|
| 880 |
|
---|
| 881 | (void)instate; /* no use for this yet */
|
---|
| 882 |
|
---|
| 883 | if(pop3code != '+') {
|
---|
| 884 | failf(data, "Access denied. %c", pop3code);
|
---|
| 885 | result = CURLE_LOGIN_DENIED;
|
---|
| 886 | }
|
---|
| 887 | else
|
---|
| 888 | /* Send the PASS command */
|
---|
| 889 | result = Curl_pp_sendf(&conn->proto.pop3c.pp, "PASS %s",
|
---|
| 890 | conn->passwd ? conn->passwd : "");
|
---|
| 891 | if(!result)
|
---|
| 892 | state(conn, POP3_PASS);
|
---|
| 893 |
|
---|
| 894 | return result;
|
---|
| 895 | }
|
---|
| 896 |
|
---|
| 897 | /* For PASS responses */
|
---|
| 898 | static CURLcode pop3_state_pass_resp(struct connectdata *conn, int pop3code,
|
---|
| 899 | pop3state instate)
|
---|
| 900 | {
|
---|
| 901 | CURLcode result = CURLE_OK;
|
---|
| 902 | struct SessionHandle *data = conn->data;
|
---|
| 903 |
|
---|
| 904 | (void)instate; /* no use for this yet */
|
---|
| 905 |
|
---|
| 906 | if(pop3code != '+') {
|
---|
| 907 | failf(data, "Access denied. %c", pop3code);
|
---|
| 908 | result = CURLE_LOGIN_DENIED;
|
---|
| 909 | }
|
---|
| 910 | else
|
---|
| 911 | /* End of connect phase */
|
---|
| 912 | state(conn, POP3_STOP);
|
---|
| 913 |
|
---|
| 914 | return result;
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | /* For command responses */
|
---|
| 918 | static CURLcode pop3_state_command_resp(struct connectdata *conn,
|
---|
| 919 | int pop3code,
|
---|
| 920 | pop3state instate)
|
---|
| 921 | {
|
---|
| 922 | CURLcode result = CURLE_OK;
|
---|
| 923 | struct SessionHandle *data = conn->data;
|
---|
| 924 | struct POP3 *pop3 = data->req.protop;
|
---|
| 925 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 926 | struct pingpong *pp = &pop3c->pp;
|
---|
| 927 |
|
---|
| 928 | (void)instate; /* no use for this yet */
|
---|
| 929 |
|
---|
| 930 | if(pop3code != '+') {
|
---|
| 931 | state(conn, POP3_STOP);
|
---|
| 932 | return CURLE_RECV_ERROR;
|
---|
| 933 | }
|
---|
| 934 |
|
---|
| 935 | /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
|
---|
| 936 | EOB string so count this is two matching bytes. This is necessary to make
|
---|
| 937 | the code detect the EOB if the only data than comes now is %2e CR LF like
|
---|
| 938 | when there is no body to return. */
|
---|
| 939 | pop3c->eob = 2;
|
---|
| 940 |
|
---|
| 941 | /* But since this initial CR LF pair is not part of the actual body, we set
|
---|
| 942 | the strip counter here so that these bytes won't be delivered. */
|
---|
| 943 | pop3c->strip = 2;
|
---|
| 944 |
|
---|
| 945 | if(pop3->transfer == FTPTRANSFER_BODY) {
|
---|
| 946 | /* POP3 download */
|
---|
| 947 | Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
|
---|
| 948 |
|
---|
| 949 | if(pp->cache) {
|
---|
| 950 | /* The header "cache" contains a bunch of data that is actually body
|
---|
| 951 | content so send it as such. Note that there may even be additional
|
---|
| 952 | "headers" after the body */
|
---|
| 953 |
|
---|
| 954 | if(!data->set.opt_no_body) {
|
---|
| 955 | result = Curl_pop3_write(conn, pp->cache, pp->cache_size);
|
---|
| 956 | if(result)
|
---|
| 957 | return result;
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | /* Free the cache */
|
---|
| 961 | Curl_safefree(pp->cache);
|
---|
| 962 |
|
---|
| 963 | /* Reset the cache size */
|
---|
| 964 | pp->cache_size = 0;
|
---|
| 965 | }
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | /* End of DO phase */
|
---|
| 969 | state(conn, POP3_STOP);
|
---|
| 970 |
|
---|
| 971 | return result;
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | static CURLcode pop3_statemach_act(struct connectdata *conn)
|
---|
| 975 | {
|
---|
| 976 | CURLcode result = CURLE_OK;
|
---|
| 977 | curl_socket_t sock = conn->sock[FIRSTSOCKET];
|
---|
| 978 | int pop3code;
|
---|
| 979 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 980 | struct pingpong *pp = &pop3c->pp;
|
---|
| 981 | size_t nread = 0;
|
---|
| 982 |
|
---|
| 983 | /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
|
---|
| 984 | if(pop3c->state == POP3_UPGRADETLS)
|
---|
| 985 | return pop3_perform_upgrade_tls(conn);
|
---|
| 986 |
|
---|
| 987 | /* Flush any data that needs to be sent */
|
---|
| 988 | if(pp->sendleft)
|
---|
| 989 | return Curl_pp_flushsend(pp);
|
---|
| 990 |
|
---|
| 991 | do {
|
---|
| 992 | /* Read the response from the server */
|
---|
| 993 | result = Curl_pp_readresp(sock, pp, &pop3code, &nread);
|
---|
| 994 | if(result)
|
---|
| 995 | return result;
|
---|
| 996 |
|
---|
| 997 | if(!pop3code)
|
---|
| 998 | break;
|
---|
| 999 |
|
---|
| 1000 | /* We have now received a full POP3 server response */
|
---|
| 1001 | switch(pop3c->state) {
|
---|
| 1002 | case POP3_SERVERGREET:
|
---|
| 1003 | result = pop3_state_servergreet_resp(conn, pop3code, pop3c->state);
|
---|
| 1004 | break;
|
---|
| 1005 |
|
---|
| 1006 | case POP3_CAPA:
|
---|
| 1007 | result = pop3_state_capa_resp(conn, pop3code, pop3c->state);
|
---|
| 1008 | break;
|
---|
| 1009 |
|
---|
| 1010 | case POP3_STARTTLS:
|
---|
| 1011 | result = pop3_state_starttls_resp(conn, pop3code, pop3c->state);
|
---|
| 1012 | break;
|
---|
| 1013 |
|
---|
| 1014 | case POP3_AUTH:
|
---|
| 1015 | result = pop3_state_auth_resp(conn, pop3code, pop3c->state);
|
---|
| 1016 | break;
|
---|
| 1017 |
|
---|
| 1018 | #ifndef CURL_DISABLE_CRYPTO_AUTH
|
---|
| 1019 | case POP3_APOP:
|
---|
| 1020 | result = pop3_state_apop_resp(conn, pop3code, pop3c->state);
|
---|
| 1021 | break;
|
---|
| 1022 | #endif
|
---|
| 1023 |
|
---|
| 1024 | case POP3_USER:
|
---|
| 1025 | result = pop3_state_user_resp(conn, pop3code, pop3c->state);
|
---|
| 1026 | break;
|
---|
| 1027 |
|
---|
| 1028 | case POP3_PASS:
|
---|
| 1029 | result = pop3_state_pass_resp(conn, pop3code, pop3c->state);
|
---|
| 1030 | break;
|
---|
| 1031 |
|
---|
| 1032 | case POP3_COMMAND:
|
---|
| 1033 | result = pop3_state_command_resp(conn, pop3code, pop3c->state);
|
---|
| 1034 | break;
|
---|
| 1035 |
|
---|
| 1036 | case POP3_QUIT:
|
---|
| 1037 | /* fallthrough, just stop! */
|
---|
| 1038 | default:
|
---|
| 1039 | /* internal error */
|
---|
| 1040 | state(conn, POP3_STOP);
|
---|
| 1041 | break;
|
---|
| 1042 | }
|
---|
| 1043 | } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
|
---|
| 1044 |
|
---|
| 1045 | return result;
|
---|
| 1046 | }
|
---|
| 1047 |
|
---|
| 1048 | /* Called repeatedly until done from multi.c */
|
---|
| 1049 | static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done)
|
---|
| 1050 | {
|
---|
| 1051 | CURLcode result = CURLE_OK;
|
---|
| 1052 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 1053 |
|
---|
| 1054 | if((conn->handler->flags & PROTOPT_SSL) && !pop3c->ssldone) {
|
---|
| 1055 | result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &pop3c->ssldone);
|
---|
| 1056 | if(result || !pop3c->ssldone)
|
---|
| 1057 | return result;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
| 1060 | result = Curl_pp_statemach(&pop3c->pp, FALSE);
|
---|
| 1061 | *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
|
---|
| 1062 |
|
---|
| 1063 | return result;
|
---|
| 1064 | }
|
---|
| 1065 |
|
---|
| 1066 | static CURLcode pop3_block_statemach(struct connectdata *conn)
|
---|
| 1067 | {
|
---|
| 1068 | CURLcode result = CURLE_OK;
|
---|
| 1069 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 1070 |
|
---|
| 1071 | while(pop3c->state != POP3_STOP && !result)
|
---|
| 1072 | result = Curl_pp_statemach(&pop3c->pp, TRUE);
|
---|
| 1073 |
|
---|
| 1074 | return result;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | /* Allocate and initialize the POP3 struct for the current SessionHandle if
|
---|
| 1078 | required */
|
---|
| 1079 | static CURLcode pop3_init(struct connectdata *conn)
|
---|
| 1080 | {
|
---|
| 1081 | CURLcode result = CURLE_OK;
|
---|
| 1082 | struct SessionHandle *data = conn->data;
|
---|
| 1083 | struct POP3 *pop3;
|
---|
| 1084 |
|
---|
| 1085 | pop3 = data->req.protop = calloc(sizeof(struct POP3), 1);
|
---|
| 1086 | if(!pop3)
|
---|
| 1087 | result = CURLE_OUT_OF_MEMORY;
|
---|
| 1088 |
|
---|
| 1089 | return result;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 | /* For the POP3 "protocol connect" and "doing" phases only */
|
---|
| 1093 | static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
|
---|
| 1094 | int numsocks)
|
---|
| 1095 | {
|
---|
| 1096 | return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks);
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 | /***********************************************************************
|
---|
| 1100 | *
|
---|
| 1101 | * pop3_connect()
|
---|
| 1102 | *
|
---|
| 1103 | * This function should do everything that is to be considered a part of the
|
---|
| 1104 | * connection phase.
|
---|
| 1105 | *
|
---|
| 1106 | * The variable 'done' points to will be TRUE if the protocol-layer connect
|
---|
| 1107 | * phase is done when this function returns, or FALSE if not.
|
---|
| 1108 | */
|
---|
| 1109 | static CURLcode pop3_connect(struct connectdata *conn, bool *done)
|
---|
| 1110 | {
|
---|
| 1111 | CURLcode result = CURLE_OK;
|
---|
| 1112 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 1113 | struct pingpong *pp = &pop3c->pp;
|
---|
| 1114 |
|
---|
| 1115 | *done = FALSE; /* default to not done yet */
|
---|
| 1116 |
|
---|
| 1117 | /* We always support persistent connections in POP3 */
|
---|
| 1118 | connkeep(conn, "POP3 default");
|
---|
| 1119 |
|
---|
| 1120 | /* Set the default response time-out */
|
---|
| 1121 | pp->response_time = RESP_TIMEOUT;
|
---|
| 1122 | pp->statemach_act = pop3_statemach_act;
|
---|
| 1123 | pp->endofresp = pop3_endofresp;
|
---|
| 1124 | pp->conn = conn;
|
---|
| 1125 |
|
---|
| 1126 | /* Set the default preferred authentication type and mechanism */
|
---|
| 1127 | pop3c->preftype = POP3_TYPE_ANY;
|
---|
| 1128 | Curl_sasl_init(&pop3c->sasl, &saslpop3);
|
---|
| 1129 |
|
---|
| 1130 | /* Initialise the pingpong layer */
|
---|
| 1131 | Curl_pp_init(pp);
|
---|
| 1132 |
|
---|
| 1133 | /* Parse the URL options */
|
---|
| 1134 | result = pop3_parse_url_options(conn);
|
---|
| 1135 | if(result)
|
---|
| 1136 | return result;
|
---|
| 1137 |
|
---|
| 1138 | /* Start off waiting for the server greeting response */
|
---|
| 1139 | state(conn, POP3_SERVERGREET);
|
---|
| 1140 |
|
---|
| 1141 | result = pop3_multi_statemach(conn, done);
|
---|
| 1142 |
|
---|
| 1143 | return result;
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | /***********************************************************************
|
---|
| 1147 | *
|
---|
| 1148 | * pop3_done()
|
---|
| 1149 | *
|
---|
| 1150 | * The DONE function. This does what needs to be done after a single DO has
|
---|
| 1151 | * performed.
|
---|
| 1152 | *
|
---|
| 1153 | * Input argument is already checked for validity.
|
---|
| 1154 | */
|
---|
| 1155 | static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
|
---|
| 1156 | bool premature)
|
---|
| 1157 | {
|
---|
| 1158 | CURLcode result = CURLE_OK;
|
---|
| 1159 | struct SessionHandle *data = conn->data;
|
---|
| 1160 | struct POP3 *pop3 = data->req.protop;
|
---|
| 1161 |
|
---|
| 1162 | (void)premature;
|
---|
| 1163 |
|
---|
| 1164 | if(!pop3)
|
---|
| 1165 | /* When the easy handle is removed from the multi interface while libcurl
|
---|
| 1166 | is still trying to resolve the host name, the POP3 struct is not yet
|
---|
| 1167 | initialized. However, the removal action calls Curl_done() which in
|
---|
| 1168 | turn calls this function, so we simply return success. */
|
---|
| 1169 | return CURLE_OK;
|
---|
| 1170 |
|
---|
| 1171 | if(status) {
|
---|
| 1172 | connclose(conn, "POP3 done with bad status");
|
---|
| 1173 | result = status; /* use the already set error code */
|
---|
| 1174 | }
|
---|
| 1175 |
|
---|
| 1176 | /* Cleanup our per-request based variables */
|
---|
| 1177 | Curl_safefree(pop3->id);
|
---|
| 1178 | Curl_safefree(pop3->custom);
|
---|
| 1179 |
|
---|
| 1180 | /* Clear the transfer mode for the next request */
|
---|
| 1181 | pop3->transfer = FTPTRANSFER_BODY;
|
---|
| 1182 |
|
---|
| 1183 | return result;
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | /***********************************************************************
|
---|
| 1187 | *
|
---|
| 1188 | * pop3_perform()
|
---|
| 1189 | *
|
---|
| 1190 | * This is the actual DO function for POP3. Get a message/listing according to
|
---|
| 1191 | * the options previously setup.
|
---|
| 1192 | */
|
---|
| 1193 | static CURLcode pop3_perform(struct connectdata *conn, bool *connected,
|
---|
| 1194 | bool *dophase_done)
|
---|
| 1195 | {
|
---|
| 1196 | /* This is POP3 and no proxy */
|
---|
| 1197 | CURLcode result = CURLE_OK;
|
---|
| 1198 | struct POP3 *pop3 = conn->data->req.protop;
|
---|
| 1199 |
|
---|
| 1200 | DEBUGF(infof(conn->data, "DO phase starts\n"));
|
---|
| 1201 |
|
---|
| 1202 | if(conn->data->set.opt_no_body) {
|
---|
| 1203 | /* Requested no body means no transfer */
|
---|
| 1204 | pop3->transfer = FTPTRANSFER_INFO;
|
---|
| 1205 | }
|
---|
| 1206 |
|
---|
| 1207 | *dophase_done = FALSE; /* not done yet */
|
---|
| 1208 |
|
---|
| 1209 | /* Start the first command in the DO phase */
|
---|
| 1210 | result = pop3_perform_command(conn);
|
---|
| 1211 | if(result)
|
---|
| 1212 | return result;
|
---|
| 1213 |
|
---|
| 1214 | /* Run the state-machine */
|
---|
| 1215 | result = pop3_multi_statemach(conn, dophase_done);
|
---|
| 1216 |
|
---|
| 1217 | *connected = conn->bits.tcpconnect[FIRSTSOCKET];
|
---|
| 1218 |
|
---|
| 1219 | if(*dophase_done)
|
---|
| 1220 | DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
---|
| 1221 |
|
---|
| 1222 | return result;
|
---|
| 1223 | }
|
---|
| 1224 |
|
---|
| 1225 | /***********************************************************************
|
---|
| 1226 | *
|
---|
| 1227 | * pop3_do()
|
---|
| 1228 | *
|
---|
| 1229 | * This function is registered as 'curl_do' function. It decodes the path
|
---|
| 1230 | * parts etc as a wrapper to the actual DO function (pop3_perform).
|
---|
| 1231 | *
|
---|
| 1232 | * The input argument is already checked for validity.
|
---|
| 1233 | */
|
---|
| 1234 | static CURLcode pop3_do(struct connectdata *conn, bool *done)
|
---|
| 1235 | {
|
---|
| 1236 | CURLcode result = CURLE_OK;
|
---|
| 1237 |
|
---|
| 1238 | *done = FALSE; /* default to false */
|
---|
| 1239 |
|
---|
| 1240 | /* Parse the URL path */
|
---|
| 1241 | result = pop3_parse_url_path(conn);
|
---|
| 1242 | if(result)
|
---|
| 1243 | return result;
|
---|
| 1244 |
|
---|
| 1245 | /* Parse the custom request */
|
---|
| 1246 | result = pop3_parse_custom_request(conn);
|
---|
| 1247 | if(result)
|
---|
| 1248 | return result;
|
---|
| 1249 |
|
---|
| 1250 | result = pop3_regular_transfer(conn, done);
|
---|
| 1251 |
|
---|
| 1252 | return result;
|
---|
| 1253 | }
|
---|
| 1254 |
|
---|
| 1255 | /***********************************************************************
|
---|
| 1256 | *
|
---|
| 1257 | * pop3_disconnect()
|
---|
| 1258 | *
|
---|
| 1259 | * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
|
---|
| 1260 | * resources. BLOCKING.
|
---|
| 1261 | */
|
---|
| 1262 | static CURLcode pop3_disconnect(struct connectdata *conn, bool dead_connection)
|
---|
| 1263 | {
|
---|
| 1264 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 1265 |
|
---|
| 1266 | /* We cannot send quit unconditionally. If this connection is stale or
|
---|
| 1267 | bad in any way, sending quit and waiting around here will make the
|
---|
| 1268 | disconnect wait in vain and cause more problems than we need to. */
|
---|
| 1269 |
|
---|
| 1270 | /* The POP3 session may or may not have been allocated/setup at this
|
---|
| 1271 | point! */
|
---|
| 1272 | if(!dead_connection && pop3c->pp.conn && pop3c->pp.conn->bits.protoconnstart)
|
---|
| 1273 | if(!pop3_perform_quit(conn))
|
---|
| 1274 | (void)pop3_block_statemach(conn); /* ignore errors on QUIT */
|
---|
| 1275 |
|
---|
| 1276 | /* Disconnect from the server */
|
---|
| 1277 | Curl_pp_disconnect(&pop3c->pp);
|
---|
| 1278 |
|
---|
| 1279 | /* Cleanup the SASL module */
|
---|
| 1280 | Curl_sasl_cleanup(conn, pop3c->sasl.authused);
|
---|
| 1281 |
|
---|
| 1282 | /* Cleanup our connection based variables */
|
---|
| 1283 | Curl_safefree(pop3c->apoptimestamp);
|
---|
| 1284 |
|
---|
| 1285 | return CURLE_OK;
|
---|
| 1286 | }
|
---|
| 1287 |
|
---|
| 1288 | /* Call this when the DO phase has completed */
|
---|
| 1289 | static CURLcode pop3_dophase_done(struct connectdata *conn, bool connected)
|
---|
| 1290 | {
|
---|
| 1291 | (void)conn;
|
---|
| 1292 | (void)connected;
|
---|
| 1293 |
|
---|
| 1294 | return CURLE_OK;
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 | /* Called from multi.c while DOing */
|
---|
| 1298 | static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done)
|
---|
| 1299 | {
|
---|
| 1300 | CURLcode result = pop3_multi_statemach(conn, dophase_done);
|
---|
| 1301 |
|
---|
| 1302 | if(result)
|
---|
| 1303 | DEBUGF(infof(conn->data, "DO phase failed\n"));
|
---|
| 1304 | else if(*dophase_done) {
|
---|
| 1305 | result = pop3_dophase_done(conn, FALSE /* not connected */);
|
---|
| 1306 |
|
---|
| 1307 | DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
---|
| 1308 | }
|
---|
| 1309 |
|
---|
| 1310 | return result;
|
---|
| 1311 | }
|
---|
| 1312 |
|
---|
| 1313 | /***********************************************************************
|
---|
| 1314 | *
|
---|
| 1315 | * pop3_regular_transfer()
|
---|
| 1316 | *
|
---|
| 1317 | * The input argument is already checked for validity.
|
---|
| 1318 | *
|
---|
| 1319 | * Performs all commands done before a regular transfer between a local and a
|
---|
| 1320 | * remote host.
|
---|
| 1321 | */
|
---|
| 1322 | static CURLcode pop3_regular_transfer(struct connectdata *conn,
|
---|
| 1323 | bool *dophase_done)
|
---|
| 1324 | {
|
---|
| 1325 | CURLcode result = CURLE_OK;
|
---|
| 1326 | bool connected = FALSE;
|
---|
| 1327 | struct SessionHandle *data = conn->data;
|
---|
| 1328 |
|
---|
| 1329 | /* Make sure size is unknown at this point */
|
---|
| 1330 | data->req.size = -1;
|
---|
| 1331 |
|
---|
| 1332 | /* Set the progress data */
|
---|
| 1333 | Curl_pgrsSetUploadCounter(data, 0);
|
---|
| 1334 | Curl_pgrsSetDownloadCounter(data, 0);
|
---|
| 1335 | Curl_pgrsSetUploadSize(data, -1);
|
---|
| 1336 | Curl_pgrsSetDownloadSize(data, -1);
|
---|
| 1337 |
|
---|
| 1338 | /* Carry out the perform */
|
---|
| 1339 | result = pop3_perform(conn, &connected, dophase_done);
|
---|
| 1340 |
|
---|
| 1341 | /* Perform post DO phase operations if necessary */
|
---|
| 1342 | if(!result && *dophase_done)
|
---|
| 1343 | result = pop3_dophase_done(conn, connected);
|
---|
| 1344 |
|
---|
| 1345 | return result;
|
---|
| 1346 | }
|
---|
| 1347 |
|
---|
| 1348 | static CURLcode pop3_setup_connection(struct connectdata *conn)
|
---|
| 1349 | {
|
---|
| 1350 | struct SessionHandle *data = conn->data;
|
---|
| 1351 |
|
---|
| 1352 | /* Initialise the POP3 layer */
|
---|
| 1353 | CURLcode result = pop3_init(conn);
|
---|
| 1354 | if(result)
|
---|
| 1355 | return result;
|
---|
| 1356 |
|
---|
| 1357 | if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
|
---|
| 1358 | /* Unless we have asked to tunnel POP3 operations through the proxy, we
|
---|
| 1359 | switch and use HTTP operations only */
|
---|
| 1360 | #ifndef CURL_DISABLE_HTTP
|
---|
| 1361 | if(conn->handler == &Curl_handler_pop3)
|
---|
| 1362 | conn->handler = &Curl_handler_pop3_proxy;
|
---|
| 1363 | else {
|
---|
| 1364 | #ifdef USE_SSL
|
---|
| 1365 | conn->handler = &Curl_handler_pop3s_proxy;
|
---|
| 1366 | #else
|
---|
| 1367 | failf(data, "POP3S not supported!");
|
---|
| 1368 | return CURLE_UNSUPPORTED_PROTOCOL;
|
---|
| 1369 | #endif
|
---|
| 1370 | }
|
---|
| 1371 |
|
---|
| 1372 | /* set it up as an HTTP connection instead */
|
---|
| 1373 | return conn->handler->setup_connection(conn);
|
---|
| 1374 | #else
|
---|
| 1375 | failf(data, "POP3 over http proxy requires HTTP support built-in!");
|
---|
| 1376 | return CURLE_UNSUPPORTED_PROTOCOL;
|
---|
| 1377 | #endif
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
| 1380 | data->state.path++; /* don't include the initial slash */
|
---|
| 1381 |
|
---|
| 1382 | return CURLE_OK;
|
---|
| 1383 | }
|
---|
| 1384 |
|
---|
| 1385 | /***********************************************************************
|
---|
| 1386 | *
|
---|
| 1387 | * pop3_parse_url_options()
|
---|
| 1388 | *
|
---|
| 1389 | * Parse the URL login options.
|
---|
| 1390 | */
|
---|
| 1391 | static CURLcode pop3_parse_url_options(struct connectdata *conn)
|
---|
| 1392 | {
|
---|
| 1393 | CURLcode result = CURLE_OK;
|
---|
| 1394 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 1395 | const char *ptr = conn->options;
|
---|
| 1396 |
|
---|
| 1397 | pop3c->sasl.resetprefs = TRUE;
|
---|
| 1398 |
|
---|
| 1399 | while(!result && ptr && *ptr) {
|
---|
| 1400 | const char *key = ptr;
|
---|
| 1401 | const char *value;
|
---|
| 1402 |
|
---|
| 1403 | while(*ptr && *ptr != '=')
|
---|
| 1404 | ptr++;
|
---|
| 1405 |
|
---|
| 1406 | value = ptr + 1;
|
---|
| 1407 |
|
---|
| 1408 | while(*ptr && *ptr != ';')
|
---|
| 1409 | ptr++;
|
---|
| 1410 |
|
---|
| 1411 | if(strnequal(key, "AUTH=", 5)) {
|
---|
| 1412 | result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
|
---|
| 1413 | value, ptr - value);
|
---|
| 1414 |
|
---|
| 1415 | if(result && strnequal(value, "+APOP", ptr - value)) {
|
---|
| 1416 | pop3c->preftype = POP3_TYPE_APOP;
|
---|
| 1417 | pop3c->sasl.prefmech = SASL_AUTH_NONE;
|
---|
| 1418 | result = CURLE_OK;
|
---|
| 1419 | }
|
---|
| 1420 | }
|
---|
| 1421 | else
|
---|
| 1422 | result = CURLE_URL_MALFORMAT;
|
---|
| 1423 |
|
---|
| 1424 | if(*ptr == ';')
|
---|
| 1425 | ptr++;
|
---|
| 1426 | }
|
---|
| 1427 |
|
---|
| 1428 | if(pop3c->preftype != POP3_TYPE_APOP)
|
---|
| 1429 | switch(pop3c->sasl.prefmech) {
|
---|
| 1430 | case SASL_AUTH_NONE:
|
---|
| 1431 | pop3c->preftype = POP3_TYPE_NONE;
|
---|
| 1432 | break;
|
---|
| 1433 | case SASL_AUTH_DEFAULT:
|
---|
| 1434 | pop3c->preftype = POP3_TYPE_ANY;
|
---|
| 1435 | break;
|
---|
| 1436 | default:
|
---|
| 1437 | pop3c->preftype = POP3_TYPE_SASL;
|
---|
| 1438 | break;
|
---|
| 1439 | }
|
---|
| 1440 |
|
---|
| 1441 | return result;
|
---|
| 1442 | }
|
---|
| 1443 |
|
---|
| 1444 | /***********************************************************************
|
---|
| 1445 | *
|
---|
| 1446 | * pop3_parse_url_path()
|
---|
| 1447 | *
|
---|
| 1448 | * Parse the URL path into separate path components.
|
---|
| 1449 | */
|
---|
| 1450 | static CURLcode pop3_parse_url_path(struct connectdata *conn)
|
---|
| 1451 | {
|
---|
| 1452 | /* The POP3 struct is already initialised in pop3_connect() */
|
---|
| 1453 | struct SessionHandle *data = conn->data;
|
---|
| 1454 | struct POP3 *pop3 = data->req.protop;
|
---|
| 1455 | const char *path = data->state.path;
|
---|
| 1456 |
|
---|
| 1457 | /* URL decode the path for the message ID */
|
---|
| 1458 | return Curl_urldecode(data, path, 0, &pop3->id, NULL, TRUE);
|
---|
| 1459 | }
|
---|
| 1460 |
|
---|
| 1461 | /***********************************************************************
|
---|
| 1462 | *
|
---|
| 1463 | * pop3_parse_custom_request()
|
---|
| 1464 | *
|
---|
| 1465 | * Parse the custom request.
|
---|
| 1466 | */
|
---|
| 1467 | static CURLcode pop3_parse_custom_request(struct connectdata *conn)
|
---|
| 1468 | {
|
---|
| 1469 | CURLcode result = CURLE_OK;
|
---|
| 1470 | struct SessionHandle *data = conn->data;
|
---|
| 1471 | struct POP3 *pop3 = data->req.protop;
|
---|
| 1472 | const char *custom = data->set.str[STRING_CUSTOMREQUEST];
|
---|
| 1473 |
|
---|
| 1474 | /* URL decode the custom request */
|
---|
| 1475 | if(custom)
|
---|
| 1476 | result = Curl_urldecode(data, custom, 0, &pop3->custom, NULL, TRUE);
|
---|
| 1477 |
|
---|
| 1478 | return result;
|
---|
| 1479 | }
|
---|
| 1480 |
|
---|
| 1481 | /***********************************************************************
|
---|
| 1482 | *
|
---|
| 1483 | * Curl_pop3_write()
|
---|
| 1484 | *
|
---|
| 1485 | * This function scans the body after the end-of-body and writes everything
|
---|
| 1486 | * until the end is found.
|
---|
| 1487 | */
|
---|
| 1488 | CURLcode Curl_pop3_write(struct connectdata *conn, char *str, size_t nread)
|
---|
| 1489 | {
|
---|
| 1490 | /* This code could be made into a special function in the handler struct */
|
---|
| 1491 | CURLcode result = CURLE_OK;
|
---|
| 1492 | struct SessionHandle *data = conn->data;
|
---|
| 1493 | struct SingleRequest *k = &data->req;
|
---|
| 1494 |
|
---|
| 1495 | struct pop3_conn *pop3c = &conn->proto.pop3c;
|
---|
| 1496 | bool strip_dot = FALSE;
|
---|
| 1497 | size_t last = 0;
|
---|
| 1498 | size_t i;
|
---|
| 1499 |
|
---|
| 1500 | /* Search through the buffer looking for the end-of-body marker which is
|
---|
| 1501 | 5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
|
---|
| 1502 | the eob so the server will have prefixed it with an extra dot which we
|
---|
| 1503 | need to strip out. Additionally the marker could of course be spread out
|
---|
| 1504 | over 5 different data chunks. */
|
---|
| 1505 | for(i = 0; i < nread; i++) {
|
---|
| 1506 | size_t prev = pop3c->eob;
|
---|
| 1507 |
|
---|
| 1508 | switch(str[i]) {
|
---|
| 1509 | case 0x0d:
|
---|
| 1510 | if(pop3c->eob == 0) {
|
---|
| 1511 | pop3c->eob++;
|
---|
| 1512 |
|
---|
| 1513 | if(i) {
|
---|
| 1514 | /* Write out the body part that didn't match */
|
---|
| 1515 | result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
|
---|
| 1516 | i - last);
|
---|
| 1517 |
|
---|
| 1518 | if(result)
|
---|
| 1519 | return result;
|
---|
| 1520 |
|
---|
| 1521 | last = i;
|
---|
| 1522 | }
|
---|
| 1523 | }
|
---|
| 1524 | else if(pop3c->eob == 3)
|
---|
| 1525 | pop3c->eob++;
|
---|
| 1526 | else
|
---|
| 1527 | /* If the character match wasn't at position 0 or 3 then restart the
|
---|
| 1528 | pattern matching */
|
---|
| 1529 | pop3c->eob = 1;
|
---|
| 1530 | break;
|
---|
| 1531 |
|
---|
| 1532 | case 0x0a:
|
---|
| 1533 | if(pop3c->eob == 1 || pop3c->eob == 4)
|
---|
| 1534 | pop3c->eob++;
|
---|
| 1535 | else
|
---|
| 1536 | /* If the character match wasn't at position 1 or 4 then start the
|
---|
| 1537 | search again */
|
---|
| 1538 | pop3c->eob = 0;
|
---|
| 1539 | break;
|
---|
| 1540 |
|
---|
| 1541 | case 0x2e:
|
---|
| 1542 | if(pop3c->eob == 2)
|
---|
| 1543 | pop3c->eob++;
|
---|
| 1544 | else if(pop3c->eob == 3) {
|
---|
| 1545 | /* We have an extra dot after the CRLF which we need to strip off */
|
---|
| 1546 | strip_dot = TRUE;
|
---|
| 1547 | pop3c->eob = 0;
|
---|
| 1548 | }
|
---|
| 1549 | else
|
---|
| 1550 | /* If the character match wasn't at position 2 then start the search
|
---|
| 1551 | again */
|
---|
| 1552 | pop3c->eob = 0;
|
---|
| 1553 | break;
|
---|
| 1554 |
|
---|
| 1555 | default:
|
---|
| 1556 | pop3c->eob = 0;
|
---|
| 1557 | break;
|
---|
| 1558 | }
|
---|
| 1559 |
|
---|
| 1560 | /* Did we have a partial match which has subsequently failed? */
|
---|
| 1561 | if(prev && prev >= pop3c->eob) {
|
---|
| 1562 | /* Strip can only be non-zero for the very first mismatch after CRLF
|
---|
| 1563 | and then both prev and strip are equal and nothing will be output
|
---|
| 1564 | below */
|
---|
| 1565 | while(prev && pop3c->strip) {
|
---|
| 1566 | prev--;
|
---|
| 1567 | pop3c->strip--;
|
---|
| 1568 | }
|
---|
| 1569 |
|
---|
| 1570 | if(prev) {
|
---|
| 1571 | /* If the partial match was the CRLF and dot then only write the CRLF
|
---|
| 1572 | as the server would have inserted the dot */
|
---|
| 1573 | result = Curl_client_write(conn, CLIENTWRITE_BODY, (char*)POP3_EOB,
|
---|
| 1574 | strip_dot ? prev - 1 : prev);
|
---|
| 1575 |
|
---|
| 1576 | if(result)
|
---|
| 1577 | return result;
|
---|
| 1578 |
|
---|
| 1579 | last = i;
|
---|
| 1580 | strip_dot = FALSE;
|
---|
| 1581 | }
|
---|
| 1582 | }
|
---|
| 1583 | }
|
---|
| 1584 |
|
---|
| 1585 | if(pop3c->eob == POP3_EOB_LEN) {
|
---|
| 1586 | /* We have a full match so the transfer is done, however we must transfer
|
---|
| 1587 | the CRLF at the start of the EOB as this is considered to be part of the
|
---|
| 1588 | message as per RFC-1939, sect. 3 */
|
---|
| 1589 | result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)POP3_EOB, 2);
|
---|
| 1590 |
|
---|
| 1591 | k->keepon &= ~KEEP_RECV;
|
---|
| 1592 | pop3c->eob = 0;
|
---|
| 1593 |
|
---|
| 1594 | return result;
|
---|
| 1595 | }
|
---|
| 1596 |
|
---|
| 1597 | if(pop3c->eob)
|
---|
| 1598 | /* While EOB is matching nothing should be output */
|
---|
| 1599 | return CURLE_OK;
|
---|
| 1600 |
|
---|
| 1601 | if(nread - last) {
|
---|
| 1602 | result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
|
---|
| 1603 | nread - last);
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
| 1606 | return result;
|
---|
| 1607 | }
|
---|
| 1608 |
|
---|
| 1609 | #endif /* CURL_DISABLE_POP3 */
|
---|