2
0

pop3.c 30 KB

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