smtp.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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 host name parts. This is regardless of
  479. whether the host name is encoded using IDN ACE */
  480. bool utf8 = FALSE;
  481. if((!smtp->custom) || (!smtp->custom[0])) {
  482. char *address = NULL;
  483. struct hostname host = { NULL, NULL, NULL, NULL };
  484. /* Parse the mailbox to verify into the local address and host name
  485. parts, converting the host name to an IDN A-label if necessary */
  486. result = smtp_parse_address(smtp->rcpt->data,
  487. &address, &host);
  488. if(result)
  489. return result;
  490. /* Establish whether we should report SMTPUTF8 to the server for this
  491. mailbox as per RFC-6531 sect. 3.1 point 6 */
  492. utf8 = (conn->proto.smtpc.utf8_supported) &&
  493. ((host.encalloc) || (!Curl_is_ASCII_name(address)) ||
  494. (!Curl_is_ASCII_name(host.name)));
  495. /* Send the VRFY command (Note: The host name part may be absent when the
  496. host is a local system) */
  497. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp, "VRFY %s%s%s%s",
  498. address,
  499. host.name ? "@" : "",
  500. host.name ? host.name : "",
  501. utf8 ? " SMTPUTF8" : "");
  502. Curl_free_idnconverted_hostname(&host);
  503. free(address);
  504. }
  505. else {
  506. /* Establish whether we should report that we support SMTPUTF8 for EXPN
  507. commands to the server as per RFC-6531 sect. 3.1 point 6 */
  508. utf8 = (conn->proto.smtpc.utf8_supported) &&
  509. (!strcmp(smtp->custom, "EXPN"));
  510. /* Send the custom recipient based command such as the EXPN command */
  511. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp,
  512. "%s %s%s", smtp->custom,
  513. smtp->rcpt->data,
  514. utf8 ? " SMTPUTF8" : "");
  515. }
  516. }
  517. else
  518. /* Send the non-recipient based command such as HELP */
  519. result = Curl_pp_sendf(data, &conn->proto.smtpc.pp, "%s",
  520. smtp->custom && smtp->custom[0] != '\0' ?
  521. smtp->custom : "HELP");
  522. if(!result)
  523. smtp_state(data, SMTP_COMMAND);
  524. return result;
  525. }
  526. /***********************************************************************
  527. *
  528. * smtp_perform_mail()
  529. *
  530. * Sends an MAIL command to initiate the upload of a message.
  531. */
  532. static CURLcode smtp_perform_mail(struct Curl_easy *data)
  533. {
  534. char *from = NULL;
  535. char *auth = NULL;
  536. char *size = NULL;
  537. CURLcode result = CURLE_OK;
  538. struct connectdata *conn = data->conn;
  539. /* We notify the server we are sending UTF-8 data if a) it supports the
  540. SMTPUTF8 extension and b) The mailbox contains UTF-8 characters, in
  541. either the local address or host name parts. This is regardless of
  542. whether the host name is encoded using IDN ACE */
  543. bool utf8 = FALSE;
  544. /* Calculate the FROM parameter */
  545. if(data->set.str[STRING_MAIL_FROM]) {
  546. char *address = NULL;
  547. struct hostname host = { NULL, NULL, NULL, NULL };
  548. /* Parse the FROM mailbox into the local address and host name parts,
  549. converting the host name to an IDN A-label if necessary */
  550. result = smtp_parse_address(data->set.str[STRING_MAIL_FROM],
  551. &address, &host);
  552. if(result)
  553. 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'll simply let the server worry
  565. about that and reply with a 501 error */
  566. from = aprintf("<%s>", address);
  567. free(address);
  568. }
  569. else
  570. /* Null reverse-path, RFC-5321, sect. 3.6.3 */
  571. from = strdup("<>");
  572. if(!from) {
  573. 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 host name parts,
  582. converting the host name 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'll simply let the server
  599. worry about it */
  600. auth = aprintf("<%s>", address);
  601. free(address);
  602. }
  603. else
  604. /* Empty AUTH, RFC-2554, sect. 5 */
  605. auth = strdup("<>");
  606. if(!auth) {
  607. 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 don't include a UTF-8
  646. based address then quickly scan through the recipient list and check if
  647. any there do, as we need to correctly identify our support for SMTPUTF8
  648. in the envelope, as per RFC-6531 sect. 3.4 */
  649. if(conn->proto.smtpc.utf8_supported && !utf8) {
  650. struct SMTP *smtp = data->req.p.smtp;
  651. struct curl_slist *rcpt = smtp->rcpt;
  652. while(rcpt && !utf8) {
  653. /* Does the host name contain non-ASCII characters? */
  654. if(!Curl_is_ASCII_name(rcpt->data))
  655. utf8 = TRUE;
  656. rcpt = rcpt->next;
  657. }
  658. }
  659. /* 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 host name parts,
  696. converting the host name 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'll 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 don't 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's multiple RCPT TO to be issued, it's 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 weren't 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. 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. break;
  1067. case SMTP_AUTH:
  1068. result = smtp_state_auth_resp(data, smtpcode, smtpc->state);
  1069. break;
  1070. case SMTP_COMMAND:
  1071. result = smtp_state_command_resp(data, smtpcode, smtpc->state);
  1072. break;
  1073. case SMTP_MAIL:
  1074. result = smtp_state_mail_resp(data, smtpcode, smtpc->state);
  1075. break;
  1076. case SMTP_RCPT:
  1077. result = smtp_state_rcpt_resp(data, conn, smtpcode, smtpc->state);
  1078. break;
  1079. case SMTP_DATA:
  1080. result = smtp_state_data_resp(data, smtpcode, smtpc->state);
  1081. break;
  1082. case SMTP_POSTDATA:
  1083. result = smtp_state_postdata_resp(data, smtpcode, smtpc->state);
  1084. break;
  1085. case SMTP_QUIT:
  1086. default:
  1087. /* internal error */
  1088. smtp_state(data, SMTP_STOP);
  1089. break;
  1090. }
  1091. } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp));
  1092. return result;
  1093. }
  1094. /* Called repeatedly until done from multi.c */
  1095. static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done)
  1096. {
  1097. CURLcode result = CURLE_OK;
  1098. struct connectdata *conn = data->conn;
  1099. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1100. if((conn->handler->flags & PROTOPT_SSL) && !smtpc->ssldone) {
  1101. bool ssldone = FALSE;
  1102. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  1103. smtpc->ssldone = ssldone;
  1104. if(result || !smtpc->ssldone)
  1105. return result;
  1106. }
  1107. result = Curl_pp_statemach(data, &smtpc->pp, FALSE, FALSE);
  1108. *done = (smtpc->state == SMTP_STOP) ? TRUE : FALSE;
  1109. return result;
  1110. }
  1111. static CURLcode smtp_block_statemach(struct Curl_easy *data,
  1112. struct connectdata *conn,
  1113. bool disconnecting)
  1114. {
  1115. CURLcode result = CURLE_OK;
  1116. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1117. while(smtpc->state != SMTP_STOP && !result)
  1118. result = Curl_pp_statemach(data, &smtpc->pp, TRUE, disconnecting);
  1119. return result;
  1120. }
  1121. /* Allocate and initialize the SMTP struct for the current Curl_easy if
  1122. required */
  1123. static CURLcode smtp_init(struct Curl_easy *data)
  1124. {
  1125. CURLcode result = CURLE_OK;
  1126. struct SMTP *smtp;
  1127. smtp = data->req.p.smtp = calloc(1, sizeof(struct SMTP));
  1128. if(!smtp)
  1129. result = CURLE_OUT_OF_MEMORY;
  1130. return result;
  1131. }
  1132. /* For the SMTP "protocol connect" and "doing" phases only */
  1133. static int smtp_getsock(struct Curl_easy *data,
  1134. struct connectdata *conn, curl_socket_t *socks)
  1135. {
  1136. return Curl_pp_getsock(data, &conn->proto.smtpc.pp, socks);
  1137. }
  1138. /***********************************************************************
  1139. *
  1140. * smtp_connect()
  1141. *
  1142. * This function should do everything that is to be considered a part of
  1143. * the connection phase.
  1144. *
  1145. * The variable pointed to by 'done' will be TRUE if the protocol-layer
  1146. * connect phase is done when this function returns, or FALSE if not.
  1147. */
  1148. static CURLcode smtp_connect(struct Curl_easy *data, bool *done)
  1149. {
  1150. CURLcode result = CURLE_OK;
  1151. struct connectdata *conn = data->conn;
  1152. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1153. struct pingpong *pp = &smtpc->pp;
  1154. *done = FALSE; /* default to not done yet */
  1155. /* We always support persistent connections in SMTP */
  1156. connkeep(conn, "SMTP default");
  1157. PINGPONG_SETUP(pp, smtp_statemachine, smtp_endofresp);
  1158. /* Initialize the SASL storage */
  1159. Curl_sasl_init(&smtpc->sasl, data, &saslsmtp);
  1160. /* Initialise the pingpong layer */
  1161. Curl_pp_init(pp);
  1162. /* Parse the URL options */
  1163. result = smtp_parse_url_options(conn);
  1164. if(result)
  1165. return result;
  1166. /* Parse the URL path */
  1167. result = smtp_parse_url_path(data);
  1168. if(result)
  1169. return result;
  1170. /* Start off waiting for the server greeting response */
  1171. smtp_state(data, SMTP_SERVERGREET);
  1172. result = smtp_multi_statemach(data, done);
  1173. return result;
  1174. }
  1175. /***********************************************************************
  1176. *
  1177. * smtp_done()
  1178. *
  1179. * The DONE function. This does what needs to be done after a single DO has
  1180. * performed.
  1181. *
  1182. * Input argument is already checked for validity.
  1183. */
  1184. static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
  1185. bool premature)
  1186. {
  1187. CURLcode result = CURLE_OK;
  1188. struct connectdata *conn = data->conn;
  1189. struct SMTP *smtp = data->req.p.smtp;
  1190. (void)premature;
  1191. if(!smtp)
  1192. return CURLE_OK;
  1193. /* Cleanup our per-request based variables */
  1194. Curl_safefree(smtp->custom);
  1195. if(status) {
  1196. connclose(conn, "SMTP done with bad status"); /* marked for closure */
  1197. result = status; /* use the already set error code */
  1198. }
  1199. else if(!data->set.connect_only && data->set.mail_rcpt &&
  1200. (data->state.upload || IS_MIME_POST(data))) {
  1201. smtp_state(data, SMTP_POSTDATA);
  1202. /* Run the state-machine */
  1203. result = smtp_block_statemach(data, conn, FALSE);
  1204. }
  1205. /* Clear the transfer mode for the next request */
  1206. smtp->transfer = PPTRANSFER_BODY;
  1207. return result;
  1208. }
  1209. /***********************************************************************
  1210. *
  1211. * smtp_perform()
  1212. *
  1213. * This is the actual DO function for SMTP. Transfer a mail, send a command
  1214. * or get some data according to the options previously setup.
  1215. */
  1216. static CURLcode smtp_perform(struct Curl_easy *data, bool *connected,
  1217. bool *dophase_done)
  1218. {
  1219. /* This is SMTP and no proxy */
  1220. CURLcode result = CURLE_OK;
  1221. struct SMTP *smtp = data->req.p.smtp;
  1222. DEBUGF(infof(data, "DO phase starts"));
  1223. if(data->req.no_body) {
  1224. /* Requested no body means no transfer */
  1225. smtp->transfer = PPTRANSFER_INFO;
  1226. }
  1227. *dophase_done = FALSE; /* not done yet */
  1228. /* Store the first recipient (or NULL if not specified) */
  1229. smtp->rcpt = data->set.mail_rcpt;
  1230. /* Track of whether we've successfully sent at least one RCPT TO command */
  1231. smtp->rcpt_had_ok = FALSE;
  1232. /* Track of the last error we've received by sending RCPT TO command */
  1233. smtp->rcpt_last_error = 0;
  1234. /* Initial data character is the first character in line: it is implicitly
  1235. preceded by a virtual CRLF. */
  1236. smtp->trailing_crlf = TRUE;
  1237. smtp->eob = 2;
  1238. /* Start the first command in the DO phase */
  1239. if((data->state.upload || IS_MIME_POST(data)) && data->set.mail_rcpt)
  1240. /* MAIL transfer */
  1241. result = smtp_perform_mail(data);
  1242. else
  1243. /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
  1244. result = smtp_perform_command(data);
  1245. if(result)
  1246. return result;
  1247. /* Run the state-machine */
  1248. result = smtp_multi_statemach(data, dophase_done);
  1249. *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
  1250. if(*dophase_done)
  1251. DEBUGF(infof(data, "DO phase is complete"));
  1252. return result;
  1253. }
  1254. /***********************************************************************
  1255. *
  1256. * smtp_do()
  1257. *
  1258. * This function is registered as 'curl_do' function. It decodes the path
  1259. * parts etc as a wrapper to the actual DO function (smtp_perform).
  1260. *
  1261. * The input argument is already checked for validity.
  1262. */
  1263. static CURLcode smtp_do(struct Curl_easy *data, bool *done)
  1264. {
  1265. CURLcode result = CURLE_OK;
  1266. DEBUGASSERT(data);
  1267. DEBUGASSERT(data->conn);
  1268. *done = FALSE; /* default to false */
  1269. /* Parse the custom request */
  1270. result = smtp_parse_custom_request(data);
  1271. if(result)
  1272. return result;
  1273. result = smtp_regular_transfer(data, done);
  1274. return result;
  1275. }
  1276. /***********************************************************************
  1277. *
  1278. * smtp_disconnect()
  1279. *
  1280. * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
  1281. * resources. BLOCKING.
  1282. */
  1283. static CURLcode smtp_disconnect(struct Curl_easy *data,
  1284. struct connectdata *conn,
  1285. bool dead_connection)
  1286. {
  1287. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1288. (void)data;
  1289. /* We cannot send quit unconditionally. If this connection is stale or
  1290. bad in any way, sending quit and waiting around here will make the
  1291. disconnect wait in vain and cause more problems than we need to. */
  1292. if(!dead_connection && conn->bits.protoconnstart) {
  1293. if(!smtp_perform_quit(data, conn))
  1294. (void)smtp_block_statemach(data, conn, TRUE); /* ignore errors on QUIT */
  1295. }
  1296. /* Disconnect from the server */
  1297. Curl_pp_disconnect(&smtpc->pp);
  1298. /* Cleanup the SASL module */
  1299. Curl_sasl_cleanup(conn, smtpc->sasl.authused);
  1300. /* Cleanup our connection based variables */
  1301. Curl_safefree(smtpc->domain);
  1302. return CURLE_OK;
  1303. }
  1304. /* Call this when the DO phase has completed */
  1305. static CURLcode smtp_dophase_done(struct Curl_easy *data, bool connected)
  1306. {
  1307. struct SMTP *smtp = data->req.p.smtp;
  1308. (void)connected;
  1309. if(smtp->transfer != PPTRANSFER_BODY)
  1310. /* no data to transfer */
  1311. Curl_xfer_setup_nop(data);
  1312. return CURLE_OK;
  1313. }
  1314. /* Called from multi.c while DOing */
  1315. static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
  1316. {
  1317. CURLcode result = smtp_multi_statemach(data, dophase_done);
  1318. if(result)
  1319. DEBUGF(infof(data, "DO phase failed"));
  1320. else if(*dophase_done) {
  1321. result = smtp_dophase_done(data, FALSE /* not connected */);
  1322. DEBUGF(infof(data, "DO phase is complete"));
  1323. }
  1324. return result;
  1325. }
  1326. /***********************************************************************
  1327. *
  1328. * smtp_regular_transfer()
  1329. *
  1330. * The input argument is already checked for validity.
  1331. *
  1332. * Performs all commands done before a regular transfer between a local and a
  1333. * remote host.
  1334. */
  1335. static CURLcode smtp_regular_transfer(struct Curl_easy *data,
  1336. bool *dophase_done)
  1337. {
  1338. CURLcode result = CURLE_OK;
  1339. bool connected = FALSE;
  1340. /* Make sure size is unknown at this point */
  1341. data->req.size = -1;
  1342. /* Set the progress data */
  1343. Curl_pgrsSetUploadCounter(data, 0);
  1344. Curl_pgrsSetDownloadCounter(data, 0);
  1345. Curl_pgrsSetUploadSize(data, -1);
  1346. Curl_pgrsSetDownloadSize(data, -1);
  1347. /* Carry out the perform */
  1348. result = smtp_perform(data, &connected, dophase_done);
  1349. /* Perform post DO phase operations if necessary */
  1350. if(!result && *dophase_done)
  1351. result = smtp_dophase_done(data, connected);
  1352. return result;
  1353. }
  1354. static CURLcode smtp_setup_connection(struct Curl_easy *data,
  1355. struct connectdata *conn)
  1356. {
  1357. CURLcode result;
  1358. /* Clear the TLS upgraded flag */
  1359. conn->bits.tls_upgraded = FALSE;
  1360. /* Initialise the SMTP layer */
  1361. result = smtp_init(data);
  1362. if(result)
  1363. return result;
  1364. return CURLE_OK;
  1365. }
  1366. /***********************************************************************
  1367. *
  1368. * smtp_parse_url_options()
  1369. *
  1370. * Parse the URL login options.
  1371. */
  1372. static CURLcode smtp_parse_url_options(struct connectdata *conn)
  1373. {
  1374. CURLcode result = CURLE_OK;
  1375. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1376. const char *ptr = conn->options;
  1377. while(!result && ptr && *ptr) {
  1378. const char *key = ptr;
  1379. const char *value;
  1380. while(*ptr && *ptr != '=')
  1381. ptr++;
  1382. value = ptr + 1;
  1383. while(*ptr && *ptr != ';')
  1384. ptr++;
  1385. if(strncasecompare(key, "AUTH=", 5))
  1386. result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
  1387. value, ptr - value);
  1388. else
  1389. result = CURLE_URL_MALFORMAT;
  1390. if(*ptr == ';')
  1391. ptr++;
  1392. }
  1393. return result;
  1394. }
  1395. /***********************************************************************
  1396. *
  1397. * smtp_parse_url_path()
  1398. *
  1399. * Parse the URL path into separate path components.
  1400. */
  1401. static CURLcode smtp_parse_url_path(struct Curl_easy *data)
  1402. {
  1403. /* The SMTP struct is already initialised in smtp_connect() */
  1404. struct connectdata *conn = data->conn;
  1405. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1406. const char *path = &data->state.up.path[1]; /* skip leading path */
  1407. char localhost[HOSTNAME_MAX + 1];
  1408. /* Calculate the path if necessary */
  1409. if(!*path) {
  1410. if(!Curl_gethostname(localhost, sizeof(localhost)))
  1411. path = localhost;
  1412. else
  1413. path = "localhost";
  1414. }
  1415. /* URL decode the path and use it as the domain in our EHLO */
  1416. return Curl_urldecode(path, 0, &smtpc->domain, NULL, REJECT_CTRL);
  1417. }
  1418. /***********************************************************************
  1419. *
  1420. * smtp_parse_custom_request()
  1421. *
  1422. * Parse the custom request.
  1423. */
  1424. static CURLcode smtp_parse_custom_request(struct Curl_easy *data)
  1425. {
  1426. CURLcode result = CURLE_OK;
  1427. struct SMTP *smtp = data->req.p.smtp;
  1428. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1429. /* URL decode the custom request */
  1430. if(custom)
  1431. result = Curl_urldecode(custom, 0, &smtp->custom, NULL, REJECT_CTRL);
  1432. return result;
  1433. }
  1434. /***********************************************************************
  1435. *
  1436. * smtp_parse_address()
  1437. *
  1438. * Parse the fully qualified mailbox address into a local address part and the
  1439. * host name, converting the host name to an IDN A-label, as per RFC-5890, if
  1440. * necessary.
  1441. *
  1442. * Parameters:
  1443. *
  1444. * conn [in] - The connection handle.
  1445. * fqma [in] - The fully qualified mailbox address (which may or
  1446. * may not contain UTF-8 characters).
  1447. * address [in/out] - A new allocated buffer which holds the local
  1448. * address part of the mailbox. This buffer must be
  1449. * free'ed by the caller.
  1450. * host [in/out] - The host name structure that holds the original,
  1451. * and optionally encoded, host name.
  1452. * Curl_free_idnconverted_hostname() must be called
  1453. * once the caller has finished with the structure.
  1454. *
  1455. * Returns CURLE_OK on success.
  1456. *
  1457. * Notes:
  1458. *
  1459. * Should a UTF-8 host name require conversion to IDN ACE and we cannot honor
  1460. * that conversion then we shall return success. This allow the caller to send
  1461. * the data to the server as a U-label (as per RFC-6531 sect. 3.2).
  1462. *
  1463. * If an mailbox '@' separator cannot be located then the mailbox is considered
  1464. * to be either a local mailbox or an invalid mailbox (depending on what the
  1465. * calling function deems it to be) then the input will simply be returned in
  1466. * the address part with the host name being NULL.
  1467. */
  1468. static CURLcode smtp_parse_address(const char *fqma, char **address,
  1469. struct hostname *host)
  1470. {
  1471. CURLcode result = CURLE_OK;
  1472. size_t length;
  1473. /* Duplicate the fully qualified email address so we can manipulate it,
  1474. ensuring it doesn't contain the delimiters if specified */
  1475. char *dup = strdup(fqma[0] == '<' ? fqma + 1 : fqma);
  1476. if(!dup)
  1477. return CURLE_OUT_OF_MEMORY;
  1478. length = strlen(dup);
  1479. if(length) {
  1480. if(dup[length - 1] == '>')
  1481. dup[length - 1] = '\0';
  1482. }
  1483. /* Extract the host name from the address (if we can) */
  1484. host->name = strpbrk(dup, "@");
  1485. if(host->name) {
  1486. *host->name = '\0';
  1487. host->name = host->name + 1;
  1488. /* Attempt to convert the host name to IDN ACE */
  1489. (void) Curl_idnconvert_hostname(host);
  1490. /* If Curl_idnconvert_hostname() fails then we shall attempt to continue
  1491. and send the host name using UTF-8 rather than as 7-bit ACE (which is
  1492. our preference) */
  1493. }
  1494. /* Extract the local address from the mailbox */
  1495. *address = dup;
  1496. return result;
  1497. }
  1498. struct cr_eob_ctx {
  1499. struct Curl_creader super;
  1500. struct bufq buf;
  1501. size_t n_eob; /* how many EOB bytes we matched so far */
  1502. size_t eob; /* Number of bytes of the EOB (End Of Body) that
  1503. have been received so far */
  1504. BIT(read_eos); /* we read an EOS from the next reader */
  1505. BIT(eos); /* we have returned an EOS */
  1506. };
  1507. static CURLcode cr_eob_init(struct Curl_easy *data,
  1508. struct Curl_creader *reader)
  1509. {
  1510. struct cr_eob_ctx *ctx = reader->ctx;
  1511. (void)data;
  1512. /* The first char we read is the first on a line, as if we had
  1513. * read CRLF just before */
  1514. ctx->n_eob = 2;
  1515. Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
  1516. return CURLE_OK;
  1517. }
  1518. static void cr_eob_close(struct Curl_easy *data, struct Curl_creader *reader)
  1519. {
  1520. struct cr_eob_ctx *ctx = reader->ctx;
  1521. (void)data;
  1522. Curl_bufq_free(&ctx->buf);
  1523. }
  1524. /* this is the 5-bytes End-Of-Body marker for SMTP */
  1525. #define SMTP_EOB "\r\n.\r\n"
  1526. #define SMTP_EOB_FIND_LEN 3
  1527. /* client reader doing SMTP End-Of-Body escaping. */
  1528. static CURLcode cr_eob_read(struct Curl_easy *data,
  1529. struct Curl_creader *reader,
  1530. char *buf, size_t blen,
  1531. size_t *pnread, bool *peos)
  1532. {
  1533. struct cr_eob_ctx *ctx = reader->ctx;
  1534. CURLcode result = CURLE_OK;
  1535. size_t nread, i, start, n;
  1536. bool eos;
  1537. if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  1538. /* Get more and convert it when needed */
  1539. result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
  1540. if(result)
  1541. return result;
  1542. ctx->read_eos = eos;
  1543. if(nread) {
  1544. if(!ctx->n_eob && !memchr(buf, SMTP_EOB[0], nread)) {
  1545. /* not in the middle of a match, no EOB start found, just pass */
  1546. *pnread = nread;
  1547. *peos = FALSE;
  1548. return CURLE_OK;
  1549. }
  1550. /* scan for EOB (continuation) and convert */
  1551. for(i = start = 0; i < nread; ++i) {
  1552. if(ctx->n_eob >= SMTP_EOB_FIND_LEN) {
  1553. /* matched the EOB prefix and seeing additional char, add '.' */
  1554. result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
  1555. if(result)
  1556. return result;
  1557. result = Curl_bufq_cwrite(&ctx->buf, ".", 1, &n);
  1558. if(result)
  1559. return result;
  1560. ctx->n_eob = 0;
  1561. start = i;
  1562. if(data->state.infilesize > 0)
  1563. data->state.infilesize++;
  1564. }
  1565. if(buf[i] != SMTP_EOB[ctx->n_eob])
  1566. ctx->n_eob = 0;
  1567. if(buf[i] == SMTP_EOB[ctx->n_eob]) {
  1568. /* matching another char of the EOB */
  1569. ++ctx->n_eob;
  1570. }
  1571. }
  1572. /* add any remainder to buf */
  1573. if(start < nread) {
  1574. result = Curl_bufq_cwrite(&ctx->buf, buf + start, nread - start, &n);
  1575. if(result)
  1576. return result;
  1577. }
  1578. }
  1579. if(ctx->read_eos) {
  1580. /* if we last matched a CRLF or if the data was empty, add ".\r\n"
  1581. * to end the body. If we sent something and it did not end with "\r\n",
  1582. * add "\r\n.\r\n" to end the body */
  1583. const char *eob = SMTP_EOB;
  1584. switch(ctx->n_eob) {
  1585. case 2:
  1586. /* seen a CRLF at the end, just add the remainder */
  1587. eob = &SMTP_EOB[2];
  1588. break;
  1589. case 3:
  1590. /* ended with '\r\n.', we should escpe the last '.' */
  1591. eob = "." SMTP_EOB;
  1592. break;
  1593. default:
  1594. break;
  1595. }
  1596. result = Curl_bufq_cwrite(&ctx->buf, eob, strlen(eob), &n);
  1597. if(result)
  1598. return result;
  1599. }
  1600. }
  1601. *peos = FALSE;
  1602. if(!Curl_bufq_is_empty(&ctx->buf)) {
  1603. result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
  1604. }
  1605. else
  1606. *pnread = 0;
  1607. if(ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
  1608. /* no more data, read all, done. */
  1609. ctx->eos = TRUE;
  1610. }
  1611. *peos = ctx->eos;
  1612. DEBUGF(infof(data, "cr_eob_read(%zu) -> %d, %zd, %d",
  1613. blen, result, *pnread, *peos));
  1614. return result;
  1615. }
  1616. static curl_off_t cr_eob_total_length(struct Curl_easy *data,
  1617. struct Curl_creader *reader)
  1618. {
  1619. /* this reader changes length depending on input */
  1620. (void)data;
  1621. (void)reader;
  1622. return -1;
  1623. }
  1624. static const struct Curl_crtype cr_eob = {
  1625. "cr-smtp-eob",
  1626. cr_eob_init,
  1627. cr_eob_read,
  1628. cr_eob_close,
  1629. Curl_creader_def_needs_rewind,
  1630. cr_eob_total_length,
  1631. Curl_creader_def_resume_from,
  1632. Curl_creader_def_rewind,
  1633. Curl_creader_def_unpause,
  1634. Curl_creader_def_is_paused,
  1635. Curl_creader_def_done,
  1636. sizeof(struct cr_eob_ctx)
  1637. };
  1638. static CURLcode cr_eob_add(struct Curl_easy *data)
  1639. {
  1640. struct Curl_creader *reader = NULL;
  1641. CURLcode result;
  1642. result = Curl_creader_create(&reader, data, &cr_eob,
  1643. CURL_CR_CONTENT_ENCODE);
  1644. if(!result)
  1645. result = Curl_creader_add(data, reader);
  1646. if(result && reader)
  1647. Curl_creader_free(data, reader);
  1648. return result;
  1649. }
  1650. #endif /* CURL_DISABLE_SMTP */