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