diff options
author | deshevoy <deshevoy@yandex-team.ru> | 2022-02-10 16:46:57 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:57 +0300 |
commit | 28148f76dbfcc644d96427d41c92f36cbf2fdc6e (patch) | |
tree | b83306b6e37edeea782e9eed673d89286c4fef35 /contrib/libs/curl/lib/smtp.c | |
parent | e988f30484abe5fdeedcc7a5d3c226c01a21800c (diff) | |
download | ydb-28148f76dbfcc644d96427d41c92f36cbf2fdc6e.tar.gz |
Restoring authorship annotation for <deshevoy@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/curl/lib/smtp.c')
-rw-r--r-- | contrib/libs/curl/lib/smtp.c | 1048 |
1 files changed, 524 insertions, 524 deletions
diff --git a/contrib/libs/curl/lib/smtp.c b/contrib/libs/curl/lib/smtp.c index a9dd81e8f2..509d802f1c 100644 --- a/contrib/libs/curl/lib/smtp.c +++ b/contrib/libs/curl/lib/smtp.c @@ -24,7 +24,7 @@ * RFC3207 SMTP over TLS * RFC4422 Simple Authentication and Security Layer (SASL) * RFC4616 PLAIN authentication - * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism + * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism * RFC4954 SMTP Authentication * RFC5321 SMTP protocol * RFC5890 Internationalized Domain Names for Applications (IDNA) @@ -32,8 +32,8 @@ * RFC6532 Internationalized Email Headers * RFC6749 OAuth 2.0 Authorization Framework * RFC8314 Use of TLS for Email Submission and Access - * Draft SMTP URL Interface <draft-earhart-url-smtp-00.txt> - * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt> + * Draft SMTP URL Interface <draft-earhart-url-smtp-00.txt> + * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt> * ***************************************************************************/ @@ -71,12 +71,12 @@ #include "transfer.h" #include "escape.h" #include "http.h" /* for HTTP proxy tunnel stuff */ -#include "mime.h" +#include "mime.h" #include "socks.h" #include "smtp.h" #include "strtoofft.h" -#include "strcase.h" -#include "vtls/vtls.h" +#include "strcase.h" +#include "vtls/vtls.h" #include "connect.h" #include "strerror.h" #include "select.h" @@ -85,8 +85,8 @@ #include "curl_gethostname.h" #include "curl_sasl.h" #include "warnless.h" -/* The last 3 #include files should be in this order */ -#include "curl_printf.h" +/* The last 3 #include files should be in this order */ +#include "curl_printf.h" #include "curl_memory.h" #include "memdebug.h" @@ -103,13 +103,13 @@ static CURLcode smtp_doing(struct connectdata *conn, bool *dophase_done); static CURLcode smtp_setup_connection(struct connectdata *conn); static CURLcode smtp_parse_url_options(struct connectdata *conn); static CURLcode smtp_parse_url_path(struct connectdata *conn); -static CURLcode smtp_parse_custom_request(struct connectdata *conn); +static CURLcode smtp_parse_custom_request(struct connectdata *conn); static CURLcode smtp_parse_address(struct connectdata *conn, const char *fqma, char **address, struct hostname *host); -static CURLcode smtp_perform_auth(struct connectdata *conn, const char *mech, - const char *initresp); -static CURLcode smtp_continue_auth(struct connectdata *conn, const char *resp); -static void smtp_get_message(char *buffer, char **outptr); +static CURLcode smtp_perform_auth(struct connectdata *conn, const char *mech, + const char *initresp); +static CURLcode smtp_continue_auth(struct connectdata *conn, const char *resp); +static void smtp_get_message(char *buffer, char **outptr); /* * SMTP protocol handler. @@ -130,12 +130,12 @@ const struct Curl_handler Curl_handler_smtp = { ZERO_NULL, /* perform_getsock */ smtp_disconnect, /* disconnect */ ZERO_NULL, /* readwrite */ - ZERO_NULL, /* connection_check */ + ZERO_NULL, /* connection_check */ PORT_SMTP, /* defport */ CURLPROTO_SMTP, /* protocol */ CURLPROTO_SMTP, /* family */ - PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */ - PROTOPT_URLOPTIONS + PROTOPT_CLOSEACTION | PROTOPT_NOURLQUERY | /* flags */ + PROTOPT_URLOPTIONS }; #ifdef USE_SSL @@ -158,33 +158,33 @@ const struct Curl_handler Curl_handler_smtps = { ZERO_NULL, /* perform_getsock */ smtp_disconnect, /* disconnect */ ZERO_NULL, /* readwrite */ - ZERO_NULL, /* connection_check */ + ZERO_NULL, /* connection_check */ PORT_SMTPS, /* defport */ - CURLPROTO_SMTPS, /* protocol */ + CURLPROTO_SMTPS, /* protocol */ CURLPROTO_SMTP, /* family */ PROTOPT_CLOSEACTION | PROTOPT_SSL - | PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS /* flags */ + | PROTOPT_NOURLQUERY | PROTOPT_URLOPTIONS /* flags */ }; #endif -/* SASL parameters for the smtp protocol */ -static const struct SASLproto saslsmtp = { - "smtp", /* The service name */ - 334, /* Code received when continuation is expected */ - 235, /* Code to receive upon authentication success */ - 512 - 8, /* Maximum initial response length (no max) */ - smtp_perform_auth, /* Send authentication command */ - smtp_continue_auth, /* Send authentication continuation */ - smtp_get_message /* Get SASL response message */ +/* SASL parameters for the smtp protocol */ +static const struct SASLproto saslsmtp = { + "smtp", /* The service name */ + 334, /* Code received when continuation is expected */ + 235, /* Code to receive upon authentication success */ + 512 - 8, /* Maximum initial response length (no max) */ + smtp_perform_auth, /* Send authentication command */ + smtp_continue_auth, /* Send authentication continuation */ + smtp_get_message /* Get SASL response message */ }; #ifdef USE_SSL static void smtp_to_smtps(struct connectdata *conn) { - /* Change the connection handler */ + /* Change the connection handler */ conn->handler = &Curl_handler_smtps; - - /* Set the connection's upgraded to TLS flag */ + + /* Set the connection's upgraded to TLS flag */ conn->bits.tls_upgraded = TRUE; } #else @@ -193,7 +193,7 @@ static void smtp_to_smtps(struct connectdata *conn) /*********************************************************************** * - * smtp_endofresp() + * smtp_endofresp() * * Checks for an ending SMTP status code at the start of the given string, but * also detects various capabilities from the EHLO response including the @@ -205,70 +205,70 @@ static bool smtp_endofresp(struct connectdata *conn, char *line, size_t len, struct smtp_conn *smtpc = &conn->proto.smtpc; bool result = FALSE; - /* Nothing for us */ + /* Nothing for us */ if(len < 4 || !ISDIGIT(line[0]) || !ISDIGIT(line[1]) || !ISDIGIT(line[2])) - return FALSE; + return FALSE; - /* Do we have a command response? This should be the response code followed - by a space and optionally some text as per RFC-5321 and as outlined in - Section 4. Examples of RFC-4954 but some e-mail servers ignore this and - only send the response code instead as per Section 4.2. */ - if(line[3] == ' ' || len == 5) { + /* Do we have a command response? This should be the response code followed + by a space and optionally some text as per RFC-5321 and as outlined in + Section 4. Examples of RFC-4954 but some e-mail servers ignore this and + only send the response code instead as per Section 4.2. */ + if(line[3] == ' ' || len == 5) { char tmpline[6]; - result = TRUE; + result = TRUE; memset(tmpline, '\0', sizeof(tmpline)); memcpy(tmpline, line, (len == 5 ? 5 : 3)); *resp = curlx_sltosi(strtol(tmpline, NULL, 10)); - /* Make sure real server never sends internal value */ - if(*resp == 1) - *resp = 0; - } - /* Do we have a multiline (continuation) response? */ - else if(line[3] == '-' && - (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) { - result = TRUE; - *resp = 1; /* Internal response code */ - } - - return result; -} - -/*********************************************************************** - * - * smtp_get_message() - * - * Gets the authentication message from the response buffer. - */ -static void smtp_get_message(char *buffer, char **outptr) -{ - size_t len = strlen(buffer); - char *message = NULL; - - if(len > 4) { - /* Find the start of the message */ - len -= 4; - for(message = buffer + 4; *message == ' ' || *message == '\t'; - message++, len--) - ; - - /* Find the end of the message */ - for(; len--;) - if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' && - message[len] != '\t') - break; - - /* Terminate the message */ - if(++len) { - message[len] = '\0'; + /* Make sure real server never sends internal value */ + if(*resp == 1) + *resp = 0; + } + /* Do we have a multiline (continuation) response? */ + else if(line[3] == '-' && + (smtpc->state == SMTP_EHLO || smtpc->state == SMTP_COMMAND)) { + result = TRUE; + *resp = 1; /* Internal response code */ + } + + return result; +} + +/*********************************************************************** + * + * smtp_get_message() + * + * Gets the authentication message from the response buffer. + */ +static void smtp_get_message(char *buffer, char **outptr) +{ + size_t len = strlen(buffer); + char *message = NULL; + + if(len > 4) { + /* Find the start of the message */ + len -= 4; + for(message = buffer + 4; *message == ' ' || *message == '\t'; + message++, len--) + ; + + /* Find the end of the message */ + for(; len--;) + if(message[len] != '\r' && message[len] != '\n' && message[len] != ' ' && + message[len] != '\t') + break; + + /* Terminate the message */ + if(++len) { + message[len] = '\0'; } } - else - /* junk input => zero length output */ - message = &buffer[len]; + else + /* junk input => zero length output */ + message = &buffer[len]; - *outptr = message; + *outptr = message; } /*********************************************************************** @@ -289,8 +289,8 @@ static void state(struct connectdata *conn, smtpstate newstate) "HELO", "STARTTLS", "UPGRADETLS", - "AUTH", - "COMMAND", + "AUTH", + "COMMAND", "MAIL", "RCPT", "DATA", @@ -319,11 +319,11 @@ static CURLcode smtp_perform_ehlo(struct connectdata *conn) CURLcode result = CURLE_OK; struct smtp_conn *smtpc = &conn->proto.smtpc; - smtpc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanism yet */ - smtpc->sasl.authused = SASL_AUTH_NONE; /* Clear the authentication mechanism - used for esmtp connections */ - smtpc->tls_supported = FALSE; /* Clear the TLS capability */ - smtpc->auth_supported = FALSE; /* Clear the AUTH capability */ + smtpc->sasl.authmechs = SASL_AUTH_NONE; /* No known auth. mechanism yet */ + smtpc->sasl.authused = SASL_AUTH_NONE; /* Clear the authentication mechanism + used for esmtp connections */ + smtpc->tls_supported = FALSE; /* Clear the TLS capability */ + smtpc->auth_supported = FALSE; /* Clear the AUTH capability */ /* Send the EHLO command */ result = Curl_pp_sendf(&smtpc->pp, "EHLO %s", smtpc->domain); @@ -345,8 +345,8 @@ static CURLcode smtp_perform_helo(struct connectdata *conn) CURLcode result = CURLE_OK; struct smtp_conn *smtpc = &conn->proto.smtpc; - smtpc->sasl.authused = SASL_AUTH_NONE; /* No authentication mechanism used - in smtp connections */ + smtpc->sasl.authused = SASL_AUTH_NONE; /* No authentication mechanism used + in smtp connections */ /* Send the HELO command */ result = Curl_pp_sendf(&smtpc->pp, "HELO %s", smtpc->domain); @@ -402,70 +402,70 @@ static CURLcode smtp_perform_upgrade_tls(struct connectdata *conn) /*********************************************************************** * - * smtp_perform_auth() + * smtp_perform_auth() * - * Sends an AUTH command allowing the client to login with the given SASL - * authentication mechanism. + * Sends an AUTH command allowing the client to login with the given SASL + * authentication mechanism. */ -static CURLcode smtp_perform_auth(struct connectdata *conn, - const char *mech, - const char *initresp) +static CURLcode smtp_perform_auth(struct connectdata *conn, + const char *mech, + const char *initresp) { CURLcode result = CURLE_OK; struct smtp_conn *smtpc = &conn->proto.smtpc; - if(initresp) { /* AUTH <mech> ...<crlf> */ - /* Send the AUTH command with the initial response */ - result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp); + if(initresp) { /* AUTH <mech> ...<crlf> */ + /* Send the AUTH command with the initial response */ + result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp); } - else { - /* Send the AUTH command */ - result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech); + else { + /* Send the AUTH command */ + result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech); } - return result; -} - -/*********************************************************************** - * - * smtp_continue_auth() - * - * Sends SASL continuation data or cancellation. - */ -static CURLcode smtp_continue_auth(struct connectdata *conn, const char *resp) -{ - struct smtp_conn *smtpc = &conn->proto.smtpc; - - return Curl_pp_sendf(&smtpc->pp, "%s", resp); -} - -/*********************************************************************** - * - * smtp_perform_authentication() - * - * Initiates the authentication sequence, with the appropriate SASL - * authentication mechanism. - */ -static CURLcode smtp_perform_authentication(struct connectdata *conn) -{ - CURLcode result = CURLE_OK; - struct smtp_conn *smtpc = &conn->proto.smtpc; - saslprogress progress; - - /* Check we have enough data to authenticate with, and the - server supports authentiation, and end the connect phase if not */ - if(!smtpc->auth_supported || - !Curl_sasl_can_authenticate(&smtpc->sasl, conn)) { - state(conn, SMTP_STOP); - return result; + return result; +} + +/*********************************************************************** + * + * smtp_continue_auth() + * + * Sends SASL continuation data or cancellation. + */ +static CURLcode smtp_continue_auth(struct connectdata *conn, const char *resp) +{ + struct smtp_conn *smtpc = &conn->proto.smtpc; + + return Curl_pp_sendf(&smtpc->pp, "%s", resp); +} + +/*********************************************************************** + * + * smtp_perform_authentication() + * + * Initiates the authentication sequence, with the appropriate SASL + * authentication mechanism. + */ +static CURLcode smtp_perform_authentication(struct connectdata *conn) +{ + CURLcode result = CURLE_OK; + struct smtp_conn *smtpc = &conn->proto.smtpc; + saslprogress progress; + + /* Check we have enough data to authenticate with, and the + server supports authentiation, and end the connect phase if not */ + if(!smtpc->auth_supported || + !Curl_sasl_can_authenticate(&smtpc->sasl, conn)) { + state(conn, SMTP_STOP); + return result; } - /* Calculate the SASL login details */ - result = Curl_sasl_start(&smtpc->sasl, conn, FALSE, &progress); - + /* Calculate the SASL login details */ + result = Curl_sasl_start(&smtpc->sasl, conn, FALSE, &progress); + if(!result) { - if(progress == SASL_INPROGRESS) - state(conn, SMTP_AUTH); + if(progress == SASL_INPROGRESS) + state(conn, SMTP_AUTH); else { /* Other mechanisms not supported */ infof(conn->data, "No known authentication mechanisms supported!\n"); @@ -478,16 +478,16 @@ static CURLcode smtp_perform_authentication(struct connectdata *conn) /*********************************************************************** * - * smtp_perform_command() - * - * Sends a SMTP based command. - */ -static CURLcode smtp_perform_command(struct connectdata *conn) -{ - CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + * smtp_perform_command() + * + * Sends a SMTP based command. + */ +static CURLcode smtp_perform_command(struct connectdata *conn) +{ + CURLcode result = CURLE_OK; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; - + if(smtp->rcpt) { /* We notify the server we are sending UTF-8 data if a) it supports the SMTPUTF8 extension and b) The mailbox contains UTF-8 charaacters, in @@ -535,20 +535,20 @@ static CURLcode smtp_perform_command(struct connectdata *conn) utf8 ? " SMTPUTF8" : ""); } } - else + else /* Send the non-recipient based command such as HELP */ - result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", - smtp->custom && smtp->custom[0] != '\0' ? - smtp->custom : "HELP"); - - if(!result) - state(conn, SMTP_COMMAND); - - return result; -} - -/*********************************************************************** - * + result = Curl_pp_sendf(&conn->proto.smtpc.pp, "%s", + smtp->custom && smtp->custom[0] != '\0' ? + smtp->custom : "HELP"); + + if(!result) + state(conn, SMTP_COMMAND); + + return result; +} + +/*********************************************************************** + * * smtp_perform_mail() * * Sends an MAIL command to initiate the upload of a message. @@ -559,7 +559,7 @@ static CURLcode smtp_perform_mail(struct connectdata *conn) char *auth = NULL; char *size = NULL; CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; /* We notify the server we are sending UTF-8 data if a) it supports the SMTPUTF8 extension and b) The mailbox contains UTF-8 charaacters, in @@ -605,7 +605,7 @@ static CURLcode smtp_perform_mail(struct connectdata *conn) return CURLE_OUT_OF_MEMORY; /* Calculate the optional AUTH parameter */ - if(data->set.str[STRING_MAIL_AUTH] && conn->proto.smtpc.sasl.authused) { + if(data->set.str[STRING_MAIL_AUTH] && conn->proto.smtpc.sasl.authused) { if(data->set.str[STRING_MAIL_AUTH][0] != '\0') { char *address = NULL; struct hostname host = { NULL, NULL, NULL, NULL }; @@ -643,52 +643,52 @@ static CURLcode smtp_perform_mail(struct connectdata *conn) auth = strdup("<>"); if(!auth) { - free(from); + free(from); return CURLE_OUT_OF_MEMORY; } } - /* Prepare the mime data if some. */ - if(data->set.mimepost.kind != MIMEKIND_NONE) { - /* Use the whole structure as data. */ - data->set.mimepost.flags &= ~MIME_BODY_ONLY; - - /* Add external headers and mime version. */ - curl_mime_headers(&data->set.mimepost, data->set.headers, 0); - result = Curl_mime_prepare_headers(&data->set.mimepost, NULL, - NULL, MIMESTRATEGY_MAIL); - - if(!result) - if(!Curl_checkheaders(conn, "Mime-Version")) - result = Curl_mime_add_header(&data->set.mimepost.curlheaders, - "Mime-Version: 1.0"); - - /* Make sure we will read the entire mime structure. */ - if(!result) - result = Curl_mime_rewind(&data->set.mimepost); - - if(result) { - free(from); - free(auth); - - return result; - } - - data->state.infilesize = Curl_mime_size(&data->set.mimepost); - - /* Read from mime structure. */ - data->state.fread_func = (curl_read_callback) Curl_mime_read; - data->state.in = (void *) &data->set.mimepost; - } - + /* Prepare the mime data if some. */ + if(data->set.mimepost.kind != MIMEKIND_NONE) { + /* Use the whole structure as data. */ + data->set.mimepost.flags &= ~MIME_BODY_ONLY; + + /* Add external headers and mime version. */ + curl_mime_headers(&data->set.mimepost, data->set.headers, 0); + result = Curl_mime_prepare_headers(&data->set.mimepost, NULL, + NULL, MIMESTRATEGY_MAIL); + + if(!result) + if(!Curl_checkheaders(conn, "Mime-Version")) + result = Curl_mime_add_header(&data->set.mimepost.curlheaders, + "Mime-Version: 1.0"); + + /* Make sure we will read the entire mime structure. */ + if(!result) + result = Curl_mime_rewind(&data->set.mimepost); + + if(result) { + free(from); + free(auth); + + return result; + } + + data->state.infilesize = Curl_mime_size(&data->set.mimepost); + + /* Read from mime structure. */ + data->state.fread_func = (curl_read_callback) Curl_mime_read; + data->state.in = (void *) &data->set.mimepost; + } + /* Calculate the optional SIZE parameter */ - if(conn->proto.smtpc.size_supported && data->state.infilesize > 0) { - size = aprintf("%" CURL_FORMAT_CURL_OFF_T, data->state.infilesize); + if(conn->proto.smtpc.size_supported && data->state.infilesize > 0) { + size = aprintf("%" CURL_FORMAT_CURL_OFF_T, data->state.infilesize); if(!size) { - free(from); - free(auth); + free(from); + free(auth); return CURLE_OUT_OF_MEMORY; } @@ -722,9 +722,9 @@ static CURLcode smtp_perform_mail(struct connectdata *conn) utf8 ? " SMTPUTF8" /* Internationalised mailbox */ : ""); /* included in our envelope */ - free(from); - free(auth); - free(size); + free(from); + free(auth); + free(size); if(!result) state(conn, SMTP_MAIL); @@ -742,7 +742,7 @@ static CURLcode smtp_perform_mail(struct connectdata *conn) static CURLcode smtp_perform_rcpt_to(struct connectdata *conn) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; char *address = NULL; struct hostname host = { NULL, NULL, NULL, NULL }; @@ -758,7 +758,7 @@ static CURLcode smtp_perform_rcpt_to(struct connectdata *conn) if(host.name) result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:<%s@%s>", address, host.name); - else + else /* An invalid mailbox was provided but we'll simply let the server worry about that and reply with a 501 error */ result = Curl_pp_sendf(&conn->proto.smtpc.pp, "RCPT TO:<%s>", address); @@ -766,8 +766,8 @@ static CURLcode smtp_perform_rcpt_to(struct connectdata *conn) Curl_free_idnconverted_hostname(&host); free(address); - if(!result) - state(conn, SMTP_RCPT); + if(!result) + state(conn, SMTP_RCPT); return result; } @@ -795,13 +795,13 @@ static CURLcode smtp_state_servergreet_resp(struct connectdata *conn, smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; (void)instate; /* no use for this yet */ if(smtpcode/100 != 2) { failf(data, "Got unexpected smtp-server response: %d", smtpcode); - result = CURLE_WEIRD_SERVER_REPLY; + result = CURLE_WEIRD_SERVER_REPLY; } else result = smtp_perform_ehlo(conn); @@ -815,17 +815,17 @@ static CURLcode smtp_state_starttls_resp(struct connectdata *conn, smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; (void)instate; /* no use for this yet */ if(smtpcode != 220) { if(data->set.use_ssl != CURLUSESSL_TRY) { - failf(data, "STARTTLS denied, code %d", smtpcode); + failf(data, "STARTTLS denied, code %d", smtpcode); result = CURLE_USE_SSL_FAILED; } else - result = smtp_perform_authentication(conn); + result = smtp_perform_authentication(conn); } else result = smtp_perform_upgrade_tls(conn); @@ -838,15 +838,15 @@ static CURLcode smtp_state_ehlo_resp(struct connectdata *conn, int smtpcode, smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct smtp_conn *smtpc = &conn->proto.smtpc; - const char *line = data->state.buffer; - size_t len = strlen(line); + const char *line = data->state.buffer; + size_t len = strlen(line); (void)instate; /* no use for this yet */ - if(smtpcode/100 != 2 && smtpcode != 1) { - if(data->set.use_ssl <= CURLUSESSL_TRY || conn->ssl[FIRSTSOCKET].use) + if(smtpcode/100 != 2 && smtpcode != 1) { + if(data->set.use_ssl <= CURLUSESSL_TRY || conn->ssl[FIRSTSOCKET].use) result = smtp_perform_helo(conn); else { failf(data, "Remote access denied: %d", smtpcode); @@ -854,78 +854,78 @@ static CURLcode smtp_state_ehlo_resp(struct connectdata *conn, int smtpcode, } } else if(len >= 4) { - line += 4; - len -= 4; + line += 4; + len -= 4; - /* Does the server support the STARTTLS capability? */ - if(len >= 8 && !memcmp(line, "STARTTLS", 8)) - smtpc->tls_supported = TRUE; + /* Does the server support the STARTTLS capability? */ + if(len >= 8 && !memcmp(line, "STARTTLS", 8)) + smtpc->tls_supported = TRUE; - /* Does the server support the SIZE capability? */ - else if(len >= 4 && !memcmp(line, "SIZE", 4)) - smtpc->size_supported = TRUE; + /* Does the server support the SIZE capability? */ + else if(len >= 4 && !memcmp(line, "SIZE", 4)) + smtpc->size_supported = TRUE; /* Does the server support the UTF-8 capability? */ else if(len >= 8 && !memcmp(line, "SMTPUTF8", 8)) smtpc->utf8_supported = TRUE; - /* Does the server support authentication? */ - else if(len >= 5 && !memcmp(line, "AUTH ", 5)) { - smtpc->auth_supported = TRUE; + /* Does the server support authentication? */ + else if(len >= 5 && !memcmp(line, "AUTH ", 5)) { + smtpc->auth_supported = TRUE; - /* Advance past the AUTH keyword */ - line += 5; - len -= 5; + /* Advance past the AUTH keyword */ + line += 5; + len -= 5; - /* Loop through the data line */ - for(;;) { - size_t llen; - size_t wordlen; - unsigned int mechbit; + /* Loop through the data line */ + for(;;) { + size_t llen; + size_t wordlen; + unsigned int mechbit; - while(len && - (*line == ' ' || *line == '\t' || - *line == '\r' || *line == '\n')) { + while(len && + (*line == ' ' || *line == '\t' || + *line == '\r' || *line == '\n')) { - line++; - len--; - } + line++; + len--; + } - if(!len) - break; + if(!len) + break; - /* Extract the word */ - for(wordlen = 0; wordlen < len && line[wordlen] != ' ' && - line[wordlen] != '\t' && line[wordlen] != '\r' && - line[wordlen] != '\n';) - wordlen++; + /* Extract the word */ + for(wordlen = 0; wordlen < len && line[wordlen] != ' ' && + line[wordlen] != '\t' && line[wordlen] != '\r' && + line[wordlen] != '\n';) + wordlen++; - /* Test the word for a matching authentication mechanism */ - mechbit = Curl_sasl_decode_mech(line, wordlen, &llen); - if(mechbit && llen == wordlen) - smtpc->sasl.authmechs |= mechbit; + /* Test the word for a matching authentication mechanism */ + mechbit = Curl_sasl_decode_mech(line, wordlen, &llen); + if(mechbit && llen == wordlen) + smtpc->sasl.authmechs |= mechbit; - line += wordlen; - len -= wordlen; + line += wordlen; + len -= wordlen; } } - if(smtpcode != 1) { - if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) { - /* We don't have a SSL/TLS connection yet, but SSL is requested */ - if(smtpc->tls_supported) - /* Switch to TLS connection now */ - result = smtp_perform_starttls(conn); - else if(data->set.use_ssl == CURLUSESSL_TRY) - /* Fallback and carry on with authentication */ - result = smtp_perform_authentication(conn); - else { - failf(data, "STARTTLS not supported."); - result = CURLE_USE_SSL_FAILED; - } + if(smtpcode != 1) { + if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) { + /* We don't have a SSL/TLS connection yet, but SSL is requested */ + if(smtpc->tls_supported) + /* Switch to TLS connection now */ + result = smtp_perform_starttls(conn); + else if(data->set.use_ssl == CURLUSESSL_TRY) + /* Fallback and carry on with authentication */ + result = smtp_perform_authentication(conn); + else { + failf(data, "STARTTLS not supported."); + result = CURLE_USE_SSL_FAILED; + } } - else - result = smtp_perform_authentication(conn); + else + result = smtp_perform_authentication(conn); } } else { @@ -936,95 +936,95 @@ static CURLcode smtp_state_ehlo_resp(struct connectdata *conn, int smtpcode, return result; } -/* For HELO responses */ -static CURLcode smtp_state_helo_resp(struct connectdata *conn, int smtpcode, - smtpstate instate) +/* For HELO responses */ +static CURLcode smtp_state_helo_resp(struct connectdata *conn, int smtpcode, + smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; (void)instate; /* no use for this yet */ - if(smtpcode/100 != 2) { - failf(data, "Remote access denied: %d", smtpcode); - result = CURLE_REMOTE_ACCESS_DENIED; + if(smtpcode/100 != 2) { + failf(data, "Remote access denied: %d", smtpcode); + result = CURLE_REMOTE_ACCESS_DENIED; } - else - /* End of connect phase */ - state(conn, SMTP_STOP); + else + /* End of connect phase */ + state(conn, SMTP_STOP); return result; } -/* For SASL authentication responses */ -static CURLcode smtp_state_auth_resp(struct connectdata *conn, - int smtpcode, - smtpstate instate) +/* For SASL authentication responses */ +static CURLcode smtp_state_auth_resp(struct connectdata *conn, + int smtpcode, + smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; - struct smtp_conn *smtpc = &conn->proto.smtpc; - saslprogress progress; + struct Curl_easy *data = conn->data; + struct smtp_conn *smtpc = &conn->proto.smtpc; + saslprogress progress; (void)instate; /* no use for this yet */ - result = Curl_sasl_continue(&smtpc->sasl, conn, smtpcode, &progress); - if(!result) - switch(progress) { - case SASL_DONE: - state(conn, SMTP_STOP); /* Authenticated */ - break; - case SASL_IDLE: /* No mechanism left after cancellation */ - failf(data, "Authentication cancelled"); - result = CURLE_LOGIN_DENIED; - break; - default: - break; + result = Curl_sasl_continue(&smtpc->sasl, conn, smtpcode, &progress); + if(!result) + switch(progress) { + case SASL_DONE: + state(conn, SMTP_STOP); /* Authenticated */ + break; + case SASL_IDLE: /* No mechanism left after cancellation */ + failf(data, "Authentication cancelled"); + result = CURLE_LOGIN_DENIED; + break; + default: + break; } return result; } -/* For command responses */ -static CURLcode smtp_state_command_resp(struct connectdata *conn, int smtpcode, - smtpstate instate) +/* For command responses */ +static CURLcode smtp_state_command_resp(struct connectdata *conn, int smtpcode, + smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; - char *line = data->state.buffer; - size_t len = strlen(line); + char *line = data->state.buffer; + size_t len = strlen(line); (void)instate; /* no use for this yet */ - if((smtp->rcpt && smtpcode/100 != 2 && smtpcode != 553 && smtpcode != 1) || - (!smtp->rcpt && smtpcode/100 != 2 && smtpcode != 1)) { - failf(data, "Command failed: %d", smtpcode); - result = CURLE_RECV_ERROR; + if((smtp->rcpt && smtpcode/100 != 2 && smtpcode != 553 && smtpcode != 1) || + (!smtp->rcpt && smtpcode/100 != 2 && smtpcode != 1)) { + failf(data, "Command failed: %d", smtpcode); + result = CURLE_RECV_ERROR; } else { - /* Temporarily add the LF character back and send as body to the client */ - if(!data->set.opt_no_body) { - line[len] = '\n'; - result = Curl_client_write(conn, CLIENTWRITE_BODY, line, len + 1); - line[len] = '\0'; + /* Temporarily add the LF character back and send as body to the client */ + if(!data->set.opt_no_body) { + line[len] = '\n'; + result = Curl_client_write(conn, CLIENTWRITE_BODY, line, len + 1); + line[len] = '\0'; } - if(smtpcode != 1) { - if(smtp->rcpt) { - smtp->rcpt = smtp->rcpt->next; - - if(smtp->rcpt) { - /* Send the next command */ - result = smtp_perform_command(conn); - } - else - /* End of DO phase */ - state(conn, SMTP_STOP); + if(smtpcode != 1) { + if(smtp->rcpt) { + smtp->rcpt = smtp->rcpt->next; + + if(smtp->rcpt) { + /* Send the next command */ + result = smtp_perform_command(conn); + } + else + /* End of DO phase */ + state(conn, SMTP_STOP); } - else - /* End of DO phase */ - state(conn, SMTP_STOP); + else + /* End of DO phase */ + state(conn, SMTP_STOP); } } @@ -1036,7 +1036,7 @@ static CURLcode smtp_state_mail_resp(struct connectdata *conn, int smtpcode, smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; (void)instate; /* no use for this yet */ @@ -1044,8 +1044,8 @@ static CURLcode smtp_state_mail_resp(struct connectdata *conn, int smtpcode, failf(data, "MAIL failed: %d", smtpcode); result = CURLE_SEND_ERROR; } - else - /* Start the RCPT TO command */ + else + /* Start the RCPT TO command */ result = smtp_perform_rcpt_to(conn); return result; @@ -1056,7 +1056,7 @@ static CURLcode smtp_state_rcpt_resp(struct connectdata *conn, int smtpcode, smtpstate instate) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; bool is_smtp_err = FALSE; bool is_smtp_blocking_err = FALSE; @@ -1086,12 +1086,12 @@ static CURLcode smtp_state_rcpt_resp(struct connectdata *conn, int smtpcode, } if(!is_smtp_blocking_err) { - smtp->rcpt = smtp->rcpt->next; - - if(smtp->rcpt) - /* Send the next RCPT TO command */ + smtp->rcpt = smtp->rcpt->next; + + if(smtp->rcpt) + /* Send the next RCPT TO command */ result = smtp_perform_rcpt_to(conn); - else { + else { /* We weren't able to issue a successful RCPT TO command while going over recipients (potentially multiple). Sending back last error. */ if(!smtp->rcpt_had_ok) { @@ -1115,27 +1115,27 @@ static CURLcode smtp_state_rcpt_resp(struct connectdata *conn, int smtpcode, static CURLcode smtp_state_data_resp(struct connectdata *conn, int smtpcode, smtpstate instate) { - CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + CURLcode result = CURLE_OK; + struct Curl_easy *data = conn->data; (void)instate; /* no use for this yet */ if(smtpcode != 354) { - failf(data, "DATA failed: %d", smtpcode); - result = CURLE_SEND_ERROR; + failf(data, "DATA failed: %d", smtpcode); + result = CURLE_SEND_ERROR; } - else { - /* Set the progress upload size */ - Curl_pgrsSetUploadSize(data, data->state.infilesize); + else { + /* Set the progress upload size */ + Curl_pgrsSetUploadSize(data, data->state.infilesize); - /* SMTP upload */ + /* SMTP upload */ Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET); - /* End of DO phase */ - state(conn, SMTP_STOP); - } + /* End of DO phase */ + state(conn, SMTP_STOP); + } - return result; + return result; } /* For POSTDATA responses, which are received after the entire DATA @@ -1161,7 +1161,7 @@ static CURLcode smtp_statemach_act(struct connectdata *conn) { CURLcode result = CURLE_OK; curl_socket_t sock = conn->sock[FIRSTSOCKET]; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; int smtpcode; struct smtp_conn *smtpc = &conn->proto.smtpc; struct pingpong *pp = &smtpc->pp; @@ -1175,19 +1175,19 @@ static CURLcode smtp_statemach_act(struct connectdata *conn) if(pp->sendleft) return Curl_pp_flushsend(pp); - do { - /* Read the response from the server */ - result = Curl_pp_readresp(sock, pp, &smtpcode, &nread); - if(result) - return result; + do { + /* Read the response from the server */ + result = Curl_pp_readresp(sock, pp, &smtpcode, &nread); + if(result) + return result; + + /* Store the latest response for later retrieval if necessary */ + if(smtpc->state != SMTP_QUIT && smtpcode != 1) + data->info.httpcode = smtpcode; - /* Store the latest response for later retrieval if necessary */ - if(smtpc->state != SMTP_QUIT && smtpcode != 1) - data->info.httpcode = smtpcode; + if(!smtpcode) + break; - if(!smtpcode) - break; - /* We have now received a full SMTP server response */ switch(smtpc->state) { case SMTP_SERVERGREET: @@ -1206,12 +1206,12 @@ static CURLcode smtp_statemach_act(struct connectdata *conn) result = smtp_state_starttls_resp(conn, smtpcode, smtpc->state); break; - case SMTP_AUTH: - result = smtp_state_auth_resp(conn, smtpcode, smtpc->state); + case SMTP_AUTH: + result = smtp_state_auth_resp(conn, smtpcode, smtpc->state); break; - case SMTP_COMMAND: - result = smtp_state_command_resp(conn, smtpcode, smtpc->state); + case SMTP_COMMAND: + result = smtp_state_command_resp(conn, smtpcode, smtpc->state); break; case SMTP_MAIL: @@ -1237,7 +1237,7 @@ static CURLcode smtp_statemach_act(struct connectdata *conn) state(conn, SMTP_STOP); break; } - } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp)); + } while(!result && smtpc->state != SMTP_STOP && Curl_pp_moredata(pp)); return result; } @@ -1272,12 +1272,12 @@ static CURLcode smtp_block_statemach(struct connectdata *conn, return result; } -/* Allocate and initialize the SMTP struct for the current Curl_easy if +/* Allocate and initialize the SMTP struct for the current Curl_easy if required */ static CURLcode smtp_init(struct connectdata *conn) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp; smtp = data->req.p.smtp = calloc(sizeof(struct SMTP), 1); @@ -1312,7 +1312,7 @@ static CURLcode smtp_connect(struct connectdata *conn, bool *done) *done = FALSE; /* default to not done yet */ /* We always support persistent connections in SMTP */ - connkeep(conn, "SMTP default"); + connkeep(conn, "SMTP default"); /* Set the default response time-out */ pp->response_time = RESP_TIMEOUT; @@ -1320,8 +1320,8 @@ static CURLcode smtp_connect(struct connectdata *conn, bool *done) pp->endofresp = smtp_endofresp; pp->conn = conn; - /* Initialize the SASL storage */ - Curl_sasl_init(&smtpc->sasl, &saslsmtp); + /* Initialize the SASL storage */ + Curl_sasl_init(&smtpc->sasl, &saslsmtp); /* Initialise the pingpong layer */ Curl_pp_setup(pp); @@ -1358,68 +1358,68 @@ static CURLcode smtp_done(struct connectdata *conn, CURLcode status, bool premature) { CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; struct pingpong *pp = &conn->proto.smtpc.pp; - char *eob; + char *eob; ssize_t len; ssize_t bytes_written; (void)premature; - if(!smtp || !pp->conn) + if(!smtp || !pp->conn) return CURLE_OK; - /* Cleanup our per-request based variables */ - Curl_safefree(smtp->custom); - + /* Cleanup our per-request based variables */ + Curl_safefree(smtp->custom); + if(status) { - connclose(conn, "SMTP done with bad status"); /* marked for closure */ + connclose(conn, "SMTP done with bad status"); /* marked for closure */ result = status; /* use the already set error code */ } - else if(!data->set.connect_only && data->set.mail_rcpt && - (data->set.upload || data->set.mimepost.kind)) { + else if(!data->set.connect_only && data->set.mail_rcpt && + (data->set.upload || data->set.mimepost.kind)) { /* Calculate the EOB taking into account any terminating CRLF from the previous line of the email or the CRLF of the DATA command when there - is "no mail data". RFC-5321, sect. 4.1.1.4. - - Note: As some SSL backends, such as OpenSSL, will cause Curl_write() to - fail when using a different pointer following a previous write, that - returned CURLE_AGAIN, we duplicate the EOB now rather than when the - bytes written doesn't equal len. */ - if(smtp->trailing_crlf || !conn->data->state.infilesize) { + is "no mail data". RFC-5321, sect. 4.1.1.4. + + Note: As some SSL backends, such as OpenSSL, will cause Curl_write() to + fail when using a different pointer following a previous write, that + returned CURLE_AGAIN, we duplicate the EOB now rather than when the + bytes written doesn't equal len. */ + if(smtp->trailing_crlf || !conn->data->state.infilesize) { eob = strdup(&SMTP_EOB[2]); - len = SMTP_EOB_LEN - 2; + len = SMTP_EOB_LEN - 2; + } + else { + eob = strdup(SMTP_EOB); + len = SMTP_EOB_LEN; } - else { - eob = strdup(SMTP_EOB); - len = SMTP_EOB_LEN; - } - - if(!eob) - return CURLE_OUT_OF_MEMORY; - + + if(!eob) + return CURLE_OUT_OF_MEMORY; + /* Send the end of block data */ result = Curl_write(conn, conn->writesockfd, eob, len, &bytes_written); - if(result) { - free(eob); + if(result) { + free(eob); return result; - } + } if(bytes_written != len) { /* The whole chunk was not sent so keep it around and adjust the pingpong structure accordingly */ - pp->sendthis = eob; + pp->sendthis = eob; pp->sendsize = len; pp->sendleft = len - bytes_written; } - else { + else { /* Successfully sent so adjust the response timeout relative to now */ - pp->response = Curl_now(); + pp->response = Curl_now(); + + free(eob); + } - free(eob); - } - state(conn, SMTP_POSTDATA); /* Run the state-machine */ @@ -1436,52 +1436,52 @@ static CURLcode smtp_done(struct connectdata *conn, CURLcode status, * * smtp_perform() * - * This is the actual DO function for SMTP. Transfer a mail, send a command - * or get some data according to the options previously setup. + * This is the actual DO function for SMTP. Transfer a mail, send a command + * or get some data according to the options previously setup. */ static CURLcode smtp_perform(struct connectdata *conn, bool *connected, bool *dophase_done) { /* This is SMTP and no proxy */ CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; DEBUGF(infof(conn->data, "DO phase starts\n")); - if(data->set.opt_no_body) { + if(data->set.opt_no_body) { /* Requested no body means no transfer */ smtp->transfer = FTPTRANSFER_INFO; } *dophase_done = FALSE; /* not done yet */ - /* Store the first recipient (or NULL if not specified) */ - smtp->rcpt = data->set.mail_rcpt; - + /* Store the first recipient (or NULL if not specified) */ + smtp->rcpt = data->set.mail_rcpt; + /* Track of whether we've successfully sent at least one RCPT TO command */ smtp->rcpt_had_ok = FALSE; /* Track of the last error we've received by sending RCPT TO command */ smtp->rcpt_last_error = 0; - /* Initial data character is the first character in line: it is implicitly - preceded by a virtual CRLF. */ - smtp->trailing_crlf = TRUE; - smtp->eob = 2; - + /* Initial data character is the first character in line: it is implicitly + preceded by a virtual CRLF. */ + smtp->trailing_crlf = TRUE; + smtp->eob = 2; + /* Start the first command in the DO phase */ - if((data->set.upload || data->set.mimepost.kind) && data->set.mail_rcpt) - /* MAIL transfer */ - result = smtp_perform_mail(conn); - else - /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */ - result = smtp_perform_command(conn); - + if((data->set.upload || data->set.mimepost.kind) && data->set.mail_rcpt) + /* MAIL transfer */ + result = smtp_perform_mail(conn); + else + /* SMTP based command (VRFY, EXPN, NOOP, RSET or HELP) */ + result = smtp_perform_command(conn); + if(result) return result; - /* Run the state-machine */ + /* Run the state-machine */ result = smtp_multi_statemach(conn, dophase_done); *connected = conn->bits.tcpconnect[FIRSTSOCKET]; @@ -1507,11 +1507,11 @@ static CURLcode smtp_do(struct connectdata *conn, bool *done) *done = FALSE; /* default to false */ - /* Parse the custom request */ - result = smtp_parse_custom_request(conn); - if(result) - return result; - + /* Parse the custom request */ + result = smtp_parse_custom_request(conn); + if(result) + return result; + result = smtp_regular_transfer(conn, done); return result; @@ -1524,7 +1524,7 @@ static CURLcode smtp_do(struct connectdata *conn, bool *done) * Disconnect from an SMTP server. Cleanup protocol-specific per-connection * resources. BLOCKING. */ -static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection) +static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection) { struct smtp_conn *smtpc = &conn->proto.smtpc; @@ -1534,7 +1534,7 @@ static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection) /* The SMTP session may or may not have been allocated/setup at this point! */ - if(!dead_connection && smtpc->pp.conn && smtpc->pp.conn->bits.protoconnstart) + if(!dead_connection && smtpc->pp.conn && smtpc->pp.conn->bits.protoconnstart) if(!smtp_perform_quit(conn)) (void)smtp_block_statemach(conn, TRUE); /* ignore errors on QUIT */ @@ -1542,7 +1542,7 @@ static CURLcode smtp_disconnect(struct connectdata *conn, bool dead_connection) Curl_pp_disconnect(&smtpc->pp); /* Cleanup the SASL module */ - Curl_sasl_cleanup(conn, smtpc->sasl.authused); + Curl_sasl_cleanup(conn, smtpc->sasl.authused); /* Cleanup our connection based variables */ Curl_safefree(smtpc->domain); @@ -1594,7 +1594,7 @@ static CURLcode smtp_regular_transfer(struct connectdata *conn, { CURLcode result = CURLE_OK; bool connected = FALSE; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; /* Make sure size is unknown at this point */ data->req.size = -1; @@ -1602,8 +1602,8 @@ static CURLcode smtp_regular_transfer(struct connectdata *conn, /* Set the progress data */ Curl_pgrsSetUploadCounter(data, 0); Curl_pgrsSetDownloadCounter(data, 0); - Curl_pgrsSetUploadSize(data, -1); - Curl_pgrsSetDownloadSize(data, -1); + Curl_pgrsSetUploadSize(data, -1); + Curl_pgrsSetDownloadSize(data, -1); /* Carry out the perform */ result = smtp_perform(conn, &connected, dophase_done); @@ -1619,7 +1619,7 @@ static CURLcode smtp_setup_connection(struct connectdata *conn) { CURLcode result; - /* Clear the TLS upgraded flag */ + /* Clear the TLS upgraded flag */ conn->bits.tls_upgraded = FALSE; /* Initialise the SMTP layer */ @@ -1640,30 +1640,30 @@ static CURLcode smtp_parse_url_options(struct connectdata *conn) { CURLcode result = CURLE_OK; struct smtp_conn *smtpc = &conn->proto.smtpc; - const char *ptr = conn->options; + const char *ptr = conn->options; + + smtpc->sasl.resetprefs = TRUE; - smtpc->sasl.resetprefs = TRUE; - - while(!result && ptr && *ptr) { + while(!result && ptr && *ptr) { const char *key = ptr; - const char *value; + const char *value; while(*ptr && *ptr != '=') - ptr++; + ptr++; - value = ptr + 1; + value = ptr + 1; - while(*ptr && *ptr != ';') - ptr++; - - if(strncasecompare(key, "AUTH=", 5)) - result = Curl_sasl_parse_url_auth_option(&smtpc->sasl, - value, ptr - value); + while(*ptr && *ptr != ';') + ptr++; + + if(strncasecompare(key, "AUTH=", 5)) + result = Curl_sasl_parse_url_auth_option(&smtpc->sasl, + value, ptr - value); else result = CURLE_URL_MALFORMAT; - - if(*ptr == ';') - ptr++; + + if(*ptr == ';') + ptr++; } return result; @@ -1678,9 +1678,9 @@ static CURLcode smtp_parse_url_options(struct connectdata *conn) static CURLcode smtp_parse_url_path(struct connectdata *conn) { /* The SMTP struct is already initialised in smtp_connect() */ - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct smtp_conn *smtpc = &conn->proto.smtpc; - const char *path = &data->state.up.path[1]; /* skip leading path */ + const char *path = &data->state.up.path[1]; /* skip leading path */ char localhost[HOSTNAME_MAX + 1]; /* Calculate the path if necessary */ @@ -1696,26 +1696,26 @@ static CURLcode smtp_parse_url_path(struct connectdata *conn) REJECT_CTRL); } -/*********************************************************************** - * - * smtp_parse_custom_request() - * - * Parse the custom request. - */ -static CURLcode smtp_parse_custom_request(struct connectdata *conn) +/*********************************************************************** + * + * smtp_parse_custom_request() + * + * Parse the custom request. + */ +static CURLcode smtp_parse_custom_request(struct connectdata *conn) { - CURLcode result = CURLE_OK; - struct Curl_easy *data = conn->data; + CURLcode result = CURLE_OK; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; - const char *custom = data->set.str[STRING_CUSTOMREQUEST]; - - /* URL decode the custom request */ - if(custom) + const char *custom = data->set.str[STRING_CUSTOMREQUEST]; + + /* URL decode the custom request */ + if(custom) result = Curl_urldecode(data, custom, 0, &smtp->custom, NULL, REJECT_CTRL); - - return result; -} - + + return result; +} + /*********************************************************************** * * smtp_parse_address() @@ -1788,8 +1788,8 @@ static CURLcode smtp_parse_address(struct connectdata *conn, const char *fqma, return result; } -CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread) -{ +CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread) +{ /* When sending a SMTP payload we must detect CRLF. sequences making sure they are sent as CRLF.. instead, as a . on the beginning of a line will be deleted by the server when not part of an EOB terminator and a @@ -1798,29 +1798,29 @@ CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread) */ ssize_t i; ssize_t si; - struct Curl_easy *data = conn->data; + struct Curl_easy *data = conn->data; struct SMTP *smtp = data->req.p.smtp; - char *scratch = data->state.scratch; - char *newscratch = NULL; - char *oldscratch = NULL; - size_t eob_sent; - - /* Do we need to allocate a scratch buffer? */ - if(!scratch || data->set.crlf) { - oldscratch = scratch; - - scratch = newscratch = malloc(2 * data->set.upload_buffer_size); - if(!newscratch) { - failf(data, "Failed to alloc scratch buffer!"); - + char *scratch = data->state.scratch; + char *newscratch = NULL; + char *oldscratch = NULL; + size_t eob_sent; + + /* Do we need to allocate a scratch buffer? */ + if(!scratch || data->set.crlf) { + oldscratch = scratch; + + scratch = newscratch = malloc(2 * data->set.upload_buffer_size); + if(!newscratch) { + failf(data, "Failed to alloc scratch buffer!"); + return CURLE_OUT_OF_MEMORY; } } - DEBUGASSERT(data->set.upload_buffer_size >= (size_t)nread); + DEBUGASSERT(data->set.upload_buffer_size >= (size_t)nread); + + /* Have we already sent part of the EOB? */ + eob_sent = smtp->eob; - /* Have we already sent part of the EOB? */ - eob_sent = smtp->eob; - /* This loop can be improved by some kind of Boyer-Moore style of approach but that is saved for later... */ for(i = 0, si = 0; i < nread; i++) { @@ -1835,8 +1835,8 @@ CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread) } else if(smtp->eob) { /* A previous substring matched so output that first */ - memcpy(&scratch[si], &SMTP_EOB[eob_sent], smtp->eob - eob_sent); - si += smtp->eob - eob_sent; + memcpy(&scratch[si], &SMTP_EOB[eob_sent], smtp->eob - eob_sent); + si += smtp->eob - eob_sent; /* Then compare the first byte */ if(SMTP_EOB[0] == data->req.upload_fromhere[i]) @@ -1844,8 +1844,8 @@ CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread) else smtp->eob = 0; - eob_sent = 0; - + eob_sent = 0; + /* Reset the trailing CRLF flag as there was more data */ smtp->trailing_crlf = FALSE; } @@ -1853,38 +1853,38 @@ CURLcode Curl_smtp_escape_eob(struct connectdata *conn, const ssize_t nread) /* Do we have a match for CRLF. as per RFC-5321, sect. 4.5.2 */ if(SMTP_EOB_FIND_LEN == smtp->eob) { /* Copy the replacement data to the target buffer */ - memcpy(&scratch[si], &SMTP_EOB_REPL[eob_sent], - SMTP_EOB_REPL_LEN - eob_sent); - si += SMTP_EOB_REPL_LEN - eob_sent; + memcpy(&scratch[si], &SMTP_EOB_REPL[eob_sent], + SMTP_EOB_REPL_LEN - eob_sent); + si += SMTP_EOB_REPL_LEN - eob_sent; smtp->eob = 0; - eob_sent = 0; + eob_sent = 0; } else if(!smtp->eob) - scratch[si++] = data->req.upload_fromhere[i]; + scratch[si++] = data->req.upload_fromhere[i]; } - if(smtp->eob - eob_sent) { + if(smtp->eob - eob_sent) { /* A substring matched before processing ended so output that now */ - memcpy(&scratch[si], &SMTP_EOB[eob_sent], smtp->eob - eob_sent); - si += smtp->eob - eob_sent; + memcpy(&scratch[si], &SMTP_EOB[eob_sent], smtp->eob - eob_sent); + si += smtp->eob - eob_sent; } - /* Only use the new buffer if we replaced something */ + /* Only use the new buffer if we replaced something */ if(si != nread) { /* Upload from the new (replaced) buffer instead */ - data->req.upload_fromhere = scratch; - - /* Save the buffer so it can be freed later */ - data->state.scratch = scratch; - - /* Free the old scratch buffer */ - free(oldscratch); - + data->req.upload_fromhere = scratch; + + /* Save the buffer so it can be freed later */ + data->state.scratch = scratch; + + /* Free the old scratch buffer */ + free(oldscratch); + /* Set the new amount too */ - data->req.upload_present = si; + data->req.upload_present = si; } - else - free(newscratch); + else + free(newscratch); return CURLE_OK; } |