smtp.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * RFC1870 SMTP Service Extension for Message Size
  22. * RFC2195 CRAM-MD5 authentication
  23. * RFC2831 DIGEST-MD5 authentication
  24. * RFC3207 SMTP over TLS
  25. * RFC4422 Simple Authentication and Security Layer (SASL)
  26. * RFC4616 PLAIN authentication
  27. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  28. * RFC4954 SMTP Authentication
  29. * RFC5321 SMTP protocol
  30. * RFC6749 OAuth 2.0 Authorization Framework
  31. * RFC8314 Use of TLS for Email Submission and Access
  32. * Draft SMTP URL Interface <draft-earhart-url-smtp-00.txt>
  33. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  34. *
  35. ***************************************************************************/
  36. #include "curl_setup.h"
  37. #ifndef CURL_DISABLE_SMTP
  38. #ifdef HAVE_NETINET_IN_H
  39. #include <netinet/in.h>
  40. #endif
  41. #ifdef HAVE_ARPA_INET_H
  42. #include <arpa/inet.h>
  43. #endif
  44. #ifdef HAVE_UTSNAME_H
  45. #include <sys/utsname.h>
  46. #endif
  47. #ifdef HAVE_NETDB_H
  48. #include <netdb.h>
  49. #endif
  50. #ifdef __VMS
  51. #include <in.h>
  52. #include <inet.h>
  53. #endif
  54. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  55. #undef in_addr_t
  56. #define in_addr_t unsigned long
  57. #endif
  58. #include <curl/curl.h>
  59. #include "urldata.h"
  60. #include "sendf.h"
  61. #include "hostip.h"
  62. #include "progress.h"
  63. #include "transfer.h"
  64. #include "escape.h"
  65. #include "http.h" /* for HTTP proxy tunnel stuff */
  66. #include "mime.h"
  67. #include "socks.h"
  68. #include "smtp.h"
  69. #include "strtoofft.h"
  70. #include "strcase.h"
  71. #include "vtls/vtls.h"
  72. #include "connect.h"
  73. #include "strerror.h"
  74. #include "select.h"
  75. #include "multiif.h"
  76. #include "url.h"
  77. #include "curl_gethostname.h"
  78. #include "curl_sasl.h"
  79. #include "warnless.h"
  80. /* The last 3 #include files should be in this order */
  81. #include "curl_printf.h"
  82. #include "curl_memory.h"
  83. #include "memdebug.h"
  84. /* Local API functions */
  85. static CURLcode smtp_regular_transfer(struct connectdata *conn, bool *done);
  86. static CURLcode smtp_do(struct connectdata *conn, bool *done);
  87. static CURLcode smtp_done(struct connectdata *conn, CURLcode status,
  88. bool premature);
  89. static CURLcode smtp_connect(struct connectdata *conn, bool *done);
  90. static CURLcode smtp_disconnect(struct connectdata *conn, bool dead);
  91. static CURLcode smtp_multi_statemach(struct connectdata *conn, bool *done);
  92. static int smtp_getsock(struct connectdata *conn, curl_socket_t *socks,
  93. int numsocks);
  94. static CURLcode smtp_doing(struct connectdata *conn, bool *dophase_done);
  95. static CURLcode smtp_setup_connection(struct connectdata *conn);
  96. static CURLcode smtp_parse_url_options(struct connectdata *conn);
  97. static CURLcode smtp_parse_url_path(struct connectdata *conn);
  98. static CURLcode smtp_parse_custom_request(struct connectdata *conn);
  99. static CURLcode smtp_perform_auth(struct connectdata *conn, const char *mech,
  100. const char *initresp);
  101. static CURLcode smtp_continue_auth(struct connectdata *conn, const char *resp);
  102. static void smtp_get_message(char *buffer, char **outptr);
  103. /*
  104. * SMTP protocol handler.
  105. */
  106. const struct Curl_handler Curl_handler_smtp = {
  107. "SMTP", /* scheme */
  108. smtp_setup_connection, /* setup_connection */
  109. smtp_do, /* do_it */
  110. smtp_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. smtp_connect, /* connect_it */
  113. smtp_multi_statemach, /* connecting */
  114. smtp_doing, /* doing */
  115. smtp_getsock, /* proto_getsock */
  116. smtp_getsock, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. smtp_disconnect, /* disconnect */
  120. ZERO_NULL, /* readwrite */
  121. ZERO_NULL, /* connection_check */
  122. PORT_SMTP, /* defport */
  123. CURLPROTO_SMTP, /* protocol */
  124. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */
  125. PROTOPT_URLOPTIONS
  126. };
  127. #ifdef USE_SSL
  128. /*
  129. * SMTPS protocol handler.
  130. */
  131. const struct Curl_handler Curl_handler_smtps = {
  132. "SMTPS", /* scheme */
  133. smtp_setup_connection, /* setup_connection */
  134. smtp_do, /* do_it */
  135. smtp_done, /* done */
  136. ZERO_NULL, /* do_more */
  137. smtp_connect, /* connect_it */
  138. smtp_multi_statemach, /* connecting */
  139. smtp_doing, /* doing */
  140. smtp_getsock, /* proto_getsock */
  141. smtp_getsock, /* doing_getsock */
  142. ZERO_NULL, /* domore_getsock */
  143. ZERO_NULL, /* perform_getsock */
  144. smtp_disconnect, /* disconnect */
  145. ZERO_NULL, /* readwrite */
  146. ZERO_NULL, /* connection_check */
  147. PORT_SMTPS, /* defport */
  148. CURLPROTO_SMTPS, /* protocol */
  149. PROTOPT_CLOSEACTION | PROTOPT_SSL
  150. | PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS /* flags */
  151. };
  152. #endif
  153. /* SASL parameters for the smtp protocol */
  154. static const struct SASLproto saslsmtp = {
  155. "smtp", /* The service name */
  156. 334, /* Code received when continuation is expected */
  157. 235, /* Code to receive upon authentication success */
  158. 512 - 8, /* Maximum initial response length (no max) */
  159. smtp_perform_auth, /* Send authentication command */
  160. smtp_continue_auth, /* Send authentication continuation */
  161. smtp_get_message /* Get SASL response message */
  162. };
  163. #ifdef USE_SSL
  164. static void smtp_to_smtps(struct connectdata *conn)
  165. {
  166. /* Change the connection handler */
  167. conn->handler = &Curl_handler_smtps;
  168. /* Set the connection's upgraded to TLS flag */
  169. conn->tls_upgraded = TRUE;
  170. }
  171. #else
  172. #define smtp_to_smtps(x) Curl_nop_stmt
  173. #endif
  174. /***********************************************************************
  175. *
  176. * smtp_endofresp()
  177. *
  178. * Checks for an ending SMTP status code at the start of the given string, but
  179. * also detects various capabilities from the EHLO response including the
  180. * supported authentication mechanisms.
  181. */
  182. static bool smtp_endofresp(struct connectdata *conn, char *line, size_t len,
  183. int *resp)
  184. {
  185. struct smtp_conn *smtpc = &conn->proto.smtpc;
  186. bool result = FALSE;
  187. /* Nothing for us */
  188. if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2]))
  189. return FALSE;
  190. /* Do we have a command response? This should be the response code followed
  191. by a space and optionally some text as per RFC-5321 and as outlined in
  192. Section 4. Examples of RFC-4954 but some e-mail servers ignore this and
  193. only send the response code instead as per Section 4.2. */
  194. if(line[3] == ' ' || len == 5) {
  195. char tmpline[6];
  196. result = TRUE;
  197. memset(tmpline, '\0', sizeof(tmpline));
  198. memcpy(tmpline, line, (len == 5 ? 5 : 3));
  199. *resp = curlx_sltosi(strtol(tmpline, NULL, 10));
  200. /* Make sure real server never sends internal value */
  201. if(*resp == 1)
  202. *resp = 0;
  203. }
  204. /* Do we have a multiline (continuation) response? */
  205. else if(line[3] == '-' &&
  206. (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) {
  207. result = TRUE;
  208. *resp = 1; /* Internal response code */
  209. }
  210. return result;
  211. }
  212. /***********************************************************************
  213. *
  214. * smtp_get_message()
  215. *
  216. * Gets the authentication message from the response buffer.
  217. */
  218. static void smtp_get_message(char *buffer, char **outptr)
  219. {
  220. size_t len = strlen(buffer);
  221. char *message = NULL;
  222. if(len > 4) {
  223. /* Find the start of the message */
  224. len -= 4;
  225. for(message = buffer + 4; *message == ' ' || *message == '\t';
  226. message++, len--)
  227. ;
  228. /* Find the end of the message */
  229. for(; len--;)
  230. if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
  231. message[len] != '\t')
  232. break;
  233. /* Terminate the message */
  234. if(++len) {
  235. message[len] = '\0';
  236. }
  237. }
  238. else
  239. /* junk input => zero length output */
  240. message = &buffer[len];
  241. *outptr = message;
  242. }
  243. /***********************************************************************
  244. *
  245. * state()
  246. *
  247. * This is the ONLY way to change SMTP state!
  248. */
  249. static void state(struct connectdata *conn, smtpstate newstate)
  250. {
  251. struct smtp_conn *smtpc = &conn->proto.smtpc;
  252. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  253. /* for debug purposes */
  254. static const char * const names[] = {
  255. "STOP",
  256. "SERVERGREET",
  257. "EHLO",
  258. "HELO",
  259. "STARTTLS",
  260. "UPGRADETLS",
  261. "AUTH",
  262. "COMMAND",
  263. "MAIL",
  264. "RCPT",
  265. "DATA",
  266. "POSTDATA",
  267. "QUIT",
  268. /* LAST */
  269. };
  270. if(smtpc->state != newstate)
  271. infof(conn->data, "SMTP %p state change from %s to %s\n",
  272. (void *)smtpc, names[smtpc->state], names[newstate]);
  273. #endif
  274. smtpc->state = newstate;
  275. }
  276. /***********************************************************************
  277. *
  278. * smtp_perform_ehlo()
  279. *
  280. * Sends the EHLO command to not only initialise communication with the ESMTP
  281. * server but to also obtain a list of server side supported capabilities.
  282. */
  283. static CURLcode smtp_perform_ehlo(struct connectdata *conn)
  284. {
  285. CURLcode result = CURLE_OK;
  286. struct smtp_conn *smtpc = &conn->proto.smtpc;
  287. smtpc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanism yet */
  288. smtpc->sasl.authused = SASL_AUTH_NONE; /* Clear the authentication mechanism
  289. used for esmtp connections */
  290. smtpc->tls_supported = FALSE; /* Clear the TLS capability */
  291. smtpc->auth_supported = FALSE; /* Clear the AUTH capability */
  292. /* Send the EHLO command */
  293. result = Curl_pp_sendf(&smtpc->pp, "EHLO %s", smtpc->domain);
  294. if(!result)
  295. state(conn, SMTP_EHLO);
  296. return result;
  297. }
  298. /***********************************************************************
  299. *
  300. * smtp_perform_helo()
  301. *
  302. * Sends the HELO command to initialise communication with the SMTP server.
  303. */
  304. static CURLcode smtp_perform_helo(struct connectdata *conn)
  305. {
  306. CURLcode result = CURLE_OK;
  307. struct smtp_conn *smtpc = &conn->proto.smtpc;
  308. smtpc->sasl.authused = SASL_AUTH_NONE; /* No authentication mechanism used
  309. in smtp connections */
  310. /* Send the HELO command */
  311. result = Curl_pp_sendf(&smtpc->pp, "HELO %s", smtpc->domain);
  312. if(!result)
  313. state(conn, SMTP_HELO);
  314. return result;
  315. }
  316. /***********************************************************************
  317. *
  318. * smtp_perform_starttls()
  319. *
  320. * Sends the STLS command to start the upgrade to TLS.
  321. */
  322. static CURLcode smtp_perform_starttls(struct connectdata *conn)
  323. {
  324. CURLcode result = CURLE_OK;
  325. /* Send the STARTTLS command */
  326. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "STARTTLS");
  327. if(!result)
  328. state(conn, SMTP_STARTTLS);
  329. return result;
  330. }
  331. /***********************************************************************
  332. *
  333. * smtp_perform_upgrade_tls()
  334. *
  335. * Performs the upgrade to TLS.
  336. */
  337. static CURLcode smtp_perform_upgrade_tls(struct connectdata *conn)
  338. {
  339. CURLcode result = CURLE_OK;
  340. struct smtp_conn *smtpc = &conn->proto.smtpc;
  341. /* Start the SSL connection */
  342. result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &smtpc->ssldone);
  343. if(!result) {
  344. if(smtpc->state != SMTP_UPGRADETLS)
  345. state(conn, SMTP_UPGRADETLS);
  346. if(smtpc->ssldone) {
  347. smtp_to_smtps(conn);
  348. result = smtp_perform_ehlo(conn);
  349. }
  350. }
  351. return result;
  352. }
  353. /***********************************************************************
  354. *
  355. * smtp_perform_auth()
  356. *
  357. * Sends an AUTH command allowing the client to login with the given SASL
  358. * authentication mechanism.
  359. */
  360. static CURLcode smtp_perform_auth(struct connectdata *conn,
  361. const char *mech,
  362. const char *initresp)
  363. {
  364. CURLcode result = CURLE_OK;
  365. struct smtp_conn *smtpc = &conn->proto.smtpc;
  366. if(initresp) { /* AUTH <mech> ...<crlf> */
  367. /* Send the AUTH command with the initial response */
  368. result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp);
  369. }
  370. else {
  371. /* Send the AUTH command */
  372. result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech);
  373. }
  374. return result;
  375. }
  376. /***********************************************************************
  377. *
  378. * smtp_continue_auth()
  379. *
  380. * Sends SASL continuation data or cancellation.
  381. */
  382. static CURLcode smtp_continue_auth(struct connectdata *conn, const char *resp)
  383. {
  384. struct smtp_conn *smtpc = &conn->proto.smtpc;
  385. return Curl_pp_sendf(&smtpc->pp, "%s", resp);
  386. }
  387. /***********************************************************************
  388. *
  389. * smtp_perform_authentication()
  390. *
  391. * Initiates the authentication sequence, with the appropriate SASL
  392. * authentication mechanism.
  393. */
  394. static CURLcode smtp_perform_authentication(struct connectdata *conn)
  395. {
  396. CURLcode result = CURLE_OK;
  397. struct smtp_conn *smtpc = &conn->proto.smtpc;
  398. saslprogress progress;
  399. /* Check we have enough data to authenticate with, and the
  400. server supports authentiation, and end the connect phase if not */
  401. if(!smtpc->auth_supported ||
  402. !Curl_sasl_can_authenticate(&smtpc->sasl, conn)) {
  403. state(conn, SMTP_STOP);
  404. return result;
  405. }
  406. /* Calculate the SASL login details */
  407. result = Curl_sasl_start(&smtpc->sasl, conn, FALSE, &progress);
  408. if(!result) {
  409. if(progress == SASL_INPROGRESS)
  410. state(conn, SMTP_AUTH);
  411. else {
  412. /* Other mechanisms not supported */
  413. infof(conn->data, "No known authentication mechanisms supported!\n");
  414. result = CURLE_LOGIN_DENIED;
  415. }
  416. }
  417. return result;
  418. }
  419. /***********************************************************************
  420. *
  421. * smtp_perform_command()
  422. *
  423. * Sends a SMTP based command.
  424. */
  425. static CURLcode smtp_perform_command(struct connectdata *conn)
  426. {
  427. CURLcode result = CURLE_OK;
  428. struct Curl_easy *data = conn->data;
  429. struct SMTP *smtp = data->req.protop;
  430. /* Send the command */
  431. if(smtp->rcpt)
  432. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s %s",
  433. smtp->custom && smtp->custom[0] != '\0' ?
  434. smtp->custom : "VRFY",
  435. smtp->rcpt->data);
  436. else
  437. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s",
  438. smtp->custom && smtp->custom[0] != '\0' ?
  439. smtp->custom : "HELP");
  440. if(!result)
  441. state(conn, SMTP_COMMAND);
  442. return result;
  443. }
  444. /***********************************************************************
  445. *
  446. * smtp_perform_mail()
  447. *
  448. * Sends an MAIL command to initiate the upload of a message.
  449. */
  450. static CURLcode smtp_perform_mail(struct connectdata *conn)
  451. {
  452. char *from = NULL;
  453. char *auth = NULL;
  454. char *size = NULL;
  455. CURLcode result = CURLE_OK;
  456. struct Curl_easy *data = conn->data;
  457. /* Calculate the FROM parameter */
  458. if(!data->set.str[STRING_MAIL_FROM])
  459. /* Null reverse-path, RFC-5321, sect. 3.6.3 */
  460. from = strdup("<>");
  461. else if(data->set.str[STRING_MAIL_FROM][0] == '<')
  462. from = aprintf("%s", data->set.str[STRING_MAIL_FROM]);
  463. else
  464. from = aprintf("<%s>", data->set.str[STRING_MAIL_FROM]);
  465. if(!from)
  466. return CURLE_OUT_OF_MEMORY;
  467. /* Calculate the optional AUTH parameter */
  468. if(data->set.str[STRING_MAIL_AUTH] && conn->proto.smtpc.sasl.authused) {
  469. if(data->set.str[STRING_MAIL_AUTH][0] != '\0')
  470. auth = aprintf("%s", data->set.str[STRING_MAIL_AUTH]);
  471. else
  472. /* Empty AUTH, RFC-2554, sect. 5 */
  473. auth = strdup("<>");
  474. if(!auth) {
  475. free(from);
  476. return CURLE_OUT_OF_MEMORY;
  477. }
  478. }
  479. /* Prepare the mime data if some. */
  480. if(data->set.mimepost.kind != MIMEKIND_NONE) {
  481. /* Use the whole structure as data. */
  482. data->set.mimepost.flags &= ~MIME_BODY_ONLY;
  483. /* Add external headers and mime version. */
  484. curl_mime_headers(&data->set.mimepost, data->set.headers, 0);
  485. result = Curl_mime_prepare_headers(&data->set.mimepost, NULL,
  486. NULL, MIMESTRATEGY_MAIL);
  487. if(!result)
  488. if(!Curl_checkheaders(conn, "Mime-Version"))
  489. result = Curl_mime_add_header(&data->set.mimepost.curlheaders,
  490. "Mime-Version: 1.0");
  491. /* Make sure we will read the entire mime structure. */
  492. if(!result)
  493. result = Curl_mime_rewind(&data->set.mimepost);
  494. if(result) {
  495. free(from);
  496. free(auth);
  497. return result;
  498. }
  499. data->state.infilesize = Curl_mime_size(&data->set.mimepost);
  500. /* Read from mime structure. */
  501. data->state.fread_func = (curl_read_callback) Curl_mime_read;
  502. data->state.in = (void *) &data->set.mimepost;
  503. }
  504. /* Calculate the optional SIZE parameter */
  505. if(conn->proto.smtpc.size_supported && data->state.infilesize > 0) {
  506. size = aprintf("%" CURL_FORMAT_CURL_OFF_T, data->state.infilesize);
  507. if(!size) {
  508. free(from);
  509. free(auth);
  510. return CURLE_OUT_OF_MEMORY;
  511. }
  512. }
  513. /* Send the MAIL command */
  514. if(!auth && !size)
  515. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  516. "MAIL FROM:%s", from);
  517. else if(auth && !size)
  518. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  519. "MAIL FROM:%s AUTH=%s", from, auth);
  520. else if(auth && size)
  521. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  522. "MAIL FROM:%s AUTH=%s SIZE=%s", from, auth, size);
  523. else
  524. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  525. "MAIL FROM:%s SIZE=%s", from, size);
  526. free(from);
  527. free(auth);
  528. free(size);
  529. if(!result)
  530. state(conn, SMTP_MAIL);
  531. return result;
  532. }
  533. /***********************************************************************
  534. *
  535. * smtp_perform_rcpt_to()
  536. *
  537. * Sends a RCPT TO command for a given recipient as part of the message upload
  538. * process.
  539. */
  540. static CURLcode smtp_perform_rcpt_to(struct connectdata *conn)
  541. {
  542. CURLcode result = CURLE_OK;
  543. struct Curl_easy *data = conn->data;
  544. struct SMTP *smtp = data->req.protop;
  545. /* Send the RCPT TO command */
  546. if(smtp->rcpt->data[0] == '<')
  547. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:%s",
  548. smtp->rcpt->data);
  549. else
  550. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:<%s>",
  551. smtp->rcpt->data);
  552. if(!result)
  553. state(conn, SMTP_RCPT);
  554. return result;
  555. }
  556. /***********************************************************************
  557. *
  558. * smtp_perform_quit()
  559. *
  560. * Performs the quit action prior to sclose() being called.
  561. */
  562. static CURLcode smtp_perform_quit(struct connectdata *conn)
  563. {
  564. CURLcode result = CURLE_OK;
  565. /* Send the QUIT command */
  566. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "QUIT");
  567. if(!result)
  568. state(conn, SMTP_QUIT);
  569. return result;
  570. }
  571. /* For the initial server greeting */
  572. static CURLcode smtp_state_servergreet_resp(struct connectdata *conn,
  573. int smtpcode,
  574. smtpstate instate)
  575. {
  576. CURLcode result = CURLE_OK;
  577. struct Curl_easy *data = conn->data;
  578. (void)instate; /* no use for this yet */
  579. if(smtpcode/100 != 2) {
  580. failf(data, "Got unexpected smtp-server response: %d", smtpcode);
  581. result = CURLE_WEIRD_SERVER_REPLY;
  582. }
  583. else
  584. result = smtp_perform_ehlo(conn);
  585. return result;
  586. }
  587. /* For STARTTLS responses */
  588. static CURLcode smtp_state_starttls_resp(struct connectdata *conn,
  589. int smtpcode,
  590. smtpstate instate)
  591. {
  592. CURLcode result = CURLE_OK;
  593. struct Curl_easy *data = conn->data;
  594. (void)instate; /* no use for this yet */
  595. if(smtpcode != 220) {
  596. if(data->set.use_ssl != CURLUSESSL_TRY) {
  597. failf(data, "STARTTLS denied, code %d", smtpcode);
  598. result = CURLE_USE_SSL_FAILED;
  599. }
  600. else
  601. result = smtp_perform_authentication(conn);
  602. }
  603. else
  604. result = smtp_perform_upgrade_tls(conn);
  605. return result;
  606. }
  607. /* For EHLO responses */
  608. static CURLcode smtp_state_ehlo_resp(struct connectdata *conn, int smtpcode,
  609. smtpstate instate)
  610. {
  611. CURLcode result = CURLE_OK;
  612. struct Curl_easy *data = conn->data;
  613. struct smtp_conn *smtpc = &conn->proto.smtpc;
  614. const char *line = data->state.buffer;
  615. size_t len = strlen(line);
  616. (void)instate; /* no use for this yet */
  617. if(smtpcode/100 != 2 && smtpcode != 1) {
  618. if(data->set.use_ssl <= CURLUSESSL_TRY || conn->ssl[FIRSTSOCKET].use)
  619. result = smtp_perform_helo(conn);
  620. else {
  621. failf(data, "Remote access denied: %d", smtpcode);
  622. result = CURLE_REMOTE_ACCESS_DENIED;
  623. }
  624. }
  625. else {
  626. line += 4;
  627. len -= 4;
  628. /* Does the server support the STARTTLS capability? */
  629. if(len >= 8 && !memcmp(line, "STARTTLS", 8))
  630. smtpc->tls_supported = TRUE;
  631. /* Does the server support the SIZE capability? */
  632. else if(len >= 4 && !memcmp(line, "SIZE", 4))
  633. smtpc->size_supported = TRUE;
  634. /* Does the server support authentication? */
  635. else if(len >= 5 && !memcmp(line, "AUTH ", 5)) {
  636. smtpc->auth_supported = TRUE;
  637. /* Advance past the AUTH keyword */
  638. line += 5;
  639. len -= 5;
  640. /* Loop through the data line */
  641. for(;;) {
  642. size_t llen;
  643. size_t wordlen;
  644. unsigned int mechbit;
  645. while(len &&
  646. (*line == ' ' || *line == '\t' ||
  647. *line == '\r' || *line == '\n')) {
  648. line++;
  649. len--;
  650. }
  651. if(!len)
  652. break;
  653. /* Extract the word */
  654. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  655. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  656. line[wordlen] != '\n';)
  657. wordlen++;
  658. /* Test the word for a matching authentication mechanism */
  659. mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
  660. if(mechbit && llen == wordlen)
  661. smtpc->sasl.authmechs |= mechbit;
  662. line += wordlen;
  663. len -= wordlen;
  664. }
  665. }
  666. if(smtpcode != 1) {
  667. if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
  668. /* We don't have a SSL/TLS connection yet, but SSL is requested */
  669. if(smtpc->tls_supported)
  670. /* Switch to TLS connection now */
  671. result = smtp_perform_starttls(conn);
  672. else if(data->set.use_ssl == CURLUSESSL_TRY)
  673. /* Fallback and carry on with authentication */
  674. result = smtp_perform_authentication(conn);
  675. else {
  676. failf(data, "STARTTLS not supported.");
  677. result = CURLE_USE_SSL_FAILED;
  678. }
  679. }
  680. else
  681. result = smtp_perform_authentication(conn);
  682. }
  683. }
  684. return result;
  685. }
  686. /* For HELO responses */
  687. static CURLcode smtp_state_helo_resp(struct connectdata *conn, int smtpcode,
  688. smtpstate instate)
  689. {
  690. CURLcode result = CURLE_OK;
  691. struct Curl_easy *data = conn->data;
  692. (void)instate; /* no use for this yet */
  693. if(smtpcode/100 != 2) {
  694. failf(data, "Remote access denied: %d", smtpcode);
  695. result = CURLE_REMOTE_ACCESS_DENIED;
  696. }
  697. else
  698. /* End of connect phase */
  699. state(conn, SMTP_STOP);
  700. return result;
  701. }
  702. /* For SASL authentication responses */
  703. static CURLcode smtp_state_auth_resp(struct connectdata *conn,
  704. int smtpcode,
  705. smtpstate instate)
  706. {
  707. CURLcode result = CURLE_OK;
  708. struct Curl_easy *data = conn->data;
  709. struct smtp_conn *smtpc = &conn->proto.smtpc;
  710. saslprogress progress;
  711. (void)instate; /* no use for this yet */
  712. result = Curl_sasl_continue(&smtpc->sasl, conn, smtpcode, &progress);
  713. if(!result)
  714. switch(progress) {
  715. case SASL_DONE:
  716. state(conn, SMTP_STOP); /* Authenticated */
  717. break;
  718. case SASL_IDLE: /* No mechanism left after cancellation */
  719. failf(data, "Authentication cancelled");
  720. result = CURLE_LOGIN_DENIED;
  721. break;
  722. default:
  723. break;
  724. }
  725. return result;
  726. }
  727. /* For command responses */
  728. static CURLcode smtp_state_command_resp(struct connectdata *conn, int smtpcode,
  729. smtpstate instate)
  730. {
  731. CURLcode result = CURLE_OK;
  732. struct Curl_easy *data = conn->data;
  733. struct SMTP *smtp = data->req.protop;
  734. char *line = data->state.buffer;
  735. size_t len = strlen(line);
  736. (void)instate; /* no use for this yet */
  737. if((smtp->rcpt && smtpcode/100 != 2 && smtpcode != 553 && smtpcode != 1) ||
  738. (!smtp->rcpt && smtpcode/100 != 2 && smtpcode != 1)) {
  739. failf(data, "Command failed: %d", smtpcode);
  740. result = CURLE_RECV_ERROR;
  741. }
  742. else {
  743. /* Temporarily add the LF character back and send as body to the client */
  744. if(!data->set.opt_no_body) {
  745. line[len] = '\n';
  746. result = Curl_client_write(conn, CLIENTWRITE_BODY, line, len + 1);
  747. line[len] = '\0';
  748. }
  749. if(smtpcode != 1) {
  750. if(smtp->rcpt) {
  751. smtp->rcpt = smtp->rcpt->next;
  752. if(smtp->rcpt) {
  753. /* Send the next command */
  754. result = smtp_perform_command(conn);
  755. }
  756. else
  757. /* End of DO phase */
  758. state(conn, SMTP_STOP);
  759. }
  760. else
  761. /* End of DO phase */
  762. state(conn, SMTP_STOP);
  763. }
  764. }
  765. return result;
  766. }
  767. /* For MAIL responses */
  768. static CURLcode smtp_state_mail_resp(struct connectdata *conn, int smtpcode,
  769. smtpstate instate)
  770. {
  771. CURLcode result = CURLE_OK;
  772. struct Curl_easy *data = conn->data;
  773. (void)instate; /* no use for this yet */
  774. if(smtpcode/100 != 2) {
  775. failf(data, "MAIL failed: %d", smtpcode);
  776. result = CURLE_SEND_ERROR;
  777. }
  778. else
  779. /* Start the RCPT TO command */
  780. result = smtp_perform_rcpt_to(conn);
  781. return result;
  782. }
  783. /* For RCPT responses */
  784. static CURLcode smtp_state_rcpt_resp(struct connectdata *conn, int smtpcode,
  785. smtpstate instate)
  786. {
  787. CURLcode result = CURLE_OK;
  788. struct Curl_easy *data = conn->data;
  789. struct SMTP *smtp = data->req.protop;
  790. (void)instate; /* no use for this yet */
  791. if(smtpcode/100 != 2) {
  792. failf(data, "RCPT failed: %d", smtpcode);
  793. result = CURLE_SEND_ERROR;
  794. }
  795. else {
  796. smtp->rcpt = smtp->rcpt->next;
  797. if(smtp->rcpt)
  798. /* Send the next RCPT TO command */
  799. result = smtp_perform_rcpt_to(conn);
  800. else {
  801. /* Send the DATA command */
  802. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "DATA");
  803. if(!result)
  804. state(conn, SMTP_DATA);
  805. }
  806. }
  807. return result;
  808. }
  809. /* For DATA response */
  810. static CURLcode smtp_state_data_resp(struct connectdata *conn, int smtpcode,
  811. smtpstate instate)
  812. {
  813. CURLcode result = CURLE_OK;
  814. struct Curl_easy *data = conn->data;
  815. (void)instate; /* no use for this yet */
  816. if(smtpcode != 354) {
  817. failf(data, "DATA failed: %d", smtpcode);
  818. result = CURLE_SEND_ERROR;
  819. }
  820. else {
  821. /* Set the progress upload size */
  822. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  823. /* SMTP upload */
  824. Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
  825. /* End of DO phase */
  826. state(conn, SMTP_STOP);
  827. }
  828. return result;
  829. }
  830. /* For POSTDATA responses, which are received after the entire DATA
  831. part has been sent to the server */
  832. static CURLcode smtp_state_postdata_resp(struct connectdata *conn,
  833. int smtpcode,
  834. smtpstate instate)
  835. {
  836. CURLcode result = CURLE_OK;
  837. (void)instate; /* no use for this yet */
  838. if(smtpcode != 250)
  839. result = CURLE_RECV_ERROR;
  840. /* End of DONE phase */
  841. state(conn, SMTP_STOP);
  842. return result;
  843. }
  844. static CURLcode smtp_statemach_act(struct connectdata *conn)
  845. {
  846. CURLcode result = CURLE_OK;
  847. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  848. struct Curl_easy *data = conn->data;
  849. int smtpcode;
  850. struct smtp_conn *smtpc = &conn->proto.smtpc;
  851. struct pingpong *pp = &smtpc->pp;
  852. size_t nread = 0;
  853. /* Busy upgrading the connection; right now all I/O is SSL/TLS, not SMTP */
  854. if(smtpc->state == SMTP_UPGRADETLS)
  855. return smtp_perform_upgrade_tls(conn);
  856. /* Flush any data that needs to be sent */
  857. if(pp->sendleft)
  858. return Curl_pp_flushsend(pp);
  859. do {
  860. /* Read the response from the server */
  861. result = Curl_pp_readresp(sock, pp, &smtpcode, &nread);
  862. if(result)
  863. return result;
  864. /* Store the latest response for later retrieval if necessary */
  865. if(smtpc->state != SMTP_QUIT && smtpcode != 1)
  866. data->info.httpcode = smtpcode;
  867. if(!smtpcode)
  868. break;
  869. /* We have now received a full SMTP server response */
  870. switch(smtpc->state) {
  871. case SMTP_SERVERGREET:
  872. result = smtp_state_servergreet_resp(conn, smtpcode, smtpc->state);
  873. break;
  874. case SMTP_EHLO:
  875. result = smtp_state_ehlo_resp(conn, smtpcode, smtpc->state);
  876. break;
  877. case SMTP_HELO:
  878. result = smtp_state_helo_resp(conn, smtpcode, smtpc->state);
  879. break;
  880. case SMTP_STARTTLS:
  881. result = smtp_state_starttls_resp(conn, smtpcode, smtpc->state);
  882. break;
  883. case SMTP_AUTH:
  884. result = smtp_state_auth_resp(conn, smtpcode, smtpc->state);
  885. break;
  886. case SMTP_COMMAND:
  887. result = smtp_state_command_resp(conn, smtpcode, smtpc->state);
  888. break;
  889. case SMTP_MAIL:
  890. result = smtp_state_mail_resp(conn, smtpcode, smtpc->state);
  891. break;
  892. case SMTP_RCPT:
  893. result = smtp_state_rcpt_resp(conn, smtpcode, smtpc->state);
  894. break;
  895. case SMTP_DATA:
  896. result = smtp_state_data_resp(conn, smtpcode, smtpc->state);
  897. break;
  898. case SMTP_POSTDATA:
  899. result = smtp_state_postdata_resp(conn, smtpcode, smtpc->state);
  900. break;
  901. case SMTP_QUIT:
  902. /* fallthrough, just stop! */
  903. default:
  904. /* internal error */
  905. state(conn, SMTP_STOP);
  906. break;
  907. }
  908. } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp));
  909. return result;
  910. }
  911. /* Called repeatedly until done from multi.c */
  912. static CURLcode smtp_multi_statemach(struct connectdata *conn, bool *done)
  913. {
  914. CURLcode result = CURLE_OK;
  915. struct smtp_conn *smtpc = &conn->proto.smtpc;
  916. if((conn->handler->flags & PROTOPT_SSL) && !smtpc->ssldone) {
  917. result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &smtpc->ssldone);
  918. if(result || !smtpc->ssldone)
  919. return result;
  920. }
  921. result = Curl_pp_statemach(&smtpc->pp, FALSE, FALSE);
  922. *done = (smtpc->state == SMTP_STOP) ? TRUE : FALSE;
  923. return result;
  924. }
  925. static CURLcode smtp_block_statemach(struct connectdata *conn,
  926. bool disconnecting)
  927. {
  928. CURLcode result = CURLE_OK;
  929. struct smtp_conn *smtpc = &conn->proto.smtpc;
  930. while(smtpc->state != SMTP_STOP && !result)
  931. result = Curl_pp_statemach(&smtpc->pp, TRUE, disconnecting);
  932. return result;
  933. }
  934. /* Allocate and initialize the SMTP struct for the current Curl_easy if
  935. required */
  936. static CURLcode smtp_init(struct connectdata *conn)
  937. {
  938. CURLcode result = CURLE_OK;
  939. struct Curl_easy *data = conn->data;
  940. struct SMTP *smtp;
  941. smtp = data->req.protop = calloc(sizeof(struct SMTP), 1);
  942. if(!smtp)
  943. result = CURLE_OUT_OF_MEMORY;
  944. return result;
  945. }
  946. /* For the SMTP "protocol connect" and "doing" phases only */
  947. static int smtp_getsock(struct connectdata *conn, curl_socket_t *socks,
  948. int numsocks)
  949. {
  950. return Curl_pp_getsock(&conn->proto.smtpc.pp, socks, numsocks);
  951. }
  952. /***********************************************************************
  953. *
  954. * smtp_connect()
  955. *
  956. * This function should do everything that is to be considered a part of
  957. * the connection phase.
  958. *
  959. * The variable pointed to by 'done' will be TRUE if the protocol-layer
  960. * connect phase is done when this function returns, or FALSE if not.
  961. */
  962. static CURLcode smtp_connect(struct connectdata *conn, bool *done)
  963. {
  964. CURLcode result = CURLE_OK;
  965. struct smtp_conn *smtpc = &conn->proto.smtpc;
  966. struct pingpong *pp = &smtpc->pp;
  967. *done = FALSE; /* default to not done yet */
  968. /* We always support persistent connections in SMTP */
  969. connkeep(conn, "SMTP default");
  970. /* Set the default response time-out */
  971. pp->response_time = RESP_TIMEOUT;
  972. pp->statemach_act = smtp_statemach_act;
  973. pp->endofresp = smtp_endofresp;
  974. pp->conn = conn;
  975. /* Initialize the SASL storage */
  976. Curl_sasl_init(&smtpc->sasl, &saslsmtp);
  977. /* Initialise the pingpong layer */
  978. Curl_pp_init(pp);
  979. /* Parse the URL options */
  980. result = smtp_parse_url_options(conn);
  981. if(result)
  982. return result;
  983. /* Parse the URL path */
  984. result = smtp_parse_url_path(conn);
  985. if(result)
  986. return result;
  987. /* Start off waiting for the server greeting response */
  988. state(conn, SMTP_SERVERGREET);
  989. result = smtp_multi_statemach(conn, done);
  990. return result;
  991. }
  992. /***********************************************************************
  993. *
  994. * smtp_done()
  995. *
  996. * The DONE function. This does what needs to be done after a single DO has
  997. * performed.
  998. *
  999. * Input argument is already checked for validity.
  1000. */
  1001. static CURLcode smtp_done(struct connectdata *conn, CURLcode status,
  1002. bool premature)
  1003. {
  1004. CURLcode result = CURLE_OK;
  1005. struct Curl_easy *data = conn->data;
  1006. struct SMTP *smtp = data->req.protop;
  1007. struct pingpong *pp = &conn->proto.smtpc.pp;
  1008. char *eob;
  1009. ssize_t len;
  1010. ssize_t bytes_written;
  1011. (void)premature;
  1012. if(!smtp || !pp->conn)
  1013. return CURLE_OK;
  1014. /* Cleanup our per-request based variables */
  1015. Curl_safefree(smtp->custom);
  1016. if(status) {
  1017. connclose(conn, "SMTP done with bad status"); /* marked for closure */
  1018. result = status; /* use the already set error code */
  1019. }
  1020. else if(!data->set.connect_only && data->set.mail_rcpt &&
  1021. (data->set.upload || data->set.mimepost.kind)) {
  1022. /* Calculate the EOB taking into account any terminating CRLF from the
  1023. previous line of the email or the CRLF of the DATA command when there
  1024. is "no mail data". RFC-5321, sect. 4.1.1.4.
  1025. Note: As some SSL backends, such as OpenSSL, will cause Curl_write() to
  1026. fail when using a different pointer following a previous write, that
  1027. returned CURLE_AGAIN, we duplicate the EOB now rather than when the
  1028. bytes written doesn't equal len. */
  1029. if(smtp->trailing_crlf || !conn->data->state.infilesize) {
  1030. eob = strdup(&SMTP_EOB[2]);
  1031. len = SMTP_EOB_LEN - 2;
  1032. }
  1033. else {
  1034. eob = strdup(SMTP_EOB);
  1035. len = SMTP_EOB_LEN;
  1036. }
  1037. if(!eob)
  1038. return CURLE_OUT_OF_MEMORY;
  1039. /* Send the end of block data */
  1040. result = Curl_write(conn, conn->writesockfd, eob, len, &bytes_written);
  1041. if(result) {
  1042. free(eob);
  1043. return result;
  1044. }
  1045. if(bytes_written != len) {
  1046. /* The whole chunk was not sent so keep it around and adjust the
  1047. pingpong structure accordingly */
  1048. pp->sendthis = eob;
  1049. pp->sendsize = len;
  1050. pp->sendleft = len - bytes_written;
  1051. }
  1052. else {
  1053. /* Successfully sent so adjust the response timeout relative to now */
  1054. pp->response = Curl_now();
  1055. free(eob);
  1056. }
  1057. state(conn, SMTP_POSTDATA);
  1058. /* Run the state-machine
  1059. TODO: when the multi interface is used, this _really_ should be using
  1060. the smtp_multi_statemach function but we have no general support for
  1061. non-blocking DONE operations!
  1062. */
  1063. result = smtp_block_statemach(conn, FALSE);
  1064. }
  1065. /* Clear the transfer mode for the next request */
  1066. smtp->transfer = FTPTRANSFER_BODY;
  1067. return result;
  1068. }
  1069. /***********************************************************************
  1070. *
  1071. * smtp_perform()
  1072. *
  1073. * This is the actual DO function for SMTP. Transfer a mail, send a command
  1074. * or get some data according to the options previously setup.
  1075. */
  1076. static CURLcode smtp_perform(struct connectdata *conn, bool *connected,
  1077. bool *dophase_done)
  1078. {
  1079. /* This is SMTP and no proxy */
  1080. CURLcode result = CURLE_OK;
  1081. struct Curl_easy *data = conn->data;
  1082. struct SMTP *smtp = data->req.protop;
  1083. DEBUGF(infof(conn->data, "DO phase starts\n"));
  1084. if(data->set.opt_no_body) {
  1085. /* Requested no body means no transfer */
  1086. smtp->transfer = FTPTRANSFER_INFO;
  1087. }
  1088. *dophase_done = FALSE; /* not done yet */
  1089. /* Store the first recipient (or NULL if not specified) */
  1090. smtp->rcpt = data->set.mail_rcpt;
  1091. /* Initial data character is the first character in line: it is implicitly
  1092. preceded by a virtual CRLF. */
  1093. smtp->trailing_crlf = TRUE;
  1094. smtp->eob = 2;
  1095. /* Start the first command in the DO phase */
  1096. if((data->set.upload || data->set.mimepost.kind) && data->set.mail_rcpt)
  1097. /* MAIL transfer */
  1098. result = smtp_perform_mail(conn);
  1099. else
  1100. /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
  1101. result = smtp_perform_command(conn);
  1102. if(result)
  1103. return result;
  1104. /* Run the state-machine */
  1105. result = smtp_multi_statemach(conn, dophase_done);
  1106. *connected = conn->bits.tcpconnect[FIRSTSOCKET];
  1107. if(*dophase_done)
  1108. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1109. return result;
  1110. }
  1111. /***********************************************************************
  1112. *
  1113. * smtp_do()
  1114. *
  1115. * This function is registered as 'curl_do' function. It decodes the path
  1116. * parts etc as a wrapper to the actual DO function (smtp_perform).
  1117. *
  1118. * The input argument is already checked for validity.
  1119. */
  1120. static CURLcode smtp_do(struct connectdata *conn, bool *done)
  1121. {
  1122. CURLcode result = CURLE_OK;
  1123. *done = FALSE; /* default to false */
  1124. /* Parse the custom request */
  1125. result = smtp_parse_custom_request(conn);
  1126. if(result)
  1127. return result;
  1128. result = smtp_regular_transfer(conn, done);
  1129. return result;
  1130. }
  1131. /***********************************************************************
  1132. *
  1133. * smtp_disconnect()
  1134. *
  1135. * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
  1136. * resources. BLOCKING.
  1137. */
  1138. static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection)
  1139. {
  1140. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1141. /* We cannot send quit unconditionally. If this connection is stale or
  1142. bad in any way, sending quit and waiting around here will make the
  1143. disconnect wait in vain and cause more problems than we need to. */
  1144. /* The SMTP session may or may not have been allocated/setup at this
  1145. point! */
  1146. if(!dead_connection && smtpc->pp.conn && smtpc->pp.conn->bits.protoconnstart)
  1147. if(!smtp_perform_quit(conn))
  1148. (void)smtp_block_statemach(conn, TRUE); /* ignore errors on QUIT */
  1149. /* Disconnect from the server */
  1150. Curl_pp_disconnect(&smtpc->pp);
  1151. /* Cleanup the SASL module */
  1152. Curl_sasl_cleanup(conn, smtpc->sasl.authused);
  1153. /* Cleanup our connection based variables */
  1154. Curl_safefree(smtpc->domain);
  1155. return CURLE_OK;
  1156. }
  1157. /* Call this when the DO phase has completed */
  1158. static CURLcode smtp_dophase_done(struct connectdata *conn, bool connected)
  1159. {
  1160. struct SMTP *smtp = conn->data->req.protop;
  1161. (void)connected;
  1162. if(smtp->transfer != FTPTRANSFER_BODY)
  1163. /* no data to transfer */
  1164. Curl_setup_transfer(conn->data, -1, -1, FALSE, -1);
  1165. return CURLE_OK;
  1166. }
  1167. /* Called from multi.c while DOing */
  1168. static CURLcode smtp_doing(struct connectdata *conn, bool *dophase_done)
  1169. {
  1170. CURLcode result = smtp_multi_statemach(conn, dophase_done);
  1171. if(result)
  1172. DEBUGF(infof(conn->data, "DO phase failed\n"));
  1173. else if(*dophase_done) {
  1174. result = smtp_dophase_done(conn, FALSE /* not connected */);
  1175. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1176. }
  1177. return result;
  1178. }
  1179. /***********************************************************************
  1180. *
  1181. * smtp_regular_transfer()
  1182. *
  1183. * The input argument is already checked for validity.
  1184. *
  1185. * Performs all commands done before a regular transfer between a local and a
  1186. * remote host.
  1187. */
  1188. static CURLcode smtp_regular_transfer(struct connectdata *conn,
  1189. bool *dophase_done)
  1190. {
  1191. CURLcode result = CURLE_OK;
  1192. bool connected = FALSE;
  1193. struct Curl_easy *data = conn->data;
  1194. /* Make sure size is unknown at this point */
  1195. data->req.size = -1;
  1196. /* Set the progress data */
  1197. Curl_pgrsSetUploadCounter(data, 0);
  1198. Curl_pgrsSetDownloadCounter(data, 0);
  1199. Curl_pgrsSetUploadSize(data, -1);
  1200. Curl_pgrsSetDownloadSize(data, -1);
  1201. /* Carry out the perform */
  1202. result = smtp_perform(conn, &connected, dophase_done);
  1203. /* Perform post DO phase operations if necessary */
  1204. if(!result && *dophase_done)
  1205. result = smtp_dophase_done(conn, connected);
  1206. return result;
  1207. }
  1208. static CURLcode smtp_setup_connection(struct connectdata *conn)
  1209. {
  1210. CURLcode result;
  1211. /* Clear the TLS upgraded flag */
  1212. conn->tls_upgraded = FALSE;
  1213. /* Initialise the SMTP layer */
  1214. result = smtp_init(conn);
  1215. if(result)
  1216. return result;
  1217. return CURLE_OK;
  1218. }
  1219. /***********************************************************************
  1220. *
  1221. * smtp_parse_url_options()
  1222. *
  1223. * Parse the URL login options.
  1224. */
  1225. static CURLcode smtp_parse_url_options(struct connectdata *conn)
  1226. {
  1227. CURLcode result = CURLE_OK;
  1228. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1229. const char *ptr = conn->options;
  1230. smtpc->sasl.resetprefs = TRUE;
  1231. while(!result && ptr && *ptr) {
  1232. const char *key = ptr;
  1233. const char *value;
  1234. while(*ptr && *ptr != '=')
  1235. ptr++;
  1236. value = ptr + 1;
  1237. while(*ptr && *ptr != ';')
  1238. ptr++;
  1239. if(strncasecompare(key, "AUTH=", 5))
  1240. result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
  1241. value, ptr - value);
  1242. else
  1243. result = CURLE_URL_MALFORMAT;
  1244. if(*ptr == ';')
  1245. ptr++;
  1246. }
  1247. return result;
  1248. }
  1249. /***********************************************************************
  1250. *
  1251. * smtp_parse_url_path()
  1252. *
  1253. * Parse the URL path into separate path components.
  1254. */
  1255. static CURLcode smtp_parse_url_path(struct connectdata *conn)
  1256. {
  1257. /* The SMTP struct is already initialised in smtp_connect() */
  1258. struct Curl_easy *data = conn->data;
  1259. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1260. const char *path = &data->state.up.path[1]; /* skip leading path */
  1261. char localhost[HOSTNAME_MAX + 1];
  1262. /* Calculate the path if necessary */
  1263. if(!*path) {
  1264. if(!Curl_gethostname(localhost, sizeof(localhost)))
  1265. path = localhost;
  1266. else
  1267. path = "localhost";
  1268. }
  1269. /* URL decode the path and use it as the domain in our EHLO */
  1270. return Curl_urldecode(conn->data, path, 0, &smtpc->domain, NULL, TRUE);
  1271. }
  1272. /***********************************************************************
  1273. *
  1274. * smtp_parse_custom_request()
  1275. *
  1276. * Parse the custom request.
  1277. */
  1278. static CURLcode smtp_parse_custom_request(struct connectdata *conn)
  1279. {
  1280. CURLcode result = CURLE_OK;
  1281. struct Curl_easy *data = conn->data;
  1282. struct SMTP *smtp = data->req.protop;
  1283. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1284. /* URL decode the custom request */
  1285. if(custom)
  1286. result = Curl_urldecode(data, custom, 0, &smtp->custom, NULL, TRUE);
  1287. return result;
  1288. }
  1289. CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread)
  1290. {
  1291. /* When sending a SMTP payload we must detect CRLF. sequences making sure
  1292. they are sent as CRLF.. instead, as a . on the beginning of a line will
  1293. be deleted by the server when not part of an EOB terminator and a
  1294. genuine CRLF.CRLF which isn't escaped will wrongly be detected as end of
  1295. data by the server
  1296. */
  1297. ssize_t i;
  1298. ssize_t si;
  1299. struct Curl_easy *data = conn->data;
  1300. struct SMTP *smtp = data->req.protop;
  1301. char *scratch = data->state.scratch;
  1302. char *newscratch = NULL;
  1303. char *oldscratch = NULL;
  1304. size_t eob_sent;
  1305. /* Do we need to allocate a scratch buffer? */
  1306. if(!scratch || data->set.crlf) {
  1307. oldscratch = scratch;
  1308. scratch = newscratch = malloc(2 * data->set.upload_buffer_size);
  1309. if(!newscratch) {
  1310. failf(data, "Failed to alloc scratch buffer!");
  1311. return CURLE_OUT_OF_MEMORY;
  1312. }
  1313. }
  1314. DEBUGASSERT(data->set.upload_buffer_size >= (size_t)nread);
  1315. /* Have we already sent part of the EOB? */
  1316. eob_sent = smtp->eob;
  1317. /* This loop can be improved by some kind of Boyer-Moore style of
  1318. approach but that is saved for later... */
  1319. for(i = 0, si = 0; i < nread; i++) {
  1320. if(SMTP_EOB[smtp->eob] == data->req.upload_fromhere[i]) {
  1321. smtp->eob++;
  1322. /* Is the EOB potentially the terminating CRLF? */
  1323. if(2 == smtp->eob || SMTP_EOB_LEN == smtp->eob)
  1324. smtp->trailing_crlf = TRUE;
  1325. else
  1326. smtp->trailing_crlf = FALSE;
  1327. }
  1328. else if(smtp->eob) {
  1329. /* A previous substring matched so output that first */
  1330. memcpy(&scratch[si], &SMTP_EOB[eob_sent], smtp->eob - eob_sent);
  1331. si += smtp->eob - eob_sent;
  1332. /* Then compare the first byte */
  1333. if(SMTP_EOB[0] == data->req.upload_fromhere[i])
  1334. smtp->eob = 1;
  1335. else
  1336. smtp->eob = 0;
  1337. eob_sent = 0;
  1338. /* Reset the trailing CRLF flag as there was more data */
  1339. smtp->trailing_crlf = FALSE;
  1340. }
  1341. /* Do we have a match for CRLF. as per RFC-5321, sect. 4.5.2 */
  1342. if(SMTP_EOB_FIND_LEN == smtp->eob) {
  1343. /* Copy the replacement data to the target buffer */
  1344. memcpy(&scratch[si], &SMTP_EOB_REPL[eob_sent],
  1345. SMTP_EOB_REPL_LEN - eob_sent);
  1346. si += SMTP_EOB_REPL_LEN - eob_sent;
  1347. smtp->eob = 0;
  1348. eob_sent = 0;
  1349. }
  1350. else if(!smtp->eob)
  1351. scratch[si++] = data->req.upload_fromhere[i];
  1352. }
  1353. if(smtp->eob - eob_sent) {
  1354. /* A substring matched before processing ended so output that now */
  1355. memcpy(&scratch[si], &SMTP_EOB[eob_sent], smtp->eob - eob_sent);
  1356. si += smtp->eob - eob_sent;
  1357. }
  1358. /* Only use the new buffer if we replaced something */
  1359. if(si != nread) {
  1360. /* Upload from the new (replaced) buffer instead */
  1361. data->req.upload_fromhere = scratch;
  1362. /* Save the buffer so it can be freed later */
  1363. data->state.scratch = scratch;
  1364. /* Free the old scratch buffer */
  1365. free(oldscratch);
  1366. /* Set the new amount too */
  1367. data->req.upload_present = si;
  1368. }
  1369. else
  1370. free(newscratch);
  1371. return CURLE_OK;
  1372. }
  1373. #endif /* CURL_DISABLE_SMTP */