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