]>
Commit | Line | Data |
---|---|---|
86e4d161 | 1 | /* |
e458e5f0 | 2 | * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>. |
86e4d161 MS |
3 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. |
4 | * | |
5 | * This code is derived from software contributed to The DragonFly Project | |
6 | * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg, | |
7 | * Germany. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. Neither the name of The DragonFly Project nor the names of its | |
20 | * contributors may be used to endorse or promote products derived | |
21 | * from this software without specific, prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
24 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
26 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
27 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
28 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
29 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
30 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
31 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
33 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
86e4d161 MS |
35 | */ |
36 | ||
9b921550 SS |
37 | #include "dfcompat.h" |
38 | ||
86e4d161 MS |
39 | #include <sys/param.h> |
40 | #include <sys/queue.h> | |
41 | #include <sys/stat.h> | |
42 | #include <sys/types.h> | |
43 | #include <sys/socket.h> | |
44 | #include <netinet/in.h> | |
45 | #include <arpa/inet.h> | |
46 | ||
86e4d161 | 47 | #include <openssl/ssl.h> |
53885e5a | 48 | #include <openssl/err.h> |
86e4d161 | 49 | |
8a6f2a49 | 50 | #include <ctype.h> |
c507d897 | 51 | #include <err.h> |
7a73c463 | 52 | #include <errno.h> |
86e4d161 MS |
53 | #include <netdb.h> |
54 | #include <setjmp.h> | |
55 | #include <signal.h> | |
56 | #include <syslog.h> | |
57 | #include <unistd.h> | |
58 | ||
59 | #include "dma.h" | |
60 | ||
249ee966 | 61 | char neterr[ERRMSG_SIZE]; |
86e4d161 | 62 | |
53885e5a SS |
63 | char * |
64 | ssl_errstr(void) | |
65 | { | |
66 | long oerr, nerr; | |
67 | ||
68 | oerr = 0; | |
69 | while ((nerr = ERR_get_error()) != 0) | |
70 | oerr = nerr; | |
71 | ||
72 | return (ERR_error_string(oerr, NULL)); | |
73 | } | |
74 | ||
86e4d161 MS |
75 | ssize_t |
76 | send_remote_command(int fd, const char* fmt, ...) | |
77 | { | |
78 | va_list va; | |
79 | char cmd[4096]; | |
5b84c25b SS |
80 | size_t len, pos; |
81 | int s; | |
82 | ssize_t n; | |
86e4d161 MS |
83 | |
84 | va_start(va, fmt); | |
5b84c25b SS |
85 | s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va); |
86 | va_end(va); | |
53885e5a SS |
87 | if (s == sizeof(cmd) - 2 || s < 0) { |
88 | strcpy(neterr, "Internal error: oversized command string"); | |
89 | return (-1); | |
90 | } | |
91 | ||
5b84c25b SS |
92 | /* We *know* there are at least two more bytes available */ |
93 | strcat(cmd, "\r\n"); | |
94 | len = strlen(cmd); | |
86e4d161 | 95 | |
a0c4afa6 SS |
96 | if (((config.features & SECURETRANS) != 0) && |
97 | ((config.features & NOSSL) == 0)) { | |
98 | while ((s = SSL_write(config.ssl, (const char*)cmd, len)) <= 0) { | |
99 | s = SSL_get_error(config.ssl, s); | |
5b84c25b | 100 | if (s != SSL_ERROR_WANT_READ && |
53885e5a SS |
101 | s != SSL_ERROR_WANT_WRITE) { |
102 | strncpy(neterr, ssl_errstr(), sizeof(neterr)); | |
5b84c25b | 103 | return (-1); |
53885e5a | 104 | } |
5b84c25b | 105 | } |
86e4d161 MS |
106 | } |
107 | else { | |
5b84c25b SS |
108 | pos = 0; |
109 | while (pos < len) { | |
110 | n = write(fd, cmd + pos, len - pos); | |
111 | if (n < 0) | |
112 | return (-1); | |
113 | pos += n; | |
114 | } | |
86e4d161 | 115 | } |
86e4d161 | 116 | |
5b84c25b | 117 | return (len); |
86e4d161 MS |
118 | } |
119 | ||
50c8e68a | 120 | int |
bf28fcc6 | 121 | read_remote(int fd, int extbufsize, char *extbuf) |
86e4d161 | 122 | { |
50c8e68a | 123 | ssize_t rlen = 0; |
8a6f2a49 | 124 | size_t pos, len, copysize; |
50c8e68a | 125 | char buff[BUF_SIZE]; |
8a6f2a49 SS |
126 | int done = 0, status = 0, status_running = 0, extbufpos = 0; |
127 | enum { parse_status, parse_spacedash, parse_rest } parsestate; | |
86e4d161 | 128 | |
8a6f2a49 | 129 | if (do_timeout(CON_TIMEOUT, 1) != 0) { |
7a73c463 | 130 | snprintf(neterr, sizeof(neterr), "Timeout reached"); |
53885e5a | 131 | return (-1); |
86e4d161 | 132 | } |
86e4d161 MS |
133 | |
134 | /* | |
50c8e68a MS |
135 | * Remote reading code from femail.c written by Henning Brauer of |
136 | * OpenBSD and released under a BSD style license. | |
86e4d161 | 137 | */ |
8a6f2a49 SS |
138 | len = 0; |
139 | pos = 0; | |
140 | parsestate = parse_status; | |
141 | neterr[0] = 0; | |
142 | while (!(done && parsestate == parse_status)) { | |
bf28fcc6 | 143 | rlen = 0; |
50c8e68a MS |
144 | if (pos == 0 || |
145 | (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) { | |
146 | memmove(buff, buff + pos, len - pos); | |
147 | len -= pos; | |
148 | pos = 0; | |
a0c4afa6 SS |
149 | if (((config.features & SECURETRANS) != 0) && |
150 | (config.features & NOSSL) == 0) { | |
8a6f2a49 | 151 | if ((rlen = SSL_read(config.ssl, buff + len, sizeof(buff) - len)) == -1) { |
53885e5a | 152 | strncpy(neterr, ssl_errstr(), sizeof(neterr)); |
8a6f2a49 | 153 | goto error; |
53885e5a | 154 | } |
50c8e68a | 155 | } else { |
53885e5a SS |
156 | if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) { |
157 | strncpy(neterr, strerror(errno), sizeof(neterr)); | |
8a6f2a49 | 158 | goto error; |
53885e5a | 159 | } |
50c8e68a MS |
160 | } |
161 | len += rlen; | |
8a6f2a49 SS |
162 | |
163 | copysize = sizeof(neterr) - strlen(neterr) - 1; | |
164 | if (copysize > len) | |
165 | copysize = len; | |
166 | strncat(neterr, buff, copysize); | |
50c8e68a | 167 | } |
bf28fcc6 MS |
168 | /* |
169 | * If there is an external buffer with a size bigger than zero | |
170 | * and as long as there is space in the external buffer and | |
171 | * there are new characters read from the mailserver | |
172 | * copy them to the external buffer | |
173 | */ | |
8a6f2a49 | 174 | if (extbufpos <= (extbufsize - 1) && rlen > 0 && extbufsize > 0 && extbuf != NULL) { |
bf28fcc6 MS |
175 | /* do not write over the bounds of the buffer */ |
176 | if(extbufpos + rlen > (extbufsize - 1)) { | |
177 | rlen = extbufsize - extbufpos; | |
178 | } | |
179 | memcpy(extbuf + extbufpos, buff + len - rlen, rlen); | |
180 | extbufpos += rlen; | |
181 | } | |
86e4d161 | 182 | |
50c8e68a | 183 | if (pos == len) |
8a6f2a49 | 184 | continue; |
86e4d161 | 185 | |
8a6f2a49 SS |
186 | switch (parsestate) { |
187 | case parse_status: | |
188 | for (; pos < len; pos++) { | |
189 | if (isdigit(buff[pos])) { | |
190 | status_running = status_running * 10 + (buff[pos] - '0'); | |
191 | } else { | |
192 | status = status_running; | |
193 | status_running = 0; | |
194 | parsestate = parse_spacedash; | |
195 | break; | |
196 | } | |
197 | } | |
198 | continue; | |
199 | ||
200 | case parse_spacedash: | |
201 | switch (buff[pos]) { | |
202 | case ' ': | |
203 | done = 1; | |
204 | break; | |
86e4d161 | 205 | |
8a6f2a49 SS |
206 | case '-': |
207 | /* ignore */ | |
208 | /* XXX read capabilities */ | |
209 | break; | |
210 | ||
211 | default: | |
212 | strcpy(neterr, "invalid syntax in reply from server"); | |
213 | goto error; | |
214 | } | |
215 | ||
216 | pos++; | |
217 | parsestate = parse_rest; | |
218 | continue; | |
219 | ||
220 | case parse_rest: | |
221 | /* skip up to \n */ | |
222 | for (; pos < len; pos++) { | |
223 | if (buff[pos] == '\n') { | |
224 | pos++; | |
225 | parsestate = parse_status; | |
226 | break; | |
227 | } | |
228 | } | |
229 | } | |
86e4d161 | 230 | |
86e4d161 | 231 | } |
50c8e68a | 232 | |
8a6f2a49 SS |
233 | do_timeout(0, 0); |
234 | ||
235 | /* chop off trailing newlines */ | |
236 | while (neterr[0] != 0 && strchr("\r\n", neterr[strlen(neterr) - 1]) != 0) | |
237 | neterr[strlen(neterr) - 1] = 0; | |
238 | ||
50c8e68a | 239 | return (status/100); |
8a6f2a49 SS |
240 | |
241 | error: | |
242 | do_timeout(0, 0); | |
243 | return (-1); | |
86e4d161 MS |
244 | } |
245 | ||
246 | /* | |
247 | * Handle SMTP authentication | |
86e4d161 MS |
248 | */ |
249 | static int | |
1fa7a882 | 250 | smtp_login(int fd, char *login, char* password, const struct smtp_features* features) |
86e4d161 | 251 | { |
86e4d161 | 252 | char *temp; |
50c8e68a | 253 | int len, res = 0; |
86e4d161 | 254 | |
1fa7a882 MT |
255 | // CRAM-MD5 |
256 | if (features->auth.cram_md5) { | |
257 | res = smtp_auth_md5(fd, login, password); | |
258 | if (res == 0) { | |
259 | return (0); | |
260 | } else if (res == -2) { | |
261 | /* | |
262 | * If the return code is -2, then then the login attempt failed, | |
263 | * do not try other login mechanisms | |
264 | */ | |
bf28fcc6 MS |
265 | return (1); |
266 | } | |
1fa7a882 MT |
267 | } |
268 | ||
269 | // LOGIN | |
270 | if (features->auth.login) { | |
271 | if ((config.features & INSECURE) != 0 || | |
272 | (config.features & SECURETRANS) != 0) { | |
273 | /* Send AUTH command according to RFC 2554 */ | |
274 | send_remote_command(fd, "AUTH LOGIN"); | |
275 | if (read_remote(fd, 0, NULL) != 3) { | |
276 | syslog(LOG_NOTICE, "remote delivery deferred:" | |
277 | " AUTH login not available: %s", | |
278 | neterr); | |
279 | return (1); | |
280 | } | |
86e4d161 | 281 | |
1fa7a882 MT |
282 | len = base64_encode(login, strlen(login), &temp); |
283 | if (len < 0) { | |
196b1b32 | 284 | encerr: |
1fa7a882 MT |
285 | syslog(LOG_ERR, "can not encode auth reply: %m"); |
286 | return (1); | |
287 | } | |
86e4d161 | 288 | |
1fa7a882 MT |
289 | send_remote_command(fd, "%s", temp); |
290 | free(temp); | |
291 | res = read_remote(fd, 0, NULL); | |
292 | if (res != 3) { | |
293 | syslog(LOG_NOTICE, "remote delivery %s: AUTH login failed: %s", | |
294 | res == 5 ? "failed" : "deferred", neterr); | |
295 | return (res == 5 ? -1 : 1); | |
296 | } | |
86e4d161 | 297 | |
1fa7a882 MT |
298 | len = base64_encode(password, strlen(password), &temp); |
299 | if (len < 0) | |
300 | goto encerr; | |
301 | ||
302 | send_remote_command(fd, "%s", temp); | |
303 | free(temp); | |
304 | res = read_remote(fd, 0, NULL); | |
305 | if (res != 2) { | |
306 | syslog(LOG_NOTICE, "remote delivery %s: Authentication failed: %s", | |
307 | res == 5 ? "failed" : "deferred", neterr); | |
308 | return (res == 5 ? -1 : 1); | |
309 | } | |
310 | } else { | |
311 | syslog(LOG_WARNING, "non-encrypted SMTP login is disabled in config, so skipping it. "); | |
312 | return (1); | |
bf28fcc6 | 313 | } |
86e4d161 MS |
314 | } |
315 | ||
316 | return (0); | |
317 | } | |
318 | ||
319 | static int | |
8cbdbd34 | 320 | open_connection(struct mx_hostentry *h) |
86e4d161 | 321 | { |
8cbdbd34 SS |
322 | int fd; |
323 | ||
324 | syslog(LOG_INFO, "trying remote delivery to %s [%s] pref %d", | |
325 | h->host, h->addr, h->pref); | |
326 | ||
327 | fd = socket(h->ai.ai_family, h->ai.ai_socktype, h->ai.ai_protocol); | |
328 | if (fd < 0) { | |
329 | syslog(LOG_INFO, "socket for %s [%s] failed: %m", | |
330 | h->host, h->addr); | |
86e4d161 MS |
331 | return (-1); |
332 | } | |
8cbdbd34 | 333 | |
32d3ec65 | 334 | if (connect(fd, (struct sockaddr *)&h->sa, h->ai.ai_addrlen) < 0) { |
8cbdbd34 SS |
335 | syslog(LOG_INFO, "connect to %s [%s] failed: %m", |
336 | h->host, h->addr); | |
337 | close(fd); | |
86e4d161 MS |
338 | return (-1); |
339 | } | |
8cbdbd34 | 340 | |
86e4d161 MS |
341 | return (fd); |
342 | } | |
343 | ||
8c365457 SS |
344 | static void |
345 | close_connection(int fd) | |
346 | { | |
f75033bb PP |
347 | if (config.ssl != NULL) { |
348 | if (((config.features & SECURETRANS) != 0) && | |
349 | ((config.features & NOSSL) == 0)) | |
350 | SSL_shutdown(config.ssl); | |
a0c4afa6 | 351 | SSL_free(config.ssl); |
f75033bb | 352 | } |
8c365457 SS |
353 | |
354 | close(fd); | |
355 | } | |
356 | ||
1fa7a882 MT |
357 | static void parse_auth_line(char* line, struct smtp_auth_mechanisms* auth) { |
358 | // Skip the auth prefix | |
359 | line += strlen("AUTH "); | |
360 | ||
361 | char* method = strtok(line, " "); | |
362 | while (method) { | |
363 | if (strcmp(method, "CRAM-MD5") == 0) | |
364 | auth->cram_md5 = 1; | |
365 | ||
366 | else if (strcmp(method, "LOGIN") == 0) | |
367 | auth->login = 1; | |
368 | ||
369 | method = strtok(NULL, " "); | |
370 | } | |
371 | } | |
372 | ||
373 | int perform_server_greeting(int fd, struct smtp_features* features) { | |
374 | /* | |
375 | Send EHLO | |
376 | XXX allow HELO fallback | |
377 | */ | |
378 | send_remote_command(fd, "EHLO %s", hostname()); | |
379 | ||
380 | char buffer[EHLO_RESPONSE_SIZE]; | |
381 | memset(buffer, 0, sizeof(buffer)); | |
382 | ||
383 | int res = read_remote(fd, sizeof(buffer) - 1, buffer); | |
384 | ||
385 | // Got an unexpected response | |
386 | if (res != 2) | |
387 | return -1; | |
388 | ||
389 | // Reset all features | |
390 | memset(features, 0, sizeof(*features)); | |
391 | ||
392 | // Run through the buffer line by line | |
393 | char linebuffer[EHLO_RESPONSE_SIZE]; | |
394 | char* p = buffer; | |
395 | ||
396 | while (*p) { | |
397 | char* line = linebuffer; | |
398 | while (*p && *p != '\n') { | |
399 | *line++ = *p++; | |
400 | } | |
401 | ||
402 | // p should never point to NULL after the loop | |
403 | // above unless we reached the end of the buffer. | |
404 | // In that case we will raise an error. | |
405 | if (!*p) { | |
406 | return -1; | |
407 | } | |
408 | ||
409 | // Otherwise p points to the newline character which | |
410 | // we will skip. | |
411 | p++; | |
412 | ||
413 | // Terminte the string (and remove the carriage-return character) | |
414 | *--line = '\0'; | |
415 | line = linebuffer; | |
416 | ||
417 | // End main loop for empty lines | |
418 | if (*line == '\0') | |
419 | break; | |
420 | ||
421 | // Process the line | |
422 | // - Must start with 250, followed by dash or space | |
423 | // - We won't check for the correct usage of space and dash because | |
424 | // that is already done in read_remote(). | |
425 | if ((strncmp(line, "250-", 4) != 0) && (strncmp(line, "250 ", 4) != 0)) { | |
426 | syslog(LOG_ERR, "Invalid line: %s\n", line); | |
427 | return -1; | |
428 | } | |
429 | ||
430 | // Skip the prefix | |
431 | line += 4; | |
432 | ||
433 | // Check for STARTTLS | |
434 | if (strcmp(line, "STARTTLS") == 0) | |
435 | features->starttls = 1; | |
436 | ||
437 | // Parse authentication mechanisms | |
438 | else if (strncmp(line, "AUTH ", 5) == 0) | |
439 | parse_auth_line(line, &features->auth); | |
440 | } | |
441 | ||
442 | syslog(LOG_DEBUG, "Server greeting successfully completed"); | |
443 | ||
444 | // STARTTLS | |
445 | if (features->starttls) | |
446 | syslog(LOG_DEBUG, " Server supports STARTTLS"); | |
447 | else | |
448 | syslog(LOG_DEBUG, " Server does not support STARTTLS"); | |
449 | ||
450 | // Authentication | |
451 | if (features->auth.cram_md5) { | |
452 | syslog(LOG_DEBUG, " Server supports CRAM-MD5 authentication"); | |
453 | } | |
454 | if (features->auth.login) { | |
455 | syslog(LOG_DEBUG, " Server supports LOGIN authentication"); | |
456 | } | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
8cbdbd34 | 461 | static int |
249ee966 | 462 | deliver_to_host(struct qitem *it, struct mx_hostentry *host) |
86e4d161 MS |
463 | { |
464 | struct authuser *a; | |
1fa7a882 | 465 | struct smtp_features features; |
8cbdbd34 | 466 | char line[1000]; |
86e4d161 | 467 | size_t linelen; |
8cbdbd34 | 468 | int fd, error = 0, do_auth = 0, res = 0; |
86e4d161 | 469 | |
6ddd63e1 | 470 | if (fseek(it->mailf, 0, SEEK_SET) != 0) { |
249ee966 | 471 | snprintf(errmsg, sizeof(errmsg), "can not seek: %s", strerror(errno)); |
40069c2f SS |
472 | return (-1); |
473 | } | |
474 | ||
4d5af2b0 | 475 | fd = open_connection(host); |
86e4d161 | 476 | if (fd < 0) |
e0a95d28 | 477 | return (1); |
86e4d161 | 478 | |
8cbdbd34 SS |
479 | #define READ_REMOTE_CHECK(c, exp) \ |
480 | res = read_remote(fd, 0, NULL); \ | |
481 | if (res == 5) { \ | |
482 | syslog(LOG_ERR, "remote delivery to %s [%s] failed after %s: %s", \ | |
483 | host->host, host->addr, c, neterr); \ | |
249ee966 | 484 | snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s", \ |
8cbdbd34 SS |
485 | host->host, host->addr, c, neterr); \ |
486 | return (-1); \ | |
487 | } else if (res != exp) { \ | |
488 | syslog(LOG_NOTICE, "remote delivery deferred: %s [%s] failed after %s: %s", \ | |
489 | host->host, host->addr, c, neterr); \ | |
490 | return (1); \ | |
491 | } | |
492 | ||
b295f51d | 493 | /* Check first reply from remote host */ |
ec04827a PP |
494 | if ((config.features & SECURETRANS) == 0 || |
495 | (config.features & STARTTLS) != 0) { | |
496 | config.features |= NOSSL; | |
497 | READ_REMOTE_CHECK("connect", 2); | |
b295f51d | 498 | |
ec04827a PP |
499 | config.features &= ~NOSSL; |
500 | } | |
b295f51d SS |
501 | |
502 | if ((config.features & SECURETRANS) != 0) { | |
1fa7a882 | 503 | error = smtp_init_crypto(fd, config.features, &features); |
a9d456bc | 504 | if (error == 0) |
b295f51d SS |
505 | syslog(LOG_DEBUG, "SSL initialization successful"); |
506 | else | |
507 | goto out; | |
ec04827a PP |
508 | |
509 | if ((config.features & STARTTLS) == 0) | |
510 | READ_REMOTE_CHECK("connect", 2); | |
b295f51d SS |
511 | } |
512 | ||
1fa7a882 MT |
513 | // Say EHLO |
514 | if (perform_server_greeting(fd, &features) != 0) { | |
515 | syslog(LOG_ERR, "Could not perform server greeting at %s [%s]: %s", | |
516 | host->host, host->addr, neterr); | |
517 | return -1; | |
518 | } | |
86e4d161 MS |
519 | |
520 | /* | |
521 | * Use SMTP authentication if the user defined an entry for the remote | |
522 | * or smarthost | |
523 | */ | |
524 | SLIST_FOREACH(a, &authusers, next) { | |
8cbdbd34 | 525 | if (strcmp(a->host, host->host) == 0) { |
86e4d161 MS |
526 | do_auth = 1; |
527 | break; | |
528 | } | |
529 | } | |
530 | ||
531 | if (do_auth == 1) { | |
dfce7456 MS |
532 | /* |
533 | * Check if the user wants plain text login without using | |
534 | * encryption. | |
535 | */ | |
a0c4afa6 | 536 | syslog(LOG_INFO, "using SMTP authentication for user %s", a->login); |
1fa7a882 | 537 | error = smtp_login(fd, a->login, a->password, &features); |
bf28fcc6 | 538 | if (error < 0) { |
4d5af2b0 SS |
539 | syslog(LOG_ERR, "remote delivery failed:" |
540 | " SMTP login failed: %m"); | |
249ee966 | 541 | snprintf(errmsg, sizeof(errmsg), "SMTP login to %s failed", host->host); |
e0a95d28 | 542 | return (-1); |
dfce7456 | 543 | } |
bf28fcc6 | 544 | /* SMTP login is not available, so try without */ |
4d5af2b0 SS |
545 | else if (error > 0) { |
546 | syslog(LOG_WARNING, "SMTP login not available. Trying without."); | |
547 | } | |
86e4d161 MS |
548 | } |
549 | ||
8945ce6c | 550 | /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */ |
c68aff7a SS |
551 | send_remote_command(fd, "MAIL FROM:<%s>", it->sender); |
552 | READ_REMOTE_CHECK("MAIL FROM", 2); | |
553 | ||
8945ce6c | 554 | /* XXX send ESMTP ORCPT */ |
e0a95d28 | 555 | send_remote_command(fd, "RCPT TO:<%s>", it->addr); |
c68aff7a | 556 | READ_REMOTE_CHECK("RCPT TO", 2); |
86e4d161 MS |
557 | |
558 | send_remote_command(fd, "DATA"); | |
c68aff7a | 559 | READ_REMOTE_CHECK("DATA", 3); |
86e4d161 | 560 | |
3a7b3ec9 | 561 | error = 0; |
fd1de51a SS |
562 | while (!feof(it->mailf)) { |
563 | if (fgets(line, sizeof(line), it->mailf) == NULL) | |
86e4d161 MS |
564 | break; |
565 | linelen = strlen(line); | |
566 | if (linelen == 0 || line[linelen - 1] != '\n') { | |
4d5af2b0 | 567 | syslog(LOG_CRIT, "remote delivery failed: corrupted queue file"); |
249ee966 | 568 | snprintf(errmsg, sizeof(errmsg), "corrupted queue file"); |
86e4d161 MS |
569 | error = -1; |
570 | goto out; | |
571 | } | |
572 | ||
573 | /* Remove trailing \n's and escape leading dots */ | |
574 | trim_line(line); | |
575 | ||
576 | /* | |
577 | * If the first character is a dot, we escape it so the line | |
578 | * length increases | |
579 | */ | |
580 | if (line[0] == '.') | |
581 | linelen++; | |
582 | ||
583 | if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) { | |
4d5af2b0 | 584 | syslog(LOG_NOTICE, "remote delivery deferred: write error"); |
86e4d161 MS |
585 | error = 1; |
586 | goto out; | |
587 | } | |
588 | } | |
589 | ||
590 | send_remote_command(fd, "."); | |
c68aff7a | 591 | READ_REMOTE_CHECK("final DATA", 2); |
86e4d161 MS |
592 | |
593 | send_remote_command(fd, "QUIT"); | |
83f3cc40 | 594 | if (read_remote(fd, 0, NULL) != 2) |
4d5af2b0 | 595 | syslog(LOG_INFO, "remote delivery succeeded but QUIT failed: %s", neterr); |
86e4d161 MS |
596 | out: |
597 | ||
8c365457 | 598 | close_connection(fd); |
86e4d161 MS |
599 | return (error); |
600 | } | |
601 | ||
8cbdbd34 | 602 | int |
249ee966 | 603 | deliver_remote(struct qitem *it) |
8cbdbd34 | 604 | { |
8cbdbd34 | 605 | struct mx_hostentry *hosts, *h; |
a0c4afa6 | 606 | const char *host; |
8cbdbd34 SS |
607 | int port; |
608 | int error = 1, smarthost = 0; | |
609 | ||
8cbdbd34 SS |
610 | port = SMTP_PORT; |
611 | ||
612 | /* Smarthost support? */ | |
a0c4afa6 SS |
613 | if (config.smarthost != NULL) { |
614 | host = config.smarthost; | |
615 | port = config.port; | |
616 | syslog(LOG_INFO, "using smarthost (%s:%i)", host, port); | |
8cbdbd34 | 617 | smarthost = 1; |
89702b7f SS |
618 | } else { |
619 | host = strrchr(it->addr, '@'); | |
620 | /* Should not happen */ | |
621 | if (host == NULL) { | |
622 | snprintf(errmsg, sizeof(errmsg), "Internal error: badly formed address %s", | |
623 | it->addr); | |
624 | return(-1); | |
625 | } else { | |
626 | /* Step over the @ */ | |
627 | host++; | |
628 | } | |
8cbdbd34 SS |
629 | } |
630 | ||
631 | error = dns_get_mx_list(host, port, &hosts, smarthost); | |
632 | if (error) { | |
5317696b SS |
633 | snprintf(errmsg, sizeof(errmsg), "DNS lookup failure: host %s not found", host); |
634 | syslog(LOG_NOTICE, "remote delivery %s: DNS lookup failure: host %s not found", | |
8cbdbd34 SS |
635 | error < 0 ? "failed" : "deferred", |
636 | host); | |
637 | return (error); | |
638 | } | |
639 | ||
640 | for (h = hosts; *h->host != 0; h++) { | |
249ee966 | 641 | switch (deliver_to_host(it, h)) { |
8cbdbd34 SS |
642 | case 0: |
643 | /* success */ | |
644 | error = 0; | |
645 | goto out; | |
646 | case 1: | |
647 | /* temp failure */ | |
648 | error = 1; | |
649 | break; | |
650 | default: | |
651 | /* perm failure */ | |
652 | error = -1; | |
653 | goto out; | |
654 | } | |
655 | } | |
656 | out: | |
657 | free(hosts); | |
658 | ||
659 | return (error); | |
660 | } |