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