Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/smtp.c
Line
Count
Source
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"
44
45
#ifndef CURL_DISABLE_SMTP
46
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
60
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"
78
79
/* meta key for storing protocol meta at easy handle */
80
0
#define CURL_META_SMTP_EASY   "meta:proto:smtp:easy"
81
/* meta key for storing protocol meta at connection */
82
0
#define CURL_META_SMTP_CONN   "meta:proto:smtp:conn"
83
84
/****************************************************************************
85
 * SMTP unique setup
86
 ***************************************************************************/
87
typedef 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;
105
106
/* smtp_conn is used for struct connection-oriented data in the connectdata
107
   struct */
108
struct 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
};
121
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. */
126
struct SMTP {
127
  curl_pp_transfer transfer;
128
  char *custom;            /* Custom Request */
129
  size_t rcpt_idx;         /* index of data->set.a.mail_rcpt to process */
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
};
137
138
/***********************************************************************
139
 *
140
 * smtp_parse_url_options()
141
 *
142
 * Parse the URL login options.
143
 */
144
static CURLcode smtp_parse_url_options(struct connectdata *conn,
145
                                       struct smtp_conn *smtpc)
146
0
{
147
0
  CURLcode result = CURLE_OK;
148
0
  const char *ptr = conn->options;
149
150
0
  while(!result && ptr && *ptr) {
151
0
    const char *key = ptr;
152
0
    const char *value;
153
154
0
    while(*ptr && *ptr != '=')
155
0
      ptr++;
156
157
0
    value = ptr + 1;
158
159
0
    while(*ptr && *ptr != ';')
160
0
      ptr++;
161
162
0
    if(curl_strnequal(key, "AUTH=", 5))
163
0
      result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
164
0
                                               value, ptr - value);
165
0
    else
166
0
      result = CURLE_URL_MALFORMAT;
167
168
0
    if(*ptr == ';')
169
0
      ptr++;
170
0
  }
171
172
0
  return result;
173
0
}
174
175
/***********************************************************************
176
 *
177
 * smtp_parse_url_path()
178
 *
179
 * Parse the URL path into separate path components.
180
 */
181
static CURLcode smtp_parse_url_path(struct Curl_easy *data,
182
                                    struct smtp_conn *smtpc)
183
0
{
184
  /* The SMTP struct is already initialized in smtp_connect() */
185
0
  const char *path = &data->state.up.path[1]; /* skip leading path */
186
0
  char localhost[HOSTNAME_MAX + 1];
187
188
  /* Calculate the path if necessary */
189
0
  if(!*path) {
190
0
    if(!Curl_gethostname(localhost, sizeof(localhost)))
191
0
      path = localhost;
192
0
    else
193
0
      path = "localhost";
194
0
  }
195
196
  /* URL decode the path and use it as the domain in our EHLO */
197
0
  return Curl_urldecode(path, 0, &smtpc->domain, NULL, REJECT_CTRL);
198
0
}
199
200
/***********************************************************************
201
 *
202
 * smtp_parse_custom_request()
203
 *
204
 * Parse the custom request.
205
 */
206
static CURLcode smtp_parse_custom_request(struct Curl_easy *data,
207
                                          struct SMTP *smtp)
208
0
{
209
0
  CURLcode result = CURLE_OK;
210
0
  const char *custom = CURL_EASY_STR(data, STRING_CUSTOMREQUEST);
211
212
  /* URL decode the custom request */
213
0
  if(custom)
214
0
    result = Curl_urldecode(custom, 0, &smtp->custom, NULL, REJECT_CTRL);
215
216
0
  return result;
217
0
}
218
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
 */
252
static CURLcode smtp_parse_address(struct Curl_easy *data, const char *fqma,
253
                                   char **address, struct hostname *host,
254
                                   const char **suffix)
255
0
{
256
0
  CURLcode result = CURLE_OK;
257
0
  size_t length;
258
0
  char *addressend;
259
0
  char *dup;
260
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
0
  if(strpbrk(fqma, "\r\n")) {
265
0
    failf(data, "Refusing to send email address with a CR or LF");
266
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
267
0
  }
268
269
  /* Duplicate the fully qualified email address so we can manipulate it,
270
     ensuring it does not contain the delimiters if specified */
271
0
  dup = curlx_strdup(fqma[0] == '<' ? fqma + 1 : fqma);
272
0
  if(!dup)
273
0
    return CURLE_OUT_OF_MEMORY;
274
275
0
  if(fqma[0] != '<') {
276
0
    length = strlen(dup);
277
0
    if(length && dup[length - 1] == '>')
278
0
      dup[length - 1] = '\0';
279
0
  }
280
0
  else {
281
0
    addressend = strrchr(dup, '>');
282
0
    if(addressend) {
283
0
      *addressend = '\0';
284
0
      *suffix = addressend + 1;
285
0
    }
286
0
  }
287
288
  /* Extract the hostname from the address (if we can) */
289
0
  host->name = strpbrk(dup, "@");
290
0
  if(host->name) {
291
0
    *host->name = '\0';
292
0
    host->name = host->name + 1;
293
294
    /* Attempt to convert the hostname to IDN ACE */
295
0
    (void)Curl_idnconvert_hostname(host);
296
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
0
  }
301
302
  /* Extract the local address from the mailbox */
303
0
  *address = dup;
304
305
0
  return result;
306
0
}
307
308
struct 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
};
318
319
static CURLcode cr_eob_init(struct Curl_easy *data,
320
                            struct Curl_creader *reader)
321
0
{
322
0
  struct cr_eob_ctx *ctx = reader->ctx;
323
0
  (void)data;
324
  /* The first char we read is the first on a line, as if we had
325
   * read CRLF before */
326
0
  ctx->n_eob = 2;
327
0
  Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
328
0
  return CURLE_OK;
329
0
}
330
331
static void cr_eob_close(struct Curl_easy *data, struct Curl_creader *reader)
332
0
{
333
0
  struct cr_eob_ctx *ctx = reader->ctx;
334
0
  (void)data;
335
0
  Curl_bufq_free(&ctx->buf);
336
0
}
337
338
/* this is the 5-bytes End-Of-Body marker for SMTP */
339
0
#define SMTP_EOB          "\r\n.\r\n"
340
0
#define SMTP_EOB_FIND_LEN 3
341
342
/* client reader doing SMTP End-Of-Body escaping. */
343
static 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)
347
0
{
348
0
  struct cr_eob_ctx *ctx = reader->ctx;
349
0
  CURLcode result = CURLE_OK;
350
0
  size_t nread, i, start, n;
351
0
  bool eos;
352
353
0
  if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
354
    /* Get more and convert it when needed */
355
0
    result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
356
0
    CURL_TRC_SMTP(data, "cr_eob_read, next_read(len=%zu) -> %d, %zu eos=%d",
357
0
                  blen, (int)result, nread, eos);
358
0
    if(result)
359
0
      return result;
360
361
0
    ctx->read_eos = eos;
362
0
    if(nread) {
363
0
      if(!ctx->n_eob && !memchr(buf, SMTP_EOB[0], nread)) {
364
        /* not in the middle of a match, no EOB start found, pass */
365
0
        *pnread = nread;
366
0
        *peos = FALSE;
367
0
        return CURLE_OK;
368
0
      }
369
      /* scan for EOB (continuation) and convert */
370
0
      for(i = start = 0; i < nread; ++i) {
371
0
        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
0
          if(data->state.infilesize == CURL_OFF_T_MAX)
375
0
            return CURLE_TOO_LARGE;
376
0
          result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
377
0
          if(result)
378
0
            return result;
379
0
          result = Curl_bufq_cwrite(&ctx->buf, ".", 1, &n);
380
0
          if(result)
381
0
            return result;
382
0
          ctx->n_eob = 0;
383
0
          start = i;
384
0
          if(data->state.infilesize > 0)
385
0
            data->state.infilesize++;
386
0
        }
387
388
0
        if(buf[i] != SMTP_EOB[ctx->n_eob])
389
0
          ctx->n_eob = 0;
390
391
0
        if(buf[i] == SMTP_EOB[ctx->n_eob]) {
392
          /* matching another char of the EOB */
393
0
          ++ctx->n_eob;
394
0
        }
395
0
      }
396
397
      /* add any remainder to buf */
398
0
      if(start < nread) {
399
0
        result = Curl_bufq_cwrite(&ctx->buf, buf + start, nread - start, &n);
400
0
        if(result)
401
0
          return result;
402
0
      }
403
0
    }
404
0
  }
405
406
0
  *peos = FALSE;
407
408
0
  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
0
    const char *eob = SMTP_EOB;
413
0
    CURL_TRC_SMTP(data, "auto-ending mail body with '\\r\\n.\\r\\n'");
414
0
    switch(ctx->n_eob) {
415
0
    case 2:
416
      /* seen a CRLF at the end, add the remainder */
417
0
      eob = &SMTP_EOB[2];
418
0
      break;
419
0
    case 3:
420
      /* ended with '\r\n.', we should escape the last '.' */
421
0
      eob = "." SMTP_EOB;
422
0
      break;
423
0
    default:
424
0
      break;
425
0
    }
426
0
    result = Curl_bufq_cwrite(&ctx->buf, eob, strlen(eob), &n);
427
0
    if(result)
428
0
      return result;
429
0
    ctx->processed_eos = TRUE;
430
0
  }
431
432
0
  if(!Curl_bufq_is_empty(&ctx->buf)) {
433
0
    result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
434
0
  }
435
0
  else
436
0
    *pnread = 0;
437
438
0
  if(ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
439
    /* no more data, read all, done. */
440
0
    CURL_TRC_SMTP(data, "mail body complete, returning EOS");
441
0
    ctx->eos = TRUE;
442
0
  }
443
0
  *peos = (bool)ctx->eos;
444
0
  DEBUGF(infof(data, "cr_eob_read(%zu) -> %d, %zu, %d",
445
0
               blen, (int)result, *pnread, *peos));
446
0
  return result;
447
0
}
448
449
static curl_off_t cr_eob_total_length(struct Curl_easy *data,
450
                                      struct Curl_creader *reader)
451
0
{
452
  /* this reader changes length depending on input */
453
0
  (void)data;
454
0
  (void)reader;
455
0
  return -1;
456
0
}
457
458
static 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
};
471
472
static CURLcode cr_eob_add(struct Curl_easy *data)
473
0
{
474
0
  struct Curl_creader *reader = NULL;
475
0
  CURLcode result;
476
477
0
  result = Curl_creader_create(&reader, data, &cr_eob, CURL_CR_CONTENT_ENCODE);
478
0
  if(!result)
479
0
    result = Curl_creader_add(data, reader);
480
481
0
  if(result && reader)
482
0
    Curl_creader_free(data, reader);
483
0
  return result;
484
0
}
485
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
 */
494
static bool smtp_endofresp(struct Curl_easy *data, struct connectdata *conn,
495
                           const char *line, size_t len, int *resp)
496
0
{
497
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
498
0
  bool end = FALSE;
499
0
  (void)data;
500
501
0
  DEBUGASSERT(smtpc);
502
0
  if(!smtpc)
503
0
    return FALSE;
504
505
  /* Nothing for us */
506
0
  if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2]))
507
0
    return FALSE;
508
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
0
  if(line[3] == ' ' || len == 5) {
514
0
    char tmpline[6];
515
0
    curl_off_t code;
516
0
    const char *p = tmpline;
517
0
    end = TRUE;
518
0
    memcpy(tmpline, line, (len == 5 ? 5 : 3));
519
0
    tmpline[len == 5 ? 5 : 3] = 0;
520
0
    if(curlx_str_number(&p, &code, len == 5 ? 99999 : 999))
521
0
      return FALSE;
522
0
    *resp = (int)code;
523
524
    /* Make sure real server never sends internal value */
525
0
    if(*resp == 1)
526
0
      *resp = 0;
527
0
  }
528
  /* Do we have a multiline (continuation) response? */
529
0
  else if(line[3] == '-' &&
530
0
          (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) {
531
0
    end = TRUE;
532
0
    *resp = 1;  /* Internal response code */
533
0
  }
534
535
0
  return end;
536
0
}
537
538
/***********************************************************************
539
 *
540
 * smtp_get_message()
541
 *
542
 * Gets the authentication message from the response buffer.
543
 */
544
static CURLcode smtp_get_message(struct Curl_easy *data, struct bufref *out)
545
0
{
546
0
  struct smtp_conn *smtpc =
547
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
548
0
  char *message;
549
0
  size_t len;
550
551
0
  if(!smtpc)
552
0
    return CURLE_FAILED_INIT;
553
554
0
  message = curlx_dyn_ptr(&smtpc->pp.recvbuf);
555
0
  len = smtpc->pp.nfinal;
556
0
  if(len > 4) {
557
    /* Find the start of the message */
558
0
    len -= 4;
559
0
    for(message += 4; ISBLANK(*message); message++, len--)
560
0
      ;
561
562
    /* Find the end of the message */
563
0
    while(len--)
564
0
      if(!ISNEWLINE(message[len]) && !ISBLANK(message[len]))
565
0
        break;
566
567
    /* Terminate the message */
568
0
    message[++len] = '\0';
569
0
    Curl_bufref_set(out, message, len, NULL);
570
0
  }
571
0
  else
572
    /* junk input => zero length output */
573
0
    Curl_bufref_set(out, "", 0, NULL);
574
575
0
  return CURLE_OK;
576
0
}
577
578
/***********************************************************************
579
 *
580
 * smtp_state()
581
 *
582
 * This is the ONLY way to change SMTP state!
583
 */
584
static void smtp_state(struct Curl_easy *data,
585
                       struct smtp_conn *smtpc,
586
                       smtpstate newstate)
587
0
{
588
0
#ifdef CURLVERBOSE
589
  /* for debug purposes */
590
0
  static const char * const names[] = {
591
0
    "STOP",
592
0
    "SERVERGREET",
593
0
    "EHLO",
594
0
    "HELO",
595
0
    "STARTTLS",
596
0
    "UPGRADETLS",
597
0
    "AUTH",
598
0
    "COMMAND",
599
0
    "MAIL",
600
0
    "RCPT",
601
0
    "DATA",
602
0
    "POSTDATA",
603
0
    "QUIT",
604
    /* LAST */
605
0
  };
606
607
0
  if(smtpc->state != newstate)
608
0
    CURL_TRC_SMTP(data, "state change from %s to %s",
609
0
                  names[smtpc->state], names[newstate]);
610
#else
611
  (void)data;
612
#endif
613
614
0
  smtpc->state = newstate;
615
0
}
616
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
 */
624
static CURLcode smtp_perform_ehlo(struct Curl_easy *data,
625
                                  struct smtp_conn *smtpc)
626
0
{
627
0
  CURLcode result = CURLE_OK;
628
629
0
  smtpc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanism yet */
630
0
  smtpc->sasl.authused = SASL_AUTH_NONE;  /* Clear the authentication mechanism
631
                                             used for esmtp connections */
632
0
  smtpc->tls_supported = FALSE;           /* Clear the TLS capability */
633
0
  smtpc->auth_supported = FALSE;          /* Clear the AUTH capability */
634
635
  /* Send the EHLO command */
636
0
  result = Curl_pp_sendf(data, &smtpc->pp, "EHLO %s", smtpc->domain);
637
638
0
  if(!result)
639
0
    smtp_state(data, smtpc, SMTP_EHLO);
640
641
0
  return result;
642
0
}
643
644
/***********************************************************************
645
 *
646
 * smtp_perform_helo()
647
 *
648
 * Sends the HELO command to initialize communication with the SMTP server.
649
 */
650
static CURLcode smtp_perform_helo(struct Curl_easy *data,
651
                                  struct smtp_conn *smtpc)
652
0
{
653
0
  CURLcode result = CURLE_OK;
654
655
0
  smtpc->sasl.authused = SASL_AUTH_NONE; /* No authentication mechanism used
656
                                            in smtp connections */
657
658
  /* Send the HELO command */
659
0
  result = Curl_pp_sendf(data, &smtpc->pp, "HELO %s", smtpc->domain);
660
661
0
  if(!result)
662
0
    smtp_state(data, smtpc, SMTP_HELO);
663
664
0
  return result;
665
0
}
666
667
/***********************************************************************
668
 *
669
 * smtp_perform_starttls()
670
 *
671
 * Sends the STLS command to start the upgrade to TLS.
672
 */
673
static CURLcode smtp_perform_starttls(struct Curl_easy *data,
674
                                      struct smtp_conn *smtpc)
675
0
{
676
  /* Send the STARTTLS command */
677
0
  CURLcode result = Curl_pp_sendf(data, &smtpc->pp, "%s", "STARTTLS");
678
679
0
  if(!result)
680
0
    smtp_state(data, smtpc, SMTP_STARTTLS);
681
682
0
  return result;
683
0
}
684
685
/***********************************************************************
686
 *
687
 * smtp_perform_upgrade_tls()
688
 *
689
 * Performs the upgrade to TLS.
690
 */
691
static CURLcode smtp_perform_upgrade_tls(struct Curl_easy *data,
692
                                         struct smtp_conn *smtpc)
693
0
{
694
0
#ifdef USE_SSL
695
  /* Start the SSL connection */
696
0
  struct connectdata *conn = data->conn;
697
0
  CURLcode result;
698
0
  bool ssldone = FALSE;
699
700
0
  DEBUGASSERT(smtpc->state == SMTP_UPGRADETLS);
701
0
  if(!Curl_conn_is_ssl(conn)) {
702
0
    result = Curl_ssl_cfilter_add(
703
0
      data, Curl_conn_get_origin(conn), &conn->chain);
704
0
    if(result)
705
0
      goto out;
706
    /* Change the connection handler and SMTP state */
707
0
    conn->scheme = &Curl_scheme_smtps;
708
0
  }
709
710
0
  DEBUGASSERT(!smtpc->ssldone);
711
0
  result = Curl_conn_connect(data, FALSE, &ssldone);
712
0
  DEBUGF(infof(data, "smtp_perform_upgrade_tls, connect -> %d, %d",
713
0
               (int)result, ssldone));
714
0
  if(!result && ssldone) {
715
0
    smtpc->ssldone = ssldone;
716
    /* perform EHLO now, changes smtp->state out of SMTP_UPGRADETLS */
717
0
    result = smtp_perform_ehlo(data, smtpc);
718
0
  }
719
0
out:
720
0
  return result;
721
#else
722
  (void)data;
723
  (void)smtpc;
724
  return CURLE_NOT_BUILT_IN;
725
#endif
726
0
}
727
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
 */
735
static CURLcode smtp_perform_auth(struct Curl_easy *data,
736
                                  const char *mech,
737
                                  const struct bufref *initresp)
738
0
{
739
0
  CURLcode result = CURLE_OK;
740
0
  struct smtp_conn *smtpc =
741
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
742
0
  const char *ir = Curl_bufref_ptr(initresp);
743
744
0
  if(!smtpc)
745
0
    return CURLE_FAILED_INIT;
746
747
0
  if(ir) {                                  /* AUTH <mech> ...<crlf> */
748
    /* Send the AUTH command with the initial response */
749
0
    result = Curl_pp_sendf(data, &smtpc->pp, "AUTH %s %s",
750
0
                           mech, *ir ? ir : "=");
751
0
  }
752
0
  else {
753
    /* Send the AUTH command */
754
0
    result = Curl_pp_sendf(data, &smtpc->pp, "AUTH %s", mech);
755
0
  }
756
757
0
  return result;
758
0
}
759
760
/***********************************************************************
761
 *
762
 * smtp_continue_auth()
763
 *
764
 * Sends SASL continuation data.
765
 */
766
static CURLcode smtp_continue_auth(struct Curl_easy *data,
767
                                   const char *mech,
768
                                   const struct bufref *resp)
769
0
{
770
0
  struct smtp_conn *smtpc =
771
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
772
773
0
  (void)mech;
774
0
  if(!smtpc)
775
0
    return CURLE_FAILED_INIT;
776
0
  return Curl_pp_sendf(data, &smtpc->pp, "%s", Curl_bufref_ptr(resp));
777
0
}
778
779
/***********************************************************************
780
 *
781
 * smtp_cancel_auth()
782
 *
783
 * Sends SASL cancellation.
784
 */
785
static CURLcode smtp_cancel_auth(struct Curl_easy *data, const char *mech)
786
0
{
787
0
  struct smtp_conn *smtpc =
788
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
789
790
0
  (void)mech;
791
0
  if(!smtpc)
792
0
    return CURLE_FAILED_INIT;
793
0
  return Curl_pp_sendf(data, &smtpc->pp, "*");
794
0
}
795
796
/***********************************************************************
797
 *
798
 * smtp_perform_authentication()
799
 *
800
 * Initiates the authentication sequence, with the appropriate SASL
801
 * authentication mechanism.
802
 */
803
static CURLcode smtp_perform_authentication(struct Curl_easy *data,
804
                                            struct smtp_conn *smtpc)
805
0
{
806
0
  CURLcode result = CURLE_OK;
807
0
  saslprogress progress;
808
809
  /* Check we have enough data to authenticate with, and the
810
     server supports authentication, and end the connect phase if not */
811
0
  if(!smtpc->auth_supported ||
812
0
     !Curl_sasl_can_authenticate(&smtpc->sasl, data)) {
813
0
    smtp_state(data, smtpc, SMTP_STOP);
814
0
    return result;
815
0
  }
816
817
  /* Calculate the SASL login details */
818
0
  result = Curl_sasl_start(&smtpc->sasl, data, FALSE, &progress);
819
820
0
  if(!result) {
821
0
    if(progress == SASL_INPROGRESS)
822
0
      smtp_state(data, smtpc, SMTP_AUTH);
823
0
    else
824
0
      result = Curl_sasl_is_blocked(&smtpc->sasl, data);
825
0
  }
826
827
0
  return result;
828
0
}
829
830
/***********************************************************************
831
 *
832
 * smtp_perform_command()
833
 *
834
 * Sends an SMTP based command.
835
 */
836
static CURLcode smtp_perform_command(struct Curl_easy *data,
837
                                     struct smtp_conn *smtpc,
838
                                     struct SMTP *smtp)
839
0
{
840
0
  const char *rcpt =
841
0
    Curl_frzn_strs_get_str(data->set.a.mail_rcpt, smtp->rcpt_idx);
842
0
  CURLcode result = CURLE_OK;
843
844
0
  if(rcpt) {
845
    /* We notify the server we are sending UTF-8 data if a) it supports the
846
       SMTPUTF8 extension and b) The mailbox contains UTF-8 characters, in
847
       either the local address or hostname parts. This is regardless of
848
       whether the hostname is encoded using IDN ACE */
849
0
    bool utf8 = FALSE;
850
851
0
    if(!smtp->custom || !smtp->custom[0]) {
852
0
      char *address = NULL;
853
0
      struct hostname host = { NULL, NULL, NULL, NULL };
854
0
      const char *suffix = "";
855
856
      /* Parse the mailbox to verify into the local address and hostname
857
         parts, converting the hostname to an IDN A-label if necessary */
858
0
      result = smtp_parse_address(data, rcpt, &address, &host, &suffix);
859
0
      if(result)
860
0
        return result;
861
862
      /* Establish whether we should report SMTPUTF8 to the server for this
863
         mailbox as per RFC-6531 sect. 3.1 point 6 */
864
0
      utf8 = smtpc->utf8_supported &&
865
0
             (host.encalloc ||
866
0
              !Curl_is_ASCII_name(address) ||
867
0
              !Curl_is_ASCII_name(host.name));
868
869
      /* Send the VRFY command (Note: The hostname part may be absent when the
870
         host is a local system) */
871
0
      result = Curl_pp_sendf(data, &smtpc->pp, "VRFY %s%s%s%s",
872
0
                             address,
873
0
                             host.name ? "@" : "",
874
0
                             host.name ? host.name : "",
875
0
                             utf8 ? " SMTPUTF8" : "");
876
877
0
      Curl_free_idnconverted_hostname(&host);
878
0
      curlx_free(address);
879
0
    }
880
0
    else {
881
      /* Establish whether we should report that we support SMTPUTF8 for EXPN
882
         commands to the server as per RFC-6531 sect. 3.1 point 6 */
883
0
      utf8 = smtpc->utf8_supported && !strcmp(smtp->custom, "EXPN");
884
885
      /* Send the custom recipient based command such as the EXPN command */
886
0
      result = Curl_pp_sendf(data, &smtpc->pp,
887
0
                             "%s %s%s", smtp->custom, rcpt,
888
0
                             utf8 ? " SMTPUTF8" : "");
889
0
    }
890
0
  }
891
0
  else
892
    /* Send the non-recipient based command such as HELP */
893
0
    result = Curl_pp_sendf(data, &smtpc->pp, "%s",
894
0
                           smtp->custom && smtp->custom[0] != '\0' ?
895
0
                           smtp->custom : "HELP");
896
897
0
  if(!result)
898
0
    smtp_state(data, smtpc, SMTP_COMMAND);
899
900
0
  return result;
901
0
}
902
903
/***********************************************************************
904
 *
905
 * smtp_perform_mail()
906
 *
907
 * Sends an MAIL command to initiate the upload of a message.
908
 */
909
static CURLcode smtp_perform_mail(struct Curl_easy *data,
910
                                  struct smtp_conn *smtpc)
911
0
{
912
0
  char *from = NULL;
913
0
  char *auth = NULL;
914
0
  char *size = NULL;
915
0
  const char *str;
916
0
  CURLcode result = CURLE_OK;
917
918
  /* We notify the server we are sending UTF-8 data if a) it supports the
919
     SMTPUTF8 extension and b) The mailbox contains UTF-8 characters, in
920
     either the local address or hostname parts. This is regardless of
921
     whether the hostname is encoded using IDN ACE */
922
0
  bool utf8 = FALSE;
923
924
  /* Calculate the FROM parameter */
925
0
  str = CURL_EASY_STR(data, STRING_MAIL_FROM);
926
0
  if(str) {
927
0
    char *address = NULL;
928
0
    struct hostname host = { NULL, NULL, NULL, NULL };
929
0
    const char *suffix = "";
930
931
    /* Parse the FROM mailbox into the local address and hostname parts,
932
       converting the hostname to an IDN A-label if necessary */
933
0
    result = smtp_parse_address(data, str, &address, &host, &suffix);
934
0
    if(result)
935
0
      goto out;
936
937
    /* Establish whether we should report SMTPUTF8 to the server for this
938
       mailbox as per RFC-6531 sect. 3.1 point 4 and sect. 3.4 */
939
0
    utf8 = smtpc->utf8_supported &&
940
0
           (host.encalloc ||
941
0
            !Curl_is_ASCII_name(address) ||
942
0
            !Curl_is_ASCII_name(host.name));
943
944
0
    if(host.name) {
945
0
      from = curl_maprintf("<%s@%s>%s", address, host.name, suffix);
946
947
0
      Curl_free_idnconverted_hostname(&host);
948
0
    }
949
0
    else
950
      /* An invalid mailbox was provided but we let the server worry
951
         about that and reply with a 501 error */
952
0
      from = curl_maprintf("<%s>%s", address, suffix);
953
954
0
    curlx_free(address);
955
0
  }
956
0
  else
957
    /* Null reverse-path, RFC-5321, sect. 3.6.3 */
958
0
    from = curlx_strdup("<>");
959
960
0
  if(!from) {
961
0
    result = CURLE_OUT_OF_MEMORY;
962
0
    goto out;
963
0
  }
964
965
  /* Calculate the optional AUTH parameter */
966
0
  str = CURL_EASY_STR(data, STRING_MAIL_AUTH);
967
0
  if(str && smtpc->sasl.authused) {
968
0
    if(str[0] != '\0') {
969
0
      char *address = NULL;
970
0
      struct hostname host = { NULL, NULL, NULL, NULL };
971
0
      const char *suffix = "";
972
973
      /* Parse the AUTH mailbox into the local address and hostname parts,
974
         converting the hostname to an IDN A-label if necessary */
975
0
      result = smtp_parse_address(data, str, &address, &host, &suffix);
976
0
      if(result)
977
0
        goto out;
978
979
      /* Establish whether we should report SMTPUTF8 to the server for this
980
         mailbox as per RFC-6531 sect. 3.1 point 4 and sect. 3.4 */
981
0
      if(!utf8 && smtpc->utf8_supported &&
982
0
         (host.encalloc ||
983
0
          !Curl_is_ASCII_name(address) ||
984
0
          !Curl_is_ASCII_name(host.name)))
985
0
        utf8 = TRUE;
986
987
0
      if(host.name) {
988
0
        auth = curl_maprintf("<%s@%s>%s", address, host.name, suffix);
989
990
0
        Curl_free_idnconverted_hostname(&host);
991
0
      }
992
0
      else
993
        /* An invalid mailbox was provided but we let the server worry
994
           about it */
995
0
        auth = curl_maprintf("<%s>%s", address, suffix);
996
0
      curlx_free(address);
997
0
    }
998
0
    else
999
      /* Empty AUTH, RFC-2554, sect. 5 */
1000
0
      auth = curlx_strdup("<>");
1001
1002
0
    if(!auth) {
1003
0
      result = CURLE_OUT_OF_MEMORY;
1004
0
      goto out;
1005
0
    }
1006
0
  }
1007
1008
0
#ifndef CURL_DISABLE_MIME
1009
  /* Prepare the mime data if some. */
1010
0
  if(IS_MIME_POST(data)) {
1011
0
    curl_mimepart *postp = data->set.a.mimepostp;
1012
1013
    /* Use the whole structure as data. */
1014
0
    postp->flags &= ~(unsigned int)MIME_BODY_ONLY;
1015
1016
    /* Add external headers and mime version. */
1017
0
    curl_mime_headers(postp, data->set.headers, 0);
1018
0
    result = Curl_mime_prepare_headers(data, postp, NULL,
1019
0
                                       NULL, MIMESTRATEGY_MAIL);
1020
1021
0
    if(!result && !Curl_checkheaders(data, STRCONST("Mime-Version")))
1022
0
      result = Curl_mime_add_header(&postp->curlheaders, "Mime-Version: 1.0");
1023
0
    if(!result)
1024
0
      result = Curl_creader_set_mime(data, postp);
1025
0
    if(result)
1026
0
      goto out;
1027
0
    data->state.infilesize = Curl_creader_total_length(data);
1028
0
  }
1029
0
  else
1030
0
#endif
1031
0
  {
1032
0
    result = Curl_creader_set_fread(data, data->state.infilesize);
1033
0
    if(result)
1034
0
      goto out;
1035
0
  }
1036
1037
  /* Calculate the optional SIZE parameter */
1038
0
  if(smtpc->size_supported && data->state.infilesize > 0) {
1039
0
    size = curl_maprintf("%" FMT_OFF_T, data->state.infilesize);
1040
1041
0
    if(!size) {
1042
0
      result = CURLE_OUT_OF_MEMORY;
1043
0
      goto out;
1044
0
    }
1045
0
  }
1046
1047
  /* If the mailboxes in the FROM and AUTH parameters do not include a UTF-8
1048
     based address then quickly scan through the recipient list and check if
1049
     any there do, as we need to correctly identify our support for SMTPUTF8
1050
     in the envelope, as per RFC-6531 sect. 3.4 */
1051
0
  if(smtpc->utf8_supported && !utf8) {
1052
0
    size_t i;
1053
0
    for(i = 0; i < Curl_frzn_strs_count(data->set.a.mail_rcpt); ++i) {
1054
0
      const char *rcpt = Curl_frzn_strs_get_str(data->set.a.mail_rcpt, i);
1055
      /* Does the hostname contain non-ASCII characters? */
1056
0
      if(rcpt && !Curl_is_ASCII_name(rcpt)) {
1057
0
        utf8 = TRUE;
1058
0
        break;
1059
0
      }
1060
0
    }
1061
0
  }
1062
1063
  /* Add the client reader doing STMP EOB escaping */
1064
0
  result = cr_eob_add(data);
1065
0
  if(result)
1066
0
    goto out;
1067
1068
  /* Send the MAIL command */
1069
0
  result = Curl_pp_sendf(data, &smtpc->pp,
1070
0
                         "MAIL FROM:%s%s%s%s%s%s",
1071
0
                         from,                 /* Mandatory                 */
1072
0
                         auth ? " AUTH=" : "", /* Optional on AUTH support  */
1073
0
                         auth ? auth : "",
1074
0
                         size ? " SIZE=" : "", /* Optional on SIZE support  */
1075
0
                         size ? size : "",
1076
0
                         utf8 ? " SMTPUTF8"    /* Internationalised mailbox */
1077
0
                               : "");          /* included in our envelope  */
1078
1079
0
out:
1080
0
  curlx_free(from);
1081
0
  curlx_free(auth);
1082
0
  curlx_free(size);
1083
1084
0
  if(!result)
1085
0
    smtp_state(data, smtpc, SMTP_MAIL);
1086
1087
0
  return result;
1088
0
}
1089
1090
/***********************************************************************
1091
 *
1092
 * smtp_perform_rcpt_to()
1093
 *
1094
 * Sends a RCPT TO command for a given recipient as part of the message upload
1095
 * process.
1096
 */
1097
static CURLcode smtp_perform_rcpt_to(struct Curl_easy *data,
1098
                                     struct smtp_conn *smtpc,
1099
                                     struct SMTP *smtp)
1100
0
{
1101
0
  const char *rcpt =
1102
0
    Curl_frzn_strs_get_str(data->set.a.mail_rcpt, smtp->rcpt_idx);
1103
0
  CURLcode result = CURLE_OK;
1104
0
  char *address = NULL;
1105
0
  struct hostname host = { NULL, NULL, NULL, NULL };
1106
0
  const char *suffix = "";
1107
1108
0
  if(!rcpt)
1109
0
    return CURLE_FAILED_INIT;
1110
  /* Parse the recipient mailbox into the local address and hostname parts,
1111
     converting the hostname to an IDN A-label if necessary */
1112
0
  result = smtp_parse_address(data, rcpt, &address, &host, &suffix);
1113
0
  if(result)
1114
0
    return result;
1115
1116
  /* Send the RCPT TO command */
1117
0
  if(host.name)
1118
0
    result = Curl_pp_sendf(data, &smtpc->pp, "RCPT TO:<%s@%s>%s",
1119
0
                           address, host.name, suffix);
1120
0
  else
1121
    /* An invalid mailbox was provided but we let the server worry about
1122
       that and reply with a 501 error */
1123
0
    result = Curl_pp_sendf(data, &smtpc->pp, "RCPT TO:<%s>%s",
1124
0
                           address, suffix);
1125
1126
0
  Curl_free_idnconverted_hostname(&host);
1127
0
  curlx_free(address);
1128
1129
0
  if(!result)
1130
0
    smtp_state(data, smtpc, SMTP_RCPT);
1131
1132
0
  return result;
1133
0
}
1134
1135
/***********************************************************************
1136
 *
1137
 * smtp_perform_quit()
1138
 *
1139
 * Performs the quit action prior to sclose() being called.
1140
 */
1141
static CURLcode smtp_perform_quit(struct Curl_easy *data,
1142
                                  struct smtp_conn *smtpc)
1143
0
{
1144
  /* Send the QUIT command */
1145
0
  CURLcode result = Curl_pp_sendf(data, &smtpc->pp, "%s", "QUIT");
1146
1147
0
  if(!result)
1148
0
    smtp_state(data, smtpc, SMTP_QUIT);
1149
1150
0
  return result;
1151
0
}
1152
1153
/* For the initial server greeting */
1154
static CURLcode smtp_state_servergreet_resp(struct Curl_easy *data,
1155
                                            struct smtp_conn *smtpc,
1156
                                            int smtpcode,
1157
                                            smtpstate instate)
1158
0
{
1159
0
  CURLcode result = CURLE_OK;
1160
0
  (void)instate;
1161
1162
0
  if(smtpcode / 100 != 2) {
1163
0
    failf(data, "Got unexpected smtp-server response: %d", smtpcode);
1164
0
    result = CURLE_WEIRD_SERVER_REPLY;
1165
0
  }
1166
0
  else
1167
0
    result = smtp_perform_ehlo(data, smtpc);
1168
1169
0
  return result;
1170
0
}
1171
1172
/* For STARTTLS responses */
1173
static CURLcode smtp_state_starttls_resp(struct Curl_easy *data,
1174
                                         struct smtp_conn *smtpc,
1175
                                         int smtpcode,
1176
                                         smtpstate instate)
1177
0
{
1178
0
  CURLcode result = CURLE_OK;
1179
0
  (void)instate;
1180
1181
  /* Pipelining in response is forbidden. */
1182
0
  if(smtpc->pp.overflow)
1183
0
    return CURLE_WEIRD_SERVER_REPLY;
1184
1185
0
  if(smtpcode != 220) {
1186
0
    if(data->set.use_ssl != CURLUSESSL_TRY) {
1187
0
      failf(data, "STARTTLS denied, code %d", smtpcode);
1188
0
      result = CURLE_USE_SSL_FAILED;
1189
0
    }
1190
0
    else
1191
0
      result = smtp_perform_authentication(data, smtpc);
1192
0
  }
1193
0
  else
1194
0
    smtp_state(data, smtpc, SMTP_UPGRADETLS);
1195
1196
0
  return result;
1197
0
}
1198
1199
/* For EHLO responses */
1200
static CURLcode smtp_state_ehlo_resp(struct Curl_easy *data,
1201
                                     struct smtp_conn *smtpc,
1202
                                     int smtpcode,
1203
                                     smtpstate instate)
1204
0
{
1205
0
  CURLcode result = CURLE_OK;
1206
0
  const char *line = curlx_dyn_ptr(&smtpc->pp.recvbuf);
1207
0
  size_t len = smtpc->pp.nfinal;
1208
1209
0
  (void)instate;
1210
1211
0
  if(smtpcode / 100 != 2 && smtpcode != 1) {
1212
0
    if(data->set.use_ssl <= CURLUSESSL_TRY ||
1213
0
       Curl_conn_is_ssl(data->conn))
1214
0
      result = smtp_perform_helo(data, smtpc);
1215
0
    else {
1216
0
      failf(data, "Remote access denied: %d", smtpcode);
1217
0
      result = CURLE_REMOTE_ACCESS_DENIED;
1218
0
    }
1219
0
  }
1220
0
  else if(len >= 4) {
1221
0
    line += 4;
1222
0
    len -= 4;
1223
1224
    /* Does the server support the STARTTLS capability? */
1225
0
    if(len >= 8 && curl_strnequal(line, "STARTTLS", 8))
1226
0
      smtpc->tls_supported = TRUE;
1227
1228
    /* Does the server support the SIZE capability? */
1229
0
    else if(len >= 4 && curl_strnequal(line, "SIZE", 4))
1230
0
      smtpc->size_supported = TRUE;
1231
1232
    /* Does the server support the UTF-8 capability? */
1233
0
    else if(len >= 8 && curl_strnequal(line, "SMTPUTF8", 8))
1234
0
      smtpc->utf8_supported = TRUE;
1235
1236
    /* Does the server support authentication? */
1237
0
    else if(len >= 5 && curl_strnequal(line, "AUTH ", 5)) {
1238
0
      smtpc->auth_supported = TRUE;
1239
1240
      /* Advance past the AUTH keyword */
1241
0
      line += 5;
1242
0
      len -= 5;
1243
1244
      /* Loop through the data line */
1245
0
      for(;;) {
1246
0
        size_t llen;
1247
0
        size_t wordlen;
1248
0
        unsigned short mechbit;
1249
1250
0
        while(len && (ISBLANK(*line) || ISNEWLINE(*line))) {
1251
0
          line++;
1252
0
          len--;
1253
0
        }
1254
1255
0
        if(!len)
1256
0
          break;
1257
1258
        /* Extract the word */
1259
0
        for(wordlen = 0; wordlen < len && !ISBLANK(line[wordlen]) &&
1260
0
              !ISNEWLINE(line[wordlen]);)
1261
0
          wordlen++;
1262
1263
        /* Test the word for a matching authentication mechanism */
1264
0
        mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
1265
0
        if(mechbit && llen == wordlen)
1266
0
          smtpc->sasl.authmechs |= mechbit;
1267
1268
0
        line += wordlen;
1269
0
        len -= wordlen;
1270
0
      }
1271
0
    }
1272
1273
0
    if(smtpcode != 1) {
1274
0
      if(data->set.use_ssl && !Curl_conn_is_ssl(data->conn)) {
1275
        /* We do not have an SSL/TLS connection yet, but SSL is requested */
1276
0
        if(smtpc->tls_supported)
1277
          /* Switch to TLS connection now */
1278
0
          result = smtp_perform_starttls(data, smtpc);
1279
0
        else if(data->set.use_ssl == CURLUSESSL_TRY)
1280
          /* Fallback and carry on with authentication */
1281
0
          result = smtp_perform_authentication(data, smtpc);
1282
0
        else {
1283
0
          failf(data, "STARTTLS not supported.");
1284
0
          result = CURLE_USE_SSL_FAILED;
1285
0
        }
1286
0
      }
1287
0
      else
1288
0
        result = smtp_perform_authentication(data, smtpc);
1289
0
    }
1290
0
  }
1291
0
  else {
1292
0
    failf(data, "Unexpectedly short EHLO response");
1293
0
    result = CURLE_WEIRD_SERVER_REPLY;
1294
0
  }
1295
1296
0
  return result;
1297
0
}
1298
1299
/* For HELO responses */
1300
static CURLcode smtp_state_helo_resp(struct Curl_easy *data,
1301
                                     struct smtp_conn *smtpc,
1302
                                     int smtpcode,
1303
                                     smtpstate instate)
1304
0
{
1305
0
  CURLcode result = CURLE_OK;
1306
0
  (void)instate;
1307
1308
0
  if(smtpcode / 100 != 2) {
1309
0
    failf(data, "Remote access denied: %d", smtpcode);
1310
0
    result = CURLE_REMOTE_ACCESS_DENIED;
1311
0
  }
1312
0
  else
1313
    /* End of connect phase */
1314
0
    smtp_state(data, smtpc, SMTP_STOP);
1315
1316
0
  return result;
1317
0
}
1318
1319
/* For SASL authentication responses */
1320
static CURLcode smtp_state_auth_resp(struct Curl_easy *data,
1321
                                     struct smtp_conn *smtpc,
1322
                                     int smtpcode,
1323
                                     smtpstate instate)
1324
0
{
1325
0
  CURLcode result = CURLE_OK;
1326
0
  saslprogress progress;
1327
1328
0
  (void)instate;
1329
1330
0
  result = Curl_sasl_continue(&smtpc->sasl, data, smtpcode, &progress);
1331
0
  if(!result)
1332
0
    switch(progress) {
1333
0
    case SASL_DONE:
1334
0
      smtp_state(data, smtpc, SMTP_STOP);  /* Authenticated */
1335
0
      break;
1336
0
    case SASL_IDLE:            /* No mechanism left after cancellation */
1337
0
      failf(data, "Authentication cancelled");
1338
0
      result = CURLE_LOGIN_DENIED;
1339
0
      break;
1340
0
    default:
1341
0
      break;
1342
0
    }
1343
1344
0
  return result;
1345
0
}
1346
1347
/* For command responses */
1348
static CURLcode smtp_state_command_resp(struct Curl_easy *data,
1349
                                        struct smtp_conn *smtpc,
1350
                                        struct SMTP *smtp,
1351
                                        int smtpcode,
1352
                                        smtpstate instate)
1353
0
{
1354
0
  const char *rcpt =
1355
0
    Curl_frzn_strs_get_str(data->set.a.mail_rcpt, smtp->rcpt_idx);
1356
0
  CURLcode result = CURLE_OK;
1357
0
  const char *line = curlx_dyn_ptr(&smtpc->pp.recvbuf);
1358
0
  size_t len = smtpc->pp.nfinal;
1359
1360
0
  (void)instate;
1361
1362
0
  if((rcpt && smtpcode / 100 != 2 && smtpcode != 553 && smtpcode != 1) ||
1363
0
     (!rcpt && smtpcode / 100 != 2 && smtpcode != 1)) {
1364
0
    failf(data, "Command failed: %d", smtpcode);
1365
0
    result = CURLE_WEIRD_SERVER_REPLY;
1366
0
  }
1367
0
  else {
1368
0
    if(!data->req.no_body)
1369
0
      result = Curl_client_write(data, CLIENTWRITE_BODY, line, len);
1370
1371
0
    if(!result && (smtpcode != 1)) {
1372
0
      if(rcpt) {
1373
0
        smtp->rcpt_idx++;
1374
1375
0
        if(smtp->rcpt_idx < Curl_frzn_strs_count(data->set.a.mail_rcpt)) {
1376
          /* Send the next command */
1377
0
          result = smtp_perform_command(data, smtpc, smtp);
1378
0
        }
1379
0
        else
1380
          /* End of DO phase */
1381
0
          smtp_state(data, smtpc, SMTP_STOP);
1382
0
      }
1383
0
      else
1384
        /* End of DO phase */
1385
0
        smtp_state(data, smtpc, SMTP_STOP);
1386
0
    }
1387
0
  }
1388
1389
0
  return result;
1390
0
}
1391
1392
/* For MAIL responses */
1393
static CURLcode smtp_state_mail_resp(struct Curl_easy *data,
1394
                                     struct smtp_conn *smtpc,
1395
                                     struct SMTP *smtp,
1396
                                     int smtpcode,
1397
                                     smtpstate instate)
1398
0
{
1399
0
  CURLcode result = CURLE_OK;
1400
0
  (void)instate;
1401
1402
0
  if(smtpcode / 100 != 2) {
1403
0
    failf(data, "MAIL failed: %d", smtpcode);
1404
0
    result = CURLE_SEND_ERROR;
1405
0
  }
1406
0
  else
1407
    /* Start the RCPT TO command */
1408
0
    result = smtp_perform_rcpt_to(data, smtpc, smtp);
1409
1410
0
  return result;
1411
0
}
1412
1413
/* For RCPT responses */
1414
static CURLcode smtp_state_rcpt_resp(struct Curl_easy *data,
1415
                                     struct smtp_conn *smtpc,
1416
                                     struct SMTP *smtp,
1417
                                     int smtpcode,
1418
                                     smtpstate instate)
1419
0
{
1420
0
  CURLcode result = CURLE_OK;
1421
0
  bool is_smtp_err = FALSE;
1422
0
  bool is_smtp_blocking_err = FALSE;
1423
1424
0
  (void)instate;
1425
1426
0
  is_smtp_err = (smtpcode / 100 != 2);
1427
1428
  /* If there are multiple RCPT TO commands to issue, it is possible to
1429
     ignore errors and proceed with only the valid addresses. */
1430
0
  is_smtp_blocking_err = (is_smtp_err && !data->set.mail_rcpt_allowfails);
1431
1432
0
  if(is_smtp_err) {
1433
    /* Remembering the last failure which we can report if all "RCPT TO" have
1434
       failed and we cannot proceed. */
1435
0
    smtp->rcpt_last_error = smtpcode;
1436
1437
0
    if(is_smtp_blocking_err) {
1438
0
      failf(data, "RCPT failed: %d", smtpcode);
1439
0
      result = CURLE_SEND_ERROR;
1440
0
    }
1441
0
  }
1442
0
  else {
1443
    /* Some RCPT TO commands have succeeded. */
1444
0
    smtp->rcpt_had_ok = TRUE;
1445
0
  }
1446
1447
0
  if(!is_smtp_blocking_err) {
1448
0
    smtp->rcpt_idx++;
1449
1450
0
    if(smtp->rcpt_idx < Curl_frzn_strs_count(data->set.a.mail_rcpt)) {
1451
      /* Send the next RCPT TO command */
1452
0
      result = smtp_perform_rcpt_to(data, smtpc, smtp);
1453
0
    }
1454
0
    else {
1455
      /* We were not able to issue a successful RCPT TO command while going
1456
         over recipients (potentially multiple). Sending back last error. */
1457
0
      if(!smtp->rcpt_had_ok) {
1458
0
        failf(data, "RCPT failed: %d (last error)", smtp->rcpt_last_error);
1459
0
        result = CURLE_SEND_ERROR;
1460
0
      }
1461
0
      else {
1462
        /* Send the DATA command */
1463
0
        result = Curl_pp_sendf(data, &smtpc->pp, "%s", "DATA");
1464
1465
0
        if(!result)
1466
0
          smtp_state(data, smtpc, SMTP_DATA);
1467
0
      }
1468
0
    }
1469
0
  }
1470
1471
0
  return result;
1472
0
}
1473
1474
/* For DATA response */
1475
static CURLcode smtp_state_data_resp(struct Curl_easy *data,
1476
                                     struct smtp_conn *smtpc,
1477
                                     int smtpcode,
1478
                                     smtpstate instate)
1479
0
{
1480
0
  CURLcode result = CURLE_OK;
1481
0
  (void)instate;
1482
1483
0
  if(smtpcode != 354) {
1484
0
    failf(data, "DATA failed: %d", smtpcode);
1485
0
    result = CURLE_SEND_ERROR;
1486
0
  }
1487
0
  else {
1488
    /* Set the progress upload size */
1489
0
    Curl_pgrsSetUploadSize(data, data->state.infilesize);
1490
1491
    /* SMTP upload */
1492
0
    Curl_xfer_setup_send(data);
1493
1494
    /* End of DO phase */
1495
0
    smtp_state(data, smtpc, SMTP_STOP);
1496
0
  }
1497
1498
0
  return result;
1499
0
}
1500
1501
/* For POSTDATA responses, which are received after the entire DATA
1502
   part has been sent to the server */
1503
static CURLcode smtp_state_postdata_resp(struct Curl_easy *data,
1504
                                         struct smtp_conn *smtpc,
1505
                                         int smtpcode,
1506
                                         smtpstate instate)
1507
0
{
1508
0
  CURLcode result = CURLE_OK;
1509
1510
0
  (void)instate;
1511
1512
0
  if(smtpcode != 250)
1513
0
    result = CURLE_WEIRD_SERVER_REPLY;
1514
1515
  /* End of DONE phase */
1516
0
  smtp_state(data, smtpc, SMTP_STOP);
1517
1518
0
  return result;
1519
0
}
1520
1521
static CURLcode smtp_pp_statemachine(struct Curl_easy *data,
1522
                                     struct connectdata *conn)
1523
0
{
1524
0
  CURLcode result = CURLE_OK;
1525
0
  int smtpcode;
1526
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1527
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1528
0
  size_t nread = 0;
1529
1530
0
  if(!smtpc || !smtp)
1531
0
    return CURLE_FAILED_INIT;
1532
1533
  /* Busy upgrading the connection; right now all I/O is SSL/TLS, not SMTP */
1534
0
upgrade_tls:
1535
0
  if(smtpc->state == SMTP_UPGRADETLS) {
1536
0
    result = smtp_perform_upgrade_tls(data, smtpc);
1537
0
    if(result || (smtpc->state == SMTP_UPGRADETLS))
1538
0
      return result;
1539
0
  }
1540
1541
  /* Flush any data that needs to be sent */
1542
0
  if(smtpc->pp.sendleft)
1543
0
    return Curl_pp_flushsend(data, &smtpc->pp);
1544
1545
0
  do {
1546
    /* Read the response from the server */
1547
0
    result = Curl_pp_readresp(data, &smtpc->pp, &smtpcode, &nread);
1548
0
    if(result)
1549
0
      return result;
1550
1551
    /* Store the latest response for later retrieval if necessary */
1552
0
    if(smtpc->state != SMTP_QUIT && smtpcode != 1)
1553
0
      data->info.httpcode = smtpcode;
1554
1555
0
    if(!smtpcode)
1556
0
      break;
1557
1558
    /* We have now received a full SMTP server response */
1559
0
    switch(smtpc->state) {
1560
0
    case SMTP_SERVERGREET:
1561
0
      result = smtp_state_servergreet_resp(data, smtpc,
1562
0
                                           smtpcode, smtpc->state);
1563
0
      break;
1564
1565
0
    case SMTP_EHLO:
1566
0
      result = smtp_state_ehlo_resp(data, smtpc, smtpcode, smtpc->state);
1567
0
      break;
1568
1569
0
    case SMTP_HELO:
1570
0
      result = smtp_state_helo_resp(data, smtpc, smtpcode, smtpc->state);
1571
0
      break;
1572
1573
0
    case SMTP_STARTTLS:
1574
0
      result = smtp_state_starttls_resp(data, smtpc, smtpcode, smtpc->state);
1575
      /* During UPGRADETLS, leave the read loop as we need to connect
1576
       * (e.g. TLS handshake) before we continue sending/receiving. */
1577
0
      if(!result && (smtpc->state == SMTP_UPGRADETLS))
1578
0
        goto upgrade_tls;
1579
0
      break;
1580
1581
0
    case SMTP_AUTH:
1582
0
      result = smtp_state_auth_resp(data, smtpc, smtpcode, smtpc->state);
1583
0
      break;
1584
1585
0
    case SMTP_COMMAND:
1586
0
      result = smtp_state_command_resp(data, smtpc, smtp,
1587
0
                                       smtpcode, smtpc->state);
1588
0
      break;
1589
1590
0
    case SMTP_MAIL:
1591
0
      result = smtp_state_mail_resp(data, smtpc, smtp, smtpcode, smtpc->state);
1592
0
      break;
1593
1594
0
    case SMTP_RCPT:
1595
0
      result = smtp_state_rcpt_resp(data, smtpc, smtp, smtpcode, smtpc->state);
1596
0
      break;
1597
1598
0
    case SMTP_DATA:
1599
0
      result = smtp_state_data_resp(data, smtpc, smtpcode, smtpc->state);
1600
0
      break;
1601
1602
0
    case SMTP_POSTDATA:
1603
0
      result = smtp_state_postdata_resp(data, smtpc, smtpcode, smtpc->state);
1604
0
      break;
1605
1606
0
    case SMTP_QUIT:
1607
0
    default:
1608
      /* internal error */
1609
0
      smtp_state(data, smtpc, SMTP_STOP);
1610
0
      break;
1611
0
    }
1612
0
  } while(!result && smtpc->state != SMTP_STOP &&
1613
0
          Curl_pp_moredata(&smtpc->pp));
1614
1615
0
  return result;
1616
0
}
1617
1618
/* Called repeatedly until done from multi.c */
1619
static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done)
1620
0
{
1621
0
  CURLcode result = CURLE_OK;
1622
0
  struct smtp_conn *smtpc =
1623
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1624
1625
0
  *done = FALSE;
1626
0
  if(!smtpc)
1627
0
    return CURLE_FAILED_INIT;
1628
1629
0
  result = Curl_pp_statemach(data, &smtpc->pp, FALSE);
1630
0
  *done = (smtpc->state == SMTP_STOP);
1631
0
  return result;
1632
0
}
1633
1634
static CURLcode smtp_block_statemach(struct Curl_easy *data,
1635
                                     struct smtp_conn *smtpc)
1636
0
{
1637
0
  CURLcode result = CURLE_OK;
1638
1639
0
  while(smtpc->state != SMTP_STOP && !result)
1640
0
    result = Curl_pp_statemach(data, &smtpc->pp, TRUE);
1641
1642
0
  return result;
1643
0
}
1644
1645
/* For the SMTP "protocol connect" and "doing" phases only */
1646
static CURLcode smtp_pollset(struct Curl_easy *data,
1647
                             struct easy_pollset *ps)
1648
0
{
1649
0
  struct smtp_conn *smtpc =
1650
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1651
0
  return smtpc ? Curl_pp_pollset(data, &smtpc->pp, ps) : CURLE_OK;
1652
0
}
1653
1654
/* SASL parameters for the smtp protocol */
1655
static const struct SASLproto saslsmtp = {
1656
  "smtp",               /* The service name */
1657
  smtp_perform_auth,    /* Send authentication command */
1658
  smtp_continue_auth,   /* Send authentication continuation */
1659
  smtp_cancel_auth,     /* Cancel authentication */
1660
  smtp_get_message,     /* Get SASL response message */
1661
  512 - 8,              /* Max line len - strlen("AUTH ") - 1 space - CRLF */
1662
  334,                  /* Code received when continuation is expected */
1663
  235,                  /* Code to receive upon authentication success */
1664
  SASL_AUTH_DEFAULT,    /* Default mechanisms */
1665
  SASL_FLAG_BASE64      /* Configuration flags */
1666
};
1667
1668
/***********************************************************************
1669
 *
1670
 * smtp_connect()
1671
 *
1672
 * This function should do everything that is to be considered a part of
1673
 * the connection phase.
1674
 *
1675
 * The variable pointed to by 'done' will be TRUE if the protocol-layer
1676
 * connect phase is done when this function returns, or FALSE if not.
1677
 */
1678
static CURLcode smtp_connect(struct Curl_easy *data, bool *done)
1679
0
{
1680
0
  struct smtp_conn *smtpc =
1681
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1682
0
  CURLcode result = CURLE_OK;
1683
1684
0
  *done = FALSE; /* default to not done yet */
1685
0
  if(!smtpc)
1686
0
    return CURLE_FAILED_INIT;
1687
1688
0
  PINGPONG_SETUP(&smtpc->pp, smtp_pp_statemachine, smtp_endofresp);
1689
1690
  /* Initialize the SASL storage */
1691
0
  Curl_sasl_init(&smtpc->sasl, data, &saslsmtp);
1692
1693
  /* Initialize the pingpong layer */
1694
0
  Curl_pp_init(&smtpc->pp, Curl_pgrs_now(data));
1695
1696
  /* Parse the URL options */
1697
0
  result = smtp_parse_url_options(data->conn, smtpc);
1698
0
  if(result)
1699
0
    return result;
1700
1701
  /* Parse the URL path */
1702
0
  result = smtp_parse_url_path(data, smtpc);
1703
0
  if(result)
1704
0
    return result;
1705
1706
  /* Start off waiting for the server greeting response */
1707
0
  smtp_state(data, smtpc, SMTP_SERVERGREET);
1708
1709
0
  result = smtp_multi_statemach(data, done);
1710
1711
0
  return result;
1712
0
}
1713
1714
/***********************************************************************
1715
 *
1716
 * smtp_done()
1717
 *
1718
 * The DONE function. This does what needs to be done after a single DO has
1719
 * performed.
1720
 *
1721
 * Input argument is already checked for validity.
1722
 */
1723
static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
1724
                          bool premature)
1725
0
{
1726
0
  struct smtp_conn *smtpc =
1727
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1728
0
  CURLcode result = CURLE_OK;
1729
0
  struct connectdata *conn = data->conn;
1730
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1731
1732
0
  (void)premature;
1733
1734
0
  if(!smtpc)
1735
0
    return CURLE_FAILED_INIT;
1736
0
  if(!smtp)
1737
0
    return CURLE_OK;
1738
1739
  /* Cleanup our per-request based variables */
1740
0
  curlx_safefree(smtp->custom);
1741
1742
0
  if(status) {
1743
0
    CURL_TRC_M(data, "SMTP done with bad status");
1744
0
    connclose(conn); /* marked for closure */
1745
0
    result = status;         /* use the already set error code */
1746
0
  }
1747
0
  else if(!data->set.connect_only && data->set.a.mail_rcpt &&
1748
0
          (data->state.upload || IS_MIME_POST(data))) {
1749
1750
0
    smtp_state(data, smtpc, SMTP_POSTDATA);
1751
1752
    /* Run the state-machine */
1753
0
    result = smtp_block_statemach(data, smtpc);
1754
0
  }
1755
1756
  /* Clear the transfer mode for the next request */
1757
0
  smtp->transfer = PPTRANSFER_BODY;
1758
0
  CURL_TRC_SMTP(data, "smtp_done(status=%d, premature=%d) -> %d",
1759
0
                (int)status, premature, (int)result);
1760
0
  return result;
1761
0
}
1762
1763
/***********************************************************************
1764
 *
1765
 * smtp_perform()
1766
 *
1767
 * This is the actual DO function for SMTP. Transfer a mail, send a command
1768
 * or get some data according to the options previously setup.
1769
 */
1770
static CURLcode smtp_perform(struct Curl_easy *data,
1771
                             struct smtp_conn *smtpc,
1772
                             struct SMTP *smtp,
1773
                             bool *connected,
1774
                             bool *dophase_done)
1775
0
{
1776
  /* This is SMTP and no proxy */
1777
0
  CURLcode result = CURLE_OK;
1778
1779
0
  CURL_TRC_SMTP(data, "smtp_perform(), start");
1780
1781
0
  if(data->req.no_body) {
1782
    /* Requested no body means no transfer */
1783
0
    smtp->transfer = PPTRANSFER_INFO;
1784
0
  }
1785
1786
0
  *dophase_done = FALSE; /* not done yet */
1787
1788
  /* Store the first recipient (or NULL if not specified) */
1789
0
  smtp->rcpt_idx = 0;
1790
1791
  /* Track of whether we have successfully sent at least one RCPT TO command */
1792
0
  smtp->rcpt_had_ok = FALSE;
1793
1794
  /* Track of the last error we have received by sending RCPT TO command */
1795
0
  smtp->rcpt_last_error = 0;
1796
1797
  /* Initial data character is the first character in line: it is implicitly
1798
     preceded by a virtual CRLF. */
1799
0
  smtp->trailing_crlf = TRUE;
1800
0
  smtp->eob = 2;
1801
1802
  /* Start the first command in the DO phase */
1803
0
  if((data->state.upload || IS_MIME_POST(data)) && data->set.a.mail_rcpt)
1804
    /* MAIL transfer */
1805
0
    result = smtp_perform_mail(data, smtpc);
1806
0
  else
1807
    /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
1808
0
    result = smtp_perform_command(data, smtpc, smtp);
1809
1810
0
  if(result)
1811
0
    goto out;
1812
1813
  /* Run the state-machine */
1814
0
  result = smtp_multi_statemach(data, dophase_done);
1815
1816
0
  *connected = Curl_conn_is_connected(data->conn);
1817
1818
0
out:
1819
0
  CURL_TRC_SMTP(data, "smtp_perform() -> %d, connected=%d, done=%d",
1820
0
                (int)result, *connected, *dophase_done);
1821
0
  return result;
1822
0
}
1823
1824
/* Call this when the DO phase has completed */
1825
static CURLcode smtp_dophase_done(struct Curl_easy *data,
1826
                                  struct SMTP *smtp,
1827
                                  bool connected)
1828
0
{
1829
0
  (void)connected;
1830
1831
0
  if(smtp->transfer != PPTRANSFER_BODY)
1832
    /* no data to transfer */
1833
0
    Curl_xfer_setup_nop(data);
1834
1835
0
  return CURLE_OK;
1836
0
}
1837
1838
/***********************************************************************
1839
 *
1840
 * smtp_regular_transfer()
1841
 *
1842
 * The input argument is already checked for validity.
1843
 *
1844
 * Performs all commands done before a regular transfer between a local and a
1845
 * remote host.
1846
 */
1847
static CURLcode smtp_regular_transfer(struct Curl_easy *data,
1848
                                      struct smtp_conn *smtpc,
1849
                                      struct SMTP *smtp,
1850
                                      bool *dophase_done)
1851
0
{
1852
0
  CURLcode result = CURLE_OK;
1853
0
  bool connected = FALSE;
1854
1855
  /* Make sure size is unknown at this point */
1856
0
  data->req.size = -1;
1857
1858
  /* Set the progress data */
1859
0
  Curl_pgrsReset(data);
1860
1861
  /* Carry out the perform */
1862
0
  result = smtp_perform(data, smtpc, smtp, &connected, dophase_done);
1863
1864
  /* Perform post DO phase operations if necessary */
1865
0
  if(!result && *dophase_done)
1866
0
    result = smtp_dophase_done(data, smtp, connected);
1867
1868
0
  CURL_TRC_SMTP(data, "smtp_regular_transfer() -> %d, done=%d",
1869
0
                (int)result, *dophase_done);
1870
0
  return result;
1871
0
}
1872
1873
/***********************************************************************
1874
 *
1875
 * smtp_do()
1876
 *
1877
 * This function is registered as 'curl_do' function. It decodes the path
1878
 * parts etc as a wrapper to the actual DO function (smtp_perform).
1879
 *
1880
 * The input argument is already checked for validity.
1881
 */
1882
static CURLcode smtp_do(struct Curl_easy *data, bool *done)
1883
0
{
1884
0
  struct smtp_conn *smtpc =
1885
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1886
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1887
0
  CURLcode result = CURLE_OK;
1888
1889
0
  DEBUGASSERT(data);
1890
0
  DEBUGASSERT(data->conn);
1891
0
  *done = FALSE; /* default to false */
1892
0
  if(!smtpc || !smtp)
1893
0
    return CURLE_FAILED_INIT;
1894
1895
  /* Parse the custom request */
1896
0
  result = smtp_parse_custom_request(data, smtp);
1897
0
  if(result)
1898
0
    return result;
1899
1900
0
  result = smtp_regular_transfer(data, smtpc, smtp, done);
1901
0
  CURL_TRC_SMTP(data, "smtp_do() -> %d, done=%d", (int)result, *done);
1902
0
  return result;
1903
0
}
1904
1905
/***********************************************************************
1906
 *
1907
 * smtp_disconnect()
1908
 *
1909
 * Disconnect from an SMTP server. Cleanup protocol-specific per-connection
1910
 * resources. BLOCKING.
1911
 */
1912
static CURLcode smtp_disconnect(struct Curl_easy *data,
1913
                                struct connectdata *conn,
1914
                                bool dead_connection)
1915
0
{
1916
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1917
1918
0
  if(!smtpc)
1919
0
    return CURLE_FAILED_INIT;
1920
1921
  /* We cannot send quit unconditionally. If this connection is stale or
1922
     bad in any way, sending quit and waiting around here will make the
1923
     disconnect wait in vain and cause more problems than we need to. */
1924
1925
0
  if(!dead_connection && conn->bits.protoconnstart &&
1926
0
     !Curl_pp_needs_flush(data, &smtpc->pp) &&
1927
0
     !smtp_perform_quit(data, smtpc))
1928
0
    (void)smtp_block_statemach(data, smtpc); /* ignore on QUIT */
1929
1930
0
  CURL_TRC_SMTP(data, "smtp_disconnect(), finished");
1931
0
  return CURLE_OK;
1932
0
}
1933
1934
/* Called from multi.c while DOing */
1935
static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
1936
0
{
1937
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1938
0
  CURLcode result;
1939
1940
0
  if(!smtp)
1941
0
    return CURLE_FAILED_INIT;
1942
0
  result = smtp_multi_statemach(data, dophase_done);
1943
0
  if(result)
1944
0
    DEBUGF(infof(data, "DO phase failed"));
1945
0
  else if(*dophase_done) {
1946
0
    result = smtp_dophase_done(data, smtp, FALSE /* not connected */);
1947
1948
0
    DEBUGF(infof(data, "DO phase is complete"));
1949
0
  }
1950
1951
0
  CURL_TRC_SMTP(data, "smtp_doing() -> %d, done=%d", (int)result,
1952
0
                *dophase_done);
1953
0
  return result;
1954
0
}
1955
1956
static void smtp_easy_dtor(const void *key, size_t klen, void *entry)
1957
0
{
1958
0
  struct SMTP *smtp = entry;
1959
0
  (void)key;
1960
0
  (void)klen;
1961
0
  curlx_free(smtp);
1962
0
}
1963
1964
static void smtp_conn_dtor(const void *key, size_t klen, void *entry)
1965
0
{
1966
0
  struct smtp_conn *smtpc = entry;
1967
0
  (void)key;
1968
0
  (void)klen;
1969
0
  Curl_pp_disconnect(&smtpc->pp);
1970
0
  curlx_safefree(smtpc->domain);
1971
0
  curlx_free(smtpc);
1972
0
}
1973
1974
static CURLcode smtp_setup_connection(struct Curl_easy *data,
1975
                                      struct connectdata *conn)
1976
0
{
1977
0
  struct smtp_conn *smtpc;
1978
0
  struct SMTP *smtp;
1979
0
  CURLcode result = CURLE_OK;
1980
1981
0
  smtpc = curlx_calloc(1, sizeof(*smtpc));
1982
0
  if(!smtpc ||
1983
0
     Curl_conn_meta_set(conn, CURL_META_SMTP_CONN, smtpc, smtp_conn_dtor)) {
1984
0
    result = CURLE_OUT_OF_MEMORY;
1985
0
    goto out;
1986
0
  }
1987
1988
0
  smtp = curlx_calloc(1, sizeof(*smtp));
1989
0
  if(!smtp ||
1990
0
     Curl_meta_set(data, CURL_META_SMTP_EASY, smtp, smtp_easy_dtor))
1991
0
    result = CURLE_OUT_OF_MEMORY;
1992
1993
0
out:
1994
0
  CURL_TRC_SMTP(data, "smtp_setup_connection() -> %d", (int)result);
1995
0
  return result;
1996
0
}
1997
1998
/*
1999
 * SMTP protocol handler.
2000
 */
2001
const struct Curl_protocol Curl_protocol_smtp = {
2002
  smtp_setup_connection,            /* setup_connection */
2003
  ZERO_NULL,                        /* setup_filters */
2004
  smtp_do,                          /* do_it */
2005
  smtp_done,                        /* done */
2006
  ZERO_NULL,                        /* do_more */
2007
  smtp_connect,                     /* connect_it */
2008
  smtp_multi_statemach,             /* connecting */
2009
  smtp_doing,                       /* doing */
2010
  smtp_pollset,                     /* proto_pollset */
2011
  smtp_pollset,                     /* doing_pollset */
2012
  ZERO_NULL,                        /* domore_pollset */
2013
  ZERO_NULL,                        /* perform_pollset */
2014
  smtp_disconnect,                  /* disconnect */
2015
  ZERO_NULL,                        /* write_resp */
2016
  ZERO_NULL,                        /* write_resp_hd */
2017
  ZERO_NULL,                        /* connection_is_dead */
2018
  ZERO_NULL,                        /* attach connection */
2019
  ZERO_NULL,                        /* follow */
2020
};
2021
2022
#endif /* CURL_DISABLE_SMTP */