2
0

smtp.c 57 KB

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