Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/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
0
#define CURL_META_SMTP_EASY   "meta:proto:smtp:easy"
91
/* meta key for storing protocol meta at connection */
92
0
#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
0
{
268
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
269
0
  bool result = FALSE;
270
0
  (void)data;
271
272
0
  DEBUGASSERT(smtpc);
273
0
  if(!smtpc)
274
0
    return FALSE;
275
276
  /* Nothing for us */
277
0
  if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2]))
278
0
    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
0
  if(line[3] == ' ' || len == 5) {
285
0
    char tmpline[6];
286
0
    curl_off_t code;
287
0
    const char *p = tmpline;
288
0
    result = TRUE;
289
0
    memcpy(tmpline, line, (len == 5 ? 5 : 3));
290
0
    tmpline[len == 5 ? 5 : 3 ] = 0;
291
0
    if(curlx_str_number(&p, &code, len == 5 ? 99999 : 999))
292
0
      return FALSE;
293
0
    *resp = (int) code;
294
295
    /* Make sure real server never sends internal value */
296
0
    if(*resp == 1)
297
0
      *resp = 0;
298
0
  }
299
  /* Do we have a multiline (continuation) response? */
300
0
  else if(line[3] == '-' &&
301
0
          (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) {
302
0
    result = TRUE;
303
0
    *resp = 1;  /* Internal response code */
304
0
  }
305
306
0
  return result;
307
0
}
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
0
{
317
0
  struct smtp_conn *smtpc =
318
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
319
0
  char *message;
320
0
  size_t len;
321
322
0
  if(!smtpc)
323
0
    return CURLE_FAILED_INIT;
324
325
0
  message = curlx_dyn_ptr(&smtpc->pp.recvbuf);
326
0
  len = smtpc->pp.nfinal;
327
0
  if(len > 4) {
328
    /* Find the start of the message */
329
0
    len -= 4;
330
0
    for(message += 4; *message == ' ' || *message == '\t'; message++, len--)
331
0
      ;
332
333
    /* Find the end of the message */
334
0
    while(len--)
335
0
      if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' &&
336
0
         message[len] != '\t')
337
0
        break;
338
339
    /* Terminate the message */
340
0
    message[++len] = '\0';
341
0
    Curl_bufref_set(out, message, len, NULL);
342
0
  }
343
0
  else
344
    /* junk input => zero length output */
345
0
    Curl_bufref_set(out, "", 0, NULL);
346
347
0
  return CURLE_OK;
348
0
}
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
0
{
360
0
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
361
  /* for debug purposes */
362
0
  static const char * const names[] = {
363
0
    "STOP",
364
0
    "SERVERGREET",
365
0
    "EHLO",
366
0
    "HELO",
367
0
    "STARTTLS",
368
0
    "UPGRADETLS",
369
0
    "AUTH",
370
0
    "COMMAND",
371
0
    "MAIL",
372
0
    "RCPT",
373
0
    "DATA",
374
0
    "POSTDATA",
375
0
    "QUIT",
376
    /* LAST */
377
0
  };
378
379
0
  if(smtpc->state != newstate)
380
0
    CURL_TRC_SMTP(data, "state change from %s to %s",
381
0
                  names[smtpc->state], names[newstate]);
382
0
#endif
383
384
0
  smtpc->state = newstate;
385
0
}
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
0
{
397
0
  CURLcode result = CURLE_OK;
398
399
0
  smtpc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanism yet */
400
0
  smtpc->sasl.authused = SASL_AUTH_NONE;  /* Clear the authentication mechanism
401
                                             used for esmtp connections */
402
0
  smtpc->tls_supported = FALSE;           /* Clear the TLS capability */
403
0
  smtpc->auth_supported = FALSE;          /* Clear the AUTH capability */
404
405
  /* Send the EHLO command */
406
0
  result = Curl_pp_sendf(data, &smtpc->pp, "EHLO %s", smtpc->domain);
407
408
0
  if(!result)
409
0
    smtp_state(data, smtpc, SMTP_EHLO);
410
411
0
  return result;
412
0
}
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
0
{
423
0
  CURLcode result = CURLE_OK;
424
425
0
  smtpc->sasl.authused = SASL_AUTH_NONE; /* No authentication mechanism used
426
                                            in smtp connections */
427
428
  /* Send the HELO command */
429
0
  result = Curl_pp_sendf(data, &smtpc->pp, "HELO %s", smtpc->domain);
430
431
0
  if(!result)
432
0
    smtp_state(data, smtpc, SMTP_HELO);
433
434
0
  return result;
435
0
}
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
0
{
508
0
  CURLcode result = CURLE_OK;
509
0
  struct smtp_conn *smtpc =
510
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
511
0
  const char *ir = (const char *) Curl_bufref_ptr(initresp);
512
513
0
  if(!smtpc)
514
0
    return CURLE_FAILED_INIT;
515
516
0
  if(ir) {                                  /* AUTH <mech> ...<crlf> */
517
    /* Send the AUTH command with the initial response */
518
0
    result = Curl_pp_sendf(data, &smtpc->pp, "AUTH %s %s", mech, ir);
519
0
  }
520
0
  else {
521
    /* Send the AUTH command */
522
0
    result = Curl_pp_sendf(data, &smtpc->pp, "AUTH %s", mech);
523
0
  }
524
525
0
  return result;
526
0
}
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
0
{
538
0
  struct smtp_conn *smtpc =
539
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
540
541
0
  (void)mech;
542
0
  if(!smtpc)
543
0
    return CURLE_FAILED_INIT;
544
0
  return Curl_pp_sendf(data, &smtpc->pp,
545
0
                       "%s", (const char *) Curl_bufref_ptr(resp));
546
0
}
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
0
{
556
0
  struct smtp_conn *smtpc =
557
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
558
559
0
  (void)mech;
560
0
  if(!smtpc)
561
0
    return CURLE_FAILED_INIT;
562
0
  return Curl_pp_sendf(data, &smtpc->pp, "*");
563
0
}
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
0
{
575
0
  CURLcode result = CURLE_OK;
576
0
  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
0
  if(!smtpc->auth_supported ||
581
0
     !Curl_sasl_can_authenticate(&smtpc->sasl, data)) {
582
0
    smtp_state(data, smtpc, SMTP_STOP);
583
0
    return result;
584
0
  }
585
586
  /* Calculate the SASL login details */
587
0
  result = Curl_sasl_start(&smtpc->sasl, data, FALSE, &progress);
588
589
0
  if(!result) {
590
0
    if(progress == SASL_INPROGRESS)
591
0
      smtp_state(data, smtpc, SMTP_AUTH);
592
0
    else
593
0
      result = Curl_sasl_is_blocked(&smtpc->sasl, data);
594
0
  }
595
596
0
  return result;
597
0
}
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
0
{
609
0
  CURLcode result = CURLE_OK;
610
611
0
  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
0
    bool utf8 = FALSE;
617
618
0
    if((!smtp->custom) || (!smtp->custom[0])) {
619
0
      char *address = NULL;
620
0
      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
0
      result = smtp_parse_address(smtp->rcpt->data,
625
0
                                  &address, &host);
626
0
      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
0
      utf8 = (smtpc->utf8_supported) &&
632
0
             ((host.encalloc) || (!Curl_is_ASCII_name(address)) ||
633
0
              (!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
0
      result = Curl_pp_sendf(data, &smtpc->pp, "VRFY %s%s%s%s",
638
0
                             address,
639
0
                             host.name ? "@" : "",
640
0
                             host.name ? host.name : "",
641
0
                             utf8 ? " SMTPUTF8" : "");
642
643
0
      Curl_free_idnconverted_hostname(&host);
644
0
      free(address);
645
0
    }
646
0
    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
0
      utf8 = (smtpc->utf8_supported) && (!strcmp(smtp->custom, "EXPN"));
650
651
      /* Send the custom recipient based command such as the EXPN command */
652
0
      result = Curl_pp_sendf(data, &smtpc->pp,
653
0
                             "%s %s%s", smtp->custom,
654
0
                             smtp->rcpt->data,
655
0
                             utf8 ? " SMTPUTF8" : "");
656
0
    }
657
0
  }
658
0
  else
659
    /* Send the non-recipient based command such as HELP */
660
0
    result = Curl_pp_sendf(data, &smtpc->pp, "%s",
661
0
                           smtp->custom && smtp->custom[0] != '\0' ?
662
0
                           smtp->custom : "HELP");
663
664
0
  if(!result)
665
0
    smtp_state(data, smtpc, SMTP_COMMAND);
666
667
0
  return result;
668
0
}
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
0
{
680
0
  char *from = NULL;
681
0
  char *auth = NULL;
682
0
  char *size = NULL;
683
0
  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
0
  bool utf8 = FALSE;
690
691
  /* Calculate the FROM parameter */
692
0
  if(data->set.str[STRING_MAIL_FROM]) {
693
0
    char *address = NULL;
694
0
    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
0
    result = smtp_parse_address(data->set.str[STRING_MAIL_FROM],
699
0
                                &address, &host);
700
0
    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
0
    utf8 = (smtpc->utf8_supported) &&
706
0
           ((host.encalloc) || (!Curl_is_ASCII_name(address)) ||
707
0
            (!Curl_is_ASCII_name(host.name)));
708
709
0
    if(host.name) {
710
0
      from = aprintf("<%s@%s>", address, host.name);
711
712
0
      Curl_free_idnconverted_hostname(&host);
713
0
    }
714
0
    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
0
      from = aprintf("<%s>", address);
718
719
0
    free(address);
720
0
  }
721
0
  else
722
    /* Null reverse-path, RFC-5321, sect. 3.6.3 */
723
0
    from = strdup("<>");
724
725
0
  if(!from) {
726
0
    result = CURLE_OUT_OF_MEMORY;
727
0
    goto out;
728
0
  }
729
730
  /* Calculate the optional AUTH parameter */
731
0
  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
0
#ifndef CURL_DISABLE_MIME
772
  /* Prepare the mime data if some. */
773
0
  if(data->set.mimepost.kind != MIMEKIND_NONE) {
774
    /* Use the whole structure as data. */
775
0
    data->set.mimepost.flags &= ~(unsigned int)MIME_BODY_ONLY;
776
777
    /* Add external headers and mime version. */
778
0
    curl_mime_headers(&data->set.mimepost, data->set.headers, 0);
779
0
    result = Curl_mime_prepare_headers(data, &data->set.mimepost, NULL,
780
0
                                       NULL, MIMESTRATEGY_MAIL);
781
782
0
    if(!result)
783
0
      if(!Curl_checkheaders(data, STRCONST("Mime-Version")))
784
0
        result = Curl_mime_add_header(&data->set.mimepost.curlheaders,
785
0
                                      "Mime-Version: 1.0");
786
787
0
    if(!result)
788
0
      result = Curl_creader_set_mime(data, &data->set.mimepost);
789
0
    if(result)
790
0
      goto out;
791
0
    data->state.infilesize = Curl_creader_total_length(data);
792
0
  }
793
0
  else
794
0
#endif
795
0
  {
796
0
    result = Curl_creader_set_fread(data, data->state.infilesize);
797
0
    if(result)
798
0
      goto out;
799
0
  }
800
801
  /* Calculate the optional SIZE parameter */
802
0
  if(smtpc->size_supported && data->state.infilesize > 0) {
803
0
    size = aprintf("%" FMT_OFF_T, data->state.infilesize);
804
805
0
    if(!size) {
806
0
      result = CURLE_OUT_OF_MEMORY;
807
0
      goto out;
808
0
    }
809
0
  }
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
0
  if(smtpc->utf8_supported && !utf8) {
816
0
    struct curl_slist *rcpt = smtp->rcpt;
817
818
0
    while(rcpt && !utf8) {
819
      /* Does the hostname contain non-ASCII characters? */
820
0
      if(!Curl_is_ASCII_name(rcpt->data))
821
0
        utf8 = TRUE;
822
823
0
      rcpt = rcpt->next;
824
0
    }
825
0
  }
826
827
  /* Add the client reader doing STMP EOB escaping */
828
0
  result = cr_eob_add(data);
829
0
  if(result)
830
0
    goto out;
831
832
  /* Send the MAIL command */
833
0
  result = Curl_pp_sendf(data, &smtpc->pp,
834
0
                         "MAIL FROM:%s%s%s%s%s%s",
835
0
                         from,                 /* Mandatory                 */
836
0
                         auth ? " AUTH=" : "", /* Optional on AUTH support  */
837
0
                         auth ? auth : "",     /*                           */
838
0
                         size ? " SIZE=" : "", /* Optional on SIZE support  */
839
0
                         size ? size : "",     /*                           */
840
0
                         utf8 ? " SMTPUTF8"    /* Internationalised mailbox */
841
0
                               : "");          /* included in our envelope  */
842
843
0
out:
844
0
  free(from);
845
0
  free(auth);
846
0
  free(size);
847
848
0
  if(!result)
849
0
    smtp_state(data, smtpc, SMTP_MAIL);
850
851
0
  return result;
852
0
}
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
0
{
865
0
  CURLcode result = CURLE_OK;
866
0
  char *address = NULL;
867
0
  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
0
  result = smtp_parse_address(smtp->rcpt->data,
872
0
                              &address, &host);
873
0
  if(result)
874
0
    return result;
875
876
  /* Send the RCPT TO command */
877
0
  if(host.name)
878
0
    result = Curl_pp_sendf(data, &smtpc->pp, "RCPT TO:<%s@%s>",
879
0
                           address, host.name);
880
0
  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
0
    result = Curl_pp_sendf(data, &smtpc->pp, "RCPT TO:<%s>", address);
884
885
0
  Curl_free_idnconverted_hostname(&host);
886
0
  free(address);
887
888
0
  if(!result)
889
0
    smtp_state(data, smtpc, SMTP_RCPT);
890
891
0
  return result;
892
0
}
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
0
{
903
  /* Send the QUIT command */
904
0
  CURLcode result = Curl_pp_sendf(data, &smtpc->pp, "%s", "QUIT");
905
906
0
  if(!result)
907
0
    smtp_state(data, smtpc, SMTP_QUIT);
908
909
0
  return result;
910
0
}
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
0
{
918
0
  CURLcode result = CURLE_OK;
919
0
  (void)instate; /* no use for this yet */
920
921
0
  if(smtpcode/100 != 2) {
922
0
    failf(data, "Got unexpected smtp-server response: %d", smtpcode);
923
0
    result = CURLE_WEIRD_SERVER_REPLY;
924
0
  }
925
0
  else
926
0
    result = smtp_perform_ehlo(data, smtpc);
927
928
0
  return result;
929
0
}
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
0
{
964
0
  CURLcode result = CURLE_OK;
965
0
  const char *line = curlx_dyn_ptr(&smtpc->pp.recvbuf);
966
0
  size_t len = smtpc->pp.nfinal;
967
968
0
  (void)instate; /* no use for this yet */
969
970
0
  if(smtpcode/100 != 2 && smtpcode != 1) {
971
0
    if(data->set.use_ssl <= CURLUSESSL_TRY
972
0
       || Curl_conn_is_ssl(data->conn, FIRSTSOCKET))
973
0
      result = smtp_perform_helo(data, smtpc);
974
0
    else {
975
0
      failf(data, "Remote access denied: %d", smtpcode);
976
0
      result = CURLE_REMOTE_ACCESS_DENIED;
977
0
    }
978
0
  }
979
0
  else if(len >= 4) {
980
0
    line += 4;
981
0
    len -= 4;
982
983
    /* Does the server support the STARTTLS capability? */
984
0
    if(len >= 8 && !memcmp(line, "STARTTLS", 8))
985
0
      smtpc->tls_supported = TRUE;
986
987
    /* Does the server support the SIZE capability? */
988
0
    else if(len >= 4 && !memcmp(line, "SIZE", 4))
989
0
      smtpc->size_supported = TRUE;
990
991
    /* Does the server support the UTF-8 capability? */
992
0
    else if(len >= 8 && !memcmp(line, "SMTPUTF8", 8))
993
0
      smtpc->utf8_supported = TRUE;
994
995
    /* Does the server support authentication? */
996
0
    else if(len >= 5 && !memcmp(line, "AUTH ", 5)) {
997
0
      smtpc->auth_supported = TRUE;
998
999
      /* Advance past the AUTH keyword */
1000
0
      line += 5;
1001
0
      len -= 5;
1002
1003
      /* Loop through the data line */
1004
0
      for(;;) {
1005
0
        size_t llen;
1006
0
        size_t wordlen;
1007
0
        unsigned short mechbit;
1008
1009
0
        while(len &&
1010
0
              (*line == ' ' || *line == '\t' ||
1011
0
               *line == '\r' || *line == '\n')) {
1012
1013
0
          line++;
1014
0
          len--;
1015
0
        }
1016
1017
0
        if(!len)
1018
0
          break;
1019
1020
        /* Extract the word */
1021
0
        for(wordlen = 0; wordlen < len && line[wordlen] != ' ' &&
1022
0
              line[wordlen] != '\t' && line[wordlen] != '\r' &&
1023
0
              line[wordlen] != '\n';)
1024
0
          wordlen++;
1025
1026
        /* Test the word for a matching authentication mechanism */
1027
0
        mechbit = Curl_sasl_decode_mech(line, wordlen, &llen);
1028
0
        if(mechbit && llen == wordlen)
1029
0
          smtpc->sasl.authmechs |= mechbit;
1030
1031
0
        line += wordlen;
1032
0
        len -= wordlen;
1033
0
      }
1034
0
    }
1035
1036
0
    if(smtpcode != 1) {
1037
0
      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
0
        if(smtpc->tls_supported)
1040
          /* Switch to TLS connection now */
1041
0
          result = smtp_perform_starttls(data, smtpc);
1042
0
        else if(data->set.use_ssl == CURLUSESSL_TRY)
1043
          /* Fallback and carry on with authentication */
1044
0
          result = smtp_perform_authentication(data, smtpc);
1045
0
        else {
1046
0
          failf(data, "STARTTLS not supported.");
1047
0
          result = CURLE_USE_SSL_FAILED;
1048
0
        }
1049
0
      }
1050
0
      else
1051
0
        result = smtp_perform_authentication(data, smtpc);
1052
0
    }
1053
0
  }
1054
0
  else {
1055
0
    failf(data, "Unexpectedly short EHLO response");
1056
0
    result = CURLE_WEIRD_SERVER_REPLY;
1057
0
  }
1058
1059
0
  return result;
1060
0
}
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
0
{
1068
0
  CURLcode result = CURLE_OK;
1069
0
  (void)instate; /* no use for this yet */
1070
1071
0
  if(smtpcode/100 != 2) {
1072
0
    failf(data, "Remote access denied: %d", smtpcode);
1073
0
    result = CURLE_REMOTE_ACCESS_DENIED;
1074
0
  }
1075
0
  else
1076
    /* End of connect phase */
1077
0
    smtp_state(data, smtpc, SMTP_STOP);
1078
1079
0
  return result;
1080
0
}
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
0
{
1088
0
  CURLcode result = CURLE_OK;
1089
0
  saslprogress progress;
1090
1091
0
  (void)instate; /* no use for this yet */
1092
1093
0
  result = Curl_sasl_continue(&smtpc->sasl, data, smtpcode, &progress);
1094
0
  if(!result)
1095
0
    switch(progress) {
1096
0
    case SASL_DONE:
1097
0
      smtp_state(data, smtpc, SMTP_STOP);  /* Authenticated */
1098
0
      break;
1099
0
    case SASL_IDLE:            /* No mechanism left after cancellation */
1100
0
      failf(data, "Authentication cancelled");
1101
0
      result = CURLE_LOGIN_DENIED;
1102
0
      break;
1103
0
    default:
1104
0
      break;
1105
0
    }
1106
1107
0
  return result;
1108
0
}
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
0
{
1117
0
  CURLcode result = CURLE_OK;
1118
0
  char *line = curlx_dyn_ptr(&smtpc->pp.recvbuf);
1119
0
  size_t len = smtpc->pp.nfinal;
1120
1121
0
  (void)instate; /* no use for this yet */
1122
1123
0
  if((smtp->rcpt && smtpcode/100 != 2 && smtpcode != 553 && smtpcode != 1) ||
1124
0
     (!smtp->rcpt && smtpcode/100 != 2 && smtpcode != 1)) {
1125
0
    failf(data, "Command failed: %d", smtpcode);
1126
0
    result = CURLE_WEIRD_SERVER_REPLY;
1127
0
  }
1128
0
  else {
1129
0
    if(!data->req.no_body)
1130
0
      result = Curl_client_write(data, CLIENTWRITE_BODY, line, len);
1131
1132
0
    if(smtpcode != 1) {
1133
0
      if(smtp->rcpt) {
1134
0
        smtp->rcpt = smtp->rcpt->next;
1135
1136
0
        if(smtp->rcpt) {
1137
          /* Send the next command */
1138
0
          result = smtp_perform_command(data, smtpc, smtp);
1139
0
        }
1140
0
        else
1141
          /* End of DO phase */
1142
0
          smtp_state(data, smtpc, SMTP_STOP);
1143
0
      }
1144
0
      else
1145
        /* End of DO phase */
1146
0
        smtp_state(data, smtpc, SMTP_STOP);
1147
0
    }
1148
0
  }
1149
1150
0
  return result;
1151
0
}
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
0
{
1160
0
  CURLcode result = CURLE_OK;
1161
0
  (void)instate; /* no use for this yet */
1162
1163
0
  if(smtpcode/100 != 2) {
1164
0
    failf(data, "MAIL failed: %d", smtpcode);
1165
0
    result = CURLE_SEND_ERROR;
1166
0
  }
1167
0
  else
1168
    /* Start the RCPT TO command */
1169
0
    result = smtp_perform_rcpt_to(data, smtpc, smtp);
1170
1171
0
  return result;
1172
0
}
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
0
{
1181
0
  CURLcode result = CURLE_OK;
1182
0
  bool is_smtp_err = FALSE;
1183
0
  bool is_smtp_blocking_err = FALSE;
1184
1185
0
  (void)instate; /* no use for this yet */
1186
1187
0
  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
0
  is_smtp_blocking_err = (is_smtp_err && !data->set.mail_rcpt_allowfails);
1192
1193
0
  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
0
    smtp->rcpt_last_error = smtpcode;
1197
1198
0
    if(is_smtp_blocking_err) {
1199
0
      failf(data, "RCPT failed: %d", smtpcode);
1200
0
      result = CURLE_SEND_ERROR;
1201
0
    }
1202
0
  }
1203
0
  else {
1204
    /* Some RCPT TO commands have succeeded. */
1205
0
    smtp->rcpt_had_ok = TRUE;
1206
0
  }
1207
1208
0
  if(!is_smtp_blocking_err) {
1209
0
    smtp->rcpt = smtp->rcpt->next;
1210
1211
0
    if(smtp->rcpt)
1212
      /* Send the next RCPT TO command */
1213
0
      result = smtp_perform_rcpt_to(data, smtpc, smtp);
1214
0
    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
0
      if(!smtp->rcpt_had_ok) {
1218
0
        failf(data, "RCPT failed: %d (last error)", smtp->rcpt_last_error);
1219
0
        result = CURLE_SEND_ERROR;
1220
0
      }
1221
0
      else {
1222
        /* Send the DATA command */
1223
0
        result = Curl_pp_sendf(data, &smtpc->pp, "%s", "DATA");
1224
1225
0
        if(!result)
1226
0
          smtp_state(data, smtpc, SMTP_DATA);
1227
0
      }
1228
0
    }
1229
0
  }
1230
1231
0
  return result;
1232
0
}
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
0
{
1240
0
  CURLcode result = CURLE_OK;
1241
0
  (void)instate; /* no use for this yet */
1242
1243
0
  if(smtpcode != 354) {
1244
0
    failf(data, "DATA failed: %d", smtpcode);
1245
0
    result = CURLE_SEND_ERROR;
1246
0
  }
1247
0
  else {
1248
    /* Set the progress upload size */
1249
0
    Curl_pgrsSetUploadSize(data, data->state.infilesize);
1250
1251
    /* SMTP upload */
1252
0
    Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
1253
1254
    /* End of DO phase */
1255
0
    smtp_state(data, smtpc, SMTP_STOP);
1256
0
  }
1257
1258
0
  return result;
1259
0
}
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
0
{
1268
0
  CURLcode result = CURLE_OK;
1269
1270
0
  (void)instate; /* no use for this yet */
1271
1272
0
  if(smtpcode != 250)
1273
0
    result = CURLE_WEIRD_SERVER_REPLY;
1274
1275
  /* End of DONE phase */
1276
0
  smtp_state(data, smtpc, SMTP_STOP);
1277
1278
0
  return result;
1279
0
}
1280
1281
static CURLcode smtp_pp_statemachine(struct Curl_easy *data,
1282
                                     struct connectdata *conn)
1283
0
{
1284
0
  CURLcode result = CURLE_OK;
1285
0
  int smtpcode;
1286
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1287
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1288
0
  size_t nread = 0;
1289
1290
0
  if(!smtpc || !smtp)
1291
0
    return CURLE_FAILED_INIT;
1292
1293
  /* Busy upgrading the connection; right now all I/O is SSL/TLS, not SMTP */
1294
0
upgrade_tls:
1295
0
  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
0
  if(smtpc->pp.sendleft)
1303
0
    return Curl_pp_flushsend(data, &smtpc->pp);
1304
1305
0
  do {
1306
    /* Read the response from the server */
1307
0
    result = Curl_pp_readresp(data, FIRSTSOCKET, &smtpc->pp,
1308
0
                              &smtpcode, &nread);
1309
0
    if(result)
1310
0
      return result;
1311
1312
    /* Store the latest response for later retrieval if necessary */
1313
0
    if(smtpc->state != SMTP_QUIT && smtpcode != 1)
1314
0
      data->info.httpcode = smtpcode;
1315
1316
0
    if(!smtpcode)
1317
0
      break;
1318
1319
    /* We have now received a full SMTP server response */
1320
0
    switch(smtpc->state) {
1321
0
    case SMTP_SERVERGREET:
1322
0
      result = smtp_state_servergreet_resp(data, smtpc,
1323
0
                                           smtpcode, smtpc->state);
1324
0
      break;
1325
1326
0
    case SMTP_EHLO:
1327
0
      result = smtp_state_ehlo_resp(data, smtpc, smtpcode, smtpc->state);
1328
0
      break;
1329
1330
0
    case SMTP_HELO:
1331
0
      result = smtp_state_helo_resp(data, smtpc, smtpcode, smtpc->state);
1332
0
      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
0
    case SMTP_AUTH:
1343
0
      result = smtp_state_auth_resp(data, smtpc, smtpcode, smtpc->state);
1344
0
      break;
1345
1346
0
    case SMTP_COMMAND:
1347
0
      result = smtp_state_command_resp(data, smtpc, smtp,
1348
0
                                       smtpcode, smtpc->state);
1349
0
      break;
1350
1351
0
    case SMTP_MAIL:
1352
0
      result = smtp_state_mail_resp(data, smtpc, smtp, smtpcode, smtpc->state);
1353
0
      break;
1354
1355
0
    case SMTP_RCPT:
1356
0
      result = smtp_state_rcpt_resp(data, smtpc, smtp, smtpcode, smtpc->state);
1357
0
      break;
1358
1359
0
    case SMTP_DATA:
1360
0
      result = smtp_state_data_resp(data, smtpc, smtpcode, smtpc->state);
1361
0
      break;
1362
1363
0
    case SMTP_POSTDATA:
1364
0
      result = smtp_state_postdata_resp(data, smtpc, smtpcode, smtpc->state);
1365
0
      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
0
    }
1373
0
  } while(!result && smtpc->state != SMTP_STOP &&
1374
0
          Curl_pp_moredata(&smtpc->pp));
1375
1376
0
  return result;
1377
0
}
1378
1379
/* Called repeatedly until done from multi.c */
1380
static CURLcode smtp_multi_statemach(struct Curl_easy *data, bool *done)
1381
0
{
1382
0
  CURLcode result = CURLE_OK;
1383
0
  struct smtp_conn *smtpc =
1384
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1385
1386
0
  *done = FALSE;
1387
0
  if(!smtpc)
1388
0
    return CURLE_FAILED_INIT;
1389
1390
0
  result = Curl_pp_statemach(data, &smtpc->pp, FALSE, FALSE);
1391
0
  *done = (smtpc->state == SMTP_STOP);
1392
0
  return result;
1393
0
}
1394
1395
static CURLcode smtp_block_statemach(struct Curl_easy *data,
1396
                                     struct smtp_conn *smtpc,
1397
                                     bool disconnecting)
1398
0
{
1399
0
  CURLcode result = CURLE_OK;
1400
1401
0
  while(smtpc->state != SMTP_STOP && !result)
1402
0
    result = Curl_pp_statemach(data, &smtpc->pp, TRUE, disconnecting);
1403
1404
0
  return result;
1405
0
}
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
0
{
1411
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1412
0
  return smtpc ?
1413
0
         Curl_pp_getsock(data, &smtpc->pp, socks) : GETSOCK_BLANK;
1414
0
}
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
0
{
1428
0
  struct smtp_conn *smtpc =
1429
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1430
0
  CURLcode result = CURLE_OK;
1431
1432
0
  *done = FALSE; /* default to not done yet */
1433
0
  if(!smtpc)
1434
0
    return CURLE_FAILED_INIT;
1435
1436
  /* We always support persistent connections in SMTP */
1437
0
  connkeep(data->conn, "SMTP default");
1438
1439
0
  PINGPONG_SETUP(&smtpc->pp, smtp_pp_statemachine, smtp_endofresp);
1440
1441
  /* Initialize the SASL storage */
1442
0
  Curl_sasl_init(&smtpc->sasl, data, &saslsmtp);
1443
1444
  /* Initialise the pingpong layer */
1445
0
  Curl_pp_init(&smtpc->pp);
1446
1447
  /* Parse the URL options */
1448
0
  result = smtp_parse_url_options(data->conn, smtpc);
1449
0
  if(result)
1450
0
    return result;
1451
1452
  /* Parse the URL path */
1453
0
  result = smtp_parse_url_path(data, smtpc);
1454
0
  if(result)
1455
0
    return result;
1456
1457
  /* Start off waiting for the server greeting response */
1458
0
  smtp_state(data, smtpc, SMTP_SERVERGREET);
1459
1460
0
  result = smtp_multi_statemach(data, done);
1461
1462
0
  return result;
1463
0
}
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
0
{
1477
0
  struct smtp_conn *smtpc =
1478
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1479
0
  CURLcode result = CURLE_OK;
1480
0
  struct connectdata *conn = data->conn;
1481
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1482
1483
0
  (void)premature;
1484
1485
0
  if(!smtpc)
1486
0
    return CURLE_FAILED_INIT;
1487
0
  if(!smtp)
1488
0
    return CURLE_OK;
1489
1490
  /* Cleanup our per-request based variables */
1491
0
  Curl_safefree(smtp->custom);
1492
1493
0
  if(status) {
1494
0
    connclose(conn, "SMTP done with bad status"); /* marked for closure */
1495
0
    result = status;         /* use the already set error code */
1496
0
  }
1497
0
  else if(!data->set.connect_only && data->set.mail_rcpt &&
1498
0
          (data->state.upload || IS_MIME_POST(data))) {
1499
1500
0
    smtp_state(data, smtpc, SMTP_POSTDATA);
1501
1502
    /* Run the state-machine */
1503
0
    result = smtp_block_statemach(data, smtpc, FALSE);
1504
0
  }
1505
1506
  /* Clear the transfer mode for the next request */
1507
0
  smtp->transfer = PPTRANSFER_BODY;
1508
0
  CURL_TRC_SMTP(data, "smtp_done(status=%d, premature=%d) -> %d",
1509
0
                status, premature, result);
1510
0
  return result;
1511
0
}
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
0
{
1526
  /* This is SMTP and no proxy */
1527
0
  CURLcode result = CURLE_OK;
1528
1529
0
  CURL_TRC_SMTP(data, "smtp_perform(), start");
1530
1531
0
  if(data->req.no_body) {
1532
    /* Requested no body means no transfer */
1533
0
    smtp->transfer = PPTRANSFER_INFO;
1534
0
  }
1535
1536
0
  *dophase_done = FALSE; /* not done yet */
1537
1538
  /* Store the first recipient (or NULL if not specified) */
1539
0
  smtp->rcpt = data->set.mail_rcpt;
1540
1541
  /* Track of whether we have successfully sent at least one RCPT TO command */
1542
0
  smtp->rcpt_had_ok = FALSE;
1543
1544
  /* Track of the last error we have received by sending RCPT TO command */
1545
0
  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
0
  smtp->trailing_crlf = TRUE;
1550
0
  smtp->eob = 2;
1551
1552
  /* Start the first command in the DO phase */
1553
0
  if((data->state.upload || IS_MIME_POST(data)) && data->set.mail_rcpt)
1554
    /* MAIL transfer */
1555
0
    result = smtp_perform_mail(data, smtpc, smtp);
1556
0
  else
1557
    /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */
1558
0
    result = smtp_perform_command(data, smtpc, smtp);
1559
1560
0
  if(result)
1561
0
    goto out;
1562
1563
  /* Run the state-machine */
1564
0
  result = smtp_multi_statemach(data, dophase_done);
1565
1566
0
  *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
1567
1568
0
out:
1569
0
  CURL_TRC_SMTP(data, "smtp_perform() -> %d, connected=%d, done=%d",
1570
0
                result, *connected, *dophase_done);
1571
0
  return result;
1572
0
}
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
0
{
1585
0
  struct smtp_conn *smtpc =
1586
0
    Curl_conn_meta_get(data->conn, CURL_META_SMTP_CONN);
1587
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1588
0
  CURLcode result = CURLE_OK;
1589
1590
0
  DEBUGASSERT(data);
1591
0
  DEBUGASSERT(data->conn);
1592
0
  *done = FALSE; /* default to false */
1593
0
  if(!smtpc || !smtp)
1594
0
    return CURLE_FAILED_INIT;
1595
1596
  /* Parse the custom request */
1597
0
  result = smtp_parse_custom_request(data, smtp);
1598
0
  if(result)
1599
0
    return result;
1600
1601
0
  result = smtp_regular_transfer(data, smtpc, smtp, done);
1602
0
  CURL_TRC_SMTP(data, "smtp_do() -> %d, done=%d", result, *done);
1603
0
  return result;
1604
0
}
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
0
{
1617
0
  struct smtp_conn *smtpc = Curl_conn_meta_get(conn, CURL_META_SMTP_CONN);
1618
1619
0
  (void)data;
1620
0
  if(!smtpc)
1621
0
    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
0
  if(!dead_connection && conn->bits.protoconnstart &&
1628
0
     !Curl_pp_needs_flush(data, &smtpc->pp)) {
1629
0
    if(!smtp_perform_quit(data, smtpc))
1630
0
      (void)smtp_block_statemach(data, smtpc, TRUE); /* ignore on QUIT */
1631
0
  }
1632
1633
0
  CURL_TRC_SMTP(data, "smtp_disconnect(), finished");
1634
0
  return CURLE_OK;
1635
0
}
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
0
{
1642
0
  (void)connected;
1643
1644
0
  if(smtp->transfer != PPTRANSFER_BODY)
1645
    /* no data to transfer */
1646
0
    Curl_xfer_setup_nop(data);
1647
1648
0
  return CURLE_OK;
1649
0
}
1650
1651
/* Called from multi.c while DOing */
1652
static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
1653
0
{
1654
0
  struct SMTP *smtp = Curl_meta_get(data, CURL_META_SMTP_EASY);
1655
0
  CURLcode result;
1656
1657
0
  if(!smtp)
1658
0
    return CURLE_FAILED_INIT;
1659
0
  result = smtp_multi_statemach(data, dophase_done);
1660
0
  if(result)
1661
0
    DEBUGF(infof(data, "DO phase failed"));
1662
0
  else if(*dophase_done) {
1663
0
    result = smtp_dophase_done(data, smtp, FALSE /* not connected */);
1664
1665
0
    DEBUGF(infof(data, "DO phase is complete"));
1666
0
  }
1667
1668
0
  CURL_TRC_SMTP(data, "smtp_doing() -> %d, done=%d", result, *dophase_done);
1669
0
  return result;
1670
0
}
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
0
{
1686
0
  CURLcode result = CURLE_OK;
1687
0
  bool connected = FALSE;
1688
1689
  /* Make sure size is unknown at this point */
1690
0
  data->req.size = -1;
1691
1692
  /* Set the progress data */
1693
0
  Curl_pgrsSetUploadCounter(data, 0);
1694
0
  Curl_pgrsSetDownloadCounter(data, 0);
1695
0
  Curl_pgrsSetUploadSize(data, -1);
1696
0
  Curl_pgrsSetDownloadSize(data, -1);
1697
1698
  /* Carry out the perform */
1699
0
  result = smtp_perform(data, smtpc, smtp, &connected, dophase_done);
1700
1701
  /* Perform post DO phase operations if necessary */
1702
0
  if(!result && *dophase_done)
1703
0
    result = smtp_dophase_done(data, smtp, connected);
1704
1705
0
  CURL_TRC_SMTP(data, "smtp_regular_transfer() -> %d, done=%d",
1706
0
                result, *dophase_done);
1707
0
  return result;
1708
0
}
1709
1710
1711
static void smtp_easy_dtor(void *key, size_t klen, void *entry)
1712
0
{
1713
0
  struct SMTP *smtp = entry;
1714
0
  (void)key;
1715
0
  (void)klen;
1716
0
  free(smtp);
1717
0
}
1718
1719
static void smtp_conn_dtor(void *key, size_t klen, void *entry)
1720
0
{
1721
0
  struct smtp_conn *smtpc = entry;
1722
0
  (void)key;
1723
0
  (void)klen;
1724
0
  Curl_pp_disconnect(&smtpc->pp);
1725
0
  Curl_safefree(smtpc->domain);
1726
0
  free(smtpc);
1727
0
}
1728
1729
static CURLcode smtp_setup_connection(struct Curl_easy *data,
1730
                                      struct connectdata *conn)
1731
0
{
1732
0
  struct smtp_conn *smtpc;
1733
0
  struct SMTP *smtp;
1734
0
  CURLcode result = CURLE_OK;
1735
1736
0
  smtpc = calloc(1, sizeof(*smtpc));
1737
0
  if(!smtpc ||
1738
0
     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
0
  smtp = calloc(1, sizeof(*smtp));
1744
0
  if(!smtp ||
1745
0
     Curl_meta_set(data, CURL_META_SMTP_EASY, smtp, smtp_easy_dtor))
1746
0
    result = CURLE_OUT_OF_MEMORY;
1747
1748
0
out:
1749
0
  CURL_TRC_SMTP(data, "smtp_setup_connection() -> %d", result);
1750
0
  return result;
1751
0
}
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
0
{
1762
0
  CURLcode result = CURLE_OK;
1763
0
  const char *ptr = conn->options;
1764
1765
0
  while(!result && ptr && *ptr) {
1766
0
    const char *key = ptr;
1767
0
    const char *value;
1768
1769
0
    while(*ptr && *ptr != '=')
1770
0
      ptr++;
1771
1772
0
    value = ptr + 1;
1773
1774
0
    while(*ptr && *ptr != ';')
1775
0
      ptr++;
1776
1777
0
    if(curl_strnequal(key, "AUTH=", 5))
1778
0
      result = Curl_sasl_parse_url_auth_option(&smtpc->sasl,
1779
0
                                               value, ptr - value);
1780
0
    else
1781
0
      result = CURLE_URL_MALFORMAT;
1782
1783
0
    if(*ptr == ';')
1784
0
      ptr++;
1785
0
  }
1786
1787
0
  return result;
1788
0
}
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
0
{
1799
  /* The SMTP struct is already initialised in smtp_connect() */
1800
0
  const char *path = &data->state.up.path[1]; /* skip leading path */
1801
0
  char localhost[HOSTNAME_MAX + 1];
1802
1803
  /* Calculate the path if necessary */
1804
0
  if(!*path) {
1805
0
    if(!Curl_gethostname(localhost, sizeof(localhost)))
1806
0
      path = localhost;
1807
0
    else
1808
0
      path = "localhost";
1809
0
  }
1810
1811
  /* URL decode the path and use it as the domain in our EHLO */
1812
0
  return Curl_urldecode(path, 0, &smtpc->domain, NULL, REJECT_CTRL);
1813
0
}
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
0
{
1824
0
  CURLcode result = CURLE_OK;
1825
0
  const char *custom = data->set.str[STRING_CUSTOMREQUEST];
1826
1827
  /* URL decode the custom request */
1828
0
  if(custom)
1829
0
    result = Curl_urldecode(custom, 0, &smtp->custom, NULL, REJECT_CTRL);
1830
1831
0
  return result;
1832
0
}
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
0
{
1871
0
  CURLcode result = CURLE_OK;
1872
0
  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
0
  char *dup = strdup(fqma[0] == '<' ? fqma + 1  : fqma);
1877
0
  if(!dup)
1878
0
    return CURLE_OUT_OF_MEMORY;
1879
1880
0
  length = strlen(dup);
1881
0
  if(length) {
1882
0
    if(dup[length - 1] == '>')
1883
0
      dup[length - 1] = '\0';
1884
0
  }
1885
1886
  /* Extract the hostname from the address (if we can) */
1887
0
  host->name = strpbrk(dup, "@");
1888
0
  if(host->name) {
1889
0
    *host->name = '\0';
1890
0
    host->name = host->name + 1;
1891
1892
    /* Attempt to convert the hostname to IDN ACE */
1893
0
    (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
0
  }
1899
1900
  /* Extract the local address from the mailbox */
1901
0
  *address = dup;
1902
1903
0
  return result;
1904
0
}
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
0
{
1919
0
  struct cr_eob_ctx *ctx = reader->ctx;
1920
0
  (void)data;
1921
  /* The first char we read is the first on a line, as if we had
1922
   * read CRLF just before */
1923
0
  ctx->n_eob = 2;
1924
0
  Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
1925
0
  return CURLE_OK;
1926
0
}
1927
1928
static void cr_eob_close(struct Curl_easy *data, struct Curl_creader *reader)
1929
0
{
1930
0
  struct cr_eob_ctx *ctx = reader->ctx;
1931
0
  (void)data;
1932
0
  Curl_bufq_free(&ctx->buf);
1933
0
}
1934
1935
/* this is the 5-bytes End-Of-Body marker for SMTP */
1936
0
#define SMTP_EOB "\r\n.\r\n"
1937
0
#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
0
{
1945
0
  struct cr_eob_ctx *ctx = reader->ctx;
1946
0
  CURLcode result = CURLE_OK;
1947
0
  size_t nread, i, start, n;
1948
0
  bool eos;
1949
1950
0
  if(!ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
1951
    /* Get more and convert it when needed */
1952
0
    result = Curl_creader_read(data, reader->next, buf, blen, &nread, &eos);
1953
0
    if(result)
1954
0
      return result;
1955
1956
0
    ctx->read_eos = eos;
1957
0
    if(nread) {
1958
0
      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
0
        *pnread = nread;
1961
0
        *peos = FALSE;
1962
0
        return CURLE_OK;
1963
0
      }
1964
      /* scan for EOB (continuation) and convert */
1965
0
      for(i = start = 0; i < nread; ++i) {
1966
0
        if(ctx->n_eob >= SMTP_EOB_FIND_LEN) {
1967
          /* matched the EOB prefix and seeing additional char, add '.' */
1968
0
          result = Curl_bufq_cwrite(&ctx->buf, buf + start, i - start, &n);
1969
0
          if(result)
1970
0
            return result;
1971
0
          result = Curl_bufq_cwrite(&ctx->buf, ".", 1, &n);
1972
0
          if(result)
1973
0
            return result;
1974
0
          ctx->n_eob = 0;
1975
0
          start = i;
1976
0
          if(data->state.infilesize > 0)
1977
0
            data->state.infilesize++;
1978
0
        }
1979
1980
0
        if(buf[i] != SMTP_EOB[ctx->n_eob])
1981
0
          ctx->n_eob = 0;
1982
1983
0
        if(buf[i] == SMTP_EOB[ctx->n_eob]) {
1984
          /* matching another char of the EOB */
1985
0
          ++ctx->n_eob;
1986
0
        }
1987
0
      }
1988
1989
      /* add any remainder to buf */
1990
0
      if(start < nread) {
1991
0
        result = Curl_bufq_cwrite(&ctx->buf, buf + start, nread - start, &n);
1992
0
        if(result)
1993
0
          return result;
1994
0
      }
1995
0
    }
1996
1997
0
    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
0
      const char *eob = SMTP_EOB;
2002
0
      switch(ctx->n_eob) {
2003
0
        case 2:
2004
          /* seen a CRLF at the end, just add the remainder */
2005
0
          eob = &SMTP_EOB[2];
2006
0
          break;
2007
0
        case 3:
2008
          /* ended with '\r\n.', we should escpe the last '.' */
2009
0
          eob = "." SMTP_EOB;
2010
0
          break;
2011
0
        default:
2012
0
          break;
2013
0
      }
2014
0
      result = Curl_bufq_cwrite(&ctx->buf, eob, strlen(eob), &n);
2015
0
      if(result)
2016
0
        return result;
2017
0
    }
2018
0
  }
2019
2020
0
  *peos = FALSE;
2021
0
  if(!Curl_bufq_is_empty(&ctx->buf)) {
2022
0
    result = Curl_bufq_cread(&ctx->buf, buf, blen, pnread);
2023
0
  }
2024
0
  else
2025
0
    *pnread = 0;
2026
2027
0
  if(ctx->read_eos && Curl_bufq_is_empty(&ctx->buf)) {
2028
    /* no more data, read all, done. */
2029
0
    ctx->eos = TRUE;
2030
0
  }
2031
0
  *peos = ctx->eos;
2032
0
  DEBUGF(infof(data, "cr_eob_read(%zu) -> %d, %zd, %d",
2033
0
         blen, result, *pnread, *peos));
2034
0
  return result;
2035
0
}
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
0
{
2063
0
  struct Curl_creader *reader = NULL;
2064
0
  CURLcode result;
2065
2066
0
  result = Curl_creader_create(&reader, data, &cr_eob,
2067
0
                               CURL_CR_CONTENT_ENCODE);
2068
0
  if(!result)
2069
0
    result = Curl_creader_add(data, reader);
2070
2071
0
  if(result && reader)
2072
0
    Curl_creader_free(data, reader);
2073
0
  return result;
2074
0
}
2075
2076
#endif /* CURL_DISABLE_SMTP */