smtp.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * RFC1870 SMTP Service Extension for Message Size
  22. * RFC2195 CRAM-MD5 authentication
  23. * RFC2831 DIGEST-MD5 authentication
  24. * RFC3207 SMTP over TLS
  25. * RFC4422 Simple Authentication and Security Layer (SASL)
  26. * RFC4616 PLAIN authentication
  27. * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
  28. * RFC4954 SMTP Authentication
  29. * RFC5321 SMTP protocol
  30. * RFC6749 OAuth 2.0 Authorization Framework
  31. * Draft SMTP URL Interface <draft-earhart-url-smtp-00.txt>
  32. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  33. *
  34. ***************************************************************************/
  35. #include "curl_setup.h"
  36. #ifndef CURL_DISABLE_SMTP
  37. #ifdef HAVE_NETINET_IN_H
  38. #include <netinet/in.h>
  39. #endif
  40. #ifdef HAVE_ARPA_INET_H
  41. #include <arpa/inet.h>
  42. #endif
  43. #ifdef HAVE_UTSNAME_H
  44. #include <sys/utsname.h>
  45. #endif
  46. #ifdef HAVE_NETDB_H
  47. #include <netdb.h>
  48. #endif
  49. #ifdef __VMS
  50. #include <in.h>
  51. #include <inet.h>
  52. #endif
  53. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  54. #undef in_addr_t
  55. #define in_addr_t unsigned long
  56. #endif
  57. #include <curl/curl.h>
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "if2ip.h"
  61. #include "hostip.h"
  62. #include "progress.h"
  63. #include "transfer.h"
  64. #include "escape.h"
  65. #include "http.h" /* for HTTP proxy tunnel stuff */
  66. #include "socks.h"
  67. #include "smtp.h"
  68. #include "strtoofft.h"
  69. #include "strequal.h"
  70. #include "vtls/vtls.h"
  71. #include "connect.h"
  72. #include "strerror.h"
  73. #include "select.h"
  74. #include "multiif.h"
  75. #include "url.h"
  76. #include "rawstr.h"
  77. #include "curl_gethostname.h"
  78. #include "curl_sasl.h"
  79. #include "warnless.h"
  80. #define _MPRINTF_REPLACE /* use our functions only */
  81. #include <curl/mprintf.h>
  82. #include "curl_memory.h"
  83. /* The last #include file should be: */
  84. #include "memdebug.h"
  85. /* Local API functions */
  86. static CURLcode smtp_regular_transfer(struct connectdata *conn, bool *done);
  87. static CURLcode smtp_do(struct connectdata *conn, bool *done);
  88. static CURLcode smtp_done(struct connectdata *conn, CURLcode status,
  89. bool premature);
  90. static CURLcode smtp_connect(struct connectdata *conn, bool *done);
  91. static CURLcode smtp_disconnect(struct connectdata *conn, bool dead);
  92. static CURLcode smtp_multi_statemach(struct connectdata *conn, bool *done);
  93. static int smtp_getsock(struct connectdata *conn, curl_socket_t *socks,
  94. int numsocks);
  95. static CURLcode smtp_doing(struct connectdata *conn, bool *dophase_done);
  96. static CURLcode smtp_setup_connection(struct connectdata *conn);
  97. static CURLcode smtp_parse_url_options(struct connectdata *conn);
  98. static CURLcode smtp_parse_url_path(struct connectdata *conn);
  99. static CURLcode smtp_parse_custom_request(struct connectdata *conn);
  100. static CURLcode smtp_calc_sasl_details(struct connectdata *conn,
  101. const char **mech,
  102. char **initresp, size_t *len,
  103. smtpstate *state1, smtpstate *state2);
  104. /*
  105. * SMTP protocol handler.
  106. */
  107. const struct Curl_handler Curl_handler_smtp = {
  108. "SMTP", /* scheme */
  109. smtp_setup_connection, /* setup_connection */
  110. smtp_do, /* do_it */
  111. smtp_done, /* done */
  112. ZERO_NULL, /* do_more */
  113. smtp_connect, /* connect_it */
  114. smtp_multi_statemach, /* connecting */
  115. smtp_doing, /* doing */
  116. smtp_getsock, /* proto_getsock */
  117. smtp_getsock, /* doing_getsock */
  118. ZERO_NULL, /* domore_getsock */
  119. ZERO_NULL, /* perform_getsock */
  120. smtp_disconnect, /* disconnect */
  121. ZERO_NULL, /* readwrite */
  122. PORT_SMTP, /* defport */
  123. CURLPROTO_SMTP, /* protocol */
  124. PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY /* flags */
  125. };
  126. #ifdef USE_SSL
  127. /*
  128. * SMTPS protocol handler.
  129. */
  130. const struct Curl_handler Curl_handler_smtps = {
  131. "SMTPS", /* scheme */
  132. smtp_setup_connection, /* setup_connection */
  133. smtp_do, /* do_it */
  134. smtp_done, /* done */
  135. ZERO_NULL, /* do_more */
  136. smtp_connect, /* connect_it */
  137. smtp_multi_statemach, /* connecting */
  138. smtp_doing, /* doing */
  139. smtp_getsock, /* proto_getsock */
  140. smtp_getsock, /* doing_getsock */
  141. ZERO_NULL, /* domore_getsock */
  142. ZERO_NULL, /* perform_getsock */
  143. smtp_disconnect, /* disconnect */
  144. ZERO_NULL, /* readwrite */
  145. PORT_SMTPS, /* defport */
  146. CURLPROTO_SMTPS, /* protocol */
  147. PROTOPT_CLOSEACTION | PROTOPT_SSL
  148. | PROTOPT_NOURLQUERY /* flags */
  149. };
  150. #endif
  151. #ifndef CURL_DISABLE_HTTP
  152. /*
  153. * HTTP-proxyed SMTP protocol handler.
  154. */
  155. static const struct Curl_handler Curl_handler_smtp_proxy = {
  156. "SMTP", /* scheme */
  157. Curl_http_setup_conn, /* setup_connection */
  158. Curl_http, /* do_it */
  159. Curl_http_done, /* done */
  160. ZERO_NULL, /* do_more */
  161. ZERO_NULL, /* connect_it */
  162. ZERO_NULL, /* connecting */
  163. ZERO_NULL, /* doing */
  164. ZERO_NULL, /* proto_getsock */
  165. ZERO_NULL, /* doing_getsock */
  166. ZERO_NULL, /* domore_getsock */
  167. ZERO_NULL, /* perform_getsock */
  168. ZERO_NULL, /* disconnect */
  169. ZERO_NULL, /* readwrite */
  170. PORT_SMTP, /* defport */
  171. CURLPROTO_HTTP, /* protocol */
  172. PROTOPT_NONE /* flags */
  173. };
  174. #ifdef USE_SSL
  175. /*
  176. * HTTP-proxyed SMTPS protocol handler.
  177. */
  178. static const struct Curl_handler Curl_handler_smtps_proxy = {
  179. "SMTPS", /* scheme */
  180. Curl_http_setup_conn, /* setup_connection */
  181. Curl_http, /* do_it */
  182. Curl_http_done, /* done */
  183. ZERO_NULL, /* do_more */
  184. ZERO_NULL, /* connect_it */
  185. ZERO_NULL, /* connecting */
  186. ZERO_NULL, /* doing */
  187. ZERO_NULL, /* proto_getsock */
  188. ZERO_NULL, /* doing_getsock */
  189. ZERO_NULL, /* domore_getsock */
  190. ZERO_NULL, /* perform_getsock */
  191. ZERO_NULL, /* disconnect */
  192. ZERO_NULL, /* readwrite */
  193. PORT_SMTPS, /* defport */
  194. CURLPROTO_HTTP, /* protocol */
  195. PROTOPT_NONE /* flags */
  196. };
  197. #endif
  198. #endif
  199. #ifdef USE_SSL
  200. static void smtp_to_smtps(struct connectdata *conn)
  201. {
  202. conn->handler = &Curl_handler_smtps;
  203. }
  204. #else
  205. #define smtp_to_smtps(x) Curl_nop_stmt
  206. #endif
  207. /***********************************************************************
  208. *
  209. * smtp_endofresp()
  210. *
  211. * Checks for an ending SMTP status code at the start of the given string, but
  212. * also detects various capabilities from the EHLO response including the
  213. * supported authentication mechanisms.
  214. */
  215. static bool smtp_endofresp(struct connectdata *conn, char *line, size_t len,
  216. int *resp)
  217. {
  218. struct smtp_conn *smtpc = &conn->proto.smtpc;
  219. bool result = FALSE;
  220. /* Nothing for us */
  221. if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2]))
  222. return FALSE;
  223. /* Do we have a command response? This should be the response code followed
  224. by a space and optionally some text as per RFC-5321 and as outlined in
  225. Section 4. Examples of RFC-4954 but some e-mail servers ignore this and
  226. only send the response code instead as per Section 4.2. */
  227. if(line[3] == ' ' || len == 5) {
  228. result = TRUE;
  229. *resp = curlx_sltosi(strtol(line, NULL, 10));
  230. /* Make sure real server never sends internal value */
  231. if(*resp == 1)
  232. *resp = 0;
  233. }
  234. /* Do we have a multiline (continuation) response? */
  235. else if(line[3] == '-' &&
  236. (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) {
  237. result = TRUE;
  238. *resp = 1; /* Internal response code */
  239. }
  240. return result;
  241. }
  242. /***********************************************************************
  243. *
  244. * smtp_get_message()
  245. *
  246. * Gets the authentication message from the response buffer.
  247. */
  248. static void smtp_get_message(char *buffer, char** outptr)
  249. {
  250. size_t len = 0;
  251. char* message = NULL;
  252. /* Find the start of the message */
  253. for(message = buffer + 4; *message == ' ' || *message == '\t'; message++)
  254. ;
  255. /* Find the end of the message */
  256. for(len = strlen(message); len--;)
  257. if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
  258. message[len] != '\t')
  259. break;
  260. /* Terminate the message */
  261. if(++len) {
  262. message[len] = '\0';
  263. }
  264. *outptr = message;
  265. }
  266. /***********************************************************************
  267. *
  268. * state()
  269. *
  270. * This is the ONLY way to change SMTP state!
  271. */
  272. static void state(struct connectdata *conn, smtpstate newstate)
  273. {
  274. struct smtp_conn *smtpc = &conn->proto.smtpc;
  275. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  276. /* for debug purposes */
  277. static const char * const names[] = {
  278. "STOP",
  279. "SERVERGREET",
  280. "EHLO",
  281. "HELO",
  282. "STARTTLS",
  283. "UPGRADETLS",
  284. "AUTH_PLAIN",
  285. "AUTH_LOGIN",
  286. "AUTH_LOGIN_PASSWD",
  287. "AUTH_CRAMMD5",
  288. "AUTH_DIGESTMD5",
  289. "AUTH_DIGESTMD5_RESP",
  290. "AUTH_NTLM",
  291. "AUTH_NTLM_TYPE2MSG",
  292. "AUTH_GSSAPI",
  293. "AUTH_GSSAPI_TOKEN",
  294. "AUTH_GSSAPI_NO_DATA",
  295. "AUTH_XOAUTH2",
  296. "AUTH_CANCEL",
  297. "AUTH_FINAL",
  298. "COMMAND",
  299. "MAIL",
  300. "RCPT",
  301. "DATA",
  302. "POSTDATA",
  303. "QUIT",
  304. /* LAST */
  305. };
  306. if(smtpc->state != newstate)
  307. infof(conn->data, "SMTP %p state change from %s to %s\n",
  308. (void *)smtpc, names[smtpc->state], names[newstate]);
  309. #endif
  310. smtpc->state = newstate;
  311. }
  312. /***********************************************************************
  313. *
  314. * smtp_perform_ehlo()
  315. *
  316. * Sends the EHLO command to not only initialise communication with the ESMTP
  317. * server but to also obtain a list of server side supported capabilities.
  318. */
  319. static CURLcode smtp_perform_ehlo(struct connectdata *conn)
  320. {
  321. CURLcode result = CURLE_OK;
  322. struct smtp_conn *smtpc = &conn->proto.smtpc;
  323. smtpc->authmechs = 0; /* No known authentication mechanisms yet */
  324. smtpc->authused = 0; /* Clear the authentication mechanism used
  325. for esmtp connections */
  326. smtpc->tls_supported = FALSE; /* Clear the TLS capability */
  327. smtpc->auth_supported = FALSE; /* Clear the AUTH capability */
  328. /* Send the EHLO command */
  329. result = Curl_pp_sendf(&smtpc->pp, "EHLO %s", smtpc->domain);
  330. if(!result)
  331. state(conn, SMTP_EHLO);
  332. return result;
  333. }
  334. /***********************************************************************
  335. *
  336. * smtp_perform_helo()
  337. *
  338. * Sends the HELO command to initialise communication with the SMTP server.
  339. */
  340. static CURLcode smtp_perform_helo(struct connectdata *conn)
  341. {
  342. CURLcode result = CURLE_OK;
  343. struct smtp_conn *smtpc = &conn->proto.smtpc;
  344. smtpc->authused = 0; /* No authentication mechanism used in smtp
  345. connections */
  346. /* Send the HELO command */
  347. result = Curl_pp_sendf(&smtpc->pp, "HELO %s", smtpc->domain);
  348. if(!result)
  349. state(conn, SMTP_HELO);
  350. return result;
  351. }
  352. /***********************************************************************
  353. *
  354. * smtp_perform_starttls()
  355. *
  356. * Sends the STLS command to start the upgrade to TLS.
  357. */
  358. static CURLcode smtp_perform_starttls(struct connectdata *conn)
  359. {
  360. CURLcode result = CURLE_OK;
  361. /* Send the STARTTLS command */
  362. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "STARTTLS");
  363. if(!result)
  364. state(conn, SMTP_STARTTLS);
  365. return result;
  366. }
  367. /***********************************************************************
  368. *
  369. * smtp_perform_upgrade_tls()
  370. *
  371. * Performs the upgrade to TLS.
  372. */
  373. static CURLcode smtp_perform_upgrade_tls(struct connectdata *conn)
  374. {
  375. CURLcode result = CURLE_OK;
  376. struct smtp_conn *smtpc = &conn->proto.smtpc;
  377. /* Start the SSL connection */
  378. result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &smtpc->ssldone);
  379. if(!result) {
  380. if(smtpc->state != SMTP_UPGRADETLS)
  381. state(conn, SMTP_UPGRADETLS);
  382. if(smtpc->ssldone) {
  383. smtp_to_smtps(conn);
  384. result = smtp_perform_ehlo(conn);
  385. }
  386. }
  387. return result;
  388. }
  389. /***********************************************************************
  390. *
  391. * smtp_perform_auth()
  392. *
  393. * Sends an AUTH command allowing the client to login with the given SASL
  394. * authentication mechanism.
  395. */
  396. static CURLcode smtp_perform_auth(struct connectdata *conn,
  397. const char *mech,
  398. const char *initresp, size_t len,
  399. smtpstate state1, smtpstate state2)
  400. {
  401. CURLcode result = CURLE_OK;
  402. struct smtp_conn *smtpc = &conn->proto.smtpc;
  403. if(initresp && 8 + strlen(mech) + len <= 512) { /* AUTH <mech> ...<crlf> */
  404. /* Send the AUTH command with the initial response */
  405. result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp);
  406. if(!result)
  407. state(conn, state2);
  408. }
  409. else {
  410. /* Send the AUTH command */
  411. result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech);
  412. if(!result)
  413. state(conn, state1);
  414. }
  415. return result;
  416. }
  417. /***********************************************************************
  418. *
  419. * smtp_perform_authentication()
  420. *
  421. * Initiates the authentication sequence, with the appropriate SASL
  422. * authentication mechanism.
  423. */
  424. static CURLcode smtp_perform_authentication(struct connectdata *conn)
  425. {
  426. CURLcode result = CURLE_OK;
  427. struct smtp_conn *smtpc = &conn->proto.smtpc;
  428. const char *mech = NULL;
  429. char *initresp = NULL;
  430. size_t len = 0;
  431. smtpstate state1 = SMTP_STOP;
  432. smtpstate state2 = SMTP_STOP;
  433. /* Check we have a username and password to authenticate with, and the
  434. server supports authentiation, and end the connect phase if not */
  435. if(!conn->bits.user_passwd || !smtpc->auth_supported) {
  436. state(conn, SMTP_STOP);
  437. return result;
  438. }
  439. /* Calculate the SASL login details */
  440. result = smtp_calc_sasl_details(conn, &mech, &initresp, &len, &state1,
  441. &state2);
  442. if(!result) {
  443. if(mech) {
  444. /* Perform SASL based authentication */
  445. result = smtp_perform_auth(conn, mech, initresp, len, state1, state2);
  446. Curl_safefree(initresp);
  447. }
  448. else {
  449. /* Other mechanisms not supported */
  450. infof(conn->data, "No known authentication mechanisms supported!\n");
  451. result = CURLE_LOGIN_DENIED;
  452. }
  453. }
  454. return result;
  455. }
  456. /***********************************************************************
  457. *
  458. * smtp_perform_command()
  459. *
  460. * Sends a SMTP based command.
  461. */
  462. static CURLcode smtp_perform_command(struct connectdata *conn)
  463. {
  464. CURLcode result = CURLE_OK;
  465. struct SessionHandle *data = conn->data;
  466. struct SMTP *smtp = data->req.protop;
  467. /* Send the command */
  468. if(smtp->rcpt)
  469. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s %s",
  470. smtp->custom && smtp->custom[0] != '\0' ?
  471. smtp->custom : "VRFY",
  472. smtp->rcpt->data);
  473. else
  474. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s",
  475. smtp->custom && smtp->custom[0] != '\0' ?
  476. smtp->custom : "HELP");
  477. if(!result)
  478. state(conn, SMTP_COMMAND);
  479. return result;
  480. }
  481. /***********************************************************************
  482. *
  483. * smtp_perform_mail()
  484. *
  485. * Sends an MAIL command to initiate the upload of a message.
  486. */
  487. static CURLcode smtp_perform_mail(struct connectdata *conn)
  488. {
  489. char *from = NULL;
  490. char *auth = NULL;
  491. char *size = NULL;
  492. CURLcode result = CURLE_OK;
  493. struct SessionHandle *data = conn->data;
  494. /* Calculate the FROM parameter */
  495. if(!data->set.str[STRING_MAIL_FROM])
  496. /* Null reverse-path, RFC-5321, sect. 3.6.3 */
  497. from = strdup("<>");
  498. else if(data->set.str[STRING_MAIL_FROM][0] == '<')
  499. from = aprintf("%s", data->set.str[STRING_MAIL_FROM]);
  500. else
  501. from = aprintf("<%s>", data->set.str[STRING_MAIL_FROM]);
  502. if(!from)
  503. return CURLE_OUT_OF_MEMORY;
  504. /* Calculate the optional AUTH parameter */
  505. if(data->set.str[STRING_MAIL_AUTH] && conn->proto.smtpc.authused) {
  506. if(data->set.str[STRING_MAIL_AUTH][0] != '\0')
  507. auth = aprintf("%s", data->set.str[STRING_MAIL_AUTH]);
  508. else
  509. /* Empty AUTH, RFC-2554, sect. 5 */
  510. auth = strdup("<>");
  511. if(!auth) {
  512. Curl_safefree(from);
  513. return CURLE_OUT_OF_MEMORY;
  514. }
  515. }
  516. /* Calculate the optional SIZE parameter */
  517. if(conn->proto.smtpc.size_supported && conn->data->state.infilesize > 0) {
  518. size = aprintf("%" CURL_FORMAT_CURL_OFF_T, data->state.infilesize);
  519. if(!size) {
  520. Curl_safefree(from);
  521. Curl_safefree(auth);
  522. return CURLE_OUT_OF_MEMORY;
  523. }
  524. }
  525. /* Send the MAIL command */
  526. if(!auth && !size)
  527. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  528. "MAIL FROM:%s", from);
  529. else if(auth && !size)
  530. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  531. "MAIL FROM:%s AUTH=%s", from, auth);
  532. else if(auth && size)
  533. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  534. "MAIL FROM:%s AUTH=%s SIZE=%s", from, auth, size);
  535. else
  536. result = Curl_pp_sendf(&conn->proto.smtpc.pp,
  537. "MAIL FROM:%s SIZE=%s", from, size);
  538. Curl_safefree(from);
  539. Curl_safefree(auth);
  540. Curl_safefree(size);
  541. if(!result)
  542. state(conn, SMTP_MAIL);
  543. return result;
  544. }
  545. /***********************************************************************
  546. *
  547. * smtp_perform_rcpt_to()
  548. *
  549. * Sends a RCPT TO command for a given recipient as part of the message upload
  550. * process.
  551. */
  552. static CURLcode smtp_perform_rcpt_to(struct connectdata *conn)
  553. {
  554. CURLcode result = CURLE_OK;
  555. struct SessionHandle *data = conn->data;
  556. struct SMTP *smtp = data->req.protop;
  557. /* Send the RCPT TO command */
  558. if(smtp->rcpt->data[0] == '<')
  559. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:%s",
  560. smtp->rcpt->data);
  561. else
  562. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:<%s>",
  563. smtp->rcpt->data);
  564. if(!result)
  565. state(conn, SMTP_RCPT);
  566. return result;
  567. }
  568. /***********************************************************************
  569. *
  570. * smtp_perform_quit()
  571. *
  572. * Performs the quit action prior to sclose() being called.
  573. */
  574. static CURLcode smtp_perform_quit(struct connectdata *conn)
  575. {
  576. CURLcode result = CURLE_OK;
  577. /* Send the QUIT command */
  578. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "QUIT");
  579. if(!result)
  580. state(conn, SMTP_QUIT);
  581. return result;
  582. }
  583. /* For the initial server greeting */
  584. static CURLcode smtp_state_servergreet_resp(struct connectdata *conn,
  585. int smtpcode,
  586. smtpstate instate)
  587. {
  588. CURLcode result = CURLE_OK;
  589. struct SessionHandle *data = conn->data;
  590. (void)instate; /* no use for this yet */
  591. if(smtpcode/100 != 2) {
  592. failf(data, "Got unexpected smtp-server response: %d", smtpcode);
  593. result = CURLE_FTP_WEIRD_SERVER_REPLY;
  594. }
  595. else
  596. result = smtp_perform_ehlo(conn);
  597. return result;
  598. }
  599. /* For STARTTLS responses */
  600. static CURLcode smtp_state_starttls_resp(struct connectdata *conn,
  601. int smtpcode,
  602. smtpstate instate)
  603. {
  604. CURLcode result = CURLE_OK;
  605. struct SessionHandle *data = conn->data;
  606. (void)instate; /* no use for this yet */
  607. if(smtpcode != 220) {
  608. if(data->set.use_ssl != CURLUSESSL_TRY) {
  609. failf(data, "STARTTLS denied. %c", smtpcode);
  610. result = CURLE_USE_SSL_FAILED;
  611. }
  612. else
  613. result = smtp_perform_authentication(conn);
  614. }
  615. else
  616. result = smtp_perform_upgrade_tls(conn);
  617. return result;
  618. }
  619. /* For EHLO responses */
  620. static CURLcode smtp_state_ehlo_resp(struct connectdata *conn, int smtpcode,
  621. smtpstate instate)
  622. {
  623. CURLcode result = CURLE_OK;
  624. struct SessionHandle *data = conn->data;
  625. struct smtp_conn *smtpc = &conn->proto.smtpc;
  626. const char *line = data->state.buffer;
  627. size_t len = strlen(line);
  628. size_t wordlen;
  629. (void)instate; /* no use for this yet */
  630. if(smtpcode/100 != 2 && smtpcode != 1) {
  631. if(data->set.use_ssl <= CURLUSESSL_TRY || conn->ssl[FIRSTSOCKET].use)
  632. result = smtp_perform_helo(conn);
  633. else {
  634. failf(data, "Remote access denied: %d", smtpcode);
  635. result = CURLE_REMOTE_ACCESS_DENIED;
  636. }
  637. }
  638. else {
  639. line += 4;
  640. len -= 4;
  641. /* Does the server support the STARTTLS capability? */
  642. if(len >= 8 && !memcmp(line, "STARTTLS", 8))
  643. smtpc->tls_supported = TRUE;
  644. /* Does the server support the SIZE capability? */
  645. else if(len >= 4 && !memcmp(line, "SIZE", 4))
  646. smtpc->size_supported = TRUE;
  647. /* Does the server support authentication? */
  648. else if(len >= 5 && !memcmp(line, "AUTH ", 5)) {
  649. smtpc->auth_supported = TRUE;
  650. /* Advance past the AUTH keyword */
  651. line += 5;
  652. len -= 5;
  653. /* Loop through the data line */
  654. for(;;) {
  655. while(len &&
  656. (*line == ' ' || *line == '\t' ||
  657. *line == '\r' || *line == '\n')) {
  658. line++;
  659. len--;
  660. }
  661. if(!len)
  662. break;
  663. /* Extract the word */
  664. for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
  665. line[wordlen] != '\t' && line[wordlen] != '\r' &&
  666. line[wordlen] != '\n';)
  667. wordlen++;
  668. /* Test the word for a matching authentication mechanism */
  669. if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_LOGIN))
  670. smtpc->authmechs |= SASL_MECH_LOGIN;
  671. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_PLAIN))
  672. smtpc->authmechs |= SASL_MECH_PLAIN;
  673. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_CRAM_MD5))
  674. smtpc->authmechs |= SASL_MECH_CRAM_MD5;
  675. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_DIGEST_MD5))
  676. smtpc->authmechs |= SASL_MECH_DIGEST_MD5;
  677. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_GSSAPI))
  678. smtpc->authmechs |= SASL_MECH_GSSAPI;
  679. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_EXTERNAL))
  680. smtpc->authmechs |= SASL_MECH_EXTERNAL;
  681. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_NTLM))
  682. smtpc->authmechs |= SASL_MECH_NTLM;
  683. else if(sasl_mech_equal(line, wordlen, SASL_MECH_STRING_XOAUTH2))
  684. smtpc->authmechs |= SASL_MECH_XOAUTH2;
  685. line += wordlen;
  686. len -= wordlen;
  687. }
  688. }
  689. if(smtpcode != 1) {
  690. if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
  691. /* We don't have a SSL/TLS connection yet, but SSL is requested */
  692. if(smtpc->tls_supported)
  693. /* Switch to TLS connection now */
  694. result = smtp_perform_starttls(conn);
  695. else if(data->set.use_ssl == CURLUSESSL_TRY)
  696. /* Fallback and carry on with authentication */
  697. result = smtp_perform_authentication(conn);
  698. else {
  699. failf(data, "STARTTLS not supported.");
  700. result = CURLE_USE_SSL_FAILED;
  701. }
  702. }
  703. else
  704. result = smtp_perform_authentication(conn);
  705. }
  706. }
  707. return result;
  708. }
  709. /* For HELO responses */
  710. static CURLcode smtp_state_helo_resp(struct connectdata *conn, int smtpcode,
  711. smtpstate instate)
  712. {
  713. CURLcode result = CURLE_OK;
  714. struct SessionHandle *data = conn->data;
  715. (void)instate; /* no use for this yet */
  716. if(smtpcode/100 != 2) {
  717. failf(data, "Remote access denied: %d", smtpcode);
  718. result = CURLE_REMOTE_ACCESS_DENIED;
  719. }
  720. else
  721. /* End of connect phase */
  722. state(conn, SMTP_STOP);
  723. return result;
  724. }
  725. /* For AUTH PLAIN (without initial response) responses */
  726. static CURLcode smtp_state_auth_plain_resp(struct connectdata *conn,
  727. int smtpcode,
  728. smtpstate instate)
  729. {
  730. CURLcode result = CURLE_OK;
  731. struct SessionHandle *data = conn->data;
  732. size_t len = 0;
  733. char *plainauth = NULL;
  734. (void)instate; /* no use for this yet */
  735. if(smtpcode != 334) {
  736. failf(data, "Access denied: %d", smtpcode);
  737. result = CURLE_LOGIN_DENIED;
  738. }
  739. else {
  740. /* Create the authorisation message */
  741. result = Curl_sasl_create_plain_message(conn->data, conn->user,
  742. conn->passwd, &plainauth, &len);
  743. if(!result && plainauth) {
  744. /* Send the message */
  745. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", plainauth);
  746. if(!result)
  747. state(conn, SMTP_AUTH_FINAL);
  748. }
  749. }
  750. Curl_safefree(plainauth);
  751. return result;
  752. }
  753. /* For AUTH LOGIN (without initial response) responses */
  754. static CURLcode smtp_state_auth_login_resp(struct connectdata *conn,
  755. int smtpcode,
  756. smtpstate instate)
  757. {
  758. CURLcode result = CURLE_OK;
  759. struct SessionHandle *data = conn->data;
  760. size_t len = 0;
  761. char *authuser = NULL;
  762. (void)instate; /* no use for this yet */
  763. if(smtpcode != 334) {
  764. failf(data, "Access denied: %d", smtpcode);
  765. result = CURLE_LOGIN_DENIED;
  766. }
  767. else {
  768. /* Create the user message */
  769. result = Curl_sasl_create_login_message(conn->data, conn->user,
  770. &authuser, &len);
  771. if(!result && authuser) {
  772. /* Send the user */
  773. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", authuser);
  774. if(!result)
  775. state(conn, SMTP_AUTH_LOGIN_PASSWD);
  776. }
  777. }
  778. Curl_safefree(authuser);
  779. return result;
  780. }
  781. /* For AUTH LOGIN user entry responses */
  782. static CURLcode smtp_state_auth_login_password_resp(struct connectdata *conn,
  783. int smtpcode,
  784. smtpstate instate)
  785. {
  786. CURLcode result = CURLE_OK;
  787. struct SessionHandle *data = conn->data;
  788. size_t len = 0;
  789. char *authpasswd = NULL;
  790. (void)instate; /* no use for this yet */
  791. if(smtpcode != 334) {
  792. failf(data, "Access denied: %d", smtpcode);
  793. result = CURLE_LOGIN_DENIED;
  794. }
  795. else {
  796. /* Create the password message */
  797. result = Curl_sasl_create_login_message(conn->data, conn->passwd,
  798. &authpasswd, &len);
  799. if(!result && authpasswd) {
  800. /* Send the password */
  801. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", authpasswd);
  802. if(!result)
  803. state(conn, SMTP_AUTH_FINAL);
  804. }
  805. }
  806. Curl_safefree(authpasswd);
  807. return result;
  808. }
  809. #ifndef CURL_DISABLE_CRYPTO_AUTH
  810. /* For AUTH CRAM-MD5 responses */
  811. static CURLcode smtp_state_auth_cram_resp(struct connectdata *conn,
  812. int smtpcode,
  813. smtpstate instate)
  814. {
  815. CURLcode result = CURLE_OK;
  816. struct SessionHandle *data = conn->data;
  817. char *chlg = NULL;
  818. char *chlg64 = NULL;
  819. char *rplyb64 = NULL;
  820. size_t len = 0;
  821. (void)instate; /* no use for this yet */
  822. if(smtpcode != 334) {
  823. failf(data, "Access denied: %d", smtpcode);
  824. return CURLE_LOGIN_DENIED;
  825. }
  826. /* Get the challenge message */
  827. smtp_get_message(data->state.buffer, &chlg64);
  828. /* Decode the challenge message */
  829. result = Curl_sasl_decode_cram_md5_message(chlg64, &chlg, &len);
  830. if(result) {
  831. /* Send the cancellation */
  832. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "*");
  833. if(!result)
  834. state(conn, SMTP_AUTH_CANCEL);
  835. }
  836. else {
  837. /* Create the response message */
  838. result = Curl_sasl_create_cram_md5_message(data, chlg, conn->user,
  839. conn->passwd, &rplyb64, &len);
  840. if(!result && rplyb64) {
  841. /* Send the response */
  842. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", rplyb64);
  843. if(!result)
  844. state(conn, SMTP_AUTH_FINAL);
  845. }
  846. }
  847. Curl_safefree(chlg);
  848. Curl_safefree(rplyb64);
  849. return result;
  850. }
  851. /* For AUTH DIGEST-MD5 challenge responses */
  852. static CURLcode smtp_state_auth_digest_resp(struct connectdata *conn,
  853. int smtpcode,
  854. smtpstate instate)
  855. {
  856. CURLcode result = CURLE_OK;
  857. struct SessionHandle *data = conn->data;
  858. char *chlg64 = NULL;
  859. char *rplyb64 = NULL;
  860. size_t len = 0;
  861. (void)instate; /* no use for this yet */
  862. if(smtpcode != 334) {
  863. failf(data, "Access denied: %d", smtpcode);
  864. return CURLE_LOGIN_DENIED;
  865. }
  866. /* Get the challenge message */
  867. smtp_get_message(data->state.buffer, &chlg64);
  868. /* Create the response message */
  869. result = Curl_sasl_create_digest_md5_message(data, chlg64,
  870. conn->user, conn->passwd,
  871. "smtp", &rplyb64, &len);
  872. if(result) {
  873. if(result == CURLE_BAD_CONTENT_ENCODING) {
  874. /* Send the cancellation */
  875. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "*");
  876. if(!result)
  877. state(conn, SMTP_AUTH_CANCEL);
  878. }
  879. }
  880. else {
  881. /* Send the response */
  882. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", rplyb64);
  883. if(!result)
  884. state(conn, SMTP_AUTH_DIGESTMD5_RESP);
  885. }
  886. Curl_safefree(rplyb64);
  887. return result;
  888. }
  889. /* For AUTH DIGEST-MD5 challenge-response responses */
  890. static CURLcode smtp_state_auth_digest_resp_resp(struct connectdata *conn,
  891. int smtpcode,
  892. smtpstate instate)
  893. {
  894. CURLcode result = CURLE_OK;
  895. struct SessionHandle *data = conn->data;
  896. (void)instate; /* no use for this yet */
  897. if(smtpcode != 334) {
  898. failf(data, "Authentication failed: %d", smtpcode);
  899. result = CURLE_LOGIN_DENIED;
  900. }
  901. else {
  902. /* Send an empty response */
  903. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "");
  904. if(!result)
  905. state(conn, SMTP_AUTH_FINAL);
  906. }
  907. return result;
  908. }
  909. #endif
  910. #ifdef USE_NTLM
  911. /* For AUTH NTLM (without initial response) responses */
  912. static CURLcode smtp_state_auth_ntlm_resp(struct connectdata *conn,
  913. int smtpcode,
  914. smtpstate instate)
  915. {
  916. CURLcode result = CURLE_OK;
  917. struct SessionHandle *data = conn->data;
  918. char *type1msg = NULL;
  919. size_t len = 0;
  920. (void)instate; /* no use for this yet */
  921. if(smtpcode != 334) {
  922. failf(data, "Access denied: %d", smtpcode);
  923. result = CURLE_LOGIN_DENIED;
  924. }
  925. else {
  926. /* Create the type-1 message */
  927. result = Curl_sasl_create_ntlm_type1_message(conn->user, conn->passwd,
  928. &conn->ntlm,
  929. &type1msg, &len);
  930. if(!result && type1msg) {
  931. /* Send the message */
  932. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", type1msg);
  933. if(!result)
  934. state(conn, SMTP_AUTH_NTLM_TYPE2MSG);
  935. }
  936. }
  937. Curl_safefree(type1msg);
  938. return result;
  939. }
  940. /* For NTLM type-2 responses (sent in reponse to our type-1 message) */
  941. static CURLcode smtp_state_auth_ntlm_type2msg_resp(struct connectdata *conn,
  942. int smtpcode,
  943. smtpstate instate)
  944. {
  945. CURLcode result = CURLE_OK;
  946. struct SessionHandle *data = conn->data;
  947. char *type2msg = NULL;
  948. char *type3msg = NULL;
  949. size_t len = 0;
  950. (void)instate; /* no use for this yet */
  951. if(smtpcode != 334) {
  952. failf(data, "Access denied: %d", smtpcode);
  953. result = CURLE_LOGIN_DENIED;
  954. }
  955. else {
  956. /* Get the type-2 message */
  957. smtp_get_message(data->state.buffer, &type2msg);
  958. /* Decode the type-2 message */
  959. result = Curl_sasl_decode_ntlm_type2_message(data, type2msg, &conn->ntlm);
  960. if(result) {
  961. /* Send the cancellation */
  962. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "*");
  963. if(!result)
  964. state(conn, SMTP_AUTH_CANCEL);
  965. }
  966. else {
  967. /* Create the type-3 message */
  968. result = Curl_sasl_create_ntlm_type3_message(data, conn->user,
  969. conn->passwd, &conn->ntlm,
  970. &type3msg, &len);
  971. if(!result && type3msg) {
  972. /* Send the message */
  973. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", type3msg);
  974. if(!result)
  975. state(conn, SMTP_AUTH_FINAL);
  976. }
  977. }
  978. }
  979. Curl_safefree(type3msg);
  980. return result;
  981. }
  982. #endif
  983. #if defined(USE_KRB5)
  984. /* For AUTH GSSAPI (without initial response) responses */
  985. static CURLcode smtp_state_auth_gssapi_resp(struct connectdata *conn,
  986. int smtpcode,
  987. smtpstate instate)
  988. {
  989. CURLcode result = CURLE_OK;
  990. struct SessionHandle *data = conn->data;
  991. struct smtp_conn *smtpc = &conn->proto.smtpc;
  992. char *respmsg = NULL;
  993. size_t len = 0;
  994. (void)instate; /* no use for this yet */
  995. if(smtpcode != 334) {
  996. failf(data, "Access denied: %d", smtpcode);
  997. result = CURLE_LOGIN_DENIED;
  998. }
  999. else {
  1000. /* Create the initial response message */
  1001. result = Curl_sasl_create_gssapi_user_message(data, conn->user,
  1002. conn->passwd, "smtp",
  1003. smtpc->mutual_auth, NULL,
  1004. &conn->krb5,
  1005. &respmsg, &len);
  1006. if(!result && respmsg) {
  1007. /* Send the message */
  1008. result = Curl_pp_sendf(&smtpc->pp, "%s", respmsg);
  1009. if(!result)
  1010. state(conn, SMTP_AUTH_GSSAPI_TOKEN);
  1011. }
  1012. }
  1013. Curl_safefree(respmsg);
  1014. return result;
  1015. }
  1016. /* For AUTH GSSAPI user token responses */
  1017. static CURLcode smtp_state_auth_gssapi_token_resp(struct connectdata *conn,
  1018. int smtpcode,
  1019. smtpstate instate)
  1020. {
  1021. CURLcode result = CURLE_OK;
  1022. struct SessionHandle *data = conn->data;
  1023. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1024. char *chlgmsg = NULL;
  1025. char *respmsg = NULL;
  1026. size_t len = 0;
  1027. (void)instate; /* no use for this yet */
  1028. if(smtpcode != 334) {
  1029. failf(data, "Access denied: %d", smtpcode);
  1030. result = CURLE_LOGIN_DENIED;
  1031. }
  1032. else {
  1033. /* Get the challenge message */
  1034. smtp_get_message(data->state.buffer, &chlgmsg);
  1035. if(smtpc->mutual_auth)
  1036. /* Decode the user token challenge and create the optional response
  1037. message */
  1038. result = Curl_sasl_create_gssapi_user_message(data, NULL, NULL, NULL,
  1039. smtpc->mutual_auth,
  1040. chlgmsg, &conn->krb5,
  1041. &respmsg, &len);
  1042. else
  1043. /* Decode the security challenge and create the response message */
  1044. result = Curl_sasl_create_gssapi_security_message(data, chlgmsg,
  1045. &conn->krb5,
  1046. &respmsg, &len);
  1047. if(result) {
  1048. if(result == CURLE_BAD_CONTENT_ENCODING) {
  1049. /* Send the cancellation */
  1050. result = Curl_pp_sendf(&smtpc->pp, "%s", "*");
  1051. if(!result)
  1052. state(conn, SMTP_AUTH_CANCEL);
  1053. }
  1054. }
  1055. else {
  1056. /* Send the response */
  1057. if(respmsg)
  1058. result = Curl_pp_sendf(&smtpc->pp, "%s", respmsg);
  1059. else
  1060. result = Curl_pp_sendf(&smtpc->pp, "%s", "");
  1061. if(!result)
  1062. state(conn, (smtpc->mutual_auth ? SMTP_AUTH_GSSAPI_NO_DATA :
  1063. SMTP_AUTH_FINAL));
  1064. }
  1065. }
  1066. Curl_safefree(respmsg);
  1067. return result;
  1068. }
  1069. /* For AUTH GSSAPI no data responses */
  1070. static CURLcode smtp_state_auth_gssapi_no_data_resp(struct connectdata *conn,
  1071. int smtpcode,
  1072. smtpstate instate)
  1073. {
  1074. CURLcode result = CURLE_OK;
  1075. struct SessionHandle *data = conn->data;
  1076. char *chlgmsg = NULL;
  1077. char *respmsg = NULL;
  1078. size_t len = 0;
  1079. (void)instate; /* no use for this yet */
  1080. if(smtpcode != 334) {
  1081. failf(data, "Access denied: %d", smtpcode);
  1082. result = CURLE_LOGIN_DENIED;
  1083. }
  1084. else {
  1085. /* Get the challenge message */
  1086. smtp_get_message(data->state.buffer, &chlgmsg);
  1087. /* Decode the security challenge and create the response message */
  1088. result = Curl_sasl_create_gssapi_security_message(data, chlgmsg,
  1089. &conn->krb5,
  1090. &respmsg, &len);
  1091. if(result) {
  1092. if(result == CURLE_BAD_CONTENT_ENCODING) {
  1093. /* Send the cancellation */
  1094. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "*");
  1095. if(!result)
  1096. state(conn, SMTP_AUTH_CANCEL);
  1097. }
  1098. }
  1099. else {
  1100. /* Send the response */
  1101. if(respmsg) {
  1102. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", respmsg);
  1103. if(!result)
  1104. state(conn, SMTP_AUTH_FINAL);
  1105. }
  1106. }
  1107. }
  1108. Curl_safefree(respmsg);
  1109. return result;
  1110. }
  1111. #endif
  1112. /* For AUTH XOAUTH2 (without initial response) responses */
  1113. static CURLcode smtp_state_auth_xoauth2_resp(struct connectdata *conn,
  1114. int smtpcode, smtpstate instate)
  1115. {
  1116. CURLcode result = CURLE_OK;
  1117. struct SessionHandle *data = conn->data;
  1118. size_t len = 0;
  1119. char *xoauth = NULL;
  1120. (void)instate; /* no use for this yet */
  1121. if(smtpcode != 334) {
  1122. failf(data, "Access denied: %d", smtpcode);
  1123. result = CURLE_LOGIN_DENIED;
  1124. }
  1125. else {
  1126. /* Create the authorisation message */
  1127. result = Curl_sasl_create_xoauth2_message(conn->data, conn->user,
  1128. conn->xoauth2_bearer,
  1129. &xoauth, &len);
  1130. if(!result && xoauth) {
  1131. /* Send the message */
  1132. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", xoauth);
  1133. if(!result)
  1134. state(conn, SMTP_AUTH_FINAL);
  1135. }
  1136. }
  1137. Curl_safefree(xoauth);
  1138. return result;
  1139. }
  1140. /* For AUTH cancellation responses */
  1141. static CURLcode smtp_state_auth_cancel_resp(struct connectdata *conn,
  1142. int smtpcode,
  1143. smtpstate instate)
  1144. {
  1145. CURLcode result = CURLE_OK;
  1146. struct SessionHandle *data = conn->data;
  1147. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1148. const char *mech = NULL;
  1149. char *initresp = NULL;
  1150. size_t len = 0;
  1151. smtpstate state1 = SMTP_STOP;
  1152. smtpstate state2 = SMTP_STOP;
  1153. (void)smtpcode;
  1154. (void)instate; /* no use for this yet */
  1155. /* Remove the offending mechanism from the supported list */
  1156. smtpc->authmechs ^= smtpc->authused;
  1157. /* Calculate alternative SASL login details */
  1158. result = smtp_calc_sasl_details(conn, &mech, &initresp, &len, &state1,
  1159. &state2);
  1160. if(!result) {
  1161. /* Do we have any mechanisms left? */
  1162. if(mech) {
  1163. /* Retry SASL based authentication */
  1164. result = smtp_perform_auth(conn, mech, initresp, len, state1, state2);
  1165. Curl_safefree(initresp);
  1166. }
  1167. else {
  1168. failf(data, "Authentication cancelled");
  1169. result = CURLE_LOGIN_DENIED;
  1170. }
  1171. }
  1172. return result;
  1173. }
  1174. /* For final responses in the AUTH sequence */
  1175. static CURLcode smtp_state_auth_final_resp(struct connectdata *conn,
  1176. int smtpcode,
  1177. smtpstate instate)
  1178. {
  1179. CURLcode result = CURLE_OK;
  1180. struct SessionHandle *data = conn->data;
  1181. (void)instate; /* no use for this yet */
  1182. if(smtpcode != 235) {
  1183. failf(data, "Authentication failed: %d", smtpcode);
  1184. result = CURLE_LOGIN_DENIED;
  1185. }
  1186. else
  1187. /* End of connect phase */
  1188. state(conn, SMTP_STOP);
  1189. return result;
  1190. }
  1191. /* For command responses */
  1192. static CURLcode smtp_state_command_resp(struct connectdata *conn, int smtpcode,
  1193. smtpstate instate)
  1194. {
  1195. CURLcode result = CURLE_OK;
  1196. struct SessionHandle *data = conn->data;
  1197. struct SMTP *smtp = data->req.protop;
  1198. char *line = data->state.buffer;
  1199. size_t len = strlen(line);
  1200. (void)instate; /* no use for this yet */
  1201. if((smtp->rcpt && smtpcode/100 != 2 && smtpcode != 553 && smtpcode != 1) ||
  1202. (!smtp->rcpt && smtpcode/100 != 2 && smtpcode != 1)) {
  1203. failf(data, "Command failed: %d", smtpcode);
  1204. result = CURLE_RECV_ERROR;
  1205. }
  1206. else {
  1207. /* Temporarily add the LF character back and send as body to the client */
  1208. if(!data->set.opt_no_body) {
  1209. line[len] = '\n';
  1210. result = Curl_client_write(conn, CLIENTWRITE_BODY, line, len + 1);
  1211. line[len] = '\0';
  1212. }
  1213. if(smtpcode != 1) {
  1214. if(smtp->rcpt) {
  1215. smtp->rcpt = smtp->rcpt->next;
  1216. if(smtp->rcpt) {
  1217. /* Send the next command */
  1218. result = smtp_perform_command(conn);
  1219. }
  1220. else
  1221. /* End of DO phase */
  1222. state(conn, SMTP_STOP);
  1223. }
  1224. else
  1225. /* End of DO phase */
  1226. state(conn, SMTP_STOP);
  1227. }
  1228. }
  1229. return result;
  1230. }
  1231. /* For MAIL responses */
  1232. static CURLcode smtp_state_mail_resp(struct connectdata *conn, int smtpcode,
  1233. smtpstate instate)
  1234. {
  1235. CURLcode result = CURLE_OK;
  1236. struct SessionHandle *data = conn->data;
  1237. (void)instate; /* no use for this yet */
  1238. if(smtpcode/100 != 2) {
  1239. failf(data, "MAIL failed: %d", smtpcode);
  1240. result = CURLE_SEND_ERROR;
  1241. }
  1242. else
  1243. /* Start the RCPT TO command */
  1244. result = smtp_perform_rcpt_to(conn);
  1245. return result;
  1246. }
  1247. /* For RCPT responses */
  1248. static CURLcode smtp_state_rcpt_resp(struct connectdata *conn, int smtpcode,
  1249. smtpstate instate)
  1250. {
  1251. CURLcode result = CURLE_OK;
  1252. struct SessionHandle *data = conn->data;
  1253. struct SMTP *smtp = data->req.protop;
  1254. (void)instate; /* no use for this yet */
  1255. if(smtpcode/100 != 2) {
  1256. failf(data, "RCPT failed: %d", smtpcode);
  1257. result = CURLE_SEND_ERROR;
  1258. }
  1259. else {
  1260. smtp->rcpt = smtp->rcpt->next;
  1261. if(smtp->rcpt)
  1262. /* Send the next RCPT TO command */
  1263. result = smtp_perform_rcpt_to(conn);
  1264. else {
  1265. /* Send the DATA command */
  1266. result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", "DATA");
  1267. if(!result)
  1268. state(conn, SMTP_DATA);
  1269. }
  1270. }
  1271. return result;
  1272. }
  1273. /* For DATA response */
  1274. static CURLcode smtp_state_data_resp(struct connectdata *conn, int smtpcode,
  1275. smtpstate instate)
  1276. {
  1277. CURLcode result = CURLE_OK;
  1278. struct SessionHandle *data = conn->data;
  1279. (void)instate; /* no use for this yet */
  1280. if(smtpcode != 354) {
  1281. failf(data, "DATA failed: %d", smtpcode);
  1282. result = CURLE_SEND_ERROR;
  1283. }
  1284. else {
  1285. /* Set the progress upload size */
  1286. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  1287. /* SMTP upload */
  1288. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
  1289. /* End of DO phase */
  1290. state(conn, SMTP_STOP);
  1291. }
  1292. return result;
  1293. }
  1294. /* For POSTDATA responses, which are received after the entire DATA
  1295. part has been sent to the server */
  1296. static CURLcode smtp_state_postdata_resp(struct connectdata *conn,
  1297. int smtpcode,
  1298. smtpstate instate)
  1299. {
  1300. CURLcode result = CURLE_OK;
  1301. (void)instate; /* no use for this yet */
  1302. if(smtpcode != 250)
  1303. result = CURLE_RECV_ERROR;
  1304. /* End of DONE phase */
  1305. state(conn, SMTP_STOP);
  1306. return result;
  1307. }
  1308. static CURLcode smtp_statemach_act(struct connectdata *conn)
  1309. {
  1310. CURLcode result = CURLE_OK;
  1311. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  1312. struct SessionHandle *data = conn->data;
  1313. int smtpcode;
  1314. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1315. struct pingpong *pp = &smtpc->pp;
  1316. size_t nread = 0;
  1317. /* Busy upgrading the connection; right now all I/O is SSL/TLS, not SMTP */
  1318. if(smtpc->state == SMTP_UPGRADETLS)
  1319. return smtp_perform_upgrade_tls(conn);
  1320. /* Flush any data that needs to be sent */
  1321. if(pp->sendleft)
  1322. return Curl_pp_flushsend(pp);
  1323. do {
  1324. /* Read the response from the server */
  1325. result = Curl_pp_readresp(sock, pp, &smtpcode, &nread);
  1326. if(result)
  1327. return result;
  1328. /* Store the latest response for later retrieval if necessary */
  1329. if(smtpc->state != SMTP_QUIT && smtpcode != 1)
  1330. data->info.httpcode = smtpcode;
  1331. if(!smtpcode)
  1332. break;
  1333. /* We have now received a full SMTP server response */
  1334. switch(smtpc->state) {
  1335. case SMTP_SERVERGREET:
  1336. result = smtp_state_servergreet_resp(conn, smtpcode, smtpc->state);
  1337. break;
  1338. case SMTP_EHLO:
  1339. result = smtp_state_ehlo_resp(conn, smtpcode, smtpc->state);
  1340. break;
  1341. case SMTP_HELO:
  1342. result = smtp_state_helo_resp(conn, smtpcode, smtpc->state);
  1343. break;
  1344. case SMTP_STARTTLS:
  1345. result = smtp_state_starttls_resp(conn, smtpcode, smtpc->state);
  1346. break;
  1347. case SMTP_AUTH_PLAIN:
  1348. result = smtp_state_auth_plain_resp(conn, smtpcode, smtpc->state);
  1349. break;
  1350. case SMTP_AUTH_LOGIN:
  1351. result = smtp_state_auth_login_resp(conn, smtpcode, smtpc->state);
  1352. break;
  1353. case SMTP_AUTH_LOGIN_PASSWD:
  1354. result = smtp_state_auth_login_password_resp(conn, smtpcode,
  1355. smtpc->state);
  1356. break;
  1357. #ifndef CURL_DISABLE_CRYPTO_AUTH
  1358. case SMTP_AUTH_CRAMMD5:
  1359. result = smtp_state_auth_cram_resp(conn, smtpcode, smtpc->state);
  1360. break;
  1361. case SMTP_AUTH_DIGESTMD5:
  1362. result = smtp_state_auth_digest_resp(conn, smtpcode, smtpc->state);
  1363. break;
  1364. case SMTP_AUTH_DIGESTMD5_RESP:
  1365. result = smtp_state_auth_digest_resp_resp(conn, smtpcode, smtpc->state);
  1366. break;
  1367. #endif
  1368. #ifdef USE_NTLM
  1369. case SMTP_AUTH_NTLM:
  1370. result = smtp_state_auth_ntlm_resp(conn, smtpcode, smtpc->state);
  1371. break;
  1372. case SMTP_AUTH_NTLM_TYPE2MSG:
  1373. result = smtp_state_auth_ntlm_type2msg_resp(conn, smtpcode,
  1374. smtpc->state);
  1375. break;
  1376. #endif
  1377. #if defined(USE_KRB5)
  1378. case SMTP_AUTH_GSSAPI:
  1379. result = smtp_state_auth_gssapi_resp(conn, smtpcode, smtpc->state);
  1380. break;
  1381. case SMTP_AUTH_GSSAPI_TOKEN:
  1382. result = smtp_state_auth_gssapi_token_resp(conn, smtpcode, smtpc->state);
  1383. break;
  1384. case SMTP_AUTH_GSSAPI_NO_DATA:
  1385. result = smtp_state_auth_gssapi_no_data_resp(conn, smtpcode,
  1386. smtpc->state);
  1387. break;
  1388. #endif
  1389. case SMTP_AUTH_XOAUTH2:
  1390. result = smtp_state_auth_xoauth2_resp(conn, smtpcode, smtpc->state);
  1391. break;
  1392. case SMTP_AUTH_CANCEL:
  1393. result = smtp_state_auth_cancel_resp(conn, smtpcode, smtpc->state);
  1394. break;
  1395. case SMTP_AUTH_FINAL:
  1396. result = smtp_state_auth_final_resp(conn, smtpcode, smtpc->state);
  1397. break;
  1398. case SMTP_COMMAND:
  1399. result = smtp_state_command_resp(conn, smtpcode, smtpc->state);
  1400. break;
  1401. case SMTP_MAIL:
  1402. result = smtp_state_mail_resp(conn, smtpcode, smtpc->state);
  1403. break;
  1404. case SMTP_RCPT:
  1405. result = smtp_state_rcpt_resp(conn, smtpcode, smtpc->state);
  1406. break;
  1407. case SMTP_DATA:
  1408. result = smtp_state_data_resp(conn, smtpcode, smtpc->state);
  1409. break;
  1410. case SMTP_POSTDATA:
  1411. result = smtp_state_postdata_resp(conn, smtpcode, smtpc->state);
  1412. break;
  1413. case SMTP_QUIT:
  1414. /* fallthrough, just stop! */
  1415. default:
  1416. /* internal error */
  1417. state(conn, SMTP_STOP);
  1418. break;
  1419. }
  1420. } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp));
  1421. return result;
  1422. }
  1423. /* Called repeatedly until done from multi.c */
  1424. static CURLcode smtp_multi_statemach(struct connectdata *conn, bool *done)
  1425. {
  1426. CURLcode result = CURLE_OK;
  1427. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1428. if((conn->handler->flags & PROTOPT_SSL) && !smtpc->ssldone) {
  1429. result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &smtpc->ssldone);
  1430. if(result || !smtpc->ssldone)
  1431. return result;
  1432. }
  1433. result = Curl_pp_statemach(&smtpc->pp, FALSE);
  1434. *done = (smtpc->state == SMTP_STOP) ? TRUE : FALSE;
  1435. return result;
  1436. }
  1437. static CURLcode smtp_block_statemach(struct connectdata *conn)
  1438. {
  1439. CURLcode result = CURLE_OK;
  1440. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1441. while(smtpc->state != SMTP_STOP && !result)
  1442. result = Curl_pp_statemach(&smtpc->pp, TRUE);
  1443. return result;
  1444. }
  1445. /* Allocate and initialize the SMTP struct for the current SessionHandle if
  1446. required */
  1447. static CURLcode smtp_init(struct connectdata *conn)
  1448. {
  1449. CURLcode result = CURLE_OK;
  1450. struct SessionHandle *data = conn->data;
  1451. struct SMTP *smtp;
  1452. smtp = data->req.protop = calloc(sizeof(struct SMTP), 1);
  1453. if(!smtp)
  1454. result = CURLE_OUT_OF_MEMORY;
  1455. return result;
  1456. }
  1457. /* For the SMTP "protocol connect" and "doing" phases only */
  1458. static int smtp_getsock(struct connectdata *conn, curl_socket_t *socks,
  1459. int numsocks)
  1460. {
  1461. return Curl_pp_getsock(&conn->proto.smtpc.pp, socks, numsocks);
  1462. }
  1463. /***********************************************************************
  1464. *
  1465. * smtp_connect()
  1466. *
  1467. * This function should do everything that is to be considered a part of
  1468. * the connection phase.
  1469. *
  1470. * The variable pointed to by 'done' will be TRUE if the protocol-layer
  1471. * connect phase is done when this function returns, or FALSE if not.
  1472. */
  1473. static CURLcode smtp_connect(struct connectdata *conn, bool *done)
  1474. {
  1475. CURLcode result = CURLE_OK;
  1476. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1477. struct pingpong *pp = &smtpc->pp;
  1478. *done = FALSE; /* default to not done yet */
  1479. /* We always support persistent connections in SMTP */
  1480. connkeep(conn, "SMTP default");
  1481. /* Set the default response time-out */
  1482. pp->response_time = RESP_TIMEOUT;
  1483. pp->statemach_act = smtp_statemach_act;
  1484. pp->endofresp = smtp_endofresp;
  1485. pp->conn = conn;
  1486. /* Set the default preferred authentication mechanism */
  1487. smtpc->prefmech = SASL_AUTH_ANY;
  1488. /* Initialise the pingpong layer */
  1489. Curl_pp_init(pp);
  1490. /* Parse the URL options */
  1491. result = smtp_parse_url_options(conn);
  1492. if(result)
  1493. return result;
  1494. /* Parse the URL path */
  1495. result = smtp_parse_url_path(conn);
  1496. if(result)
  1497. return result;
  1498. /* Start off waiting for the server greeting response */
  1499. state(conn, SMTP_SERVERGREET);
  1500. result = smtp_multi_statemach(conn, done);
  1501. return result;
  1502. }
  1503. /***********************************************************************
  1504. *
  1505. * smtp_done()
  1506. *
  1507. * The DONE function. This does what needs to be done after a single DO has
  1508. * performed.
  1509. *
  1510. * Input argument is already checked for validity.
  1511. */
  1512. static CURLcode smtp_done(struct connectdata *conn, CURLcode status,
  1513. bool premature)
  1514. {
  1515. CURLcode result = CURLE_OK;
  1516. struct SessionHandle *data = conn->data;
  1517. struct SMTP *smtp = data->req.protop;
  1518. struct pingpong *pp = &conn->proto.smtpc.pp;
  1519. char *eob;
  1520. ssize_t len;
  1521. ssize_t bytes_written;
  1522. (void)premature;
  1523. if(!smtp || !pp->conn)
  1524. /* When the easy handle is removed from the multi interface while libcurl
  1525. is still trying to resolve the host name, the SMTP struct is not yet
  1526. initialized. However, the removal action calls Curl_done() which in
  1527. turn calls this function, so we simply return success. */
  1528. return CURLE_OK;
  1529. if(status) {
  1530. connclose(conn, "SMTP done with bad status"); /* marked for closure */
  1531. result = status; /* use the already set error code */
  1532. }
  1533. else if(!data->set.connect_only && data->set.upload && data->set.mail_rcpt) {
  1534. /* Calculate the EOB taking into account any terminating CRLF from the
  1535. previous line of the email or the CRLF of the DATA command when there
  1536. is "no mail data". RFC-5321, sect. 4.1.1.4.
  1537. Note: As some SSL backends, such as OpenSSL, will cause Curl_write() to
  1538. fail when using a different pointer following a previous write, that
  1539. returned CURLE_AGAIN, we duplicate the EOB now rather than when the
  1540. bytes written doesn't equal len. */
  1541. if(smtp->trailing_crlf || !conn->data->state.infilesize) {
  1542. eob = strdup(SMTP_EOB + 2);
  1543. len = SMTP_EOB_LEN - 2;
  1544. }
  1545. else {
  1546. eob = strdup(SMTP_EOB);
  1547. len = SMTP_EOB_LEN;
  1548. }
  1549. if(!eob)
  1550. return CURLE_OUT_OF_MEMORY;
  1551. /* Send the end of block data */
  1552. result = Curl_write(conn, conn->writesockfd, eob, len, &bytes_written);
  1553. if(result) {
  1554. free(eob);
  1555. return result;
  1556. }
  1557. if(bytes_written != len) {
  1558. /* The whole chunk was not sent so keep it around and adjust the
  1559. pingpong structure accordingly */
  1560. pp->sendthis = eob;
  1561. pp->sendsize = len;
  1562. pp->sendleft = len - bytes_written;
  1563. }
  1564. else {
  1565. /* Successfully sent so adjust the response timeout relative to now */
  1566. pp->response = Curl_tvnow();
  1567. free(eob);
  1568. }
  1569. state(conn, SMTP_POSTDATA);
  1570. /* Run the state-machine
  1571. TODO: when the multi interface is used, this _really_ should be using
  1572. the smtp_multi_statemach function but we have no general support for
  1573. non-blocking DONE operations, not in the multi state machine and with
  1574. Curl_done() invokes on several places in the code!
  1575. */
  1576. result = smtp_block_statemach(conn);
  1577. }
  1578. /* Cleanup our per-request based variables */
  1579. Curl_safefree(smtp->custom);
  1580. /* Clear the transfer mode for the next request */
  1581. smtp->transfer = FTPTRANSFER_BODY;
  1582. return result;
  1583. }
  1584. /***********************************************************************
  1585. *
  1586. * smtp_perform()
  1587. *
  1588. * This is the actual DO function for SMTP. Transfer a mail, send a command
  1589. * or get some data according to the options previously setup.
  1590. */
  1591. static CURLcode smtp_perform(struct connectdata *conn, bool *connected,
  1592. bool *dophase_done)
  1593. {
  1594. /* This is SMTP and no proxy */
  1595. CURLcode result = CURLE_OK;
  1596. struct SessionHandle *data = conn->data;
  1597. struct SMTP *smtp = data->req.protop;
  1598. DEBUGF(infof(conn->data, "DO phase starts\n"));
  1599. if(data->set.opt_no_body) {
  1600. /* Requested no body means no transfer */
  1601. smtp->transfer = FTPTRANSFER_INFO;
  1602. }
  1603. *dophase_done = FALSE; /* not done yet */
  1604. /* Store the first recipient (or NULL if not specified) */
  1605. smtp->rcpt = data->set.mail_rcpt;
  1606. /* Start the first command in the DO phase */
  1607. if(data->set.upload && data->set.mail_rcpt)
  1608. /* MAIL transfer */
  1609. result = smtp_perform_mail(conn);
  1610. else
  1611. /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
  1612. result = smtp_perform_command(conn);
  1613. if(result)
  1614. return result;
  1615. /* Run the state-machine */
  1616. result = smtp_multi_statemach(conn, dophase_done);
  1617. *connected = conn->bits.tcpconnect[FIRSTSOCKET];
  1618. if(*dophase_done)
  1619. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1620. return result;
  1621. }
  1622. /***********************************************************************
  1623. *
  1624. * smtp_do()
  1625. *
  1626. * This function is registered as 'curl_do' function. It decodes the path
  1627. * parts etc as a wrapper to the actual DO function (smtp_perform).
  1628. *
  1629. * The input argument is already checked for validity.
  1630. */
  1631. static CURLcode smtp_do(struct connectdata *conn, bool *done)
  1632. {
  1633. CURLcode result = CURLE_OK;
  1634. *done = FALSE; /* default to false */
  1635. /* Parse the custom request */
  1636. result = smtp_parse_custom_request(conn);
  1637. if(result)
  1638. return result;
  1639. result = smtp_regular_transfer(conn, done);
  1640. return result;
  1641. }
  1642. /***********************************************************************
  1643. *
  1644. * smtp_disconnect()
  1645. *
  1646. * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
  1647. * resources. BLOCKING.
  1648. */
  1649. static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection)
  1650. {
  1651. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1652. /* We cannot send quit unconditionally. If this connection is stale or
  1653. bad in any way, sending quit and waiting around here will make the
  1654. disconnect wait in vain and cause more problems than we need to. */
  1655. /* The SMTP session may or may not have been allocated/setup at this
  1656. point! */
  1657. if(!dead_connection && smtpc->pp.conn && smtpc->pp.conn->bits.protoconnstart)
  1658. if(!smtp_perform_quit(conn))
  1659. (void)smtp_block_statemach(conn); /* ignore errors on QUIT */
  1660. /* Disconnect from the server */
  1661. Curl_pp_disconnect(&smtpc->pp);
  1662. /* Cleanup the SASL module */
  1663. Curl_sasl_cleanup(conn, smtpc->authused);
  1664. /* Cleanup our connection based variables */
  1665. Curl_safefree(smtpc->domain);
  1666. return CURLE_OK;
  1667. }
  1668. /* Call this when the DO phase has completed */
  1669. static CURLcode smtp_dophase_done(struct connectdata *conn, bool connected)
  1670. {
  1671. struct SMTP *smtp = conn->data->req.protop;
  1672. (void)connected;
  1673. if(smtp->transfer != FTPTRANSFER_BODY)
  1674. /* no data to transfer */
  1675. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1676. return CURLE_OK;
  1677. }
  1678. /* Called from multi.c while DOing */
  1679. static CURLcode smtp_doing(struct connectdata *conn, bool *dophase_done)
  1680. {
  1681. CURLcode result = smtp_multi_statemach(conn, dophase_done);
  1682. if(result)
  1683. DEBUGF(infof(conn->data, "DO phase failed\n"));
  1684. else if(*dophase_done) {
  1685. result = smtp_dophase_done(conn, FALSE /* not connected */);
  1686. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1687. }
  1688. return result;
  1689. }
  1690. /***********************************************************************
  1691. *
  1692. * smtp_regular_transfer()
  1693. *
  1694. * The input argument is already checked for validity.
  1695. *
  1696. * Performs all commands done before a regular transfer between a local and a
  1697. * remote host.
  1698. */
  1699. static CURLcode smtp_regular_transfer(struct connectdata *conn,
  1700. bool *dophase_done)
  1701. {
  1702. CURLcode result = CURLE_OK;
  1703. bool connected = FALSE;
  1704. struct SessionHandle *data = conn->data;
  1705. /* Make sure size is unknown at this point */
  1706. data->req.size = -1;
  1707. /* Set the progress data */
  1708. Curl_pgrsSetUploadCounter(data, 0);
  1709. Curl_pgrsSetDownloadCounter(data, 0);
  1710. Curl_pgrsSetUploadSize(data, -1);
  1711. Curl_pgrsSetDownloadSize(data, -1);
  1712. /* Carry out the perform */
  1713. result = smtp_perform(conn, &connected, dophase_done);
  1714. /* Perform post DO phase operations if necessary */
  1715. if(!result && *dophase_done)
  1716. result = smtp_dophase_done(conn, connected);
  1717. return result;
  1718. }
  1719. static CURLcode smtp_setup_connection(struct connectdata *conn)
  1720. {
  1721. struct SessionHandle *data = conn->data;
  1722. CURLcode result;
  1723. if(conn->bits.httpproxy && !data->set.tunnel_thru_httpproxy) {
  1724. /* Unless we have asked to tunnel SMTP operations through the proxy, we
  1725. switch and use HTTP operations only */
  1726. #ifndef CURL_DISABLE_HTTP
  1727. if(conn->handler == &Curl_handler_smtp)
  1728. conn->handler = &Curl_handler_smtp_proxy;
  1729. else {
  1730. #ifdef USE_SSL
  1731. conn->handler = &Curl_handler_smtps_proxy;
  1732. #else
  1733. failf(data, "SMTPS not supported!");
  1734. return CURLE_UNSUPPORTED_PROTOCOL;
  1735. #endif
  1736. }
  1737. /* set it up as a HTTP connection instead */
  1738. return conn->handler->setup_connection(conn);
  1739. #else
  1740. failf(data, "SMTP over http proxy requires HTTP support built-in!");
  1741. return CURLE_UNSUPPORTED_PROTOCOL;
  1742. #endif
  1743. }
  1744. /* Initialise the SMTP layer */
  1745. result = smtp_init(conn);
  1746. if(result)
  1747. return result;
  1748. data->state.path++; /* don't include the initial slash */
  1749. return CURLE_OK;
  1750. }
  1751. /***********************************************************************
  1752. *
  1753. * smtp_parse_url_options()
  1754. *
  1755. * Parse the URL login options.
  1756. */
  1757. static CURLcode smtp_parse_url_options(struct connectdata *conn)
  1758. {
  1759. CURLcode result = CURLE_OK;
  1760. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1761. const char *options = conn->options;
  1762. const char *ptr = options;
  1763. bool reset = TRUE;
  1764. while(ptr && *ptr) {
  1765. const char *key = ptr;
  1766. while(*ptr && *ptr != '=')
  1767. ptr++;
  1768. if(strnequal(key, "AUTH", 4)) {
  1769. size_t len = 0;
  1770. const char *value = ++ptr;
  1771. if(reset) {
  1772. reset = FALSE;
  1773. smtpc->prefmech = SASL_AUTH_NONE;
  1774. }
  1775. while(*ptr && *ptr != ';') {
  1776. ptr++;
  1777. len++;
  1778. }
  1779. if(strnequal(value, "*", len))
  1780. smtpc->prefmech = SASL_AUTH_ANY;
  1781. else if(strnequal(value, SASL_MECH_STRING_LOGIN, len))
  1782. smtpc->prefmech |= SASL_MECH_LOGIN;
  1783. else if(strnequal(value, SASL_MECH_STRING_PLAIN, len))
  1784. smtpc->prefmech |= SASL_MECH_PLAIN;
  1785. else if(strnequal(value, SASL_MECH_STRING_CRAM_MD5, len))
  1786. smtpc->prefmech |= SASL_MECH_CRAM_MD5;
  1787. else if(strnequal(value, SASL_MECH_STRING_DIGEST_MD5, len))
  1788. smtpc->prefmech |= SASL_MECH_DIGEST_MD5;
  1789. else if(strnequal(value, SASL_MECH_STRING_GSSAPI, len))
  1790. smtpc->prefmech |= SASL_MECH_GSSAPI;
  1791. else if(strnequal(value, SASL_MECH_STRING_NTLM, len))
  1792. smtpc->prefmech |= SASL_MECH_NTLM;
  1793. else if(strnequal(value, SASL_MECH_STRING_XOAUTH2, len))
  1794. smtpc->prefmech |= SASL_MECH_XOAUTH2;
  1795. if(*ptr == ';')
  1796. ptr++;
  1797. }
  1798. else
  1799. result = CURLE_URL_MALFORMAT;
  1800. }
  1801. return result;
  1802. }
  1803. /***********************************************************************
  1804. *
  1805. * smtp_parse_url_path()
  1806. *
  1807. * Parse the URL path into separate path components.
  1808. */
  1809. static CURLcode smtp_parse_url_path(struct connectdata *conn)
  1810. {
  1811. /* The SMTP struct is already initialised in smtp_connect() */
  1812. struct SessionHandle *data = conn->data;
  1813. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1814. const char *path = data->state.path;
  1815. char localhost[HOSTNAME_MAX + 1];
  1816. /* Calculate the path if necessary */
  1817. if(!*path) {
  1818. if(!Curl_gethostname(localhost, sizeof(localhost)))
  1819. path = localhost;
  1820. else
  1821. path = "localhost";
  1822. }
  1823. /* URL decode the path and use it as the domain in our EHLO */
  1824. return Curl_urldecode(conn->data, path, 0, &smtpc->domain, NULL, TRUE);
  1825. }
  1826. /***********************************************************************
  1827. *
  1828. * smtp_parse_custom_request()
  1829. *
  1830. * Parse the custom request.
  1831. */
  1832. static CURLcode smtp_parse_custom_request(struct connectdata *conn)
  1833. {
  1834. CURLcode result = CURLE_OK;
  1835. struct SessionHandle *data = conn->data;
  1836. struct SMTP *smtp = data->req.protop;
  1837. const char *custom = data->set.str[STRING_CUSTOMREQUEST];
  1838. /* URL decode the custom request */
  1839. if(custom)
  1840. result = Curl_urldecode(data, custom, 0, &smtp->custom, NULL, TRUE);
  1841. return result;
  1842. }
  1843. /***********************************************************************
  1844. *
  1845. * smtp_calc_sasl_details()
  1846. *
  1847. * Calculate the required login details for SASL authentication.
  1848. */
  1849. static CURLcode smtp_calc_sasl_details(struct connectdata *conn,
  1850. const char **mech,
  1851. char **initresp, size_t *len,
  1852. smtpstate *state1, smtpstate *state2)
  1853. {
  1854. CURLcode result = CURLE_OK;
  1855. struct SessionHandle *data = conn->data;
  1856. struct smtp_conn *smtpc = &conn->proto.smtpc;
  1857. /* Calculate the supported authentication mechanism, by decreasing order of
  1858. security, as well as the initial response where appropriate */
  1859. #if defined(USE_KRB5)
  1860. if((smtpc->authmechs & SASL_MECH_GSSAPI) &&
  1861. (smtpc->prefmech & SASL_MECH_GSSAPI)) {
  1862. smtpc->mutual_auth = FALSE; /* TODO: Calculate mutual authentication */
  1863. *mech = SASL_MECH_STRING_GSSAPI;
  1864. *state1 = SMTP_AUTH_GSSAPI;
  1865. *state2 = SMTP_AUTH_GSSAPI_TOKEN;
  1866. smtpc->authused = SASL_MECH_GSSAPI;
  1867. if(data->set.sasl_ir)
  1868. result = Curl_sasl_create_gssapi_user_message(data, conn->user,
  1869. conn->passwd, "smtp",
  1870. smtpc->mutual_auth,
  1871. NULL, &conn->krb5,
  1872. initresp, len);
  1873. }
  1874. else
  1875. #endif
  1876. #ifndef CURL_DISABLE_CRYPTO_AUTH
  1877. if((smtpc->authmechs & SASL_MECH_DIGEST_MD5) &&
  1878. (smtpc->prefmech & SASL_MECH_DIGEST_MD5)) {
  1879. *mech = SASL_MECH_STRING_DIGEST_MD5;
  1880. *state1 = SMTP_AUTH_DIGESTMD5;
  1881. smtpc->authused = SASL_MECH_DIGEST_MD5;
  1882. }
  1883. else if((smtpc->authmechs & SASL_MECH_CRAM_MD5) &&
  1884. (smtpc->prefmech & SASL_MECH_CRAM_MD5)) {
  1885. *mech = SASL_MECH_STRING_CRAM_MD5;
  1886. *state1 = SMTP_AUTH_CRAMMD5;
  1887. smtpc->authused = SASL_MECH_CRAM_MD5;
  1888. }
  1889. else
  1890. #endif
  1891. #ifdef USE_NTLM
  1892. if((smtpc->authmechs & SASL_MECH_NTLM) &&
  1893. (smtpc->prefmech & SASL_MECH_NTLM)) {
  1894. *mech = SASL_MECH_STRING_NTLM;
  1895. *state1 = SMTP_AUTH_NTLM;
  1896. *state2 = SMTP_AUTH_NTLM_TYPE2MSG;
  1897. smtpc->authused = SASL_MECH_NTLM;
  1898. if(data->set.sasl_ir)
  1899. result = Curl_sasl_create_ntlm_type1_message(conn->user, conn->passwd,
  1900. &conn->ntlm,
  1901. initresp, len);
  1902. }
  1903. else
  1904. #endif
  1905. if(((smtpc->authmechs & SASL_MECH_XOAUTH2) &&
  1906. (smtpc->prefmech & SASL_MECH_XOAUTH2) &&
  1907. (smtpc->prefmech != SASL_AUTH_ANY)) || conn->xoauth2_bearer) {
  1908. *mech = SASL_MECH_STRING_XOAUTH2;
  1909. *state1 = SMTP_AUTH_XOAUTH2;
  1910. *state2 = SMTP_AUTH_FINAL;
  1911. smtpc->authused = SASL_MECH_XOAUTH2;
  1912. if(data->set.sasl_ir)
  1913. result = Curl_sasl_create_xoauth2_message(data, conn->user,
  1914. conn->xoauth2_bearer,
  1915. initresp, len);
  1916. }
  1917. else if((smtpc->authmechs & SASL_MECH_LOGIN) &&
  1918. (smtpc->prefmech & SASL_MECH_LOGIN)) {
  1919. *mech = SASL_MECH_STRING_LOGIN;
  1920. *state1 = SMTP_AUTH_LOGIN;
  1921. *state2 = SMTP_AUTH_LOGIN_PASSWD;
  1922. smtpc->authused = SASL_MECH_LOGIN;
  1923. if(data->set.sasl_ir)
  1924. result = Curl_sasl_create_login_message(data, conn->user, initresp, len);
  1925. }
  1926. else if((smtpc->authmechs & SASL_MECH_PLAIN) &&
  1927. (smtpc->prefmech & SASL_MECH_PLAIN)) {
  1928. *mech = SASL_MECH_STRING_PLAIN;
  1929. *state1 = SMTP_AUTH_PLAIN;
  1930. *state2 = SMTP_AUTH_FINAL;
  1931. smtpc->authused = SASL_MECH_PLAIN;
  1932. if(data->set.sasl_ir)
  1933. result = Curl_sasl_create_plain_message(data, conn->user, conn->passwd,
  1934. initresp, len);
  1935. }
  1936. return result;
  1937. }
  1938. CURLcode Curl_smtp_escape_eob(struct connectdata *conn, ssize_t nread)
  1939. {
  1940. /* When sending a SMTP payload we must detect CRLF. sequences making sure
  1941. they are sent as CRLF.. instead, as a . on the beginning of a line will
  1942. be deleted by the server when not part of an EOB terminator and a
  1943. genuine CRLF.CRLF which isn't escaped will wrongly be detected as end of
  1944. data by the server
  1945. */
  1946. ssize_t i;
  1947. ssize_t si;
  1948. struct SessionHandle *data = conn->data;
  1949. struct SMTP *smtp = data->req.protop;
  1950. /* Do we need to allocate the scatch buffer? */
  1951. if(!data->state.scratch) {
  1952. data->state.scratch = malloc(2 * BUFSIZE);
  1953. if(!data->state.scratch) {
  1954. failf (data, "Failed to alloc scratch buffer!");
  1955. return CURLE_OUT_OF_MEMORY;
  1956. }
  1957. }
  1958. /* This loop can be improved by some kind of Boyer-Moore style of
  1959. approach but that is saved for later... */
  1960. for(i = 0, si = 0; i < nread; i++) {
  1961. if(SMTP_EOB[smtp->eob] == data->req.upload_fromhere[i]) {
  1962. smtp->eob++;
  1963. /* Is the EOB potentially the terminating CRLF? */
  1964. if(2 == smtp->eob || SMTP_EOB_LEN == smtp->eob)
  1965. smtp->trailing_crlf = TRUE;
  1966. else
  1967. smtp->trailing_crlf = FALSE;
  1968. }
  1969. else if(smtp->eob) {
  1970. /* A previous substring matched so output that first */
  1971. memcpy(&data->state.scratch[si], SMTP_EOB, smtp->eob);
  1972. si += smtp->eob;
  1973. /* Then compare the first byte */
  1974. if(SMTP_EOB[0] == data->req.upload_fromhere[i])
  1975. smtp->eob = 1;
  1976. else
  1977. smtp->eob = 0;
  1978. /* Reset the trailing CRLF flag as there was more data */
  1979. smtp->trailing_crlf = FALSE;
  1980. }
  1981. /* Do we have a match for CRLF. as per RFC-5321, sect. 4.5.2 */
  1982. if(SMTP_EOB_FIND_LEN == smtp->eob) {
  1983. /* Copy the replacement data to the target buffer */
  1984. memcpy(&data->state.scratch[si], SMTP_EOB_REPL, SMTP_EOB_REPL_LEN);
  1985. si += SMTP_EOB_REPL_LEN;
  1986. smtp->eob = 0;
  1987. }
  1988. else if(!smtp->eob)
  1989. data->state.scratch[si++] = data->req.upload_fromhere[i];
  1990. }
  1991. if(smtp->eob) {
  1992. /* A substring matched before processing ended so output that now */
  1993. memcpy(&data->state.scratch[si], SMTP_EOB, smtp->eob);
  1994. si += smtp->eob;
  1995. smtp->eob = 0;
  1996. }
  1997. if(si != nread) {
  1998. /* Only use the new buffer if we replaced something */
  1999. nread = si;
  2000. /* Upload from the new (replaced) buffer instead */
  2001. data->req.upload_fromhere = data->state.scratch;
  2002. /* Set the new amount too */
  2003. data->req.upload_present = nread;
  2004. }
  2005. return CURLE_OK;
  2006. }
  2007. #endif /* CURL_DISABLE_SMTP */