2
0

pop3.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. /* The last 3 #include files should be in this order */
  77. #include "curl_printf.h"
  78. #include "curl_memory.h"
  79. #include "memdebug.h"
  80. /* Local API functions */
  81. static CURLcode pop3_regular_transfer(struct Curl_easy *data, bool *done);
  82. static CURLcode pop3_do(struct Curl_easy *data, bool *done);
  83. static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
  84. bool premature);
  85. static CURLcode pop3_connect(struct Curl_easy *data, bool *done);
  86. static CURLcode pop3_disconnect(struct Curl_easy *data,
  87. struct connectdata *conn, bool dead);
  88. static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done);
  89. static int pop3_getsock(struct Curl_easy *data,
  90. struct connectdata *conn, curl_socket_t *socks);
  91. static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done);
  92. static CURLcode pop3_setup_connection(struct Curl_easy *data,
  93. struct connectdata *conn);
  94. static CURLcode pop3_parse_url_options(struct connectdata *conn);
  95. static CURLcode pop3_parse_url_path(struct Curl_easy *data);
  96. static CURLcode pop3_parse_custom_request(struct Curl_easy *data);
  97. static CURLcode pop3_perform_auth(struct Curl_easy *data, const char *mech,
  98. const struct bufref *initresp);
  99. static CURLcode pop3_continue_auth(struct Curl_easy *data, const char *mech,
  100. const struct bufref *resp);
  101. static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech);
  102. static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out);
  103. /*
  104. * POP3 protocol handler.
  105. */
  106. const struct Curl_handler Curl_handler_pop3 = {
  107. "POP3", /* scheme */
  108. pop3_setup_connection, /* setup_connection */
  109. pop3_do, /* do_it */
  110. pop3_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. pop3_connect, /* connect_it */
  113. pop3_multi_statemach, /* connecting */
  114. pop3_doing, /* doing */
  115. pop3_getsock, /* proto_getsock */
  116. pop3_getsock, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. pop3_disconnect, /* disconnect */
  120. ZERO_NULL, /* readwrite */
  121. ZERO_NULL, /* connection_check */
  122. ZERO_NULL, /* attach connection */
  123. PORT_POP3, /* defport */
  124. CURLPROTO_POP3, /* protocol */
  125. CURLPROTO_POP3, /* family */
  126. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */
  127. PROTOPT_URLOPTIONS
  128. };
  129. #ifdef USE_SSL
  130. /*
  131. * POP3S protocol handler.
  132. */
  133. const struct Curl_handler Curl_handler_pop3s = {
  134. "POP3S", /* scheme */
  135. pop3_setup_connection, /* setup_connection */
  136. pop3_do, /* do_it */
  137. pop3_done, /* done */
  138. ZERO_NULL, /* do_more */
  139. pop3_connect, /* connect_it */
  140. pop3_multi_statemach, /* connecting */
  141. pop3_doing, /* doing */
  142. pop3_getsock, /* proto_getsock */
  143. pop3_getsock, /* doing_getsock */
  144. ZERO_NULL, /* domore_getsock */
  145. ZERO_NULL, /* perform_getsock */
  146. pop3_disconnect, /* disconnect */
  147. ZERO_NULL, /* readwrite */
  148. ZERO_NULL, /* connection_check */
  149. ZERO_NULL, /* attach connection */
  150. PORT_POP3S, /* defport */
  151. CURLPROTO_POP3S, /* protocol */
  152. CURLPROTO_POP3, /* family */
  153. PROTOPT_CLOSEACTION | PROTOPT_SSL
  154. | PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS /* flags */
  155. };
  156. #endif
  157. /* SASL parameters for the pop3 protocol */
  158. static const struct SASLproto saslpop3 = {
  159. "pop", /* The service name */
  160. pop3_perform_auth, /* Send authentication command */
  161. pop3_continue_auth, /* Send authentication continuation */
  162. pop3_cancel_auth, /* Send authentication cancellation */
  163. pop3_get_message, /* Get SASL response message */
  164. 255 - 8, /* Max line len - strlen("AUTH ") - 1 space - crlf */
  165. '*', /* Code received when continuation is expected */
  166. '+', /* Code to receive upon authentication success */
  167. SASL_AUTH_DEFAULT, /* Default mechanisms */
  168. SASL_FLAG_BASE64 /* Configuration flags */
  169. };
  170. #ifdef USE_SSL
  171. static void pop3_to_pop3s(struct connectdata *conn)
  172. {
  173. /* Change the connection handler */
  174. conn->handler = &Curl_handler_pop3s;
  175. /* Set the connection's upgraded to TLS flag */
  176. conn->bits.tls_upgraded = TRUE;
  177. }
  178. #else
  179. #define pop3_to_pop3s(x) Curl_nop_stmt
  180. #endif
  181. /***********************************************************************
  182. *
  183. * pop3_endofresp()
  184. *
  185. * Checks for an ending POP3 status code at the start of the given string, but
  186. * also detects the APOP timestamp from the server greeting and various
  187. * capabilities from the CAPA response including the supported authentication
  188. * types and allowed SASL mechanisms.
  189. */
  190. static bool pop3_endofresp(struct Curl_easy *data, struct connectdata *conn,
  191. char *line, size_t len, int *resp)
  192. {
  193. struct pop3_conn *pop3c = &conn->proto.pop3c;
  194. (void)data;
  195. /* Do we have an error response? */
  196. if(len >= 4 && !memcmp("-ERR", line, 4)) {
  197. *resp = '-';
  198. return TRUE;
  199. }
  200. /* Are we processing CAPA command responses? */
  201. if(pop3c->state == POP3_CAPA) {
  202. /* Do we have the terminating line? */
  203. if(len >= 1 && line[0] == '.')
  204. /* Treat the response as a success */
  205. *resp = '+';
  206. else
  207. /* Treat the response as an untagged continuation */
  208. *resp = '*';
  209. return TRUE;
  210. }
  211. /* Do we have a success response? */
  212. if(len >= 3 && !memcmp("+OK", line, 3)) {
  213. *resp = '+';
  214. return TRUE;
  215. }
  216. /* Do we have a continuation response? */
  217. if(len >= 1 && line[0] == '+') {
  218. *resp = '*';
  219. return TRUE;
  220. }
  221. return FALSE; /* Nothing for us */
  222. }
  223. /***********************************************************************
  224. *
  225. * pop3_get_message()
  226. *
  227. * Gets the authentication message from the response buffer.
  228. */
  229. static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out)
  230. {
  231. char *message = data->state.buffer;
  232. size_t len = strlen(message);
  233. if(len > 2) {
  234. /* Find the start of the message */
  235. len -= 2;
  236. for(message += 2; *message == ' ' || *message == '\t'; message++, len--)
  237. ;
  238. /* Find the end of the message */
  239. while(len--)
  240. if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
  241. message[len] != '\t')
  242. break;
  243. /* Terminate the message */
  244. message[++len] = '\0';
  245. Curl_bufref_set(out, message, len, NULL);
  246. }
  247. else
  248. /* junk input => zero length output */
  249. Curl_bufref_set(out, "", 0, NULL);
  250. return CURLE_OK;
  251. }
  252. /***********************************************************************
  253. *
  254. * pop3_state()
  255. *
  256. * This is the ONLY way to change POP3 state!
  257. */
  258. static void pop3_state(struct Curl_easy *data, pop3state newstate)
  259. {
  260. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  261. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  262. /* for debug purposes */
  263. static const char * const names[] = {
  264. "STOP",
  265. "SERVERGREET",
  266. "CAPA",
  267. "STARTTLS",
  268. "UPGRADETLS",
  269. "AUTH",
  270. "APOP",
  271. "USER",
  272. "PASS",
  273. "COMMAND",
  274. "QUIT",
  275. /* LAST */
  276. };
  277. if(pop3c->state != newstate)
  278. infof(data, "POP3 %p state change from %s to %s",
  279. (void *)pop3c, names[pop3c->state], names[newstate]);
  280. #endif
  281. pop3c->state = newstate;
  282. }
  283. /***********************************************************************
  284. *
  285. * pop3_perform_capa()
  286. *
  287. * Sends the CAPA command in order to obtain a list of server side supported
  288. * capabilities.
  289. */
  290. static CURLcode pop3_perform_capa(struct Curl_easy *data,
  291. struct connectdata *conn)
  292. {
  293. CURLcode result = CURLE_OK;
  294. struct pop3_conn *pop3c = &conn->proto.pop3c;
  295. pop3c->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanisms yet */
  296. pop3c->sasl.authused = SASL_AUTH_NONE; /* Clear the auth. mechanism used */
  297. pop3c->tls_supported = FALSE; /* Clear the TLS capability */
  298. /* Send the CAPA command */
  299. result = Curl_pp_sendf(data, &pop3c->pp, "%s", "CAPA");
  300. if(!result)
  301. pop3_state(data, POP3_CAPA);
  302. return result;
  303. }
  304. /***********************************************************************
  305. *
  306. * pop3_perform_starttls()
  307. *
  308. * Sends the STLS command to start the upgrade to TLS.
  309. */
  310. static CURLcode pop3_perform_starttls(struct Curl_easy *data,
  311. struct connectdata *conn)
  312. {
  313. /* Send the STLS command */
  314. CURLcode result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s", "STLS");
  315. if(!result)
  316. pop3_state(data, POP3_STARTTLS);
  317. return result;
  318. }
  319. /***********************************************************************
  320. *
  321. * pop3_perform_upgrade_tls()
  322. *
  323. * Performs the upgrade to TLS.
  324. */
  325. static CURLcode pop3_perform_upgrade_tls(struct Curl_easy *data,
  326. struct connectdata *conn)
  327. {
  328. /* Start the SSL connection */
  329. struct pop3_conn *pop3c = &conn->proto.pop3c;
  330. CURLcode result;
  331. bool ssldone = FALSE;
  332. if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
  333. result = Curl_ssl_cfilter_add(data, conn, FIRSTSOCKET);
  334. if(result)
  335. goto out;
  336. }
  337. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  338. if(!result) {
  339. pop3c->ssldone = ssldone;
  340. if(pop3c->state != POP3_UPGRADETLS)
  341. pop3_state(data, POP3_UPGRADETLS);
  342. if(pop3c->ssldone) {
  343. pop3_to_pop3s(conn);
  344. result = pop3_perform_capa(data, conn);
  345. }
  346. }
  347. out:
  348. return result;
  349. }
  350. /***********************************************************************
  351. *
  352. * pop3_perform_user()
  353. *
  354. * Sends a clear text USER command to authenticate with.
  355. */
  356. static CURLcode pop3_perform_user(struct Curl_easy *data,
  357. struct connectdata *conn)
  358. {
  359. CURLcode result = CURLE_OK;
  360. /* Check we have a username and password to authenticate with and end the
  361. connect phase if we don't */
  362. if(!data->state.aptr.user) {
  363. pop3_state(data, POP3_STOP);
  364. return result;
  365. }
  366. /* Send the USER command */
  367. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "USER %s",
  368. conn->user ? conn->user : "");
  369. if(!result)
  370. pop3_state(data, POP3_USER);
  371. return result;
  372. }
  373. #ifndef CURL_DISABLE_CRYPTO_AUTH
  374. /***********************************************************************
  375. *
  376. * pop3_perform_apop()
  377. *
  378. * Sends an APOP command to authenticate with.
  379. */
  380. static CURLcode pop3_perform_apop(struct Curl_easy *data,
  381. struct connectdata *conn)
  382. {
  383. CURLcode result = CURLE_OK;
  384. struct pop3_conn *pop3c = &conn->proto.pop3c;
  385. size_t i;
  386. struct MD5_context *ctxt;
  387. unsigned char digest[MD5_DIGEST_LEN];
  388. char secret[2 * MD5_DIGEST_LEN + 1];
  389. /* Check we have a username and password to authenticate with and end the
  390. connect phase if we don't */
  391. if(!data->state.aptr.user) {
  392. pop3_state(data, POP3_STOP);
  393. return result;
  394. }
  395. /* Create the digest */
  396. ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
  397. if(!ctxt)
  398. return CURLE_OUT_OF_MEMORY;
  399. Curl_MD5_update(ctxt, (const unsigned char *) pop3c->apoptimestamp,
  400. curlx_uztoui(strlen(pop3c->apoptimestamp)));
  401. Curl_MD5_update(ctxt, (const unsigned char *) conn->passwd,
  402. curlx_uztoui(strlen(conn->passwd)));
  403. /* Finalise the digest */
  404. Curl_MD5_final(ctxt, digest);
  405. /* Convert the calculated 16 octet digest into a 32 byte hex string */
  406. for(i = 0; i < MD5_DIGEST_LEN; i++)
  407. msnprintf(&secret[2 * i], 3, "%02x", digest[i]);
  408. result = Curl_pp_sendf(data, &pop3c->pp, "APOP %s %s", conn->user, secret);
  409. if(!result)
  410. pop3_state(data, POP3_APOP);
  411. return result;
  412. }
  413. #endif
  414. /***********************************************************************
  415. *
  416. * pop3_perform_auth()
  417. *
  418. * Sends an AUTH command allowing the client to login with the given SASL
  419. * authentication mechanism.
  420. */
  421. static CURLcode pop3_perform_auth(struct Curl_easy *data,
  422. const char *mech,
  423. const struct bufref *initresp)
  424. {
  425. CURLcode result = CURLE_OK;
  426. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  427. const char *ir = (const char *) Curl_bufref_ptr(initresp);
  428. if(ir) { /* AUTH <mech> ...<crlf> */
  429. /* Send the AUTH command with the initial response */
  430. result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s %s", mech, ir);
  431. }
  432. else {
  433. /* Send the AUTH command */
  434. result = Curl_pp_sendf(data, &pop3c->pp, "AUTH %s", mech);
  435. }
  436. return result;
  437. }
  438. /***********************************************************************
  439. *
  440. * pop3_continue_auth()
  441. *
  442. * Sends SASL continuation data.
  443. */
  444. static CURLcode pop3_continue_auth(struct Curl_easy *data,
  445. const char *mech,
  446. const struct bufref *resp)
  447. {
  448. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  449. (void)mech;
  450. return Curl_pp_sendf(data, &pop3c->pp,
  451. "%s", (const char *) Curl_bufref_ptr(resp));
  452. }
  453. /***********************************************************************
  454. *
  455. * pop3_cancel_auth()
  456. *
  457. * Sends SASL cancellation.
  458. */
  459. static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech)
  460. {
  461. struct pop3_conn *pop3c = &data->conn->proto.pop3c;
  462. (void)mech;
  463. return Curl_pp_sendf(data, &pop3c->pp, "*");
  464. }
  465. /***********************************************************************
  466. *
  467. * pop3_perform_authentication()
  468. *
  469. * Initiates the authentication sequence, with the appropriate SASL
  470. * authentication mechanism, falling back to APOP and clear text should a
  471. * common mechanism not be available between the client and server.
  472. */
  473. static CURLcode pop3_perform_authentication(struct Curl_easy *data,
  474. struct connectdata *conn)
  475. {
  476. CURLcode result = CURLE_OK;
  477. struct pop3_conn *pop3c = &conn->proto.pop3c;
  478. saslprogress progress = SASL_IDLE;
  479. /* Check we have enough data to authenticate with and end the
  480. connect phase if we don't */
  481. if(!Curl_sasl_can_authenticate(&pop3c->sasl, data)) {
  482. pop3_state(data, POP3_STOP);
  483. return result;
  484. }
  485. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_SASL) {
  486. /* Calculate the SASL login details */
  487. result = Curl_sasl_start(&pop3c->sasl, data, FALSE, &progress);
  488. if(!result)
  489. if(progress == SASL_INPROGRESS)
  490. pop3_state(data, POP3_AUTH);
  491. }
  492. if(!result && progress == SASL_IDLE) {
  493. #ifndef CURL_DISABLE_CRYPTO_AUTH
  494. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
  495. /* Perform APOP authentication */
  496. result = pop3_perform_apop(data, conn);
  497. else
  498. #endif
  499. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
  500. /* Perform clear text authentication */
  501. result = pop3_perform_user(data, conn);
  502. else {
  503. /* Other mechanisms not supported */
  504. infof(data, "No known authentication mechanisms supported");
  505. result = CURLE_LOGIN_DENIED;
  506. }
  507. }
  508. return result;
  509. }
  510. /***********************************************************************
  511. *
  512. * pop3_perform_command()
  513. *
  514. * Sends a POP3 based command.
  515. */
  516. static CURLcode pop3_perform_command(struct Curl_easy *data)
  517. {
  518. CURLcode result = CURLE_OK;
  519. struct connectdata *conn = data->conn;
  520. struct POP3 *pop3 = data->req.p.pop3;
  521. const char *command = NULL;
  522. /* Calculate the default command */
  523. if(pop3->id[0] == '\0' || data->set.list_only) {
  524. command = "LIST";
  525. if(pop3->id[0] != '\0')
  526. /* Message specific LIST so skip the BODY transfer */
  527. pop3->transfer = PPTRANSFER_INFO;
  528. }
  529. else
  530. command = "RETR";
  531. /* Send the command */
  532. if(pop3->id[0] != '\0')
  533. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s %s",
  534. (pop3->custom && pop3->custom[0] != '\0' ?
  535. pop3->custom : command), pop3->id);
  536. else
  537. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s",
  538. (pop3->custom && pop3->custom[0] != '\0' ?
  539. pop3->custom : command));
  540. if(!result)
  541. pop3_state(data, POP3_COMMAND);
  542. return result;
  543. }
  544. /***********************************************************************
  545. *
  546. * pop3_perform_quit()
  547. *
  548. * Performs the quit action prior to sclose() be called.
  549. */
  550. static CURLcode pop3_perform_quit(struct Curl_easy *data,
  551. struct connectdata *conn)
  552. {
  553. /* Send the QUIT command */
  554. CURLcode result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "%s", "QUIT");
  555. if(!result)
  556. pop3_state(data, POP3_QUIT);
  557. return result;
  558. }
  559. /* For the initial server greeting */
  560. static CURLcode pop3_state_servergreet_resp(struct Curl_easy *data,
  561. int pop3code,
  562. pop3state instate)
  563. {
  564. CURLcode result = CURLE_OK;
  565. struct connectdata *conn = data->conn;
  566. struct pop3_conn *pop3c = &conn->proto.pop3c;
  567. const char *line = data->state.buffer;
  568. size_t len = strlen(line);
  569. (void)instate; /* no use for this yet */
  570. if(pop3code != '+') {
  571. failf(data, "Got unexpected pop3-server response");
  572. result = CURLE_WEIRD_SERVER_REPLY;
  573. }
  574. else {
  575. /* Does the server support APOP authentication? */
  576. if(len >= 4 && line[len - 2] == '>') {
  577. /* Look for the APOP timestamp */
  578. size_t i;
  579. for(i = 3; i < len - 2; ++i) {
  580. if(line[i] == '<') {
  581. /* Calculate the length of the timestamp */
  582. size_t timestamplen = len - 1 - i;
  583. char *at;
  584. if(!timestamplen)
  585. break;
  586. /* Allocate some memory for the timestamp */
  587. pop3c->apoptimestamp = (char *)calloc(1, timestamplen + 1);
  588. if(!pop3c->apoptimestamp)
  589. break;
  590. /* Copy the timestamp */
  591. memcpy(pop3c->apoptimestamp, line + i, timestamplen);
  592. pop3c->apoptimestamp[timestamplen] = '\0';
  593. /* If the timestamp does not contain '@' it is not (as required by
  594. RFC-1939) conformant to the RFC-822 message id syntax, and we
  595. therefore do not use APOP authentication. */
  596. at = strchr(pop3c->apoptimestamp, '@');
  597. if(!at)
  598. Curl_safefree(pop3c->apoptimestamp);
  599. else
  600. /* Store the APOP capability */
  601. pop3c->authtypes |= POP3_TYPE_APOP;
  602. break;
  603. }
  604. }
  605. }
  606. result = pop3_perform_capa(data, conn);
  607. }
  608. return result;
  609. }
  610. /* For CAPA responses */
  611. static CURLcode pop3_state_capa_resp(struct Curl_easy *data, int pop3code,
  612. pop3state instate)
  613. {
  614. CURLcode result = CURLE_OK;
  615. struct connectdata *conn = data->conn;
  616. struct pop3_conn *pop3c = &conn->proto.pop3c;
  617. const char *line = data->state.buffer;
  618. size_t len = strlen(line);
  619. (void)instate; /* no use for this yet */
  620. /* Do we have a untagged continuation response? */
  621. if(pop3code == '*') {
  622. /* Does the server support the STLS capability? */
  623. if(len >= 4 && !memcmp(line, "STLS", 4))
  624. pop3c->tls_supported = TRUE;
  625. /* Does the server support clear text authentication? */
  626. else if(len >= 4 && !memcmp(line, "USER", 4))
  627. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  628. /* Does the server support SASL based authentication? */
  629. else if(len >= 5 && !memcmp(line, "SASL ", 5)) {
  630. pop3c->authtypes |= POP3_TYPE_SASL;
  631. /* Advance past the SASL keyword */
  632. line += 5;
  633. len -= 5;
  634. /* Loop through the data line */
  635. for(;;) {
  636. size_t llen;
  637. size_t wordlen;
  638. unsigned short mechbit;
  639. while(len &&
  640. (*line == ' ' || *line == '\t' ||
  641. *line == '\r' || *line == '\n')) {
  642. line++;
  643. len--;
  644. }
  645. if(!len)
  646. break;
  647. /* Extract the word */
  648. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  649. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  650. line[wordlen] != '\n';)
  651. wordlen++;
  652. /* Test the word for a matching authentication mechanism */
  653. mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
  654. if(mechbit && llen == wordlen)
  655. pop3c->sasl.authmechs |= mechbit;
  656. line += wordlen;
  657. len -= wordlen;
  658. }
  659. }
  660. }
  661. else {
  662. /* Clear text is supported when CAPA isn't recognised */
  663. if(pop3code != '+')
  664. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  665. if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET))
  666. result = pop3_perform_authentication(data, conn);
  667. else if(pop3code == '+' && pop3c->tls_supported)
  668. /* Switch to TLS connection now */
  669. result = pop3_perform_starttls(data, conn);
  670. else if(data->set.use_ssl <= CURLUSESSL_TRY)
  671. /* Fallback and carry on with authentication */
  672. result = pop3_perform_authentication(data, conn);
  673. else {
  674. failf(data, "STLS not supported.");
  675. result = CURLE_USE_SSL_FAILED;
  676. }
  677. }
  678. return result;
  679. }
  680. /* For STARTTLS responses */
  681. static CURLcode pop3_state_starttls_resp(struct Curl_easy *data,
  682. struct connectdata *conn,
  683. int pop3code,
  684. pop3state instate)
  685. {
  686. CURLcode result = CURLE_OK;
  687. (void)instate; /* no use for this yet */
  688. /* Pipelining in response is forbidden. */
  689. if(data->conn->proto.pop3c.pp.cache_size)
  690. return CURLE_WEIRD_SERVER_REPLY;
  691. if(pop3code != '+') {
  692. if(data->set.use_ssl != CURLUSESSL_TRY) {
  693. failf(data, "STARTTLS denied");
  694. result = CURLE_USE_SSL_FAILED;
  695. }
  696. else
  697. result = pop3_perform_authentication(data, conn);
  698. }
  699. else
  700. result = pop3_perform_upgrade_tls(data, conn);
  701. return result;
  702. }
  703. /* For SASL authentication responses */
  704. static CURLcode pop3_state_auth_resp(struct Curl_easy *data,
  705. int pop3code,
  706. pop3state instate)
  707. {
  708. CURLcode result = CURLE_OK;
  709. struct connectdata *conn = data->conn;
  710. struct pop3_conn *pop3c = &conn->proto.pop3c;
  711. saslprogress progress;
  712. (void)instate; /* no use for this yet */
  713. result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress);
  714. if(!result)
  715. switch(progress) {
  716. case SASL_DONE:
  717. pop3_state(data, POP3_STOP); /* Authenticated */
  718. break;
  719. case SASL_IDLE: /* No mechanism left after cancellation */
  720. #ifndef CURL_DISABLE_CRYPTO_AUTH
  721. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
  722. /* Perform APOP authentication */
  723. result = pop3_perform_apop(data, conn);
  724. else
  725. #endif
  726. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
  727. /* Perform clear text authentication */
  728. result = pop3_perform_user(data, conn);
  729. else {
  730. failf(data, "Authentication cancelled");
  731. result = CURLE_LOGIN_DENIED;
  732. }
  733. break;
  734. default:
  735. break;
  736. }
  737. return result;
  738. }
  739. #ifndef CURL_DISABLE_CRYPTO_AUTH
  740. /* For APOP responses */
  741. static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code,
  742. pop3state instate)
  743. {
  744. CURLcode result = CURLE_OK;
  745. (void)instate; /* no use for this yet */
  746. if(pop3code != '+') {
  747. failf(data, "Authentication failed: %d", pop3code);
  748. result = CURLE_LOGIN_DENIED;
  749. }
  750. else
  751. /* End of connect phase */
  752. pop3_state(data, POP3_STOP);
  753. return result;
  754. }
  755. #endif
  756. /* For USER responses */
  757. static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code,
  758. pop3state instate)
  759. {
  760. CURLcode result = CURLE_OK;
  761. struct connectdata *conn = data->conn;
  762. (void)instate; /* no use for this yet */
  763. if(pop3code != '+') {
  764. failf(data, "Access denied. %c", pop3code);
  765. result = CURLE_LOGIN_DENIED;
  766. }
  767. else
  768. /* Send the PASS command */
  769. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "PASS %s",
  770. conn->passwd ? conn->passwd : "");
  771. if(!result)
  772. pop3_state(data, POP3_PASS);
  773. return result;
  774. }
  775. /* For PASS responses */
  776. static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code,
  777. pop3state instate)
  778. {
  779. CURLcode result = CURLE_OK;
  780. (void)instate; /* no use for this yet */
  781. if(pop3code != '+') {
  782. failf(data, "Access denied. %c", pop3code);
  783. result = CURLE_LOGIN_DENIED;
  784. }
  785. else
  786. /* End of connect phase */
  787. pop3_state(data, POP3_STOP);
  788. return result;
  789. }
  790. /* For command responses */
  791. static CURLcode pop3_state_command_resp(struct Curl_easy *data,
  792. int pop3code,
  793. pop3state instate)
  794. {
  795. CURLcode result = CURLE_OK;
  796. struct connectdata *conn = data->conn;
  797. struct POP3 *pop3 = data->req.p.pop3;
  798. struct pop3_conn *pop3c = &conn->proto.pop3c;
  799. struct pingpong *pp = &pop3c->pp;
  800. (void)instate; /* no use for this yet */
  801. if(pop3code != '+') {
  802. pop3_state(data, POP3_STOP);
  803. return CURLE_WEIRD_SERVER_REPLY;
  804. }
  805. /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
  806. EOB string so count this is two matching bytes. This is necessary to make
  807. the code detect the EOB if the only data than comes now is %2e CR LF like
  808. when there is no body to return. */
  809. pop3c->eob = 2;
  810. /* But since this initial CR LF pair is not part of the actual body, we set
  811. the strip counter here so that these bytes won't be delivered. */
  812. pop3c->strip = 2;
  813. if(pop3->transfer == PPTRANSFER_BODY) {
  814. /* POP3 download */
  815. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  816. if(pp->cache) {
  817. /* The header "cache" contains a bunch of data that is actually body
  818. content so send it as such. Note that there may even be additional
  819. "headers" after the body */
  820. if(!data->req.no_body) {
  821. result = Curl_pop3_write(data, pp->cache, pp->cache_size);
  822. if(result)
  823. return result;
  824. }
  825. /* Free the cache */
  826. Curl_safefree(pp->cache);
  827. /* Reset the cache size */
  828. pp->cache_size = 0;
  829. }
  830. }
  831. /* End of DO phase */
  832. pop3_state(data, POP3_STOP);
  833. return result;
  834. }
  835. static CURLcode pop3_statemachine(struct Curl_easy *data,
  836. struct connectdata *conn)
  837. {
  838. CURLcode result = CURLE_OK;
  839. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  840. int pop3code;
  841. struct pop3_conn *pop3c = &conn->proto.pop3c;
  842. struct pingpong *pp = &pop3c->pp;
  843. size_t nread = 0;
  844. (void)data;
  845. /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
  846. if(pop3c->state == POP3_UPGRADETLS)
  847. return pop3_perform_upgrade_tls(data, conn);
  848. /* Flush any data that needs to be sent */
  849. if(pp->sendleft)
  850. return Curl_pp_flushsend(data, pp);
  851. do {
  852. /* Read the response from the server */
  853. result = Curl_pp_readresp(data, sock, pp, &pop3code, &nread);
  854. if(result)
  855. return result;
  856. if(!pop3code)
  857. break;
  858. /* We have now received a full POP3 server response */
  859. switch(pop3c->state) {
  860. case POP3_SERVERGREET:
  861. result = pop3_state_servergreet_resp(data, pop3code, pop3c->state);
  862. break;
  863. case POP3_CAPA:
  864. result = pop3_state_capa_resp(data, pop3code, pop3c->state);
  865. break;
  866. case POP3_STARTTLS:
  867. result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state);
  868. break;
  869. case POP3_AUTH:
  870. result = pop3_state_auth_resp(data, pop3code, pop3c->state);
  871. break;
  872. #ifndef CURL_DISABLE_CRYPTO_AUTH
  873. case POP3_APOP:
  874. result = pop3_state_apop_resp(data, pop3code, pop3c->state);
  875. break;
  876. #endif
  877. case POP3_USER:
  878. result = pop3_state_user_resp(data, pop3code, pop3c->state);
  879. break;
  880. case POP3_PASS:
  881. result = pop3_state_pass_resp(data, pop3code, pop3c->state);
  882. break;
  883. case POP3_COMMAND:
  884. result = pop3_state_command_resp(data, pop3code, pop3c->state);
  885. break;
  886. case POP3_QUIT:
  887. pop3_state(data, POP3_STOP);
  888. break;
  889. default:
  890. /* internal error */
  891. pop3_state(data, POP3_STOP);
  892. break;
  893. }
  894. } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
  895. return result;
  896. }
  897. /* Called repeatedly until done from multi.c */
  898. static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done)
  899. {
  900. CURLcode result = CURLE_OK;
  901. struct connectdata *conn = data->conn;
  902. struct pop3_conn *pop3c = &conn->proto.pop3c;
  903. if((conn->handler->flags & PROTOPT_SSL) && !pop3c->ssldone) {
  904. bool ssldone = FALSE;
  905. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  906. pop3c->ssldone = ssldone;
  907. if(result || !pop3c->ssldone)
  908. return result;
  909. }
  910. result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE);
  911. *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
  912. return result;
  913. }
  914. static CURLcode pop3_block_statemach(struct Curl_easy *data,
  915. struct connectdata *conn,
  916. bool disconnecting)
  917. {
  918. CURLcode result = CURLE_OK;
  919. struct pop3_conn *pop3c = &conn->proto.pop3c;
  920. while(pop3c->state != POP3_STOP && !result)
  921. result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting);
  922. return result;
  923. }
  924. /* Allocate and initialize the POP3 struct for the current Curl_easy if
  925. required */
  926. static CURLcode pop3_init(struct Curl_easy *data)
  927. {
  928. CURLcode result = CURLE_OK;
  929. struct POP3 *pop3;
  930. pop3 = data->req.p.pop3 = calloc(sizeof(struct POP3), 1);
  931. if(!pop3)
  932. result = CURLE_OUT_OF_MEMORY;
  933. return result;
  934. }
  935. /* For the POP3 "protocol connect" and "doing" phases only */
  936. static int pop3_getsock(struct Curl_easy *data,
  937. struct connectdata *conn, curl_socket_t *socks)
  938. {
  939. return Curl_pp_getsock(data, &conn->proto.pop3c.pp, socks);
  940. }
  941. /***********************************************************************
  942. *
  943. * pop3_connect()
  944. *
  945. * This function should do everything that is to be considered a part of the
  946. * connection phase.
  947. *
  948. * The variable 'done' points to will be TRUE if the protocol-layer connect
  949. * phase is done when this function returns, or FALSE if not.
  950. */
  951. static CURLcode pop3_connect(struct Curl_easy *data, bool *done)
  952. {
  953. CURLcode result = CURLE_OK;
  954. struct connectdata *conn = data->conn;
  955. struct pop3_conn *pop3c = &conn->proto.pop3c;
  956. struct pingpong *pp = &pop3c->pp;
  957. *done = FALSE; /* default to not done yet */
  958. /* We always support persistent connections in POP3 */
  959. connkeep(conn, "POP3 default");
  960. PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp);
  961. /* Set the default preferred authentication type and mechanism */
  962. pop3c->preftype = POP3_TYPE_ANY;
  963. Curl_sasl_init(&pop3c->sasl, data, &saslpop3);
  964. /* Initialise the pingpong layer */
  965. Curl_pp_setup(pp);
  966. Curl_pp_init(data, 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, 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 */