| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#include "curl_setup.h" |
| 41 |
|
| 42 |
#ifndef CURL_DISABLE_POP3 |
| 43 |
|
| 44 |
#ifdef HAVE_NETINET_IN_H |
| 45 |
#include <netinet/in.h> |
| 46 |
#endif |
| 47 |
#ifdef HAVE_ARPA_INET_H |
| 48 |
#include <arpa/inet.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 |
#include <curl/curl.h> |
| 59 |
#include "urldata.h" |
| 60 |
#include "sendf.h" |
| 61 |
#include "hostip.h" |
| 62 |
#include "progress.h" |
| 63 |
#include "transfer.h" |
| 64 |
#include "escape.h" |
| 65 |
#include "http.h" |
| 66 |
#include "socks.h" |
| 67 |
#include "pingpong.h" |
| 68 |
#include "pop3.h" |
| 69 |
#include "strcase.h" |
| 70 |
#include "vtls/vtls.h" |
| 71 |
#include "cfilters.h" |
| 72 |
#include "connect.h" |
| 73 |
#include "select.h" |
| 74 |
#include "multiif.h" |
| 75 |
#include "url.h" |
| 76 |
#include "bufref.h" |
| 77 |
#include "curl_sasl.h" |
| 78 |
#include "curl_md5.h" |
| 79 |
#include "curlx/warnless.h" |
| 80 |
#include "strdup.h" |
| 81 |
|
| 82 |
#include "curl_printf.h" |
| 83 |
#include "curl_memory.h" |
| 84 |
#include "memdebug.h" |
| 85 |
|
| 86 |
|
| 87 |
#define POP3_TYPE_CLEARTEXT (1 << 0) |
| 88 |
#define POP3_TYPE_APOP (1 << 1) |
| 89 |
#define POP3_TYPE_SASL (1 << 2) |
| 90 |
|
| 91 |
|
| 92 |
#define POP3_TYPE_NONE 0 |
| 93 |
#define POP3_TYPE_ANY (POP3_TYPE_CLEARTEXT|POP3_TYPE_APOP|POP3_TYPE_SASL) |
| 94 |
|
| 95 |
|
| 96 |
#define POP3_EOB "\x0d\x0a\x2e\x0d\x0a" |
| 97 |
#define POP3_EOB_LEN 5 |
| 98 |
|
| 99 |
|
| 100 |
#define CURL_META_POP3_EASY "meta:proto:pop3:easy" |
| 101 |
|
| 102 |
#define CURL_META_POP3_CONN "meta:proto:pop3:conn" |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
struct POP3 { |
| 108 |
curl_pp_transfer transfer; |
| 109 |
char *id; |
| 110 |
char *custom; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
typedef enum { |
| 117 |
POP3_STOP, |
| 118 |
POP3_SERVERGREET, |
| 119 |
|
| 120 |
POP3_CAPA, |
| 121 |
POP3_STARTTLS, |
| 122 |
POP3_UPGRADETLS, |
| 123 |
|
| 124 |
POP3_AUTH, |
| 125 |
POP3_APOP, |
| 126 |
POP3_USER, |
| 127 |
POP3_PASS, |
| 128 |
POP3_COMMAND, |
| 129 |
POP3_QUIT, |
| 130 |
POP3_LAST |
| 131 |
} pop3state; |
| 132 |
|
| 133 |
struct pop3_conn { |
| 134 |
struct pingpong pp; |
| 135 |
pop3state state; |
| 136 |
size_t eob; |
| 137 |
|
| 138 |
size_t strip; |
| 139 |
|
| 140 |
struct SASL sasl; |
| 141 |
char *apoptimestamp; |
| 142 |
unsigned char authtypes; |
| 143 |
unsigned char preftype; |
| 144 |
BIT(ssldone); |
| 145 |
BIT(tls_supported); |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
static CURLcode pop3_regular_transfer(struct Curl_easy *data, bool *done); |
| 150 |
static CURLcode pop3_do(struct Curl_easy *data, bool *done); |
| 151 |
static CURLcode pop3_done(struct Curl_easy *data, CURLcode status, |
| 152 |
bool premature); |
| 153 |
static CURLcode pop3_connect(struct Curl_easy *data, bool *done); |
| 154 |
static CURLcode pop3_disconnect(struct Curl_easy *data, |
| 155 |
struct connectdata *conn, bool dead); |
| 156 |
static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done); |
| 157 |
static int pop3_getsock(struct Curl_easy *data, |
| 158 |
struct connectdata *conn, curl_socket_t *socks); |
| 159 |
static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done); |
| 160 |
static CURLcode pop3_setup_connection(struct Curl_easy *data, |
| 161 |
struct connectdata *conn); |
| 162 |
static CURLcode pop3_parse_url_options(struct connectdata *conn); |
| 163 |
static CURLcode pop3_parse_url_path(struct Curl_easy *data); |
| 164 |
static CURLcode pop3_parse_custom_request(struct Curl_easy *data); |
| 165 |
static CURLcode pop3_perform_auth(struct Curl_easy *data, const char *mech, |
| 166 |
const struct bufref *initresp); |
| 167 |
static CURLcode pop3_continue_auth(struct Curl_easy *data, const char *mech, |
| 168 |
const struct bufref *resp); |
| 169 |
static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech); |
| 170 |
static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static CURLcode pop3_write(struct Curl_easy *data, |
| 175 |
const char *str, size_t nread, bool is_eos); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
const struct Curl_handler Curl_handler_pop3 = { |
| 182 |
"pop3", |
| 183 |
pop3_setup_connection, |
| 184 |
pop3_do, |
| 185 |
pop3_done, |
| 186 |
ZERO_NULL, |
| 187 |
pop3_connect, |
| 188 |
pop3_multi_statemach, |
| 189 |
pop3_doing, |
| 190 |
pop3_getsock, |
| 191 |
pop3_getsock, |
| 192 |
ZERO_NULL, |
| 193 |
ZERO_NULL, |
| 194 |
pop3_disconnect, |
| 195 |
pop3_write, |
| 196 |
ZERO_NULL, |
| 197 |
ZERO_NULL, |
| 198 |
ZERO_NULL, |
| 199 |
ZERO_NULL, |
| 200 |
PORT_POP3, |
| 201 |
CURLPROTO_POP3, |
| 202 |
CURLPROTO_POP3, |
| 203 |
PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | |
| 204 |
PROTOPT_URLOPTIONS |
| 205 |
}; |
| 206 |
|
| 207 |
#ifdef USE_SSL |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
const struct Curl_handler Curl_handler_pop3s = { |
| 213 |
"pop3s", |
| 214 |
pop3_setup_connection, |
| 215 |
pop3_do, |
| 216 |
pop3_done, |
| 217 |
ZERO_NULL, |
| 218 |
pop3_connect, |
| 219 |
pop3_multi_statemach, |
| 220 |
pop3_doing, |
| 221 |
pop3_getsock, |
| 222 |
pop3_getsock, |
| 223 |
ZERO_NULL, |
| 224 |
ZERO_NULL, |
| 225 |
pop3_disconnect, |
| 226 |
pop3_write, |
| 227 |
ZERO_NULL, |
| 228 |
ZERO_NULL, |
| 229 |
ZERO_NULL, |
| 230 |
ZERO_NULL, |
| 231 |
PORT_POP3S, |
| 232 |
CURLPROTO_POP3S, |
| 233 |
CURLPROTO_POP3, |
| 234 |
PROTOPT_CLOSEACTION | PROTOPT_SSL |
| 235 |
| PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS |
| 236 |
}; |
| 237 |
#endif |
| 238 |
|
| 239 |
|
| 240 |
static const struct SASLproto saslpop3 = { |
| 241 |
"pop", |
| 242 |
pop3_perform_auth, |
| 243 |
pop3_continue_auth, |
| 244 |
pop3_cancel_auth, |
| 245 |
pop3_get_message, |
| 246 |
255 - 8, |
| 247 |
'*', |
| 248 |
'+', |
| 249 |
SASL_AUTH_DEFAULT, |
| 250 |
SASL_FLAG_BASE64 |
| 251 |
}; |
| 252 |
|
| 253 |
struct pop3_cmd { |
| 254 |
const char *name; |
| 255 |
unsigned short nlen; |
| 256 |
BIT(multiline); |
| 257 |
BIT(multiline_with_args); |
| 258 |
}; |
| 259 |
|
| 260 |
static const struct pop3_cmd pop3cmds[] = { |
| 261 |
{ "APOP", 4, FALSE, FALSE }, |
| 262 |
{ "AUTH", 4, FALSE, FALSE }, |
| 263 |
{ "CAPA", 4, TRUE, TRUE }, |
| 264 |
{ "DELE", 4, FALSE, FALSE }, |
| 265 |
{ "LIST", 4, TRUE, FALSE }, |
| 266 |
{ "MSG", 3, TRUE, TRUE }, |
| 267 |
{ "NOOP", 4, FALSE, FALSE }, |
| 268 |
{ "PASS", 4, FALSE, FALSE }, |
| 269 |
{ "QUIT", 4, FALSE, FALSE }, |
| 270 |
{ "RETR", 4, TRUE, TRUE }, |
| 271 |
{ "RSET", 4, FALSE, FALSE }, |
| 272 |
{ "STAT", 4, FALSE, FALSE }, |
| 273 |
{ "STLS", 4, FALSE, FALSE }, |
| 274 |
{ "TOP", 3, TRUE, TRUE }, |
| 275 |
{ "UIDL", 4, TRUE, FALSE }, |
| 276 |
{ "USER", 4, FALSE, FALSE }, |
| 277 |
{ "UTF8", 4, FALSE, FALSE }, |
| 278 |
{ "XTND", 4, TRUE, TRUE }, |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static bool pop3_is_multiline(const char *cmdline) |
| 285 |
{ |
| 286 |
size_t i; |
| 287 |
for(i = 0; i < CURL_ARRAYSIZE(pop3cmds); ++i) { |
| 288 |
if(strncasecompare(pop3cmds[i].name, cmdline, pop3cmds[i].nlen)) { |
| 289 |
if(!cmdline[pop3cmds[i].nlen]) |
| 290 |
return pop3cmds[i].multiline; |
| 291 |
else if(cmdline[pop3cmds[i].nlen] == ' ') |
| 292 |
return pop3cmds[i].multiline_with_args; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
return TRUE; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static bool pop3_endofresp(struct Curl_easy *data, struct connectdata *conn, |
| 310 |
const char *line, size_t len, int *resp) |
| 311 |
{ |
| 312 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 313 |
(void)data; |
| 314 |
DEBUGASSERT(pop3c); |
| 315 |
if(!pop3c) |
| 316 |
return TRUE; |
| 317 |
|
| 318 |
|
| 319 |
if(len >= 4 && !memcmp("-ERR", line, 4)) { |
| 320 |
*resp = '-'; |
| 321 |
|
| 322 |
return TRUE; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
if(pop3c->state == POP3_CAPA) { |
| 327 |
|
| 328 |
if(len >= 1 && line[0] == '.') |
| 329 |
|
| 330 |
*resp = '+'; |
| 331 |
else |
| 332 |
|
| 333 |
*resp = '*'; |
| 334 |
|
| 335 |
return TRUE; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if(len >= 3 && !memcmp("+OK", line, 3)) { |
| 340 |
*resp = '+'; |
| 341 |
|
| 342 |
return TRUE; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
if(len >= 1 && line[0] == '+') { |
| 347 |
*resp = '*'; |
| 348 |
|
| 349 |
return TRUE; |
| 350 |
} |
| 351 |
|
| 352 |
return FALSE; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out) |
| 362 |
{ |
| 363 |
struct pop3_conn *pop3c = |
| 364 |
Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN); |
| 365 |
char *message; |
| 366 |
size_t len; |
| 367 |
|
| 368 |
if(!pop3c) |
| 369 |
return CURLE_FAILED_INIT; |
| 370 |
message = curlx_dyn_ptr(&pop3c->pp.recvbuf); |
| 371 |
len = pop3c->pp.nfinal; |
| 372 |
if(len > 2) { |
| 373 |
|
| 374 |
len -= 2; |
| 375 |
for(message += 2; *message == ' ' || *message == '\t'; message++, len--) |
| 376 |
; |
| 377 |
|
| 378 |
|
| 379 |
while(len--) |
| 380 |
if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' && |
| 381 |
message[len] != '\t') |
| 382 |
break; |
| 383 |
|
| 384 |
|
| 385 |
message[++len] = '\0'; |
| 386 |
Curl_bufref_set(out, message, len, NULL); |
| 387 |
} |
| 388 |
else |
| 389 |
|
| 390 |
Curl_bufref_set(out, "", 0, NULL); |
| 391 |
|
| 392 |
return CURLE_OK; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static void pop3_state(struct Curl_easy *data, pop3state newstate) |
| 402 |
{ |
| 403 |
struct pop3_conn *pop3c = |
| 404 |
Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN); |
| 405 |
if(pop3c) { |
| 406 |
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
| 407 |
|
| 408 |
static const char * const names[] = { |
| 409 |
"STOP", |
| 410 |
"SERVERGREET", |
| 411 |
"CAPA", |
| 412 |
"STARTTLS", |
| 413 |
"UPGRADETLS", |
| 414 |
"AUTH", |
| 415 |
"APOP", |
| 416 |
"USER", |
| 417 |
"PASS", |
| 418 |
"COMMAND", |
| 419 |
"QUIT", |
| 420 |
|
| 421 |
}; |
| 422 |
|
| 423 |
if(pop3c->state != newstate) |
| 424 |
infof(data, "POP3 %p state change from %s to %s", |
| 425 |
(void *)pop3c, names[pop3c->state], names[newstate]); |
| 426 |
#endif |
| 427 |
|
| 428 |
pop3c->state = newstate; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
static CURLcode pop3_perform_capa(struct Curl_easy *data, |
| 440 |
struct connectdata *conn) |
| 441 |
{ |
| 442 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 443 |
CURLcode result = CURLE_OK; |
| 444 |
|
| 445 |
if(!pop3c) |
| 446 |
return CURLE_FAILED_INIT; |
| 447 |
|
| 448 |
pop3c->sasl.authmechs = SASL_AUTH_NONE; |
| 449 |
pop3c->sasl.authused = SASL_AUTH_NONE; |
| 450 |
pop3c->tls_supported = FALSE; |
| 451 |
|
| 452 |
|
| 453 |
result = Curl_pp_sendf(data, &pop3c->pp, "%s", "CAPA"); |
| 454 |
|
| 455 |
if(!result) |
| 456 |
pop3_state(data, POP3_CAPA); |
| 457 |
|
| 458 |
return result; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
static CURLcode pop3_perform_starttls(struct Curl_easy *data, |
| 468 |
struct connectdata *conn) |
| 469 |
{ |
| 470 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 471 |
CURLcode result; |
| 472 |
|
| 473 |
if(!pop3c) |
| 474 |
return CURLE_FAILED_INIT; |
| 475 |
|
| 476 |
|
| 477 |
result = Curl_pp_sendf(data, &pop3c->pp, "%s", "STLS"); |
| 478 |
if(!result) |
| 479 |
pop3_state(data, POP3_STARTTLS); |
| 480 |
|
| 481 |
return result; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
static CURLcode pop3_perform_upgrade_tls(struct Curl_easy *data, |
| 491 |
struct connectdata *conn) |
| 492 |
{ |
| 493 |
#ifdef USE_SSL |
| 494 |
|
| 495 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 496 |
CURLcode result; |
| 497 |
bool ssldone = FALSE; |
| 498 |
|
| 499 |
if(!pop3c) |
| 500 |
return CURLE_FAILED_INIT; |
| 501 |
|
| 502 |
if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) { |
| 503 |
result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET); |
| 504 |
if(result) |
| 505 |
goto out; |
| 506 |
|
| 507 |
conn->handler = &Curl_handler_pop3s; |
| 508 |
} |
| 509 |
|
| 510 |
DEBUGASSERT(!pop3c->ssldone); |
| 511 |
result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone); |
| 512 |
DEBUGF(infof(data, "pop3_perform_upgrade_tls, connect -> %d, %d", |
| 513 |
result, ssldone)); |
| 514 |
if(!result && ssldone) { |
| 515 |
pop3c->ssldone = ssldone; |
| 516 |
|
| 517 |
result = pop3_perform_capa(data, conn); |
| 518 |
} |
| 519 |
out: |
| 520 |
return result; |
| 521 |
#else |
| 522 |
(void)data; |
| 523 |
(void)conn; |
| 524 |
return CURLE_NOT_BUILT_IN; |
| 525 |
#endif |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
static CURLcode pop3_perform_user(struct Curl_easy *data, |
| 535 |
struct connectdata *conn) |
| 536 |
{ |
| 537 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 538 |
CURLcode result = CURLE_OK; |
| 539 |
|
| 540 |
if(!pop3c) |
| 541 |
return CURLE_FAILED_INIT; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
if(!data->state.aptr.user) { |
| 546 |
pop3_state(data, POP3_STOP); |
| 547 |
|
| 548 |
return result; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
result = Curl_pp_sendf(data, &pop3c->pp, "USER %s", |
| 553 |
conn->user ? conn->user : ""); |
| 554 |
if(!result) |
| 555 |
pop3_state(data, POP3_USER); |
| 556 |
|
| 557 |
return result; |
| 558 |
} |
| 559 |
|
| 560 |
#ifndef CURL_DISABLE_DIGEST_AUTH |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
static CURLcode pop3_perform_apop(struct Curl_easy *data, |
| 568 |
struct connectdata *conn) |
| 569 |
{ |
| 570 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 571 |
CURLcode result = CURLE_OK; |
| 572 |
size_t i; |
| 573 |
struct MD5_context *ctxt; |
| 574 |
unsigned char digest[MD5_DIGEST_LEN]; |
| 575 |
char secret[2 * MD5_DIGEST_LEN + 1]; |
| 576 |
|
| 577 |
if(!pop3c) |
| 578 |
return CURLE_FAILED_INIT; |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
if(!data->state.aptr.user) { |
| 583 |
pop3_state(data, POP3_STOP); |
| 584 |
|
| 585 |
return result; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
ctxt = Curl_MD5_init(&Curl_DIGEST_MD5); |
| 590 |
if(!ctxt) |
| 591 |
return CURLE_OUT_OF_MEMORY; |
| 592 |
|
| 593 |
Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp, |
| 594 |
curlx_uztoui(strlen(pop3c->apoptimestamp))); |
| 595 |
|
| 596 |
Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd, |
| 597 |
curlx_uztoui(strlen(conn->passwd))); |
| 598 |
|
| 599 |
|
| 600 |
Curl_MD5_final(ctxt, digest); |
| 601 |
|
| 602 |
|
| 603 |
for(i = 0; i < MD5_DIGEST_LEN; i++) |
| 604 |
msnprintf(&secret[2 * i], 3, "%02x", digest[i]); |
| 605 |
|
| 606 |
result = Curl_pp_sendf(data, &pop3c->pp, "APOP %s %s", conn->user, secret); |
| 607 |
|
| 608 |
if(!result) |
| 609 |
pop3_state(data, POP3_APOP); |
| 610 |
|
| 611 |
return result; |
| 612 |
} |
| 613 |
#endif |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static CURLcode pop3_perform_auth(struct Curl_easy *data, |
| 623 |
const char *mech, |
| 624 |
const struct bufref *initresp) |
| 625 |
{ |
| 626 |
struct pop3_conn *pop3c = |
| 627 |
Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN); |
| 628 |
CURLcode result = CURLE_OK; |
| 629 |
const char *ir = (const char *) Curl_bufref_ptr(initresp); |
| 630 |
|
| 631 |
if(!pop3c) |
| 632 |
return CURLE_FAILED_INIT; |
| 633 |
|
| 634 |
if(ir) { |
| 635 |
|
| 636 |
result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s %s", mech, ir); |
| 637 |
} |
| 638 |
else { |
| 639 |
|
| 640 |
result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s", mech); |
| 641 |
} |
| 642 |
|
| 643 |
return result; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
static CURLcode pop3_continue_auth(struct Curl_easy *data, |
| 653 |
const char *mech, |
| 654 |
const struct bufref *resp) |
| 655 |
{ |
| 656 |
struct pop3_conn *pop3c = |
| 657 |
Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN); |
| 658 |
|
| 659 |
(void)mech; |
| 660 |
if(!pop3c) |
| 661 |
return CURLE_FAILED_INIT; |
| 662 |
|
| 663 |
return Curl_pp_sendf(data, &pop3c->pp, |
| 664 |
"%s", (const char *) Curl_bufref_ptr(resp)); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech) |
| 674 |
{ |
| 675 |
struct pop3_conn *pop3c = |
| 676 |
Curl_conn_meta_get(data->conn, CURL_META_POP3_CONN); |
| 677 |
|
| 678 |
(void)mech; |
| 679 |
if(!pop3c) |
| 680 |
return CURLE_FAILED_INIT; |
| 681 |
|
| 682 |
return Curl_pp_sendf(data, &pop3c->pp, "*"); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
static CURLcode pop3_perform_authentication(struct Curl_easy *data, |
| 694 |
struct connectdata *conn) |
| 695 |
{ |
| 696 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 697 |
CURLcode result = CURLE_OK; |
| 698 |
saslprogress progress = SASL_IDLE; |
| 699 |
|
| 700 |
if(!pop3c) |
| 701 |
return CURLE_FAILED_INIT; |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
if(!Curl_sasl_can_authenticate(&pop3c->sasl, data)) { |
| 706 |
pop3_state(data, POP3_STOP); |
| 707 |
return result; |
| 708 |
} |
| 709 |
|
| 710 |
if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) { |
| 711 |
|
| 712 |
result = Curl_sasl_start(&pop3c->sasl, data, FALSE, &progress); |
| 713 |
|
| 714 |
if(!result) |
| 715 |
if(progress == SASL_INPROGRESS) |
| 716 |
pop3_state(data, POP3_AUTH); |
| 717 |
} |
| 718 |
|
| 719 |
if(!result && progress == SASL_IDLE) { |
| 720 |
#ifndef CURL_DISABLE_DIGEST_AUTH |
| 721 |
if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP) |
| 722 |
|
| 723 |
result = pop3_perform_apop(data, conn); |
| 724 |
else |
| 725 |
#endif |
| 726 |
if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT) |
| 727 |
|
| 728 |
result = pop3_perform_user(data, conn); |
| 729 |
else |
| 730 |
result = Curl_sasl_is_blocked(&pop3c->sasl, data); |
| 731 |
} |
| 732 |
|
| 733 |
return result; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
static CURLcode pop3_perform_command(struct Curl_easy *data) |
| 743 |
{ |
| 744 |
CURLcode result = CURLE_OK; |
| 745 |
struct connectdata *conn = data->conn; |
| 746 |
struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY); |
| 747 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 748 |
const char *command = NULL; |
| 749 |
|
| 750 |
if(!pop3 || !pop3c) |
| 751 |
return CURLE_FAILED_INIT; |
| 752 |
|
| 753 |
|
| 754 |
if(pop3->id[0] == '\0' || data->set.list_only) { |
| 755 |
command = "LIST"; |
| 756 |
|
| 757 |
if(pop3->id[0] != '\0') |
| 758 |
|
| 759 |
pop3->transfer = PPTRANSFER_INFO; |
| 760 |
} |
| 761 |
else |
| 762 |
command = "RETR"; |
| 763 |
|
| 764 |
if(pop3->custom && pop3->custom[0] != '\0') |
| 765 |
command = pop3->custom; |
| 766 |
|
| 767 |
|
| 768 |
if(pop3->id[0] != '\0') |
| 769 |
result = Curl_pp_sendf(data, &pop3c->pp, "%s %s", command, pop3->id); |
| 770 |
else |
| 771 |
result = Curl_pp_sendf(data, &pop3c->pp, "%s", command); |
| 772 |
|
| 773 |
if(!result) { |
| 774 |
pop3_state(data, POP3_COMMAND); |
| 775 |
data->req.no_body = !pop3_is_multiline(command); |
| 776 |
} |
| 777 |
|
| 778 |
return result; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
static CURLcode pop3_perform_quit(struct Curl_easy *data, |
| 788 |
struct connectdata *conn) |
| 789 |
{ |
| 790 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 791 |
CURLcode result; |
| 792 |
|
| 793 |
if(!pop3c) |
| 794 |
return CURLE_FAILED_INIT; |
| 795 |
|
| 796 |
|
| 797 |
result = Curl_pp_sendf(data, &pop3c->pp, "%s", "QUIT"); |
| 798 |
if(!result) |
| 799 |
pop3_state(data, POP3_QUIT); |
| 800 |
|
| 801 |
return result; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
static CURLcode pop3_state_servergreet_resp(struct Curl_easy *data, |
| 806 |
int pop3code, |
| 807 |
pop3state instate) |
| 808 |
{ |
| 809 |
CURLcode result = CURLE_OK; |
| 810 |
struct connectdata *conn = data->conn; |
| 811 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 812 |
const char *line; |
| 813 |
size_t len; |
| 814 |
|
| 815 |
(void)instate; |
| 816 |
if(!pop3c) |
| 817 |
return CURLE_FAILED_INIT; |
| 818 |
|
| 819 |
line = curlx_dyn_ptr(&pop3c->pp.recvbuf); |
| 820 |
len = pop3c->pp.nfinal; |
| 821 |
|
| 822 |
if(pop3code != '+') { |
| 823 |
failf(data, "Got unexpected pop3-server response"); |
| 824 |
result = CURLE_WEIRD_SERVER_REPLY; |
| 825 |
} |
| 826 |
else if(len > 3) { |
| 827 |
|
| 828 |
char *lt; |
| 829 |
char *gt = NULL; |
| 830 |
|
| 831 |
|
| 832 |
lt = memchr(line, '<', len); |
| 833 |
if(lt) |
| 834 |
|
| 835 |
gt = memchr(lt, '>', len - (lt - line)); |
| 836 |
if(gt) { |
| 837 |
|
| 838 |
size_t timestamplen = gt - lt + 1; |
| 839 |
char *at = memchr(lt, '@', timestamplen); |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
if(at) { |
| 844 |
|
| 845 |
pop3c->apoptimestamp = Curl_memdup0(lt, timestamplen); |
| 846 |
if(!pop3c->apoptimestamp) |
| 847 |
return CURLE_OUT_OF_MEMORY; |
| 848 |
|
| 849 |
pop3c->authtypes |= POP3_TYPE_APOP; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
if(!result) |
| 854 |
result = pop3_perform_capa(data, conn); |
| 855 |
} |
| 856 |
|
| 857 |
return result; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
static CURLcode pop3_state_capa_resp(struct Curl_easy *data, int pop3code, |
| 862 |
pop3state instate) |
| 863 |
{ |
| 864 |
CURLcode result = CURLE_OK; |
| 865 |
struct connectdata *conn = data->conn; |
| 866 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 867 |
const char *line; |
| 868 |
size_t len; |
| 869 |
|
| 870 |
(void)instate; |
| 871 |
if(!pop3c) |
| 872 |
return CURLE_FAILED_INIT; |
| 873 |
|
| 874 |
line = curlx_dyn_ptr(&pop3c->pp.recvbuf); |
| 875 |
len = pop3c->pp.nfinal; |
| 876 |
|
| 877 |
|
| 878 |
if(pop3code == '*') { |
| 879 |
|
| 880 |
if(len >= 4 && !memcmp(line, "STLS", 4)) |
| 881 |
pop3c->tls_supported = TRUE; |
| 882 |
|
| 883 |
|
| 884 |
else if(len >= 4 && !memcmp(line, "USER", 4)) |
| 885 |
pop3c->authtypes |= POP3_TYPE_CLEARTEXT; |
| 886 |
|
| 887 |
|
| 888 |
else if(len >= 5 && !memcmp(line, "SASL ", 5)) { |
| 889 |
pop3c->authtypes |= POP3_TYPE_SASL; |
| 890 |
|
| 891 |
|
| 892 |
line += 5; |
| 893 |
len -= 5; |
| 894 |
|
| 895 |
|
| 896 |
for(;;) { |
| 897 |
size_t llen; |
| 898 |
size_t wordlen; |
| 899 |
unsigned short mechbit; |
| 900 |
|
| 901 |
while(len && |
| 902 |
(*line == ' ' || *line == '\t' || |
| 903 |
*line == '\r' || *line == '\n')) { |
| 904 |
|
| 905 |
line++; |
| 906 |
len--; |
| 907 |
} |
| 908 |
|
| 909 |
if(!len) |
| 910 |
break; |
| 911 |
|
| 912 |
|
| 913 |
for(wordlen = 0; wordlen < len && line[wordlen] != ' ' && |
| 914 |
line[wordlen] != '\t' && line[wordlen] != '\r' && |
| 915 |
line[wordlen] != '\n';) |
| 916 |
wordlen++; |
| 917 |
|
| 918 |
|
| 919 |
mechbit = Curl_sasl_decode_mech(line, wordlen, &llen); |
| 920 |
if(mechbit && llen == wordlen) |
| 921 |
pop3c->sasl.authmechs |= mechbit; |
| 922 |
|
| 923 |
line += wordlen; |
| 924 |
len -= wordlen; |
| 925 |
} |
| 926 |
} |
| 927 |
} |
| 928 |
else { |
| 929 |
|
| 930 |
if(pop3code != '+') |
| 931 |
pop3c->authtypes |= POP3_TYPE_CLEARTEXT; |
| 932 |
|
| 933 |
if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET)) |
| 934 |
result = pop3_perform_authentication(data, conn); |
| 935 |
else if(pop3code == '+' && pop3c->tls_supported) |
| 936 |
|
| 937 |
result = pop3_perform_starttls(data, conn); |
| 938 |
else if(data->set.use_ssl <= CURLUSESSL_TRY) |
| 939 |
|
| 940 |
result = pop3_perform_authentication(data, conn); |
| 941 |
else { |
| 942 |
failf(data, "STLS not supported."); |
| 943 |
result = CURLE_USE_SSL_FAILED; |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
return result; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
static CURLcode pop3_state_starttls_resp(struct Curl_easy *data, |
| 952 |
struct connectdata *conn, |
| 953 |
int pop3code, |
| 954 |
pop3state instate) |
| 955 |
{ |
| 956 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 957 |
CURLcode result = CURLE_OK; |
| 958 |
(void)instate; |
| 959 |
|
| 960 |
if(!pop3c) |
| 961 |
return CURLE_FAILED_INIT; |
| 962 |
|
| 963 |
|
| 964 |
if(pop3c->pp.overflow) |
| 965 |
return CURLE_WEIRD_SERVER_REPLY; |
| 966 |
|
| 967 |
if(pop3code != '+') { |
| 968 |
if(data->set.use_ssl != CURLUSESSL_TRY) { |
| 969 |
failf(data, "STARTTLS denied"); |
| 970 |
result = CURLE_USE_SSL_FAILED; |
| 971 |
} |
| 972 |
else |
| 973 |
result = pop3_perform_authentication(data, conn); |
| 974 |
} |
| 975 |
else |
| 976 |
pop3_state(data, POP3_UPGRADETLS); |
| 977 |
|
| 978 |
return result; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
static CURLcode pop3_state_auth_resp(struct Curl_easy *data, |
| 983 |
int pop3code, |
| 984 |
pop3state instate) |
| 985 |
{ |
| 986 |
CURLcode result = CURLE_OK; |
| 987 |
struct connectdata *conn = data->conn; |
| 988 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 989 |
saslprogress progress; |
| 990 |
|
| 991 |
(void)instate; |
| 992 |
if(!pop3c) |
| 993 |
return CURLE_FAILED_INIT; |
| 994 |
|
| 995 |
result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress); |
| 996 |
if(!result) |
| 997 |
switch(progress) { |
| 998 |
case SASL_DONE: |
| 999 |
pop3_state(data, POP3_STOP); |
| 1000 |
break; |
| 1001 |
case SASL_IDLE: |
| 1002 |
#ifndef CURL_DISABLE_DIGEST_AUTH |
| 1003 |
if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP) |
| 1004 |
|
| 1005 |
result = pop3_perform_apop(data, conn); |
| 1006 |
else |
| 1007 |
#endif |
| 1008 |
if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT) |
| 1009 |
|
| 1010 |
result = pop3_perform_user(data, conn); |
| 1011 |
else { |
| 1012 |
failf(data, "Authentication cancelled"); |
| 1013 |
result = CURLE_LOGIN_DENIED; |
| 1014 |
} |
| 1015 |
break; |
| 1016 |
default: |
| 1017 |
break; |
| 1018 |
} |
| 1019 |
|
| 1020 |
return result; |
| 1021 |
} |
| 1022 |
|
| 1023 |
#ifndef CURL_DISABLE_DIGEST_AUTH |
| 1024 |
|
| 1025 |
static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code, |
| 1026 |
pop3state instate) |
| 1027 |
{ |
| 1028 |
CURLcode result = CURLE_OK; |
| 1029 |
(void)instate; |
| 1030 |
|
| 1031 |
if(pop3code != '+') { |
| 1032 |
failf(data, "Authentication failed: %d", pop3code); |
| 1033 |
result = CURLE_LOGIN_DENIED; |
| 1034 |
} |
| 1035 |
else |
| 1036 |
|
| 1037 |
pop3_state(data, POP3_STOP); |
| 1038 |
|
| 1039 |
return result; |
| 1040 |
} |
| 1041 |
#endif |
| 1042 |
|
| 1043 |
|
| 1044 |
static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code, |
| 1045 |
pop3state instate) |
| 1046 |
{ |
| 1047 |
CURLcode result = CURLE_OK; |
| 1048 |
struct connectdata *conn = data->conn; |
| 1049 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1050 |
(void)instate; |
| 1051 |
|
| 1052 |
if(!pop3c) |
| 1053 |
return CURLE_FAILED_INIT; |
| 1054 |
|
| 1055 |
if(pop3code != '+') { |
| 1056 |
failf(data, "Access denied. %c", pop3code); |
| 1057 |
result = CURLE_LOGIN_DENIED; |
| 1058 |
} |
| 1059 |
else |
| 1060 |
|
| 1061 |
result = Curl_pp_sendf(data, &pop3c->pp, "PASS %s", |
| 1062 |
conn->passwd ? conn->passwd : ""); |
| 1063 |
if(!result) |
| 1064 |
pop3_state(data, POP3_PASS); |
| 1065 |
|
| 1066 |
return result; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code, |
| 1071 |
pop3state instate) |
| 1072 |
{ |
| 1073 |
CURLcode result = CURLE_OK; |
| 1074 |
(void)instate; |
| 1075 |
|
| 1076 |
if(pop3code != '+') { |
| 1077 |
failf(data, "Access denied. %c", pop3code); |
| 1078 |
result = CURLE_LOGIN_DENIED; |
| 1079 |
} |
| 1080 |
else |
| 1081 |
|
| 1082 |
pop3_state(data, POP3_STOP); |
| 1083 |
|
| 1084 |
return result; |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
static CURLcode pop3_state_command_resp(struct Curl_easy *data, |
| 1089 |
int pop3code, |
| 1090 |
pop3state instate) |
| 1091 |
{ |
| 1092 |
CURLcode result = CURLE_OK; |
| 1093 |
struct connectdata *conn = data->conn; |
| 1094 |
struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY); |
| 1095 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1096 |
struct pingpong *pp; |
| 1097 |
|
| 1098 |
(void)instate; |
| 1099 |
if(!pop3 || !pop3c) |
| 1100 |
return CURLE_FAILED_INIT; |
| 1101 |
|
| 1102 |
pp = &pop3c->pp; |
| 1103 |
if(pop3code != '+') { |
| 1104 |
pop3_state(data, POP3_STOP); |
| 1105 |
return CURLE_WEIRD_SERVER_REPLY; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
pop3c->eob = 2; |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
pop3c->strip = 2; |
| 1117 |
|
| 1118 |
if(pop3->transfer == PPTRANSFER_BODY) { |
| 1119 |
|
| 1120 |
Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE); |
| 1121 |
|
| 1122 |
if(pp->overflow) { |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
curlx_dyn_tail(&pp->recvbuf, pp->overflow); |
| 1129 |
pp->nfinal = 0; |
| 1130 |
|
| 1131 |
if(!data->req.no_body) { |
| 1132 |
result = pop3_write(data, curlx_dyn_ptr(&pp->recvbuf), |
| 1133 |
curlx_dyn_len(&pp->recvbuf), FALSE); |
| 1134 |
if(result) |
| 1135 |
return result; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
curlx_dyn_reset(&pp->recvbuf); |
| 1140 |
pp->overflow = 0; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
else |
| 1144 |
pp->overflow = 0; |
| 1145 |
|
| 1146 |
|
| 1147 |
pop3_state(data, POP3_STOP); |
| 1148 |
|
| 1149 |
return result; |
| 1150 |
} |
| 1151 |
|
| 1152 |
static CURLcode pop3_statemachine(struct Curl_easy *data, |
| 1153 |
struct connectdata *conn) |
| 1154 |
{ |
| 1155 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1156 |
CURLcode result = CURLE_OK; |
| 1157 |
int pop3code; |
| 1158 |
struct pingpong *pp; |
| 1159 |
size_t nread = 0; |
| 1160 |
(void)data; |
| 1161 |
|
| 1162 |
if(!pop3c) |
| 1163 |
return CURLE_FAILED_INIT; |
| 1164 |
|
| 1165 |
pp = &pop3c->pp; |
| 1166 |
|
| 1167 |
upgrade_tls: |
| 1168 |
if(pop3c->state == POP3_UPGRADETLS) { |
| 1169 |
result = pop3_perform_upgrade_tls(data, conn); |
| 1170 |
if(result || (pop3c->state == POP3_UPGRADETLS)) |
| 1171 |
return result; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
if(pp->sendleft) |
| 1176 |
return Curl_pp_flushsend(data, pp); |
| 1177 |
|
| 1178 |
do { |
| 1179 |
|
| 1180 |
result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &pop3code, &nread); |
| 1181 |
if(result) |
| 1182 |
return result; |
| 1183 |
|
| 1184 |
if(!pop3code) |
| 1185 |
break; |
| 1186 |
|
| 1187 |
|
| 1188 |
switch(pop3c->state) { |
| 1189 |
case POP3_SERVERGREET: |
| 1190 |
result = pop3_state_servergreet_resp(data, pop3code, pop3c->state); |
| 1191 |
break; |
| 1192 |
|
| 1193 |
case POP3_CAPA: |
| 1194 |
result = pop3_state_capa_resp(data, pop3code, pop3c->state); |
| 1195 |
break; |
| 1196 |
|
| 1197 |
case POP3_STARTTLS: |
| 1198 |
result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state); |
| 1199 |
|
| 1200 |
|
| 1201 |
if(!result && (pop3c->state == POP3_UPGRADETLS)) |
| 1202 |
goto upgrade_tls; |
| 1203 |
break; |
| 1204 |
|
| 1205 |
case POP3_AUTH: |
| 1206 |
result = pop3_state_auth_resp(data, pop3code, pop3c->state); |
| 1207 |
break; |
| 1208 |
|
| 1209 |
#ifndef CURL_DISABLE_DIGEST_AUTH |
| 1210 |
case POP3_APOP: |
| 1211 |
result = pop3_state_apop_resp(data, pop3code, pop3c->state); |
| 1212 |
break; |
| 1213 |
#endif |
| 1214 |
|
| 1215 |
case POP3_USER: |
| 1216 |
result = pop3_state_user_resp(data, pop3code, pop3c->state); |
| 1217 |
break; |
| 1218 |
|
| 1219 |
case POP3_PASS: |
| 1220 |
result = pop3_state_pass_resp(data, pop3code, pop3c->state); |
| 1221 |
break; |
| 1222 |
|
| 1223 |
case POP3_COMMAND: |
| 1224 |
result = pop3_state_command_resp(data, pop3code, pop3c->state); |
| 1225 |
break; |
| 1226 |
|
| 1227 |
case POP3_QUIT: |
| 1228 |
pop3_state(data, POP3_STOP); |
| 1229 |
break; |
| 1230 |
|
| 1231 |
default: |
| 1232 |
|
| 1233 |
pop3_state(data, POP3_STOP); |
| 1234 |
break; |
| 1235 |
} |
| 1236 |
} while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp)); |
| 1237 |
|
| 1238 |
return result; |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done) |
| 1243 |
{ |
| 1244 |
CURLcode result = CURLE_OK; |
| 1245 |
struct connectdata *conn = data->conn; |
| 1246 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1247 |
|
| 1248 |
if(!pop3c) |
| 1249 |
return CURLE_FAILED_INIT; |
| 1250 |
result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE); |
| 1251 |
*done = (pop3c->state == POP3_STOP); |
| 1252 |
|
| 1253 |
return result; |
| 1254 |
} |
| 1255 |
|
| 1256 |
static CURLcode pop3_block_statemach(struct Curl_easy *data, |
| 1257 |
struct connectdata *conn, |
| 1258 |
bool disconnecting) |
| 1259 |
{ |
| 1260 |
CURLcode result = CURLE_OK; |
| 1261 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1262 |
|
| 1263 |
if(!pop3c) |
| 1264 |
return CURLE_FAILED_INIT; |
| 1265 |
|
| 1266 |
while(pop3c->state != POP3_STOP && !result) |
| 1267 |
result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting); |
| 1268 |
|
| 1269 |
return result; |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
static int pop3_getsock(struct Curl_easy *data, |
| 1274 |
struct connectdata *conn, curl_socket_t *socks) |
| 1275 |
{ |
| 1276 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1277 |
if(pop3c) |
| 1278 |
return Curl_pp_getsock(data, &pop3c->pp, socks); |
| 1279 |
return GETSOCK_BLANK; |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
static CURLcode pop3_connect(struct Curl_easy *data, bool *done) |
| 1293 |
{ |
| 1294 |
CURLcode result = CURLE_OK; |
| 1295 |
struct connectdata *conn = data->conn; |
| 1296 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1297 |
struct pingpong *pp = pop3c ? &pop3c->pp : NULL; |
| 1298 |
|
| 1299 |
*done = FALSE; |
| 1300 |
if(!pop3c) |
| 1301 |
return CURLE_FAILED_INIT; |
| 1302 |
|
| 1303 |
|
| 1304 |
connkeep(conn, "POP3 default"); |
| 1305 |
|
| 1306 |
PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp); |
| 1307 |
|
| 1308 |
|
| 1309 |
pop3c->preftype = POP3_TYPE_ANY; |
| 1310 |
Curl_sasl_init(&pop3c->sasl, data, &saslpop3); |
| 1311 |
|
| 1312 |
|
| 1313 |
Curl_pp_init(pp); |
| 1314 |
|
| 1315 |
|
| 1316 |
result = pop3_parse_url_options(conn); |
| 1317 |
if(result) |
| 1318 |
return result; |
| 1319 |
|
| 1320 |
|
| 1321 |
pop3_state(data, POP3_SERVERGREET); |
| 1322 |
|
| 1323 |
result = pop3_multi_statemach(data, done); |
| 1324 |
|
| 1325 |
return result; |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
static CURLcode pop3_done(struct Curl_easy *data, CURLcode status, |
| 1338 |
bool premature) |
| 1339 |
{ |
| 1340 |
CURLcode result = CURLE_OK; |
| 1341 |
struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY); |
| 1342 |
|
| 1343 |
(void)premature; |
| 1344 |
|
| 1345 |
if(!pop3) |
| 1346 |
return CURLE_OK; |
| 1347 |
|
| 1348 |
if(status) { |
| 1349 |
connclose(data->conn, "POP3 done with bad status"); |
| 1350 |
result = status; |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
Curl_safefree(pop3->id); |
| 1355 |
Curl_safefree(pop3->custom); |
| 1356 |
|
| 1357 |
|
| 1358 |
pop3->transfer = PPTRANSFER_BODY; |
| 1359 |
|
| 1360 |
return result; |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
static CURLcode pop3_perform(struct Curl_easy *data, bool *connected, |
| 1371 |
bool *dophase_done) |
| 1372 |
{ |
| 1373 |
|
| 1374 |
CURLcode result = CURLE_OK; |
| 1375 |
struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY); |
| 1376 |
|
| 1377 |
if(!pop3) |
| 1378 |
return CURLE_FAILED_INIT; |
| 1379 |
|
| 1380 |
DEBUGF(infof(data, "DO phase starts")); |
| 1381 |
|
| 1382 |
if(data->req.no_body) { |
| 1383 |
|
| 1384 |
pop3->transfer = PPTRANSFER_INFO; |
| 1385 |
} |
| 1386 |
|
| 1387 |
*dophase_done = FALSE; |
| 1388 |
|
| 1389 |
|
| 1390 |
result = pop3_perform_command(data); |
| 1391 |
if(result) |
| 1392 |
return result; |
| 1393 |
|
| 1394 |
|
| 1395 |
result = pop3_multi_statemach(data, dophase_done); |
| 1396 |
*connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET); |
| 1397 |
|
| 1398 |
if(*dophase_done) |
| 1399 |
DEBUGF(infof(data, "DO phase is complete")); |
| 1400 |
|
| 1401 |
return result; |
| 1402 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
static CURLcode pop3_do(struct Curl_easy *data, bool *done) |
| 1414 |
{ |
| 1415 |
CURLcode result = CURLE_OK; |
| 1416 |
*done = FALSE; |
| 1417 |
|
| 1418 |
|
| 1419 |
result = pop3_parse_url_path(data); |
| 1420 |
if(result) |
| 1421 |
return result; |
| 1422 |
|
| 1423 |
|
| 1424 |
result = pop3_parse_custom_request(data); |
| 1425 |
if(result) |
| 1426 |
return result; |
| 1427 |
|
| 1428 |
result = pop3_regular_transfer(data, done); |
| 1429 |
|
| 1430 |
return result; |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
static CURLcode pop3_disconnect(struct Curl_easy *data, |
| 1441 |
struct connectdata *conn, bool dead_connection) |
| 1442 |
{ |
| 1443 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1444 |
(void)data; |
| 1445 |
|
| 1446 |
if(!pop3c) |
| 1447 |
return CURLE_FAILED_INIT; |
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
if(!dead_connection && conn->bits.protoconnstart) { |
| 1454 |
if(!pop3_perform_quit(data, conn)) |
| 1455 |
(void)pop3_block_statemach(data, conn, TRUE); |
| 1456 |
} |
| 1457 |
|
| 1458 |
|
| 1459 |
Curl_pp_disconnect(&pop3c->pp); |
| 1460 |
|
| 1461 |
|
| 1462 |
Curl_sasl_cleanup(conn, pop3c->sasl.authused); |
| 1463 |
|
| 1464 |
|
| 1465 |
Curl_safefree(pop3c->apoptimestamp); |
| 1466 |
|
| 1467 |
return CURLE_OK; |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
static CURLcode pop3_dophase_done(struct Curl_easy *data, bool connected) |
| 1472 |
{ |
| 1473 |
(void)data; |
| 1474 |
(void)connected; |
| 1475 |
|
| 1476 |
return CURLE_OK; |
| 1477 |
} |
| 1478 |
|
| 1479 |
|
| 1480 |
static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done) |
| 1481 |
{ |
| 1482 |
CURLcode result = pop3_multi_statemach(data, dophase_done); |
| 1483 |
|
| 1484 |
if(result) |
| 1485 |
DEBUGF(infof(data, "DO phase failed")); |
| 1486 |
else if(*dophase_done) { |
| 1487 |
result = pop3_dophase_done(data, FALSE ); |
| 1488 |
|
| 1489 |
DEBUGF(infof(data, "DO phase is complete")); |
| 1490 |
} |
| 1491 |
|
| 1492 |
return result; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
static CURLcode pop3_regular_transfer(struct Curl_easy *data, |
| 1505 |
bool *dophase_done) |
| 1506 |
{ |
| 1507 |
CURLcode result = CURLE_OK; |
| 1508 |
bool connected = FALSE; |
| 1509 |
|
| 1510 |
|
| 1511 |
data->req.size = -1; |
| 1512 |
|
| 1513 |
|
| 1514 |
Curl_pgrsSetUploadCounter(data, 0); |
| 1515 |
Curl_pgrsSetDownloadCounter(data, 0); |
| 1516 |
Curl_pgrsSetUploadSize(data, -1); |
| 1517 |
Curl_pgrsSetDownloadSize(data, -1); |
| 1518 |
|
| 1519 |
|
| 1520 |
result = pop3_perform(data, &connected, dophase_done); |
| 1521 |
|
| 1522 |
|
| 1523 |
if(!result && *dophase_done) |
| 1524 |
result = pop3_dophase_done(data, connected); |
| 1525 |
|
| 1526 |
return result; |
| 1527 |
} |
| 1528 |
|
| 1529 |
static void pop3_easy_dtor(void *key, size_t klen, void *entry) |
| 1530 |
{ |
| 1531 |
struct POP3 *pop3 = entry; |
| 1532 |
(void)key; |
| 1533 |
(void)klen; |
| 1534 |
DEBUGASSERT(pop3); |
| 1535 |
|
| 1536 |
Curl_safefree(pop3->id); |
| 1537 |
Curl_safefree(pop3->custom); |
| 1538 |
free(pop3); |
| 1539 |
} |
| 1540 |
|
| 1541 |
static void pop3_conn_dtor(void *key, size_t klen, void *entry) |
| 1542 |
{ |
| 1543 |
struct pop3_conn *pop3c = entry; |
| 1544 |
(void)key; |
| 1545 |
(void)klen; |
| 1546 |
DEBUGASSERT(pop3c); |
| 1547 |
Curl_pp_disconnect(&pop3c->pp); |
| 1548 |
Curl_safefree(pop3c->apoptimestamp); |
| 1549 |
free(pop3c); |
| 1550 |
} |
| 1551 |
|
| 1552 |
static CURLcode pop3_setup_connection(struct Curl_easy *data, |
| 1553 |
struct connectdata *conn) |
| 1554 |
{ |
| 1555 |
struct pop3_conn *pop3c; |
| 1556 |
struct POP3 *pop3 = calloc(1, sizeof(*pop3)); |
| 1557 |
if(!pop3 || |
| 1558 |
Curl_meta_set(data, CURL_META_POP3_EASY, pop3, pop3_easy_dtor)) |
| 1559 |
return CURLE_OUT_OF_MEMORY; |
| 1560 |
|
| 1561 |
pop3c = calloc(1, sizeof(*pop3c)); |
| 1562 |
if(!pop3c || |
| 1563 |
Curl_conn_meta_set(conn, CURL_META_POP3_CONN, pop3c, pop3_conn_dtor)) |
| 1564 |
return CURLE_OUT_OF_MEMORY; |
| 1565 |
|
| 1566 |
return CURLE_OK; |
| 1567 |
} |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
static CURLcode pop3_parse_url_options(struct connectdata *conn) |
| 1576 |
{ |
| 1577 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1578 |
CURLcode result = CURLE_OK; |
| 1579 |
const char *ptr = conn->options; |
| 1580 |
|
| 1581 |
if(!pop3c) |
| 1582 |
return CURLE_FAILED_INIT; |
| 1583 |
|
| 1584 |
while(!result && ptr && *ptr) { |
| 1585 |
const char *key = ptr; |
| 1586 |
const char *value; |
| 1587 |
|
| 1588 |
while(*ptr && *ptr != '=') |
| 1589 |
ptr++; |
| 1590 |
|
| 1591 |
value = ptr + 1; |
| 1592 |
|
| 1593 |
while(*ptr && *ptr != ';') |
| 1594 |
ptr++; |
| 1595 |
|
| 1596 |
if(strncasecompare(key, "AUTH=", 5)) { |
| 1597 |
result = Curl_sasl_parse_url_auth_option(&pop3c->sasl, |
| 1598 |
value, ptr - value); |
| 1599 |
|
| 1600 |
if(result && strncasecompare(value, "+APOP", ptr - value)) { |
| 1601 |
pop3c->preftype = POP3_TYPE_APOP; |
| 1602 |
pop3c->sasl.prefmech = SASL_AUTH_NONE; |
| 1603 |
result = CURLE_OK; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
else |
| 1607 |
result = CURLE_URL_MALFORMAT; |
| 1608 |
|
| 1609 |
if(*ptr == ';') |
| 1610 |
ptr++; |
| 1611 |
} |
| 1612 |
|
| 1613 |
if(pop3c->preftype != POP3_TYPE_APOP) |
| 1614 |
switch(pop3c->sasl.prefmech) { |
| 1615 |
case SASL_AUTH_NONE: |
| 1616 |
pop3c->preftype = POP3_TYPE_NONE; |
| 1617 |
break; |
| 1618 |
case SASL_AUTH_DEFAULT: |
| 1619 |
pop3c->preftype = POP3_TYPE_ANY; |
| 1620 |
break; |
| 1621 |
default: |
| 1622 |
pop3c->preftype = POP3_TYPE_SASL; |
| 1623 |
break; |
| 1624 |
} |
| 1625 |
|
| 1626 |
return result; |
| 1627 |
} |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
static CURLcode pop3_parse_url_path(struct Curl_easy *data) |
| 1636 |
{ |
| 1637 |
|
| 1638 |
struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY); |
| 1639 |
const char *path = &data->state.up.path[1]; |
| 1640 |
|
| 1641 |
if(!pop3) |
| 1642 |
return CURLE_FAILED_INIT; |
| 1643 |
|
| 1644 |
return Curl_urldecode(path, 0, &pop3->id, NULL, REJECT_CTRL); |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
static CURLcode pop3_parse_custom_request(struct Curl_easy *data) |
| 1654 |
{ |
| 1655 |
CURLcode result = CURLE_OK; |
| 1656 |
struct POP3 *pop3 = Curl_meta_get(data, CURL_META_POP3_EASY); |
| 1657 |
const char *custom = data->set.str[STRING_CUSTOMREQUEST]; |
| 1658 |
|
| 1659 |
if(!pop3) |
| 1660 |
return CURLE_FAILED_INIT; |
| 1661 |
|
| 1662 |
if(custom) |
| 1663 |
result = Curl_urldecode(custom, 0, &pop3->custom, NULL, REJECT_CTRL); |
| 1664 |
|
| 1665 |
return result; |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
static CURLcode pop3_write(struct Curl_easy *data, const char *str, |
| 1676 |
size_t nread, bool is_eos) |
| 1677 |
{ |
| 1678 |
|
| 1679 |
CURLcode result = CURLE_OK; |
| 1680 |
struct SingleRequest *k = &data->req; |
| 1681 |
struct connectdata *conn = data->conn; |
| 1682 |
struct pop3_conn *pop3c = Curl_conn_meta_get(conn, CURL_META_POP3_CONN); |
| 1683 |
bool strip_dot = FALSE; |
| 1684 |
size_t last = 0; |
| 1685 |
size_t i; |
| 1686 |
(void)is_eos; |
| 1687 |
|
| 1688 |
if(!pop3c) |
| 1689 |
return CURLE_FAILED_INIT; |
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
for(i = 0; i < nread; i++) { |
| 1697 |
size_t prev = pop3c->eob; |
| 1698 |
|
| 1699 |
switch(str[i]) { |
| 1700 |
case 0x0d: |
| 1701 |
if(pop3c->eob == 0) { |
| 1702 |
pop3c->eob++; |
| 1703 |
|
| 1704 |
if(i) { |
| 1705 |
|
| 1706 |
result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last], |
| 1707 |
i - last); |
| 1708 |
|
| 1709 |
if(result) |
| 1710 |
return result; |
| 1711 |
|
| 1712 |
last = i; |
| 1713 |
} |
| 1714 |
} |
| 1715 |
else if(pop3c->eob == 3) |
| 1716 |
pop3c->eob++; |
| 1717 |
else |
| 1718 |
|
| 1719 |
|
| 1720 |
pop3c->eob = 1; |
| 1721 |
break; |
| 1722 |
|
| 1723 |
case 0x0a: |
| 1724 |
if(pop3c->eob == 1 || pop3c->eob == 4) |
| 1725 |
pop3c->eob++; |
| 1726 |
else |
| 1727 |
|
| 1728 |
|
| 1729 |
pop3c->eob = 0; |
| 1730 |
break; |
| 1731 |
|
| 1732 |
case 0x2e: |
| 1733 |
if(pop3c->eob == 2) |
| 1734 |
pop3c->eob++; |
| 1735 |
else if(pop3c->eob == 3) { |
| 1736 |
|
| 1737 |
strip_dot = TRUE; |
| 1738 |
pop3c->eob = 0; |
| 1739 |
} |
| 1740 |
else |
| 1741 |
|
| 1742 |
|
| 1743 |
pop3c->eob = 0; |
| 1744 |
break; |
| 1745 |
|
| 1746 |
default: |
| 1747 |
pop3c->eob = 0; |
| 1748 |
break; |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
if(prev && prev >= pop3c->eob) { |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
while(prev && pop3c->strip) { |
| 1757 |
prev--; |
| 1758 |
pop3c->strip--; |
| 1759 |
} |
| 1760 |
|
| 1761 |
if(prev) { |
| 1762 |
|
| 1763 |
|
| 1764 |
if(strip_dot && prev - 1 > 0) { |
| 1765 |
result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB, |
| 1766 |
prev - 1); |
| 1767 |
} |
| 1768 |
else if(!strip_dot) { |
| 1769 |
result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB, |
| 1770 |
prev); |
| 1771 |
} |
| 1772 |
else { |
| 1773 |
result = CURLE_OK; |
| 1774 |
} |
| 1775 |
|
| 1776 |
if(result) |
| 1777 |
return result; |
| 1778 |
|
| 1779 |
last = i; |
| 1780 |
strip_dot = FALSE; |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
|
| 1785 |
if(pop3c->eob == POP3_EOB_LEN) { |
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
result = Curl_client_write(data, CLIENTWRITE_BODY, POP3_EOB, 2); |
| 1790 |
|
| 1791 |
k->keepon &= ~KEEP_RECV; |
| 1792 |
pop3c->eob = 0; |
| 1793 |
|
| 1794 |
return result; |
| 1795 |
} |
| 1796 |
|
| 1797 |
if(pop3c->eob) |
| 1798 |
|
| 1799 |
return CURLE_OK; |
| 1800 |
|
| 1801 |
if(nread - last) { |
| 1802 |
result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last], |
| 1803 |
nread - last); |
| 1804 |
} |
| 1805 |
|
| 1806 |
return result; |
| 1807 |
} |
| 1808 |
|
| 1809 |
#endif |