2
0

smtp.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  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(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 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("%" CURL_FORMAT_CURL_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) ? TRUE : FALSE;
  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 =
  959. (is_smtp_err && !data->set.mail_rcpt_allowfails) ? TRUE : FALSE;
  960. if(is_smtp_err) {
  961. /* Remembering the last failure which we can report if all "RCPT TO" have
  962. failed and we cannot proceed. */
  963. smtp->rcpt_last_error = smtpcode;
  964. if(is_smtp_blocking_err) {
  965. failf(data, "RCPT failed: %d", smtpcode);
  966. result = CURLE_SEND_ERROR;
  967. }
  968. }
  969. else {
  970. /* Some RCPT TO commands have succeeded. */
  971. smtp->rcpt_had_ok = TRUE;
  972. }
  973. if(!is_smtp_blocking_err) {
  974. smtp->rcpt = smtp->rcpt->next;
  975. if(smtp->rcpt)
  976. /* Send the next RCPT TO command */
  977. result = smtp_perform_rcpt_to(data);
  978. else {
  979. /* We were not able to issue a successful RCPT TO command while going
  980. over recipients (potentially multiple). Sending back last error. */
  981. if(!smtp->rcpt_had_ok) {
  982. failf(data, "RCPT failed: %d (last error)", smtp->rcpt_last_error);
  983. result = CURLE_SEND_ERROR;
  984. }
  985. else {
  986. /* Send the DATA command */
  987. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp, "%s", "DATA");
  988. if(!result)
  989. smtp_state(data, SMTP_DATA);
  990. }
  991. }
  992. }
  993. return result;
  994. }
  995. /* For DATA response */
  996. static CURLcode smtp_state_data_resp(struct Curl_easy *data, int smtpcode,
  997. smtpstate instate)
  998. {
  999. CURLcode result = CURLE_OK;
  1000. (void)instate; /* no use for this yet */
  1001. if(smtpcode != 354) {
  1002. failf(data, "DATA failed: %d", smtpcode);
  1003. result = CURLE_SEND_ERROR;
  1004. }
  1005. else {
  1006. /* Set the progress upload size */
  1007. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  1008. /* SMTP upload */
  1009. Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
  1010. /* End of DO phase */
  1011. smtp_state(data, SMTP_STOP);
  1012. }
  1013. return result;
  1014. }
  1015. /* For POSTDATA responses, which are received after the entire DATA
  1016. part has been sent to the server */
  1017. static CURLcode smtp_state_postdata_resp(struct Curl_easy *data,
  1018. int smtpcode,
  1019. smtpstate instate)
  1020. {
  1021. CURLcode result = CURLE_OK;
  1022. (void)instate; /* no use for this yet */
  1023. if(smtpcode != 250)
  1024. result = CURLE_WEIRD_SERVER_REPLY;
  1025. /* End of DONE phase */
  1026. smtp_state(data, SMTP_STOP);
  1027. return result;
  1028. }
  1029. static CURLcode smtp_statemachine(struct Curl_easy *data,
  1030. struct connectdata *conn)
  1031. {
  1032. CURLcode result = CURLE_OK;
  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. upgrade_tls:
  1039. if(smtpc->state == SMTP_UPGRADETLS)
  1040. return smtp_perform_upgrade_tls(data);
  1041. /* Flush any data that needs to be sent */
  1042. if(pp->sendleft)
  1043. return Curl_pp_flushsend(data, pp);
  1044. do {
  1045. /* Read the response from the server */
  1046. result = Curl_pp_readresp(data, FIRSTSOCKET, pp, &smtpcode, &nread);
  1047. if(result)
  1048. return result;
  1049. /* Store the latest response for later retrieval if necessary */
  1050. if(smtpc->state != SMTP_QUIT && smtpcode != 1)
  1051. data->info.httpcode = smtpcode;
  1052. if(!smtpcode)
  1053. break;
  1054. /* We have now received a full SMTP server response */
  1055. switch(smtpc->state) {
  1056. case SMTP_SERVERGREET:
  1057. result = smtp_state_servergreet_resp(data, smtpcode, smtpc->state);
  1058. break;
  1059. case SMTP_EHLO:
  1060. result = smtp_state_ehlo_resp(data, conn, smtpcode, smtpc->state);
  1061. break;
  1062. case SMTP_HELO:
  1063. result = smtp_state_helo_resp(data, smtpcode, smtpc->state);
  1064. break;
  1065. case SMTP_STARTTLS:
  1066. result = smtp_state_starttls_resp(data, smtpcode, smtpc->state);
  1067. /* During UPGRADETLS, leave the read loop as we need to connect
  1068. * (e.g. TLS handshake) before we continue sending/receiving. */
  1069. if(!result && (smtpc->state == SMTP_UPGRADETLS))
  1070. goto upgrade_tls;
  1071. break;
  1072. case SMTP_AUTH:
  1073. result = smtp_state_auth_resp(data, smtpcode, smtpc->state);
  1074. break;
  1075. case SMTP_COMMAND:
  1076. result = smtp_state_command_resp(data, smtpcode, smtpc->state);
  1077. break;
  1078. case SMTP_MAIL:
  1079. result = smtp_state_mail_resp(data, smtpcode, smtpc->state);
  1080. break;
  1081. case SMTP_RCPT:
  1082. result = smtp_state_rcpt_resp(data, conn, smtpcode, smtpc->state);
  1083. break;
  1084. case SMTP_DATA:
  1085. result = smtp_state_data_resp(data, smtpcode, smtpc->state);
  1086. break;
  1087. case SMTP_POSTDATA:
  1088. result = smtp_state_postdata_resp(data, smtpcode, smtpc->state);
  1089. break;
  1090. case SMTP_QUIT:
  1091. default:
  1092. /* internal error */
  1093. smtp_state(data, SMTP_STOP);
  1094. break;
  1095. }
  1096. } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp));
  1097. return result;
  1098. }
  1099. /* Called repeatedly until done from multi.c */
  1100. static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done)
  1101. {
  1102. CURLcode result = CURLE_OK;
  1103. struct connectdata *conn = data->conn;
  1104. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1105. if((conn->handler->flags & PROTOPT_SSL) && !smtpc->ssldone) {
  1106. bool ssldone = FALSE;
  1107. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  1108. smtpc->ssldone = ssldone;
  1109. if(result || !smtpc->ssldone)
  1110. return result;
  1111. }
  1112. result = Curl_pp_statemach(data, &smtpc->pp, FALSE, FALSE);
  1113. *done = (smtpc->state == SMTP_STOP) ? TRUE : FALSE;
  1114. return result;
  1115. }
  1116. static CURLcode smtp_block_statemach(struct Curl_easy *data,
  1117. struct connectdata *conn,
  1118. bool disconnecting)
  1119. {
  1120. CURLcode result = CURLE_OK;
  1121. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1122. while(smtpc->state != SMTP_STOP && !result)
  1123. result = Curl_pp_statemach(data, &smtpc->pp, TRUE, disconnecting);
  1124. return result;
  1125. }
  1126. /* Allocate and initialize the SMTP struct for the current Curl_easy if
  1127. required */
  1128. static CURLcode smtp_init(struct Curl_easy *data)
  1129. {
  1130. CURLcode result = CURLE_OK;
  1131. struct SMTP *smtp;
  1132. smtp = data->req.p.smtp = calloc(1, sizeof(struct SMTP));
  1133. if(!smtp)
  1134. result = CURLE_OUT_OF_MEMORY;
  1135. return result;
  1136. }
  1137. /* For the SMTP "protocol connect" and "doing" phases only */
  1138. static int smtp_getsock(struct Curl_easy *data,
  1139. struct connectdata *conn, curl_socket_t *socks)
  1140. {
  1141. return Curl_pp_getsock(data, &conn->proto.smtpc.pp, socks);
  1142. }
  1143. /***********************************************************************
  1144. *
  1145. * smtp_connect()
  1146. *
  1147. * This function should do everything that is to be considered a part of
  1148. * the connection phase.
  1149. *
  1150. * The variable pointed to by 'done' will be TRUE if the protocol-layer
  1151. * connect phase is done when this function returns, or FALSE if not.
  1152. */
  1153. static CURLcode smtp_connect(struct Curl_easy *data, bool *done)
  1154. {
  1155. CURLcode result = CURLE_OK;
  1156. struct connectdata *conn = data->conn;
  1157. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1158. struct pingpong *pp = &smtpc->pp;
  1159. *done = FALSE; /* default to not done yet */
  1160. /* We always support persistent connections in SMTP */
  1161. connkeep(conn, "SMTP default");
  1162. PINGPONG_SETUP(pp, smtp_statemachine, smtp_endofresp);
  1163. /* Initialize the SASL storage */
  1164. Curl_sasl_init(&smtpc->sasl, data, &saslsmtp);
  1165. /* Initialise the pingpong layer */
  1166. Curl_pp_init(pp);
  1167. /* Parse the URL options */
  1168. result = smtp_parse_url_options(conn);
  1169. if(result)
  1170. return result;
  1171. /* Parse the URL path */
  1172. result = smtp_parse_url_path(data);
  1173. if(result)
  1174. return result;
  1175. /* Start off waiting for the server greeting response */
  1176. smtp_state(data, SMTP_SERVERGREET);
  1177. result = smtp_multi_statemach(data, done);
  1178. return result;
  1179. }
  1180. /***********************************************************************
  1181. *
  1182. * smtp_done()
  1183. *
  1184. * The DONE function. This does what needs to be done after a single DO has
  1185. * performed.
  1186. *
  1187. * Input argument is already checked for validity.
  1188. */
  1189. static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
  1190. bool premature)
  1191. {
  1192. CURLcode result = CURLE_OK;
  1193. struct connectdata *conn = data->conn;
  1194. struct SMTP *smtp = data->req.p.smtp;
  1195. (void)premature;
  1196. if(!smtp)
  1197. return CURLE_OK;
  1198. /* Cleanup our per-request based variables */
  1199. Curl_safefree(smtp->custom);
  1200. if(status) {
  1201. connclose(conn, "SMTP done with bad status"); /* marked for closure */
  1202. result = status; /* use the already set error code */
  1203. }
  1204. else if(!data->set.connect_only && data->set.mail_rcpt &&
  1205. (data->state.upload || IS_MIME_POST(data))) {
  1206. smtp_state(data, SMTP_POSTDATA);
  1207. /* Run the state-machine */
  1208. result = smtp_block_statemach(data, conn, FALSE);
  1209. }
  1210. /* Clear the transfer mode for the next request */
  1211. smtp->transfer = PPTRANSFER_BODY;
  1212. return result;
  1213. }
  1214. /***********************************************************************
  1215. *
  1216. * smtp_perform()
  1217. *
  1218. * This is the actual DO function for SMTP. Transfer a mail, send a command
  1219. * or get some data according to the options previously setup.
  1220. */
  1221. static CURLcode smtp_perform(struct Curl_easy *data, bool *connected,
  1222. bool *dophase_done)
  1223. {
  1224. /* This is SMTP and no proxy */
  1225. CURLcode result = CURLE_OK;
  1226. struct SMTP *smtp = data->req.p.smtp;
  1227. DEBUGF(infof(data, "DO phase starts"));
  1228. if(data->req.no_body) {
  1229. /* Requested no body means no transfer */
  1230. smtp->transfer = PPTRANSFER_INFO;
  1231. }
  1232. *dophase_done = FALSE; /* not done yet */
  1233. /* Store the first recipient (or NULL if not specified) */
  1234. smtp->rcpt = data->set.mail_rcpt;
  1235. /* Track of whether we have successfully sent at least one RCPT TO command */
  1236. smtp->rcpt_had_ok = FALSE;
  1237. /* Track of the last error we have received by sending RCPT TO command */
  1238. smtp->rcpt_last_error = 0;
  1239. /* Initial data character is the first character in line: it is implicitly
  1240. preceded by a virtual CRLF. */
  1241. smtp->trailing_crlf = TRUE;
  1242. smtp->eob = 2;
  1243. /* Start the first command in the DO phase */
  1244. if((data->state.upload || IS_MIME_POST(data)) && data->set.mail_rcpt)
  1245. /* MAIL transfer */
  1246. result = smtp_perform_mail(data);
  1247. else
  1248. /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
  1249. result = smtp_perform_command(data);
  1250. if(result)
  1251. return result;
  1252. /* Run the state-machine */
  1253. result = smtp_multi_statemach(data, dophase_done);
  1254. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  1255. if(*dophase_done)
  1256. DEBUGF(infof(data, "DO phase is complete"));
  1257. return result;
  1258. }
  1259. /***********************************************************************
  1260. *
  1261. * smtp_do()
  1262. *
  1263. * This function is registered as 'curl_do' function. It decodes the path
  1264. * parts etc as a wrapper to the actual DO function (smtp_perform).
  1265. *
  1266. * The input argument is already checked for validity.
  1267. */
  1268. static CURLcode smtp_do(struct Curl_easy *data, bool *done)
  1269. {
  1270. CURLcode result = CURLE_OK;
  1271. DEBUGASSERT(data);
  1272. DEBUGASSERT(data->conn);
  1273. *done = FALSE; /* default to false */
  1274. /* Parse the custom request */
  1275. result = smtp_parse_custom_request(data);
  1276. if(result)
  1277. return result;
  1278. result = smtp_regular_transfer(data, done);
  1279. return result;
  1280. }
  1281. /***********************************************************************
  1282. *
  1283. * smtp_disconnect()
  1284. *
  1285. * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
  1286. * resources. BLOCKING.
  1287. */
  1288. static CURLcode smtp_disconnect(struct Curl_easy *data,
  1289. struct connectdata *conn,
  1290. bool dead_connection)
  1291. {
  1292. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1293. (void)data;
  1294. /* We cannot send quit unconditionally. If this connection is stale or
  1295. bad in any way, sending quit and waiting around here will make the
  1296. disconnect wait in vain and cause more problems than we need to. */
  1297. if(!dead_connection && conn->bits.protoconnstart) {
  1298. if(!smtp_perform_quit(data, conn))
  1299. (void)smtp_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
  1300. }
  1301. /* Disconnect from the server */
  1302. Curl_pp_disconnect(&smtpc->pp);
  1303. /* Cleanup the SASL module */
  1304. Curl_sasl_cleanup(conn, smtpc->sasl.authused);
  1305. /* Cleanup our connection based variables */
  1306. Curl_safefree(smtpc->domain);
  1307. return CURLE_OK;
  1308. }
  1309. /* Call this when the DO phase has completed */
  1310. static CURLcode smtp_dophase_done(struct Curl_easy *data, bool connected)
  1311. {
  1312. struct SMTP *smtp = data->req.p.smtp;
  1313. (void)connected;
  1314. if(smtp->transfer != PPTRANSFER_BODY)
  1315. /* no data to transfer */
  1316. Curl_xfer_setup_nop(data);
  1317. return CURLE_OK;
  1318. }
  1319. /* Called from multi.c while DOing */
  1320. static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
  1321. {
  1322. CURLcode result = smtp_multi_statemach(data, dophase_done);
  1323. if(result)
  1324. DEBUGF(infof(data, "DO phase failed"));
  1325. else if(*dophase_done) {
  1326. result = smtp_dophase_done(data, FALSE /* not connected */);
  1327. DEBUGF(infof(data, "DO phase is complete"));
  1328. }
  1329. return result;
  1330. }
  1331. /***********************************************************************
  1332. *
  1333. * smtp_regular_transfer()
  1334. *
  1335. * The input argument is already checked for validity.
  1336. *
  1337. * Performs all commands done before a regular transfer between a local and a
  1338. * remote host.
  1339. */
  1340. static CURLcode smtp_regular_transfer(struct Curl_easy *data,
  1341. bool *dophase_done)
  1342. {
  1343. CURLcode result = CURLE_OK;
  1344. bool connected = FALSE;
  1345. /* Make sure size is unknown at this point */
  1346. data->req.size = -1;
  1347. /* Set the progress data */
  1348. Curl_pgrsSetUploadCounter(data, 0);
  1349. Curl_pgrsSetDownloadCounter(data, 0);
  1350. Curl_pgrsSetUploadSize(data, -1);
  1351. Curl_pgrsSetDownloadSize(data, -1);
  1352. /* Carry out the perform */
  1353. result = smtp_perform(data, &connected, dophase_done);
  1354. /* Perform post DO phase operations if necessary */
  1355. if(!result && *dophase_done)
  1356. result = smtp_dophase_done(data, connected);
  1357. return result;
  1358. }
  1359. static CURLcode smtp_setup_connection(struct Curl_easy *data,
  1360. struct connectdata *conn)
  1361. {
  1362. CURLcode result;
  1363. /* Clear the TLS upgraded flag */
  1364. conn->bits.tls_upgraded = FALSE;
  1365. /* Initialise the SMTP layer */
  1366. result = smtp_init(data);
  1367. if(result)
  1368. return result;
  1369. return CURLE_OK;
  1370. }
  1371. /***********************************************************************
  1372. *
  1373. * smtp_parse_url_options()
  1374. *
  1375. * Parse the URL login options.
  1376. */
  1377. static CURLcode smtp_parse_url_options(struct connectdata *conn)
  1378. {
  1379. CURLcode result = CURLE_OK;
  1380. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1381. const char *ptr = conn->options;
  1382. while(!result && ptr && *ptr) {
  1383. const char *key = ptr;
  1384. const char *value;
  1385. while(*ptr && *ptr != '=')
  1386. ptr++;
  1387. value = ptr + 1;
  1388. while(*ptr && *ptr != ';')
  1389. ptr++;
  1390. if(strncasecompare(key, "AUTH=", 5))
  1391. result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
  1392. value, ptr - value);
  1393. else
  1394. result = CURLE_URL_MALFORMAT;
  1395. if(*ptr == ';')
  1396. ptr++;
  1397. }
  1398. return result;
  1399. }
  1400. /***********************************************************************
  1401. *
  1402. * smtp_parse_url_path()
  1403. *
  1404. * Parse the URL path into separate path components.
  1405. */
  1406. static CURLcode smtp_parse_url_path(struct Curl_easy *data)
  1407. {
  1408. /* The SMTP struct is already initialised in smtp_connect() */
  1409. struct connectdata *conn = data->conn;
  1410. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1411. const char *path = &data->state.up.path[1]; /* skip leading path */
  1412. char localhost[HOSTNAME_MAX + 1];
  1413. /* Calculate the path if necessary */
  1414. if(!*path) {
  1415. if(!Curl_gethostname(localhost, sizeof(localhost)))
  1416. path = localhost;
  1417. else
  1418. path = "localhost";
  1419. }
  1420. /* URL decode the path and use it as the domain in our EHLO */
  1421. return Curl_urldecode(path, 0, &smtpc->domain, NULL, REJECT_CTRL);
  1422. }
  1423. /***********************************************************************
  1424. *
  1425. * smtp_parse_custom_request()
  1426. *
  1427. * Parse the custom request.
  1428. */
  1429. static CURLcode smtp_parse_custom_request(struct Curl_easy *data)
  1430. {
  1431. CURLcode result = CURLE_OK;
  1432. struct SMTP *smtp = data->req.p.smtp;
  1433. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1434. /* URL decode the custom request */
  1435. if(custom)
  1436. result = Curl_urldecode(custom, 0, &smtp->custom, NULL, REJECT_CTRL);
  1437. return result;
  1438. }
  1439. /***********************************************************************
  1440. *
  1441. * smtp_parse_address()
  1442. *
  1443. * Parse the fully qualified mailbox address into a local address part and the
  1444. * hostname, converting the hostname to an IDN A-label, as per RFC-5890, if
  1445. * necessary.
  1446. *
  1447. * Parameters:
  1448. *
  1449. * conn [in] - The connection handle.
  1450. * fqma [in] - The fully qualified mailbox address (which may or
  1451. * may not contain UTF-8 characters).
  1452. * address [in/out] - A new allocated buffer which holds the local
  1453. * address part of the mailbox. This buffer must be
  1454. * free'ed by the caller.
  1455. * host [in/out] - The hostname structure that holds the original,
  1456. * and optionally encoded, hostname.
  1457. * Curl_free_idnconverted_hostname() must be called
  1458. * once the caller has finished with the structure.
  1459. *
  1460. * Returns CURLE_OK on success.
  1461. *
  1462. * Notes:
  1463. *
  1464. * Should a UTF-8 hostname require conversion to IDN ACE and we cannot honor
  1465. * that conversion then we shall return success. This allow the caller to send
  1466. * the data to the server as a U-label (as per RFC-6531 sect. 3.2).
  1467. *
  1468. * If an mailbox '@' separator cannot be located then the mailbox is considered
  1469. * to be either a local mailbox or an invalid mailbox (depending on what the
  1470. * calling function deems it to be) then the input will simply be returned in
  1471. * the address part with the hostname being NULL.
  1472. */
  1473. static CURLcode smtp_parse_address(const char *fqma, char **address,
  1474. struct hostname *host)
  1475. {
  1476. CURLcode result = CURLE_OK;
  1477. size_t length;
  1478. /* Duplicate the fully qualified email address so we can manipulate it,
  1479. ensuring it does not contain the delimiters if specified */
  1480. char *dup = strdup(fqma[0] == '<' ? fqma + 1 : fqma);
  1481. if(!dup)
  1482. return CURLE_OUT_OF_MEMORY;
  1483. length = strlen(dup);
  1484. if(length) {
  1485. if(dup[length - 1] == '>')
  1486. dup[length - 1] = '\0';
  1487. }
  1488. /* Extract the hostname from the address (if we can) */
  1489. host->name = strpbrk(dup, "@");
  1490. if(host->name) {
  1491. *host->name = '\0';
  1492. host->name = host->name + 1;
  1493. /* Attempt to convert the hostname to IDN ACE */
  1494. (void) Curl_idnconvert_hostname(host);
  1495. /* If Curl_idnconvert_hostname() fails then we shall attempt to continue
  1496. and send the hostname using UTF-8 rather than as 7-bit ACE (which is
  1497. our preference) */
  1498. }
  1499. /* Extract the local address from the mailbox */
  1500. *address = dup;
  1501. return result;
  1502. }
  1503. struct cr_eob_ctx {
  1504. struct Curl_creader super;
  1505. struct bufq buf;
  1506. size_t n_eob; /* how many EOB bytes we matched so far */
  1507. size_t eob; /* Number of bytes of the EOB (End Of Body) that
  1508. have been received so far */
  1509. BIT(read_eos); /* we read an EOS from the next reader */
  1510. BIT(eos); /* we have returned an EOS */
  1511. };
  1512. static CURLcode cr_eob_init(struct Curl_easy *data,
  1513. struct Curl_creader *reader)
  1514. {
  1515. struct cr_eob_ctx *ctx = reader->ctx;
  1516. (void)data;
  1517. /* The first char we read is the first on a line, as if we had
  1518. * read CRLF just before */
  1519. ctx->n_eob = 2;
  1520. Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
  1521. return CURLE_OK;
  1522. }
  1523. static void cr_eob_close(struct Curl_easy *data, struct Curl_creader *reader)
  1524. {
  1525. struct cr_eob_ctx *ctx = reader->ctx;
  1526. (void)data;
  1527. Curl_bufq_free(&ctx->buf);
  1528. }
  1529. /* this is the 5-bytes End-Of-Body marker for SMTP */
  1530. #define SMTP_EOB "\r\n.\r\n"
  1531. #define SMTP_EOB_FIND_LEN 3
  1532. /* client reader doing SMTP End-Of-Body escaping. */
  1533. static CURLcode cr_eob_read(struct Curl_easy *data,
  1534. struct Curl_creader *reader,
  1535. char *buf, size_t blen,
  1536. size_t *pnread, bool *peos)
  1537. {
  1538. struct cr_eob_ctx *ctx = reader->ctx;
  1539. CURLcode result = CURLE_OK;
  1540. size_t nread, i, start, n;
  1541. bool eos;
  1542. if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  1543. /* Get more and convert it when needed */
  1544. result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
  1545. if(result)
  1546. return result;
  1547. ctx->read_eos = eos;
  1548. if(nread) {
  1549. if(!ctx->n_eob && !memchr(buf, SMTP_EOB[0], nread)) {
  1550. /* not in the middle of a match, no EOB start found, just pass */
  1551. *pnread = nread;
  1552. *peos = FALSE;
  1553. return CURLE_OK;
  1554. }
  1555. /* scan for EOB (continuation) and convert */
  1556. for(i = start = 0; i < nread; ++i) {
  1557. if(ctx->n_eob >= SMTP_EOB_FIND_LEN) {
  1558. /* matched the EOB prefix and seeing additional char, add '.' */
  1559. result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
  1560. if(result)
  1561. return result;
  1562. result = Curl_bufq_cwrite(&ctx->buf, ".", 1, &n);
  1563. if(result)
  1564. return result;
  1565. ctx->n_eob = 0;
  1566. start = i;
  1567. if(data->state.infilesize > 0)
  1568. data->state.infilesize++;
  1569. }
  1570. if(buf[i] != SMTP_EOB[ctx->n_eob])
  1571. ctx->n_eob = 0;
  1572. if(buf[i] == SMTP_EOB[ctx->n_eob]) {
  1573. /* matching another char of the EOB */
  1574. ++ctx->n_eob;
  1575. }
  1576. }
  1577. /* add any remainder to buf */
  1578. if(start < nread) {
  1579. result = Curl_bufq_cwrite(&ctx->buf, buf + start, nread - start, &n);
  1580. if(result)
  1581. return result;
  1582. }
  1583. }
  1584. if(ctx->read_eos) {
  1585. /* if we last matched a CRLF or if the data was empty, add ".\r\n"
  1586. * to end the body. If we sent something and it did not end with "\r\n",
  1587. * add "\r\n.\r\n" to end the body */
  1588. const char *eob = SMTP_EOB;
  1589. switch(ctx->n_eob) {
  1590. case 2:
  1591. /* seen a CRLF at the end, just add the remainder */
  1592. eob = &SMTP_EOB[2];
  1593. break;
  1594. case 3:
  1595. /* ended with '\r\n.', we should escpe the last '.' */
  1596. eob = "." SMTP_EOB;
  1597. break;
  1598. default:
  1599. break;
  1600. }
  1601. result = Curl_bufq_cwrite(&ctx->buf, eob, strlen(eob), &n);
  1602. if(result)
  1603. return result;
  1604. }
  1605. }
  1606. *peos = FALSE;
  1607. if(!Curl_bufq_is_empty(&ctx->buf)) {
  1608. result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
  1609. }
  1610. else
  1611. *pnread = 0;
  1612. if(ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  1613. /* no more data, read all, done. */
  1614. ctx->eos = TRUE;
  1615. }
  1616. *peos = ctx->eos;
  1617. DEBUGF(infof(data, "cr_eob_read(%zu) -> %d, %zd, %d",
  1618. blen, result, *pnread, *peos));
  1619. return result;
  1620. }
  1621. static curl_off_t cr_eob_total_length(struct Curl_easy *data,
  1622. struct Curl_creader *reader)
  1623. {
  1624. /* this reader changes length depending on input */
  1625. (void)data;
  1626. (void)reader;
  1627. return -1;
  1628. }
  1629. static const struct Curl_crtype cr_eob = {
  1630. "cr-smtp-eob",
  1631. cr_eob_init,
  1632. cr_eob_read,
  1633. cr_eob_close,
  1634. Curl_creader_def_needs_rewind,
  1635. cr_eob_total_length,
  1636. Curl_creader_def_resume_from,
  1637. Curl_creader_def_rewind,
  1638. Curl_creader_def_unpause,
  1639. Curl_creader_def_is_paused,
  1640. Curl_creader_def_done,
  1641. sizeof(struct cr_eob_ctx)
  1642. };
  1643. static CURLcode cr_eob_add(struct Curl_easy *data)
  1644. {
  1645. struct Curl_creader *reader = NULL;
  1646. CURLcode result;
  1647. result = Curl_creader_create(&reader, data, &cr_eob,
  1648. CURL_CR_CONTENT_ENCODE);
  1649. if(!result)
  1650. result = Curl_creader_add(data, reader);
  1651. if(result && reader)
  1652. Curl_creader_free(data, reader);
  1653. return result;
  1654. }
  1655. #endif /* CURL_DISABLE_SMTP */