pop3.c 45 KB

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