1f67bedddSMatthias Schmidt /* 2f67bedddSMatthias Schmidt * Copyright (c) 2008 The DragonFly Project. All rights reserved. 3f67bedddSMatthias Schmidt * 4f67bedddSMatthias Schmidt * This code is derived from software contributed to The DragonFly Project 5f67bedddSMatthias Schmidt * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg, 6f67bedddSMatthias Schmidt * Germany. 7f67bedddSMatthias Schmidt * 8f67bedddSMatthias Schmidt * Redistribution and use in source and binary forms, with or without 9f67bedddSMatthias Schmidt * modification, are permitted provided that the following conditions 10f67bedddSMatthias Schmidt * are met: 11f67bedddSMatthias Schmidt * 12f67bedddSMatthias Schmidt * 1. Redistributions of source code must retain the above copyright 13f67bedddSMatthias Schmidt * notice, this list of conditions and the following disclaimer. 14f67bedddSMatthias Schmidt * 2. Redistributions in binary form must reproduce the above copyright 15f67bedddSMatthias Schmidt * notice, this list of conditions and the following disclaimer in 16f67bedddSMatthias Schmidt * the documentation and/or other materials provided with the 17f67bedddSMatthias Schmidt * distribution. 18f67bedddSMatthias Schmidt * 3. Neither the name of The DragonFly Project nor the names of its 19f67bedddSMatthias Schmidt * contributors may be used to endorse or promote products derived 20f67bedddSMatthias Schmidt * from this software without specific, prior written permission. 21f67bedddSMatthias Schmidt * 22f67bedddSMatthias Schmidt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23f67bedddSMatthias Schmidt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24f67bedddSMatthias Schmidt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25f67bedddSMatthias Schmidt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26f67bedddSMatthias Schmidt * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27f67bedddSMatthias Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28f67bedddSMatthias Schmidt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29f67bedddSMatthias Schmidt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30f67bedddSMatthias Schmidt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31f67bedddSMatthias Schmidt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32f67bedddSMatthias Schmidt * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33f67bedddSMatthias Schmidt * SUCH DAMAGE. 34f67bedddSMatthias Schmidt * 35bc7baf1dSSascha Wildner * $DragonFly: src/libexec/dma/net.c,v 1.9 2008/09/30 17:47:21 swildner Exp $ 36f67bedddSMatthias Schmidt */ 37f67bedddSMatthias Schmidt 38f67bedddSMatthias Schmidt #include <sys/param.h> 39f67bedddSMatthias Schmidt #include <sys/queue.h> 40f67bedddSMatthias Schmidt #include <sys/stat.h> 41f67bedddSMatthias Schmidt #include <sys/types.h> 42f67bedddSMatthias Schmidt #include <sys/socket.h> 43f67bedddSMatthias Schmidt #include <netinet/in.h> 44f67bedddSMatthias Schmidt #include <arpa/inet.h> 45f67bedddSMatthias Schmidt 46f67bedddSMatthias Schmidt #include <openssl/ssl.h> 47f67bedddSMatthias Schmidt 48bc7baf1dSSascha Wildner #include <err.h> 49a5a8a1a4SSimon Schubert #include <errno.h> 50f67bedddSMatthias Schmidt #include <netdb.h> 51f67bedddSMatthias Schmidt #include <setjmp.h> 52f67bedddSMatthias Schmidt #include <signal.h> 53f67bedddSMatthias Schmidt #include <syslog.h> 54f67bedddSMatthias Schmidt #include <unistd.h> 55f67bedddSMatthias Schmidt 56f67bedddSMatthias Schmidt #include "dma.h" 57f67bedddSMatthias Schmidt 58f67bedddSMatthias Schmidt extern struct config *config; 59f67bedddSMatthias Schmidt extern struct authusers authusers; 60f67bedddSMatthias Schmidt static jmp_buf timeout_alarm; 61a5a8a1a4SSimon Schubert char neterr[BUF_SIZE]; 62f67bedddSMatthias Schmidt 63f67bedddSMatthias Schmidt static void 64dba19026SMatthias Schmidt sig_alarm(int signo __unused) 65f67bedddSMatthias Schmidt { 66f67bedddSMatthias Schmidt longjmp(timeout_alarm, 1); 67f67bedddSMatthias Schmidt } 68f67bedddSMatthias Schmidt 69f67bedddSMatthias Schmidt ssize_t 70f67bedddSMatthias Schmidt send_remote_command(int fd, const char* fmt, ...) 71f67bedddSMatthias Schmidt { 72f67bedddSMatthias Schmidt va_list va; 73f67bedddSMatthias Schmidt char cmd[4096]; 742922fd2bSSimon Schubert size_t len, pos; 752922fd2bSSimon Schubert int s; 762922fd2bSSimon Schubert ssize_t n; 77f67bedddSMatthias Schmidt 78f67bedddSMatthias Schmidt va_start(va, fmt); 792922fd2bSSimon Schubert s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va); 802922fd2bSSimon Schubert va_end(va); 812922fd2bSSimon Schubert if (s == sizeof(cmd) - 2 || s < 0) 822922fd2bSSimon Schubert errx(1, "Internal error: oversized command string"); 832922fd2bSSimon Schubert /* We *know* there are at least two more bytes available */ 842922fd2bSSimon Schubert strcat(cmd, "\r\n"); 852922fd2bSSimon Schubert len = strlen(cmd); 86f67bedddSMatthias Schmidt 87f67bedddSMatthias Schmidt if (((config->features & SECURETRANS) != 0) && 886ef9fe01SMatthias Schmidt ((config->features & NOSSL) == 0)) { 892922fd2bSSimon Schubert while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) { 902922fd2bSSimon Schubert s = SSL_get_error(config->ssl, s); 912922fd2bSSimon Schubert if (s != SSL_ERROR_WANT_READ && 922922fd2bSSimon Schubert s != SSL_ERROR_WANT_WRITE) 932922fd2bSSimon Schubert return (-1); 942922fd2bSSimon Schubert } 95f67bedddSMatthias Schmidt } 96f67bedddSMatthias Schmidt else { 972922fd2bSSimon Schubert pos = 0; 982922fd2bSSimon Schubert while (pos < len) { 992922fd2bSSimon Schubert n = write(fd, cmd + pos, len - pos); 1002922fd2bSSimon Schubert if (n < 0) 1012922fd2bSSimon Schubert return (-1); 1022922fd2bSSimon Schubert pos += n; 103f67bedddSMatthias Schmidt } 1042922fd2bSSimon Schubert } 105f67bedddSMatthias Schmidt 1062922fd2bSSimon Schubert return (len); 107f67bedddSMatthias Schmidt } 108f67bedddSMatthias Schmidt 1096ef9fe01SMatthias Schmidt int 1107b68d8aeSMatthias Schmidt read_remote(int fd, int extbufsize, char *extbuf) 111f67bedddSMatthias Schmidt { 1126ef9fe01SMatthias Schmidt ssize_t rlen = 0; 1136ef9fe01SMatthias Schmidt size_t pos, len; 1146ef9fe01SMatthias Schmidt char buff[BUF_SIZE]; 1157b68d8aeSMatthias Schmidt int done = 0, status = 0, extbufpos = 0; 116f67bedddSMatthias Schmidt 117f67bedddSMatthias Schmidt if (signal(SIGALRM, sig_alarm) == SIG_ERR) { 118a5a8a1a4SSimon Schubert snprintf(neterr, sizeof(neterr), "SIGALRM error: %s", 119a5a8a1a4SSimon Schubert strerror(errno)); 120a5a8a1a4SSimon Schubert return (1); 121f67bedddSMatthias Schmidt } 122f67bedddSMatthias Schmidt if (setjmp(timeout_alarm) != 0) { 123a5a8a1a4SSimon Schubert snprintf(neterr, sizeof(neterr), "Timeout reached"); 124f67bedddSMatthias Schmidt return (1); 125f67bedddSMatthias Schmidt } 126f67bedddSMatthias Schmidt alarm(CON_TIMEOUT); 127f67bedddSMatthias Schmidt 128f67bedddSMatthias Schmidt /* 1296ef9fe01SMatthias Schmidt * Remote reading code from femail.c written by Henning Brauer of 1306ef9fe01SMatthias Schmidt * OpenBSD and released under a BSD style license. 131f67bedddSMatthias Schmidt */ 1326ef9fe01SMatthias Schmidt for (len = pos = 0; !done; ) { 1337b68d8aeSMatthias Schmidt rlen = 0; 1346ef9fe01SMatthias Schmidt if (pos == 0 || 1356ef9fe01SMatthias Schmidt (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) { 1366ef9fe01SMatthias Schmidt memmove(buff, buff + pos, len - pos); 1376ef9fe01SMatthias Schmidt len -= pos; 1386ef9fe01SMatthias Schmidt pos = 0; 139f67bedddSMatthias Schmidt if (((config->features & SECURETRANS) != 0) && 1406ef9fe01SMatthias Schmidt (config->features & NOSSL) == 0) { 1416ef9fe01SMatthias Schmidt if ((rlen = SSL_read(config->ssl, buff + len, 1426ef9fe01SMatthias Schmidt sizeof(buff) - len)) == -1) 1436ef9fe01SMatthias Schmidt err(1, "read"); 1446ef9fe01SMatthias Schmidt } else { 1456ef9fe01SMatthias Schmidt if ((rlen = read(fd, buff + len, 1466ef9fe01SMatthias Schmidt sizeof(buff) - len)) == -1) 1476ef9fe01SMatthias Schmidt err(1, "read"); 1486ef9fe01SMatthias Schmidt } 1496ef9fe01SMatthias Schmidt len += rlen; 1506ef9fe01SMatthias Schmidt } 1517b68d8aeSMatthias Schmidt /* 1527b68d8aeSMatthias Schmidt * If there is an external buffer with a size bigger than zero 1537b68d8aeSMatthias Schmidt * and as long as there is space in the external buffer and 1547b68d8aeSMatthias Schmidt * there are new characters read from the mailserver 1557b68d8aeSMatthias Schmidt * copy them to the external buffer 1567b68d8aeSMatthias Schmidt */ 1577b68d8aeSMatthias Schmidt if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0 1587b68d8aeSMatthias Schmidt && extbuf != NULL) { 1597b68d8aeSMatthias Schmidt /* do not write over the bounds of the buffer */ 1607b68d8aeSMatthias Schmidt if(extbufpos + rlen > (extbufsize - 1)) { 1617b68d8aeSMatthias Schmidt rlen = extbufsize - extbufpos; 1627b68d8aeSMatthias Schmidt } 1637b68d8aeSMatthias Schmidt memcpy(extbuf + extbufpos, buff + len - rlen, rlen); 1647b68d8aeSMatthias Schmidt extbufpos += rlen; 1657b68d8aeSMatthias Schmidt } 1666ef9fe01SMatthias Schmidt for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++) 1676ef9fe01SMatthias Schmidt ; /* Do nothing */ 168f67bedddSMatthias Schmidt 1696ef9fe01SMatthias Schmidt if (pos == len) 1706ef9fe01SMatthias Schmidt return (0); 1716ef9fe01SMatthias Schmidt 1726ef9fe01SMatthias Schmidt if (buff[pos] == ' ') 1736ef9fe01SMatthias Schmidt done = 1; 1746ef9fe01SMatthias Schmidt else if (buff[pos] != '-') 1756ef9fe01SMatthias Schmidt syslog(LOG_ERR, "invalid syntax in reply from server"); 1766ef9fe01SMatthias Schmidt 1776ef9fe01SMatthias Schmidt /* skip up to \n */ 1786ef9fe01SMatthias Schmidt for (; pos < len && buff[pos - 1] != '\n'; pos++) 1796ef9fe01SMatthias Schmidt ; /* Do nothing */ 1806ef9fe01SMatthias Schmidt 1816ef9fe01SMatthias Schmidt } 182f67bedddSMatthias Schmidt alarm(0); 183f67bedddSMatthias Schmidt 184a5a8a1a4SSimon Schubert buff[len] = '\0'; 185a5a8a1a4SSimon Schubert while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n')) 186a5a8a1a4SSimon Schubert buff[--len] = '\0'; 187a5a8a1a4SSimon Schubert snprintf(neterr, sizeof(neterr), "%s", buff); 1886ef9fe01SMatthias Schmidt status = atoi(buff); 1896ef9fe01SMatthias Schmidt return (status/100); 190f67bedddSMatthias Schmidt } 191f67bedddSMatthias Schmidt 192f67bedddSMatthias Schmidt /* 193f67bedddSMatthias Schmidt * Handle SMTP authentication 194f67bedddSMatthias Schmidt */ 195f67bedddSMatthias Schmidt static int 196f67bedddSMatthias Schmidt smtp_login(struct qitem *it, int fd, char *login, char* password) 197f67bedddSMatthias Schmidt { 198f67bedddSMatthias Schmidt char *temp; 1996ef9fe01SMatthias Schmidt int len, res = 0; 200f67bedddSMatthias Schmidt 2017b68d8aeSMatthias Schmidt res = smtp_auth_md5(it, fd, login, password); 2027b68d8aeSMatthias Schmidt if (res == 0) { 2037b68d8aeSMatthias Schmidt return (0); 2047b68d8aeSMatthias Schmidt } else if (res == -2) { 2057b68d8aeSMatthias Schmidt /* 2067b68d8aeSMatthias Schmidt * If the return code is -2, then then the login attempt failed, 2077b68d8aeSMatthias Schmidt * do not try other login mechanisms 2087b68d8aeSMatthias Schmidt */ 2097b68d8aeSMatthias Schmidt return (-1); 2107b68d8aeSMatthias Schmidt } 2117b68d8aeSMatthias Schmidt 2127b68d8aeSMatthias Schmidt if ((config->features & INSECURE) != 0) { 213f67bedddSMatthias Schmidt /* Send AUTH command according to RFC 2554 */ 214f67bedddSMatthias Schmidt send_remote_command(fd, "AUTH LOGIN"); 2157b68d8aeSMatthias Schmidt if (read_remote(fd, 0, NULL) != 3) { 21699d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred:" 217a5a8a1a4SSimon Schubert " AUTH login not available: %s", 218a5a8a1a4SSimon Schubert it->queueid, neterr); 219f67bedddSMatthias Schmidt return (1); 220f67bedddSMatthias Schmidt } 221f67bedddSMatthias Schmidt 222f67bedddSMatthias Schmidt len = base64_encode(login, strlen(login), &temp); 223f67bedddSMatthias Schmidt if (len <= 0) 224f67bedddSMatthias Schmidt return (-1); 225f67bedddSMatthias Schmidt 226f67bedddSMatthias Schmidt send_remote_command(fd, "%s", temp); 2277b68d8aeSMatthias Schmidt if (read_remote(fd, 0, NULL) != 3) { 22899d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred:" 229a5a8a1a4SSimon Schubert " AUTH login failed: %s", it->queueid, 230a5a8a1a4SSimon Schubert neterr); 231f67bedddSMatthias Schmidt return (-1); 232f67bedddSMatthias Schmidt } 233f67bedddSMatthias Schmidt 234f67bedddSMatthias Schmidt len = base64_encode(password, strlen(password), &temp); 235f67bedddSMatthias Schmidt if (len <= 0) 236f67bedddSMatthias Schmidt return (-1); 237f67bedddSMatthias Schmidt 238f67bedddSMatthias Schmidt send_remote_command(fd, "%s", temp); 2397b68d8aeSMatthias Schmidt res = read_remote(fd, 0, NULL); 2406ef9fe01SMatthias Schmidt if (res == 5) { 24199d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery failed:" 242a5a8a1a4SSimon Schubert " Authentication failed: %s", 243a5a8a1a4SSimon Schubert it->queueid, neterr); 2446ef9fe01SMatthias Schmidt return (-1); 2456ef9fe01SMatthias Schmidt } else if (res != 2) { 24699d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery failed:" 247a5a8a1a4SSimon Schubert " AUTH password failed: %s", 248a5a8a1a4SSimon Schubert it->queueid, neterr); 249f67bedddSMatthias Schmidt return (-1); 250f67bedddSMatthias Schmidt } 2517b68d8aeSMatthias Schmidt } else { 25299d6192fSSimon Schubert syslog(LOG_WARNING, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ", 2537b68d8aeSMatthias Schmidt it->queueid); 2547b68d8aeSMatthias Schmidt return (1); 2557b68d8aeSMatthias Schmidt } 256f67bedddSMatthias Schmidt 257f67bedddSMatthias Schmidt return (0); 258f67bedddSMatthias Schmidt } 259f67bedddSMatthias Schmidt 260f67bedddSMatthias Schmidt static int 261f67bedddSMatthias Schmidt open_connection(struct qitem *it, const char *host) 262f67bedddSMatthias Schmidt { 263f67bedddSMatthias Schmidt struct addrinfo hints, *res, *res0; 264f67bedddSMatthias Schmidt char servname[128]; 265f67bedddSMatthias Schmidt const char *errmsg = NULL; 266f67bedddSMatthias Schmidt int fd, error = 0, port; 267f67bedddSMatthias Schmidt 268dba19026SMatthias Schmidt if (config->port != 0) 269f67bedddSMatthias Schmidt port = config->port; 270f67bedddSMatthias Schmidt else 271f67bedddSMatthias Schmidt port = SMTP_PORT; 272f67bedddSMatthias Schmidt 2737b68d8aeSMatthias Schmidt /* FIXME get MX record of host */ 274f67bedddSMatthias Schmidt /* Shamelessly taken from getaddrinfo(3) */ 275f67bedddSMatthias Schmidt memset(&hints, 0, sizeof(hints)); 276f67bedddSMatthias Schmidt hints.ai_family = PF_UNSPEC; 277f67bedddSMatthias Schmidt hints.ai_socktype = SOCK_STREAM; 278f67bedddSMatthias Schmidt hints.ai_protocol = IPPROTO_TCP; 279f67bedddSMatthias Schmidt 280f67bedddSMatthias Schmidt snprintf(servname, sizeof(servname), "%d", port); 281f67bedddSMatthias Schmidt error = getaddrinfo(host, servname, &hints, &res0); 282f67bedddSMatthias Schmidt if (error) { 28399d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: " 284f67bedddSMatthias Schmidt "%s: %m", it->queueid, gai_strerror(error)); 285f67bedddSMatthias Schmidt return (-1); 286f67bedddSMatthias Schmidt } 287f67bedddSMatthias Schmidt fd = -1; 288f67bedddSMatthias Schmidt for (res = res0; res; res = res->ai_next) { 289f67bedddSMatthias Schmidt fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol); 290f67bedddSMatthias Schmidt if (fd < 0) { 291f67bedddSMatthias Schmidt errmsg = "socket failed"; 292f67bedddSMatthias Schmidt continue; 293f67bedddSMatthias Schmidt } 294f67bedddSMatthias Schmidt if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 295f67bedddSMatthias Schmidt errmsg = "connect failed"; 296f67bedddSMatthias Schmidt close(fd); 297f67bedddSMatthias Schmidt fd = -1; 298f67bedddSMatthias Schmidt continue; 299f67bedddSMatthias Schmidt } 300f67bedddSMatthias Schmidt break; 301f67bedddSMatthias Schmidt } 302f67bedddSMatthias Schmidt if (fd < 0) { 30399d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: %s (%s:%s)", 304f67bedddSMatthias Schmidt it->queueid, errmsg, host, servname); 305f67bedddSMatthias Schmidt freeaddrinfo(res0); 306f67bedddSMatthias Schmidt return (-1); 307f67bedddSMatthias Schmidt } 308f67bedddSMatthias Schmidt freeaddrinfo(res0); 309f67bedddSMatthias Schmidt return (fd); 310f67bedddSMatthias Schmidt } 311f67bedddSMatthias Schmidt 312*10eeb0dfSSimon Schubert static void 313*10eeb0dfSSimon Schubert close_connection(int fd) 314*10eeb0dfSSimon Schubert { 315*10eeb0dfSSimon Schubert if (((config->features & SECURETRANS) != 0) && 316*10eeb0dfSSimon Schubert ((config->features & NOSSL) == 0)) 317*10eeb0dfSSimon Schubert SSL_shutdown(config->ssl); 318*10eeb0dfSSimon Schubert 319*10eeb0dfSSimon Schubert if (config->ssl != NULL) 320*10eeb0dfSSimon Schubert SSL_free(config->ssl); 321*10eeb0dfSSimon Schubert 322*10eeb0dfSSimon Schubert close(fd); 323*10eeb0dfSSimon Schubert } 324*10eeb0dfSSimon Schubert 325f67bedddSMatthias Schmidt int 3265868e44cSSimon Schubert deliver_remote(struct qitem *it, const char **errmsg) 327f67bedddSMatthias Schmidt { 328f67bedddSMatthias Schmidt struct authuser *a; 3296ef9fe01SMatthias Schmidt char *host, line[1000]; 3306ef9fe01SMatthias Schmidt int fd, error = 0, do_auth = 0, res = 0; 331f67bedddSMatthias Schmidt size_t linelen; 3325868e44cSSimon Schubert /* asprintf can't take const */ 3335868e44cSSimon Schubert void *errmsgc = __DECONST(char **, errmsg); 334f67bedddSMatthias Schmidt 335f67bedddSMatthias Schmidt host = strrchr(it->addr, '@'); 336f67bedddSMatthias Schmidt /* Should not happen */ 33739f7c85aSSimon Schubert if (host == NULL) { 3385868e44cSSimon Schubert asprintf(errmsgc, "Internal error: badly formed address %s", 33939f7c85aSSimon Schubert it->addr); 340f67bedddSMatthias Schmidt return(-1); 34139f7c85aSSimon Schubert } else { 342f67bedddSMatthias Schmidt /* Step over the @ */ 343f67bedddSMatthias Schmidt host++; 34439f7c85aSSimon Schubert } 345f67bedddSMatthias Schmidt 346f67bedddSMatthias Schmidt /* Smarthost support? */ 347f67bedddSMatthias Schmidt if (config->smarthost != NULL && strlen(config->smarthost) > 0) { 3484a23bd3dSMatthias Schmidt syslog(LOG_INFO, "%s: using smarthost (%s:%i)", 3494a23bd3dSMatthias Schmidt it->queueid, config->smarthost, config->port); 350f67bedddSMatthias Schmidt host = config->smarthost; 351f67bedddSMatthias Schmidt } 352f67bedddSMatthias Schmidt 353f67bedddSMatthias Schmidt fd = open_connection(it, host); 354f67bedddSMatthias Schmidt if (fd < 0) 355f67bedddSMatthias Schmidt return (1); 356f67bedddSMatthias Schmidt 3576ef9fe01SMatthias Schmidt /* Check first reply from remote host */ 3586ef9fe01SMatthias Schmidt config->features |= NOSSL; 3597b68d8aeSMatthias Schmidt res = read_remote(fd, 0, NULL); 3606ef9fe01SMatthias Schmidt if (res != 2) { 36199d6192fSSimon Schubert syslog(LOG_WARNING, "%s: Invalid initial response: %i", 3626ef9fe01SMatthias Schmidt it->queueid, res); 3636ef9fe01SMatthias Schmidt return(1); 3646ef9fe01SMatthias Schmidt } 3656ef9fe01SMatthias Schmidt config->features &= ~NOSSL; 3666ef9fe01SMatthias Schmidt 367f67bedddSMatthias Schmidt if ((config->features & SECURETRANS) != 0) { 368f67bedddSMatthias Schmidt error = smtp_init_crypto(it, fd, config->features); 369f67bedddSMatthias Schmidt if (error >= 0) 3700ca0cd25SSascha Wildner syslog(LOG_INFO, "%s: SSL initialization successful", 371f67bedddSMatthias Schmidt it->queueid); 372f67bedddSMatthias Schmidt else 373f67bedddSMatthias Schmidt goto out; 374f67bedddSMatthias Schmidt } 37566674731SSimon Schubert 376f67bedddSMatthias Schmidt send_remote_command(fd, "EHLO %s", hostname()); 3777b68d8aeSMatthias Schmidt if (read_remote(fd, 0, NULL) != 2) { 37899d6192fSSimon Schubert syslog(LOG_WARNING, "%s: remote delivery deferred: " 379a5a8a1a4SSimon Schubert " EHLO failed: %s", it->queueid, neterr); 3805868e44cSSimon Schubert asprintf(errmsgc, "%s did not like our EHLO:\n%s", 38139f7c85aSSimon Schubert host, neterr); 382f67bedddSMatthias Schmidt return (-1); 383f67bedddSMatthias Schmidt } 384f67bedddSMatthias Schmidt 385f67bedddSMatthias Schmidt /* 386f67bedddSMatthias Schmidt * Use SMTP authentication if the user defined an entry for the remote 387f67bedddSMatthias Schmidt * or smarthost 388f67bedddSMatthias Schmidt */ 389f67bedddSMatthias Schmidt SLIST_FOREACH(a, &authusers, next) { 390f67bedddSMatthias Schmidt if (strcmp(a->host, host) == 0) { 391f67bedddSMatthias Schmidt do_auth = 1; 392f67bedddSMatthias Schmidt break; 393f67bedddSMatthias Schmidt } 394f67bedddSMatthias Schmidt } 395f67bedddSMatthias Schmidt 396f67bedddSMatthias Schmidt if (do_auth == 1) { 397b558d098SMatthias Schmidt /* 398b558d098SMatthias Schmidt * Check if the user wants plain text login without using 399b558d098SMatthias Schmidt * encryption. 400b558d098SMatthias Schmidt */ 401b558d098SMatthias Schmidt syslog(LOG_INFO, "%s: Use SMTP authentication", 402b558d098SMatthias Schmidt it->queueid); 403f67bedddSMatthias Schmidt error = smtp_login(it, fd, a->login, a->password); 404f67bedddSMatthias Schmidt if (error < 0) { 405f67bedddSMatthias Schmidt syslog(LOG_ERR, "%s: remote delivery failed:" 406f67bedddSMatthias Schmidt " SMTP login failed: %m", it->queueid); 4075868e44cSSimon Schubert asprintf(errmsgc, "SMTP login to %s failed", host); 408f67bedddSMatthias Schmidt return (-1); 409f67bedddSMatthias Schmidt } 410f67bedddSMatthias Schmidt /* SMTP login is not available, so try without */ 411f67bedddSMatthias Schmidt else if (error > 0) 41299d6192fSSimon Schubert syslog(LOG_WARNING, "%s: SMTP login not available." 413b558d098SMatthias Schmidt " Try without", it->queueid); 414b558d098SMatthias Schmidt } 415f67bedddSMatthias Schmidt 4167dfd2fd8SSimon Schubert #define READ_REMOTE_CHECK(c, exp) \ 4177dfd2fd8SSimon Schubert res = read_remote(fd, 0, NULL); \ 4187dfd2fd8SSimon Schubert if (res == 5) { \ 4197dfd2fd8SSimon Schubert syslog(LOG_ERR, "%s: remote delivery failed: " \ 4207dfd2fd8SSimon Schubert c " failed: %s", it->queueid, neterr); \ 4215868e44cSSimon Schubert asprintf(errmsgc, "%s did not like our " c ":\n%s", \ 42239f7c85aSSimon Schubert host, neterr); \ 4237dfd2fd8SSimon Schubert return (-1); \ 4247dfd2fd8SSimon Schubert } else if (res != exp) { \ 42599d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: " \ 4267dfd2fd8SSimon Schubert c " failed: %s", it->queueid, neterr); \ 4277dfd2fd8SSimon Schubert return (1); \ 428f67bedddSMatthias Schmidt } 429f67bedddSMatthias Schmidt 4307dfd2fd8SSimon Schubert send_remote_command(fd, "MAIL FROM:<%s>", it->sender); 4317dfd2fd8SSimon Schubert READ_REMOTE_CHECK("MAIL FROM", 2); 4327dfd2fd8SSimon Schubert 433f67bedddSMatthias Schmidt send_remote_command(fd, "RCPT TO:<%s>", it->addr); 4347dfd2fd8SSimon Schubert READ_REMOTE_CHECK("RCPT TO", 2); 435f67bedddSMatthias Schmidt 436f67bedddSMatthias Schmidt send_remote_command(fd, "DATA"); 4377dfd2fd8SSimon Schubert READ_REMOTE_CHECK("DATA", 3); 438f67bedddSMatthias Schmidt 439f67bedddSMatthias Schmidt if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) { 440a5a8a1a4SSimon Schubert syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s", 441a5a8a1a4SSimon Schubert it->queueid, neterr); 442f67bedddSMatthias Schmidt return (1); 443f67bedddSMatthias Schmidt } 444f67bedddSMatthias Schmidt 4450caaabf6SSimon Schubert error = 0; 446f67bedddSMatthias Schmidt while (!feof(it->queuef)) { 447f67bedddSMatthias Schmidt if (fgets(line, sizeof(line), it->queuef) == NULL) 448f67bedddSMatthias Schmidt break; 449f67bedddSMatthias Schmidt linelen = strlen(line); 450f67bedddSMatthias Schmidt if (linelen == 0 || line[linelen - 1] != '\n') { 451f67bedddSMatthias Schmidt syslog(LOG_CRIT, "%s: remote delivery failed:" 452f67bedddSMatthias Schmidt "corrupted queue file", it->queueid); 4535868e44cSSimon Schubert *errmsg = "corrupted queue file"; 454f67bedddSMatthias Schmidt error = -1; 455f67bedddSMatthias Schmidt goto out; 456f67bedddSMatthias Schmidt } 457f67bedddSMatthias Schmidt 458f67bedddSMatthias Schmidt /* Remove trailing \n's and escape leading dots */ 459f67bedddSMatthias Schmidt trim_line(line); 460f67bedddSMatthias Schmidt 461f67bedddSMatthias Schmidt /* 462f67bedddSMatthias Schmidt * If the first character is a dot, we escape it so the line 463f67bedddSMatthias Schmidt * length increases 464f67bedddSMatthias Schmidt */ 465f67bedddSMatthias Schmidt if (line[0] == '.') 466f67bedddSMatthias Schmidt linelen++; 467f67bedddSMatthias Schmidt 468f67bedddSMatthias Schmidt if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) { 46999d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: " 470f67bedddSMatthias Schmidt "write error", it->queueid); 471f67bedddSMatthias Schmidt error = 1; 472f67bedddSMatthias Schmidt goto out; 473f67bedddSMatthias Schmidt } 474f67bedddSMatthias Schmidt } 475f67bedddSMatthias Schmidt 476f67bedddSMatthias Schmidt send_remote_command(fd, "."); 4777dfd2fd8SSimon Schubert READ_REMOTE_CHECK("final DATA", 2); 478f67bedddSMatthias Schmidt 479f67bedddSMatthias Schmidt send_remote_command(fd, "QUIT"); 4806cfc247dSSimon Schubert if (read_remote(fd, 0, NULL) != 2) 48199d6192fSSimon Schubert syslog(LOG_INFO, "%s: remote delivery succeeded but " 482a5a8a1a4SSimon Schubert "QUIT failed: %s", it->queueid, neterr); 483f67bedddSMatthias Schmidt out: 484f67bedddSMatthias Schmidt 485*10eeb0dfSSimon Schubert close_connection(fd); 486f67bedddSMatthias Schmidt return (error); 487f67bedddSMatthias Schmidt } 488f67bedddSMatthias Schmidt 489