pop3.c 45 KB

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