Coverage Report

Created: 2023-12-08 06:48

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