pop3.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. * RFC1734 POP3 Authentication
  24. * RFC1939 POP3 protocol
  25. * RFC2195 CRAM-MD5 authentication
  26. * RFC2384 POP URL Scheme
  27. * RFC2449 POP3 Extension Mechanism
  28. * RFC2595 Using TLS with IMAP, POP3 and ACAP
  29. * RFC2831 DIGEST-MD5 authentication
  30. * RFC4422 Simple Authentication and Security Layer (SASL)
  31. * RFC4616 PLAIN authentication
  32. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  33. * RFC5034 POP3 SASL Authentication Mechanism
  34. * RFC6749 OAuth 2.0 Authorization Framework
  35. * RFC8314 Use of TLS for Email Submission and Access
  36. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  37. *
  38. ***************************************************************************/
  39. #include "curl_setup.h"
  40. #ifndef CURL_DISABLE_POP3
  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_NETDB_H
  48. #include <netdb.h>
  49. #endif
  50. #ifdef __VMS
  51. #include <in.h>
  52. #include <inet.h>
  53. #endif
  54. #include <curl/curl.h>
  55. #include "urldata.h"
  56. #include "sendf.h"
  57. #include "hostip.h"
  58. #include "progress.h"
  59. #include "transfer.h"
  60. #include "escape.h"
  61. #include "http.h" /* for HTTP proxy tunnel stuff */
  62. #include "socks.h"
  63. #include "pop3.h"
  64. #include "strtoofft.h"
  65. #include "strcase.h"
  66. #include "vtls/vtls.h"
  67. #include "cfilters.h"
  68. #include "connect.h"
  69. #include "select.h"
  70. #include "multiif.h"
  71. #include "url.h"
  72. #include "bufref.h"
  73. #include "curl_sasl.h"
  74. #include "curl_md5.h"
  75. #include "warnless.h"
  76. #include "strdup.h"
  77. /* The last 3 #include files should be in this order */
  78. #include "curl_printf.h"
  79. #include "curl_memory.h"
  80. #include "memdebug.h"
  81. /* Local API functions */
  82. static CURLcode pop3_regular_transfer(struct Curl_easy *data, bool *done);
  83. static CURLcode pop3_do(struct Curl_easy *data, bool *done);
  84. static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
  85. bool premature);
  86. static CURLcode pop3_connect(struct Curl_easy *data, bool *done);
  87. static CURLcode pop3_disconnect(struct Curl_easy *data,
  88. struct connectdata *conn, bool dead);
  89. static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done);
  90. static int pop3_getsock(struct Curl_easy *data,
  91. struct connectdata *conn, curl_socket_t *socks);
  92. static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done);
  93. static CURLcode pop3_setup_connection(struct Curl_easy *data,
  94. struct connectdata *conn);
  95. static CURLcode pop3_parse_url_options(struct connectdata *conn);
  96. static CURLcode pop3_parse_url_path(struct Curl_easy *data);
  97. static CURLcode pop3_parse_custom_request(struct Curl_easy *data);
  98. static CURLcode pop3_perform_auth(struct Curl_easy *data, const char *mech,
  99. const struct bufref *initresp);
  100. static CURLcode pop3_continue_auth(struct Curl_easy *data, const char *mech,
  101. const struct bufref *resp);
  102. static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech);
  103. static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out);
  104. /*
  105. * POP3 protocol handler.
  106. */
  107. const struct Curl_handler Curl_handler_pop3 = {
  108. "pop3", /* scheme */
  109. pop3_setup_connection, /* setup_connection */
  110. pop3_do, /* do_it */
  111. pop3_done, /* done */
  112. ZERO_NULL, /* do_more */
  113. pop3_connect, /* connect_it */
  114. pop3_multi_statemach, /* connecting */
  115. pop3_doing, /* doing */
  116. pop3_getsock, /* proto_getsock */
  117. pop3_getsock, /* doing_getsock */
  118. ZERO_NULL, /* domore_getsock */
  119. ZERO_NULL, /* perform_getsock */
  120. pop3_disconnect, /* disconnect */
  121. ZERO_NULL, /* write_resp */
  122. ZERO_NULL, /* write_resp_hd */
  123. ZERO_NULL, /* connection_check */
  124. ZERO_NULL, /* attach connection */
  125. PORT_POP3, /* defport */
  126. CURLPROTO_POP3, /* protocol */
  127. CURLPROTO_POP3, /* family */
  128. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */
  129. PROTOPT_URLOPTIONS
  130. };
  131. #ifdef USE_SSL
  132. /*
  133. * POP3S protocol handler.
  134. */
  135. const struct Curl_handler Curl_handler_pop3s = {
  136. "pop3s", /* scheme */
  137. pop3_setup_connection, /* setup_connection */
  138. pop3_do, /* do_it */
  139. pop3_done, /* done */
  140. ZERO_NULL, /* do_more */
  141. pop3_connect, /* connect_it */
  142. pop3_multi_statemach, /* connecting */
  143. pop3_doing, /* doing */
  144. pop3_getsock, /* proto_getsock */
  145. pop3_getsock, /* doing_getsock */
  146. ZERO_NULL, /* domore_getsock */
  147. ZERO_NULL, /* perform_getsock */
  148. pop3_disconnect, /* disconnect */
  149. ZERO_NULL, /* write_resp */
  150. ZERO_NULL, /* write_resp_hd */
  151. ZERO_NULL, /* connection_check */
  152. ZERO_NULL, /* attach connection */
  153. PORT_POP3S, /* defport */
  154. CURLPROTO_POP3S, /* protocol */
  155. CURLPROTO_POP3, /* family */
  156. PROTOPT_CLOSEACTION | PROTOPT_SSL
  157. | PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS /* flags */
  158. };
  159. #endif
  160. /* SASL parameters for the pop3 protocol */
  161. static const struct SASLproto saslpop3 = {
  162. "pop", /* The service name */
  163. pop3_perform_auth, /* Send authentication command */
  164. pop3_continue_auth, /* Send authentication continuation */
  165. pop3_cancel_auth, /* Send authentication cancellation */
  166. pop3_get_message, /* Get SASL response message */
  167. 255 - 8, /* Max line len - strlen("AUTH ") - 1 space - crlf */
  168. '*', /* Code received when continuation is expected */
  169. '+', /* Code to receive upon authentication success */
  170. SASL_AUTH_DEFAULT, /* Default mechanisms */
  171. SASL_FLAG_BASE64 /* Configuration flags */
  172. };
  173. #ifdef USE_SSL
  174. static void pop3_to_pop3s(struct connectdata *conn)
  175. {
  176. /* Change the connection handler */
  177. conn->handler = &Curl_handler_pop3s;
  178. /* Set the connection's upgraded to TLS flag */
  179. conn->bits.tls_upgraded = TRUE;
  180. }
  181. #else
  182. #define pop3_to_pop3s(x) Curl_nop_stmt
  183. #endif
  184. /***********************************************************************
  185. *
  186. * pop3_endofresp()
  187. *
  188. * Checks for an ending POP3 status code at the start of the given string, but
  189. * also detects the APOP timestamp from the server greeting and various
  190. * capabilities from the CAPA response including the supported authentication
  191. * types and allowed SASL mechanisms.
  192. */
  193. static bool pop3_endofresp(struct Curl_easy *data, struct connectdata *conn,
  194. char *line, size_t len, int *resp)
  195. {
  196. struct pop3_conn *pop3c = &conn->proto.pop3c;
  197. (void)data;
  198. /* Do we have an error response? */
  199. if(len >= 4 && !memcmp("-ERR", line, 4)) {
  200. *resp = '-';
  201. return TRUE;
  202. }
  203. /* Are we processing CAPA command responses? */
  204. if(pop3c->state == POP3_CAPA) {
  205. /* Do we have the terminating line? */
  206. if(len >= 1 && line[0] == '.')
  207. /* Treat the response as a success */
  208. *resp = '+';
  209. else
  210. /* Treat the response as an untagged continuation */
  211. *resp = '*';
  212. return TRUE;
  213. }
  214. /* Do we have a success response? */
  215. if(len >= 3 && !memcmp("+OK", line, 3)) {
  216. *resp = '+';
  217. return TRUE;
  218. }
  219. /* Do we have a continuation response? */
  220. if(len >= 1 && line[0] == '+') {
  221. *resp = '*';
  222. return TRUE;
  223. }
  224. return FALSE; /* Nothing for us */
  225. }
  226. /***********************************************************************
  227. *
  228. * pop3_get_message()
  229. *
  230. * Gets the authentication message from the response buffer.
  231. */
  232. static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out)
  233. {
  234. char *message = Curl_dyn_ptr(&data->conn->proto.pop3c.pp.recvbuf);
  235. size_t len = data->conn->proto.pop3c.pp.nfinal;
  236. if(len > 2) {
  237. /* Find the start of the message */
  238. len -= 2;
  239. for(message += 2; *message == ' ' || *message == '\t'; message++, len--)
  240. ;
  241. /* Find the end of the message */
  242. while(len--)
  243. if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
  244. message[len] != '\t')
  245. break;
  246. /* Terminate the message */
  247. message[++len] = '\0';
  248. Curl_bufref_set(out, message, len, NULL);
  249. }
  250. else
  251. /* junk input => zero length output */
  252. Curl_bufref_set(out, "", 0, NULL);
  253. return CURLE_OK;
  254. }
  255. /***********************************************************************
  256. *
  257. * pop3_state()
  258. *
  259. * This is the ONLY way to change POP3 state!
  260. */
  261. static void pop3_state(struct Curl_easy *data, pop3state newstate)
  262. {
  263. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  264. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  265. /* for debug purposes */
  266. static const char * const names[] = {
  267. "STOP",
  268. "SERVERGREET",
  269. "CAPA",
  270. "STARTTLS",
  271. "UPGRADETLS",
  272. "AUTH",
  273. "APOP",
  274. "USER",
  275. "PASS",
  276. "COMMAND",
  277. "QUIT",
  278. /* LAST */
  279. };
  280. if(pop3c->state != newstate)
  281. infof(data, "POP3 %p state change from %s to %s",
  282. (void *)pop3c, names[pop3c->state], names[newstate]);
  283. #endif
  284. pop3c->state = newstate;
  285. }
  286. /***********************************************************************
  287. *
  288. * pop3_perform_capa()
  289. *
  290. * Sends the CAPA command in order to obtain a list of server side supported
  291. * capabilities.
  292. */
  293. static CURLcode pop3_perform_capa(struct Curl_easy *data,
  294. struct connectdata *conn)
  295. {
  296. CURLcode result = CURLE_OK;
  297. struct pop3_conn *pop3c = &conn->proto.pop3c;
  298. pop3c->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
  299. pop3c->sasl.authused = SASL_AUTH_NONE; /* Clear the auth. mechanism used */
  300. pop3c->tls_supported = FALSE; /* Clear the TLS capability */
  301. /* Send the CAPA command */
  302. result = Curl_pp_sendf(data, &pop3c->pp, "%s", "CAPA");
  303. if(!result)
  304. pop3_state(data, POP3_CAPA);
  305. return result;
  306. }
  307. /***********************************************************************
  308. *
  309. * pop3_perform_starttls()
  310. *
  311. * Sends the STLS command to start the upgrade to TLS.
  312. */
  313. static CURLcode pop3_perform_starttls(struct Curl_easy *data,
  314. struct connectdata *conn)
  315. {
  316. /* Send the STLS command */
  317. CURLcode result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s", "STLS");
  318. if(!result)
  319. pop3_state(data, POP3_STARTTLS);
  320. return result;
  321. }
  322. /***********************************************************************
  323. *
  324. * pop3_perform_upgrade_tls()
  325. *
  326. * Performs the upgrade to TLS.
  327. */
  328. static CURLcode pop3_perform_upgrade_tls(struct Curl_easy *data,
  329. struct connectdata *conn)
  330. {
  331. /* Start the SSL connection */
  332. struct pop3_conn *pop3c = &conn->proto.pop3c;
  333. CURLcode result;
  334. bool ssldone = FALSE;
  335. if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
  336. result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET);
  337. if(result)
  338. goto out;
  339. }
  340. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  341. if(!result) {
  342. pop3c->ssldone = ssldone;
  343. if(pop3c->state != POP3_UPGRADETLS)
  344. pop3_state(data, POP3_UPGRADETLS);
  345. if(pop3c->ssldone) {
  346. pop3_to_pop3s(conn);
  347. result = pop3_perform_capa(data, conn);
  348. }
  349. }
  350. out:
  351. return result;
  352. }
  353. /***********************************************************************
  354. *
  355. * pop3_perform_user()
  356. *
  357. * Sends a clear text USER command to authenticate with.
  358. */
  359. static CURLcode pop3_perform_user(struct Curl_easy *data,
  360. struct connectdata *conn)
  361. {
  362. CURLcode result = CURLE_OK;
  363. /* Check we have a username and password to authenticate with and end the
  364. connect phase if we don't */
  365. if(!data->state.aptr.user) {
  366. pop3_state(data, POP3_STOP);
  367. return result;
  368. }
  369. /* Send the USER command */
  370. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "USER %s",
  371. conn->user ? conn->user : "");
  372. if(!result)
  373. pop3_state(data, POP3_USER);
  374. return result;
  375. }
  376. #ifndef CURL_DISABLE_DIGEST_AUTH
  377. /***********************************************************************
  378. *
  379. * pop3_perform_apop()
  380. *
  381. * Sends an APOP command to authenticate with.
  382. */
  383. static CURLcode pop3_perform_apop(struct Curl_easy *data,
  384. struct connectdata *conn)
  385. {
  386. CURLcode result = CURLE_OK;
  387. struct pop3_conn *pop3c = &conn->proto.pop3c;
  388. size_t i;
  389. struct MD5_context *ctxt;
  390. unsigned char digest[MD5_DIGEST_LEN];
  391. char secret[2 * MD5_DIGEST_LEN + 1];
  392. /* Check we have a username and password to authenticate with and end the
  393. connect phase if we don't */
  394. if(!data->state.aptr.user) {
  395. pop3_state(data, POP3_STOP);
  396. return result;
  397. }
  398. /* Create the digest */
  399. ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
  400. if(!ctxt)
  401. return CURLE_OUT_OF_MEMORY;
  402. Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp,
  403. curlx_uztoui(strlen(pop3c->apoptimestamp)));
  404. Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd,
  405. curlx_uztoui(strlen(conn->passwd)));
  406. /* Finalise the digest */
  407. Curl_MD5_final(ctxt, digest);
  408. /* Convert the calculated 16 octet digest into a 32 byte hex string */
  409. for(i = 0; i < MD5_DIGEST_LEN; i++)
  410. msnprintf(&secret[2 * i], 3, "%02x", digest[i]);
  411. result = Curl_pp_sendf(data, &pop3c->pp, "APOP %s %s", conn->user, secret);
  412. if(!result)
  413. pop3_state(data, POP3_APOP);
  414. return result;
  415. }
  416. #endif
  417. /***********************************************************************
  418. *
  419. * pop3_perform_auth()
  420. *
  421. * Sends an AUTH command allowing the client to login with the given SASL
  422. * authentication mechanism.
  423. */
  424. static CURLcode pop3_perform_auth(struct Curl_easy *data,
  425. const char *mech,
  426. const struct bufref *initresp)
  427. {
  428. CURLcode result = CURLE_OK;
  429. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  430. const char *ir = (const char *) Curl_bufref_ptr(initresp);
  431. if(ir) { /* AUTH <mech> ...<crlf> */
  432. /* Send the AUTH command with the initial response */
  433. result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s %s", mech, ir);
  434. }
  435. else {
  436. /* Send the AUTH command */
  437. result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s", mech);
  438. }
  439. return result;
  440. }
  441. /***********************************************************************
  442. *
  443. * pop3_continue_auth()
  444. *
  445. * Sends SASL continuation data.
  446. */
  447. static CURLcode pop3_continue_auth(struct Curl_easy *data,
  448. const char *mech,
  449. const struct bufref *resp)
  450. {
  451. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  452. (void)mech;
  453. return Curl_pp_sendf(data, &pop3c->pp,
  454. "%s", (const char *) Curl_bufref_ptr(resp));
  455. }
  456. /***********************************************************************
  457. *
  458. * pop3_cancel_auth()
  459. *
  460. * Sends SASL cancellation.
  461. */
  462. static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech)
  463. {
  464. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  465. (void)mech;
  466. return Curl_pp_sendf(data, &pop3c->pp, "*");
  467. }
  468. /***********************************************************************
  469. *
  470. * pop3_perform_authentication()
  471. *
  472. * Initiates the authentication sequence, with the appropriate SASL
  473. * authentication mechanism, falling back to APOP and clear text should a
  474. * common mechanism not be available between the client and server.
  475. */
  476. static CURLcode pop3_perform_authentication(struct Curl_easy *data,
  477. struct connectdata *conn)
  478. {
  479. CURLcode result = CURLE_OK;
  480. struct pop3_conn *pop3c = &conn->proto.pop3c;
  481. saslprogress progress = SASL_IDLE;
  482. /* Check we have enough data to authenticate with and end the
  483. connect phase if we don't */
  484. if(!Curl_sasl_can_authenticate(&pop3c->sasl, data)) {
  485. pop3_state(data, POP3_STOP);
  486. return result;
  487. }
  488. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) {
  489. /* Calculate the SASL login details */
  490. result = Curl_sasl_start(&pop3c->sasl, data, FALSE, &progress);
  491. if(!result)
  492. if(progress == SASL_INPROGRESS)
  493. pop3_state(data, POP3_AUTH);
  494. }
  495. if(!result && progress == SASL_IDLE) {
  496. #ifndef CURL_DISABLE_DIGEST_AUTH
  497. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
  498. /* Perform APOP authentication */
  499. result = pop3_perform_apop(data, conn);
  500. else
  501. #endif
  502. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
  503. /* Perform clear text authentication */
  504. result = pop3_perform_user(data, conn);
  505. else {
  506. /* Other mechanisms not supported */
  507. infof(data, "No known authentication mechanisms supported");
  508. result = CURLE_LOGIN_DENIED;
  509. }
  510. }
  511. return result;
  512. }
  513. /***********************************************************************
  514. *
  515. * pop3_perform_command()
  516. *
  517. * Sends a POP3 based command.
  518. */
  519. static CURLcode pop3_perform_command(struct Curl_easy *data)
  520. {
  521. CURLcode result = CURLE_OK;
  522. struct connectdata *conn = data->conn;
  523. struct POP3 *pop3 = data->req.p.pop3;
  524. const char *command = NULL;
  525. /* Calculate the default command */
  526. if(pop3->id[0] == '\0' || data->set.list_only) {
  527. command = "LIST";
  528. if(pop3->id[0] != '\0')
  529. /* Message specific LIST so skip the BODY transfer */
  530. pop3->transfer = PPTRANSFER_INFO;
  531. }
  532. else
  533. command = "RETR";
  534. /* Send the command */
  535. if(pop3->id[0] != '\0')
  536. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s %s",
  537. (pop3->custom && pop3->custom[0] != '\0' ?
  538. pop3->custom : command), pop3->id);
  539. else
  540. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s",
  541. (pop3->custom && pop3->custom[0] != '\0' ?
  542. pop3->custom : command));
  543. if(!result)
  544. pop3_state(data, POP3_COMMAND);
  545. return result;
  546. }
  547. /***********************************************************************
  548. *
  549. * pop3_perform_quit()
  550. *
  551. * Performs the quit action prior to sclose() be called.
  552. */
  553. static CURLcode pop3_perform_quit(struct Curl_easy *data,
  554. struct connectdata *conn)
  555. {
  556. /* Send the QUIT command */
  557. CURLcode result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s", "QUIT");
  558. if(!result)
  559. pop3_state(data, POP3_QUIT);
  560. return result;
  561. }
  562. /* For the initial server greeting */
  563. static CURLcode pop3_state_servergreet_resp(struct Curl_easy *data,
  564. int pop3code,
  565. pop3state instate)
  566. {
  567. CURLcode result = CURLE_OK;
  568. struct connectdata *conn = data->conn;
  569. struct pop3_conn *pop3c = &conn->proto.pop3c;
  570. const char *line = Curl_dyn_ptr(&data->conn->proto.pop3c.pp.recvbuf);
  571. size_t len = data->conn->proto.pop3c.pp.nfinal;
  572. (void)instate; /* no use for this yet */
  573. if(pop3code != '+') {
  574. failf(data, "Got unexpected pop3-server response");
  575. result = CURLE_WEIRD_SERVER_REPLY;
  576. }
  577. else if(len > 3) {
  578. /* Does the server support APOP authentication? */
  579. char *lt;
  580. char *gt = NULL;
  581. /* Look for the APOP timestamp */
  582. lt = memchr(line, '<', len);
  583. if(lt)
  584. /* search the remainder for '>' */
  585. gt = memchr(lt, '>', len - (lt - line));
  586. if(gt) {
  587. /* the length of the timestamp, including the brackets */
  588. size_t timestamplen = gt - lt + 1;
  589. char *at = memchr(lt, '@', timestamplen);
  590. /* If the timestamp does not contain '@' it is not (as required by
  591. RFC-1939) conformant to the RFC-822 message id syntax, and we
  592. therefore do not use APOP authentication. */
  593. if(at) {
  594. /* dupe the timestamp */
  595. pop3c->apoptimestamp = Curl_memdup0(lt, timestamplen);
  596. if(!pop3c->apoptimestamp)
  597. return CURLE_OUT_OF_MEMORY;
  598. /* Store the APOP capability */
  599. pop3c->authtypes |= POP3_TYPE_APOP;
  600. }
  601. }
  602. if(!result)
  603. result = pop3_perform_capa(data, conn);
  604. }
  605. return result;
  606. }
  607. /* For CAPA responses */
  608. static CURLcode pop3_state_capa_resp(struct Curl_easy *data, int pop3code,
  609. pop3state instate)
  610. {
  611. CURLcode result = CURLE_OK;
  612. struct connectdata *conn = data->conn;
  613. struct pop3_conn *pop3c = &conn->proto.pop3c;
  614. const char *line = Curl_dyn_ptr(&data->conn->proto.pop3c.pp.recvbuf);
  615. size_t len = data->conn->proto.pop3c.pp.nfinal;
  616. (void)instate; /* no use for this yet */
  617. /* Do we have a untagged continuation response? */
  618. if(pop3code == '*') {
  619. /* Does the server support the STLS capability? */
  620. if(len >= 4 && !memcmp(line, "STLS", 4))
  621. pop3c->tls_supported = TRUE;
  622. /* Does the server support clear text authentication? */
  623. else if(len >= 4 && !memcmp(line, "USER", 4))
  624. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  625. /* Does the server support SASL based authentication? */
  626. else if(len >= 5 && !memcmp(line, "SASL ", 5)) {
  627. pop3c->authtypes |= POP3_TYPE_SASL;
  628. /* Advance past the SASL keyword */
  629. line += 5;
  630. len -= 5;
  631. /* Loop through the data line */
  632. for(;;) {
  633. size_t llen;
  634. size_t wordlen;
  635. unsigned short mechbit;
  636. while(len &&
  637. (*line == ' ' || *line == '\t' ||
  638. *line == '\r' || *line == '\n')) {
  639. line++;
  640. len--;
  641. }
  642. if(!len)
  643. break;
  644. /* Extract the word */
  645. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  646. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  647. line[wordlen] != '\n';)
  648. wordlen++;
  649. /* Test the word for a matching authentication mechanism */
  650. mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
  651. if(mechbit && llen == wordlen)
  652. pop3c->sasl.authmechs |= mechbit;
  653. line += wordlen;
  654. len -= wordlen;
  655. }
  656. }
  657. }
  658. else {
  659. /* Clear text is supported when CAPA isn't recognised */
  660. if(pop3code != '+')
  661. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  662. if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET))
  663. result = pop3_perform_authentication(data, conn);
  664. else if(pop3code == '+' && pop3c->tls_supported)
  665. /* Switch to TLS connection now */
  666. result = pop3_perform_starttls(data, conn);
  667. else if(data->set.use_ssl <= CURLUSESSL_TRY)
  668. /* Fallback and carry on with authentication */
  669. result = pop3_perform_authentication(data, conn);
  670. else {
  671. failf(data, "STLS not supported.");
  672. result = CURLE_USE_SSL_FAILED;
  673. }
  674. }
  675. return result;
  676. }
  677. /* For STARTTLS responses */
  678. static CURLcode pop3_state_starttls_resp(struct Curl_easy *data,
  679. struct connectdata *conn,
  680. int pop3code,
  681. pop3state instate)
  682. {
  683. CURLcode result = CURLE_OK;
  684. (void)instate; /* no use for this yet */
  685. /* Pipelining in response is forbidden. */
  686. if(data->conn->proto.pop3c.pp.overflow)
  687. return CURLE_WEIRD_SERVER_REPLY;
  688. if(pop3code != '+') {
  689. if(data->set.use_ssl != CURLUSESSL_TRY) {
  690. failf(data, "STARTTLS denied");
  691. result = CURLE_USE_SSL_FAILED;
  692. }
  693. else
  694. result = pop3_perform_authentication(data, conn);
  695. }
  696. else
  697. result = pop3_perform_upgrade_tls(data, conn);
  698. return result;
  699. }
  700. /* For SASL authentication responses */
  701. static CURLcode pop3_state_auth_resp(struct Curl_easy *data,
  702. int pop3code,
  703. pop3state instate)
  704. {
  705. CURLcode result = CURLE_OK;
  706. struct connectdata *conn = data->conn;
  707. struct pop3_conn *pop3c = &conn->proto.pop3c;
  708. saslprogress progress;
  709. (void)instate; /* no use for this yet */
  710. result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress);
  711. if(!result)
  712. switch(progress) {
  713. case SASL_DONE:
  714. pop3_state(data, POP3_STOP); /* Authenticated */
  715. break;
  716. case SASL_IDLE: /* No mechanism left after cancellation */
  717. #ifndef CURL_DISABLE_DIGEST_AUTH
  718. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
  719. /* Perform APOP authentication */
  720. result = pop3_perform_apop(data, conn);
  721. else
  722. #endif
  723. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
  724. /* Perform clear text authentication */
  725. result = pop3_perform_user(data, conn);
  726. else {
  727. failf(data, "Authentication cancelled");
  728. result = CURLE_LOGIN_DENIED;
  729. }
  730. break;
  731. default:
  732. break;
  733. }
  734. return result;
  735. }
  736. #ifndef CURL_DISABLE_DIGEST_AUTH
  737. /* For APOP responses */
  738. static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code,
  739. pop3state instate)
  740. {
  741. CURLcode result = CURLE_OK;
  742. (void)instate; /* no use for this yet */
  743. if(pop3code != '+') {
  744. failf(data, "Authentication failed: %d", pop3code);
  745. result = CURLE_LOGIN_DENIED;
  746. }
  747. else
  748. /* End of connect phase */
  749. pop3_state(data, POP3_STOP);
  750. return result;
  751. }
  752. #endif
  753. /* For USER responses */
  754. static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code,
  755. pop3state instate)
  756. {
  757. CURLcode result = CURLE_OK;
  758. struct connectdata *conn = data->conn;
  759. (void)instate; /* no use for this yet */
  760. if(pop3code != '+') {
  761. failf(data, "Access denied. %c", pop3code);
  762. result = CURLE_LOGIN_DENIED;
  763. }
  764. else
  765. /* Send the PASS command */
  766. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "PASS %s",
  767. conn->passwd ? conn->passwd : "");
  768. if(!result)
  769. pop3_state(data, POP3_PASS);
  770. return result;
  771. }
  772. /* For PASS responses */
  773. static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code,
  774. pop3state instate)
  775. {
  776. CURLcode result = CURLE_OK;
  777. (void)instate; /* no use for this yet */
  778. if(pop3code != '+') {
  779. failf(data, "Access denied. %c", pop3code);
  780. result = CURLE_LOGIN_DENIED;
  781. }
  782. else
  783. /* End of connect phase */
  784. pop3_state(data, POP3_STOP);
  785. return result;
  786. }
  787. /* For command responses */
  788. static CURLcode pop3_state_command_resp(struct Curl_easy *data,
  789. int pop3code,
  790. pop3state instate)
  791. {
  792. CURLcode result = CURLE_OK;
  793. struct connectdata *conn = data->conn;
  794. struct POP3 *pop3 = data->req.p.pop3;
  795. struct pop3_conn *pop3c = &conn->proto.pop3c;
  796. struct pingpong *pp = &pop3c->pp;
  797. (void)instate; /* no use for this yet */
  798. if(pop3code != '+') {
  799. pop3_state(data, POP3_STOP);
  800. return CURLE_WEIRD_SERVER_REPLY;
  801. }
  802. /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
  803. EOB string so count this is two matching bytes. This is necessary to make
  804. the code detect the EOB if the only data than comes now is %2e CR LF like
  805. when there is no body to return. */
  806. pop3c->eob = 2;
  807. /* But since this initial CR LF pair is not part of the actual body, we set
  808. the strip counter here so that these bytes won't be delivered. */
  809. pop3c->strip = 2;
  810. if(pop3->transfer == PPTRANSFER_BODY) {
  811. /* POP3 download */
  812. Curl_xfer_setup(data, FIRSTSOCKET, -1, FALSE, -1);
  813. if(pp->overflow) {
  814. /* The recv buffer contains data that is actually body content so send
  815. it as such. Note that there may even be additional "headers" after
  816. the body */
  817. /* keep only the overflow */
  818. Curl_dyn_tail(&pp->recvbuf, pp->overflow);
  819. pp->nfinal = 0; /* done */
  820. if(!data->req.no_body) {
  821. result = Curl_pop3_write(data, Curl_dyn_ptr(&pp->recvbuf),
  822. Curl_dyn_len(&pp->recvbuf));
  823. if(result)
  824. return result;
  825. }
  826. /* reset the buffer */
  827. Curl_dyn_reset(&pp->recvbuf);
  828. pp->overflow = 0;
  829. }
  830. }
  831. else
  832. pp->overflow = 0;
  833. /* End of DO phase */
  834. pop3_state(data, POP3_STOP);
  835. return result;
  836. }
  837. static CURLcode pop3_statemachine(struct Curl_easy *data,
  838. struct connectdata *conn)
  839. {
  840. CURLcode result = CURLE_OK;
  841. int pop3code;
  842. struct pop3_conn *pop3c = &conn->proto.pop3c;
  843. struct pingpong *pp = &pop3c->pp;
  844. size_t nread = 0;
  845. (void)data;
  846. /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
  847. if(pop3c->state == POP3_UPGRADETLS)
  848. return pop3_perform_upgrade_tls(data, conn);
  849. /* Flush any data that needs to be sent */
  850. if(pp->sendleft)
  851. return Curl_pp_flushsend(data, pp);
  852. do {
  853. /* Read the response from the server */
  854. result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &pop3code, &nread);
  855. if(result)
  856. return result;
  857. if(!pop3code)
  858. break;
  859. /* We have now received a full POP3 server response */
  860. switch(pop3c->state) {
  861. case POP3_SERVERGREET:
  862. result = pop3_state_servergreet_resp(data, pop3code, pop3c->state);
  863. break;
  864. case POP3_CAPA:
  865. result = pop3_state_capa_resp(data, pop3code, pop3c->state);
  866. break;
  867. case POP3_STARTTLS:
  868. result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state);
  869. break;
  870. case POP3_AUTH:
  871. result = pop3_state_auth_resp(data, pop3code, pop3c->state);
  872. break;
  873. #ifndef CURL_DISABLE_DIGEST_AUTH
  874. case POP3_APOP:
  875. result = pop3_state_apop_resp(data, pop3code, pop3c->state);
  876. break;
  877. #endif
  878. case POP3_USER:
  879. result = pop3_state_user_resp(data, pop3code, pop3c->state);
  880. break;
  881. case POP3_PASS:
  882. result = pop3_state_pass_resp(data, pop3code, pop3c->state);
  883. break;
  884. case POP3_COMMAND:
  885. result = pop3_state_command_resp(data, pop3code, pop3c->state);
  886. break;
  887. case POP3_QUIT:
  888. pop3_state(data, POP3_STOP);
  889. break;
  890. default:
  891. /* internal error */
  892. pop3_state(data, POP3_STOP);
  893. break;
  894. }
  895. } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
  896. return result;
  897. }
  898. /* Called repeatedly until done from multi.c */
  899. static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done)
  900. {
  901. CURLcode result = CURLE_OK;
  902. struct connectdata *conn = data->conn;
  903. struct pop3_conn *pop3c = &conn->proto.pop3c;
  904. if((conn->handler->flags & PROTOPT_SSL) && !pop3c->ssldone) {
  905. bool ssldone = FALSE;
  906. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  907. pop3c->ssldone = ssldone;
  908. if(result || !pop3c->ssldone)
  909. return result;
  910. }
  911. result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE);
  912. *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
  913. return result;
  914. }
  915. static CURLcode pop3_block_statemach(struct Curl_easy *data,
  916. struct connectdata *conn,
  917. bool disconnecting)
  918. {
  919. CURLcode result = CURLE_OK;
  920. struct pop3_conn *pop3c = &conn->proto.pop3c;
  921. while(pop3c->state != POP3_STOP && !result)
  922. result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting);
  923. return result;
  924. }
  925. /* Allocate and initialize the POP3 struct for the current Curl_easy if
  926. required */
  927. static CURLcode pop3_init(struct Curl_easy *data)
  928. {
  929. CURLcode result = CURLE_OK;
  930. struct POP3 *pop3;
  931. pop3 = data->req.p.pop3 = calloc(1, sizeof(struct POP3));
  932. if(!pop3)
  933. result = CURLE_OUT_OF_MEMORY;
  934. return result;
  935. }
  936. /* For the POP3 "protocol connect" and "doing" phases only */
  937. static int pop3_getsock(struct Curl_easy *data,
  938. struct connectdata *conn, curl_socket_t *socks)
  939. {
  940. return Curl_pp_getsock(data, &conn->proto.pop3c.pp, socks);
  941. }
  942. /***********************************************************************
  943. *
  944. * pop3_connect()
  945. *
  946. * This function should do everything that is to be considered a part of the
  947. * connection phase.
  948. *
  949. * The variable 'done' points to will be TRUE if the protocol-layer connect
  950. * phase is done when this function returns, or FALSE if not.
  951. */
  952. static CURLcode pop3_connect(struct Curl_easy *data, bool *done)
  953. {
  954. CURLcode result = CURLE_OK;
  955. struct connectdata *conn = data->conn;
  956. struct pop3_conn *pop3c = &conn->proto.pop3c;
  957. struct pingpong *pp = &pop3c->pp;
  958. *done = FALSE; /* default to not done yet */
  959. /* We always support persistent connections in POP3 */
  960. connkeep(conn, "POP3 default");
  961. PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp);
  962. /* Set the default preferred authentication type and mechanism */
  963. pop3c->preftype = POP3_TYPE_ANY;
  964. Curl_sasl_init(&pop3c->sasl, data, &saslpop3);
  965. /* Initialise the pingpong layer */
  966. Curl_pp_init(pp);
  967. /* Parse the URL options */
  968. result = pop3_parse_url_options(conn);
  969. if(result)
  970. return result;
  971. /* Start off waiting for the server greeting response */
  972. pop3_state(data, POP3_SERVERGREET);
  973. result = pop3_multi_statemach(data, done);
  974. return result;
  975. }
  976. /***********************************************************************
  977. *
  978. * pop3_done()
  979. *
  980. * The DONE function. This does what needs to be done after a single DO has
  981. * performed.
  982. *
  983. * Input argument is already checked for validity.
  984. */
  985. static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
  986. bool premature)
  987. {
  988. CURLcode result = CURLE_OK;
  989. struct POP3 *pop3 = data->req.p.pop3;
  990. (void)premature;
  991. if(!pop3)
  992. return CURLE_OK;
  993. if(status) {
  994. connclose(data->conn, "POP3 done with bad status");
  995. result = status; /* use the already set error code */
  996. }
  997. /* Cleanup our per-request based variables */
  998. Curl_safefree(pop3->id);
  999. Curl_safefree(pop3->custom);
  1000. /* Clear the transfer mode for the next request */
  1001. pop3->transfer = PPTRANSFER_BODY;
  1002. return result;
  1003. }
  1004. /***********************************************************************
  1005. *
  1006. * pop3_perform()
  1007. *
  1008. * This is the actual DO function for POP3. Get a message/listing according to
  1009. * the options previously setup.
  1010. */
  1011. static CURLcode pop3_perform(struct Curl_easy *data, bool *connected,
  1012. bool *dophase_done)
  1013. {
  1014. /* This is POP3 and no proxy */
  1015. CURLcode result = CURLE_OK;
  1016. struct POP3 *pop3 = data->req.p.pop3;
  1017. DEBUGF(infof(data, "DO phase starts"));
  1018. if(data->req.no_body) {
  1019. /* Requested no body means no transfer */
  1020. pop3->transfer = PPTRANSFER_INFO;
  1021. }
  1022. *dophase_done = FALSE; /* not done yet */
  1023. /* Start the first command in the DO phase */
  1024. result = pop3_perform_command(data);
  1025. if(result)
  1026. return result;
  1027. /* Run the state-machine */
  1028. result = pop3_multi_statemach(data, dophase_done);
  1029. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  1030. if(*dophase_done)
  1031. DEBUGF(infof(data, "DO phase is complete"));
  1032. return result;
  1033. }
  1034. /***********************************************************************
  1035. *
  1036. * pop3_do()
  1037. *
  1038. * This function is registered as 'curl_do' function. It decodes the path
  1039. * parts etc as a wrapper to the actual DO function (pop3_perform).
  1040. *
  1041. * The input argument is already checked for validity.
  1042. */
  1043. static CURLcode pop3_do(struct Curl_easy *data, bool *done)
  1044. {
  1045. CURLcode result = CURLE_OK;
  1046. *done = FALSE; /* default to false */
  1047. /* Parse the URL path */
  1048. result = pop3_parse_url_path(data);
  1049. if(result)
  1050. return result;
  1051. /* Parse the custom request */
  1052. result = pop3_parse_custom_request(data);
  1053. if(result)
  1054. return result;
  1055. result = pop3_regular_transfer(data, done);
  1056. return result;
  1057. }
  1058. /***********************************************************************
  1059. *
  1060. * pop3_disconnect()
  1061. *
  1062. * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
  1063. * resources. BLOCKING.
  1064. */
  1065. static CURLcode pop3_disconnect(struct Curl_easy *data,
  1066. struct connectdata *conn, bool dead_connection)
  1067. {
  1068. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1069. (void)data;
  1070. /* We cannot send quit unconditionally. If this connection is stale or
  1071. bad in any way, sending quit and waiting around here will make the
  1072. disconnect wait in vain and cause more problems than we need to. */
  1073. if(!dead_connection && conn->bits.protoconnstart) {
  1074. if(!pop3_perform_quit(data, conn))
  1075. (void)pop3_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
  1076. }
  1077. /* Disconnect from the server */
  1078. Curl_pp_disconnect(&pop3c->pp);
  1079. /* Cleanup the SASL module */
  1080. Curl_sasl_cleanup(conn, pop3c->sasl.authused);
  1081. /* Cleanup our connection based variables */
  1082. Curl_safefree(pop3c->apoptimestamp);
  1083. return CURLE_OK;
  1084. }
  1085. /* Call this when the DO phase has completed */
  1086. static CURLcode pop3_dophase_done(struct Curl_easy *data, bool connected)
  1087. {
  1088. (void)data;
  1089. (void)connected;
  1090. return CURLE_OK;
  1091. }
  1092. /* Called from multi.c while DOing */
  1093. static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done)
  1094. {
  1095. CURLcode result = pop3_multi_statemach(data, dophase_done);
  1096. if(result)
  1097. DEBUGF(infof(data, "DO phase failed"));
  1098. else if(*dophase_done) {
  1099. result = pop3_dophase_done(data, FALSE /* not connected */);
  1100. DEBUGF(infof(data, "DO phase is complete"));
  1101. }
  1102. return result;
  1103. }
  1104. /***********************************************************************
  1105. *
  1106. * pop3_regular_transfer()
  1107. *
  1108. * The input argument is already checked for validity.
  1109. *
  1110. * Performs all commands done before a regular transfer between a local and a
  1111. * remote host.
  1112. */
  1113. static CURLcode pop3_regular_transfer(struct Curl_easy *data,
  1114. bool *dophase_done)
  1115. {
  1116. CURLcode result = CURLE_OK;
  1117. bool connected = FALSE;
  1118. /* Make sure size is unknown at this point */
  1119. data->req.size = -1;
  1120. /* Set the progress data */
  1121. Curl_pgrsSetUploadCounter(data, 0);
  1122. Curl_pgrsSetDownloadCounter(data, 0);
  1123. Curl_pgrsSetUploadSize(data, -1);
  1124. Curl_pgrsSetDownloadSize(data, -1);
  1125. /* Carry out the perform */
  1126. result = pop3_perform(data, &connected, dophase_done);
  1127. /* Perform post DO phase operations if necessary */
  1128. if(!result && *dophase_done)
  1129. result = pop3_dophase_done(data, connected);
  1130. return result;
  1131. }
  1132. static CURLcode pop3_setup_connection(struct Curl_easy *data,
  1133. struct connectdata *conn)
  1134. {
  1135. /* Initialise the POP3 layer */
  1136. CURLcode result = pop3_init(data);
  1137. if(result)
  1138. return result;
  1139. /* Clear the TLS upgraded flag */
  1140. conn->bits.tls_upgraded = FALSE;
  1141. return CURLE_OK;
  1142. }
  1143. /***********************************************************************
  1144. *
  1145. * pop3_parse_url_options()
  1146. *
  1147. * Parse the URL login options.
  1148. */
  1149. static CURLcode pop3_parse_url_options(struct connectdata *conn)
  1150. {
  1151. CURLcode result = CURLE_OK;
  1152. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1153. const char *ptr = conn->options;
  1154. while(!result && ptr && *ptr) {
  1155. const char *key = ptr;
  1156. const char *value;
  1157. while(*ptr && *ptr != '=')
  1158. ptr++;
  1159. value = ptr + 1;
  1160. while(*ptr && *ptr != ';')
  1161. ptr++;
  1162. if(strncasecompare(key, "AUTH=", 5)) {
  1163. result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
  1164. value, ptr - value);
  1165. if(result && strncasecompare(value, "+APOP", ptr - value)) {
  1166. pop3c->preftype = POP3_TYPE_APOP;
  1167. pop3c->sasl.prefmech = SASL_AUTH_NONE;
  1168. result = CURLE_OK;
  1169. }
  1170. }
  1171. else
  1172. result = CURLE_URL_MALFORMAT;
  1173. if(*ptr == ';')
  1174. ptr++;
  1175. }
  1176. if(pop3c->preftype != POP3_TYPE_APOP)
  1177. switch(pop3c->sasl.prefmech) {
  1178. case SASL_AUTH_NONE:
  1179. pop3c->preftype = POP3_TYPE_NONE;
  1180. break;
  1181. case SASL_AUTH_DEFAULT:
  1182. pop3c->preftype = POP3_TYPE_ANY;
  1183. break;
  1184. default:
  1185. pop3c->preftype = POP3_TYPE_SASL;
  1186. break;
  1187. }
  1188. return result;
  1189. }
  1190. /***********************************************************************
  1191. *
  1192. * pop3_parse_url_path()
  1193. *
  1194. * Parse the URL path into separate path components.
  1195. */
  1196. static CURLcode pop3_parse_url_path(struct Curl_easy *data)
  1197. {
  1198. /* The POP3 struct is already initialised in pop3_connect() */
  1199. struct POP3 *pop3 = data->req.p.pop3;
  1200. const char *path = &data->state.up.path[1]; /* skip leading path */
  1201. /* URL decode the path for the message ID */
  1202. return Curl_urldecode(path, 0, &pop3->id, NULL, REJECT_CTRL);
  1203. }
  1204. /***********************************************************************
  1205. *
  1206. * pop3_parse_custom_request()
  1207. *
  1208. * Parse the custom request.
  1209. */
  1210. static CURLcode pop3_parse_custom_request(struct Curl_easy *data)
  1211. {
  1212. CURLcode result = CURLE_OK;
  1213. struct POP3 *pop3 = data->req.p.pop3;
  1214. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1215. /* URL decode the custom request */
  1216. if(custom)
  1217. result = Curl_urldecode(custom, 0, &pop3->custom, NULL, REJECT_CTRL);
  1218. return result;
  1219. }
  1220. /***********************************************************************
  1221. *
  1222. * Curl_pop3_write()
  1223. *
  1224. * This function scans the body after the end-of-body and writes everything
  1225. * until the end is found.
  1226. */
  1227. CURLcode Curl_pop3_write(struct Curl_easy *data, const char *str, size_t nread)
  1228. {
  1229. /* This code could be made into a special function in the handler struct */
  1230. CURLcode result = CURLE_OK;
  1231. struct SingleRequest *k = &data->req;
  1232. struct connectdata *conn = data->conn;
  1233. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1234. bool strip_dot = FALSE;
  1235. size_t last = 0;
  1236. size_t i;
  1237. /* Search through the buffer looking for the end-of-body marker which is
  1238. 5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
  1239. the eob so the server will have prefixed it with an extra dot which we
  1240. need to strip out. Additionally the marker could of course be spread out
  1241. over 5 different data chunks. */
  1242. for(i = 0; i < nread; i++) {
  1243. size_t prev = pop3c->eob;
  1244. switch(str[i]) {
  1245. case 0x0d:
  1246. if(pop3c->eob == 0) {
  1247. pop3c->eob++;
  1248. if(i) {
  1249. /* Write out the body part that didn't match */
  1250. result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
  1251. i - last);
  1252. if(result)
  1253. return result;
  1254. last = i;
  1255. }
  1256. }
  1257. else if(pop3c->eob == 3)
  1258. pop3c->eob++;
  1259. else
  1260. /* If the character match wasn't at position 0 or 3 then restart the
  1261. pattern matching */
  1262. pop3c->eob = 1;
  1263. break;
  1264. case 0x0a:
  1265. if(pop3c->eob == 1 || pop3c->eob == 4)
  1266. pop3c->eob++;
  1267. else
  1268. /* If the character match wasn't at position 1 or 4 then start the
  1269. search again */
  1270. pop3c->eob = 0;
  1271. break;
  1272. case 0x2e:
  1273. if(pop3c->eob == 2)
  1274. pop3c->eob++;
  1275. else if(pop3c->eob == 3) {
  1276. /* We have an extra dot after the CRLF which we need to strip off */
  1277. strip_dot = TRUE;
  1278. pop3c->eob = 0;
  1279. }
  1280. else
  1281. /* If the character match wasn't at position 2 then start the search
  1282. again */
  1283. pop3c->eob = 0;
  1284. break;
  1285. default:
  1286. pop3c->eob = 0;
  1287. break;
  1288. }
  1289. /* Did we have a partial match which has subsequently failed? */
  1290. if(prev && prev >= pop3c->eob) {
  1291. /* Strip can only be non-zero for the very first mismatch after CRLF
  1292. and then both prev and strip are equal and nothing will be output
  1293. below */
  1294. while(prev && pop3c->strip) {
  1295. prev--;
  1296. pop3c->strip--;
  1297. }
  1298. if(prev) {
  1299. /* If the partial match was the CRLF and dot then only write the CRLF
  1300. as the server would have inserted the dot */
  1301. if(strip_dot && prev - 1 > 0) {
  1302. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)POP3_EOB,
  1303. prev - 1);
  1304. }
  1305. else if(!strip_dot) {
  1306. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)POP3_EOB,
  1307. prev);
  1308. }
  1309. else {
  1310. result = CURLE_OK;
  1311. }
  1312. if(result)
  1313. return result;
  1314. last = i;
  1315. strip_dot = FALSE;
  1316. }
  1317. }
  1318. }
  1319. if(pop3c->eob == POP3_EOB_LEN) {
  1320. /* We have a full match so the transfer is done, however we must transfer
  1321. the CRLF at the start of the EOB as this is considered to be part of the
  1322. message as per RFC-1939, sect. 3 */
  1323. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)POP3_EOB, 2);
  1324. k->keepon &= ~KEEP_RECV;
  1325. pop3c->eob = 0;
  1326. return result;
  1327. }
  1328. if(pop3c->eob)
  1329. /* While EOB is matching nothing should be output */
  1330. return CURLE_OK;
  1331. if(nread - last) {
  1332. result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
  1333. nread - last);
  1334. }
  1335. return result;
  1336. }
  1337. #endif /* CURL_DISABLE_POP3 */