Coverage Report

Created: 2026-01-17 06:25

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