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