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