pop3.c 43 KB

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