pop3.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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. * 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. * RFC4616 PLAIN authentication
  29. *
  30. ***************************************************************************/
  31. #include "setup.h"
  32. #ifndef CURL_DISABLE_POP3
  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 "if2ip.h"
  63. #include "hostip.h"
  64. #include "progress.h"
  65. #include "transfer.h"
  66. #include "escape.h"
  67. #include "http.h" /* for HTTP proxy tunnel stuff */
  68. #include "socks.h"
  69. #include "pop3.h"
  70. #include "strtoofft.h"
  71. #include "strequal.h"
  72. #include "sslgen.h"
  73. #include "connect.h"
  74. #include "strerror.h"
  75. #include "select.h"
  76. #include "multiif.h"
  77. #include "url.h"
  78. #include "rawstr.h"
  79. #include "curl_sasl.h"
  80. #include "curl_md5.h"
  81. #include "warnless.h"
  82. #define _MPRINTF_REPLACE /* use our functions only */
  83. #include <curl/mprintf.h>
  84. #include "curl_memory.h"
  85. /* The last #include file should be: */
  86. #include "memdebug.h"
  87. /* Local API functions */
  88. static CURLcode pop3_parse_url_path(struct connectdata *conn);
  89. static CURLcode pop3_parse_custom_request(struct connectdata *conn);
  90. static CURLcode pop3_regular_transfer(struct connectdata *conn, bool *done);
  91. static CURLcode pop3_do(struct connectdata *conn, bool *done);
  92. static CURLcode pop3_done(struct connectdata *conn,
  93. CURLcode, bool premature);
  94. static CURLcode pop3_connect(struct connectdata *conn, bool *done);
  95. static CURLcode pop3_disconnect(struct connectdata *conn, bool dead);
  96. static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done);
  97. static int pop3_getsock(struct connectdata *conn,
  98. curl_socket_t *socks,
  99. int numsocks);
  100. static CURLcode pop3_doing(struct connectdata *conn,
  101. bool *dophase_done);
  102. static CURLcode pop3_setup_connection(struct connectdata * conn);
  103. /*
  104. * POP3 protocol handler.
  105. */
  106. const struct Curl_handler Curl_handler_pop3 = {
  107. "POP3", /* scheme */
  108. pop3_setup_connection, /* setup_connection */
  109. pop3_do, /* do_it */
  110. pop3_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. pop3_connect, /* connect_it */
  113. pop3_multi_statemach, /* connecting */
  114. pop3_doing, /* doing */
  115. pop3_getsock, /* proto_getsock */
  116. pop3_getsock, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. pop3_disconnect, /* disconnect */
  120. ZERO_NULL, /* readwrite */
  121. PORT_POP3, /* defport */
  122. CURLPROTO_POP3, /* protocol */
  123. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY /* flags */
  124. };
  125. #ifdef USE_SSL
  126. /*
  127. * POP3S protocol handler.
  128. */
  129. const struct Curl_handler Curl_handler_pop3s = {
  130. "POP3S", /* scheme */
  131. pop3_setup_connection, /* setup_connection */
  132. pop3_do, /* do_it */
  133. pop3_done, /* done */
  134. ZERO_NULL, /* do_more */
  135. pop3_connect, /* connect_it */
  136. pop3_multi_statemach, /* connecting */
  137. pop3_doing, /* doing */
  138. pop3_getsock, /* proto_getsock */
  139. pop3_getsock, /* doing_getsock */
  140. ZERO_NULL, /* domore_getsock */
  141. ZERO_NULL, /* perform_getsock */
  142. pop3_disconnect, /* disconnect */
  143. ZERO_NULL, /* readwrite */
  144. PORT_POP3S, /* defport */
  145. CURLPROTO_POP3 | CURLPROTO_POP3S, /* protocol */
  146. PROTOPT_CLOSEACTION | PROTOPT_SSL
  147. | PROTOPT_NOURLQUERY /* flags */
  148. };
  149. #endif
  150. #ifndef CURL_DISABLE_HTTP
  151. /*
  152. * HTTP-proxyed POP3 protocol handler.
  153. */
  154. static const struct Curl_handler Curl_handler_pop3_proxy = {
  155. "POP3", /* scheme */
  156. ZERO_NULL, /* setup_connection */
  157. Curl_http, /* do_it */
  158. Curl_http_done, /* done */
  159. ZERO_NULL, /* do_more */
  160. ZERO_NULL, /* connect_it */
  161. ZERO_NULL, /* connecting */
  162. ZERO_NULL, /* doing */
  163. ZERO_NULL, /* proto_getsock */
  164. ZERO_NULL, /* doing_getsock */
  165. ZERO_NULL, /* domore_getsock */
  166. ZERO_NULL, /* perform_getsock */
  167. ZERO_NULL, /* disconnect */
  168. ZERO_NULL, /* readwrite */
  169. PORT_POP3, /* defport */
  170. CURLPROTO_HTTP, /* protocol */
  171. PROTOPT_NONE /* flags */
  172. };
  173. #ifdef USE_SSL
  174. /*
  175. * HTTP-proxyed POP3S protocol handler.
  176. */
  177. static const struct Curl_handler Curl_handler_pop3s_proxy = {
  178. "POP3S", /* scheme */
  179. ZERO_NULL, /* setup_connection */
  180. Curl_http, /* do_it */
  181. Curl_http_done, /* done */
  182. ZERO_NULL, /* do_more */
  183. ZERO_NULL, /* connect_it */
  184. ZERO_NULL, /* connecting */
  185. ZERO_NULL, /* doing */
  186. ZERO_NULL, /* proto_getsock */
  187. ZERO_NULL, /* doing_getsock */
  188. ZERO_NULL, /* domore_getsock */
  189. ZERO_NULL, /* perform_getsock */
  190. ZERO_NULL, /* disconnect */
  191. ZERO_NULL, /* readwrite */
  192. PORT_POP3S, /* defport */
  193. CURLPROTO_HTTP, /* protocol */
  194. PROTOPT_NONE /* flags */
  195. };
  196. #endif
  197. #endif
  198. /* Function that checks for an ending pop3 status code at the start of the
  199. given string, but also detects the APOP timestamp from the server greeting
  200. as well as the supported authentication types and allowed SASL mechanisms
  201. from the CAPA response. */
  202. static int pop3_endofresp(struct pingpong *pp, int *resp)
  203. {
  204. char *line = pp->linestart_resp;
  205. size_t len = strlen(pp->linestart_resp);
  206. struct connectdata *conn = pp->conn;
  207. struct pop3_conn *pop3c = &conn->proto.pop3c;
  208. size_t wordlen;
  209. size_t i;
  210. /* Do we have an error response? */
  211. if(len >= 4 && !memcmp("-ERR", line, 4)) {
  212. *resp = '-';
  213. return FALSE;
  214. }
  215. /* Are we processing servergreet responses */
  216. if(pop3c->state == POP3_SERVERGREET) {
  217. /* Look for the APOP timestamp */
  218. if(len >= 3 && line[len - 3] == '>') {
  219. for(i = 0; i < len - 3; ++i) {
  220. if(line[i] == '<') {
  221. /* Calculate the length of the timestamp */
  222. size_t timestamplen = len - 2 - i;
  223. /* Allocate some memory for the timestamp */
  224. pop3c->apoptimestamp = (char *)calloc(1, timestamplen + 1);
  225. if(!pop3c->apoptimestamp)
  226. break;
  227. /* Copy the timestamp */
  228. memcpy(pop3c->apoptimestamp, line + i, timestamplen);
  229. pop3c->apoptimestamp[timestamplen] = '\0';
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. /* Are we processing CAPA command responses? */
  236. else if(pop3c->state == POP3_CAPA) {
  237. /* Do we have the terminating character? */
  238. if(len >= 1 && !memcmp(line, ".", 1)) {
  239. *resp = '+';
  240. return TRUE;
  241. }
  242. /* Does the server support clear text? */
  243. if(len >= 4 && !memcmp(line, "USER", 4)) {
  244. pop3c->authtypes |= POP3_TYPE_CLEARTEXT;
  245. return FALSE;
  246. }
  247. /* Does the server support APOP? */
  248. if(len >= 4 && !memcmp(line, "APOP", 4)) {
  249. pop3c->authtypes |= POP3_TYPE_APOP;
  250. return FALSE;
  251. }
  252. /* Does the server support SASL? */
  253. if(len < 4 || memcmp(line, "SASL", 4))
  254. return FALSE;
  255. pop3c->authtypes |= POP3_TYPE_SASL;
  256. /* Advance past the SASL keyword */
  257. line += 4;
  258. len -= 4;
  259. /* Loop through the data line */
  260. for(;;) {
  261. while(len &&
  262. (*line == ' ' || *line == '\t' ||
  263. *line == '\r' || *line == '\n')) {
  264. if(*line == '\n')
  265. return FALSE;
  266. line++;
  267. len--;
  268. }
  269. if(!len)
  270. break;
  271. /* Extract the word */
  272. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  273. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  274. line[wordlen] != '\n';)
  275. wordlen++;
  276. /* Test the word for a matching authentication mechanism */
  277. if(wordlen == 5 && !memcmp(line, "LOGIN", 5))
  278. pop3c->authmechs |= SASL_MECH_LOGIN;
  279. else if(wordlen == 5 && !memcmp(line, "PLAIN", 5))
  280. pop3c->authmechs |= SASL_MECH_PLAIN;
  281. else if(wordlen == 8 && !memcmp(line, "CRAM-MD5", 8))
  282. pop3c->authmechs |= SASL_MECH_CRAM_MD5;
  283. else if(wordlen == 10 && !memcmp(line, "DIGEST-MD5", 10))
  284. pop3c->authmechs |= SASL_MECH_DIGEST_MD5;
  285. else if(wordlen == 6 && !memcmp(line, "GSSAPI", 6))
  286. pop3c->authmechs |= SASL_MECH_GSSAPI;
  287. else if(wordlen == 8 && !memcmp(line, "EXTERNAL", 8))
  288. pop3c->authmechs |= SASL_MECH_EXTERNAL;
  289. else if(wordlen == 4 && !memcmp(line, "NTLM", 4))
  290. pop3c->authmechs |= SASL_MECH_NTLM;
  291. line += wordlen;
  292. len -= wordlen;
  293. }
  294. }
  295. if((len < 1 || memcmp("+", line, 1)) &&
  296. (len < 3 || memcmp("+OK", line, 3)))
  297. return FALSE; /* Nothing for us */
  298. /* Otherwise it's a positive response */
  299. *resp = '+';
  300. return TRUE;
  301. }
  302. /* This is the ONLY way to change POP3 state! */
  303. static void state(struct connectdata *conn, pop3state newstate)
  304. {
  305. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  306. /* for debug purposes */
  307. static const char * const names[] = {
  308. "STOP",
  309. "SERVERGREET",
  310. "STARTTLS",
  311. "CAPA",
  312. "AUTH_PLAIN",
  313. "AUTH_LOGIN",
  314. "AUTH_LOGIN_PASSWD",
  315. "AUTH_CRAMMD5",
  316. "AUTH_DIGESTMD5",
  317. "AUTH_DIGESTMD5_RESP",
  318. "AUTH_NTLM",
  319. "AUTH_NTLM_TYPE2MSG",
  320. "AUTH",
  321. "APOP",
  322. "USER",
  323. "PASS",
  324. "COMMAND",
  325. "QUIT",
  326. /* LAST */
  327. };
  328. #endif
  329. struct pop3_conn *pop3c = &conn->proto.pop3c;
  330. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  331. if(pop3c->state != newstate)
  332. infof(conn->data, "POP3 %p state change from %s to %s\n",
  333. pop3c, names[pop3c->state], names[newstate]);
  334. #endif
  335. pop3c->state = newstate;
  336. }
  337. static CURLcode pop3_state_capa(struct connectdata *conn)
  338. {
  339. CURLcode result = CURLE_OK;
  340. struct pop3_conn *pop3c = &conn->proto.pop3c;
  341. pop3c->authmechs = 0; /* No known authentication mechanisms yet */
  342. pop3c->authused = 0; /* Clear the authentication mechanism used */
  343. /* Check we have a username and password to authenticate with and end the
  344. connect phase if we don't */
  345. if(!conn->bits.user_passwd) {
  346. state(conn, POP3_STOP);
  347. return result;
  348. }
  349. /* Send the CAPA command */
  350. result = Curl_pp_sendf(&pop3c->pp, "CAPA");
  351. if(result)
  352. return result;
  353. state(conn, POP3_CAPA);
  354. return CURLE_OK;
  355. }
  356. static CURLcode pop3_state_user(struct connectdata *conn)
  357. {
  358. CURLcode result;
  359. struct FTP *pop3 = conn->data->state.proto.pop3;
  360. /* Send the USER command */
  361. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "USER %s",
  362. pop3->user ? pop3->user : "");
  363. if(result)
  364. return result;
  365. state(conn, POP3_USER);
  366. return CURLE_OK;
  367. }
  368. static CURLcode pop3_state_apop(struct connectdata *conn)
  369. {
  370. CURLcode result = CURLE_OK;
  371. struct pop3_conn *pop3c = &conn->proto.pop3c;
  372. size_t i;
  373. MD5_context *ctxt;
  374. unsigned char digest[MD5_DIGEST_LEN];
  375. char secret[2 * MD5_DIGEST_LEN + 1];
  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. snprintf(&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. static CURLcode pop3_authenticate(struct connectdata *conn)
  394. {
  395. CURLcode result = CURLE_OK;
  396. struct pop3_conn *pop3c = &conn->proto.pop3c;
  397. const char *mech = NULL;
  398. pop3state authstate = POP3_STOP;
  399. /* Check supported authentication mechanisms by decreasing order of
  400. security */
  401. #ifndef CURL_DISABLE_CRYPTO_AUTH
  402. if(pop3c->authmechs & SASL_MECH_DIGEST_MD5) {
  403. mech = "DIGEST-MD5";
  404. authstate = POP3_AUTH_DIGESTMD5;
  405. pop3c->authused = SASL_MECH_DIGEST_MD5;
  406. }
  407. else if(pop3c->authmechs & SASL_MECH_CRAM_MD5) {
  408. mech = "CRAM-MD5";
  409. authstate = POP3_AUTH_CRAMMD5;
  410. pop3c->authused = SASL_MECH_CRAM_MD5;
  411. }
  412. else
  413. #endif
  414. #ifdef USE_NTLM
  415. if(pop3c->authmechs & SASL_MECH_NTLM) {
  416. mech = "NTLM";
  417. authstate = POP3_AUTH_NTLM;
  418. pop3c->authused = SASL_MECH_NTLM;
  419. }
  420. else
  421. #endif
  422. if(pop3c->authmechs & SASL_MECH_LOGIN) {
  423. mech = "LOGIN";
  424. authstate = POP3_AUTH_LOGIN;
  425. pop3c->authused = SASL_MECH_LOGIN;
  426. }
  427. else if(pop3c->authmechs & SASL_MECH_PLAIN) {
  428. mech = "PLAIN";
  429. authstate = POP3_AUTH_PLAIN;
  430. pop3c->authused = SASL_MECH_PLAIN;
  431. }
  432. else {
  433. infof(conn->data, "No known SASL authentication mechanisms supported!\n");
  434. result = CURLE_LOGIN_DENIED; /* Other mechanisms not supported */
  435. }
  436. if(!result) {
  437. result = Curl_pp_sendf(&pop3c->pp, "AUTH %s", mech);
  438. if(!result)
  439. state(conn, authstate);
  440. }
  441. return result;
  442. }
  443. /* For the POP3 "protocol connect" and "doing" phases only */
  444. static int pop3_getsock(struct connectdata *conn, curl_socket_t *socks,
  445. int numsocks)
  446. {
  447. return Curl_pp_getsock(&conn->proto.pop3c.pp, socks, numsocks);
  448. }
  449. #ifdef USE_SSL
  450. static void pop3_to_pop3s(struct connectdata *conn)
  451. {
  452. conn->handler = &Curl_handler_pop3s;
  453. }
  454. #else
  455. #define pop3_to_pop3s(x) Curl_nop_stmt
  456. #endif
  457. /* For the initial server greeting */
  458. static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
  459. int pop3code,
  460. pop3state instate)
  461. {
  462. CURLcode result = CURLE_OK;
  463. struct SessionHandle *data = conn->data;
  464. struct pop3_conn *pop3c = &conn->proto.pop3c;
  465. (void)instate; /* no use for this yet */
  466. if(pop3code != '+') {
  467. failf(data, "Got unexpected pop3-server response");
  468. return CURLE_FTP_WEIRD_SERVER_REPLY;
  469. }
  470. if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
  471. /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
  472. to TLS connection now */
  473. result = Curl_pp_sendf(&pop3c->pp, "STLS");
  474. state(conn, POP3_STARTTLS);
  475. }
  476. else
  477. result = pop3_state_capa(conn);
  478. return result;
  479. }
  480. /* For STARTTLS responses */
  481. static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
  482. int pop3code,
  483. pop3state instate)
  484. {
  485. CURLcode result = CURLE_OK;
  486. struct SessionHandle *data = conn->data;
  487. (void)instate; /* no use for this yet */
  488. if(pop3code != '+') {
  489. if(data->set.use_ssl != CURLUSESSL_TRY) {
  490. failf(data, "STARTTLS denied. %c", pop3code);
  491. result = CURLE_USE_SSL_FAILED;
  492. state(conn, POP3_STOP);
  493. }
  494. else
  495. result = pop3_state_capa(conn);
  496. }
  497. else {
  498. /* Curl_ssl_connect is BLOCKING */
  499. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  500. if(CURLE_OK == result) {
  501. pop3_to_pop3s(conn);
  502. result = pop3_state_capa(conn);
  503. }
  504. else {
  505. /* End of connect phase */
  506. state(conn, POP3_STOP);
  507. }
  508. }
  509. return result;
  510. }
  511. /* For CAPA responses */
  512. static CURLcode pop3_state_capa_resp(struct connectdata *conn,
  513. int pop3code,
  514. pop3state instate)
  515. {
  516. CURLcode result = CURLE_OK;
  517. (void)instate; /* no use for this yet */
  518. if(pop3code != '+')
  519. result = pop3_state_user(conn);
  520. else {
  521. /* Check supported authentication types by decreasing order of security */
  522. if(conn->proto.pop3c.authtypes & POP3_TYPE_SASL)
  523. result = pop3_authenticate(conn);
  524. else if(conn->proto.pop3c.authtypes & POP3_TYPE_APOP)
  525. result = pop3_state_apop(conn);
  526. else if(conn->proto.pop3c.authtypes & POP3_TYPE_CLEARTEXT)
  527. result = pop3_state_user(conn);
  528. else {
  529. infof(conn->data, "No known authentication types supported!\n");
  530. result = CURLE_LOGIN_DENIED; /* Other types not supported */
  531. }
  532. }
  533. return result;
  534. }
  535. /* For AUTH PLAIN responses */
  536. static CURLcode pop3_state_auth_plain_resp(struct connectdata *conn,
  537. int pop3code,
  538. pop3state instate)
  539. {
  540. CURLcode result = CURLE_OK;
  541. struct SessionHandle *data = conn->data;
  542. size_t len = 0;
  543. char *plainauth = NULL;
  544. (void)instate; /* no use for this yet */
  545. if(pop3code != '+') {
  546. failf(data, "Access denied. %c", pop3code);
  547. result = CURLE_LOGIN_DENIED;
  548. }
  549. else {
  550. /* Create the authorisation message */
  551. result = Curl_sasl_create_plain_message(data, conn->user, conn->passwd,
  552. &plainauth, &len);
  553. /* Send the message */
  554. if(!result) {
  555. if(plainauth) {
  556. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", plainauth);
  557. if(!result)
  558. state(conn, POP3_AUTH);
  559. }
  560. Curl_safefree(plainauth);
  561. }
  562. }
  563. return result;
  564. }
  565. /* For AUTH LOGIN responses */
  566. static CURLcode pop3_state_auth_login_resp(struct connectdata *conn,
  567. int pop3code,
  568. pop3state instate)
  569. {
  570. CURLcode result = CURLE_OK;
  571. struct SessionHandle *data = conn->data;
  572. size_t len = 0;
  573. char *authuser = NULL;
  574. (void)instate; /* no use for this yet */
  575. if(pop3code != '+') {
  576. failf(data, "Access denied: %d", pop3code);
  577. result = CURLE_LOGIN_DENIED;
  578. }
  579. else {
  580. /* Create the user message */
  581. result = Curl_sasl_create_login_message(data, conn->user,
  582. &authuser, &len);
  583. /* Send the user */
  584. if(!result) {
  585. if(authuser) {
  586. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", authuser);
  587. if(!result)
  588. state(conn, POP3_AUTH_LOGIN_PASSWD);
  589. }
  590. Curl_safefree(authuser);
  591. }
  592. }
  593. return result;
  594. }
  595. /* For AUTH LOGIN user entry responses */
  596. static CURLcode pop3_state_auth_login_password_resp(struct connectdata *conn,
  597. int pop3code,
  598. pop3state instate)
  599. {
  600. CURLcode result = CURLE_OK;
  601. struct SessionHandle *data = conn->data;
  602. size_t len = 0;
  603. char *authpasswd = NULL;
  604. (void)instate; /* no use for this yet */
  605. if(pop3code != '+') {
  606. failf(data, "Access denied: %d", pop3code);
  607. result = CURLE_LOGIN_DENIED;
  608. }
  609. else {
  610. /* Create the password message */
  611. result = Curl_sasl_create_login_message(data, conn->passwd,
  612. &authpasswd, &len);
  613. /* Send the password */
  614. if(!result) {
  615. if(authpasswd) {
  616. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", authpasswd);
  617. if(!result)
  618. state(conn, POP3_AUTH);
  619. }
  620. Curl_safefree(authpasswd);
  621. }
  622. }
  623. return result;
  624. }
  625. #ifndef CURL_DISABLE_CRYPTO_AUTH
  626. /* For AUTH CRAM-MD5 responses */
  627. static CURLcode pop3_state_auth_cram_resp(struct connectdata *conn,
  628. int pop3code,
  629. pop3state instate)
  630. {
  631. CURLcode result = CURLE_OK;
  632. struct SessionHandle *data = conn->data;
  633. char *chlg64 = data->state.buffer;
  634. size_t len = 0;
  635. char *rplyb64 = NULL;
  636. (void)instate; /* no use for this yet */
  637. if(pop3code != '+') {
  638. failf(data, "Access denied: %d", pop3code);
  639. return CURLE_LOGIN_DENIED;
  640. }
  641. /* Get the challenge */
  642. for(chlg64 += 2; *chlg64 == ' ' || *chlg64 == '\t'; chlg64++)
  643. ;
  644. /* Terminate the challenge */
  645. if(*chlg64 != '=') {
  646. for(len = strlen(chlg64); len--;)
  647. if(chlg64[len] != '\r' && chlg64[len] != '\n' && chlg64[len] != ' ' &&
  648. chlg64[len] != '\t')
  649. break;
  650. if(++len) {
  651. chlg64[len] = '\0';
  652. }
  653. }
  654. /* Create the response message */
  655. result = Curl_sasl_create_cram_md5_message(data, chlg64, conn->user,
  656. conn->passwd, &rplyb64, &len);
  657. /* Send the response */
  658. if(!result) {
  659. if(rplyb64) {
  660. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", rplyb64);
  661. if(!result)
  662. state(conn, POP3_AUTH);
  663. }
  664. Curl_safefree(rplyb64);
  665. }
  666. return result;
  667. }
  668. /* For AUTH DIGEST-MD5 challenge responses */
  669. static CURLcode pop3_state_auth_digest_resp(struct connectdata *conn,
  670. int pop3code,
  671. pop3state instate)
  672. {
  673. CURLcode result = CURLE_OK;
  674. struct SessionHandle *data = conn->data;
  675. char *chlg64 = data->state.buffer;
  676. size_t len = 0;
  677. char *rplyb64 = NULL;
  678. (void)instate; /* no use for this yet */
  679. if(pop3code != '+') {
  680. failf(data, "Access denied: %d", pop3code);
  681. return CURLE_LOGIN_DENIED;
  682. }
  683. /* Get the challenge */
  684. for(chlg64 += 2; *chlg64 == ' ' || *chlg64 == '\t'; chlg64++)
  685. ;
  686. /* Create the response message */
  687. result = Curl_sasl_create_digest_md5_message(data, chlg64, conn->user,
  688. conn->passwd, "pop",
  689. &rplyb64, &len);
  690. /* Send the response */
  691. if(!result) {
  692. if(rplyb64) {
  693. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", rplyb64);
  694. if(!result)
  695. state(conn, POP3_AUTH_DIGESTMD5_RESP);
  696. }
  697. Curl_safefree(rplyb64);
  698. }
  699. return result;
  700. }
  701. /* For AUTH DIGEST-MD5 challenge-response responses */
  702. static CURLcode pop3_state_auth_digest_resp_resp(struct connectdata *conn,
  703. int pop3code,
  704. pop3state instate)
  705. {
  706. CURLcode result = CURLE_OK;
  707. struct SessionHandle *data = conn->data;
  708. (void)instate; /* no use for this yet */
  709. if(pop3code != '+') {
  710. failf(data, "Authentication failed: %d", pop3code);
  711. result = CURLE_LOGIN_DENIED;
  712. }
  713. else {
  714. /* Send an empty response */
  715. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "");
  716. if(!result)
  717. state(conn, POP3_AUTH);
  718. }
  719. return result;
  720. }
  721. #endif
  722. #ifdef USE_NTLM
  723. /* For AUTH NTLM responses */
  724. static CURLcode pop3_state_auth_ntlm_resp(struct connectdata *conn,
  725. int pop3code,
  726. pop3state instate)
  727. {
  728. CURLcode result = CURLE_OK;
  729. struct SessionHandle *data = conn->data;
  730. size_t len = 0;
  731. char *type1msg = NULL;
  732. (void)instate; /* no use for this yet */
  733. if(pop3code != '+') {
  734. failf(data, "Access denied: %d", pop3code);
  735. result = CURLE_LOGIN_DENIED;
  736. }
  737. else {
  738. /* Create the type-1 message */
  739. result = Curl_sasl_create_ntlm_type1_message(conn->user, conn->passwd,
  740. &conn->ntlm,
  741. &type1msg, &len);
  742. /* Send the message */
  743. if(!result) {
  744. if(type1msg) {
  745. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", type1msg);
  746. if(!result)
  747. state(conn, POP3_AUTH_NTLM_TYPE2MSG);
  748. }
  749. Curl_safefree(type1msg);
  750. }
  751. }
  752. return result;
  753. }
  754. /* For NTLM type-2 responses (sent in reponse to our type-1 message) */
  755. static CURLcode pop3_state_auth_ntlm_type2msg_resp(struct connectdata *conn,
  756. int pop3code,
  757. pop3state instate)
  758. {
  759. CURLcode result = CURLE_OK;
  760. struct SessionHandle *data = conn->data;
  761. size_t len = 0;
  762. char *type3msg = NULL;
  763. (void)instate; /* no use for this yet */
  764. if(pop3code != '+') {
  765. failf(data, "Access denied: %d", pop3code);
  766. result = CURLE_LOGIN_DENIED;
  767. }
  768. else {
  769. /* Create the type-3 message */
  770. result = Curl_sasl_create_ntlm_type3_message(data,
  771. data->state.buffer + 2,
  772. conn->user, conn->passwd,
  773. &conn->ntlm,
  774. &type3msg, &len);
  775. /* Send the message */
  776. if(!result) {
  777. if(type3msg) {
  778. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s", type3msg);
  779. if(!result)
  780. state(conn, POP3_AUTH);
  781. }
  782. Curl_safefree(type3msg);
  783. }
  784. }
  785. return result;
  786. }
  787. #endif
  788. /* For final responses to the AUTH sequence */
  789. static CURLcode pop3_state_auth_final_resp(struct connectdata *conn,
  790. int pop3code,
  791. pop3state instate)
  792. {
  793. CURLcode result = CURLE_OK;
  794. struct SessionHandle *data = conn->data;
  795. (void)instate; /* no use for this yet */
  796. if(pop3code != '+') {
  797. failf(data, "Authentication failed: %d", pop3code);
  798. result = CURLE_LOGIN_DENIED;
  799. }
  800. /* End of connect phase */
  801. state(conn, POP3_STOP);
  802. return result;
  803. }
  804. static CURLcode pop3_state_apop_resp(struct connectdata *conn,
  805. int pop3code,
  806. pop3state instate)
  807. {
  808. CURLcode result = CURLE_OK;
  809. struct SessionHandle *data = conn->data;
  810. (void)instate; /* no use for this yet */
  811. if(pop3code != '+') {
  812. failf(data, "Authentication failed: %d", pop3code);
  813. result = CURLE_LOGIN_DENIED;
  814. }
  815. /* End of connect phase */
  816. state(conn, POP3_STOP);
  817. return result;
  818. }
  819. /* For USER responses */
  820. static CURLcode pop3_state_user_resp(struct connectdata *conn,
  821. int pop3code,
  822. pop3state instate)
  823. {
  824. CURLcode result = CURLE_OK;
  825. struct SessionHandle *data = conn->data;
  826. struct FTP *pop3 = data->state.proto.pop3;
  827. (void)instate; /* no use for this yet */
  828. if(pop3code != '+') {
  829. failf(data, "Access denied. %c", pop3code);
  830. result = CURLE_LOGIN_DENIED;
  831. }
  832. else
  833. /* Send the PASS command */
  834. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "PASS %s",
  835. pop3->passwd ? pop3->passwd : "");
  836. if(result)
  837. return result;
  838. state(conn, POP3_PASS);
  839. return result;
  840. }
  841. /* For PASS responses */
  842. static CURLcode pop3_state_pass_resp(struct connectdata *conn,
  843. int pop3code,
  844. pop3state instate)
  845. {
  846. CURLcode result = CURLE_OK;
  847. struct SessionHandle *data = conn->data;
  848. (void)instate; /* no use for this yet */
  849. if(pop3code != '+') {
  850. failf(data, "Access denied. %c", pop3code);
  851. result = CURLE_LOGIN_DENIED;
  852. }
  853. /* End of connect phase */
  854. state(conn, POP3_STOP);
  855. return result;
  856. }
  857. /* For command responses */
  858. static CURLcode pop3_state_command_resp(struct connectdata *conn,
  859. int pop3code,
  860. pop3state instate)
  861. {
  862. CURLcode result = CURLE_OK;
  863. struct SessionHandle *data = conn->data;
  864. struct FTP *pop3 = data->state.proto.pop3;
  865. struct pop3_conn *pop3c = &conn->proto.pop3c;
  866. struct pingpong *pp = &pop3c->pp;
  867. (void)instate; /* no use for this yet */
  868. if(pop3code != '+') {
  869. state(conn, POP3_STOP);
  870. return CURLE_RECV_ERROR;
  871. }
  872. /* This 'OK' line ends with a CR LF pair which is the two first bytes of the
  873. EOB string so count this is two matching bytes. This is necessary to make
  874. the code detect the EOB if the only data than comes now is %2e CR LF like
  875. when there is no body to return. */
  876. pop3c->eob = 2;
  877. /* But since this initial CR LF pair is not part of the actual body, we set
  878. the strip counter here so that these bytes won't be delivered. */
  879. pop3c->strip = 2;
  880. /* POP3 download */
  881. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, pop3->bytecountp,
  882. -1, NULL); /* no upload here */
  883. if(pp->cache) {
  884. /* The header "cache" contains a bunch of data that is actually body
  885. content so send it as such. Note that there may even be additional
  886. "headers" after the body */
  887. if(!data->set.opt_no_body) {
  888. result = Curl_pop3_write(conn, pp->cache, pp->cache_size);
  889. if(result)
  890. return result;
  891. }
  892. /* Free the cache */
  893. Curl_safefree(pp->cache);
  894. /* Reset the cache size */
  895. pp->cache_size = 0;
  896. }
  897. /* End of do phase */
  898. state(conn, POP3_STOP);
  899. return result;
  900. }
  901. /* Start the DO phase for the command */
  902. static CURLcode pop3_command(struct connectdata *conn)
  903. {
  904. CURLcode result = CURLE_OK;
  905. struct pop3_conn *pop3c = &conn->proto.pop3c;
  906. const char *command = NULL;
  907. /* Calculate the default command */
  908. if(pop3c->mailbox[0] == '\0' || conn->data->set.ftp_list_only) {
  909. command = "LIST";
  910. if(pop3c->mailbox[0] != '\0') {
  911. /* Message specific LIST so skip the BODY transfer */
  912. struct FTP *pop3 = conn->data->state.proto.pop3;
  913. pop3->transfer = FTPTRANSFER_INFO;
  914. }
  915. }
  916. else
  917. command = "RETR";
  918. /* Send the command */
  919. if(pop3c->mailbox[0] != '\0')
  920. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "%s %s",
  921. (pop3c->custom && pop3c->custom[0] != '\0' ?
  922. pop3c->custom : command), pop3c->mailbox);
  923. else
  924. result = Curl_pp_sendf(&conn->proto.pop3c.pp,
  925. (pop3c->custom && pop3c->custom[0] != '\0' ?
  926. pop3c->custom : command));
  927. if(result)
  928. return result;
  929. state(conn, POP3_COMMAND);
  930. return result;
  931. }
  932. static CURLcode pop3_statemach_act(struct connectdata *conn)
  933. {
  934. CURLcode result;
  935. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  936. int pop3code;
  937. struct pop3_conn *pop3c = &conn->proto.pop3c;
  938. struct pingpong *pp = &pop3c->pp;
  939. size_t nread = 0;
  940. /* Flush any data that needs to be sent */
  941. if(pp->sendleft)
  942. return Curl_pp_flushsend(pp);
  943. /* Read the response from the server */
  944. result = Curl_pp_readresp(sock, pp, &pop3code, &nread);
  945. if(result)
  946. return result;
  947. if(pop3code) {
  948. /* We have now received a full POP3 server response */
  949. switch(pop3c->state) {
  950. case POP3_SERVERGREET:
  951. result = pop3_state_servergreet_resp(conn, pop3code, pop3c->state);
  952. break;
  953. case POP3_STARTTLS:
  954. result = pop3_state_starttls_resp(conn, pop3code, pop3c->state);
  955. break;
  956. case POP3_CAPA:
  957. result = pop3_state_capa_resp(conn, pop3code, pop3c->state);
  958. break;
  959. case POP3_AUTH_PLAIN:
  960. result = pop3_state_auth_plain_resp(conn, pop3code, pop3c->state);
  961. break;
  962. case POP3_AUTH_LOGIN:
  963. result = pop3_state_auth_login_resp(conn, pop3code, pop3c->state);
  964. break;
  965. case POP3_AUTH_LOGIN_PASSWD:
  966. result = pop3_state_auth_login_password_resp(conn, pop3code,
  967. pop3c->state);
  968. break;
  969. #ifndef CURL_DISABLE_CRYPTO_AUTH
  970. case POP3_AUTH_CRAMMD5:
  971. result = pop3_state_auth_cram_resp(conn, pop3code, pop3c->state);
  972. break;
  973. case POP3_AUTH_DIGESTMD5:
  974. result = pop3_state_auth_digest_resp(conn, pop3code, pop3c->state);
  975. break;
  976. case POP3_AUTH_DIGESTMD5_RESP:
  977. result = pop3_state_auth_digest_resp_resp(conn, pop3code, pop3c->state);
  978. break;
  979. #endif
  980. #ifdef USE_NTLM
  981. case POP3_AUTH_NTLM:
  982. result = pop3_state_auth_ntlm_resp(conn, pop3code, pop3c->state);
  983. break;
  984. case POP3_AUTH_NTLM_TYPE2MSG:
  985. result = pop3_state_auth_ntlm_type2msg_resp(conn, pop3code,
  986. pop3c->state);
  987. break;
  988. #endif
  989. case POP3_AUTH:
  990. result = pop3_state_auth_final_resp(conn, pop3code, pop3c->state);
  991. break;
  992. case POP3_APOP:
  993. result = pop3_state_apop_resp(conn, pop3code, pop3c->state);
  994. break;
  995. case POP3_USER:
  996. result = pop3_state_user_resp(conn, pop3code, pop3c->state);
  997. break;
  998. case POP3_PASS:
  999. result = pop3_state_pass_resp(conn, pop3code, pop3c->state);
  1000. break;
  1001. case POP3_COMMAND:
  1002. result = pop3_state_command_resp(conn, pop3code, pop3c->state);
  1003. break;
  1004. case POP3_QUIT:
  1005. /* fallthrough, just stop! */
  1006. default:
  1007. /* internal error */
  1008. state(conn, POP3_STOP);
  1009. break;
  1010. }
  1011. }
  1012. return result;
  1013. }
  1014. /* Called repeatedly until done from multi.c */
  1015. static CURLcode pop3_multi_statemach(struct connectdata *conn, bool *done)
  1016. {
  1017. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1018. CURLcode result = Curl_pp_multi_statemach(&pop3c->pp);
  1019. *done = (pop3c->state == POP3_STOP) ? TRUE : FALSE;
  1020. return result;
  1021. }
  1022. static CURLcode pop3_easy_statemach(struct connectdata *conn)
  1023. {
  1024. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1025. struct pingpong *pp = &pop3c->pp;
  1026. CURLcode result = CURLE_OK;
  1027. while(pop3c->state != POP3_STOP) {
  1028. result = Curl_pp_easy_statemach(pp);
  1029. if(result)
  1030. break;
  1031. }
  1032. return result;
  1033. }
  1034. /* Allocate and initialize the POP3 struct for the current SessionHandle if
  1035. required */
  1036. static CURLcode pop3_init(struct connectdata *conn)
  1037. {
  1038. struct SessionHandle *data = conn->data;
  1039. struct FTP *pop3 = data->state.proto.pop3;
  1040. if(!pop3) {
  1041. pop3 = data->state.proto.pop3 = calloc(sizeof(struct FTP), 1);
  1042. if(!pop3)
  1043. return CURLE_OUT_OF_MEMORY;
  1044. }
  1045. /* Get some initial data into the pop3 struct */
  1046. pop3->bytecountp = &data->req.bytecount;
  1047. /* No need to duplicate user+password, the connectdata struct won't change
  1048. during a session, but we re-init them here since on subsequent inits
  1049. since the conn struct may have changed or been replaced.
  1050. */
  1051. pop3->user = conn->user;
  1052. pop3->passwd = conn->passwd;
  1053. return CURLE_OK;
  1054. }
  1055. /***********************************************************************
  1056. *
  1057. * pop3_connect()
  1058. *
  1059. * This function should do everything that is to be considered a part of the
  1060. * connection phase.
  1061. *
  1062. * The variable 'done' points to will be TRUE if the protocol-layer connect
  1063. * phase is done when this function returns, or FALSE is not. When called as
  1064. * a part of the easy interface, it will always be TRUE.
  1065. */
  1066. static CURLcode pop3_connect(struct connectdata *conn, bool *done)
  1067. {
  1068. CURLcode result;
  1069. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1070. struct SessionHandle *data = conn->data;
  1071. struct pingpong *pp = &pop3c->pp;
  1072. *done = FALSE; /* default to not done yet */
  1073. /* If there already is a protocol-specific struct allocated for this
  1074. sessionhandle, deal with it */
  1075. Curl_reset_reqproto(conn);
  1076. result = pop3_init(conn);
  1077. if(CURLE_OK != result)
  1078. return result;
  1079. /* We always support persistent connections on pop3 */
  1080. conn->bits.close = FALSE;
  1081. pp->response_time = RESP_TIMEOUT; /* set default response time-out */
  1082. pp->statemach_act = pop3_statemach_act;
  1083. pp->endofresp = pop3_endofresp;
  1084. pp->conn = conn;
  1085. if(conn->handler->flags & PROTOPT_SSL) {
  1086. /* BLOCKING */
  1087. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  1088. if(result)
  1089. return result;
  1090. }
  1091. Curl_pp_init(pp); /* init the response reader stuff */
  1092. /* When we connect, we start in the state where we await the server greet
  1093. response */
  1094. state(conn, POP3_SERVERGREET);
  1095. if(data->state.used_interface == Curl_if_multi)
  1096. result = pop3_multi_statemach(conn, done);
  1097. else {
  1098. result = pop3_easy_statemach(conn);
  1099. if(!result)
  1100. *done = TRUE;
  1101. }
  1102. return result;
  1103. }
  1104. /***********************************************************************
  1105. *
  1106. * pop3_done()
  1107. *
  1108. * The DONE function. This does what needs to be done after a single DO has
  1109. * performed.
  1110. *
  1111. * Input argument is already checked for validity.
  1112. */
  1113. static CURLcode pop3_done(struct connectdata *conn, CURLcode status,
  1114. bool premature)
  1115. {
  1116. struct SessionHandle *data = conn->data;
  1117. struct FTP *pop3 = data->state.proto.pop3;
  1118. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1119. CURLcode result = CURLE_OK;
  1120. (void)premature;
  1121. if(!pop3)
  1122. /* When the easy handle is removed from the multi while libcurl is still
  1123. * trying to resolve the host name, it seems that the pop3 struct is not
  1124. * yet initialized, but the removal action calls Curl_done() which calls
  1125. * this function. So we simply return success if no pop3 pointer is set.
  1126. */
  1127. return CURLE_OK;
  1128. if(status) {
  1129. conn->bits.close = TRUE; /* marked for closure */
  1130. result = status; /* use the already set error code */
  1131. }
  1132. /* Cleanup our do based variables */
  1133. Curl_safefree(pop3c->mailbox);
  1134. Curl_safefree(pop3c->custom);
  1135. /* Clear the transfer mode for the next connection */
  1136. pop3->transfer = FTPTRANSFER_BODY;
  1137. return result;
  1138. }
  1139. /***********************************************************************
  1140. *
  1141. * pop3_perform()
  1142. *
  1143. * This is the actual DO function for POP3. Get a file/directory according to
  1144. * the options previously setup.
  1145. */
  1146. static CURLcode pop3_perform(struct connectdata *conn, bool *connected,
  1147. bool *dophase_done)
  1148. {
  1149. /* This is POP3 and no proxy */
  1150. CURLcode result = CURLE_OK;
  1151. DEBUGF(infof(conn->data, "DO phase starts\n"));
  1152. if(conn->data->set.opt_no_body) {
  1153. /* Requested no body means no transfer */
  1154. struct FTP *pop3 = conn->data->state.proto.pop3;
  1155. pop3->transfer = FTPTRANSFER_INFO;
  1156. }
  1157. *dophase_done = FALSE; /* not done yet */
  1158. /* Start the first command in the DO phase */
  1159. result = pop3_command(conn);
  1160. if(result)
  1161. return result;
  1162. /* Run the state-machine */
  1163. if(conn->data->state.used_interface == Curl_if_multi)
  1164. result = pop3_multi_statemach(conn, dophase_done);
  1165. else {
  1166. result = pop3_easy_statemach(conn);
  1167. *dophase_done = TRUE; /* with the easy interface we are done here */
  1168. }
  1169. *connected = conn->bits.tcpconnect[FIRSTSOCKET];
  1170. if(*dophase_done)
  1171. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1172. return result;
  1173. }
  1174. /***********************************************************************
  1175. *
  1176. * pop3_do()
  1177. *
  1178. * This function is registered as 'curl_do' function. It decodes the path
  1179. * parts etc as a wrapper to the actual DO function (pop3_perform).
  1180. *
  1181. * The input argument is already checked for validity.
  1182. */
  1183. static CURLcode pop3_do(struct connectdata *conn, bool *done)
  1184. {
  1185. CURLcode retcode = CURLE_OK;
  1186. *done = FALSE; /* default to false */
  1187. /*
  1188. Since connections can be re-used between SessionHandles, this might be a
  1189. connection already existing but on a fresh SessionHandle struct so we must
  1190. make sure we have a good 'struct POP3' to play with. For new connections,
  1191. the struct POP3 is allocated and setup in the pop3_connect() function.
  1192. */
  1193. Curl_reset_reqproto(conn);
  1194. retcode = pop3_init(conn);
  1195. if(retcode)
  1196. return retcode;
  1197. /* Parse the URL path */
  1198. retcode = pop3_parse_url_path(conn);
  1199. if(retcode)
  1200. return retcode;
  1201. /* Parse the custom request */
  1202. retcode = pop3_parse_custom_request(conn);
  1203. if(retcode)
  1204. return retcode;
  1205. retcode = pop3_regular_transfer(conn, done);
  1206. return retcode;
  1207. }
  1208. /***********************************************************************
  1209. *
  1210. * pop3_quit()
  1211. *
  1212. * This should be called before calling sclose(). We should then wait for the
  1213. * response from the server before returning. The calling code should then try
  1214. * to close the connection.
  1215. */
  1216. static CURLcode pop3_quit(struct connectdata *conn)
  1217. {
  1218. CURLcode result = CURLE_OK;
  1219. result = Curl_pp_sendf(&conn->proto.pop3c.pp, "QUIT", NULL);
  1220. if(result)
  1221. return result;
  1222. state(conn, POP3_QUIT);
  1223. result = pop3_easy_statemach(conn);
  1224. return result;
  1225. }
  1226. /***********************************************************************
  1227. *
  1228. * pop3_disconnect()
  1229. *
  1230. * Disconnect from an POP3 server. Cleanup protocol-specific per-connection
  1231. * resources. BLOCKING.
  1232. */
  1233. static CURLcode pop3_disconnect(struct connectdata *conn,
  1234. bool dead_connection)
  1235. {
  1236. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1237. /* We cannot send quit unconditionally. If this connection is stale or
  1238. bad in any way, sending quit and waiting around here will make the
  1239. disconnect wait in vain and cause more problems than we need to */
  1240. /* The POP3 session may or may not have been allocated/setup at this
  1241. point! */
  1242. if(!dead_connection && pop3c->pp.conn)
  1243. (void)pop3_quit(conn); /* ignore errors on the LOGOUT */
  1244. /* Disconnect from the server */
  1245. Curl_pp_disconnect(&pop3c->pp);
  1246. /* Cleanup the SASL module */
  1247. Curl_sasl_cleanup(conn, pop3c->authused);
  1248. /* Cleanup our connection based variables */
  1249. Curl_safefree(pop3c->apoptimestamp);
  1250. return CURLE_OK;
  1251. }
  1252. /***********************************************************************
  1253. *
  1254. * pop3_parse_url_path()
  1255. *
  1256. * Parse the URL path into separate path components.
  1257. */
  1258. static CURLcode pop3_parse_url_path(struct connectdata *conn)
  1259. {
  1260. /* The POP3 struct is already initialised in pop3_connect() */
  1261. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1262. struct SessionHandle *data = conn->data;
  1263. const char *path = data->state.path;
  1264. /* URL decode the path and use this mailbox */
  1265. return Curl_urldecode(data, path, 0, &pop3c->mailbox, NULL, TRUE);
  1266. }
  1267. static CURLcode pop3_parse_custom_request(struct connectdata *conn)
  1268. {
  1269. CURLcode result = CURLE_OK;
  1270. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1271. struct SessionHandle *data = conn->data;
  1272. const char *custom = conn->data->set.str[STRING_CUSTOMREQUEST];
  1273. /* URL decode the custom request */
  1274. if(custom)
  1275. result = Curl_urldecode(data, custom, 0, &pop3c->custom, NULL, TRUE);
  1276. return result;
  1277. }
  1278. /* Call this when the DO phase has completed */
  1279. static CURLcode pop3_dophase_done(struct connectdata *conn, bool connected)
  1280. {
  1281. struct FTP *pop3 = conn->data->state.proto.pop3;
  1282. (void)connected;
  1283. if(pop3->transfer != FTPTRANSFER_BODY)
  1284. /* no data to transfer */
  1285. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1286. return CURLE_OK;
  1287. }
  1288. /* Called from multi.c while DOing */
  1289. static CURLcode pop3_doing(struct connectdata *conn, bool *dophase_done)
  1290. {
  1291. CURLcode result;
  1292. result = pop3_multi_statemach(conn, dophase_done);
  1293. if(*dophase_done) {
  1294. result = pop3_dophase_done(conn, FALSE /* not connected */);
  1295. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1296. }
  1297. return result;
  1298. }
  1299. /***********************************************************************
  1300. *
  1301. * pop3_regular_transfer()
  1302. *
  1303. * The input argument is already checked for validity.
  1304. *
  1305. * Performs all commands done before a regular transfer between a local and a
  1306. * remote host.
  1307. */
  1308. static CURLcode pop3_regular_transfer(struct connectdata *conn,
  1309. bool *dophase_done)
  1310. {
  1311. CURLcode result = CURLE_OK;
  1312. bool connected = FALSE;
  1313. struct SessionHandle *data = conn->data;
  1314. /* Make sure size is unknown at this point */
  1315. data->req.size = -1;
  1316. Curl_pgrsSetUploadCounter(data, 0);
  1317. Curl_pgrsSetDownloadCounter(data, 0);
  1318. Curl_pgrsSetUploadSize(data, 0);
  1319. Curl_pgrsSetDownloadSize(data, 0);
  1320. result = pop3_perform(conn, &connected, dophase_done);
  1321. if(CURLE_OK == result) {
  1322. if(!*dophase_done)
  1323. /* The DO phase has not completed yet */
  1324. return CURLE_OK;
  1325. result = pop3_dophase_done(conn, connected);
  1326. if(result)
  1327. return result;
  1328. }
  1329. return result;
  1330. }
  1331. static CURLcode pop3_setup_connection(struct connectdata * conn)
  1332. {
  1333. struct SessionHandle *data = conn->data;
  1334. if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
  1335. /* Unless we have asked to tunnel pop3 operations through the proxy, we
  1336. switch and use HTTP operations only */
  1337. #ifndef CURL_DISABLE_HTTP
  1338. if(conn->handler == &Curl_handler_pop3)
  1339. conn->handler = &Curl_handler_pop3_proxy;
  1340. else {
  1341. #ifdef USE_SSL
  1342. conn->handler = &Curl_handler_pop3s_proxy;
  1343. #else
  1344. failf(data, "POP3S not supported!");
  1345. return CURLE_UNSUPPORTED_PROTOCOL;
  1346. #endif
  1347. }
  1348. /* We explicitly mark this connection as persistent here as we're doing
  1349. POP3 over HTTP and thus we accidentally avoid setting this value
  1350. otherwise */
  1351. conn->bits.close = FALSE;
  1352. #else
  1353. failf(data, "POP3 over http proxy requires HTTP support built-in!");
  1354. return CURLE_UNSUPPORTED_PROTOCOL;
  1355. #endif
  1356. }
  1357. data->state.path++; /* don't include the initial slash */
  1358. return CURLE_OK;
  1359. }
  1360. /* This function scans the body after the end-of-body and writes everything
  1361. until the end is found */
  1362. CURLcode Curl_pop3_write(struct connectdata *conn, char *str, size_t nread)
  1363. {
  1364. /* This code could be made into a special function in the handler struct */
  1365. CURLcode result = CURLE_OK;
  1366. struct SessionHandle *data = conn->data;
  1367. struct SingleRequest *k = &data->req;
  1368. struct pop3_conn *pop3c = &conn->proto.pop3c;
  1369. bool strip_dot = FALSE;
  1370. size_t last = 0;
  1371. size_t i;
  1372. /* Search through the buffer looking for the end-of-body marker which is
  1373. 5 bytes (0d 0a 2e 0d 0a). Note that a line starting with a dot matches
  1374. the eob so the server will have prefixed it with an extra dot which we
  1375. need to strip out. Additionally the marker could of course be spread out
  1376. over 5 different data chunks */
  1377. for(i = 0; i < nread; i++) {
  1378. size_t prev = pop3c->eob;
  1379. switch(str[i]) {
  1380. case 0x0d:
  1381. if(pop3c->eob == 0) {
  1382. pop3c->eob++;
  1383. if(i) {
  1384. /* Write out the body part that didn't match */
  1385. result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
  1386. i - last);
  1387. if(result)
  1388. return result;
  1389. last = i;
  1390. }
  1391. }
  1392. else if(pop3c->eob == 3)
  1393. pop3c->eob++;
  1394. else
  1395. /* If the character match wasn't at position 0 or 3 then restart the
  1396. pattern matching */
  1397. pop3c->eob = 1;
  1398. break;
  1399. case 0x0a:
  1400. if(pop3c->eob == 1 || pop3c->eob == 4)
  1401. pop3c->eob++;
  1402. else
  1403. /* If the character match wasn't at position 1 or 4 then start the
  1404. search again */
  1405. pop3c->eob = 0;
  1406. break;
  1407. case 0x2e:
  1408. if(pop3c->eob == 2)
  1409. pop3c->eob++;
  1410. else if(pop3c->eob == 3) {
  1411. /* We have an extra dot after the CRLF which we need to strip off */
  1412. strip_dot = TRUE;
  1413. pop3c->eob = 0;
  1414. }
  1415. else
  1416. /* If the character match wasn't at position 2 then start the search
  1417. again */
  1418. pop3c->eob = 0;
  1419. break;
  1420. default:
  1421. pop3c->eob = 0;
  1422. break;
  1423. }
  1424. /* Did we have a partial match which has subsequently failed? */
  1425. if(prev && prev >= pop3c->eob) {
  1426. /* Strip can only be non-zero for the very first mismatch after CRLF
  1427. and then both prev and strip are equal and nothing will be output
  1428. below */
  1429. while(prev && pop3c->strip) {
  1430. prev--;
  1431. pop3c->strip--;
  1432. }
  1433. if(prev) {
  1434. /* If the partial match was the CRLF and dot then only write the CRLF
  1435. as the server would have inserted the dot */
  1436. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char*)POP3_EOB,
  1437. strip_dot ? prev - 1 : prev);
  1438. if(result)
  1439. return result;
  1440. last = i;
  1441. strip_dot = FALSE;
  1442. }
  1443. }
  1444. }
  1445. if(pop3c->eob == POP3_EOB_LEN) {
  1446. /* We have a full match so the transfer is done, however we must transfer
  1447. the CRLF at the start of the EOB as this is considered to be part of the
  1448. message as per RFC-1939, sect. 3 */
  1449. result = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)POP3_EOB, 2);
  1450. k->keepon &= ~KEEP_RECV;
  1451. pop3c->eob = 0;
  1452. return result;
  1453. }
  1454. if(pop3c->eob)
  1455. /* While EOB is matching nothing should be output */
  1456. return CURLE_OK;
  1457. if(nread - last) {
  1458. result = Curl_client_write(conn, CLIENTWRITE_BODY, &str[last],
  1459. nread - last);
  1460. }
  1461. return result;
  1462. }
  1463. #endif /* CURL_DISABLE_POP3 */