pop3.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 http://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. * RFC1939 POP3 protocol
  22. * RFC2384 POP URL Scheme
  23. * RFC2595 Using TLS with IMAP, POP3 and ACAP
  24. *
  25. ***************************************************************************/
  26. #include "setup.h"
  27. #ifndef CURL_DISABLE_POP3
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36. #ifdef HAVE_SYS_SOCKET_H
  37. #include <sys/socket.h>
  38. #endif
  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 "easyif.h" /* for Curl_convert_... prototypes */
  63. #include "if2ip.h"
  64. #include "hostip.h"
  65. #include "progress.h"
  66. #include "transfer.h"
  67. #include "escape.h"
  68. #include "http.h" /* for HTTP proxy tunnel stuff */
  69. #include "socks.h"
  70. #include "pop3.h"
  71. #include "strtoofft.h"
  72. #include "strequal.h"
  73. #include "sslgen.h"
  74. #include "connect.h"
  75. #include "strerror.h"
  76. #include "select.h"
  77. #include "multiif.h"
  78. #include "url.h"
  79. #include "rawstr.h"
  80. #include "strtoofft.h"
  81. #define _MPRINTF_REPLACE /* use our functions only */
  82. #include <curl/mprintf.h>
  83. #include "curl_memory.h"
  84. /* The last #include file should be: */
  85. #include "memdebug.h"
  86. /* Local API functions */
  87. static CURLcode pop3_parse_url_path(struct connectdata *conn);
  88. static CURLcode pop3_regular_transfer(struct connectdata *conn, bool *done);
  89. static CURLcode pop3_do(struct connectdata *conn, bool *done);
  90. static CURLcode pop3_done(struct connectdata *conn,
  91. CURLcode, bool premature);
  92. static CURLcode pop3_connect(struct connectdata *conn, bool *done);
  93. static CURLcode pop3_disconnect(struct connectdata *conn, bool dead_connection);
  94. static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done);
  95. static int pop3_getsock(struct connectdata *conn,
  96. curl_socket_t *socks,
  97. int numsocks);
  98. static CURLcode pop3_doing(struct connectdata *conn,
  99. bool *dophase_done);
  100. static CURLcode pop3_setup_connection(struct connectdata * conn);
  101. /*
  102. * POP3 protocol handler.
  103. */
  104. const struct Curl_handler Curl_handler_pop3 = {
  105. "POP3", /* scheme */
  106. pop3_setup_connection, /* setup_connection */
  107. pop3_do, /* do_it */
  108. pop3_done, /* done */
  109. ZERO_NULL, /* do_more */
  110. pop3_connect, /* connect_it */
  111. pop3_multi_statemach, /* connecting */
  112. pop3_doing, /* doing */
  113. pop3_getsock, /* proto_getsock */
  114. pop3_getsock, /* doing_getsock */
  115. ZERO_NULL, /* perform_getsock */
  116. pop3_disconnect, /* disconnect */
  117. PORT_POP3, /* defport */
  118. PROT_POP3 /* protocol */
  119. };
  120. #ifdef USE_SSL
  121. /*
  122. * POP3S protocol handler.
  123. */
  124. const struct Curl_handler Curl_handler_pop3s = {
  125. "POP3S", /* scheme */
  126. pop3_setup_connection, /* setup_connection */
  127. pop3_do, /* do_it */
  128. pop3_done, /* done */
  129. ZERO_NULL, /* do_more */
  130. pop3_connect, /* connect_it */
  131. pop3_multi_statemach, /* connecting */
  132. pop3_doing, /* doing */
  133. pop3_getsock, /* proto_getsock */
  134. pop3_getsock, /* doing_getsock */
  135. ZERO_NULL, /* perform_getsock */
  136. pop3_disconnect, /* disconnect */
  137. PORT_POP3S, /* defport */
  138. PROT_POP3 | PROT_POP3S | PROT_SSL /* protocol */
  139. };
  140. #endif
  141. #ifndef CURL_DISABLE_HTTP
  142. /*
  143. * HTTP-proxyed POP3 protocol handler.
  144. */
  145. static const struct Curl_handler Curl_handler_pop3_proxy = {
  146. "POP3", /* scheme */
  147. ZERO_NULL, /* setup_connection */
  148. Curl_http, /* do_it */
  149. Curl_http_done, /* done */
  150. ZERO_NULL, /* do_more */
  151. ZERO_NULL, /* connect_it */
  152. ZERO_NULL, /* connecting */
  153. ZERO_NULL, /* doing */
  154. ZERO_NULL, /* proto_getsock */
  155. ZERO_NULL, /* doing_getsock */
  156. ZERO_NULL, /* perform_getsock */
  157. ZERO_NULL, /* disconnect */
  158. PORT_POP3, /* defport */
  159. PROT_HTTP /* protocol */
  160. };
  161. #ifdef USE_SSL
  162. /*
  163. * HTTP-proxyed POP3S protocol handler.
  164. */
  165. static const struct Curl_handler Curl_handler_pop3s_proxy = {
  166. "POP3S", /* scheme */
  167. ZERO_NULL, /* setup_connection */
  168. Curl_http, /* do_it */
  169. Curl_http_done, /* done */
  170. ZERO_NULL, /* do_more */
  171. ZERO_NULL, /* connect_it */
  172. ZERO_NULL, /* connecting */
  173. ZERO_NULL, /* doing */
  174. ZERO_NULL, /* proto_getsock */
  175. ZERO_NULL, /* doing_getsock */
  176. ZERO_NULL, /* perform_getsock */
  177. ZERO_NULL, /* disconnect */
  178. PORT_POP3S, /* defport */
  179. PROT_HTTP /* protocol */
  180. };
  181. #endif
  182. #endif
  183. /* function that checks for a pop3 status code at the start of the given
  184. string */
  185. static int pop3_endofresp(struct pingpong *pp,
  186. int *resp)
  187. {
  188. char *line = pp->linestart_resp;
  189. size_t len = pp->nread_resp;
  190. if( ((len >= 3) && !memcmp("+OK", line, 3)) ||
  191. ((len >= 4) && !memcmp("-ERR", line, 4)) ) {
  192. *resp=line[1]; /* O or E */
  193. return TRUE;
  194. }
  195. return FALSE; /* nothing for us */
  196. }
  197. /* This is the ONLY way to change POP3 state! */
  198. static void state(struct connectdata *conn,
  199. pop3state newstate)
  200. {
  201. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  202. /* for debug purposes */
  203. static const char * const names[]={
  204. "STOP",
  205. "SERVERGREET",
  206. "USER",
  207. "PASS",
  208. "STARTTLS",
  209. "LIST",
  210. "RETR",
  211. "QUIT",
  212. /* LAST */
  213. };
  214. #endif
  215. struct pop3_conn *pop3c = &conn->proto.pop3c;
  216. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  217. if(pop3c->state != newstate)
  218. infof(conn->data, "POP3 %p state change from %s to %s\n",
  219. pop3c, names[pop3c->state], names[newstate]);
  220. #endif
  221. pop3c->state = newstate;
  222. }
  223. static CURLcode pop3_state_user(struct connectdata *conn)
  224. {
  225. CURLcode result;
  226. struct FTP *pop3 = conn->data->state.proto.pop3;
  227. /* send USER */
  228. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "USER %s",
  229. pop3->user?pop3->user:"");
  230. if(result)
  231. return result;
  232. state(conn, POP3_USER);
  233. return CURLE_OK;
  234. }
  235. /* For the POP3 "protocol connect" and "doing" phases only */
  236. static int pop3_getsock(struct connectdata *conn,
  237. curl_socket_t *socks,
  238. int numsocks)
  239. {
  240. return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks);
  241. }
  242. /* for STARTTLS responses */
  243. static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
  244. int pop3code,
  245. pop3state instate)
  246. {
  247. CURLcode result = CURLE_OK;
  248. struct SessionHandle *data = conn->data;
  249. (void)instate; /* no use for this yet */
  250. if(pop3code != 'O') {
  251. failf(data, "STARTTLS denied. %c", pop3code);
  252. result = CURLE_LOGIN_DENIED;
  253. }
  254. else {
  255. /* Curl_ssl_connect is BLOCKING */
  256. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  257. if(CURLE_OK == result) {
  258. conn->protocol |= PROT_POP3S;
  259. result = pop3_state_user(conn);
  260. }
  261. }
  262. state(conn, POP3_STOP);
  263. return result;
  264. }
  265. /* for USER responses */
  266. static CURLcode pop3_state_user_resp(struct connectdata *conn,
  267. int pop3code,
  268. pop3state instate)
  269. {
  270. CURLcode result = CURLE_OK;
  271. struct SessionHandle *data = conn->data;
  272. struct FTP *pop3 = data->state.proto.pop3;
  273. (void)instate; /* no use for this yet */
  274. if(pop3code != 'O') {
  275. failf(data, "Access denied. %c", pop3code);
  276. result = CURLE_LOGIN_DENIED;
  277. }
  278. else
  279. /* send PASS */
  280. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "PASS %s",
  281. pop3->passwd?pop3->passwd:"");
  282. if(result)
  283. return result;
  284. state(conn, POP3_PASS);
  285. return result;
  286. }
  287. /* for PASS responses */
  288. static CURLcode pop3_state_pass_resp(struct connectdata *conn,
  289. int pop3code,
  290. pop3state instate)
  291. {
  292. CURLcode result = CURLE_OK;
  293. struct SessionHandle *data = conn->data;
  294. (void)instate; /* no use for this yet */
  295. if(pop3code != 'O') {
  296. failf(data, "Access denied. %c", pop3code);
  297. result = CURLE_LOGIN_DENIED;
  298. }
  299. state(conn, POP3_STOP);
  300. return result;
  301. }
  302. /* for the retr response */
  303. static CURLcode pop3_state_retr_resp(struct connectdata *conn,
  304. int pop3code,
  305. pop3state instate)
  306. {
  307. CURLcode result = CURLE_OK;
  308. struct SessionHandle *data = conn->data;
  309. struct FTP *pop3 = data->state.proto.pop3;
  310. struct pop3_conn *pop3c = &conn->proto.pop3c;
  311. struct pingpong *pp = &pop3c->pp;
  312. (void)instate; /* no use for this yet */
  313. if('O' != pop3code) {
  314. state(conn, POP3_STOP);
  315. return CURLE_RECV_ERROR;
  316. }
  317. /* POP3 download */
  318. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE,
  319. pop3->bytecountp, -1, NULL); /* no upload here */
  320. if(pp->cache) {
  321. /* At this point there is a bunch of data in the header "cache" that is
  322. actually body content, send it as body and then skip it. Do note
  323. that there may even be additional "headers" after the body. */
  324. /* we may get the EOB already here! */
  325. result = Curl_pop3_write(conn, pp->cache, pp->cache_size);
  326. if(result)
  327. return result;
  328. /* cache is drained */
  329. free(pp->cache);
  330. pp->cache = NULL;
  331. pp->cache_size = 0;
  332. }
  333. state(conn, POP3_STOP);
  334. return result;
  335. }
  336. /* for the list response */
  337. static CURLcode pop3_state_list_resp(struct connectdata *conn,
  338. int pop3code,
  339. pop3state instate)
  340. {
  341. CURLcode result = CURLE_OK;
  342. struct SessionHandle *data = conn->data;
  343. struct FTP *pop3 = data->state.proto.pop3;
  344. struct pop3_conn *pop3c = &conn->proto.pop3c;
  345. struct pingpong *pp = &pop3c->pp;
  346. (void)instate; /* no use for this yet */
  347. if('O' != pop3code) {
  348. state(conn, POP3_STOP);
  349. return CURLE_RECV_ERROR;
  350. }
  351. /* POP3 download */
  352. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, pop3->bytecountp,
  353. -1, NULL); /* no upload here */
  354. if(pp->cache) {
  355. /* cache holds the email ID listing */
  356. /* we may get the EOB already here! */
  357. result = Curl_pop3_write(conn, pp->cache, pp->cache_size);
  358. if(result)
  359. return result;
  360. /* cache is drained */
  361. free(pp->cache);
  362. pp->cache = NULL;
  363. pp->cache_size = 0;
  364. }
  365. state(conn, POP3_STOP);
  366. return result;
  367. }
  368. /* start the DO phase for RETR */
  369. static CURLcode pop3_retr(struct connectdata *conn)
  370. {
  371. CURLcode result = CURLE_OK;
  372. struct pop3_conn *pop3c = &conn->proto.pop3c;
  373. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "RETR %s", pop3c->mailbox);
  374. if(result)
  375. return result;
  376. state(conn, POP3_RETR);
  377. return result;
  378. }
  379. /* start the DO phase for LIST */
  380. static CURLcode pop3_list(struct connectdata *conn)
  381. {
  382. CURLcode result = CURLE_OK;
  383. struct pop3_conn *pop3c = &conn->proto.pop3c;
  384. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "LIST %s", pop3c->mailbox);
  385. if(result)
  386. return result;
  387. state(conn, POP3_LIST);
  388. return result;
  389. }
  390. static CURLcode pop3_statemach_act(struct connectdata *conn)
  391. {
  392. CURLcode result;
  393. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  394. struct SessionHandle *data=conn->data;
  395. int pop3code;
  396. struct pop3_conn *pop3c = &conn->proto.pop3c;
  397. struct pingpong *pp = &pop3c->pp;
  398. size_t nread = 0;
  399. if(pp->sendleft)
  400. return Curl_pp_flushsend(pp);
  401. /* we read a piece of response */
  402. result = Curl_pp_readresp(sock, pp, &pop3code, &nread);
  403. if(result)
  404. return result;
  405. if(pop3code) {
  406. /* we have now received a full POP3 server response */
  407. switch(pop3c->state) {
  408. case POP3_SERVERGREET:
  409. if(pop3code != 'O') {
  410. failf(data, "Got unexpected pop3-server response");
  411. return CURLE_FTP_WEIRD_SERVER_REPLY;
  412. }
  413. if(data->set.ftp_ssl && !conn->ssl[FIRSTSOCKET].use) {
  414. /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
  415. to TLS connection now */
  416. result = Curl_pp_sendf(&pop3c->pp, "STARTTLS", NULL);
  417. state(conn, POP3_STARTTLS);
  418. }
  419. else
  420. result = pop3_state_user(conn);
  421. if(result)
  422. return result;
  423. break;
  424. case POP3_USER:
  425. result = pop3_state_user_resp(conn, pop3code, pop3c->state);
  426. break;
  427. case POP3_PASS:
  428. result = pop3_state_pass_resp(conn, pop3code, pop3c->state);
  429. break;
  430. case POP3_STARTTLS:
  431. result = pop3_state_starttls_resp(conn, pop3code, pop3c->state);
  432. break;
  433. case POP3_RETR:
  434. result = pop3_state_retr_resp(conn, pop3code, pop3c->state);
  435. break;
  436. case POP3_LIST:
  437. result = pop3_state_list_resp(conn, pop3code, pop3c->state);
  438. break;
  439. case POP3_QUIT:
  440. /* fallthrough, just stop! */
  441. default:
  442. /* internal error */
  443. state(conn, POP3_STOP);
  444. break;
  445. }
  446. }
  447. return result;
  448. }
  449. /* called repeatedly until done from multi.c */
  450. static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done)
  451. {
  452. struct pop3_conn *pop3c = &conn->proto.pop3c;
  453. CURLcode result = Curl_pp_multi_statemach(&pop3c->pp);
  454. *done = (bool)(pop3c->state == POP3_STOP);
  455. return result;
  456. }
  457. static CURLcode pop3_easy_statemach(struct connectdata *conn)
  458. {
  459. struct pop3_conn *pop3c = &conn->proto.pop3c;
  460. struct pingpong *pp = &pop3c->pp;
  461. CURLcode result = CURLE_OK;
  462. while(pop3c->state != POP3_STOP) {
  463. result = Curl_pp_easy_statemach(pp);
  464. if(result)
  465. break;
  466. }
  467. return result;
  468. }
  469. /*
  470. * Allocate and initialize the struct POP3 for the current SessionHandle. If
  471. * need be.
  472. */
  473. static CURLcode pop3_init(struct connectdata *conn)
  474. {
  475. struct SessionHandle *data = conn->data;
  476. struct FTP *pop3 = data->state.proto.pop3;
  477. if(!pop3) {
  478. pop3 = data->state.proto.pop3 = calloc(sizeof(struct FTP), 1);
  479. if(!pop3)
  480. return CURLE_OUT_OF_MEMORY;
  481. }
  482. /* get some initial data into the pop3 struct */
  483. pop3->bytecountp = &data->req.bytecount;
  484. /* No need to duplicate user+password, the connectdata struct won't change
  485. during a session, but we re-init them here since on subsequent inits
  486. since the conn struct may have changed or been replaced.
  487. */
  488. pop3->user = conn->user;
  489. pop3->passwd = conn->passwd;
  490. return CURLE_OK;
  491. }
  492. /*
  493. * pop3_connect() should do everything that is to be considered a part of
  494. * the connection phase.
  495. *
  496. * The variable 'done' points to will be TRUE if the protocol-layer connect
  497. * phase is done when this function returns, or FALSE is not. When called as
  498. * a part of the easy interface, it will always be TRUE.
  499. */
  500. static CURLcode pop3_connect(struct connectdata *conn,
  501. bool *done) /* see description above */
  502. {
  503. CURLcode result;
  504. struct pop3_conn *pop3c = &conn->proto.pop3c;
  505. struct SessionHandle *data=conn->data;
  506. struct pingpong *pp = &pop3c->pp;
  507. *done = FALSE; /* default to not done yet */
  508. /* If there already is a protocol-specific struct allocated for this
  509. sessionhandle, deal with it */
  510. Curl_reset_reqproto(conn);
  511. result = pop3_init(conn);
  512. if(CURLE_OK != result)
  513. return result;
  514. /* We always support persistant connections on pop3 */
  515. conn->bits.close = FALSE;
  516. pp->response_time = RESP_TIMEOUT; /* set default response time-out */
  517. pp->statemach_act = pop3_statemach_act;
  518. pp->endofresp = pop3_endofresp;
  519. pp->conn = conn;
  520. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
  521. if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
  522. /* for POP3 over HTTP proxy */
  523. struct HTTP http_proxy;
  524. struct FTP *pop3_save;
  525. /* BLOCKING */
  526. /* We want "seamless" POP3 operations through HTTP proxy tunnel */
  527. /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the member
  528. * conn->proto.http; we want POP3 through HTTP and we have to change the
  529. * member temporarily for connecting to the HTTP proxy. After
  530. * Curl_proxyCONNECT we have to set back the member to the original struct
  531. * POP3 pointer
  532. */
  533. pop3_save = data->state.proto.pop3;
  534. memset(&http_proxy, 0, sizeof(http_proxy));
  535. data->state.proto.http = &http_proxy;
  536. result = Curl_proxyCONNECT(conn, FIRSTSOCKET,
  537. conn->host.name, conn->remote_port);
  538. data->state.proto.pop3 = pop3_save;
  539. if(CURLE_OK != result)
  540. return result;
  541. }
  542. #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */
  543. if(conn->protocol & PROT_POP3S) {
  544. /* BLOCKING */
  545. /* POP3S is simply pop3 with SSL for the control channel */
  546. /* now, perform the SSL initialization for this socket */
  547. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  548. if(result)
  549. return result;
  550. }
  551. Curl_pp_init(pp); /* init the response reader stuff */
  552. /* When we connect, we start in the state where we await the server greet
  553. response */
  554. state(conn, POP3_SERVERGREET);
  555. if(data->state.used_interface == Curl_if_multi)
  556. result = pop3_multi_statemach(conn, done);
  557. else {
  558. result = pop3_easy_statemach(conn);
  559. if(!result)
  560. *done = TRUE;
  561. }
  562. return result;
  563. }
  564. /***********************************************************************
  565. *
  566. * pop3_done()
  567. *
  568. * The DONE function. This does what needs to be done after a single DO has
  569. * performed.
  570. *
  571. * Input argument is already checked for validity.
  572. */
  573. static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
  574. bool premature)
  575. {
  576. struct SessionHandle *data = conn->data;
  577. struct FTP *pop3 = data->state.proto.pop3;
  578. CURLcode result=CURLE_OK;
  579. (void)premature;
  580. if(!pop3)
  581. /* When the easy handle is removed from the multi while libcurl is still
  582. * trying to resolve the host name, it seems that the pop3 struct is not
  583. * yet initialized, but the removal action calls Curl_done() which calls
  584. * this function. So we simply return success if no pop3 pointer is set.
  585. */
  586. return CURLE_OK;
  587. if(status) {
  588. conn->bits.close = TRUE; /* marked for closure */
  589. result = status; /* use the already set error code */
  590. }
  591. /* clear these for next connection */
  592. pop3->transfer = FTPTRANSFER_BODY;
  593. return result;
  594. }
  595. /***********************************************************************
  596. *
  597. * pop3_perform()
  598. *
  599. * This is the actual DO function for POP3. Get a file/directory according to
  600. * the options previously setup.
  601. */
  602. static
  603. CURLcode pop3_perform(struct connectdata *conn,
  604. bool *connected, /* connect status after PASV / PORT */
  605. bool *dophase_done)
  606. {
  607. /* this is POP3 and no proxy */
  608. CURLcode result=CURLE_OK;
  609. struct pop3_conn *pop3c = &conn->proto.pop3c;
  610. DEBUGF(infof(conn->data, "DO phase starts\n"));
  611. if(conn->data->set.opt_no_body) {
  612. /* requested no body means no transfer... */
  613. struct FTP *pop3 = conn->data->state.proto.pop3;
  614. pop3->transfer = FTPTRANSFER_INFO;
  615. }
  616. *dophase_done = FALSE; /* not done yet */
  617. /* start the first command in the DO phase */
  618. /* If mailbox is empty, then assume user wants listing for mail IDs,
  619. * otherwise, attempt to retrieve the mail-id stored in mailbox
  620. */
  621. if (strlen(pop3c->mailbox))
  622. result = pop3_retr(conn);
  623. else
  624. result = pop3_list(conn);
  625. if(result)
  626. return result;
  627. /* run the state-machine */
  628. if(conn->data->state.used_interface == Curl_if_multi)
  629. result = pop3_multi_statemach(conn, dophase_done);
  630. else {
  631. result = pop3_easy_statemach(conn);
  632. *dophase_done = TRUE; /* with the easy interface we are done here */
  633. }
  634. *connected = conn->bits.tcpconnect;
  635. if(*dophase_done)
  636. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  637. return result;
  638. }
  639. /***********************************************************************
  640. *
  641. * pop3_do()
  642. *
  643. * This function is registered as 'curl_do' function. It decodes the path
  644. * parts etc as a wrapper to the actual DO function (pop3_perform).
  645. *
  646. * The input argument is already checked for validity.
  647. */
  648. static CURLcode pop3_do(struct connectdata *conn, bool *done)
  649. {
  650. CURLcode retcode = CURLE_OK;
  651. *done = FALSE; /* default to false */
  652. /*
  653. Since connections can be re-used between SessionHandles, this might be a
  654. connection already existing but on a fresh SessionHandle struct so we must
  655. make sure we have a good 'struct POP3' to play with. For new connections,
  656. the struct POP3 is allocated and setup in the pop3_connect() function.
  657. */
  658. Curl_reset_reqproto(conn);
  659. retcode = pop3_init(conn);
  660. if(retcode)
  661. return retcode;
  662. retcode = pop3_parse_url_path(conn);
  663. if(retcode)
  664. return retcode;
  665. retcode = pop3_regular_transfer(conn, done);
  666. return retcode;
  667. }
  668. /***********************************************************************
  669. *
  670. * pop3_quit()
  671. *
  672. * This should be called before calling sclose(). We should then wait for the
  673. * response from the server before returning. The calling code should then try
  674. * to close the connection.
  675. *
  676. */
  677. static CURLcode pop3_quit(struct connectdata *conn)
  678. {
  679. CURLcode result = CURLE_OK;
  680. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "QUIT", NULL);
  681. if(result)
  682. return result;
  683. state(conn, POP3_QUIT);
  684. result = pop3_easy_statemach(conn);
  685. return result;
  686. }
  687. /***********************************************************************
  688. *
  689. * pop3_disconnect()
  690. *
  691. * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
  692. * resources. BLOCKING.
  693. */
  694. static CURLcode pop3_disconnect(struct connectdata *conn, bool dead_connection)
  695. {
  696. struct pop3_conn *pop3c= &conn->proto.pop3c;
  697. /* We cannot send quit unconditionally. If this connection is stale or
  698. bad in any way, sending quit and waiting around here will make the
  699. disconnect wait in vain and cause more problems than we need to.
  700. */
  701. /* The POP3 session may or may not have been allocated/setup at this
  702. point! */
  703. if(!dead_connection && pop3c->pp.conn)
  704. (void)pop3_quit(conn); /* ignore errors on the LOGOUT */
  705. Curl_pp_disconnect(&pop3c->pp);
  706. return CURLE_OK;
  707. }
  708. /***********************************************************************
  709. *
  710. * pop3_parse_url_path()
  711. *
  712. * Parse the URL path into separate path components.
  713. *
  714. */
  715. static CURLcode pop3_parse_url_path(struct connectdata *conn)
  716. {
  717. /* the pop3 struct is already inited in pop3_connect() */
  718. struct pop3_conn *pop3c = &conn->proto.pop3c;
  719. struct SessionHandle *data = conn->data;
  720. const char *path = data->state.path;
  721. /* url decode the path and use this mailbox */
  722. pop3c->mailbox = curl_easy_unescape(data, path, 0, NULL);
  723. if (!pop3c->mailbox)
  724. return CURLE_OUT_OF_MEMORY;
  725. return CURLE_OK;
  726. }
  727. /* call this when the DO phase has completed */
  728. static CURLcode pop3_dophase_done(struct connectdata *conn,
  729. bool connected)
  730. {
  731. struct FTP *pop3 = conn->data->state.proto.pop3;
  732. struct pop3_conn *pop3c = &conn->proto.pop3c;
  733. (void)connected;
  734. if(pop3->transfer != FTPTRANSFER_BODY)
  735. /* no data to transfer */
  736. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  737. free(pop3c->mailbox);
  738. return CURLE_OK;
  739. }
  740. /* called from multi.c while DOing */
  741. static CURLcode pop3_doing(struct connectdata *conn,
  742. bool *dophase_done)
  743. {
  744. CURLcode result;
  745. result = pop3_multi_statemach(conn, dophase_done);
  746. if(*dophase_done) {
  747. result = pop3_dophase_done(conn, FALSE /* not connected */);
  748. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  749. }
  750. return result;
  751. }
  752. /***********************************************************************
  753. *
  754. * pop3_regular_transfer()
  755. *
  756. * The input argument is already checked for validity.
  757. *
  758. * Performs all commands done before a regular transfer between a local and a
  759. * remote host.
  760. *
  761. */
  762. static
  763. CURLcode pop3_regular_transfer(struct connectdata *conn,
  764. bool *dophase_done)
  765. {
  766. CURLcode result=CURLE_OK;
  767. bool connected=FALSE;
  768. struct SessionHandle *data = conn->data;
  769. data->req.size = -1; /* make sure this is unknown at this point */
  770. Curl_pgrsSetUploadCounter(data, 0);
  771. Curl_pgrsSetDownloadCounter(data, 0);
  772. Curl_pgrsSetUploadSize(data, 0);
  773. Curl_pgrsSetDownloadSize(data, 0);
  774. result = pop3_perform(conn,
  775. &connected, /* have we connected after PASV/PORT */
  776. dophase_done); /* all commands in the DO-phase done? */
  777. if(CURLE_OK == result) {
  778. if(!*dophase_done)
  779. /* the DO phase has not completed yet */
  780. return CURLE_OK;
  781. result = pop3_dophase_done(conn, connected);
  782. if(result)
  783. return result;
  784. }
  785. return result;
  786. }
  787. static CURLcode pop3_setup_connection(struct connectdata * conn)
  788. {
  789. struct SessionHandle *data = conn->data;
  790. if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
  791. /* Unless we have asked to tunnel pop3 operations through the proxy, we
  792. switch and use HTTP operations only */
  793. #ifndef CURL_DISABLE_HTTP
  794. if(conn->handler == &Curl_handler_pop3)
  795. conn->handler = &Curl_handler_pop3_proxy;
  796. else {
  797. #ifdef USE_SSL
  798. conn->handler = &Curl_handler_pop3s_proxy;
  799. #else
  800. failf(data, "POP3S not supported!");
  801. return CURLE_UNSUPPORTED_PROTOCOL;
  802. #endif
  803. }
  804. /*
  805. * We explicitly mark this connection as persistent here as we're doing
  806. * POP3 over HTTP and thus we accidentally avoid setting this value
  807. * otherwise.
  808. */
  809. conn->bits.close = FALSE;
  810. #else
  811. failf(data, "POP3 over http proxy requires HTTP support built-in!");
  812. return CURLE_UNSUPPORTED_PROTOCOL;
  813. #endif
  814. }
  815. data->state.path++; /* don't include the initial slash */
  816. return CURLE_OK;
  817. }
  818. /* this is the 5-bytes End-Of-Body marker for POP3 */
  819. #define POP3_EOB "\x0d\x0a\x2e\x0d\x0a"
  820. #define POP3_EOB_LEN 5
  821. /*
  822. * This function scans the body after the end-of-body and writes everything
  823. * until the end is found.
  824. */
  825. CURLcode Curl_pop3_write(struct connectdata *conn,
  826. char *str,
  827. size_t nread)
  828. {
  829. /* This code could be made into a special function in the handler struct. */
  830. CURLcode result;
  831. struct SessionHandle *data = conn->data;
  832. struct SingleRequest *k = &data->req;
  833. /* Detect the end-of-body marker, which is 5 bytes:
  834. 0d 0a 2e 0d 0a. This marker can of course be spread out
  835. over up to 5 different data chunks. Deal with it! */
  836. struct pop3_conn *pop3c = &conn->proto.pop3c;
  837. size_t checkmax = (nread >= POP3_EOB_LEN?POP3_EOB_LEN:nread);
  838. size_t checkleft = POP3_EOB_LEN-pop3c->eob;
  839. size_t check = (checkmax >= checkleft?checkleft:checkmax);
  840. if(!memcmp(POP3_EOB, &str[nread - check], check)) {
  841. /* substring match */
  842. pop3c->eob += check;
  843. if(pop3c->eob == POP3_EOB_LEN) {
  844. /* full match, the transfer is done! */
  845. str[nread - check] = '\0';
  846. nread -= check;
  847. k->keepon &= ~KEEP_RECV;
  848. pop3c->eob = 0;
  849. }
  850. }
  851. else if(pop3c->eob) {
  852. /* not a match, but we matched a piece before so we must now
  853. send that part as body first, before we move on and send
  854. this buffer */
  855. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  856. (char *)POP3_EOB, pop3c->eob);
  857. if(result)
  858. return result;
  859. pop3c->eob = 0;
  860. }
  861. result = Curl_client_write(conn, CLIENTWRITE_BODY, str, nread);
  862. return result;
  863. }
  864. #endif /* CURL_DISABLE_POP3 */