smtp.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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_NETDB_H
  50. #include <netdb.h>
  51. #endif
  52. #ifdef __VMS
  53. #include <in.h>
  54. #include <inet.h>
  55. #endif
  56. #include <curl/curl.h>
  57. #include "urldata.h"
  58. #include "sendf.h"
  59. #include "hostip.h"
  60. #include "progress.h"
  61. #include "transfer.h"
  62. #include "escape.h"
  63. #include "http.h" /* for HTTP proxy tunnel stuff */
  64. #include "mime.h"
  65. #include "socks.h"
  66. #include "smtp.h"
  67. #include "strtoofft.h"
  68. #include "strcase.h"
  69. #include "vtls/vtls.h"
  70. #include "cfilters.h"
  71. #include "connect.h"
  72. #include "select.h"
  73. #include "multiif.h"
  74. #include "url.h"
  75. #include "curl_gethostname.h"
  76. #include "bufref.h"
  77. #include "curl_sasl.h"
  78. #include "warnless.h"
  79. #include "idn.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 Curl_easy *data, bool *done);
  86. static CURLcode smtp_do(struct Curl_easy *data, bool *done);
  87. static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
  88. bool premature);
  89. static CURLcode smtp_connect(struct Curl_easy *data, bool *done);
  90. static CURLcode smtp_disconnect(struct Curl_easy *data,
  91. struct connectdata *conn, bool dead);
  92. static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done);
  93. static int smtp_getsock(struct Curl_easy *data,
  94. struct connectdata *conn, curl_socket_t *socks);
  95. static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done);
  96. static CURLcode smtp_setup_connection(struct Curl_easy *data,
  97. struct connectdata *conn);
  98. static CURLcode smtp_parse_url_options(struct connectdata *conn);
  99. static CURLcode smtp_parse_url_path(struct Curl_easy *data);
  100. static CURLcode smtp_parse_custom_request(struct Curl_easy *data);
  101. static CURLcode smtp_parse_address(const char *fqma,
  102. char **address, struct hostname *host);
  103. static CURLcode smtp_perform_auth(struct Curl_easy *data, const char *mech,
  104. const struct bufref *initresp);
  105. static CURLcode smtp_continue_auth(struct Curl_easy *data, const char *mech,
  106. const struct bufref *resp);
  107. static CURLcode smtp_cancel_auth(struct Curl_easy *data, const char *mech);
  108. static CURLcode smtp_get_message(struct Curl_easy *data, struct bufref *out);
  109. static CURLcode cr_eob_add(struct Curl_easy *data);
  110. /*
  111. * SMTP protocol handler.
  112. */
  113. const struct Curl_handler Curl_handler_smtp = {
  114. "smtp", /* scheme */
  115. smtp_setup_connection, /* setup_connection */
  116. smtp_do, /* do_it */
  117. smtp_done, /* done */
  118. ZERO_NULL, /* do_more */
  119. smtp_connect, /* connect_it */
  120. smtp_multi_statemach, /* connecting */
  121. smtp_doing, /* doing */
  122. smtp_getsock, /* proto_getsock */
  123. smtp_getsock, /* doing_getsock */
  124. ZERO_NULL, /* domore_getsock */
  125. ZERO_NULL, /* perform_getsock */
  126. smtp_disconnect, /* disconnect */
  127. ZERO_NULL, /* write_resp */
  128. ZERO_NULL, /* write_resp_hd */
  129. ZERO_NULL, /* connection_check */
  130. ZERO_NULL, /* attach connection */
  131. PORT_SMTP, /* defport */
  132. CURLPROTO_SMTP, /* protocol */
  133. CURLPROTO_SMTP, /* family */
  134. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */
  135. PROTOPT_URLOPTIONS
  136. };
  137. #ifdef USE_SSL
  138. /*
  139. * SMTPS protocol handler.
  140. */
  141. const struct Curl_handler Curl_handler_smtps = {
  142. "smtps", /* scheme */
  143. smtp_setup_connection, /* setup_connection */
  144. smtp_do, /* do_it */
  145. smtp_done, /* done */
  146. ZERO_NULL, /* do_more */
  147. smtp_connect, /* connect_it */
  148. smtp_multi_statemach, /* connecting */
  149. smtp_doing, /* doing */
  150. smtp_getsock, /* proto_getsock */
  151. smtp_getsock, /* doing_getsock */
  152. ZERO_NULL, /* domore_getsock */
  153. ZERO_NULL, /* perform_getsock */
  154. smtp_disconnect, /* disconnect */
  155. ZERO_NULL, /* write_resp */
  156. ZERO_NULL, /* write_resp_hd */
  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 = Curl_dyn_ptr(&data->conn->proto.smtpc.pp.recvbuf);
  238. size_t len = data->conn->proto.smtpc.pp.nfinal;
  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(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. CURL_TRC_SMTP(data, "state change from %s to %s",
  287. 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 hostname parts. This is regardless of
  479. whether the hostname 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 hostname
  485. parts, converting the hostname 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 hostname 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 hostname parts. This is regardless of
  542. whether the hostname 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 hostname parts,
  549. converting the hostname 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. goto out;
  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 will simply let the server
  565. worry 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. result = CURLE_OUT_OF_MEMORY;
  574. goto out;
  575. }
  576. /* Calculate the optional AUTH parameter */
  577. if(data->set.str[STRING_MAIL_AUTH] && conn->proto.smtpc.sasl.authused) {
  578. if(data->set.str[STRING_MAIL_AUTH][0] != '\0') {
  579. char *address = NULL;
  580. struct hostname host = { NULL, NULL, NULL, NULL };
  581. /* Parse the AUTH mailbox into the local address and hostname parts,
  582. converting the hostname to an IDN A-label if necessary */
  583. result = smtp_parse_address(data->set.str[STRING_MAIL_AUTH],
  584. &address, &host);
  585. if(result)
  586. goto out;
  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 will 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. result = CURLE_OUT_OF_MEMORY;
  608. goto out;
  609. }
  610. }
  611. #ifndef CURL_DISABLE_MIME
  612. /* Prepare the mime data if some. */
  613. if(data->set.mimepost.kind != MIMEKIND_NONE) {
  614. /* Use the whole structure as data. */
  615. data->set.mimepost.flags &= ~(unsigned int)MIME_BODY_ONLY;
  616. /* Add external headers and mime version. */
  617. curl_mime_headers(&data->set.mimepost, data->set.headers, 0);
  618. result = Curl_mime_prepare_headers(data, &data->set.mimepost, NULL,
  619. NULL, MIMESTRATEGY_MAIL);
  620. if(!result)
  621. if(!Curl_checkheaders(data, STRCONST("Mime-Version")))
  622. result = Curl_mime_add_header(&data->set.mimepost.curlheaders,
  623. "Mime-Version: 1.0");
  624. if(!result)
  625. result = Curl_creader_set_mime(data, &data->set.mimepost);
  626. if(result)
  627. goto out;
  628. data->state.infilesize = Curl_creader_total_length(data);
  629. }
  630. else
  631. #endif
  632. {
  633. result = Curl_creader_set_fread(data, data->state.infilesize);
  634. if(result)
  635. goto out;
  636. }
  637. /* Calculate the optional SIZE parameter */
  638. if(conn->proto.smtpc.size_supported && data->state.infilesize > 0) {
  639. size = aprintf("%" FMT_OFF_T, data->state.infilesize);
  640. if(!size) {
  641. result = CURLE_OUT_OF_MEMORY;
  642. goto out;
  643. }
  644. }
  645. /* If the mailboxes in the FROM and AUTH parameters do not 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 hostname contain non-ASCII characters? */
  654. if(!Curl_is_ASCII_name(rcpt->data))
  655. utf8 = TRUE;
  656. rcpt = rcpt->next;
  657. }
  658. }
  659. /* Add the client reader doing STMP EOB escaping */
  660. result = cr_eob_add(data);
  661. if(result)
  662. goto out;
  663. /* Send the MAIL command */
  664. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp,
  665. "MAIL FROM:%s%s%s%s%s%s",
  666. from, /* Mandatory */
  667. auth ? " AUTH=" : "", /* Optional on AUTH support */
  668. auth ? auth : "", /* */
  669. size ? " SIZE=" : "", /* Optional on SIZE support */
  670. size ? size : "", /* */
  671. utf8 ? " SMTPUTF8" /* Internationalised mailbox */
  672. : ""); /* included in our envelope */
  673. out:
  674. free(from);
  675. free(auth);
  676. free(size);
  677. if(!result)
  678. smtp_state(data, SMTP_MAIL);
  679. return result;
  680. }
  681. /***********************************************************************
  682. *
  683. * smtp_perform_rcpt_to()
  684. *
  685. * Sends a RCPT TO command for a given recipient as part of the message upload
  686. * process.
  687. */
  688. static CURLcode smtp_perform_rcpt_to(struct Curl_easy *data)
  689. {
  690. CURLcode result = CURLE_OK;
  691. struct connectdata *conn = data->conn;
  692. struct SMTP *smtp = data->req.p.smtp;
  693. char *address = NULL;
  694. struct hostname host = { NULL, NULL, NULL, NULL };
  695. /* Parse the recipient mailbox into the local address and hostname parts,
  696. converting the hostname to an IDN A-label if necessary */
  697. result = smtp_parse_address(smtp->rcpt->data,
  698. &address, &host);
  699. if(result)
  700. return result;
  701. /* Send the RCPT TO command */
  702. if(host.name)
  703. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp, "RCPT TO:<%s@%s>",
  704. address, host.name);
  705. else
  706. /* An invalid mailbox was provided but we will simply let the server worry
  707. about that and reply with a 501 error */
  708. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp, "RCPT TO:<%s>",
  709. address);
  710. Curl_free_idnconverted_hostname(&host);
  711. free(address);
  712. if(!result)
  713. smtp_state(data, SMTP_RCPT);
  714. return result;
  715. }
  716. /***********************************************************************
  717. *
  718. * smtp_perform_quit()
  719. *
  720. * Performs the quit action prior to sclose() being called.
  721. */
  722. static CURLcode smtp_perform_quit(struct Curl_easy *data,
  723. struct connectdata *conn)
  724. {
  725. /* Send the QUIT command */
  726. CURLcode result = Curl_pp_sendf(data, &conn->proto.smtpc.pp, "%s", "QUIT");
  727. if(!result)
  728. smtp_state(data, SMTP_QUIT);
  729. return result;
  730. }
  731. /* For the initial server greeting */
  732. static CURLcode smtp_state_servergreet_resp(struct Curl_easy *data,
  733. int smtpcode,
  734. smtpstate instate)
  735. {
  736. CURLcode result = CURLE_OK;
  737. (void)instate; /* no use for this yet */
  738. if(smtpcode/100 != 2) {
  739. failf(data, "Got unexpected smtp-server response: %d", smtpcode);
  740. result = CURLE_WEIRD_SERVER_REPLY;
  741. }
  742. else
  743. result = smtp_perform_ehlo(data);
  744. return result;
  745. }
  746. /* For STARTTLS responses */
  747. static CURLcode smtp_state_starttls_resp(struct Curl_easy *data,
  748. int smtpcode,
  749. smtpstate instate)
  750. {
  751. CURLcode result = CURLE_OK;
  752. (void)instate; /* no use for this yet */
  753. /* Pipelining in response is forbidden. */
  754. if(data->conn->proto.smtpc.pp.overflow)
  755. return CURLE_WEIRD_SERVER_REPLY;
  756. if(smtpcode != 220) {
  757. if(data->set.use_ssl != CURLUSESSL_TRY) {
  758. failf(data, "STARTTLS denied, code %d", smtpcode);
  759. result = CURLE_USE_SSL_FAILED;
  760. }
  761. else
  762. result = smtp_perform_authentication(data);
  763. }
  764. else
  765. result = smtp_perform_upgrade_tls(data);
  766. return result;
  767. }
  768. /* For EHLO responses */
  769. static CURLcode smtp_state_ehlo_resp(struct Curl_easy *data,
  770. struct connectdata *conn, int smtpcode,
  771. smtpstate instate)
  772. {
  773. CURLcode result = CURLE_OK;
  774. struct smtp_conn *smtpc = &conn->proto.smtpc;
  775. const char *line = Curl_dyn_ptr(&smtpc->pp.recvbuf);
  776. size_t len = smtpc->pp.nfinal;
  777. (void)instate; /* no use for this yet */
  778. if(smtpcode/100 != 2 && smtpcode != 1) {
  779. if(data->set.use_ssl <= CURLUSESSL_TRY
  780. || Curl_conn_is_ssl(conn, FIRSTSOCKET))
  781. result = smtp_perform_helo(data, conn);
  782. else {
  783. failf(data, "Remote access denied: %d", smtpcode);
  784. result = CURLE_REMOTE_ACCESS_DENIED;
  785. }
  786. }
  787. else if(len >= 4) {
  788. line += 4;
  789. len -= 4;
  790. /* Does the server support the STARTTLS capability? */
  791. if(len >= 8 && !memcmp(line, "STARTTLS", 8))
  792. smtpc->tls_supported = TRUE;
  793. /* Does the server support the SIZE capability? */
  794. else if(len >= 4 && !memcmp(line, "SIZE", 4))
  795. smtpc->size_supported = TRUE;
  796. /* Does the server support the UTF-8 capability? */
  797. else if(len >= 8 && !memcmp(line, "SMTPUTF8", 8))
  798. smtpc->utf8_supported = TRUE;
  799. /* Does the server support authentication? */
  800. else if(len >= 5 && !memcmp(line, "AUTH ", 5)) {
  801. smtpc->auth_supported = TRUE;
  802. /* Advance past the AUTH keyword */
  803. line += 5;
  804. len -= 5;
  805. /* Loop through the data line */
  806. for(;;) {
  807. size_t llen;
  808. size_t wordlen;
  809. unsigned short mechbit;
  810. while(len &&
  811. (*line == ' ' || *line == '\t' ||
  812. *line == '\r' || *line == '\n')) {
  813. line++;
  814. len--;
  815. }
  816. if(!len)
  817. break;
  818. /* Extract the word */
  819. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  820. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  821. line[wordlen] != '\n';)
  822. wordlen++;
  823. /* Test the word for a matching authentication mechanism */
  824. mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
  825. if(mechbit && llen == wordlen)
  826. smtpc->sasl.authmechs |= mechbit;
  827. line += wordlen;
  828. len -= wordlen;
  829. }
  830. }
  831. if(smtpcode != 1) {
  832. if(data->set.use_ssl && !Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
  833. /* We do not have a SSL/TLS connection yet, but SSL is requested */
  834. if(smtpc->tls_supported)
  835. /* Switch to TLS connection now */
  836. result = smtp_perform_starttls(data, conn);
  837. else if(data->set.use_ssl == CURLUSESSL_TRY)
  838. /* Fallback and carry on with authentication */
  839. result = smtp_perform_authentication(data);
  840. else {
  841. failf(data, "STARTTLS not supported.");
  842. result = CURLE_USE_SSL_FAILED;
  843. }
  844. }
  845. else
  846. result = smtp_perform_authentication(data);
  847. }
  848. }
  849. else {
  850. failf(data, "Unexpectedly short EHLO response");
  851. result = CURLE_WEIRD_SERVER_REPLY;
  852. }
  853. return result;
  854. }
  855. /* For HELO responses */
  856. static CURLcode smtp_state_helo_resp(struct Curl_easy *data, int smtpcode,
  857. smtpstate instate)
  858. {
  859. CURLcode result = CURLE_OK;
  860. (void)instate; /* no use for this yet */
  861. if(smtpcode/100 != 2) {
  862. failf(data, "Remote access denied: %d", smtpcode);
  863. result = CURLE_REMOTE_ACCESS_DENIED;
  864. }
  865. else
  866. /* End of connect phase */
  867. smtp_state(data, SMTP_STOP);
  868. return result;
  869. }
  870. /* For SASL authentication responses */
  871. static CURLcode smtp_state_auth_resp(struct Curl_easy *data,
  872. int smtpcode,
  873. smtpstate instate)
  874. {
  875. CURLcode result = CURLE_OK;
  876. struct connectdata *conn = data->conn;
  877. struct smtp_conn *smtpc = &conn->proto.smtpc;
  878. saslprogress progress;
  879. (void)instate; /* no use for this yet */
  880. result = Curl_sasl_continue(&smtpc->sasl, data, smtpcode, &progress);
  881. if(!result)
  882. switch(progress) {
  883. case SASL_DONE:
  884. smtp_state(data, SMTP_STOP); /* Authenticated */
  885. break;
  886. case SASL_IDLE: /* No mechanism left after cancellation */
  887. failf(data, "Authentication cancelled");
  888. result = CURLE_LOGIN_DENIED;
  889. break;
  890. default:
  891. break;
  892. }
  893. return result;
  894. }
  895. /* For command responses */
  896. static CURLcode smtp_state_command_resp(struct Curl_easy *data, int smtpcode,
  897. smtpstate instate)
  898. {
  899. CURLcode result = CURLE_OK;
  900. struct SMTP *smtp = data->req.p.smtp;
  901. char *line = Curl_dyn_ptr(&data->conn->proto.smtpc.pp.recvbuf);
  902. size_t len = data->conn->proto.smtpc.pp.nfinal;
  903. (void)instate; /* no use for this yet */
  904. if((smtp->rcpt && smtpcode/100 != 2 && smtpcode != 553 && smtpcode != 1) ||
  905. (!smtp->rcpt && smtpcode/100 != 2 && smtpcode != 1)) {
  906. failf(data, "Command failed: %d", smtpcode);
  907. result = CURLE_WEIRD_SERVER_REPLY;
  908. }
  909. else {
  910. if(!data->req.no_body)
  911. result = Curl_client_write(data, CLIENTWRITE_BODY, line, len);
  912. if(smtpcode != 1) {
  913. if(smtp->rcpt) {
  914. smtp->rcpt = smtp->rcpt->next;
  915. if(smtp->rcpt) {
  916. /* Send the next command */
  917. result = smtp_perform_command(data);
  918. }
  919. else
  920. /* End of DO phase */
  921. smtp_state(data, SMTP_STOP);
  922. }
  923. else
  924. /* End of DO phase */
  925. smtp_state(data, SMTP_STOP);
  926. }
  927. }
  928. return result;
  929. }
  930. /* For MAIL responses */
  931. static CURLcode smtp_state_mail_resp(struct Curl_easy *data, int smtpcode,
  932. smtpstate instate)
  933. {
  934. CURLcode result = CURLE_OK;
  935. (void)instate; /* no use for this yet */
  936. if(smtpcode/100 != 2) {
  937. failf(data, "MAIL failed: %d", smtpcode);
  938. result = CURLE_SEND_ERROR;
  939. }
  940. else
  941. /* Start the RCPT TO command */
  942. result = smtp_perform_rcpt_to(data);
  943. return result;
  944. }
  945. /* For RCPT responses */
  946. static CURLcode smtp_state_rcpt_resp(struct Curl_easy *data,
  947. struct connectdata *conn, int smtpcode,
  948. smtpstate instate)
  949. {
  950. CURLcode result = CURLE_OK;
  951. struct SMTP *smtp = data->req.p.smtp;
  952. bool is_smtp_err = FALSE;
  953. bool is_smtp_blocking_err = FALSE;
  954. (void)instate; /* no use for this yet */
  955. is_smtp_err = (smtpcode/100 != 2);
  956. /* If there is multiple RCPT TO to be issued, it is possible to ignore errors
  957. and proceed with only the valid addresses. */
  958. is_smtp_blocking_err = (is_smtp_err && !data->set.mail_rcpt_allowfails);
  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 were not 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_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
  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. int smtpcode;
  1033. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1034. struct pingpong *pp = &smtpc->pp;
  1035. size_t nread = 0;
  1036. /* Busy upgrading the connection; right now all I/O is SSL/TLS, not SMTP */
  1037. upgrade_tls:
  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, FIRSTSOCKET, 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. /* During UPGRADETLS, leave the read loop as we need to connect
  1067. * (e.g. TLS handshake) before we continue sending/receiving. */
  1068. if(!result && (smtpc->state == SMTP_UPGRADETLS))
  1069. goto upgrade_tls;
  1070. break;
  1071. case SMTP_AUTH:
  1072. result = smtp_state_auth_resp(data, smtpcode, smtpc->state);
  1073. break;
  1074. case SMTP_COMMAND:
  1075. result = smtp_state_command_resp(data, smtpcode, smtpc->state);
  1076. break;
  1077. case SMTP_MAIL:
  1078. result = smtp_state_mail_resp(data, smtpcode, smtpc->state);
  1079. break;
  1080. case SMTP_RCPT:
  1081. result = smtp_state_rcpt_resp(data, conn, smtpcode, smtpc->state);
  1082. break;
  1083. case SMTP_DATA:
  1084. result = smtp_state_data_resp(data, smtpcode, smtpc->state);
  1085. break;
  1086. case SMTP_POSTDATA:
  1087. result = smtp_state_postdata_resp(data, smtpcode, smtpc->state);
  1088. break;
  1089. case SMTP_QUIT:
  1090. default:
  1091. /* internal error */
  1092. smtp_state(data, SMTP_STOP);
  1093. break;
  1094. }
  1095. } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp));
  1096. return result;
  1097. }
  1098. /* Called repeatedly until done from multi.c */
  1099. static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done)
  1100. {
  1101. CURLcode result = CURLE_OK;
  1102. struct connectdata *conn = data->conn;
  1103. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1104. if((conn->handler->flags & PROTOPT_SSL) && !smtpc->ssldone) {
  1105. bool ssldone = FALSE;
  1106. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  1107. smtpc->ssldone = ssldone;
  1108. if(result || !smtpc->ssldone)
  1109. return result;
  1110. }
  1111. result = Curl_pp_statemach(data, &smtpc->pp, FALSE, FALSE);
  1112. *done = (smtpc->state == SMTP_STOP);
  1113. return result;
  1114. }
  1115. static CURLcode smtp_block_statemach(struct Curl_easy *data,
  1116. struct connectdata *conn,
  1117. bool disconnecting)
  1118. {
  1119. CURLcode result = CURLE_OK;
  1120. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1121. while(smtpc->state != SMTP_STOP && !result)
  1122. result = Curl_pp_statemach(data, &smtpc->pp, TRUE, disconnecting);
  1123. return result;
  1124. }
  1125. /* Allocate and initialize the SMTP struct for the current Curl_easy if
  1126. required */
  1127. static CURLcode smtp_init(struct Curl_easy *data)
  1128. {
  1129. CURLcode result = CURLE_OK;
  1130. struct SMTP *smtp;
  1131. smtp = data->req.p.smtp = calloc(1, sizeof(struct SMTP));
  1132. if(!smtp)
  1133. result = CURLE_OUT_OF_MEMORY;
  1134. return result;
  1135. }
  1136. /* For the SMTP "protocol connect" and "doing" phases only */
  1137. static int smtp_getsock(struct Curl_easy *data,
  1138. struct connectdata *conn, curl_socket_t *socks)
  1139. {
  1140. return Curl_pp_getsock(data, &conn->proto.smtpc.pp, socks);
  1141. }
  1142. /***********************************************************************
  1143. *
  1144. * smtp_connect()
  1145. *
  1146. * This function should do everything that is to be considered a part of
  1147. * the connection phase.
  1148. *
  1149. * The variable pointed to by 'done' will be TRUE if the protocol-layer
  1150. * connect phase is done when this function returns, or FALSE if not.
  1151. */
  1152. static CURLcode smtp_connect(struct Curl_easy *data, bool *done)
  1153. {
  1154. CURLcode result = CURLE_OK;
  1155. struct connectdata *conn = data->conn;
  1156. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1157. struct pingpong *pp = &smtpc->pp;
  1158. *done = FALSE; /* default to not done yet */
  1159. /* We always support persistent connections in SMTP */
  1160. connkeep(conn, "SMTP default");
  1161. PINGPONG_SETUP(pp, smtp_statemachine, smtp_endofresp);
  1162. /* Initialize the SASL storage */
  1163. Curl_sasl_init(&smtpc->sasl, data, &saslsmtp);
  1164. /* Initialise the pingpong layer */
  1165. Curl_pp_init(pp);
  1166. /* Parse the URL options */
  1167. result = smtp_parse_url_options(conn);
  1168. if(result)
  1169. return result;
  1170. /* Parse the URL path */
  1171. result = smtp_parse_url_path(data);
  1172. if(result)
  1173. return result;
  1174. /* Start off waiting for the server greeting response */
  1175. smtp_state(data, SMTP_SERVERGREET);
  1176. result = smtp_multi_statemach(data, done);
  1177. return result;
  1178. }
  1179. /***********************************************************************
  1180. *
  1181. * smtp_done()
  1182. *
  1183. * The DONE function. This does what needs to be done after a single DO has
  1184. * performed.
  1185. *
  1186. * Input argument is already checked for validity.
  1187. */
  1188. static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
  1189. bool premature)
  1190. {
  1191. CURLcode result = CURLE_OK;
  1192. struct connectdata *conn = data->conn;
  1193. struct SMTP *smtp = data->req.p.smtp;
  1194. (void)premature;
  1195. if(!smtp)
  1196. return CURLE_OK;
  1197. /* Cleanup our per-request based variables */
  1198. Curl_safefree(smtp->custom);
  1199. if(status) {
  1200. connclose(conn, "SMTP done with bad status"); /* marked for closure */
  1201. result = status; /* use the already set error code */
  1202. }
  1203. else if(!data->set.connect_only && data->set.mail_rcpt &&
  1204. (data->state.upload || IS_MIME_POST(data))) {
  1205. smtp_state(data, SMTP_POSTDATA);
  1206. /* Run the state-machine */
  1207. result = smtp_block_statemach(data, conn, FALSE);
  1208. }
  1209. /* Clear the transfer mode for the next request */
  1210. smtp->transfer = PPTRANSFER_BODY;
  1211. CURL_TRC_SMTP(data, "smtp_done(status=%d, premature=%d) -> %d",
  1212. status, premature, result);
  1213. return result;
  1214. }
  1215. /***********************************************************************
  1216. *
  1217. * smtp_perform()
  1218. *
  1219. * This is the actual DO function for SMTP. Transfer a mail, send a command
  1220. * or get some data according to the options previously setup.
  1221. */
  1222. static CURLcode smtp_perform(struct Curl_easy *data, bool *connected,
  1223. bool *dophase_done)
  1224. {
  1225. /* This is SMTP and no proxy */
  1226. CURLcode result = CURLE_OK;
  1227. struct SMTP *smtp = data->req.p.smtp;
  1228. CURL_TRC_SMTP(data, "smtp_perform(), start");
  1229. if(data->req.no_body) {
  1230. /* Requested no body means no transfer */
  1231. smtp->transfer = PPTRANSFER_INFO;
  1232. }
  1233. *dophase_done = FALSE; /* not done yet */
  1234. /* Store the first recipient (or NULL if not specified) */
  1235. smtp->rcpt = data->set.mail_rcpt;
  1236. /* Track of whether we have successfully sent at least one RCPT TO command */
  1237. smtp->rcpt_had_ok = FALSE;
  1238. /* Track of the last error we have received by sending RCPT TO command */
  1239. smtp->rcpt_last_error = 0;
  1240. /* Initial data character is the first character in line: it is implicitly
  1241. preceded by a virtual CRLF. */
  1242. smtp->trailing_crlf = TRUE;
  1243. smtp->eob = 2;
  1244. /* Start the first command in the DO phase */
  1245. if((data->state.upload || IS_MIME_POST(data)) && data->set.mail_rcpt)
  1246. /* MAIL transfer */
  1247. result = smtp_perform_mail(data);
  1248. else
  1249. /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
  1250. result = smtp_perform_command(data);
  1251. if(result)
  1252. goto out;
  1253. /* Run the state-machine */
  1254. result = smtp_multi_statemach(data, dophase_done);
  1255. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  1256. out:
  1257. CURL_TRC_SMTP(data, "smtp_perform() -> %d, connected=%d, done=%d",
  1258. result, *connected, *dophase_done);
  1259. return result;
  1260. }
  1261. /***********************************************************************
  1262. *
  1263. * smtp_do()
  1264. *
  1265. * This function is registered as 'curl_do' function. It decodes the path
  1266. * parts etc as a wrapper to the actual DO function (smtp_perform).
  1267. *
  1268. * The input argument is already checked for validity.
  1269. */
  1270. static CURLcode smtp_do(struct Curl_easy *data, bool *done)
  1271. {
  1272. CURLcode result = CURLE_OK;
  1273. DEBUGASSERT(data);
  1274. DEBUGASSERT(data->conn);
  1275. *done = FALSE; /* default to false */
  1276. /* Parse the custom request */
  1277. result = smtp_parse_custom_request(data);
  1278. if(result)
  1279. return result;
  1280. result = smtp_regular_transfer(data, done);
  1281. CURL_TRC_SMTP(data, "smtp_do() -> %d, done=%d", result, *done);
  1282. return result;
  1283. }
  1284. /***********************************************************************
  1285. *
  1286. * smtp_disconnect()
  1287. *
  1288. * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
  1289. * resources. BLOCKING.
  1290. */
  1291. static CURLcode smtp_disconnect(struct Curl_easy *data,
  1292. struct connectdata *conn,
  1293. bool dead_connection)
  1294. {
  1295. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1296. (void)data;
  1297. /* We cannot send quit unconditionally. If this connection is stale or
  1298. bad in any way, sending quit and waiting around here will make the
  1299. disconnect wait in vain and cause more problems than we need to. */
  1300. if(!dead_connection && conn->bits.protoconnstart) {
  1301. if(!smtp_perform_quit(data, conn))
  1302. (void)smtp_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
  1303. }
  1304. /* Disconnect from the server */
  1305. Curl_pp_disconnect(&smtpc->pp);
  1306. /* Cleanup the SASL module */
  1307. Curl_sasl_cleanup(conn, smtpc->sasl.authused);
  1308. /* Cleanup our connection based variables */
  1309. Curl_safefree(smtpc->domain);
  1310. CURL_TRC_SMTP(data, "smtp_disconnect(), finished");
  1311. return CURLE_OK;
  1312. }
  1313. /* Call this when the DO phase has completed */
  1314. static CURLcode smtp_dophase_done(struct Curl_easy *data, bool connected)
  1315. {
  1316. struct SMTP *smtp = data->req.p.smtp;
  1317. (void)connected;
  1318. if(smtp->transfer != PPTRANSFER_BODY)
  1319. /* no data to transfer */
  1320. Curl_xfer_setup_nop(data);
  1321. return CURLE_OK;
  1322. }
  1323. /* Called from multi.c while DOing */
  1324. static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
  1325. {
  1326. CURLcode result = smtp_multi_statemach(data, dophase_done);
  1327. if(result)
  1328. DEBUGF(infof(data, "DO phase failed"));
  1329. else if(*dophase_done) {
  1330. result = smtp_dophase_done(data, FALSE /* not connected */);
  1331. DEBUGF(infof(data, "DO phase is complete"));
  1332. }
  1333. CURL_TRC_SMTP(data, "smtp_doing() -> %d, done=%d", result, *dophase_done);
  1334. return result;
  1335. }
  1336. /***********************************************************************
  1337. *
  1338. * smtp_regular_transfer()
  1339. *
  1340. * The input argument is already checked for validity.
  1341. *
  1342. * Performs all commands done before a regular transfer between a local and a
  1343. * remote host.
  1344. */
  1345. static CURLcode smtp_regular_transfer(struct Curl_easy *data,
  1346. bool *dophase_done)
  1347. {
  1348. CURLcode result = CURLE_OK;
  1349. bool connected = FALSE;
  1350. /* Make sure size is unknown at this point */
  1351. data->req.size = -1;
  1352. /* Set the progress data */
  1353. Curl_pgrsSetUploadCounter(data, 0);
  1354. Curl_pgrsSetDownloadCounter(data, 0);
  1355. Curl_pgrsSetUploadSize(data, -1);
  1356. Curl_pgrsSetDownloadSize(data, -1);
  1357. /* Carry out the perform */
  1358. result = smtp_perform(data, &connected, dophase_done);
  1359. /* Perform post DO phase operations if necessary */
  1360. if(!result && *dophase_done)
  1361. result = smtp_dophase_done(data, connected);
  1362. CURL_TRC_SMTP(data, "smtp_regular_transfer() -> %d, done=%d",
  1363. result, *dophase_done);
  1364. return result;
  1365. }
  1366. static CURLcode smtp_setup_connection(struct Curl_easy *data,
  1367. struct connectdata *conn)
  1368. {
  1369. CURLcode result;
  1370. /* Clear the TLS upgraded flag */
  1371. conn->bits.tls_upgraded = FALSE;
  1372. /* Initialise the SMTP layer */
  1373. result = smtp_init(data);
  1374. CURL_TRC_SMTP(data, "smtp_setup_connection() -> %d", result);
  1375. return result;
  1376. }
  1377. /***********************************************************************
  1378. *
  1379. * smtp_parse_url_options()
  1380. *
  1381. * Parse the URL login options.
  1382. */
  1383. static CURLcode smtp_parse_url_options(struct connectdata *conn)
  1384. {
  1385. CURLcode result = CURLE_OK;
  1386. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1387. const char *ptr = conn->options;
  1388. while(!result && ptr && *ptr) {
  1389. const char *key = ptr;
  1390. const char *value;
  1391. while(*ptr && *ptr != '=')
  1392. ptr++;
  1393. value = ptr + 1;
  1394. while(*ptr && *ptr != ';')
  1395. ptr++;
  1396. if(strncasecompare(key, "AUTH=", 5))
  1397. result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
  1398. value, ptr - value);
  1399. else
  1400. result = CURLE_URL_MALFORMAT;
  1401. if(*ptr == ';')
  1402. ptr++;
  1403. }
  1404. return result;
  1405. }
  1406. /***********************************************************************
  1407. *
  1408. * smtp_parse_url_path()
  1409. *
  1410. * Parse the URL path into separate path components.
  1411. */
  1412. static CURLcode smtp_parse_url_path(struct Curl_easy *data)
  1413. {
  1414. /* The SMTP struct is already initialised in smtp_connect() */
  1415. struct connectdata *conn = data->conn;
  1416. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1417. const char *path = &data->state.up.path[1]; /* skip leading path */
  1418. char localhost[HOSTNAME_MAX + 1];
  1419. /* Calculate the path if necessary */
  1420. if(!*path) {
  1421. if(!Curl_gethostname(localhost, sizeof(localhost)))
  1422. path = localhost;
  1423. else
  1424. path = "localhost";
  1425. }
  1426. /* URL decode the path and use it as the domain in our EHLO */
  1427. return Curl_urldecode(path, 0, &smtpc->domain, NULL, REJECT_CTRL);
  1428. }
  1429. /***********************************************************************
  1430. *
  1431. * smtp_parse_custom_request()
  1432. *
  1433. * Parse the custom request.
  1434. */
  1435. static CURLcode smtp_parse_custom_request(struct Curl_easy *data)
  1436. {
  1437. CURLcode result = CURLE_OK;
  1438. struct SMTP *smtp = data->req.p.smtp;
  1439. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1440. /* URL decode the custom request */
  1441. if(custom)
  1442. result = Curl_urldecode(custom, 0, &smtp->custom, NULL, REJECT_CTRL);
  1443. return result;
  1444. }
  1445. /***********************************************************************
  1446. *
  1447. * smtp_parse_address()
  1448. *
  1449. * Parse the fully qualified mailbox address into a local address part and the
  1450. * hostname, converting the hostname to an IDN A-label, as per RFC-5890, if
  1451. * necessary.
  1452. *
  1453. * Parameters:
  1454. *
  1455. * conn [in] - The connection handle.
  1456. * fqma [in] - The fully qualified mailbox address (which may or
  1457. * may not contain UTF-8 characters).
  1458. * address [in/out] - A new allocated buffer which holds the local
  1459. * address part of the mailbox. This buffer must be
  1460. * free'ed by the caller.
  1461. * host [in/out] - The hostname structure that holds the original,
  1462. * and optionally encoded, hostname.
  1463. * Curl_free_idnconverted_hostname() must be called
  1464. * once the caller has finished with the structure.
  1465. *
  1466. * Returns CURLE_OK on success.
  1467. *
  1468. * Notes:
  1469. *
  1470. * Should a UTF-8 hostname require conversion to IDN ACE and we cannot honor
  1471. * that conversion then we shall return success. This allow the caller to send
  1472. * the data to the server as a U-label (as per RFC-6531 sect. 3.2).
  1473. *
  1474. * If an mailbox '@' separator cannot be located then the mailbox is considered
  1475. * to be either a local mailbox or an invalid mailbox (depending on what the
  1476. * calling function deems it to be) then the input will simply be returned in
  1477. * the address part with the hostname being NULL.
  1478. */
  1479. static CURLcode smtp_parse_address(const char *fqma, char **address,
  1480. struct hostname *host)
  1481. {
  1482. CURLcode result = CURLE_OK;
  1483. size_t length;
  1484. /* Duplicate the fully qualified email address so we can manipulate it,
  1485. ensuring it does not contain the delimiters if specified */
  1486. char *dup = strdup(fqma[0] == '<' ? fqma + 1 : fqma);
  1487. if(!dup)
  1488. return CURLE_OUT_OF_MEMORY;
  1489. length = strlen(dup);
  1490. if(length) {
  1491. if(dup[length - 1] == '>')
  1492. dup[length - 1] = '\0';
  1493. }
  1494. /* Extract the hostname from the address (if we can) */
  1495. host->name = strpbrk(dup, "@");
  1496. if(host->name) {
  1497. *host->name = '\0';
  1498. host->name = host->name + 1;
  1499. /* Attempt to convert the hostname to IDN ACE */
  1500. (void) Curl_idnconvert_hostname(host);
  1501. /* If Curl_idnconvert_hostname() fails then we shall attempt to continue
  1502. and send the hostname using UTF-8 rather than as 7-bit ACE (which is
  1503. our preference) */
  1504. }
  1505. /* Extract the local address from the mailbox */
  1506. *address = dup;
  1507. return result;
  1508. }
  1509. struct cr_eob_ctx {
  1510. struct Curl_creader super;
  1511. struct bufq buf;
  1512. size_t n_eob; /* how many EOB bytes we matched so far */
  1513. size_t eob; /* Number of bytes of the EOB (End Of Body) that
  1514. have been received so far */
  1515. BIT(read_eos); /* we read an EOS from the next reader */
  1516. BIT(eos); /* we have returned an EOS */
  1517. };
  1518. static CURLcode cr_eob_init(struct Curl_easy *data,
  1519. struct Curl_creader *reader)
  1520. {
  1521. struct cr_eob_ctx *ctx = reader->ctx;
  1522. (void)data;
  1523. /* The first char we read is the first on a line, as if we had
  1524. * read CRLF just before */
  1525. ctx->n_eob = 2;
  1526. Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
  1527. return CURLE_OK;
  1528. }
  1529. static void cr_eob_close(struct Curl_easy *data, struct Curl_creader *reader)
  1530. {
  1531. struct cr_eob_ctx *ctx = reader->ctx;
  1532. (void)data;
  1533. Curl_bufq_free(&ctx->buf);
  1534. }
  1535. /* this is the 5-bytes End-Of-Body marker for SMTP */
  1536. #define SMTP_EOB "\r\n.\r\n"
  1537. #define SMTP_EOB_FIND_LEN 3
  1538. /* client reader doing SMTP End-Of-Body escaping. */
  1539. static CURLcode cr_eob_read(struct Curl_easy *data,
  1540. struct Curl_creader *reader,
  1541. char *buf, size_t blen,
  1542. size_t *pnread, bool *peos)
  1543. {
  1544. struct cr_eob_ctx *ctx = reader->ctx;
  1545. CURLcode result = CURLE_OK;
  1546. size_t nread, i, start, n;
  1547. bool eos;
  1548. if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  1549. /* Get more and convert it when needed */
  1550. result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
  1551. if(result)
  1552. return result;
  1553. ctx->read_eos = eos;
  1554. if(nread) {
  1555. if(!ctx->n_eob && !memchr(buf, SMTP_EOB[0], nread)) {
  1556. /* not in the middle of a match, no EOB start found, just pass */
  1557. *pnread = nread;
  1558. *peos = FALSE;
  1559. return CURLE_OK;
  1560. }
  1561. /* scan for EOB (continuation) and convert */
  1562. for(i = start = 0; i < nread; ++i) {
  1563. if(ctx->n_eob >= SMTP_EOB_FIND_LEN) {
  1564. /* matched the EOB prefix and seeing additional char, add '.' */
  1565. result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
  1566. if(result)
  1567. return result;
  1568. result = Curl_bufq_cwrite(&ctx->buf, ".", 1, &n);
  1569. if(result)
  1570. return result;
  1571. ctx->n_eob = 0;
  1572. start = i;
  1573. if(data->state.infilesize > 0)
  1574. data->state.infilesize++;
  1575. }
  1576. if(buf[i] != SMTP_EOB[ctx->n_eob])
  1577. ctx->n_eob = 0;
  1578. if(buf[i] == SMTP_EOB[ctx->n_eob]) {
  1579. /* matching another char of the EOB */
  1580. ++ctx->n_eob;
  1581. }
  1582. }
  1583. /* add any remainder to buf */
  1584. if(start < nread) {
  1585. result = Curl_bufq_cwrite(&ctx->buf, buf + start, nread - start, &n);
  1586. if(result)
  1587. return result;
  1588. }
  1589. }
  1590. if(ctx->read_eos) {
  1591. /* if we last matched a CRLF or if the data was empty, add ".\r\n"
  1592. * to end the body. If we sent something and it did not end with "\r\n",
  1593. * add "\r\n.\r\n" to end the body */
  1594. const char *eob = SMTP_EOB;
  1595. switch(ctx->n_eob) {
  1596. case 2:
  1597. /* seen a CRLF at the end, just add the remainder */
  1598. eob = &SMTP_EOB[2];
  1599. break;
  1600. case 3:
  1601. /* ended with '\r\n.', we should escpe the last '.' */
  1602. eob = "." SMTP_EOB;
  1603. break;
  1604. default:
  1605. break;
  1606. }
  1607. result = Curl_bufq_cwrite(&ctx->buf, eob, strlen(eob), &n);
  1608. if(result)
  1609. return result;
  1610. }
  1611. }
  1612. *peos = FALSE;
  1613. if(!Curl_bufq_is_empty(&ctx->buf)) {
  1614. result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
  1615. }
  1616. else
  1617. *pnread = 0;
  1618. if(ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  1619. /* no more data, read all, done. */
  1620. ctx->eos = TRUE;
  1621. }
  1622. *peos = ctx->eos;
  1623. DEBUGF(infof(data, "cr_eob_read(%zu) -> %d, %zd, %d",
  1624. blen, result, *pnread, *peos));
  1625. return result;
  1626. }
  1627. static curl_off_t cr_eob_total_length(struct Curl_easy *data,
  1628. struct Curl_creader *reader)
  1629. {
  1630. /* this reader changes length depending on input */
  1631. (void)data;
  1632. (void)reader;
  1633. return -1;
  1634. }
  1635. static const struct Curl_crtype cr_eob = {
  1636. "cr-smtp-eob",
  1637. cr_eob_init,
  1638. cr_eob_read,
  1639. cr_eob_close,
  1640. Curl_creader_def_needs_rewind,
  1641. cr_eob_total_length,
  1642. Curl_creader_def_resume_from,
  1643. Curl_creader_def_rewind,
  1644. Curl_creader_def_unpause,
  1645. Curl_creader_def_is_paused,
  1646. Curl_creader_def_done,
  1647. sizeof(struct cr_eob_ctx)
  1648. };
  1649. static CURLcode cr_eob_add(struct Curl_easy *data)
  1650. {
  1651. struct Curl_creader *reader = NULL;
  1652. CURLcode result;
  1653. result = Curl_creader_create(&reader, data, &cr_eob,
  1654. CURL_CR_CONTENT_ENCODE);
  1655. if(!result)
  1656. result = Curl_creader_add(data, reader);
  1657. if(result && reader)
  1658. Curl_creader_free(data, reader);
  1659. return result;
  1660. }
  1661. #endif /* CURL_DISABLE_SMTP */