smtp.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * RFC2821 SMTP protocol
  22. * RFC3207 SMTP over TLS
  23. * RFC4954 SMTP Authentication
  24. * RFC2195 CRAM-MD5 authentication
  25. * RFC4616 PLAIN authentication
  26. *
  27. ***************************************************************************/
  28. #include "setup.h"
  29. #ifndef CURL_DISABLE_SMTP
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <stdarg.h>
  34. #include <ctype.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #ifdef HAVE_NETINET_IN_H
  42. #include <netinet/in.h>
  43. #endif
  44. #ifdef HAVE_ARPA_INET_H
  45. #include <arpa/inet.h>
  46. #endif
  47. #ifdef HAVE_UTSNAME_H
  48. #include <sys/utsname.h>
  49. #endif
  50. #ifdef HAVE_NETDB_H
  51. #include <netdb.h>
  52. #endif
  53. #ifdef __VMS
  54. #include <in.h>
  55. #include <inet.h>
  56. #endif
  57. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  58. #undef in_addr_t
  59. #define in_addr_t unsigned long
  60. #endif
  61. #include <curl/curl.h>
  62. #include "urldata.h"
  63. #include "sendf.h"
  64. #include "easyif.h" /* for Curl_convert_... prototypes */
  65. #include "if2ip.h"
  66. #include "hostip.h"
  67. #include "progress.h"
  68. #include "transfer.h"
  69. #include "escape.h"
  70. #include "http.h" /* for HTTP proxy tunnel stuff */
  71. #include "socks.h"
  72. #include "smtp.h"
  73. #include "strtoofft.h"
  74. #include "strequal.h"
  75. #include "sslgen.h"
  76. #include "connect.h"
  77. #include "strerror.h"
  78. #include "select.h"
  79. #include "multiif.h"
  80. #include "url.h"
  81. #include "rawstr.h"
  82. #include "strtoofft.h"
  83. #include "curl_base64.h"
  84. #include "curl_md5.h"
  85. #include "curl_hmac.h"
  86. #include "curl_gethostname.h"
  87. #include "warnless.h"
  88. #define _MPRINTF_REPLACE /* use our functions only */
  89. #include <curl/mprintf.h>
  90. #include "curl_memory.h"
  91. /* The last #include file should be: */
  92. #include "memdebug.h"
  93. /* Local API functions */
  94. static CURLcode smtp_regular_transfer(struct connectdata *conn, bool *done);
  95. static CURLcode smtp_do(struct connectdata *conn, bool *done);
  96. static CURLcode smtp_done(struct connectdata *conn,
  97. CURLcode, bool premature);
  98. static CURLcode smtp_connect(struct connectdata *conn, bool *done);
  99. static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection);
  100. static CURLcode smtp_multi_statemach(struct connectdata *conn, bool *done);
  101. static int smtp_getsock(struct connectdata *conn,
  102. curl_socket_t *socks,
  103. int numsocks);
  104. static CURLcode smtp_doing(struct connectdata *conn,
  105. bool *dophase_done);
  106. static CURLcode smtp_setup_connection(struct connectdata * conn);
  107. /*
  108. * SMTP protocol handler.
  109. */
  110. const struct Curl_handler Curl_handler_smtp = {
  111. "SMTP", /* scheme */
  112. smtp_setup_connection, /* setup_connection */
  113. smtp_do, /* do_it */
  114. smtp_done, /* done */
  115. ZERO_NULL, /* do_more */
  116. smtp_connect, /* connect_it */
  117. smtp_multi_statemach, /* connecting */
  118. smtp_doing, /* doing */
  119. smtp_getsock, /* proto_getsock */
  120. smtp_getsock, /* doing_getsock */
  121. ZERO_NULL, /* perform_getsock */
  122. smtp_disconnect, /* disconnect */
  123. PORT_SMTP, /* defport */
  124. PROT_SMTP /* protocol */
  125. };
  126. #ifdef USE_SSL
  127. /*
  128. * SMTPS protocol handler.
  129. */
  130. const struct Curl_handler Curl_handler_smtps = {
  131. "SMTPS", /* scheme */
  132. smtp_setup_connection, /* setup_connection */
  133. smtp_do, /* do_it */
  134. smtp_done, /* done */
  135. ZERO_NULL, /* do_more */
  136. smtp_connect, /* connect_it */
  137. smtp_multi_statemach, /* connecting */
  138. smtp_doing, /* doing */
  139. smtp_getsock, /* proto_getsock */
  140. smtp_getsock, /* doing_getsock */
  141. ZERO_NULL, /* perform_getsock */
  142. smtp_disconnect, /* disconnect */
  143. PORT_SMTPS, /* defport */
  144. PROT_SMTP | PROT_SMTPS | PROT_SSL /* protocol */
  145. };
  146. #endif
  147. #ifndef CURL_DISABLE_HTTP
  148. /*
  149. * HTTP-proxyed SMTP protocol handler.
  150. */
  151. static const struct Curl_handler Curl_handler_smtp_proxy = {
  152. "SMTP", /* scheme */
  153. ZERO_NULL, /* setup_connection */
  154. Curl_http, /* do_it */
  155. Curl_http_done, /* done */
  156. ZERO_NULL, /* do_more */
  157. ZERO_NULL, /* connect_it */
  158. ZERO_NULL, /* connecting */
  159. ZERO_NULL, /* doing */
  160. ZERO_NULL, /* proto_getsock */
  161. ZERO_NULL, /* doing_getsock */
  162. ZERO_NULL, /* perform_getsock */
  163. ZERO_NULL, /* disconnect */
  164. PORT_SMTP, /* defport */
  165. PROT_HTTP /* protocol */
  166. };
  167. #ifdef USE_SSL
  168. /*
  169. * HTTP-proxyed SMTPS protocol handler.
  170. */
  171. static const struct Curl_handler Curl_handler_smtps_proxy = {
  172. "SMTPS", /* scheme */
  173. ZERO_NULL, /* setup_connection */
  174. Curl_http, /* do_it */
  175. Curl_http_done, /* done */
  176. ZERO_NULL, /* do_more */
  177. ZERO_NULL, /* connect_it */
  178. ZERO_NULL, /* connecting */
  179. ZERO_NULL, /* doing */
  180. ZERO_NULL, /* proto_getsock */
  181. ZERO_NULL, /* doing_getsock */
  182. ZERO_NULL, /* perform_getsock */
  183. ZERO_NULL, /* disconnect */
  184. PORT_SMTPS, /* defport */
  185. PROT_HTTP /* protocol */
  186. };
  187. #endif
  188. #endif
  189. /* Function that checks for an ending smtp status code at the start of the
  190. given string.
  191. As a side effect, it also flags allowed authentication mechanisms according
  192. to EHLO AUTH response. */
  193. static int smtp_endofresp(struct pingpong *pp, int *resp)
  194. {
  195. char *line = pp->linestart_resp;
  196. size_t len = pp->nread_resp;
  197. struct connectdata *conn = pp->conn;
  198. struct smtp_conn *smtpc = &conn->proto.smtpc;
  199. int result;
  200. size_t wordlen;
  201. if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2]))
  202. return FALSE; /* Nothing for us. */
  203. if((result = (line[3] == ' ')) != 0)
  204. *resp = curlx_sltosi(strtol(line, NULL, 10));
  205. line += 4;
  206. len -= 4;
  207. if(smtpc->state == SMTP_EHLO && len >= 5 && !memcmp(line, "AUTH ", 5)) {
  208. line += 5;
  209. len -= 5;
  210. for (;;) {
  211. while (len &&
  212. (*line == ' ' || *line == '\t' ||
  213. *line == '\r' || *line == '\n')) {
  214. line++;
  215. len--;
  216. }
  217. if(!len)
  218. break;
  219. for (wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  220. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  221. line[wordlen] != '\n';)
  222. wordlen++;
  223. if(wordlen == 5 && !memcmp(line, "LOGIN", 5))
  224. smtpc->authmechs |= SMTP_AUTH_LOGIN;
  225. else if(wordlen == 5 && !memcmp(line, "PLAIN", 5))
  226. smtpc->authmechs |= SMTP_AUTH_PLAIN;
  227. else if(wordlen == 8 && !memcmp(line, "CRAM-MD5", 8))
  228. smtpc->authmechs |= SMTP_AUTH_CRAM_MD5;
  229. else if(wordlen == 10 && !memcmp(line, "DIGEST-MD5", 10))
  230. smtpc->authmechs |= SMTP_AUTH_DIGEST_MD5;
  231. else if(wordlen == 6 && !memcmp(line, "GSSAPI", 6))
  232. smtpc->authmechs |= SMTP_AUTH_GSSAPI;
  233. else if(wordlen == 8 && !memcmp(line, "EXTERNAL", 8))
  234. smtpc->authmechs |= SMTP_AUTH_EXTERNAL;
  235. line += wordlen;
  236. len -= wordlen;
  237. }
  238. }
  239. return result;
  240. }
  241. /* This is the ONLY way to change SMTP state! */
  242. static void state(struct connectdata *conn,
  243. smtpstate newstate)
  244. {
  245. struct smtp_conn *smtpc = &conn->proto.smtpc;
  246. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  247. /* for debug purposes */
  248. static const char * const names[]={
  249. "STOP",
  250. "SERVERGREET",
  251. "EHLO",
  252. "HELO",
  253. "STARTTLS",
  254. "AUTHPLAIN",
  255. "AUTHLOGIN",
  256. "AUTHPASSWD",
  257. "AUTHCRAM",
  258. "AUTH",
  259. "MAIL",
  260. "RCPT",
  261. "DATA",
  262. "POSTDATA",
  263. "QUIT",
  264. /* LAST */
  265. };
  266. if(smtpc->state != newstate)
  267. infof(conn->data, "SMTP %p state change from %s to %s\n",
  268. smtpc, names[smtpc->state], names[newstate]);
  269. #endif
  270. smtpc->state = newstate;
  271. }
  272. static CURLcode smtp_state_ehlo(struct connectdata *conn)
  273. {
  274. CURLcode result;
  275. struct smtp_conn *smtpc = &conn->proto.smtpc;
  276. smtpc->authmechs = 0; /* No known authentication mechanisms yet. */
  277. /* send EHLO */
  278. result = Curl_pp_sendf(&smtpc->pp, "EHLO %s", smtpc->domain);
  279. if(result)
  280. return result;
  281. state(conn, SMTP_EHLO);
  282. return CURLE_OK;
  283. }
  284. static CURLcode smtp_state_helo(struct connectdata *conn)
  285. {
  286. CURLcode result;
  287. struct smtp_conn *smtpc = &conn->proto.smtpc;
  288. /* send HELO */
  289. result = Curl_pp_sendf(&smtpc->pp, "HELO %s", smtpc->domain);
  290. if(result)
  291. return result;
  292. state(conn, SMTP_HELO);
  293. return CURLE_OK;
  294. }
  295. static size_t smtp_auth_plain_data(struct connectdata * conn, char * * outptr)
  296. {
  297. char plainauth[2 * MAX_CURL_USER_LENGTH + MAX_CURL_PASSWORD_LENGTH];
  298. size_t ulen;
  299. size_t plen;
  300. ulen = strlen(conn->user);
  301. plen = strlen(conn->passwd);
  302. if(2 * ulen + plen + 2 > sizeof plainauth)
  303. return 0;
  304. memcpy(plainauth, conn->user, ulen);
  305. plainauth[ulen] = '\0';
  306. memcpy(plainauth + ulen + 1, conn->user, ulen);
  307. plainauth[2 * ulen + 1] = '\0';
  308. memcpy(plainauth + 2 * ulen + 2, conn->passwd, plen);
  309. return Curl_base64_encode(conn->data, plainauth, 2 * ulen + plen + 2, outptr);
  310. }
  311. static size_t smtp_auth_login_user(struct connectdata * conn, char * * outptr)
  312. {
  313. size_t ulen;
  314. ulen = strlen(conn->user);
  315. if(!ulen) {
  316. *outptr = strdup("=");
  317. return *outptr? 1: 0;
  318. }
  319. return Curl_base64_encode(conn->data, conn->user, ulen, outptr);
  320. }
  321. static CURLcode smtp_authenticate(struct connectdata *conn)
  322. {
  323. CURLcode result = CURLE_OK;
  324. struct smtp_conn *smtpc = &conn->proto.smtpc;
  325. char * initresp;
  326. const char * mech;
  327. size_t l;
  328. smtpstate state1;
  329. smtpstate state2;
  330. if(!conn->bits.user_passwd)
  331. state(conn, SMTP_STOP); /* End of connect phase. */
  332. else {
  333. initresp = (char *) NULL;
  334. l = 1;
  335. /* Check supported authentication mechanisms by decreasing order of
  336. preference. */
  337. mech = (const char *) NULL; /* Avoid compiler warnings. */
  338. state1 = SMTP_STOP;
  339. state2 = SMTP_STOP;
  340. #ifndef CURL_DISABLE_CRYPTO_AUTH
  341. if(smtpc->authmechs & SMTP_AUTH_CRAM_MD5) {
  342. mech = "CRAM-MD5";
  343. state1 = SMTP_AUTHCRAM;
  344. }
  345. else
  346. #endif
  347. if(smtpc->authmechs & SMTP_AUTH_PLAIN) {
  348. mech = "PLAIN";
  349. state1 = SMTP_AUTHPLAIN;
  350. state2 = SMTP_AUTH;
  351. l = smtp_auth_plain_data(conn, &initresp);
  352. }
  353. else if(smtpc->authmechs & SMTP_AUTH_LOGIN) {
  354. mech = "LOGIN";
  355. state1 = SMTP_AUTHLOGIN;
  356. state2 = SMTP_AUTHPASSWD;
  357. l = smtp_auth_login_user(conn, &initresp);
  358. }
  359. else {
  360. infof(conn->data, "No known auth mechanisms supported!\n");
  361. result = CURLE_LOGIN_DENIED; /* Other mechanisms not supported. */
  362. }
  363. if(!result) {
  364. if(!l)
  365. result = CURLE_OUT_OF_MEMORY;
  366. else if(initresp &&
  367. l + strlen(mech) <= 512 - 8) { /* AUTH <mech> ...<crlf> */
  368. result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp);
  369. free(initresp);
  370. if(!result)
  371. state(conn, state2);
  372. }
  373. else {
  374. Curl_safefree(initresp);
  375. result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech);
  376. if(!result)
  377. state(conn, state1);
  378. }
  379. }
  380. }
  381. return result;
  382. }
  383. /* For the SMTP "protocol connect" and "doing" phases only */
  384. static int smtp_getsock(struct connectdata *conn,
  385. curl_socket_t *socks,
  386. int numsocks)
  387. {
  388. return Curl_pp_getsock(&conn->proto.smtpc.pp, socks, numsocks);
  389. }
  390. /* for STARTTLS responses */
  391. static CURLcode smtp_state_starttls_resp(struct connectdata *conn,
  392. int smtpcode,
  393. smtpstate instate)
  394. {
  395. CURLcode result = CURLE_OK;
  396. struct SessionHandle *data = conn->data;
  397. (void)instate; /* no use for this yet */
  398. if(smtpcode != 220) {
  399. if(data->set.ftp_ssl != CURLUSESSL_TRY) {
  400. failf(data, "STARTTLS denied. %c", smtpcode);
  401. result = CURLE_LOGIN_DENIED;
  402. }
  403. else
  404. result = smtp_authenticate(conn);
  405. }
  406. else {
  407. /* Curl_ssl_connect is BLOCKING */
  408. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  409. if(CURLE_OK == result) {
  410. conn->protocol |= PROT_SMTPS;
  411. result = smtp_state_ehlo(conn);
  412. }
  413. }
  414. return result;
  415. }
  416. /* for EHLO responses */
  417. static CURLcode smtp_state_ehlo_resp(struct connectdata *conn,
  418. int smtpcode,
  419. smtpstate instate)
  420. {
  421. CURLcode result = CURLE_OK;
  422. struct SessionHandle *data = conn->data;
  423. (void)instate; /* no use for this yet */
  424. if(smtpcode/100 != 2) {
  425. if((data->set.ftp_ssl <= CURLUSESSL_TRY || conn->ssl[FIRSTSOCKET].use) &&
  426. !conn->bits.user_passwd)
  427. result = smtp_state_helo(conn);
  428. else {
  429. failf(data, "Access denied: %d", smtpcode);
  430. result = CURLE_LOGIN_DENIED;
  431. }
  432. }
  433. else if(data->set.ftp_ssl && !conn->ssl[FIRSTSOCKET].use) {
  434. /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
  435. to TLS connection now */
  436. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "STARTTLS");
  437. state(conn, SMTP_STARTTLS);
  438. }
  439. else
  440. result = smtp_authenticate(conn);
  441. return result;
  442. }
  443. /* for HELO responses */
  444. static CURLcode smtp_state_helo_resp(struct connectdata *conn,
  445. int smtpcode,
  446. smtpstate instate)
  447. {
  448. CURLcode result = CURLE_OK;
  449. struct SessionHandle *data = conn->data;
  450. (void)instate; /* no use for this yet */
  451. if(smtpcode/100 != 2) {
  452. failf(data, "Access denied: %d", smtpcode);
  453. result = CURLE_LOGIN_DENIED;
  454. }
  455. else {
  456. /* end the connect phase */
  457. state(conn, SMTP_STOP);
  458. }
  459. return result;
  460. }
  461. /* for AUTH PLAIN (without initial response) responses */
  462. static CURLcode smtp_state_authplain_resp(struct connectdata *conn,
  463. int smtpcode,
  464. smtpstate instate)
  465. {
  466. CURLcode result = CURLE_OK;
  467. struct SessionHandle *data = conn->data;
  468. size_t l;
  469. char * plainauth;
  470. (void)instate; /* no use for this yet */
  471. if(smtpcode != 334) {
  472. failf(data, "Access denied: %d", smtpcode);
  473. result = CURLE_LOGIN_DENIED;
  474. }
  475. else {
  476. l = smtp_auth_plain_data(conn, &plainauth);
  477. if(!l)
  478. result = CURLE_OUT_OF_MEMORY;
  479. else {
  480. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", plainauth);
  481. free(plainauth);
  482. if(!result)
  483. state(conn, SMTP_AUTH);
  484. }
  485. }
  486. return result;
  487. }
  488. /* for AUTH LOGIN (without initial response) responses */
  489. static CURLcode smtp_state_authlogin_resp(struct connectdata *conn,
  490. int smtpcode,
  491. smtpstate instate)
  492. {
  493. CURLcode result = CURLE_OK;
  494. struct SessionHandle *data = conn->data;
  495. size_t l;
  496. char * authuser;
  497. (void)instate; /* no use for this yet */
  498. if(smtpcode != 334) {
  499. failf(data, "Access denied: %d", smtpcode);
  500. result = CURLE_LOGIN_DENIED;
  501. }
  502. else {
  503. l = smtp_auth_login_user(conn, &authuser);
  504. if(!l)
  505. result = CURLE_OUT_OF_MEMORY;
  506. else {
  507. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", authuser);
  508. free(authuser);
  509. if(!result)
  510. state(conn, SMTP_AUTHPASSWD);
  511. }
  512. }
  513. return result;
  514. }
  515. /* for responses to user entry of AUTH LOGIN. */
  516. static CURLcode smtp_state_authpasswd_resp(struct connectdata *conn,
  517. int smtpcode,
  518. smtpstate instate)
  519. {
  520. CURLcode result = CURLE_OK;
  521. struct SessionHandle *data = conn->data;
  522. size_t plen;
  523. size_t l;
  524. char *authpasswd;
  525. (void)instate; /* no use for this yet */
  526. if(smtpcode != 334) {
  527. failf(data, "Access denied: %d", smtpcode);
  528. result = CURLE_LOGIN_DENIED;
  529. }
  530. else {
  531. plen = strlen(conn->passwd);
  532. if(!plen)
  533. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "=");
  534. else {
  535. l = Curl_base64_encode(data, conn->passwd, plen, &authpasswd);
  536. if(!l)
  537. result = CURLE_OUT_OF_MEMORY;
  538. else {
  539. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", authpasswd);
  540. free(authpasswd);
  541. if(!result)
  542. state(conn, SMTP_AUTH);
  543. }
  544. }
  545. }
  546. return result;
  547. }
  548. #ifndef CURL_DISABLE_CRYPTO_AUTH
  549. /* for AUTH CRAM-MD5 responses. */
  550. static CURLcode smtp_state_authcram_resp(struct connectdata *conn,
  551. int smtpcode,
  552. smtpstate instate)
  553. {
  554. CURLcode result = CURLE_OK;
  555. struct SessionHandle *data = conn->data;
  556. char * chlg64 = data->state.buffer;
  557. unsigned char * chlg;
  558. size_t chlglen;
  559. size_t l;
  560. char * rplyb64;
  561. HMAC_context * ctxt;
  562. unsigned char digest[16];
  563. char reply[MAX_CURL_USER_LENGTH + 32 /* 2 * size of MD5 digest */ + 1];
  564. (void)instate; /* no use for this yet */
  565. if(smtpcode != 334) {
  566. failf(data, "Access denied: %d", smtpcode);
  567. return CURLE_LOGIN_DENIED;
  568. }
  569. /* Get the challenge. */
  570. for (chlg64 += 4; *chlg64 == ' ' || *chlg64 == '\t'; chlg64++)
  571. ;
  572. chlg = (unsigned char *) NULL;
  573. chlglen = 0;
  574. if(*chlg64 != '=') {
  575. for (l = strlen(chlg64); l--;)
  576. if(chlg64[l] != '\r' && chlg64[l] != '\n' && chlg64[l] != ' ' &&
  577. chlg64[l] != '\t')
  578. break;
  579. if(++l) {
  580. chlg64[l] = '\0';
  581. chlglen = Curl_base64_decode(chlg64, &chlg);
  582. if(!chlglen)
  583. return CURLE_OUT_OF_MEMORY;
  584. }
  585. }
  586. /* Compute digest. */
  587. ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
  588. (const unsigned char *) conn->passwd,
  589. (unsigned int)(strlen(conn->passwd)));
  590. if(!ctxt) {
  591. if(chlg)
  592. free(chlg);
  593. return CURLE_OUT_OF_MEMORY;
  594. }
  595. if(chlglen > 0)
  596. Curl_HMAC_update(ctxt, chlg, (unsigned int)(chlglen));
  597. if(chlg)
  598. free(chlg);
  599. Curl_HMAC_final(ctxt, digest);
  600. /* Prepare the reply. */
  601. snprintf(reply, sizeof reply,
  602. "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  603. conn->user, digest[0], digest[1], digest[2], digest[3], digest[4], digest[5],
  604. digest[6], digest[7], digest[8], digest[9], digest[10], digest[11],
  605. digest[12], digest[13], digest[14], digest[15]);
  606. /* Encode it to base64 and send it. */
  607. l = Curl_base64_encode(data, reply, 0, &rplyb64);
  608. if(!l)
  609. result = CURLE_OUT_OF_MEMORY;
  610. else {
  611. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", rplyb64);
  612. free(rplyb64);
  613. if(!result)
  614. state(conn, SMTP_AUTH);
  615. }
  616. return result;
  617. }
  618. #endif
  619. /* for final responses to AUTH sequences. */
  620. static CURLcode smtp_state_auth_resp(struct connectdata *conn,
  621. int smtpcode,
  622. smtpstate instate)
  623. {
  624. CURLcode result = CURLE_OK;
  625. struct SessionHandle *data = conn->data;
  626. (void)instate; /* no use for this yet */
  627. if(smtpcode != 235) {
  628. failf(data, "Authentication failed: %d", smtpcode);
  629. result = CURLE_LOGIN_DENIED;
  630. }
  631. else
  632. state(conn, SMTP_STOP); /* End of connect phase. */
  633. return result;
  634. }
  635. /* start the DO phase */
  636. static CURLcode smtp_mail(struct connectdata *conn)
  637. {
  638. CURLcode result = CURLE_OK;
  639. struct SessionHandle *data = conn->data;
  640. /* send MAIL */
  641. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "MAIL FROM:%s",
  642. data->set.str[STRING_MAIL_FROM]);
  643. if(result)
  644. return result;
  645. state(conn, SMTP_MAIL);
  646. return result;
  647. }
  648. static CURLcode smtp_rcpt_to(struct connectdata *conn)
  649. {
  650. CURLcode result = CURLE_OK;
  651. struct smtp_conn *smtpc = &conn->proto.smtpc;
  652. /* send RCPT TO */
  653. if(smtpc->rcpt) {
  654. if(smtpc->rcpt->data[0] == '<')
  655. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:%s",
  656. smtpc->rcpt->data);
  657. else
  658. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:<%s>",
  659. smtpc->rcpt->data);
  660. if(!result)
  661. state(conn, SMTP_RCPT);
  662. }
  663. return result;
  664. }
  665. /* for MAIL responses */
  666. static CURLcode smtp_state_mail_resp(struct connectdata *conn,
  667. int smtpcode,
  668. smtpstate instate)
  669. {
  670. CURLcode result = CURLE_OK;
  671. struct SessionHandle *data = conn->data;
  672. (void)instate; /* no use for this yet */
  673. if(smtpcode/100 != 2) {
  674. failf(data, "Access denied: %d", smtpcode);
  675. result = CURLE_LOGIN_DENIED;
  676. state(conn, SMTP_STOP);
  677. }
  678. else {
  679. struct smtp_conn *smtpc = &conn->proto.smtpc;
  680. smtpc->rcpt = data->set.mail_rcpt;
  681. result = smtp_rcpt_to(conn);
  682. }
  683. return result;
  684. }
  685. /* for RCPT responses */
  686. static CURLcode smtp_state_rcpt_resp(struct connectdata *conn,
  687. int smtpcode,
  688. smtpstate instate)
  689. {
  690. CURLcode result = CURLE_OK;
  691. struct SessionHandle *data = conn->data;
  692. (void)instate; /* no use for this yet */
  693. if(smtpcode/100 != 2) {
  694. failf(data, "Access denied: %d", smtpcode);
  695. result = CURLE_LOGIN_DENIED;
  696. state(conn, SMTP_STOP);
  697. }
  698. else {
  699. struct smtp_conn *smtpc = &conn->proto.smtpc;
  700. if(smtpc->rcpt) {
  701. smtpc->rcpt = smtpc->rcpt->next;
  702. result = smtp_rcpt_to(conn);
  703. /* if we failed or still is in RCPT sending, return */
  704. if(result || smtpc->rcpt)
  705. return result;
  706. }
  707. /* send DATA */
  708. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "DATA");
  709. if(result)
  710. return result;
  711. state(conn, SMTP_DATA);
  712. }
  713. return result;
  714. }
  715. /* for the DATA response */
  716. static CURLcode smtp_state_data_resp(struct connectdata *conn,
  717. int smtpcode,
  718. smtpstate instate)
  719. {
  720. struct SessionHandle *data = conn->data;
  721. struct FTP *smtp = data->state.proto.smtp;
  722. (void)instate; /* no use for this yet */
  723. if(smtpcode != 354) {
  724. state(conn, SMTP_STOP);
  725. return CURLE_RECV_ERROR;
  726. }
  727. /* SMTP upload */
  728. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
  729. FIRSTSOCKET, smtp->bytecountp);
  730. state(conn, SMTP_STOP);
  731. return CURLE_OK;
  732. }
  733. /* for the POSTDATA response, which is received after the entire DATA
  734. part has been sent off to the server */
  735. static CURLcode smtp_state_postdata_resp(struct connectdata *conn,
  736. int smtpcode,
  737. smtpstate instate)
  738. {
  739. CURLcode result = CURLE_OK;
  740. (void)instate; /* no use for this yet */
  741. if(smtpcode != 250)
  742. result = CURLE_RECV_ERROR;
  743. state(conn, SMTP_STOP);
  744. return result;
  745. }
  746. static CURLcode smtp_statemach_act(struct connectdata *conn)
  747. {
  748. CURLcode result;
  749. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  750. struct SessionHandle *data=conn->data;
  751. int smtpcode;
  752. struct smtp_conn *smtpc = &conn->proto.smtpc;
  753. struct pingpong *pp = &smtpc->pp;
  754. size_t nread = 0;
  755. if(pp->sendleft)
  756. /* we have a piece of a command still left to send */
  757. return Curl_pp_flushsend(pp);
  758. /* we read a piece of response */
  759. result = Curl_pp_readresp(sock, pp, &smtpcode, &nread);
  760. if(result)
  761. return result;
  762. if(smtpcode) {
  763. /* we have now received a full SMTP server response */
  764. switch(smtpc->state) {
  765. case SMTP_SERVERGREET:
  766. if(smtpcode/100 != 2) {
  767. failf(data, "Got unexpected smtp-server response: %d", smtpcode);
  768. return CURLE_FTP_WEIRD_SERVER_REPLY;
  769. }
  770. result = smtp_state_ehlo(conn);
  771. if(result)
  772. return result;
  773. break;
  774. case SMTP_EHLO:
  775. result = smtp_state_ehlo_resp(conn, smtpcode, smtpc->state);
  776. break;
  777. case SMTP_HELO:
  778. result = smtp_state_helo_resp(conn, smtpcode, smtpc->state);
  779. break;
  780. case SMTP_STARTTLS:
  781. result = smtp_state_starttls_resp(conn, smtpcode, smtpc->state);
  782. break;
  783. case SMTP_AUTHPLAIN:
  784. result = smtp_state_authplain_resp(conn, smtpcode, smtpc->state);
  785. break;
  786. case SMTP_AUTHLOGIN:
  787. result = smtp_state_authlogin_resp(conn, smtpcode, smtpc->state);
  788. break;
  789. case SMTP_AUTHPASSWD:
  790. result = smtp_state_authpasswd_resp(conn, smtpcode, smtpc->state);
  791. break;
  792. #ifndef CURL_DISABLE_CRYPTO_AUTH
  793. case SMTP_AUTHCRAM:
  794. result = smtp_state_authcram_resp(conn, smtpcode, smtpc->state);
  795. break;
  796. #endif
  797. case SMTP_AUTH:
  798. result = smtp_state_auth_resp(conn, smtpcode, smtpc->state);
  799. break;
  800. case SMTP_MAIL:
  801. result = smtp_state_mail_resp(conn, smtpcode, smtpc->state);
  802. break;
  803. case SMTP_RCPT:
  804. result = smtp_state_rcpt_resp(conn, smtpcode, smtpc->state);
  805. break;
  806. case SMTP_DATA:
  807. result = smtp_state_data_resp(conn, smtpcode, smtpc->state);
  808. break;
  809. case SMTP_POSTDATA:
  810. result = smtp_state_postdata_resp(conn, smtpcode, smtpc->state);
  811. break;
  812. case SMTP_QUIT:
  813. /* fallthrough, just stop! */
  814. default:
  815. /* internal error */
  816. state(conn, SMTP_STOP);
  817. break;
  818. }
  819. }
  820. return result;
  821. }
  822. /* called repeatedly until done from multi.c */
  823. static CURLcode smtp_multi_statemach(struct connectdata *conn,
  824. bool *done)
  825. {
  826. struct smtp_conn *smtpc = &conn->proto.smtpc;
  827. CURLcode result = Curl_pp_multi_statemach(&smtpc->pp);
  828. *done = (bool)(smtpc->state == SMTP_STOP);
  829. return result;
  830. }
  831. static CURLcode smtp_easy_statemach(struct connectdata *conn)
  832. {
  833. struct smtp_conn *smtpc = &conn->proto.smtpc;
  834. struct pingpong *pp = &smtpc->pp;
  835. CURLcode result = CURLE_OK;
  836. while(smtpc->state != SMTP_STOP) {
  837. result = Curl_pp_easy_statemach(pp);
  838. if(result)
  839. break;
  840. }
  841. return result;
  842. }
  843. /*
  844. * Allocate and initialize the struct SMTP for the current SessionHandle. If
  845. * need be.
  846. */
  847. static CURLcode smtp_init(struct connectdata *conn)
  848. {
  849. struct SessionHandle *data = conn->data;
  850. struct FTP *smtp = data->state.proto.smtp;
  851. if(!smtp) {
  852. smtp = data->state.proto.smtp = calloc(sizeof(struct FTP), 1);
  853. if(!smtp)
  854. return CURLE_OUT_OF_MEMORY;
  855. }
  856. /* get some initial data into the smtp struct */
  857. smtp->bytecountp = &data->req.bytecount;
  858. /* No need to duplicate user+password, the connectdata struct won't change
  859. during a session, but we re-init them here since on subsequent inits
  860. since the conn struct may have changed or been replaced.
  861. */
  862. smtp->user = conn->user;
  863. smtp->passwd = conn->passwd;
  864. return CURLE_OK;
  865. }
  866. /*
  867. * smtp_connect() should do everything that is to be considered a part of
  868. * the connection phase.
  869. *
  870. * The variable 'done' points to will be TRUE if the protocol-layer connect
  871. * phase is done when this function returns, or FALSE is not. When called as
  872. * a part of the easy interface, it will always be TRUE.
  873. */
  874. static CURLcode smtp_connect(struct connectdata *conn,
  875. bool *done) /* see description above */
  876. {
  877. CURLcode result;
  878. struct smtp_conn *smtpc = &conn->proto.smtpc;
  879. struct SessionHandle *data=conn->data;
  880. struct pingpong *pp=&smtpc->pp;
  881. const char *path = conn->data->state.path;
  882. int len;
  883. char localhost[1024 + 1];
  884. *done = FALSE; /* default to not done yet */
  885. /* If there already is a protocol-specific struct allocated for this
  886. sessionhandle, deal with it */
  887. Curl_reset_reqproto(conn);
  888. result = smtp_init(conn);
  889. if(CURLE_OK != result)
  890. return result;
  891. /* We always support persistant connections on smtp */
  892. conn->bits.close = FALSE;
  893. pp->response_time = RESP_TIMEOUT; /* set default response time-out */
  894. pp->statemach_act = smtp_statemach_act;
  895. pp->endofresp = smtp_endofresp;
  896. pp->conn = conn;
  897. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
  898. if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
  899. /* for SMTP over HTTP proxy */
  900. struct HTTP http_proxy;
  901. struct FTP *smtp_save;
  902. /* BLOCKING */
  903. /* We want "seamless" SMTP operations through HTTP proxy tunnel */
  904. /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the member
  905. * conn->proto.http; we want SMTP through HTTP and we have to change the
  906. * member temporarily for connecting to the HTTP proxy. After
  907. * Curl_proxyCONNECT we have to set back the member to the original struct
  908. * SMTP pointer
  909. */
  910. smtp_save = data->state.proto.smtp;
  911. memset(&http_proxy, 0, sizeof(http_proxy));
  912. data->state.proto.http = &http_proxy;
  913. result = Curl_proxyCONNECT(conn, FIRSTSOCKET,
  914. conn->host.name, conn->remote_port);
  915. data->state.proto.smtp = smtp_save;
  916. if(CURLE_OK != result)
  917. return result;
  918. }
  919. #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */
  920. if(conn->protocol & PROT_SMTPS) {
  921. /* BLOCKING */
  922. /* SMTPS is simply smtp with SSL for the control channel */
  923. /* now, perform the SSL initialization for this socket */
  924. result = Curl_ssl_connect(conn, FIRSTSOCKET);
  925. if(result)
  926. return result;
  927. }
  928. Curl_pp_init(pp); /* init the response reader stuff */
  929. pp->response_time = RESP_TIMEOUT; /* set default response time-out */
  930. pp->statemach_act = smtp_statemach_act;
  931. pp->endofresp = smtp_endofresp;
  932. pp->conn = conn;
  933. if(!*path) {
  934. if(!Curl_gethostname(localhost, sizeof localhost))
  935. path = localhost;
  936. else
  937. path = "localhost";
  938. }
  939. /* url decode the path and use it as domain with EHLO */
  940. smtpc->domain = curl_easy_unescape(conn->data, path, 0, &len);
  941. if(!smtpc->domain)
  942. return CURLE_OUT_OF_MEMORY;
  943. /* When we connect, we start in the state where we await the server greeting
  944. */
  945. state(conn, SMTP_SERVERGREET);
  946. if(data->state.used_interface == Curl_if_multi)
  947. result = smtp_multi_statemach(conn, done);
  948. else {
  949. result = smtp_easy_statemach(conn);
  950. if(!result)
  951. *done = TRUE;
  952. }
  953. return result;
  954. }
  955. /***********************************************************************
  956. *
  957. * smtp_done()
  958. *
  959. * The DONE function. This does what needs to be done after a single DO has
  960. * performed.
  961. *
  962. * Input argument is already checked for validity.
  963. */
  964. static CURLcode smtp_done(struct connectdata *conn, CURLcode status,
  965. bool premature)
  966. {
  967. struct SessionHandle *data = conn->data;
  968. struct FTP *smtp = data->state.proto.smtp;
  969. CURLcode result=CURLE_OK;
  970. ssize_t bytes_written;
  971. (void)premature;
  972. if(!smtp)
  973. /* When the easy handle is removed from the multi while libcurl is still
  974. * trying to resolve the host name, it seems that the smtp struct is not
  975. * yet initialized, but the removal action calls Curl_done() which calls
  976. * this function. So we simply return success if no smtp pointer is set.
  977. */
  978. return CURLE_OK;
  979. if(status) {
  980. conn->bits.close = TRUE; /* marked for closure */
  981. result = status; /* use the already set error code */
  982. }
  983. else
  984. /* TODO: make this work even when the socket is EWOULDBLOCK in this call! */
  985. /* write to socket (send away data) */
  986. result = Curl_write(conn,
  987. conn->writesockfd, /* socket to send to */
  988. SMTP_EOB, /* buffer pointer */
  989. SMTP_EOB_LEN, /* buffer size */
  990. &bytes_written); /* actually sent away */
  991. if(status == CURLE_OK) {
  992. struct smtp_conn *smtpc = &conn->proto.smtpc;
  993. struct pingpong *pp= &smtpc->pp;
  994. pp->response = Curl_tvnow(); /* timeout relative now */
  995. state(conn, SMTP_POSTDATA);
  996. /* run the state-machine
  997. TODO: when the multi interface is used, this _really_ should be using
  998. the smtp_multi_statemach function but we have no general support for
  999. non-blocking DONE operations, not in the multi state machine and with
  1000. Curl_done() invokes on several places in the code!
  1001. */
  1002. result = smtp_easy_statemach(conn);
  1003. }
  1004. /* clear these for next connection */
  1005. smtp->transfer = FTPTRANSFER_BODY;
  1006. return result;
  1007. }
  1008. /***********************************************************************
  1009. *
  1010. * smtp_perform()
  1011. *
  1012. * This is the actual DO function for SMTP. Get a file/directory according to
  1013. * the options previously setup.
  1014. */
  1015. static
  1016. CURLcode smtp_perform(struct connectdata *conn,
  1017. bool *connected, /* connect status after PASV / PORT */
  1018. bool *dophase_done)
  1019. {
  1020. /* this is SMTP and no proxy */
  1021. CURLcode result=CURLE_OK;
  1022. DEBUGF(infof(conn->data, "DO phase starts\n"));
  1023. if(conn->data->set.opt_no_body) {
  1024. /* requested no body means no transfer... */
  1025. struct FTP *smtp = conn->data->state.proto.smtp;
  1026. smtp->transfer = FTPTRANSFER_INFO;
  1027. }
  1028. *dophase_done = FALSE; /* not done yet */
  1029. /* start the first command in the DO phase */
  1030. result = smtp_mail(conn);
  1031. if(result)
  1032. return result;
  1033. /* run the state-machine */
  1034. if(conn->data->state.used_interface == Curl_if_multi)
  1035. result = smtp_multi_statemach(conn, dophase_done);
  1036. else {
  1037. result = smtp_easy_statemach(conn);
  1038. *dophase_done = TRUE; /* with the easy interface we are done here */
  1039. }
  1040. *connected = conn->bits.tcpconnect;
  1041. if(*dophase_done)
  1042. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1043. return result;
  1044. }
  1045. /***********************************************************************
  1046. *
  1047. * smtp_do()
  1048. *
  1049. * This function is registered as 'curl_do' function. It decodes the path
  1050. * parts etc as a wrapper to the actual DO function (smtp_perform).
  1051. *
  1052. * The input argument is already checked for validity.
  1053. */
  1054. static CURLcode smtp_do(struct connectdata *conn, bool *done)
  1055. {
  1056. CURLcode retcode = CURLE_OK;
  1057. *done = FALSE; /* default to false */
  1058. /*
  1059. Since connections can be re-used between SessionHandles, this might be a
  1060. connection already existing but on a fresh SessionHandle struct so we must
  1061. make sure we have a good 'struct SMTP' to play with. For new connections,
  1062. the struct SMTP is allocated and setup in the smtp_connect() function.
  1063. */
  1064. Curl_reset_reqproto(conn);
  1065. retcode = smtp_init(conn);
  1066. if(retcode)
  1067. return retcode;
  1068. retcode = smtp_regular_transfer(conn, done);
  1069. return retcode;
  1070. }
  1071. /***********************************************************************
  1072. *
  1073. * smtp_quit()
  1074. *
  1075. * This should be called before calling sclose(). We should then wait for the
  1076. * response from the server before returning. The calling code should then try
  1077. * to close the connection.
  1078. *
  1079. */
  1080. static CURLcode smtp_quit(struct connectdata *conn)
  1081. {
  1082. CURLcode result = CURLE_OK;
  1083. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "QUIT");
  1084. if(result)
  1085. return result;
  1086. state(conn, SMTP_QUIT);
  1087. result = smtp_easy_statemach(conn);
  1088. return result;
  1089. }
  1090. /***********************************************************************
  1091. *
  1092. * smtp_disconnect()
  1093. *
  1094. * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
  1095. * resources. BLOCKING.
  1096. */
  1097. static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection)
  1098. {
  1099. struct smtp_conn *smtpc= &conn->proto.smtpc;
  1100. /* We cannot send quit unconditionally. If this connection is stale or
  1101. bad in any way, sending quit and waiting around here will make the
  1102. disconnect wait in vain and cause more problems than we need to.
  1103. */
  1104. /* The SMTP session may or may not have been allocated/setup at this
  1105. point! */
  1106. if(!dead_connection && smtpc->pp.conn)
  1107. (void)smtp_quit(conn); /* ignore errors on the LOGOUT */
  1108. Curl_pp_disconnect(&smtpc->pp);
  1109. /* This won't already be freed in some error cases */
  1110. Curl_safefree(smtpc->domain);
  1111. smtpc->domain = NULL;
  1112. return CURLE_OK;
  1113. }
  1114. /* call this when the DO phase has completed */
  1115. static CURLcode smtp_dophase_done(struct connectdata *conn,
  1116. bool connected)
  1117. {
  1118. struct FTP *smtp = conn->data->state.proto.smtp;
  1119. struct smtp_conn *smtpc= &conn->proto.smtpc;
  1120. (void)connected;
  1121. if(smtp->transfer != FTPTRANSFER_BODY)
  1122. /* no data to transfer */
  1123. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1124. free(smtpc->domain);
  1125. smtpc->domain = NULL;
  1126. return CURLE_OK;
  1127. }
  1128. /* called from multi.c while DOing */
  1129. static CURLcode smtp_doing(struct connectdata *conn,
  1130. bool *dophase_done)
  1131. {
  1132. CURLcode result;
  1133. result = smtp_multi_statemach(conn, dophase_done);
  1134. if(*dophase_done) {
  1135. result = smtp_dophase_done(conn, FALSE /* not connected */);
  1136. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1137. }
  1138. return result;
  1139. }
  1140. /***********************************************************************
  1141. *
  1142. * smtp_regular_transfer()
  1143. *
  1144. * The input argument is already checked for validity.
  1145. *
  1146. * Performs all commands done before a regular transfer between a local and a
  1147. * remote host.
  1148. */
  1149. static
  1150. CURLcode smtp_regular_transfer(struct connectdata *conn,
  1151. bool *dophase_done)
  1152. {
  1153. CURLcode result=CURLE_OK;
  1154. bool connected=FALSE;
  1155. struct SessionHandle *data = conn->data;
  1156. data->req.size = -1; /* make sure this is unknown at this point */
  1157. Curl_pgrsSetUploadCounter(data, 0);
  1158. Curl_pgrsSetDownloadCounter(data, 0);
  1159. Curl_pgrsSetUploadSize(data, 0);
  1160. Curl_pgrsSetDownloadSize(data, 0);
  1161. result = smtp_perform(conn,
  1162. &connected, /* have we connected after PASV/PORT */
  1163. dophase_done); /* all commands in the DO-phase done? */
  1164. if(CURLE_OK == result) {
  1165. if(!*dophase_done)
  1166. /* the DO phase has not completed yet */
  1167. return CURLE_OK;
  1168. result = smtp_dophase_done(conn, connected);
  1169. if(result)
  1170. return result;
  1171. }
  1172. return result;
  1173. }
  1174. static CURLcode smtp_setup_connection(struct connectdata * conn)
  1175. {
  1176. struct SessionHandle *data = conn->data;
  1177. if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
  1178. /* Unless we have asked to tunnel smtp operations through the proxy, we
  1179. switch and use HTTP operations only */
  1180. #ifndef CURL_DISABLE_HTTP
  1181. if(conn->handler == &Curl_handler_smtp)
  1182. conn->handler = &Curl_handler_smtp_proxy;
  1183. else {
  1184. #ifdef USE_SSL
  1185. conn->handler = &Curl_handler_smtps_proxy;
  1186. #else
  1187. failf(data, "SMTPS not supported!");
  1188. return CURLE_UNSUPPORTED_PROTOCOL;
  1189. #endif
  1190. }
  1191. /*
  1192. * We explicitly mark this connection as persistent here as we're doing
  1193. * SMTP over HTTP and thus we accidentally avoid setting this value
  1194. * otherwise.
  1195. */
  1196. conn->bits.close = FALSE;
  1197. #else
  1198. failf(data, "SMTP over http proxy requires HTTP support built-in!");
  1199. return CURLE_UNSUPPORTED_PROTOCOL;
  1200. #endif
  1201. }
  1202. data->state.path++; /* don't include the initial slash */
  1203. return CURLE_OK;
  1204. }
  1205. CURLcode Curl_smtp_escape_eob(struct connectdata *conn, ssize_t nread)
  1206. {
  1207. /* When sending SMTP payload, we must detect CRLF.CRLF sequences in
  1208. * the data and make sure it is sent as CRLF..CRLF instead, as
  1209. * otherwise it will wrongly be detected as end of data by the server.
  1210. */
  1211. ssize_t i;
  1212. ssize_t si;
  1213. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1214. struct SessionHandle *data = conn->data;
  1215. if(data->state.scratch == NULL)
  1216. data->state.scratch = malloc(2*BUFSIZE);
  1217. if(data->state.scratch == NULL) {
  1218. failf (data, "Failed to alloc scratch buffer!");
  1219. return CURLE_OUT_OF_MEMORY;
  1220. }
  1221. /* This loop can be improved by some kind of Boyer-Moore style of
  1222. approach but that is saved for later... */
  1223. for(i = 0, si = 0; i < nread; i++, si++) {
  1224. ssize_t left = nread - i;
  1225. if(left>= (ssize_t)(SMTP_EOB_LEN-smtpc->eob)) {
  1226. if(!memcmp(SMTP_EOB+smtpc->eob, &data->req.upload_fromhere[i],
  1227. SMTP_EOB_LEN-smtpc->eob)) {
  1228. /* It matched, copy the replacement data to the target buffer
  1229. instead. Note that the replacement does not contain the
  1230. trailing CRLF but we instead continue to match on that one
  1231. to deal with repeated sequences. Like CRLF.CRLF.CRLF etc
  1232. */
  1233. memcpy(&data->state.scratch[si], SMTP_EOB_REPL,
  1234. SMTP_EOB_REPL_LEN);
  1235. si+=SMTP_EOB_REPL_LEN-1; /* minus one since the for() increments
  1236. it */
  1237. i+=SMTP_EOB_LEN-smtpc->eob-1-2;
  1238. smtpc->eob = 0; /* start over */
  1239. continue;
  1240. }
  1241. }
  1242. else if(!memcmp(SMTP_EOB+smtpc->eob, &data->req.upload_fromhere[i],
  1243. left)) {
  1244. /* the last piece of the data matches the EOB so we can't send that
  1245. until we know the rest of it */
  1246. smtpc->eob += left;
  1247. break;
  1248. }
  1249. data->state.scratch[si] = data->req.upload_fromhere[i];
  1250. } /* for() */
  1251. if(si != nread) {
  1252. /* only use the new buffer if we replaced something */
  1253. nread = si;
  1254. /* upload from the new (replaced) buffer instead */
  1255. data->req.upload_fromhere = data->state.scratch;
  1256. /* set the new amount too */
  1257. data->req.upload_present = nread;
  1258. }
  1259. return CURLE_OK;
  1260. }
  1261. #endif /* CURL_DISABLE_SMTP */