pop3.c 45 KB

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