Artifactspublic-v0

curl

master
ply4 git calls40 ms301 KiB3 cachedprotocol v2
2019 lines60 KiBRaw
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#include "urldata.h"
43#include "smtp.h"
45#ifndef CURL_DISABLE_SMTP
47#ifdef HAVE_NETINET_IN_H
48#include <netinet/in.h>
49#endif
50#ifdef HAVE_ARPA_INET_H
51#include <arpa/inet.h>
52#endif
53#ifdef HAVE_NETDB_H
54#include <netdb.h>
55#endif
56#ifdef __VMS
57#include <in.h>
58#include <inet.h>
59#endif
61#include "sendf.h"
62#include "curl_trc.h"
63#include "progress.h"
64#include "transfer.h"
65#include "escape.h"
66#include "pingpong.h"
67#include "mime.h"
68#include "vtls/vtls.h"
69#include "cfilters.h"
70#include "connect.h"
71#include "select.h"
72#include "url.h"
73#include "curl_gethostname.h"
74#include "bufref.h"
75#include "curl_sasl.h"
76#include "idn.h"
77#include "curlx/strparse.h"
79/* meta key for storing protocol meta at easy handle */
80#define CURL_META_SMTP_EASY "meta:proto:smtp:easy"
81/* meta key for storing protocol meta at connection */
82#define CURL_META_SMTP_CONN "meta:proto:smtp:conn"
84/****************************************************************************
85 * SMTP unique setup
86 ***************************************************************************/
87typedef enum {
88 SMTP_STOP, /* do nothing state, stops the state machine */
89 SMTP_SERVERGREET, /* waiting for the initial greeting immediately after
90 a connect */
91 SMTP_EHLO,
92 SMTP_HELO,
93 SMTP_STARTTLS,
94 SMTP_UPGRADETLS, /* asynchronously upgrade the connection to SSL/TLS
95 (multi mode only) */
96 SMTP_AUTH,
97 SMTP_COMMAND, /* VRFY, EXPN, NOOP, RSET and HELP */
98 SMTP_MAIL, /* MAIL FROM */
99 SMTP_RCPT, /* RCPT TO */
100 SMTP_DATA,
101 SMTP_POSTDATA,
102 SMTP_QUIT,
103 SMTP_LAST /* never used */
104} smtpstate;
106/* smtp_conn is used for struct connection-oriented data in the connectdata
107 struct */
108struct smtp_conn {
109 struct pingpong pp;
110 struct SASL sasl; /* SASL-related storage */
111 smtpstate state; /* Always use smtp.c:state() to change state! */
112 char *domain; /* Client address/name to send in the EHLO */
113 BIT(ssldone); /* Is connect() over SSL done? */
114 BIT(tls_supported); /* StartTLS capability supported by server */
115 BIT(size_supported); /* If server supports SIZE extension according to
116 RFC 1870 */
117 BIT(utf8_supported); /* If server supports SMTPUTF8 extension according
118 to RFC 6531 */
119 BIT(auth_supported); /* AUTH capability supported by server */
120};
122/* This SMTP struct is used in the Curl_easy. All SMTP data that is
123 connection-oriented must be in smtp_conn to properly deal with the fact that
124 perhaps the Curl_easy is changed between the times the connection is
125 used. */
126struct SMTP {
127 curl_pp_transfer transfer;
128 char *custom; /* Custom Request */
129 struct curl_slist *rcpt; /* Recipient list */
130 int rcpt_last_error; /* The last error received for RCPT TO command */
131 size_t eob; /* Number of bytes of the EOB (End Of Body) that
132 have been received so far */
133 BIT(rcpt_had_ok); /* Whether any of RCPT TO commands (depends on
134 total number of recipients) succeeded so far */
135 BIT(trailing_crlf); /* Specifies if the trailing CRLF is present */
136};
138/***********************************************************************
139 *
140 * smtp_parse_url_options()
141 *
142 * Parse the URL login options.
143 */
144static CURLcode smtp_parse_url_options(struct connectdata *conn,
145 struct smtp_conn *smtpc)
147 CURLcode result = CURLE_OK;
148 const char *ptr = conn->options;
150 while(!result && ptr && *ptr) {
151 const char *key = ptr;
152 const char *value;
154 while(*ptr && *ptr != '=')
155 ptr++;
157 value = ptr + 1;
159 while(*ptr && *ptr != ';')
160 ptr++;
162 if(curl_strnequal(key, "AUTH=", 5))
163 result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
164 value, ptr - value);
165 else
166 result = CURLE_URL_MALFORMAT;
168 if(*ptr == ';')
169 ptr++;
170 }
172 return result;
175/***********************************************************************
176 *
177 * smtp_parse_url_path()
178 *
179 * Parse the URL path into separate path components.
180 */
181static CURLcode smtp_parse_url_path(struct Curl_easy *data,
182 struct smtp_conn *smtpc)
184 /* The SMTP struct is already initialized in smtp_connect() */
185 const char *path = &data->state.up.path[1]; /* skip leading path */
186 char localhost[HOSTNAME_MAX + 1];
188 /* Calculate the path if necessary */
189 if(!*path) {
190 if(!Curl_gethostname(localhost, sizeof(localhost)))
191 path = localhost;
192 else
193 path = "localhost";
194 }
196 /* URL decode the path and use it as the domain in our EHLO */
197 return Curl_urldecode(path, 0, &smtpc->domain, NULL, REJECT_CTRL);
200/***********************************************************************
201 *
202 * smtp_parse_custom_request()
203 *
204 * Parse the custom request.
205 */
206static CURLcode smtp_parse_custom_request(struct Curl_easy *data,
207 struct SMTP *smtp)
209 CURLcode result = CURLE_OK;
210 const char *custom = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
212 /* URL decode the custom request */
213 if(custom)
214 result = Curl_urldecode(custom, 0, &smtp->custom, NULL, REJECT_CTRL);
216 return result;
219/***********************************************************************
220 *
221 * smtp_parse_address()
222 *
223 * Parse the fully qualified mailbox address into a local address part and the
224 * hostname, converting the hostname to an IDN A-label, as per RFC-5890, if
225 * necessary.
226 *
227 * Parameters:
228 *
229 * fqma [in] - The fully qualified mailbox address (which may or
230 * may not contain UTF-8 characters).
231 * address [in/out] - A new allocated buffer which holds the local
232 * address part of the mailbox. This buffer must be
233 * free'ed by the caller.
234 * host [in/out] - The hostname structure that holds the original,
235 * and optionally encoded, hostname.
236 * Curl_free_idnconverted_hostname() must be called
237 * once the caller has finished with the structure.
238 *
239 * Returns CURLE_OK on success.
240 *
241 * Notes:
242 *
243 * Should a UTF-8 hostname require conversion to IDN ACE and we cannot honor
244 * that conversion then we shall return success. This allow the caller to send
245 * the data to the server as a U-label (as per RFC-6531 sect. 3.2).
246 *
247 * If an mailbox '@' separator cannot be located then the mailbox is considered
248 * to be either a local mailbox or an invalid mailbox (depending on what the
249 * calling function deems it to be) then the input will be returned in
250 * the address part with the hostname being NULL.
251 */
252static CURLcode smtp_parse_address(struct Curl_easy *data, const char *fqma,
253 char **address, struct hostname *host,
254 const char **suffix)
256 CURLcode result = CURLE_OK;
257 size_t length;
258 char *addressend;
259 char *dup;
261 /* A CR or LF in the address ends up verbatim in the MAIL FROM/RCPT TO
262 command line, so a crafted address could smuggle further SMTP commands
263 onto the wire. Reject it before the command is built. */
264 if(strpbrk(fqma, "\r\n")) {
265 failf(data, "Refusing to send email address with a CR or LF");
266 return CURLE_BAD_FUNCTION_ARGUMENT;
267 }
269 /* Duplicate the fully qualified email address so we can manipulate it,
270 ensuring it does not contain the delimiters if specified */
271 dup = curlx_strdup(fqma[0] == '<' ? fqma + 1 : fqma);
272 if(!dup)
273 return CURLE_OUT_OF_MEMORY;
275 if(fqma[0] != '<') {
276 length = strlen(dup);
277 if(length && dup[length - 1] == '>')
278 dup[length - 1] = '\0';
279 }
280 else {
281 addressend = strrchr(dup, '>');
282 if(addressend) {
283 *addressend = '\0';
284 *suffix = addressend + 1;
285 }
286 }
288 /* Extract the hostname from the address (if we can) */
289 host->name = strpbrk(dup, "@");
290 if(host->name) {
291 *host->name = '\0';
292 host->name = host->name + 1;
294 /* Attempt to convert the hostname to IDN ACE */
295 (void)Curl_idnconvert_hostname(host);
297 /* If Curl_idnconvert_hostname() fails then we shall attempt to continue
298 and send the hostname using UTF-8 rather than as 7-bit ACE (which is
299 our preference) */
300 }
302 /* Extract the local address from the mailbox */
303 *address = dup;
305 return result;
308struct cr_eob_ctx {
309 struct Curl_creader super;
310 struct bufq buf;
311 size_t n_eob; /* how many EOB bytes we matched so far */
312 size_t eob; /* Number of bytes of the EOB (End Of Body) that
313 have been received so far */
314 BIT(read_eos); /* we read an EOS from the next reader */
315 BIT(processed_eos); /* we read and processed an EOS */
316 BIT(eos); /* we have returned an EOS */
317};
319static CURLcode cr_eob_init(struct Curl_easy *data,
320 struct Curl_creader *reader)
322 struct cr_eob_ctx *ctx = reader->ctx;
323 (void)data;
324 /* The first char we read is the first on a line, as if we had
325 * read CRLF before */
326 ctx->n_eob = 2;
327 Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
328 return CURLE_OK;
331static void cr_eob_close(struct Curl_easy *data, struct Curl_creader *reader)
333 struct cr_eob_ctx *ctx = reader->ctx;
334 (void)data;
335 Curl_bufq_free(&ctx->buf);
338/* this is the 5-bytes End-Of-Body marker for SMTP */
339#define SMTP_EOB "\r\n.\r\n"
340#define SMTP_EOB_FIND_LEN 3
342/* client reader doing SMTP End-Of-Body escaping. */
343static CURLcode cr_eob_read(struct Curl_easy *data,
344 struct Curl_creader *reader,
345 char *buf, size_t blen,
346 size_t *pnread, bool *peos)
348 struct cr_eob_ctx *ctx = reader->ctx;
349 CURLcode result = CURLE_OK;
350 size_t nread, i, start, n;
351 bool eos;
353 if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
354 /* Get more and convert it when needed */
355 result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
356 CURL_TRC_SMTP(data, "cr_eob_read, next_read(len=%zu) -> %d, %zu eos=%d",
357 blen, (int)result, nread, eos);
358 if(result)
359 return result;
361 ctx->read_eos = eos;
362 if(nread) {
363 if(!ctx->n_eob && !memchr(buf, SMTP_EOB[0], nread)) {
364 /* not in the middle of a match, no EOB start found, pass */
365 *pnread = nread;
366 *peos = FALSE;
367 return CURLE_OK;
368 }
369 /* scan for EOB (continuation) and convert */
370 for(i = start = 0; i < nread; ++i) {
371 if(ctx->n_eob >= SMTP_EOB_FIND_LEN) {
372 /* matched the EOB prefix and seeing additional char, add '.' */
373 /* The additional byte cannot be represented in the upload size. */
374 if(data->state.infilesize == CURL_OFF_T_MAX)
375 return CURLE_TOO_LARGE;
376 result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
377 if(result)
378 return result;
379 result = Curl_bufq_cwrite(&ctx->buf, ".", 1, &n);
380 if(result)
381 return result;
382 ctx->n_eob = 0;
383 start = i;
384 if(data->state.infilesize > 0)
385 data->state.infilesize++;
386 }
388 if(buf[i] != SMTP_EOB[ctx->n_eob])
389 ctx->n_eob = 0;
391 if(buf[i] == SMTP_EOB[ctx->n_eob]) {
392 /* matching another char of the EOB */
393 ++ctx->n_eob;
394 }
395 }
397 /* add any remainder to buf */
398 if(start < nread) {
399 result = Curl_bufq_cwrite(&ctx->buf, buf + start, nread - start, &n);
400 if(result)
401 return result;
402 }
403 }
404 }
406 *peos = FALSE;
408 if(ctx->read_eos && !ctx->processed_eos) {
409 /* if we last matched a CRLF or if the data was empty, add ".\r\n"
410 * to end the body. If we sent something and it did not end with "\r\n",
411 * add "\r\n.\r\n" to end the body */
412 const char *eob = SMTP_EOB;
413 CURL_TRC_SMTP(data, "auto-ending mail body with '\\r\\n.\\r\\n'");
414 switch(ctx->n_eob) {
415 case 2:
416 /* seen a CRLF at the end, add the remainder */
417 eob = &SMTP_EOB[2];
418 break;
419 case 3:
420 /* ended with '\r\n.', we should escape the last '.' */
421 eob = "." SMTP_EOB;
422 break;
423 default:
424 break;
425 }
426 result = Curl_bufq_cwrite(&ctx->buf, eob, strlen(eob), &n);
427 if(result)
428 return result;
429 ctx->processed_eos = TRUE;
430 }
432 if(!Curl_bufq_is_empty(&ctx->buf)) {
433 result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
434 }
435 else
436 *pnread = 0;
438 if(ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
439 /* no more data, read all, done. */
440 CURL_TRC_SMTP(data, "mail body complete, returning EOS");
441 ctx->eos = TRUE;
442 }
443 *peos = (bool)ctx->eos;
444 DEBUGF(infof(data, "cr_eob_read(%zu) -> %d, %zu, %d",
445 blen, (int)result, *pnread, *peos));
446 return result;
449static curl_off_t cr_eob_total_length(struct Curl_easy *data,
450 struct Curl_creader *reader)
452 /* this reader changes length depending on input */
453 (void)data;
454 (void)reader;
455 return -1;
458static const struct Curl_crtype cr_eob = {
459 "cr-smtp-eob",
460 cr_eob_init,
461 cr_eob_read,
462 cr_eob_close,
463 Curl_creader_def_needs_rewind,
464 cr_eob_total_length,
465 Curl_creader_def_resume_from,
466 Curl_creader_def_cntrl,
467 Curl_creader_def_is_paused,
468 Curl_creader_def_done,
469 sizeof(struct cr_eob_ctx)
470};
472static CURLcode cr_eob_add(struct Curl_easy *data)
474 struct Curl_creader *reader = NULL;
475 CURLcode result;
477 result = Curl_creader_create(&reader, data, &cr_eob, CURL_CR_CONTENT_ENCODE);
478 if(!result)
479 result = Curl_creader_add(data, reader);
481 if(result && reader)
482 Curl_creader_free(data, reader);
483 return result;
486/***********************************************************************
487 *
488 * smtp_endofresp()
489 *
490 * Checks for an ending SMTP status code at the start of the given string, but
491 * also detects various capabilities from the EHLO response including the
492 * supported authentication mechanisms.
493 */
494static bool smtp_endofresp(struct Curl_easy *data, struct connectdata *conn,
495 const char *line, size_t len, int *resp)
497 struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
498 bool end = FALSE;
499 (void)data;
501 DEBUGASSERT(smtpc);
502 if(!smtpc)
503 return FALSE;
505 /* Nothing for us */
506 if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2]))
507 return FALSE;
509 /* Do we have a command response? This should be the response code followed
510 by a space and optionally some text as per RFC-5321 and as outlined in
511 Section 4. Examples of RFC-4954 but some email servers ignore this and
512 only send the response code instead as per Section 4.2. */
513 if(line[3] == ' ' || len == 5) {
514 char tmpline[6];
515 curl_off_t code;
516 const char *p = tmpline;
517 end = TRUE;
518 memcpy(tmpline, line, (len == 5 ? 5 : 3));
519 tmpline[len == 5 ? 5 : 3] = 0;
520 if(curlx_str_number(&p, &code, len == 5 ? 99999 : 999))
521 return FALSE;
522 *resp = (int)code;
524 /* Make sure real server never sends internal value */
525 if(*resp == 1)
526 *resp = 0;
527 }
528 /* Do we have a multiline (continuation) response? */
529 else if(line[3] == '-' &&
530 (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) {
531 end = TRUE;
532 *resp = 1; /* Internal response code */
533 }
535 return end;
538/***********************************************************************
539 *
540 * smtp_get_message()
541 *
542 * Gets the authentication message from the response buffer.
543 */
544static CURLcode smtp_get_message(struct Curl_easy *data, struct bufref *out)
546 struct smtp_conn *smtpc =
547 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
548 char *message;
549 size_t len;
551 if(!smtpc)
552 return CURLE_FAILED_INIT;
554 message = curlx_dyn_ptr(&smtpc->pp.recvbuf);
555 len = smtpc->pp.nfinal;
556 if(len > 4) {
557 /* Find the start of the message */
558 len -= 4;
559 for(message += 4; ISBLANK(*message); message++, len--)
560 ;
562 /* Find the end of the message */
563 while(len--)
564 if(!ISNEWLINE(message[len]) && !ISBLANK(message[len]))
565 break;
567 /* Terminate the message */
568 message[++len] = '\0';
569 Curl_bufref_set(out, message, len, NULL);
570 }
571 else
572 /* junk input => zero length output */
573 Curl_bufref_set(out, "", 0, NULL);
575 return CURLE_OK;
578/***********************************************************************
579 *
580 * smtp_state()
581 *
582 * This is the ONLY way to change SMTP state!
583 */
584static void smtp_state(struct Curl_easy *data,
585 struct smtp_conn *smtpc,
586 smtpstate newstate)
588#ifdef CURLVERBOSE
589 /* for debug purposes */
590 static const char * const names[] = {
591 "STOP",
592 "SERVERGREET",
593 "EHLO",
594 "HELO",
595 "STARTTLS",
596 "UPGRADETLS",
597 "AUTH",
598 "COMMAND",
599 "MAIL",
600 "RCPT",
601 "DATA",
602 "POSTDATA",
603 "QUIT",
604 /* LAST */
605 };
607 if(smtpc->state != newstate)
608 CURL_TRC_SMTP(data, "state change from %s to %s",
609 names[smtpc->state], names[newstate]);
610#else
611 (void)data;
612#endif
614 smtpc->state = newstate;
617/***********************************************************************
618 *
619 * smtp_perform_ehlo()
620 *
621 * Sends the EHLO command to not only initialize communication with the ESMTP
622 * server but to also obtain a list of server side supported capabilities.
623 */
624static CURLcode smtp_perform_ehlo(struct Curl_easy *data,
625 struct smtp_conn *smtpc)
627 CURLcode result = CURLE_OK;
629 smtpc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanism yet */
630 smtpc->sasl.authused = SASL_AUTH_NONE; /* Clear the authentication mechanism
631 used for esmtp connections */
632 smtpc->tls_supported = FALSE; /* Clear the TLS capability */
633 smtpc->auth_supported = FALSE; /* Clear the AUTH capability */
635 /* Send the EHLO command */
636 result = Curl_pp_sendf(data, &smtpc->pp, "EHLO %s", smtpc->domain);
638 if(!result)
639 smtp_state(data, smtpc, SMTP_EHLO);
641 return result;
644/***********************************************************************
645 *
646 * smtp_perform_helo()
647 *
648 * Sends the HELO command to initialize communication with the SMTP server.
649 */
650static CURLcode smtp_perform_helo(struct Curl_easy *data,
651 struct smtp_conn *smtpc)
653 CURLcode result = CURLE_OK;
655 smtpc->sasl.authused = SASL_AUTH_NONE; /* No authentication mechanism used
656 in smtp connections */
658 /* Send the HELO command */
659 result = Curl_pp_sendf(data, &smtpc->pp, "HELO %s", smtpc->domain);
661 if(!result)
662 smtp_state(data, smtpc, SMTP_HELO);
664 return result;
667/***********************************************************************
668 *
669 * smtp_perform_starttls()
670 *
671 * Sends the STLS command to start the upgrade to TLS.
672 */
673static CURLcode smtp_perform_starttls(struct Curl_easy *data,
674 struct smtp_conn *smtpc)
676 /* Send the STARTTLS command */
677 CURLcode result = Curl_pp_sendf(data, &smtpc->pp, "%s", "STARTTLS");
679 if(!result)
680 smtp_state(data, smtpc, SMTP_STARTTLS);
682 return result;
685/***********************************************************************
686 *
687 * smtp_perform_upgrade_tls()
688 *
689 * Performs the upgrade to TLS.
690 */
691static CURLcode smtp_perform_upgrade_tls(struct Curl_easy *data,
692 struct smtp_conn *smtpc)
694#ifdef USE_SSL
695 /* Start the SSL connection */
696 struct connectdata *conn = data->conn;
697 CURLcode result;
698 bool ssldone = FALSE;
700 DEBUGASSERT(smtpc->state == SMTP_UPGRADETLS);
701 if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
702 result = Curl_ssl_cfilter_add(
703 data, Curl_conn_get_origin(conn, FIRSTSOCKET), conn, FIRSTSOCKET);
704 if(result)
705 goto out;
706 /* Change the connection handler and SMTP state */
707 conn->scheme = &Curl_scheme_smtps;
708 }
710 DEBUGASSERT(!smtpc->ssldone);
711 result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
712 DEBUGF(infof(data, "smtp_perform_upgrade_tls, connect -> %d, %d",
713 (int)result, ssldone));
714 if(!result && ssldone) {
715 smtpc->ssldone = ssldone;
716 /* perform EHLO now, changes smtp->state out of SMTP_UPGRADETLS */
717 result = smtp_perform_ehlo(data, smtpc);
718 }
719out:
720 return result;
721#else
722 (void)data;
723 (void)smtpc;
724 return CURLE_NOT_BUILT_IN;
725#endif
728/***********************************************************************
729 *
730 * smtp_perform_auth()
731 *
732 * Sends an AUTH command allowing the client to login with the given SASL
733 * authentication mechanism.
734 */
735static CURLcode smtp_perform_auth(struct Curl_easy *data,
736 const char *mech,
737 const struct bufref *initresp)
739 CURLcode result = CURLE_OK;
740 struct smtp_conn *smtpc =
741 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
742 const char *ir = Curl_bufref_ptr(initresp);
744 if(!smtpc)
745 return CURLE_FAILED_INIT;
747 if(ir) { /* AUTH <mech> ...<crlf> */
748 /* Send the AUTH command with the initial response */
749 result = Curl_pp_sendf(data, &smtpc->pp, "AUTH %s %s",
750 mech, *ir ? ir : "=");
751 }
752 else {
753 /* Send the AUTH command */
754 result = Curl_pp_sendf(data, &smtpc->pp, "AUTH %s", mech);
755 }
757 return result;
760/***********************************************************************
761 *
762 * smtp_continue_auth()
763 *
764 * Sends SASL continuation data.
765 */
766static CURLcode smtp_continue_auth(struct Curl_easy *data,
767 const char *mech,
768 const struct bufref *resp)
770 struct smtp_conn *smtpc =
771 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
773 (void)mech;
774 if(!smtpc)
775 return CURLE_FAILED_INIT;
776 return Curl_pp_sendf(data, &smtpc->pp, "%s", Curl_bufref_ptr(resp));
779/***********************************************************************
780 *
781 * smtp_cancel_auth()
782 *
783 * Sends SASL cancellation.
784 */
785static CURLcode smtp_cancel_auth(struct Curl_easy *data, const char *mech)
787 struct smtp_conn *smtpc =
788 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
790 (void)mech;
791 if(!smtpc)
792 return CURLE_FAILED_INIT;
793 return Curl_pp_sendf(data, &smtpc->pp, "*");
796/***********************************************************************
797 *
798 * smtp_perform_authentication()
799 *
800 * Initiates the authentication sequence, with the appropriate SASL
801 * authentication mechanism.
802 */
803static CURLcode smtp_perform_authentication(struct Curl_easy *data,
804 struct smtp_conn *smtpc)
806 CURLcode result = CURLE_OK;
807 saslprogress progress;
809 /* Check we have enough data to authenticate with, and the
810 server supports authentication, and end the connect phase if not */
811 if(!smtpc->auth_supported ||
812 !Curl_sasl_can_authenticate(&smtpc->sasl, data)) {
813 smtp_state(data, smtpc, SMTP_STOP);
814 return result;
815 }
817 /* Calculate the SASL login details */
818 result = Curl_sasl_start(&smtpc->sasl, data, FALSE, &progress);
820 if(!result) {
821 if(progress == SASL_INPROGRESS)
822 smtp_state(data, smtpc, SMTP_AUTH);
823 else
824 result = Curl_sasl_is_blocked(&smtpc->sasl, data);
825 }
827 return result;
830/***********************************************************************
831 *
832 * smtp_perform_command()
833 *
834 * Sends an SMTP based command.
835 */
836static CURLcode smtp_perform_command(struct Curl_easy *data,
837 struct smtp_conn *smtpc,
838 struct SMTP *smtp)
840 CURLcode result = CURLE_OK;
842 if(smtp->rcpt) {
843 /* We notify the server we are sending UTF-8 data if a) it supports the
844 SMTPUTF8 extension and b) The mailbox contains UTF-8 characters, in
845 either the local address or hostname parts. This is regardless of
846 whether the hostname is encoded using IDN ACE */
847 bool utf8 = FALSE;
849 if(!smtp->custom || !smtp->custom[0]) {
850 char *address = NULL;
851 struct hostname host = { NULL, NULL, NULL, NULL };
852 const char *suffix = "";
854 /* Parse the mailbox to verify into the local address and hostname
855 parts, converting the hostname to an IDN A-label if necessary */
856 result = smtp_parse_address(data, smtp->rcpt->data,
857 &address, &host, &suffix);
858 if(result)
859 return result;
861 /* Establish whether we should report SMTPUTF8 to the server for this
862 mailbox as per RFC-6531 sect. 3.1 point 6 */
863 utf8 = smtpc->utf8_supported &&
864 (host.encalloc ||
865 !Curl_is_ASCII_name(address) ||
866 !Curl_is_ASCII_name(host.name));
868 /* Send the VRFY command (Note: The hostname part may be absent when the
869 host is a local system) */
870 result = Curl_pp_sendf(data, &smtpc->pp, "VRFY %s%s%s%s",
871 address,
872 host.name ? "@" : "",
873 host.name ? host.name : "",
874 utf8 ? " SMTPUTF8" : "");
876 Curl_free_idnconverted_hostname(&host);
877 curlx_free(address);
878 }
879 else {
880 /* Establish whether we should report that we support SMTPUTF8 for EXPN
881 commands to the server as per RFC-6531 sect. 3.1 point 6 */
882 utf8 = smtpc->utf8_supported && !strcmp(smtp->custom, "EXPN");
884 /* Send the custom recipient based command such as the EXPN command */
885 result = Curl_pp_sendf(data, &smtpc->pp,
886 "%s %s%s", smtp->custom,
887 smtp->rcpt->data,
888 utf8 ? " SMTPUTF8" : "");
889 }
890 }
891 else
892 /* Send the non-recipient based command such as HELP */
893 result = Curl_pp_sendf(data, &smtpc->pp, "%s",
894 smtp->custom && smtp->custom[0] != '\0' ?
895 smtp->custom : "HELP");
897 if(!result)
898 smtp_state(data, smtpc, SMTP_COMMAND);
900 return result;
903/***********************************************************************
904 *
905 * smtp_perform_mail()
906 *
907 * Sends an MAIL command to initiate the upload of a message.
908 */
909static CURLcode smtp_perform_mail(struct Curl_easy *data,
910 struct smtp_conn *smtpc,
911 struct SMTP *smtp)
913 char *from = NULL;
914 char *auth = NULL;
915 char *size = NULL;
916 const char *str;
917 CURLcode result = CURLE_OK;
919 /* We notify the server we are sending UTF-8 data if a) it supports the
920 SMTPUTF8 extension and b) The mailbox contains UTF-8 characters, in
921 either the local address or hostname parts. This is regardless of
922 whether the hostname is encoded using IDN ACE */
923 bool utf8 = FALSE;
925 /* Calculate the FROM parameter */
926 str = CURL_EASY_STR(data, STRING_MAIL_FROM);
927 if(str) {
928 char *address = NULL;
929 struct hostname host = { NULL, NULL, NULL, NULL };
930 const char *suffix = "";
932 /* Parse the FROM mailbox into the local address and hostname parts,
933 converting the hostname to an IDN A-label if necessary */
934 result = smtp_parse_address(data, str, &address, &host, &suffix);
935 if(result)
936 goto out;
938 /* Establish whether we should report SMTPUTF8 to the server for this
939 mailbox as per RFC-6531 sect. 3.1 point 4 and sect. 3.4 */
940 utf8 = smtpc->utf8_supported &&
941 (host.encalloc ||
942 !Curl_is_ASCII_name(address) ||
943 !Curl_is_ASCII_name(host.name));
945 if(host.name) {
946 from = curl_maprintf("<%s@%s>%s", address, host.name, suffix);
948 Curl_free_idnconverted_hostname(&host);
949 }
950 else
951 /* An invalid mailbox was provided but we let the server worry
952 about that and reply with a 501 error */
953 from = curl_maprintf("<%s>%s", address, suffix);
955 curlx_free(address);
956 }
957 else
958 /* Null reverse-path, RFC-5321, sect. 3.6.3 */
959 from = curlx_strdup("<>");
961 if(!from) {
962 result = CURLE_OUT_OF_MEMORY;
963 goto out;
964 }
966 /* Calculate the optional AUTH parameter */
967 str = CURL_EASY_STR(data, STRING_MAIL_AUTH);
968 if(str && smtpc->sasl.authused) {
969 if(str[0] != '\0') {
970 char *address = NULL;
971 struct hostname host = { NULL, NULL, NULL, NULL };
972 const char *suffix = "";
974 /* Parse the AUTH mailbox into the local address and hostname parts,
975 converting the hostname to an IDN A-label if necessary */
976 result = smtp_parse_address(data, str, &address, &host, &suffix);
977 if(result)
978 goto out;
980 /* Establish whether we should report SMTPUTF8 to the server for this
981 mailbox as per RFC-6531 sect. 3.1 point 4 and sect. 3.4 */
982 if(!utf8 && smtpc->utf8_supported &&
983 (host.encalloc ||
984 !Curl_is_ASCII_name(address) ||
985 !Curl_is_ASCII_name(host.name)))
986 utf8 = TRUE;
988 if(host.name) {
989 auth = curl_maprintf("<%s@%s>%s", address, host.name, suffix);
991 Curl_free_idnconverted_hostname(&host);
992 }
993 else
994 /* An invalid mailbox was provided but we let the server worry
995 about it */
996 auth = curl_maprintf("<%s>%s", address, suffix);
997 curlx_free(address);
998 }
999 else
1000 /* Empty AUTH, RFC-2554, sect. 5 */
1001 auth = curlx_strdup("<>");
1003 if(!auth) {
1004 result = CURLE_OUT_OF_MEMORY;
1005 goto out;
1009#ifndef CURL_DISABLE_MIME
1010 /* Prepare the mime data if some. */
1011 if(IS_MIME_POST(data)) {
1012 curl_mimepart *postp = data->set.mimepostp;
1014 /* Use the whole structure as data. */
1015 postp->flags &= ~(unsigned int)MIME_BODY_ONLY;
1017 /* Add external headers and mime version. */
1018 curl_mime_headers(postp, data->set.headers, 0);
1019 result = Curl_mime_prepare_headers(data, postp, NULL,
1020 NULL, MIMESTRATEGY_MAIL);
1022 if(!result && !Curl_checkheaders(data, STRCONST("Mime-Version")))
1023 result = Curl_mime_add_header(&postp->curlheaders, "Mime-Version: 1.0");
1024 if(!result)
1025 result = Curl_creader_set_mime(data, postp);
1026 if(result)
1027 goto out;
1028 data->state.infilesize = Curl_creader_total_length(data);
1030 else
1031#endif
1033 result = Curl_creader_set_fread(data, data->state.infilesize);
1034 if(result)
1035 goto out;
1038 /* Calculate the optional SIZE parameter */
1039 if(smtpc->size_supported && data->state.infilesize > 0) {
1040 size = curl_maprintf("%" FMT_OFF_T, data->state.infilesize);
1042 if(!size) {
1043 result = CURLE_OUT_OF_MEMORY;
1044 goto out;
1048 /* If the mailboxes in the FROM and AUTH parameters do not include a UTF-8
1049 based address then quickly scan through the recipient list and check if
1050 any there do, as we need to correctly identify our support for SMTPUTF8
1051 in the envelope, as per RFC-6531 sect. 3.4 */
1052 if(smtpc->utf8_supported && !utf8) {
1053 struct curl_slist *rcpt = smtp->rcpt;
1055 while(rcpt && !utf8) {
1056 /* Does the hostname contain non-ASCII characters? */
1057 if(!Curl_is_ASCII_name(rcpt->data))
1058 utf8 = TRUE;
1060 rcpt = rcpt->next;
1064 /* Add the client reader doing STMP EOB escaping */
1065 result = cr_eob_add(data);
1066 if(result)
1067 goto out;
1069 /* Send the MAIL command */
1070 result = Curl_pp_sendf(data, &smtpc->pp,
1071 "MAIL FROM:%s%s%s%s%s%s",
1072 from, /* Mandatory */
1073 auth ? " AUTH=" : "", /* Optional on AUTH support */
1074 auth ? auth : "",
1075 size ? " SIZE=" : "", /* Optional on SIZE support */
1076 size ? size : "",
1077 utf8 ? " SMTPUTF8" /* Internationalised mailbox */
1078 : ""); /* included in our envelope */
1080out:
1081 curlx_free(from);
1082 curlx_free(auth);
1083 curlx_free(size);
1085 if(!result)
1086 smtp_state(data, smtpc, SMTP_MAIL);
1088 return result;
1091/***********************************************************************
1093 * smtp_perform_rcpt_to()
1095 * Sends a RCPT TO command for a given recipient as part of the message upload
1096 * process.
1097 */
1098static CURLcode smtp_perform_rcpt_to(struct Curl_easy *data,
1099 struct smtp_conn *smtpc,
1100 struct SMTP *smtp)
1102 CURLcode result = CURLE_OK;
1103 char *address = NULL;
1104 struct hostname host = { NULL, NULL, NULL, NULL };
1105 const char *suffix = "";
1107 /* Parse the recipient mailbox into the local address and hostname parts,
1108 converting the hostname to an IDN A-label if necessary */
1109 result = smtp_parse_address(data, smtp->rcpt->data,
1110 &address, &host, &suffix);
1111 if(result)
1112 return result;
1114 /* Send the RCPT TO command */
1115 if(host.name)
1116 result = Curl_pp_sendf(data, &smtpc->pp, "RCPT TO:<%s@%s>%s",
1117 address, host.name, suffix);
1118 else
1119 /* An invalid mailbox was provided but we let the server worry about
1120 that and reply with a 501 error */
1121 result = Curl_pp_sendf(data, &smtpc->pp, "RCPT TO:<%s>%s",
1122 address, suffix);
1124 Curl_free_idnconverted_hostname(&host);
1125 curlx_free(address);
1127 if(!result)
1128 smtp_state(data, smtpc, SMTP_RCPT);
1130 return result;
1133/***********************************************************************
1135 * smtp_perform_quit()
1137 * Performs the quit action prior to sclose() being called.
1138 */
1139static CURLcode smtp_perform_quit(struct Curl_easy *data,
1140 struct smtp_conn *smtpc)
1142 /* Send the QUIT command */
1143 CURLcode result = Curl_pp_sendf(data, &smtpc->pp, "%s", "QUIT");
1145 if(!result)
1146 smtp_state(data, smtpc, SMTP_QUIT);
1148 return result;
1151/* For the initial server greeting */
1152static CURLcode smtp_state_servergreet_resp(struct Curl_easy *data,
1153 struct smtp_conn *smtpc,
1154 int smtpcode,
1155 smtpstate instate)
1157 CURLcode result = CURLE_OK;
1158 (void)instate;
1160 if(smtpcode / 100 != 2) {
1161 failf(data, "Got unexpected smtp-server response: %d", smtpcode);
1162 result = CURLE_WEIRD_SERVER_REPLY;
1164 else
1165 result = smtp_perform_ehlo(data, smtpc);
1167 return result;
1170/* For STARTTLS responses */
1171static CURLcode smtp_state_starttls_resp(struct Curl_easy *data,
1172 struct smtp_conn *smtpc,
1173 int smtpcode,
1174 smtpstate instate)
1176 CURLcode result = CURLE_OK;
1177 (void)instate;
1179 /* Pipelining in response is forbidden. */
1180 if(smtpc->pp.overflow)
1181 return CURLE_WEIRD_SERVER_REPLY;
1183 if(smtpcode != 220) {
1184 if(data->set.use_ssl != CURLUSESSL_TRY) {
1185 failf(data, "STARTTLS denied, code %d", smtpcode);
1186 result = CURLE_USE_SSL_FAILED;
1188 else
1189 result = smtp_perform_authentication(data, smtpc);
1191 else
1192 smtp_state(data, smtpc, SMTP_UPGRADETLS);
1194 return result;
1197/* For EHLO responses */
1198static CURLcode smtp_state_ehlo_resp(struct Curl_easy *data,
1199 struct smtp_conn *smtpc,
1200 int smtpcode,
1201 smtpstate instate)
1203 CURLcode result = CURLE_OK;
1204 const char *line = curlx_dyn_ptr(&smtpc->pp.recvbuf);
1205 size_t len = smtpc->pp.nfinal;
1207 (void)instate;
1209 if(smtpcode / 100 != 2 && smtpcode != 1) {
1210 if(data->set.use_ssl <= CURLUSESSL_TRY ||
1211 Curl_conn_is_ssl(data->conn, FIRSTSOCKET))
1212 result = smtp_perform_helo(data, smtpc);
1213 else {
1214 failf(data, "Remote access denied: %d", smtpcode);
1215 result = CURLE_REMOTE_ACCESS_DENIED;
1218 else if(len >= 4) {
1219 line += 4;
1220 len -= 4;
1222 /* Does the server support the STARTTLS capability? */
1223 if(len >= 8 && curl_strnequal(line, "STARTTLS", 8))
1224 smtpc->tls_supported = TRUE;
1226 /* Does the server support the SIZE capability? */
1227 else if(len >= 4 && curl_strnequal(line, "SIZE", 4))
1228 smtpc->size_supported = TRUE;
1230 /* Does the server support the UTF-8 capability? */
1231 else if(len >= 8 && curl_strnequal(line, "SMTPUTF8", 8))
1232 smtpc->utf8_supported = TRUE;
1234 /* Does the server support authentication? */
1235 else if(len >= 5 && curl_strnequal(line, "AUTH ", 5)) {
1236 smtpc->auth_supported = TRUE;
1238 /* Advance past the AUTH keyword */
1239 line += 5;
1240 len -= 5;
1242 /* Loop through the data line */
1243 for(;;) {
1244 size_t llen;
1245 size_t wordlen;
1246 unsigned short mechbit;
1248 while(len && (ISBLANK(*line) || ISNEWLINE(*line))) {
1249 line++;
1250 len--;
1253 if(!len)
1254 break;
1256 /* Extract the word */
1257 for(wordlen = 0; wordlen < len && !ISBLANK(line[wordlen]) &&
1258 !ISNEWLINE(line[wordlen]);)
1259 wordlen++;
1261 /* Test the word for a matching authentication mechanism */
1262 mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
1263 if(mechbit && llen == wordlen)
1264 smtpc->sasl.authmechs |= mechbit;
1266 line += wordlen;
1267 len -= wordlen;
1271 if(smtpcode != 1) {
1272 if(data->set.use_ssl && !Curl_conn_is_ssl(data->conn, FIRSTSOCKET)) {
1273 /* We do not have an SSL/TLS connection yet, but SSL is requested */
1274 if(smtpc->tls_supported)
1275 /* Switch to TLS connection now */
1276 result = smtp_perform_starttls(data, smtpc);
1277 else if(data->set.use_ssl == CURLUSESSL_TRY)
1278 /* Fallback and carry on with authentication */
1279 result = smtp_perform_authentication(data, smtpc);
1280 else {
1281 failf(data, "STARTTLS not supported.");
1282 result = CURLE_USE_SSL_FAILED;
1285 else
1286 result = smtp_perform_authentication(data, smtpc);
1289 else {
1290 failf(data, "Unexpectedly short EHLO response");
1291 result = CURLE_WEIRD_SERVER_REPLY;
1294 return result;
1297/* For HELO responses */
1298static CURLcode smtp_state_helo_resp(struct Curl_easy *data,
1299 struct smtp_conn *smtpc,
1300 int smtpcode,
1301 smtpstate instate)
1303 CURLcode result = CURLE_OK;
1304 (void)instate;
1306 if(smtpcode / 100 != 2) {
1307 failf(data, "Remote access denied: %d", smtpcode);
1308 result = CURLE_REMOTE_ACCESS_DENIED;
1310 else
1311 /* End of connect phase */
1312 smtp_state(data, smtpc, SMTP_STOP);
1314 return result;
1317/* For SASL authentication responses */
1318static CURLcode smtp_state_auth_resp(struct Curl_easy *data,
1319 struct smtp_conn *smtpc,
1320 int smtpcode,
1321 smtpstate instate)
1323 CURLcode result = CURLE_OK;
1324 saslprogress progress;
1326 (void)instate;
1328 result = Curl_sasl_continue(&smtpc->sasl, data, smtpcode, &progress);
1329 if(!result)
1330 switch(progress) {
1331 case SASL_DONE:
1332 smtp_state(data, smtpc, SMTP_STOP); /* Authenticated */
1333 break;
1334 case SASL_IDLE: /* No mechanism left after cancellation */
1335 failf(data, "Authentication cancelled");
1336 result = CURLE_LOGIN_DENIED;
1337 break;
1338 default:
1339 break;
1342 return result;
1345/* For command responses */
1346static CURLcode smtp_state_command_resp(struct Curl_easy *data,
1347 struct smtp_conn *smtpc,
1348 struct SMTP *smtp,
1349 int smtpcode,
1350 smtpstate instate)
1352 CURLcode result = CURLE_OK;
1353 const char *line = curlx_dyn_ptr(&smtpc->pp.recvbuf);
1354 size_t len = smtpc->pp.nfinal;
1356 (void)instate;
1358 if((smtp->rcpt && smtpcode / 100 != 2 && smtpcode != 553 && smtpcode != 1) ||
1359 (!smtp->rcpt && smtpcode / 100 != 2 && smtpcode != 1)) {
1360 failf(data, "Command failed: %d", smtpcode);
1361 result = CURLE_WEIRD_SERVER_REPLY;
1363 else {
1364 if(!data->req.no_body)
1365 result = Curl_client_write(data, CLIENTWRITE_BODY, line, len);
1367 if(!result && (smtpcode != 1)) {
1368 if(smtp->rcpt) {
1369 smtp->rcpt = smtp->rcpt->next;
1371 if(smtp->rcpt) {
1372 /* Send the next command */
1373 result = smtp_perform_command(data, smtpc, smtp);
1375 else
1376 /* End of DO phase */
1377 smtp_state(data, smtpc, SMTP_STOP);
1379 else
1380 /* End of DO phase */
1381 smtp_state(data, smtpc, SMTP_STOP);
1385 return result;
1388/* For MAIL responses */
1389static CURLcode smtp_state_mail_resp(struct Curl_easy *data,
1390 struct smtp_conn *smtpc,
1391 struct SMTP *smtp,
1392 int smtpcode,
1393 smtpstate instate)
1395 CURLcode result = CURLE_OK;
1396 (void)instate;
1398 if(smtpcode / 100 != 2) {
1399 failf(data, "MAIL failed: %d", smtpcode);
1400 result = CURLE_SEND_ERROR;
1402 else
1403 /* Start the RCPT TO command */
1404 result = smtp_perform_rcpt_to(data, smtpc, smtp);
1406 return result;
1409/* For RCPT responses */
1410static CURLcode smtp_state_rcpt_resp(struct Curl_easy *data,
1411 struct smtp_conn *smtpc,
1412 struct SMTP *smtp,
1413 int smtpcode,
1414 smtpstate instate)
1416 CURLcode result = CURLE_OK;
1417 bool is_smtp_err = FALSE;
1418 bool is_smtp_blocking_err = FALSE;
1420 (void)instate;
1422 is_smtp_err = (smtpcode / 100 != 2);
1424 /* If there are multiple RCPT TO commands to issue, it is possible to
1425 ignore errors and proceed with only the valid addresses. */
1426 is_smtp_blocking_err = (is_smtp_err && !data->set.mail_rcpt_allowfails);
1428 if(is_smtp_err) {
1429 /* Remembering the last failure which we can report if all "RCPT TO" have
1430 failed and we cannot proceed. */
1431 smtp->rcpt_last_error = smtpcode;
1433 if(is_smtp_blocking_err) {
1434 failf(data, "RCPT failed: %d", smtpcode);
1435 result = CURLE_SEND_ERROR;
1438 else {
1439 /* Some RCPT TO commands have succeeded. */
1440 smtp->rcpt_had_ok = TRUE;
1443 if(!is_smtp_blocking_err) {
1444 smtp->rcpt = smtp->rcpt->next;
1446 if(smtp->rcpt)
1447 /* Send the next RCPT TO command */
1448 result = smtp_perform_rcpt_to(data, smtpc, smtp);
1449 else {
1450 /* We were not able to issue a successful RCPT TO command while going
1451 over recipients (potentially multiple). Sending back last error. */
1452 if(!smtp->rcpt_had_ok) {
1453 failf(data, "RCPT failed: %d (last error)", smtp->rcpt_last_error);
1454 result = CURLE_SEND_ERROR;
1456 else {
1457 /* Send the DATA command */
1458 result = Curl_pp_sendf(data, &smtpc->pp, "%s", "DATA");
1460 if(!result)
1461 smtp_state(data, smtpc, SMTP_DATA);
1466 return result;
1469/* For DATA response */
1470static CURLcode smtp_state_data_resp(struct Curl_easy *data,
1471 struct smtp_conn *smtpc,
1472 int smtpcode,
1473 smtpstate instate)
1475 CURLcode result = CURLE_OK;
1476 (void)instate;
1478 if(smtpcode != 354) {
1479 failf(data, "DATA failed: %d", smtpcode);
1480 result = CURLE_SEND_ERROR;
1482 else {
1483 /* Set the progress upload size */
1484 Curl_pgrsSetUploadSize(data, data->state.infilesize);
1486 /* SMTP upload */
1487 Curl_xfer_setup_send(data, FIRSTSOCKET);
1489 /* End of DO phase */
1490 smtp_state(data, smtpc, SMTP_STOP);
1493 return result;
1496/* For POSTDATA responses, which are received after the entire DATA
1497 part has been sent to the server */
1498static CURLcode smtp_state_postdata_resp(struct Curl_easy *data,
1499 struct smtp_conn *smtpc,
1500 int smtpcode,
1501 smtpstate instate)
1503 CURLcode result = CURLE_OK;
1505 (void)instate;
1507 if(smtpcode != 250)
1508 result = CURLE_WEIRD_SERVER_REPLY;
1510 /* End of DONE phase */
1511 smtp_state(data, smtpc, SMTP_STOP);
1513 return result;
1516static CURLcode smtp_pp_statemachine(struct Curl_easy *data,
1517 struct connectdata *conn)
1519 CURLcode result = CURLE_OK;
1520 int smtpcode;
1521 struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1522 struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1523 size_t nread = 0;
1525 if(!smtpc || !smtp)
1526 return CURLE_FAILED_INIT;
1528 /* Busy upgrading the connection; right now all I/O is SSL/TLS, not SMTP */
1529upgrade_tls:
1530 if(smtpc->state == SMTP_UPGRADETLS) {
1531 result = smtp_perform_upgrade_tls(data, smtpc);
1532 if(result || (smtpc->state == SMTP_UPGRADETLS))
1533 return result;
1536 /* Flush any data that needs to be sent */
1537 if(smtpc->pp.sendleft)
1538 return Curl_pp_flushsend(data, &smtpc->pp);
1540 do {
1541 /* Read the response from the server */
1542 result = Curl_pp_readresp(data, FIRSTSOCKET, &smtpc->pp,
1543 &smtpcode, &nread);
1544 if(result)
1545 return result;
1547 /* Store the latest response for later retrieval if necessary */
1548 if(smtpc->state != SMTP_QUIT && smtpcode != 1)
1549 data->info.httpcode = smtpcode;
1551 if(!smtpcode)
1552 break;
1554 /* We have now received a full SMTP server response */
1555 switch(smtpc->state) {
1556 case SMTP_SERVERGREET:
1557 result = smtp_state_servergreet_resp(data, smtpc,
1558 smtpcode, smtpc->state);
1559 break;
1561 case SMTP_EHLO:
1562 result = smtp_state_ehlo_resp(data, smtpc, smtpcode, smtpc->state);
1563 break;
1565 case SMTP_HELO:
1566 result = smtp_state_helo_resp(data, smtpc, smtpcode, smtpc->state);
1567 break;
1569 case SMTP_STARTTLS:
1570 result = smtp_state_starttls_resp(data, smtpc, smtpcode, smtpc->state);
1571 /* During UPGRADETLS, leave the read loop as we need to connect
1572 * (e.g. TLS handshake) before we continue sending/receiving. */
1573 if(!result && (smtpc->state == SMTP_UPGRADETLS))
1574 goto upgrade_tls;
1575 break;
1577 case SMTP_AUTH:
1578 result = smtp_state_auth_resp(data, smtpc, smtpcode, smtpc->state);
1579 break;
1581 case SMTP_COMMAND:
1582 result = smtp_state_command_resp(data, smtpc, smtp,
1583 smtpcode, smtpc->state);
1584 break;
1586 case SMTP_MAIL:
1587 result = smtp_state_mail_resp(data, smtpc, smtp, smtpcode, smtpc->state);
1588 break;
1590 case SMTP_RCPT:
1591 result = smtp_state_rcpt_resp(data, smtpc, smtp, smtpcode, smtpc->state);
1592 break;
1594 case SMTP_DATA:
1595 result = smtp_state_data_resp(data, smtpc, smtpcode, smtpc->state);
1596 break;
1598 case SMTP_POSTDATA:
1599 result = smtp_state_postdata_resp(data, smtpc, smtpcode, smtpc->state);
1600 break;
1602 case SMTP_QUIT:
1603 default:
1604 /* internal error */
1605 smtp_state(data, smtpc, SMTP_STOP);
1606 break;
1608 } while(!result && smtpc->state != SMTP_STOP &&
1609 Curl_pp_moredata(&smtpc->pp));
1611 return result;
1614/* Called repeatedly until done from multi.c */
1615static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done)
1617 CURLcode result = CURLE_OK;
1618 struct smtp_conn *smtpc =
1619 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1621 *done = FALSE;
1622 if(!smtpc)
1623 return CURLE_FAILED_INIT;
1625 result = Curl_pp_statemach(data, &smtpc->pp, FALSE, FALSE);
1626 *done = (smtpc->state == SMTP_STOP);
1627 return result;
1630static CURLcode smtp_block_statemach(struct Curl_easy *data,
1631 struct smtp_conn *smtpc,
1632 bool disconnecting)
1634 CURLcode result = CURLE_OK;
1636 while(smtpc->state != SMTP_STOP && !result)
1637 result = Curl_pp_statemach(data, &smtpc->pp, TRUE, disconnecting);
1639 return result;
1642/* For the SMTP "protocol connect" and "doing" phases only */
1643static CURLcode smtp_pollset(struct Curl_easy *data,
1644 struct easy_pollset *ps)
1646 struct smtp_conn *smtpc =
1647 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1648 return smtpc ? Curl_pp_pollset(data, &smtpc->pp, ps) : CURLE_OK;
1651/* SASL parameters for the smtp protocol */
1652static const struct SASLproto saslsmtp = {
1653 "smtp", /* The service name */
1654 smtp_perform_auth, /* Send authentication command */
1655 smtp_continue_auth, /* Send authentication continuation */
1656 smtp_cancel_auth, /* Cancel authentication */
1657 smtp_get_message, /* Get SASL response message */
1658 512 - 8, /* Max line len - strlen("AUTH ") - 1 space - CRLF */
1659 334, /* Code received when continuation is expected */
1660 235, /* Code to receive upon authentication success */
1661 SASL_AUTH_DEFAULT, /* Default mechanisms */
1662 SASL_FLAG_BASE64 /* Configuration flags */
1665/***********************************************************************
1667 * smtp_connect()
1669 * This function should do everything that is to be considered a part of
1670 * the connection phase.
1672 * The variable pointed to by 'done' will be TRUE if the protocol-layer
1673 * connect phase is done when this function returns, or FALSE if not.
1674 */
1675static CURLcode smtp_connect(struct Curl_easy *data, bool *done)
1677 struct smtp_conn *smtpc =
1678 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1679 CURLcode result = CURLE_OK;
1681 *done = FALSE; /* default to not done yet */
1682 if(!smtpc)
1683 return CURLE_FAILED_INIT;
1685 PINGPONG_SETUP(&smtpc->pp, smtp_pp_statemachine, smtp_endofresp);
1687 /* Initialize the SASL storage */
1688 Curl_sasl_init(&smtpc->sasl, data, &saslsmtp);
1690 /* Initialize the pingpong layer */
1691 Curl_pp_init(&smtpc->pp, Curl_pgrs_now(data));
1693 /* Parse the URL options */
1694 result = smtp_parse_url_options(data->conn, smtpc);
1695 if(result)
1696 return result;
1698 /* Parse the URL path */
1699 result = smtp_parse_url_path(data, smtpc);
1700 if(result)
1701 return result;
1703 /* Start off waiting for the server greeting response */
1704 smtp_state(data, smtpc, SMTP_SERVERGREET);
1706 result = smtp_multi_statemach(data, done);
1708 return result;
1711/***********************************************************************
1713 * smtp_done()
1715 * The DONE function. This does what needs to be done after a single DO has
1716 * performed.
1718 * Input argument is already checked for validity.
1719 */
1720static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
1721 bool premature)
1723 struct smtp_conn *smtpc =
1724 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1725 CURLcode result = CURLE_OK;
1726 struct connectdata *conn = data->conn;
1727 struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1729 (void)premature;
1731 if(!smtpc)
1732 return CURLE_FAILED_INIT;
1733 if(!smtp)
1734 return CURLE_OK;
1736 /* Cleanup our per-request based variables */
1737 curlx_safefree(smtp->custom);
1739 if(status) {
1740 CURL_TRC_M(data, "SMTP done with bad status");
1741 connclose(conn); /* marked for closure */
1742 result = status; /* use the already set error code */
1744 else if(!data->set.connect_only && data->set.mail_rcpt &&
1745 (data->state.upload || IS_MIME_POST(data))) {
1747 smtp_state(data, smtpc, SMTP_POSTDATA);
1749 /* Run the state-machine */
1750 result = smtp_block_statemach(data, smtpc, FALSE);
1753 /* Clear the transfer mode for the next request */
1754 smtp->transfer = PPTRANSFER_BODY;
1755 CURL_TRC_SMTP(data, "smtp_done(status=%d, premature=%d) -> %d",
1756 (int)status, premature, (int)result);
1757 return result;
1760/***********************************************************************
1762 * smtp_perform()
1764 * This is the actual DO function for SMTP. Transfer a mail, send a command
1765 * or get some data according to the options previously setup.
1766 */
1767static CURLcode smtp_perform(struct Curl_easy *data,
1768 struct smtp_conn *smtpc,
1769 struct SMTP *smtp,
1770 bool *connected,
1771 bool *dophase_done)
1773 /* This is SMTP and no proxy */
1774 CURLcode result = CURLE_OK;
1776 CURL_TRC_SMTP(data, "smtp_perform(), start");
1778 if(data->req.no_body) {
1779 /* Requested no body means no transfer */
1780 smtp->transfer = PPTRANSFER_INFO;
1783 *dophase_done = FALSE; /* not done yet */
1785 /* Store the first recipient (or NULL if not specified) */
1786 smtp->rcpt = data->set.mail_rcpt;
1788 /* Track of whether we have successfully sent at least one RCPT TO command */
1789 smtp->rcpt_had_ok = FALSE;
1791 /* Track of the last error we have received by sending RCPT TO command */
1792 smtp->rcpt_last_error = 0;
1794 /* Initial data character is the first character in line: it is implicitly
1795 preceded by a virtual CRLF. */
1796 smtp->trailing_crlf = TRUE;
1797 smtp->eob = 2;
1799 /* Start the first command in the DO phase */
1800 if((data->state.upload || IS_MIME_POST(data)) && data->set.mail_rcpt)
1801 /* MAIL transfer */
1802 result = smtp_perform_mail(data, smtpc, smtp);
1803 else
1804 /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
1805 result = smtp_perform_command(data, smtpc, smtp);
1807 if(result)
1808 goto out;
1810 /* Run the state-machine */
1811 result = smtp_multi_statemach(data, dophase_done);
1813 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
1815out:
1816 CURL_TRC_SMTP(data, "smtp_perform() -> %d, connected=%d, done=%d",
1817 (int)result, *connected, *dophase_done);
1818 return result;
1821/* Call this when the DO phase has completed */
1822static CURLcode smtp_dophase_done(struct Curl_easy *data,
1823 struct SMTP *smtp,
1824 bool connected)
1826 (void)connected;
1828 if(smtp->transfer != PPTRANSFER_BODY)
1829 /* no data to transfer */
1830 Curl_xfer_setup_nop(data);
1832 return CURLE_OK;
1835/***********************************************************************
1837 * smtp_regular_transfer()
1839 * The input argument is already checked for validity.
1841 * Performs all commands done before a regular transfer between a local and a
1842 * remote host.
1843 */
1844static CURLcode smtp_regular_transfer(struct Curl_easy *data,
1845 struct smtp_conn *smtpc,
1846 struct SMTP *smtp,
1847 bool *dophase_done)
1849 CURLcode result = CURLE_OK;
1850 bool connected = FALSE;
1852 /* Make sure size is unknown at this point */
1853 data->req.size = -1;
1855 /* Set the progress data */
1856 Curl_pgrsReset(data);
1858 /* Carry out the perform */
1859 result = smtp_perform(data, smtpc, smtp, &connected, dophase_done);
1861 /* Perform post DO phase operations if necessary */
1862 if(!result && *dophase_done)
1863 result = smtp_dophase_done(data, smtp, connected);
1865 CURL_TRC_SMTP(data, "smtp_regular_transfer() -> %d, done=%d",
1866 (int)result, *dophase_done);
1867 return result;
1870/***********************************************************************
1872 * smtp_do()
1874 * This function is registered as 'curl_do' function. It decodes the path
1875 * parts etc as a wrapper to the actual DO function (smtp_perform).
1877 * The input argument is already checked for validity.
1878 */
1879static CURLcode smtp_do(struct Curl_easy *data, bool *done)
1881 struct smtp_conn *smtpc =
1882 Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1883 struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1884 CURLcode result = CURLE_OK;
1886 DEBUGASSERT(data);
1887 DEBUGASSERT(data->conn);
1888 *done = FALSE; /* default to false */
1889 if(!smtpc || !smtp)
1890 return CURLE_FAILED_INIT;
1892 /* Parse the custom request */
1893 result = smtp_parse_custom_request(data, smtp);
1894 if(result)
1895 return result;
1897 result = smtp_regular_transfer(data, smtpc, smtp, done);
1898 CURL_TRC_SMTP(data, "smtp_do() -> %d, done=%d", (int)result, *done);
1899 return result;
1902/***********************************************************************
1904 * smtp_disconnect()
1906 * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
1907 * resources. BLOCKING.
1908 */
1909static CURLcode smtp_disconnect(struct Curl_easy *data,
1910 struct connectdata *conn,
1911 bool dead_connection)
1913 struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1915 if(!smtpc)
1916 return CURLE_FAILED_INIT;
1918 /* We cannot send quit unconditionally. If this connection is stale or
1919 bad in any way, sending quit and waiting around here will make the
1920 disconnect wait in vain and cause more problems than we need to. */
1922 if(!dead_connection && conn->bits.protoconnstart &&
1923 !Curl_pp_needs_flush(data, &smtpc->pp) &&
1924 !smtp_perform_quit(data, smtpc))
1925 (void)smtp_block_statemach(data, smtpc, TRUE); /* ignore on QUIT */
1927 CURL_TRC_SMTP(data, "smtp_disconnect(), finished");
1928 return CURLE_OK;
1931/* Called from multi.c while DOing */
1932static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
1934 struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1935 CURLcode result;
1937 if(!smtp)
1938 return CURLE_FAILED_INIT;
1939 result = smtp_multi_statemach(data, dophase_done);
1940 if(result)
1941 DEBUGF(infof(data, "DO phase failed"));
1942 else if(*dophase_done) {
1943 result = smtp_dophase_done(data, smtp, FALSE /* not connected */);
1945 DEBUGF(infof(data, "DO phase is complete"));
1948 CURL_TRC_SMTP(data, "smtp_doing() -> %d, done=%d", (int)result,
1949 *dophase_done);
1950 return result;
1953static void smtp_easy_dtor(const void *key, size_t klen, void *entry)
1955 struct SMTP *smtp = entry;
1956 (void)key;
1957 (void)klen;
1958 curlx_free(smtp);
1961static void smtp_conn_dtor(const void *key, size_t klen, void *entry)
1963 struct smtp_conn *smtpc = entry;
1964 (void)key;
1965 (void)klen;
1966 Curl_pp_disconnect(&smtpc->pp);
1967 curlx_safefree(smtpc->domain);
1968 curlx_free(smtpc);
1971static CURLcode smtp_setup_connection(struct Curl_easy *data,
1972 struct connectdata *conn)
1974 struct smtp_conn *smtpc;
1975 struct SMTP *smtp;
1976 CURLcode result = CURLE_OK;
1978 smtpc = curlx_calloc(1, sizeof(*smtpc));
1979 if(!smtpc ||
1980 Curl_conn_meta_set(conn, CURL_META_SMTP_CONN, smtpc, smtp_conn_dtor)) {
1981 result = CURLE_OUT_OF_MEMORY;
1982 goto out;
1985 smtp = curlx_calloc(1, sizeof(*smtp));
1986 if(!smtp ||
1987 Curl_meta_set(data, CURL_META_SMTP_EASY, smtp, smtp_easy_dtor))
1988 result = CURLE_OUT_OF_MEMORY;
1990out:
1991 CURL_TRC_SMTP(data, "smtp_setup_connection() -> %d", (int)result);
1992 return result;
1996 * SMTP protocol handler.
1997 */
1998const struct Curl_protocol Curl_protocol_smtp = {
1999 smtp_setup_connection, /* setup_connection */
2000 ZERO_NULL, /* setup_filters */
2001 smtp_do, /* do_it */
2002 smtp_done, /* done */
2003 ZERO_NULL, /* do_more */
2004 smtp_connect, /* connect_it */
2005 smtp_multi_statemach, /* connecting */
2006 smtp_doing, /* doing */
2007 smtp_pollset, /* proto_pollset */
2008 smtp_pollset, /* doing_pollset */
2009 ZERO_NULL, /* domore_pollset */
2010 ZERO_NULL, /* perform_pollset */
2011 smtp_disconnect, /* disconnect */
2012 ZERO_NULL, /* write_resp */
2013 ZERO_NULL, /* write_resp_hd */
2014 ZERO_NULL, /* connection_is_dead */
2015 ZERO_NULL, /* attach connection */
2016 ZERO_NULL, /* follow */
2019#endif /* CURL_DISABLE_SMTP */
Diagnostics4 git calls · 40 ms
CallStatusSizeTime040 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20014 KiB · hit34 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=31
git.response.bytes=14698
git.response.cache=HIT
git.response.duration_ms=34
git.fetch.wants=a19f1a8e50528887ef61cb06ee70c7861180299a
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=14698
git.pkt.payload.max=14677
git.sideband.band1.bytes=14676
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e269c4ff59f4e0
Request
69378e47…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format