pop3.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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_UTSNAME_H
  48. #include <sys/utsname.h>
  49. #endif
  50. #ifdef HAVE_NETDB_H
  51. #include <netdb.h>
  52. #endif
  53. #ifdef __VMS
  54. #include <in.h>
  55. #include <inet.h>
  56. #endif
  57. #include <curl/curl.h>
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "hostip.h"
  61. #include "progress.h"
  62. #include "transfer.h"
  63. #include "escape.h"
  64. #include "http.h" /* for HTTP proxy tunnel stuff */
  65. #include "socks.h"
  66. #include "pop3.h"
  67. #include "strtoofft.h"
  68. #include "strcase.h"
  69. #include "vtls/vtls.h"
  70. #include "cfilters.h"
  71. #include "connect.h"
  72. #include "select.h"
  73. #include "multiif.h"
  74. #include "url.h"
  75. #include "bufref.h"
  76. #include "curl_sasl.h"
  77. #include "curl_md5.h"
  78. #include "warnless.h"
  79. /* The last 3 #include files should be in this order */
  80. #include "curl_printf.h"
  81. #include "curl_memory.h"
  82. #include "memdebug.h"
  83. /* Local API functions */
  84. static CURLcode pop3_regular_transfer(struct Curl_easy *data, bool *done);
  85. static CURLcode pop3_do(struct Curl_easy *data, bool *done);
  86. static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
  87. bool premature);
  88. static CURLcode pop3_connect(struct Curl_easy *data, bool *done);
  89. static CURLcode pop3_disconnect(struct Curl_easy *data,
  90. struct connectdata *conn, bool dead);
  91. static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done);
  92. static int pop3_getsock(struct Curl_easy *data,
  93. struct connectdata *conn, curl_socket_t *socks);
  94. static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done);
  95. static CURLcode pop3_setup_connection(struct Curl_easy *data,
  96. struct connectdata *conn);
  97. static CURLcode pop3_parse_url_options(struct connectdata *conn);
  98. static CURLcode pop3_parse_url_path(struct Curl_easy *data);
  99. static CURLcode pop3_parse_custom_request(struct Curl_easy *data);
  100. static CURLcode pop3_perform_auth(struct Curl_easy *data, const char *mech,
  101. const struct bufref *initresp);
  102. static CURLcode pop3_continue_auth(struct Curl_easy *data, const char *mech,
  103. const struct bufref *resp);
  104. static CURLcode pop3_cancel_auth(struct Curl_easy *data, const char *mech);
  105. static CURLcode pop3_get_message(struct Curl_easy *data, struct bufref *out);
  106. /*
  107. * POP3 protocol handler.
  108. */
  109. const struct Curl_handler Curl_handler_pop3 = {
  110. "POP3", /* scheme */
  111. pop3_setup_connection, /* setup_connection */
  112. pop3_do, /* do_it */
  113. pop3_done, /* done */
  114. ZERO_NULL, /* do_more */
  115. pop3_connect, /* connect_it */
  116. pop3_multi_statemach, /* connecting */
  117. pop3_doing, /* doing */
  118. pop3_getsock, /* proto_getsock */
  119. pop3_getsock, /* doing_getsock */
  120. ZERO_NULL, /* domore_getsock */
  121. ZERO_NULL, /* perform_getsock */
  122. pop3_disconnect, /* disconnect */
  123. ZERO_NULL, /* readwrite */
  124. ZERO_NULL, /* connection_check */
  125. ZERO_NULL, /* attach connection */
  126. PORT_POP3, /* defport */
  127. CURLPROTO_POP3, /* protocol */
  128. CURLPROTO_POP3, /* family */
  129. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */
  130. PROTOPT_URLOPTIONS
  131. };
  132. #ifdef USE_SSL
  133. /*
  134. * POP3S protocol handler.
  135. */
  136. const struct Curl_handler Curl_handler_pop3s = {
  137. "POP3S", /* scheme */
  138. pop3_setup_connection, /* setup_connection */
  139. pop3_do, /* do_it */
  140. pop3_done, /* done */
  141. ZERO_NULL, /* do_more */
  142. pop3_connect, /* connect_it */
  143. pop3_multi_statemach, /* connecting */
  144. pop3_doing, /* doing */
  145. pop3_getsock, /* proto_getsock */
  146. pop3_getsock, /* doing_getsock */
  147. ZERO_NULL, /* domore_getsock */
  148. ZERO_NULL, /* perform_getsock */
  149. pop3_disconnect, /* disconnect */
  150. ZERO_NULL, /* readwrite */
  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 = data->state.buffer;
  235. size_t len = strlen(message);
  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. * state()
  258. *
  259. * This is the ONLY way to change POP3 state!
  260. */
  261. static void 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. 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. 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. 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. 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. state(data, POP3_USER);
  374. return result;
  375. }
  376. #ifndef CURL_DISABLE_CRYPTO_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. 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. 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. 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. state(data, POP3_AUTH);
  494. }
  495. if(!result && progress == SASL_IDLE) {
  496. #ifndef CURL_DISABLE_CRYPTO_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. 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. 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 = data->state.buffer;
  571. size_t len = strlen(line);
  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 {
  578. /* Does the server support APOP authentication? */
  579. if(len >= 4 && line[len - 2] == '>') {
  580. /* Look for the APOP timestamp */
  581. size_t i;
  582. for(i = 3; i < len - 2; ++i) {
  583. if(line[i] == '<') {
  584. /* Calculate the length of the timestamp */
  585. size_t timestamplen = len - 1 - i;
  586. char *at;
  587. if(!timestamplen)
  588. break;
  589. /* Allocate some memory for the timestamp */
  590. pop3c->apoptimestamp = (char *)calloc(1, timestamplen + 1);
  591. if(!pop3c->apoptimestamp)
  592. break;
  593. /* Copy the timestamp */
  594. memcpy(pop3c->apoptimestamp, line + i, timestamplen);
  595. pop3c->apoptimestamp[timestamplen] = '\0';
  596. /* If the timestamp does not contain '@' it is not (as required by
  597. RFC-1939) conformant to the RFC-822 message id syntax, and we
  598. therefore do not use APOP authentication. */
  599. at = strchr(pop3c->apoptimestamp, '@');
  600. if(!at)
  601. Curl_safefree(pop3c->apoptimestamp);
  602. else
  603. /* Store the APOP capability */
  604. pop3c->authtypes |= POP3_TYPE_APOP;
  605. break;
  606. }
  607. }
  608. }
  609. result = pop3_perform_capa(data, conn);
  610. }
  611. return result;
  612. }
  613. /* For CAPA responses */
  614. static CURLcode pop3_state_capa_resp(struct Curl_easy *data, int pop3code,
  615. pop3state instate)
  616. {
  617. CURLcode result = CURLE_OK;
  618. struct connectdata *conn = data->conn;
  619. struct pop3_conn *pop3c = &conn->proto.pop3c;
  620. const char *line = data->state.buffer;
  621. size_t len = strlen(line);
  622. (void)instate; /* no use for this yet */
  623. /* Do we have a untagged continuation response? */
  624. if(pop3code == '*') {
  625. /* Does the server support the STLS capability? */
  626. if(len >= 4 && !memcmp(line, "STLS", 4))
  627. pop3c->tls_supported = TRUE;
  628. /* Does the server support clear text authentication? */
  629. else if(len >= 4 && !memcmp(line, "USER", 4))
  630. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  631. /* Does the server support SASL based authentication? */
  632. else if(len >= 5 && !memcmp(line, "SASL ", 5)) {
  633. pop3c->authtypes |= POP3_TYPE_SASL;
  634. /* Advance past the SASL keyword */
  635. line += 5;
  636. len -= 5;
  637. /* Loop through the data line */
  638. for(;;) {
  639. size_t llen;
  640. size_t wordlen;
  641. unsigned short mechbit;
  642. while(len &&
  643. (*line == ' ' || *line == '\t' ||
  644. *line == '\r' || *line == '\n')) {
  645. line++;
  646. len--;
  647. }
  648. if(!len)
  649. break;
  650. /* Extract the word */
  651. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  652. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  653. line[wordlen] != '\n';)
  654. wordlen++;
  655. /* Test the word for a matching authentication mechanism */
  656. mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
  657. if(mechbit && llen == wordlen)
  658. pop3c->sasl.authmechs |= mechbit;
  659. line += wordlen;
  660. len -= wordlen;
  661. }
  662. }
  663. }
  664. else {
  665. /* Clear text is supported when CAPA isn't recognised */
  666. if(pop3code != '+')
  667. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  668. if(!data->set.use_ssl || Curl_conn_is_ssl(conn, FIRSTSOCKET))
  669. result = pop3_perform_authentication(data, conn);
  670. else if(pop3code == '+' && pop3c->tls_supported)
  671. /* Switch to TLS connection now */
  672. result = pop3_perform_starttls(data, conn);
  673. else if(data->set.use_ssl <= CURLUSESSL_TRY)
  674. /* Fallback and carry on with authentication */
  675. result = pop3_perform_authentication(data, conn);
  676. else {
  677. failf(data, "STLS not supported.");
  678. result = CURLE_USE_SSL_FAILED;
  679. }
  680. }
  681. return result;
  682. }
  683. /* For STARTTLS responses */
  684. static CURLcode pop3_state_starttls_resp(struct Curl_easy *data,
  685. struct connectdata *conn,
  686. int pop3code,
  687. pop3state instate)
  688. {
  689. CURLcode result = CURLE_OK;
  690. (void)instate; /* no use for this yet */
  691. /* Pipelining in response is forbidden. */
  692. if(data->conn->proto.pop3c.pp.cache_size)
  693. return CURLE_WEIRD_SERVER_REPLY;
  694. if(pop3code != '+') {
  695. if(data->set.use_ssl != CURLUSESSL_TRY) {
  696. failf(data, "STARTTLS denied");
  697. result = CURLE_USE_SSL_FAILED;
  698. }
  699. else
  700. result = pop3_perform_authentication(data, conn);
  701. }
  702. else
  703. result = pop3_perform_upgrade_tls(data, conn);
  704. return result;
  705. }
  706. /* For SASL authentication responses */
  707. static CURLcode pop3_state_auth_resp(struct Curl_easy *data,
  708. int pop3code,
  709. pop3state instate)
  710. {
  711. CURLcode result = CURLE_OK;
  712. struct connectdata *conn = data->conn;
  713. struct pop3_conn *pop3c = &conn->proto.pop3c;
  714. saslprogress progress;
  715. (void)instate; /* no use for this yet */
  716. result = Curl_sasl_continue(&pop3c->sasl, data, pop3code, &progress);
  717. if(!result)
  718. switch(progress) {
  719. case SASL_DONE:
  720. state(data, POP3_STOP); /* Authenticated */
  721. break;
  722. case SASL_IDLE: /* No mechanism left after cancellation */
  723. #ifndef CURL_DISABLE_CRYPTO_AUTH
  724. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_APOP)
  725. /* Perform APOP authentication */
  726. result = pop3_perform_apop(data, conn);
  727. else
  728. #endif
  729. if(pop3c->authtypes & pop3c->preftype & POP3_TYPE_CLEARTEXT)
  730. /* Perform clear text authentication */
  731. result = pop3_perform_user(data, conn);
  732. else {
  733. failf(data, "Authentication cancelled");
  734. result = CURLE_LOGIN_DENIED;
  735. }
  736. break;
  737. default:
  738. break;
  739. }
  740. return result;
  741. }
  742. #ifndef CURL_DISABLE_CRYPTO_AUTH
  743. /* For APOP responses */
  744. static CURLcode pop3_state_apop_resp(struct Curl_easy *data, int pop3code,
  745. pop3state instate)
  746. {
  747. CURLcode result = CURLE_OK;
  748. (void)instate; /* no use for this yet */
  749. if(pop3code != '+') {
  750. failf(data, "Authentication failed: %d", pop3code);
  751. result = CURLE_LOGIN_DENIED;
  752. }
  753. else
  754. /* End of connect phase */
  755. state(data, POP3_STOP);
  756. return result;
  757. }
  758. #endif
  759. /* For USER responses */
  760. static CURLcode pop3_state_user_resp(struct Curl_easy *data, int pop3code,
  761. pop3state instate)
  762. {
  763. CURLcode result = CURLE_OK;
  764. struct connectdata *conn = data->conn;
  765. (void)instate; /* no use for this yet */
  766. if(pop3code != '+') {
  767. failf(data, "Access denied. %c", pop3code);
  768. result = CURLE_LOGIN_DENIED;
  769. }
  770. else
  771. /* Send the PASS command */
  772. result = Curl_pp_sendf(data, &conn->proto.pop3c.pp, "PASS %s",
  773. conn->passwd ? conn->passwd : "");
  774. if(!result)
  775. state(data, POP3_PASS);
  776. return result;
  777. }
  778. /* For PASS responses */
  779. static CURLcode pop3_state_pass_resp(struct Curl_easy *data, int pop3code,
  780. pop3state instate)
  781. {
  782. CURLcode result = CURLE_OK;
  783. (void)instate; /* no use for this yet */
  784. if(pop3code != '+') {
  785. failf(data, "Access denied. %c", pop3code);
  786. result = CURLE_LOGIN_DENIED;
  787. }
  788. else
  789. /* End of connect phase */
  790. state(data, POP3_STOP);
  791. return result;
  792. }
  793. /* For command responses */
  794. static CURLcode pop3_state_command_resp(struct Curl_easy *data,
  795. int pop3code,
  796. pop3state instate)
  797. {
  798. CURLcode result = CURLE_OK;
  799. struct connectdata *conn = data->conn;
  800. struct POP3 *pop3 = data->req.p.pop3;
  801. struct pop3_conn *pop3c = &conn->proto.pop3c;
  802. struct pingpong *pp = &pop3c->pp;
  803. (void)instate; /* no use for this yet */
  804. if(pop3code != '+') {
  805. state(data, POP3_STOP);
  806. return CURLE_WEIRD_SERVER_REPLY;
  807. }
  808. /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
  809. EOB string so count this is two matching bytes. This is necessary to make
  810. the code detect the EOB if the only data than comes now is %2e CR LF like
  811. when there is no body to return. */
  812. pop3c->eob = 2;
  813. /* But since this initial CR LF pair is not part of the actual body, we set
  814. the strip counter here so that these bytes won't be delivered. */
  815. pop3c->strip = 2;
  816. if(pop3->transfer == PPTRANSFER_BODY) {
  817. /* POP3 download */
  818. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  819. if(pp->cache) {
  820. /* The header "cache" contains a bunch of data that is actually body
  821. content so send it as such. Note that there may even be additional
  822. "headers" after the body */
  823. if(!data->req.no_body) {
  824. result = Curl_pop3_write(data, pp->cache, pp->cache_size);
  825. if(result)
  826. return result;
  827. }
  828. /* Free the cache */
  829. Curl_safefree(pp->cache);
  830. /* Reset the cache size */
  831. pp->cache_size = 0;
  832. }
  833. }
  834. /* End of DO phase */
  835. state(data, POP3_STOP);
  836. return result;
  837. }
  838. static CURLcode pop3_statemachine(struct Curl_easy *data,
  839. struct connectdata *conn)
  840. {
  841. CURLcode result = CURLE_OK;
  842. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  843. int pop3code;
  844. struct pop3_conn *pop3c = &conn->proto.pop3c;
  845. struct pingpong *pp = &pop3c->pp;
  846. size_t nread = 0;
  847. (void)data;
  848. /* Busy upgrading the connection; right now all I/O is SSL/TLS, not POP3 */
  849. if(pop3c->state == POP3_UPGRADETLS)
  850. return pop3_perform_upgrade_tls(data, conn);
  851. /* Flush any data that needs to be sent */
  852. if(pp->sendleft)
  853. return Curl_pp_flushsend(data, pp);
  854. do {
  855. /* Read the response from the server */
  856. result = Curl_pp_readresp(data, sock, pp, &pop3code, &nread);
  857. if(result)
  858. return result;
  859. if(!pop3code)
  860. break;
  861. /* We have now received a full POP3 server response */
  862. switch(pop3c->state) {
  863. case POP3_SERVERGREET:
  864. result = pop3_state_servergreet_resp(data, pop3code, pop3c->state);
  865. break;
  866. case POP3_CAPA:
  867. result = pop3_state_capa_resp(data, pop3code, pop3c->state);
  868. break;
  869. case POP3_STARTTLS:
  870. result = pop3_state_starttls_resp(data, conn, pop3code, pop3c->state);
  871. break;
  872. case POP3_AUTH:
  873. result = pop3_state_auth_resp(data, pop3code, pop3c->state);
  874. break;
  875. #ifndef CURL_DISABLE_CRYPTO_AUTH
  876. case POP3_APOP:
  877. result = pop3_state_apop_resp(data, pop3code, pop3c->state);
  878. break;
  879. #endif
  880. case POP3_USER:
  881. result = pop3_state_user_resp(data, pop3code, pop3c->state);
  882. break;
  883. case POP3_PASS:
  884. result = pop3_state_pass_resp(data, pop3code, pop3c->state);
  885. break;
  886. case POP3_COMMAND:
  887. result = pop3_state_command_resp(data, pop3code, pop3c->state);
  888. break;
  889. case POP3_QUIT:
  890. state(data, POP3_STOP);
  891. break;
  892. default:
  893. /* internal error */
  894. state(data, POP3_STOP);
  895. break;
  896. }
  897. } while(!result && pop3c->state != POP3_STOP && Curl_pp_moredata(pp));
  898. return result;
  899. }
  900. /* Called repeatedly until done from multi.c */
  901. static CURLcode pop3_multi_statemach(struct Curl_easy *data, bool *done)
  902. {
  903. CURLcode result = CURLE_OK;
  904. struct connectdata *conn = data->conn;
  905. struct pop3_conn *pop3c = &conn->proto.pop3c;
  906. if((conn->handler->flags & PROTOPT_SSL) && !pop3c->ssldone) {
  907. bool ssldone = FALSE;
  908. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  909. pop3c->ssldone = ssldone;
  910. if(result || !pop3c->ssldone)
  911. return result;
  912. }
  913. result = Curl_pp_statemach(data, &pop3c->pp, FALSE, FALSE);
  914. *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
  915. return result;
  916. }
  917. static CURLcode pop3_block_statemach(struct Curl_easy *data,
  918. struct connectdata *conn,
  919. bool disconnecting)
  920. {
  921. CURLcode result = CURLE_OK;
  922. struct pop3_conn *pop3c = &conn->proto.pop3c;
  923. while(pop3c->state != POP3_STOP && !result)
  924. result = Curl_pp_statemach(data, &pop3c->pp, TRUE, disconnecting);
  925. return result;
  926. }
  927. /* Allocate and initialize the POP3 struct for the current Curl_easy if
  928. required */
  929. static CURLcode pop3_init(struct Curl_easy *data)
  930. {
  931. CURLcode result = CURLE_OK;
  932. struct POP3 *pop3;
  933. pop3 = data->req.p.pop3 = calloc(sizeof(struct POP3), 1);
  934. if(!pop3)
  935. result = CURLE_OUT_OF_MEMORY;
  936. return result;
  937. }
  938. /* For the POP3 "protocol connect" and "doing" phases only */
  939. static int pop3_getsock(struct Curl_easy *data,
  940. struct connectdata *conn, curl_socket_t *socks)
  941. {
  942. return Curl_pp_getsock(data, &conn->proto.pop3c.pp, socks);
  943. }
  944. /***********************************************************************
  945. *
  946. * pop3_connect()
  947. *
  948. * This function should do everything that is to be considered a part of the
  949. * connection phase.
  950. *
  951. * The variable 'done' points to will be TRUE if the protocol-layer connect
  952. * phase is done when this function returns, or FALSE if not.
  953. */
  954. static CURLcode pop3_connect(struct Curl_easy *data, bool *done)
  955. {
  956. CURLcode result = CURLE_OK;
  957. struct connectdata *conn = data->conn;
  958. struct pop3_conn *pop3c = &conn->proto.pop3c;
  959. struct pingpong *pp = &pop3c->pp;
  960. *done = FALSE; /* default to not done yet */
  961. /* We always support persistent connections in POP3 */
  962. connkeep(conn, "POP3 default");
  963. PINGPONG_SETUP(pp, pop3_statemachine, pop3_endofresp);
  964. /* Set the default preferred authentication type and mechanism */
  965. pop3c->preftype = POP3_TYPE_ANY;
  966. Curl_sasl_init(&pop3c->sasl, data, &saslpop3);
  967. /* Initialise the pingpong layer */
  968. Curl_pp_setup(pp);
  969. Curl_pp_init(data, pp);
  970. /* Parse the URL options */
  971. result = pop3_parse_url_options(conn);
  972. if(result)
  973. return result;
  974. /* Start off waiting for the server greeting response */
  975. state(data, POP3_SERVERGREET);
  976. result = pop3_multi_statemach(data, done);
  977. return result;
  978. }
  979. /***********************************************************************
  980. *
  981. * pop3_done()
  982. *
  983. * The DONE function. This does what needs to be done after a single DO has
  984. * performed.
  985. *
  986. * Input argument is already checked for validity.
  987. */
  988. static CURLcode pop3_done(struct Curl_easy *data, CURLcode status,
  989. bool premature)
  990. {
  991. CURLcode result = CURLE_OK;
  992. struct POP3 *pop3 = data->req.p.pop3;
  993. (void)premature;
  994. if(!pop3)
  995. return CURLE_OK;
  996. if(status) {
  997. connclose(data->conn, "POP3 done with bad status");
  998. result = status; /* use the already set error code */
  999. }
  1000. /* Cleanup our per-request based variables */
  1001. Curl_safefree(pop3->id);
  1002. Curl_safefree(pop3->custom);
  1003. /* Clear the transfer mode for the next request */
  1004. pop3->transfer = PPTRANSFER_BODY;
  1005. return result;
  1006. }
  1007. /***********************************************************************
  1008. *
  1009. * pop3_perform()
  1010. *
  1011. * This is the actual DO function for POP3. Get a message/listing according to
  1012. * the options previously setup.
  1013. */
  1014. static CURLcode pop3_perform(struct Curl_easy *data, bool *connected,
  1015. bool *dophase_done)
  1016. {
  1017. /* This is POP3 and no proxy */
  1018. CURLcode result = CURLE_OK;
  1019. struct POP3 *pop3 = data->req.p.pop3;
  1020. DEBUGF(infof(data, "DO phase starts"));
  1021. if(data->req.no_body) {
  1022. /* Requested no body means no transfer */
  1023. pop3->transfer = PPTRANSFER_INFO;
  1024. }
  1025. *dophase_done = FALSE; /* not done yet */
  1026. /* Start the first command in the DO phase */
  1027. result = pop3_perform_command(data);
  1028. if(result)
  1029. return result;
  1030. /* Run the state-machine */
  1031. result = pop3_multi_statemach(data, dophase_done);
  1032. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  1033. if(*dophase_done)
  1034. DEBUGF(infof(data, "DO phase is complete"));
  1035. return result;
  1036. }
  1037. /***********************************************************************
  1038. *
  1039. * pop3_do()
  1040. *
  1041. * This function is registered as 'curl_do' function. It decodes the path
  1042. * parts etc as a wrapper to the actual DO function (pop3_perform).
  1043. *
  1044. * The input argument is already checked for validity.
  1045. */
  1046. static CURLcode pop3_do(struct Curl_easy *data, bool *done)
  1047. {
  1048. CURLcode result = CURLE_OK;
  1049. *done = FALSE; /* default to false */
  1050. /* Parse the URL path */
  1051. result = pop3_parse_url_path(data);
  1052. if(result)
  1053. return result;
  1054. /* Parse the custom request */
  1055. result = pop3_parse_custom_request(data);
  1056. if(result)
  1057. return result;
  1058. result = pop3_regular_transfer(data, done);
  1059. return result;
  1060. }
  1061. /***********************************************************************
  1062. *
  1063. * pop3_disconnect()
  1064. *
  1065. * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
  1066. * resources. BLOCKING.
  1067. */
  1068. static CURLcode pop3_disconnect(struct Curl_easy *data,
  1069. struct connectdata *conn, bool dead_connection)
  1070. {
  1071. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1072. (void)data;
  1073. /* We cannot send quit unconditionally. If this connection is stale or
  1074. bad in any way, sending quit and waiting around here will make the
  1075. disconnect wait in vain and cause more problems than we need to. */
  1076. if(!dead_connection && conn->bits.protoconnstart) {
  1077. if(!pop3_perform_quit(data, conn))
  1078. (void)pop3_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
  1079. }
  1080. /* Disconnect from the server */
  1081. Curl_pp_disconnect(&pop3c->pp);
  1082. /* Cleanup the SASL module */
  1083. Curl_sasl_cleanup(conn, pop3c->sasl.authused);
  1084. /* Cleanup our connection based variables */
  1085. Curl_safefree(pop3c->apoptimestamp);
  1086. return CURLE_OK;
  1087. }
  1088. /* Call this when the DO phase has completed */
  1089. static CURLcode pop3_dophase_done(struct Curl_easy *data, bool connected)
  1090. {
  1091. (void)data;
  1092. (void)connected;
  1093. return CURLE_OK;
  1094. }
  1095. /* Called from multi.c while DOing */
  1096. static CURLcode pop3_doing(struct Curl_easy *data, bool *dophase_done)
  1097. {
  1098. CURLcode result = pop3_multi_statemach(data, dophase_done);
  1099. if(result)
  1100. DEBUGF(infof(data, "DO phase failed"));
  1101. else if(*dophase_done) {
  1102. result = pop3_dophase_done(data, FALSE /* not connected */);
  1103. DEBUGF(infof(data, "DO phase is complete"));
  1104. }
  1105. return result;
  1106. }
  1107. /***********************************************************************
  1108. *
  1109. * pop3_regular_transfer()
  1110. *
  1111. * The input argument is already checked for validity.
  1112. *
  1113. * Performs all commands done before a regular transfer between a local and a
  1114. * remote host.
  1115. */
  1116. static CURLcode pop3_regular_transfer(struct Curl_easy *data,
  1117. bool *dophase_done)
  1118. {
  1119. CURLcode result = CURLE_OK;
  1120. bool connected = FALSE;
  1121. /* Make sure size is unknown at this point */
  1122. data->req.size = -1;
  1123. /* Set the progress data */
  1124. Curl_pgrsSetUploadCounter(data, 0);
  1125. Curl_pgrsSetDownloadCounter(data, 0);
  1126. Curl_pgrsSetUploadSize(data, -1);
  1127. Curl_pgrsSetDownloadSize(data, -1);
  1128. /* Carry out the perform */
  1129. result = pop3_perform(data, &connected, dophase_done);
  1130. /* Perform post DO phase operations if necessary */
  1131. if(!result && *dophase_done)
  1132. result = pop3_dophase_done(data, connected);
  1133. return result;
  1134. }
  1135. static CURLcode pop3_setup_connection(struct Curl_easy *data,
  1136. struct connectdata *conn)
  1137. {
  1138. /* Initialise the POP3 layer */
  1139. CURLcode result = pop3_init(data);
  1140. if(result)
  1141. return result;
  1142. /* Clear the TLS upgraded flag */
  1143. conn->bits.tls_upgraded = FALSE;
  1144. return CURLE_OK;
  1145. }
  1146. /***********************************************************************
  1147. *
  1148. * pop3_parse_url_options()
  1149. *
  1150. * Parse the URL login options.
  1151. */
  1152. static CURLcode pop3_parse_url_options(struct connectdata *conn)
  1153. {
  1154. CURLcode result = CURLE_OK;
  1155. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1156. const char *ptr = conn->options;
  1157. while(!result && ptr && *ptr) {
  1158. const char *key = ptr;
  1159. const char *value;
  1160. while(*ptr && *ptr != '=')
  1161. ptr++;
  1162. value = ptr + 1;
  1163. while(*ptr && *ptr != ';')
  1164. ptr++;
  1165. if(strncasecompare(key, "AUTH=", 5)) {
  1166. result = Curl_sasl_parse_url_auth_option(&pop3c->sasl,
  1167. value, ptr - value);
  1168. if(result && strncasecompare(value, "+APOP", ptr - value)) {
  1169. pop3c->preftype = POP3_TYPE_APOP;
  1170. pop3c->sasl.prefmech = SASL_AUTH_NONE;
  1171. result = CURLE_OK;
  1172. }
  1173. }
  1174. else
  1175. result = CURLE_URL_MALFORMAT;
  1176. if(*ptr == ';')
  1177. ptr++;
  1178. }
  1179. if(pop3c->preftype != POP3_TYPE_APOP)
  1180. switch(pop3c->sasl.prefmech) {
  1181. case SASL_AUTH_NONE:
  1182. pop3c->preftype = POP3_TYPE_NONE;
  1183. break;
  1184. case SASL_AUTH_DEFAULT:
  1185. pop3c->preftype = POP3_TYPE_ANY;
  1186. break;
  1187. default:
  1188. pop3c->preftype = POP3_TYPE_SASL;
  1189. break;
  1190. }
  1191. return result;
  1192. }
  1193. /***********************************************************************
  1194. *
  1195. * pop3_parse_url_path()
  1196. *
  1197. * Parse the URL path into separate path components.
  1198. */
  1199. static CURLcode pop3_parse_url_path(struct Curl_easy *data)
  1200. {
  1201. /* The POP3 struct is already initialised in pop3_connect() */
  1202. struct POP3 *pop3 = data->req.p.pop3;
  1203. const char *path = &data->state.up.path[1]; /* skip leading path */
  1204. /* URL decode the path for the message ID */
  1205. return Curl_urldecode(path, 0, &pop3->id, NULL, REJECT_CTRL);
  1206. }
  1207. /***********************************************************************
  1208. *
  1209. * pop3_parse_custom_request()
  1210. *
  1211. * Parse the custom request.
  1212. */
  1213. static CURLcode pop3_parse_custom_request(struct Curl_easy *data)
  1214. {
  1215. CURLcode result = CURLE_OK;
  1216. struct POP3 *pop3 = data->req.p.pop3;
  1217. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1218. /* URL decode the custom request */
  1219. if(custom)
  1220. result = Curl_urldecode(custom, 0, &pop3->custom, NULL, REJECT_CTRL);
  1221. return result;
  1222. }
  1223. /***********************************************************************
  1224. *
  1225. * Curl_pop3_write()
  1226. *
  1227. * This function scans the body after the end-of-body and writes everything
  1228. * until the end is found.
  1229. */
  1230. CURLcode Curl_pop3_write(struct Curl_easy *data, char *str, size_t nread)
  1231. {
  1232. /* This code could be made into a special function in the handler struct */
  1233. CURLcode result = CURLE_OK;
  1234. struct SingleRequest *k = &data->req;
  1235. struct connectdata *conn = data->conn;
  1236. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1237. bool strip_dot = FALSE;
  1238. size_t last = 0;
  1239. size_t i;
  1240. /* Search through the buffer looking for the end-of-body marker which is
  1241. 5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
  1242. the eob so the server will have prefixed it with an extra dot which we
  1243. need to strip out. Additionally the marker could of course be spread out
  1244. over 5 different data chunks. */
  1245. for(i = 0; i < nread; i++) {
  1246. size_t prev = pop3c->eob;
  1247. switch(str[i]) {
  1248. case 0x0d:
  1249. if(pop3c->eob == 0) {
  1250. pop3c->eob++;
  1251. if(i) {
  1252. /* Write out the body part that didn't match */
  1253. result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
  1254. i - last);
  1255. if(result)
  1256. return result;
  1257. last = i;
  1258. }
  1259. }
  1260. else if(pop3c->eob == 3)
  1261. pop3c->eob++;
  1262. else
  1263. /* If the character match wasn't at position 0 or 3 then restart the
  1264. pattern matching */
  1265. pop3c->eob = 1;
  1266. break;
  1267. case 0x0a:
  1268. if(pop3c->eob == 1 || pop3c->eob == 4)
  1269. pop3c->eob++;
  1270. else
  1271. /* If the character match wasn't at position 1 or 4 then start the
  1272. search again */
  1273. pop3c->eob = 0;
  1274. break;
  1275. case 0x2e:
  1276. if(pop3c->eob == 2)
  1277. pop3c->eob++;
  1278. else if(pop3c->eob == 3) {
  1279. /* We have an extra dot after the CRLF which we need to strip off */
  1280. strip_dot = TRUE;
  1281. pop3c->eob = 0;
  1282. }
  1283. else
  1284. /* If the character match wasn't at position 2 then start the search
  1285. again */
  1286. pop3c->eob = 0;
  1287. break;
  1288. default:
  1289. pop3c->eob = 0;
  1290. break;
  1291. }
  1292. /* Did we have a partial match which has subsequently failed? */
  1293. if(prev && prev >= pop3c->eob) {
  1294. /* Strip can only be non-zero for the very first mismatch after CRLF
  1295. and then both prev and strip are equal and nothing will be output
  1296. below */
  1297. while(prev && pop3c->strip) {
  1298. prev--;
  1299. pop3c->strip--;
  1300. }
  1301. if(prev) {
  1302. /* If the partial match was the CRLF and dot then only write the CRLF
  1303. as the server would have inserted the dot */
  1304. if(strip_dot && prev - 1 > 0) {
  1305. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)POP3_EOB,
  1306. prev - 1);
  1307. }
  1308. else if(!strip_dot) {
  1309. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)POP3_EOB,
  1310. prev);
  1311. }
  1312. else {
  1313. result = CURLE_OK;
  1314. }
  1315. if(result)
  1316. return result;
  1317. last = i;
  1318. strip_dot = FALSE;
  1319. }
  1320. }
  1321. }
  1322. if(pop3c->eob == POP3_EOB_LEN) {
  1323. /* We have a full match so the transfer is done, however we must transfer
  1324. the CRLF at the start of the EOB as this is considered to be part of the
  1325. message as per RFC-1939, sect. 3 */
  1326. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *)POP3_EOB, 2);
  1327. k->keepon &= ~KEEP_RECV;
  1328. pop3c->eob = 0;
  1329. return result;
  1330. }
  1331. if(pop3c->eob)
  1332. /* While EOB is matching nothing should be output */
  1333. return CURLE_OK;
  1334. if(nread - last) {
  1335. result = Curl_client_write(data, CLIENTWRITE_BODY, &str[last],
  1336. nread - last);
  1337. }
  1338. return result;
  1339. }
  1340. #endif /* CURL_DISABLE_POP3 */