Coverage Report

Created: 2025-07-11 07:03

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