| Commit | Line | Data |
|---|---|---|
| f67beddd 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. | |
| f67beddd MS |
34 | */ |
| 35 | ||
| c8b07ee5 SW |
36 | #include "dfcompat.h" |
| 37 | ||
| f67beddd 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 | ||
| f67beddd | 46 | #include <openssl/ssl.h> |
| e2c88018 | 47 | #include <openssl/err.h> |
| f67beddd | 48 | |
| c8b07ee5 | 49 | #include <ctype.h> |
| bc7baf1d | 50 | #include <err.h> |
| a5a8a1a4 | 51 | #include <errno.h> |
| f67beddd 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 | ||
| c8b07ee5 | 60 | char neterr[ERRMSG_SIZE]; |
| f67beddd | 61 | |
| e2c88018 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 | ||
| f67beddd MS |
74 | ssize_t |
| 75 | send_remote_command(int fd, const char* fmt, ...) | |
| 76 | { | |
| 77 | va_list va; | |
| 78 | char cmd[4096]; | |
| 2922fd2b SS |
79 | size_t len, pos; |
| 80 | int s; | |
| 81 | ssize_t n; | |
| f67beddd MS |
82 | |
| 83 | va_start(va, fmt); | |
| 2922fd2b SS |
84 | s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va); |
| 85 | va_end(va); | |
| e2c88018 SS |
86 | if (s == sizeof(cmd) - 2 || s < 0) { |
| 87 | strcpy(neterr, "Internal error: oversized command string"); | |
| 88 | return (-1); | |
| 89 | } | |
| 90 | ||
| 2922fd2b SS |
91 | /* We *know* there are at least two more bytes available */ |
| 92 | strcat(cmd, "\r\n"); | |
| 93 | len = strlen(cmd); | |
| f67beddd | 94 | |
| ca259d14 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); | |
| 2922fd2b | 99 | if (s != SSL_ERROR_WANT_READ && |
| e2c88018 SS |
100 | s != SSL_ERROR_WANT_WRITE) { |
| 101 | strncpy(neterr, ssl_errstr(), sizeof(neterr)); | |
| 2922fd2b | 102 | return (-1); |
| e2c88018 | 103 | } |
| 2922fd2b | 104 | } |
| f67beddd MS |
105 | } |
| 106 | else { | |
| 2922fd2b 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 | } | |
| f67beddd | 114 | } |
| f67beddd | 115 | |
| 2922fd2b | 116 | return (len); |
| f67beddd MS |
117 | } |
| 118 | ||
| 6ef9fe01 | 119 | int |
| 7b68d8ae | 120 | read_remote(int fd, int extbufsize, char *extbuf) |
| f67beddd | 121 | { |
| 6ef9fe01 | 122 | ssize_t rlen = 0; |
| c8b07ee5 | 123 | size_t pos, len, copysize; |
| 6ef9fe01 | 124 | char buff[BUF_SIZE]; |
| c8b07ee5 SW |
125 | int done = 0, status = 0, status_running = 0, extbufpos = 0; |
| 126 | enum { parse_status, parse_spacedash, parse_rest } parsestate; | |
| f67beddd | 127 | |
| c8b07ee5 | 128 | if (do_timeout(CON_TIMEOUT, 1) != 0) { |
| a5a8a1a4 | 129 | snprintf(neterr, sizeof(neterr), "Timeout reached"); |
| e2c88018 | 130 | return (-1); |
| f67beddd | 131 | } |
| f67beddd MS |
132 | |
| 133 | /* | |
| 6ef9fe01 MS |
134 | * Remote reading code from femail.c written by Henning Brauer of |
| 135 | * OpenBSD and released under a BSD style license. | |
| f67beddd | 136 | */ |
| c8b07ee5 SW |
137 | len = 0; |
| 138 | pos = 0; | |
| 139 | parsestate = parse_status; | |
| 140 | neterr[0] = 0; | |
| 141 | while (!(done && parsestate == parse_status)) { | |
| 7b68d8ae | 142 | rlen = 0; |
| 6ef9fe01 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; | |
| ca259d14 SS |
148 | if (((config.features & SECURETRANS) != 0) && |
| 149 | (config.features & NOSSL) == 0) { | |
| c8b07ee5 | 150 | if ((rlen = SSL_read(config.ssl, buff + len, sizeof(buff) - len)) == -1) { |
| e2c88018 | 151 | strncpy(neterr, ssl_errstr(), sizeof(neterr)); |
| c8b07ee5 | 152 | goto error; |
| e2c88018 | 153 | } |
| 6ef9fe01 | 154 | } else { |
| e2c88018 SS |
155 | if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) { |
| 156 | strncpy(neterr, strerror(errno), sizeof(neterr)); | |
| c8b07ee5 | 157 | goto error; |
| e2c88018 | 158 | } |
| 6ef9fe01 MS |
159 | } |
| 160 | len += rlen; | |
| c8b07ee5 SW |
161 | |
| 162 | copysize = sizeof(neterr) - strlen(neterr) - 1; | |
| 163 | if (copysize > len) | |
| 164 | copysize = len; | |
| 165 | strncat(neterr, buff, copysize); | |
| 6ef9fe01 | 166 | } |
| 7b68d8ae 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 | */ | |
| c8b07ee5 | 173 | if (extbufpos <= (extbufsize - 1) && rlen > 0 && extbufsize > 0 && extbuf != NULL) { |
| 7b68d8ae 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 | } | |
| f67beddd | 181 | |
| 6ef9fe01 | 182 | if (pos == len) |
| c8b07ee5 SW |
183 | continue; |
| 184 | ||
| 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; | |
| 204 | ||
| 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 | } | |
| f67beddd | 214 | |
| c8b07ee5 SW |
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 | } | |
| e2c88018 | 228 | } |
| f67beddd | 229 | |
| f67beddd | 230 | } |
| 6ef9fe01 | 231 | |
| c8b07ee5 SW |
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 | ||
| 6ef9fe01 | 238 | return (status/100); |
| c8b07ee5 SW |
239 | |
| 240 | error: | |
| 241 | do_timeout(0, 0); | |
| 242 | return (-1); | |
| f67beddd MS |
243 | } |
| 244 | ||
| 245 | /* | |
| 246 | * Handle SMTP authentication | |
| f67beddd MS |
247 | */ |
| 248 | static int | |
| 405f48ee | 249 | smtp_login(int fd, char *login, char* password) |
| f67beddd | 250 | { |
| f67beddd | 251 | char *temp; |
| 6ef9fe01 | 252 | int len, res = 0; |
| f67beddd | 253 | |
| 405f48ee | 254 | res = smtp_auth_md5(fd, login, password); |
| 7b68d8ae 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 | */ | |
| 5d7fe8bb | 262 | return (1); |
| f67beddd MS |
263 | } |
| 264 | ||
| ca259d14 SS |
265 | if ((config.features & INSECURE) != 0 || |
| 266 | (config.features & SECURETRANS) != 0) { | |
| 7b68d8ae MS |
267 | /* Send AUTH command according to RFC 2554 */ |
| 268 | send_remote_command(fd, "AUTH LOGIN"); | |
| 269 | if (read_remote(fd, 0, NULL) != 3) { | |
| 405f48ee | 270 | syslog(LOG_NOTICE, "remote delivery deferred:" |
| a5a8a1a4 | 271 | " AUTH login not available: %s", |
| 405f48ee | 272 | neterr); |
| 7b68d8ae MS |
273 | return (1); |
| 274 | } | |
| f67beddd | 275 | |
| 7b68d8ae | 276 | len = base64_encode(login, strlen(login), &temp); |
| 5d7fe8bb SS |
277 | if (len < 0) { |
| 278 | encerr: | |
| 405f48ee | 279 | syslog(LOG_ERR, "can not encode auth reply: %m"); |
| 5d7fe8bb SS |
280 | return (1); |
| 281 | } | |
| f67beddd | 282 | |
| 7b68d8ae | 283 | send_remote_command(fd, "%s", temp); |
| 5d7fe8bb | 284 | free(temp); |
| e2c88018 SS |
285 | res = read_remote(fd, 0, NULL); |
| 286 | if (res != 3) { | |
| 405f48ee SS |
287 | syslog(LOG_NOTICE, "remote delivery %s: AUTH login failed: %s", |
| 288 | res == 5 ? "failed" : "deferred", neterr); | |
| e2c88018 | 289 | return (res == 5 ? -1 : 1); |
| 7b68d8ae | 290 | } |
| f67beddd | 291 | |
| 7b68d8ae | 292 | len = base64_encode(password, strlen(password), &temp); |
| 5d7fe8bb SS |
293 | if (len < 0) |
| 294 | goto encerr; | |
| 7b68d8ae MS |
295 | |
| 296 | send_remote_command(fd, "%s", temp); | |
| 5d7fe8bb | 297 | free(temp); |
| 7b68d8ae | 298 | res = read_remote(fd, 0, NULL); |
| e2c88018 | 299 | if (res != 2) { |
| 405f48ee SS |
300 | syslog(LOG_NOTICE, "remote delivery %s: Authentication failed: %s", |
| 301 | res == 5 ? "failed" : "deferred", neterr); | |
| e2c88018 | 302 | return (res == 5 ? -1 : 1); |
| 7b68d8ae MS |
303 | } |
| 304 | } else { | |
| 405f48ee | 305 | syslog(LOG_WARNING, "non-encrypted SMTP login is disabled in config, so skipping it. "); |
| 7b68d8ae | 306 | return (1); |
| f67beddd MS |
307 | } |
| 308 | ||
| 309 | return (0); | |
| 310 | } | |
| 311 | ||
| 312 | static int | |
| 3021968a | 313 | open_connection(struct mx_hostentry *h) |
| f67beddd | 314 | { |
| 3021968a 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); | |
| f67beddd MS |
324 | return (-1); |
| 325 | } | |
| 3021968a | 326 | |
| 725a35c6 | 327 | if (connect(fd, (struct sockaddr *)&h->sa, h->ai.ai_addrlen) < 0) { |
| 3021968a SS |
328 | syslog(LOG_INFO, "connect to %s [%s] failed: %m", |
| 329 | h->host, h->addr); | |
| 330 | close(fd); | |
| f67beddd MS |
331 | return (-1); |
| 332 | } | |
| 3021968a | 333 | |
| f67beddd MS |
334 | return (fd); |
| 335 | } | |
| 336 | ||
| 10eeb0df SS |
337 | static void |
| 338 | close_connection(int fd) | |
| 339 | { | |
| de30bd07 MS |
340 | if (config.ssl != NULL) { |
| 341 | if (((config.features & SECURETRANS) != 0) && | |
| 342 | ((config.features & NOSSL) == 0)) | |
| 343 | SSL_shutdown(config.ssl); | |
| ca259d14 | 344 | SSL_free(config.ssl); |
| de30bd07 | 345 | } |
| 10eeb0df SS |
346 | |
| 347 | close(fd); | |
| 348 | } | |
| 349 | ||
| 3021968a | 350 | static int |
| c8b07ee5 | 351 | deliver_to_host(struct qitem *it, struct mx_hostentry *host) |
| f67beddd MS |
352 | { |
| 353 | struct authuser *a; | |
| 3021968a | 354 | char line[1000]; |
| f67beddd | 355 | size_t linelen; |
| 3021968a | 356 | int fd, error = 0, do_auth = 0, res = 0; |
| f67beddd | 357 | |
| ebffba26 | 358 | if (fseek(it->mailf, 0, SEEK_SET) != 0) { |
| c8b07ee5 | 359 | snprintf(errmsg, sizeof(errmsg), "can not seek: %s", strerror(errno)); |
| 7d35694e SS |
360 | return (-1); |
| 361 | } | |
| 362 | ||
| 405f48ee | 363 | fd = open_connection(host); |
| f67beddd | 364 | if (fd < 0) |
| 4a23bd3d | 365 | return (1); |
| f67beddd | 366 | |
| 3021968a 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); \ | |
| c8b07ee5 | 372 | snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s", \ |
| 3021968a 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 | ||
| b8b7d065 | 381 | /* Check first reply from remote host */ |
| c8b07ee5 SW |
382 | if ((config.features & SECURETRANS) == 0 || |
| 383 | (config.features & STARTTLS) != 0) { | |
| 384 | config.features |= NOSSL; | |
| 385 | READ_REMOTE_CHECK("connect", 2); | |
| b8b7d065 | 386 | |
| c8b07ee5 SW |
387 | config.features &= ~NOSSL; |
| 388 | } | |
| b8b7d065 SS |
389 | |
| 390 | if ((config.features & SECURETRANS) != 0) { | |
| 391 | error = smtp_init_crypto(fd, config.features); | |
| 655c0566 | 392 | if (error == 0) |
| b8b7d065 SS |
393 | syslog(LOG_DEBUG, "SSL initialization successful"); |
| 394 | else | |
| 395 | goto out; | |
| c8b07ee5 SW |
396 | |
| 397 | if ((config.features & STARTTLS) == 0) | |
| 398 | READ_REMOTE_CHECK("connect", 2); | |
| b8b7d065 SS |
399 | } |
| 400 | ||
| c21e2cfd SS |
401 | /* XXX allow HELO fallback */ |
| 402 | /* XXX record ESMTP keywords */ | |
| 66674731 | 403 | send_remote_command(fd, "EHLO %s", hostname()); |
| 3021968a | 404 | READ_REMOTE_CHECK("EHLO", 2); |
| f67beddd MS |
405 | |
| 406 | /* | |
| 407 | * Use SMTP authentication if the user defined an entry for the remote | |
| 408 | * or smarthost | |
| 409 | */ | |
| 410 | SLIST_FOREACH(a, &authusers, next) { | |
| 3021968a | 411 | if (strcmp(a->host, host->host) == 0) { |
| f67beddd MS |
412 | do_auth = 1; |
| 413 | break; | |
| 414 | } | |
| 415 | } | |
| 416 | ||
| 417 | if (do_auth == 1) { | |
| b558d098 MS |
418 | /* |
| 419 | * Check if the user wants plain text login without using | |
| 420 | * encryption. | |
| 421 | */ | |
| ca259d14 | 422 | syslog(LOG_INFO, "using SMTP authentication for user %s", a->login); |
| 405f48ee | 423 | error = smtp_login(fd, a->login, a->password); |
| 7b68d8ae | 424 | if (error < 0) { |
| 405f48ee SS |
425 | syslog(LOG_ERR, "remote delivery failed:" |
| 426 | " SMTP login failed: %m"); | |
| c8b07ee5 | 427 | snprintf(errmsg, sizeof(errmsg), "SMTP login to %s failed", host->host); |
| 4a23bd3d | 428 | return (-1); |
| b558d098 | 429 | } |
| 7b68d8ae | 430 | /* SMTP login is not available, so try without */ |
| 405f48ee SS |
431 | else if (error > 0) { |
| 432 | syslog(LOG_WARNING, "SMTP login not available. Trying without."); | |
| 433 | } | |
| f67beddd MS |
434 | } |
| 435 | ||
| c21e2cfd | 436 | /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */ |
| 7dfd2fd8 SS |
437 | send_remote_command(fd, "MAIL FROM:<%s>", it->sender); |
| 438 | READ_REMOTE_CHECK("MAIL FROM", 2); | |
| 439 | ||
| c21e2cfd | 440 | /* XXX send ESMTP ORCPT */ |
| 4a23bd3d | 441 | send_remote_command(fd, "RCPT TO:<%s>", it->addr); |
| 7dfd2fd8 | 442 | READ_REMOTE_CHECK("RCPT TO", 2); |
| f67beddd MS |
443 | |
| 444 | send_remote_command(fd, "DATA"); | |
| 7dfd2fd8 | 445 | READ_REMOTE_CHECK("DATA", 3); |
| f67beddd | 446 | |
| 0caaabf6 | 447 | error = 0; |
| f4e61a9f SS |
448 | while (!feof(it->mailf)) { |
| 449 | if (fgets(line, sizeof(line), it->mailf) == NULL) | |
| f67beddd MS |
450 | break; |
| 451 | linelen = strlen(line); | |
| 452 | if (linelen == 0 || line[linelen - 1] != '\n') { | |
| 405f48ee | 453 | syslog(LOG_CRIT, "remote delivery failed: corrupted queue file"); |
| c8b07ee5 | 454 | snprintf(errmsg, sizeof(errmsg), "corrupted queue file"); |
| f67beddd MS |
455 | error = -1; |
| 456 | goto out; | |
| 457 | } | |
| 458 | ||
| 459 | /* Remove trailing \n's and escape leading dots */ | |
| 460 | trim_line(line); | |
| 461 | ||
| 462 | /* | |
| 463 | * If the first character is a dot, we escape it so the line | |
| 464 | * length increases | |
| 465 | */ | |
| 466 | if (line[0] == '.') | |
| 467 | linelen++; | |
| 468 | ||
| 469 | if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) { | |
| 405f48ee | 470 | syslog(LOG_NOTICE, "remote delivery deferred: write error"); |
| f67beddd MS |
471 | error = 1; |
| 472 | goto out; | |
| 473 | } | |
| 474 | } | |
| 475 | ||
| 476 | send_remote_command(fd, "."); | |
| 7dfd2fd8 | 477 | READ_REMOTE_CHECK("final DATA", 2); |
| f67beddd MS |
478 | |
| 479 | send_remote_command(fd, "QUIT"); | |
| 6cfc247d | 480 | if (read_remote(fd, 0, NULL) != 2) |
| 405f48ee | 481 | syslog(LOG_INFO, "remote delivery succeeded but QUIT failed: %s", neterr); |
| f67beddd MS |
482 | out: |
| 483 | ||
| 10eeb0df | 484 | close_connection(fd); |
| f67beddd MS |
485 | return (error); |
| 486 | } | |
| 487 | ||
| 3021968a | 488 | int |
| c8b07ee5 | 489 | deliver_remote(struct qitem *it) |
| 3021968a | 490 | { |
| 3021968a | 491 | struct mx_hostentry *hosts, *h; |
| ca259d14 | 492 | const char *host; |
| 3021968a SS |
493 | int port; |
| 494 | int error = 1, smarthost = 0; | |
| 495 | ||
| 496 | host = strrchr(it->addr, '@'); | |
| 497 | /* Should not happen */ | |
| 498 | if (host == NULL) { | |
| c8b07ee5 | 499 | snprintf(errmsg, sizeof(errmsg), "Internal error: badly formed address %s", |
| 3021968a SS |
500 | it->addr); |
| 501 | return(-1); | |
| 502 | } else { | |
| 503 | /* Step over the @ */ | |
| 504 | host++; | |
| 505 | } | |
| 506 | ||
| 507 | port = SMTP_PORT; | |
| 508 | ||
| 509 | /* Smarthost support? */ | |
| ca259d14 SS |
510 | if (config.smarthost != NULL) { |
| 511 | host = config.smarthost; | |
| 512 | port = config.port; | |
| 513 | syslog(LOG_INFO, "using smarthost (%s:%i)", host, port); | |
| 3021968a | 514 | smarthost = 1; |
| 3021968a SS |
515 | } |
| 516 | ||
| 517 | error = dns_get_mx_list(host, port, &hosts, smarthost); | |
| 518 | if (error) { | |
| 519 | syslog(LOG_NOTICE, "remote delivery %s: DNS failure (%s)", | |
| 520 | error < 0 ? "failed" : "deferred", | |
| 521 | host); | |
| 522 | return (error); | |
| 523 | } | |
| 524 | ||
| 525 | for (h = hosts; *h->host != 0; h++) { | |
| c8b07ee5 | 526 | switch (deliver_to_host(it, h)) { |
| 3021968a SS |
527 | case 0: |
| 528 | /* success */ | |
| 529 | error = 0; | |
| 530 | goto out; | |
| 531 | case 1: | |
| 532 | /* temp failure */ | |
| 533 | error = 1; | |
| 534 | break; | |
| 535 | default: | |
| 536 | /* perm failure */ | |
| 537 | error = -1; | |
| 538 | goto out; | |
| 539 | } | |
| 540 | } | |
| 541 | out: | |
| 542 | free(hosts); | |
| 543 | ||
| 544 | return (error); | |
| 545 | } |