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