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> 47*e2c88018SSimon Schubert #include <openssl/err.h> 48f67bedddSMatthias Schmidt 49bc7baf1dSSascha Wildner #include <err.h> 50a5a8a1a4SSimon Schubert #include <errno.h> 51f67bedddSMatthias Schmidt #include <netdb.h> 52f67bedddSMatthias Schmidt #include <setjmp.h> 53f67bedddSMatthias Schmidt #include <signal.h> 54f67bedddSMatthias Schmidt #include <syslog.h> 55f67bedddSMatthias Schmidt #include <unistd.h> 56f67bedddSMatthias Schmidt 57f67bedddSMatthias Schmidt #include "dma.h" 58f67bedddSMatthias Schmidt 59f67bedddSMatthias Schmidt extern struct config *config; 60f67bedddSMatthias Schmidt extern struct authusers authusers; 61f67bedddSMatthias Schmidt static jmp_buf timeout_alarm; 62a5a8a1a4SSimon Schubert char neterr[BUF_SIZE]; 63f67bedddSMatthias Schmidt 64*e2c88018SSimon Schubert char * 65*e2c88018SSimon Schubert ssl_errstr(void) 66*e2c88018SSimon Schubert { 67*e2c88018SSimon Schubert long oerr, nerr; 68*e2c88018SSimon Schubert 69*e2c88018SSimon Schubert oerr = 0; 70*e2c88018SSimon Schubert while ((nerr = ERR_get_error()) != 0) 71*e2c88018SSimon Schubert oerr = nerr; 72*e2c88018SSimon Schubert 73*e2c88018SSimon Schubert return (ERR_error_string(oerr, NULL)); 74*e2c88018SSimon Schubert } 75*e2c88018SSimon Schubert 76f67bedddSMatthias Schmidt static void 77dba19026SMatthias Schmidt sig_alarm(int signo __unused) 78f67bedddSMatthias Schmidt { 79f67bedddSMatthias Schmidt longjmp(timeout_alarm, 1); 80f67bedddSMatthias Schmidt } 81f67bedddSMatthias Schmidt 82f67bedddSMatthias Schmidt ssize_t 83f67bedddSMatthias Schmidt send_remote_command(int fd, const char* fmt, ...) 84f67bedddSMatthias Schmidt { 85f67bedddSMatthias Schmidt va_list va; 86f67bedddSMatthias Schmidt char cmd[4096]; 872922fd2bSSimon Schubert size_t len, pos; 882922fd2bSSimon Schubert int s; 892922fd2bSSimon Schubert ssize_t n; 90f67bedddSMatthias Schmidt 91f67bedddSMatthias Schmidt va_start(va, fmt); 922922fd2bSSimon Schubert s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va); 932922fd2bSSimon Schubert va_end(va); 94*e2c88018SSimon Schubert if (s == sizeof(cmd) - 2 || s < 0) { 95*e2c88018SSimon Schubert strcpy(neterr, "Internal error: oversized command string"); 96*e2c88018SSimon Schubert return (-1); 97*e2c88018SSimon Schubert } 98*e2c88018SSimon Schubert 992922fd2bSSimon Schubert /* We *know* there are at least two more bytes available */ 1002922fd2bSSimon Schubert strcat(cmd, "\r\n"); 1012922fd2bSSimon Schubert len = strlen(cmd); 102f67bedddSMatthias Schmidt 103f67bedddSMatthias Schmidt if (((config->features & SECURETRANS) != 0) && 1046ef9fe01SMatthias Schmidt ((config->features & NOSSL) == 0)) { 1052922fd2bSSimon Schubert while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) { 1062922fd2bSSimon Schubert s = SSL_get_error(config->ssl, s); 1072922fd2bSSimon Schubert if (s != SSL_ERROR_WANT_READ && 108*e2c88018SSimon Schubert s != SSL_ERROR_WANT_WRITE) { 109*e2c88018SSimon Schubert strncpy(neterr, ssl_errstr(), sizeof(neterr)); 1102922fd2bSSimon Schubert return (-1); 1112922fd2bSSimon Schubert } 112f67bedddSMatthias Schmidt } 113*e2c88018SSimon Schubert } 114f67bedddSMatthias Schmidt else { 1152922fd2bSSimon Schubert pos = 0; 1162922fd2bSSimon Schubert while (pos < len) { 1172922fd2bSSimon Schubert n = write(fd, cmd + pos, len - pos); 1182922fd2bSSimon Schubert if (n < 0) 1192922fd2bSSimon Schubert return (-1); 1202922fd2bSSimon Schubert pos += n; 121f67bedddSMatthias Schmidt } 1222922fd2bSSimon Schubert } 123f67bedddSMatthias Schmidt 1242922fd2bSSimon Schubert return (len); 125f67bedddSMatthias Schmidt } 126f67bedddSMatthias Schmidt 1276ef9fe01SMatthias Schmidt int 1287b68d8aeSMatthias Schmidt read_remote(int fd, int extbufsize, char *extbuf) 129f67bedddSMatthias Schmidt { 1306ef9fe01SMatthias Schmidt ssize_t rlen = 0; 1316ef9fe01SMatthias Schmidt size_t pos, len; 1326ef9fe01SMatthias Schmidt char buff[BUF_SIZE]; 1337b68d8aeSMatthias Schmidt int done = 0, status = 0, extbufpos = 0; 134f67bedddSMatthias Schmidt 135f67bedddSMatthias Schmidt if (signal(SIGALRM, sig_alarm) == SIG_ERR) { 136a5a8a1a4SSimon Schubert snprintf(neterr, sizeof(neterr), "SIGALRM error: %s", 137a5a8a1a4SSimon Schubert strerror(errno)); 138*e2c88018SSimon Schubert return (-1); 139f67bedddSMatthias Schmidt } 140f67bedddSMatthias Schmidt if (setjmp(timeout_alarm) != 0) { 141a5a8a1a4SSimon Schubert snprintf(neterr, sizeof(neterr), "Timeout reached"); 142*e2c88018SSimon Schubert return (-1); 143f67bedddSMatthias Schmidt } 144f67bedddSMatthias Schmidt alarm(CON_TIMEOUT); 145f67bedddSMatthias Schmidt 146f67bedddSMatthias Schmidt /* 1476ef9fe01SMatthias Schmidt * Remote reading code from femail.c written by Henning Brauer of 1486ef9fe01SMatthias Schmidt * OpenBSD and released under a BSD style license. 149f67bedddSMatthias Schmidt */ 1506ef9fe01SMatthias Schmidt for (len = pos = 0; !done; ) { 1517b68d8aeSMatthias Schmidt rlen = 0; 1526ef9fe01SMatthias Schmidt if (pos == 0 || 1536ef9fe01SMatthias Schmidt (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) { 1546ef9fe01SMatthias Schmidt memmove(buff, buff + pos, len - pos); 1556ef9fe01SMatthias Schmidt len -= pos; 1566ef9fe01SMatthias Schmidt pos = 0; 157f67bedddSMatthias Schmidt if (((config->features & SECURETRANS) != 0) && 1586ef9fe01SMatthias Schmidt (config->features & NOSSL) == 0) { 1596ef9fe01SMatthias Schmidt if ((rlen = SSL_read(config->ssl, buff + len, 160*e2c88018SSimon Schubert sizeof(buff) - len)) == -1) { 161*e2c88018SSimon Schubert strncpy(neterr, ssl_errstr(), sizeof(neterr)); 162*e2c88018SSimon Schubert return (-1); 163*e2c88018SSimon Schubert } 1646ef9fe01SMatthias Schmidt } else { 165*e2c88018SSimon Schubert if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) { 166*e2c88018SSimon Schubert strncpy(neterr, strerror(errno), sizeof(neterr)); 167*e2c88018SSimon Schubert return (-1); 168*e2c88018SSimon Schubert } 1696ef9fe01SMatthias Schmidt } 1706ef9fe01SMatthias Schmidt len += rlen; 1716ef9fe01SMatthias Schmidt } 1727b68d8aeSMatthias Schmidt /* 1737b68d8aeSMatthias Schmidt * If there is an external buffer with a size bigger than zero 1747b68d8aeSMatthias Schmidt * and as long as there is space in the external buffer and 1757b68d8aeSMatthias Schmidt * there are new characters read from the mailserver 1767b68d8aeSMatthias Schmidt * copy them to the external buffer 1777b68d8aeSMatthias Schmidt */ 1787b68d8aeSMatthias Schmidt if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0 1797b68d8aeSMatthias Schmidt && extbuf != NULL) { 1807b68d8aeSMatthias Schmidt /* do not write over the bounds of the buffer */ 1817b68d8aeSMatthias Schmidt if(extbufpos + rlen > (extbufsize - 1)) { 1827b68d8aeSMatthias Schmidt rlen = extbufsize - extbufpos; 1837b68d8aeSMatthias Schmidt } 1847b68d8aeSMatthias Schmidt memcpy(extbuf + extbufpos, buff + len - rlen, rlen); 1857b68d8aeSMatthias Schmidt extbufpos += rlen; 1867b68d8aeSMatthias Schmidt } 1876ef9fe01SMatthias Schmidt for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++) 1886ef9fe01SMatthias Schmidt ; /* Do nothing */ 189f67bedddSMatthias Schmidt 1906ef9fe01SMatthias Schmidt if (pos == len) 1916ef9fe01SMatthias Schmidt return (0); 1926ef9fe01SMatthias Schmidt 193*e2c88018SSimon Schubert if (buff[pos] == ' ') { 1946ef9fe01SMatthias Schmidt done = 1; 195*e2c88018SSimon Schubert } else if (buff[pos] != '-') { 196*e2c88018SSimon Schubert strcpy(neterr, "invalid syntax in reply from server"); 197*e2c88018SSimon Schubert return (-1); 198*e2c88018SSimon Schubert } 1996ef9fe01SMatthias Schmidt 2006ef9fe01SMatthias Schmidt /* skip up to \n */ 2016ef9fe01SMatthias Schmidt for (; pos < len && buff[pos - 1] != '\n'; pos++) 2026ef9fe01SMatthias Schmidt ; /* Do nothing */ 2036ef9fe01SMatthias Schmidt 2046ef9fe01SMatthias Schmidt } 205f67bedddSMatthias Schmidt alarm(0); 206f67bedddSMatthias Schmidt 207a5a8a1a4SSimon Schubert buff[len] = '\0'; 208a5a8a1a4SSimon Schubert while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n')) 209a5a8a1a4SSimon Schubert buff[--len] = '\0'; 210a5a8a1a4SSimon Schubert snprintf(neterr, sizeof(neterr), "%s", buff); 2116ef9fe01SMatthias Schmidt status = atoi(buff); 2126ef9fe01SMatthias Schmidt return (status/100); 213f67bedddSMatthias Schmidt } 214f67bedddSMatthias Schmidt 215f67bedddSMatthias Schmidt /* 216f67bedddSMatthias Schmidt * Handle SMTP authentication 217f67bedddSMatthias Schmidt */ 218f67bedddSMatthias Schmidt static int 219f67bedddSMatthias Schmidt smtp_login(struct qitem *it, int fd, char *login, char* password) 220f67bedddSMatthias Schmidt { 221f67bedddSMatthias Schmidt char *temp; 2226ef9fe01SMatthias Schmidt int len, res = 0; 223f67bedddSMatthias Schmidt 2247b68d8aeSMatthias Schmidt res = smtp_auth_md5(it, fd, login, password); 2257b68d8aeSMatthias Schmidt if (res == 0) { 2267b68d8aeSMatthias Schmidt return (0); 2277b68d8aeSMatthias Schmidt } else if (res == -2) { 2287b68d8aeSMatthias Schmidt /* 2297b68d8aeSMatthias Schmidt * If the return code is -2, then then the login attempt failed, 2307b68d8aeSMatthias Schmidt * do not try other login mechanisms 2317b68d8aeSMatthias Schmidt */ 2325d7fe8bbSSimon Schubert return (1); 2337b68d8aeSMatthias Schmidt } 2347b68d8aeSMatthias Schmidt 235bf83173bSSimon Schubert if ((config->features & INSECURE) != 0 || 236bf83173bSSimon Schubert (config->features & SECURETRANS) != 0) { 237f67bedddSMatthias Schmidt /* Send AUTH command according to RFC 2554 */ 238f67bedddSMatthias Schmidt send_remote_command(fd, "AUTH LOGIN"); 2397b68d8aeSMatthias Schmidt if (read_remote(fd, 0, NULL) != 3) { 24099d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred:" 241a5a8a1a4SSimon Schubert " AUTH login not available: %s", 242a5a8a1a4SSimon Schubert it->queueid, neterr); 243f67bedddSMatthias Schmidt return (1); 244f67bedddSMatthias Schmidt } 245f67bedddSMatthias Schmidt 246f67bedddSMatthias Schmidt len = base64_encode(login, strlen(login), &temp); 2475d7fe8bbSSimon Schubert if (len < 0) { 2485d7fe8bbSSimon Schubert encerr: 2495d7fe8bbSSimon Schubert syslog(LOG_ERR, "%s: can not encode auth reply: %m", 2505d7fe8bbSSimon Schubert it->queueid); 2515d7fe8bbSSimon Schubert return (1); 2525d7fe8bbSSimon Schubert } 253f67bedddSMatthias Schmidt 254f67bedddSMatthias Schmidt send_remote_command(fd, "%s", temp); 2555d7fe8bbSSimon Schubert free(temp); 256*e2c88018SSimon Schubert res = read_remote(fd, 0, NULL); 257*e2c88018SSimon Schubert if (res != 3) { 258*e2c88018SSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery %s: AUTH login failed: %s", 259*e2c88018SSimon Schubert it->queueid, res == 5 ? "failed" : "deferred", neterr); 260*e2c88018SSimon Schubert return (res == 5 ? -1 : 1); 261f67bedddSMatthias Schmidt } 262f67bedddSMatthias Schmidt 263f67bedddSMatthias Schmidt len = base64_encode(password, strlen(password), &temp); 2645d7fe8bbSSimon Schubert if (len < 0) 2655d7fe8bbSSimon Schubert goto encerr; 266f67bedddSMatthias Schmidt 267f67bedddSMatthias Schmidt send_remote_command(fd, "%s", temp); 2685d7fe8bbSSimon Schubert free(temp); 2697b68d8aeSMatthias Schmidt res = read_remote(fd, 0, NULL); 270*e2c88018SSimon Schubert if (res != 2) { 271*e2c88018SSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery %s: Authentication failed: %s", 272*e2c88018SSimon Schubert it->queueid, res == 5 ? "failed" : "deferred", neterr); 273*e2c88018SSimon Schubert return (res == 5 ? -1 : 1); 274f67bedddSMatthias Schmidt } 2757b68d8aeSMatthias Schmidt } else { 27699d6192fSSimon Schubert syslog(LOG_WARNING, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ", 2777b68d8aeSMatthias Schmidt it->queueid); 2787b68d8aeSMatthias Schmidt return (1); 2797b68d8aeSMatthias Schmidt } 280f67bedddSMatthias Schmidt 281f67bedddSMatthias Schmidt return (0); 282f67bedddSMatthias Schmidt } 283f67bedddSMatthias Schmidt 284f67bedddSMatthias Schmidt static int 285f67bedddSMatthias Schmidt open_connection(struct qitem *it, const char *host) 286f67bedddSMatthias Schmidt { 287f67bedddSMatthias Schmidt struct addrinfo hints, *res, *res0; 288f67bedddSMatthias Schmidt char servname[128]; 289f67bedddSMatthias Schmidt const char *errmsg = NULL; 290f67bedddSMatthias Schmidt int fd, error = 0, port; 291f67bedddSMatthias Schmidt 292dba19026SMatthias Schmidt if (config->port != 0) 293f67bedddSMatthias Schmidt port = config->port; 294f67bedddSMatthias Schmidt else 295f67bedddSMatthias Schmidt port = SMTP_PORT; 296f67bedddSMatthias Schmidt 2977b68d8aeSMatthias Schmidt /* FIXME get MX record of host */ 298f67bedddSMatthias Schmidt /* Shamelessly taken from getaddrinfo(3) */ 299f67bedddSMatthias Schmidt memset(&hints, 0, sizeof(hints)); 300f67bedddSMatthias Schmidt hints.ai_family = PF_UNSPEC; 301f67bedddSMatthias Schmidt hints.ai_socktype = SOCK_STREAM; 302f67bedddSMatthias Schmidt hints.ai_protocol = IPPROTO_TCP; 303f67bedddSMatthias Schmidt 304f67bedddSMatthias Schmidt snprintf(servname, sizeof(servname), "%d", port); 305f67bedddSMatthias Schmidt error = getaddrinfo(host, servname, &hints, &res0); 306f67bedddSMatthias Schmidt if (error) { 30799d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: " 308f67bedddSMatthias Schmidt "%s: %m", it->queueid, gai_strerror(error)); 309f67bedddSMatthias Schmidt return (-1); 310f67bedddSMatthias Schmidt } 311f67bedddSMatthias Schmidt fd = -1; 312f67bedddSMatthias Schmidt for (res = res0; res; res = res->ai_next) { 313f67bedddSMatthias Schmidt fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol); 314f67bedddSMatthias Schmidt if (fd < 0) { 315f67bedddSMatthias Schmidt errmsg = "socket failed"; 316f67bedddSMatthias Schmidt continue; 317f67bedddSMatthias Schmidt } 318f67bedddSMatthias Schmidt if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) { 319f67bedddSMatthias Schmidt errmsg = "connect failed"; 320f67bedddSMatthias Schmidt close(fd); 321f67bedddSMatthias Schmidt fd = -1; 322f67bedddSMatthias Schmidt continue; 323f67bedddSMatthias Schmidt } 324f67bedddSMatthias Schmidt break; 325f67bedddSMatthias Schmidt } 326f67bedddSMatthias Schmidt if (fd < 0) { 32799d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: %s (%s:%s)", 328f67bedddSMatthias Schmidt it->queueid, errmsg, host, servname); 329f67bedddSMatthias Schmidt freeaddrinfo(res0); 330f67bedddSMatthias Schmidt return (-1); 331f67bedddSMatthias Schmidt } 332f67bedddSMatthias Schmidt freeaddrinfo(res0); 333f67bedddSMatthias Schmidt return (fd); 334f67bedddSMatthias Schmidt } 335f67bedddSMatthias Schmidt 33610eeb0dfSSimon Schubert static void 33710eeb0dfSSimon Schubert close_connection(int fd) 33810eeb0dfSSimon Schubert { 33910eeb0dfSSimon Schubert if (((config->features & SECURETRANS) != 0) && 34010eeb0dfSSimon Schubert ((config->features & NOSSL) == 0)) 34110eeb0dfSSimon Schubert SSL_shutdown(config->ssl); 34210eeb0dfSSimon Schubert 34310eeb0dfSSimon Schubert if (config->ssl != NULL) 34410eeb0dfSSimon Schubert SSL_free(config->ssl); 34510eeb0dfSSimon Schubert 34610eeb0dfSSimon Schubert close(fd); 34710eeb0dfSSimon Schubert } 34810eeb0dfSSimon Schubert 349f67bedddSMatthias Schmidt int 3505868e44cSSimon Schubert deliver_remote(struct qitem *it, const char **errmsg) 351f67bedddSMatthias Schmidt { 352f67bedddSMatthias Schmidt struct authuser *a; 3536ef9fe01SMatthias Schmidt char *host, line[1000]; 3546ef9fe01SMatthias Schmidt int fd, error = 0, do_auth = 0, res = 0; 355f67bedddSMatthias Schmidt size_t linelen; 3565868e44cSSimon Schubert /* asprintf can't take const */ 3575868e44cSSimon Schubert void *errmsgc = __DECONST(char **, errmsg); 358f67bedddSMatthias Schmidt 359f67bedddSMatthias Schmidt host = strrchr(it->addr, '@'); 360f67bedddSMatthias Schmidt /* Should not happen */ 36139f7c85aSSimon Schubert if (host == NULL) { 3625868e44cSSimon Schubert asprintf(errmsgc, "Internal error: badly formed address %s", 36339f7c85aSSimon Schubert it->addr); 364f67bedddSMatthias Schmidt return(-1); 36539f7c85aSSimon Schubert } else { 366f67bedddSMatthias Schmidt /* Step over the @ */ 367f67bedddSMatthias Schmidt host++; 36839f7c85aSSimon Schubert } 369f67bedddSMatthias Schmidt 370f67bedddSMatthias Schmidt /* Smarthost support? */ 371f67bedddSMatthias Schmidt if (config->smarthost != NULL && strlen(config->smarthost) > 0) { 3724a23bd3dSMatthias Schmidt syslog(LOG_INFO, "%s: using smarthost (%s:%i)", 3734a23bd3dSMatthias Schmidt it->queueid, config->smarthost, config->port); 374f67bedddSMatthias Schmidt host = config->smarthost; 375f67bedddSMatthias Schmidt } 376f67bedddSMatthias Schmidt 377f67bedddSMatthias Schmidt fd = open_connection(it, host); 378f67bedddSMatthias Schmidt if (fd < 0) 379f67bedddSMatthias Schmidt return (1); 380f67bedddSMatthias Schmidt 3816ef9fe01SMatthias Schmidt /* Check first reply from remote host */ 3826ef9fe01SMatthias Schmidt config->features |= NOSSL; 3837b68d8aeSMatthias Schmidt res = read_remote(fd, 0, NULL); 3846ef9fe01SMatthias Schmidt if (res != 2) { 38599d6192fSSimon Schubert syslog(LOG_WARNING, "%s: Invalid initial response: %i", 3866ef9fe01SMatthias Schmidt it->queueid, res); 3876ef9fe01SMatthias Schmidt return(1); 3886ef9fe01SMatthias Schmidt } 3896ef9fe01SMatthias Schmidt config->features &= ~NOSSL; 3906ef9fe01SMatthias Schmidt 391f67bedddSMatthias Schmidt if ((config->features & SECURETRANS) != 0) { 392f67bedddSMatthias Schmidt error = smtp_init_crypto(it, fd, config->features); 393f67bedddSMatthias Schmidt if (error >= 0) 394*e2c88018SSimon Schubert syslog(LOG_DEBUG, "%s: SSL initialization successful", 395f67bedddSMatthias Schmidt it->queueid); 396f67bedddSMatthias Schmidt else 397f67bedddSMatthias Schmidt goto out; 398f67bedddSMatthias Schmidt } 39966674731SSimon Schubert 400f67bedddSMatthias Schmidt send_remote_command(fd, "EHLO %s", hostname()); 4017b68d8aeSMatthias Schmidt if (read_remote(fd, 0, NULL) != 2) { 40299d6192fSSimon Schubert syslog(LOG_WARNING, "%s: remote delivery deferred: " 403a5a8a1a4SSimon Schubert " EHLO failed: %s", it->queueid, neterr); 4045868e44cSSimon Schubert asprintf(errmsgc, "%s did not like our EHLO:\n%s", 40539f7c85aSSimon Schubert host, neterr); 406f67bedddSMatthias Schmidt return (-1); 407f67bedddSMatthias Schmidt } 408f67bedddSMatthias Schmidt 409f67bedddSMatthias Schmidt /* 410f67bedddSMatthias Schmidt * Use SMTP authentication if the user defined an entry for the remote 411f67bedddSMatthias Schmidt * or smarthost 412f67bedddSMatthias Schmidt */ 413f67bedddSMatthias Schmidt SLIST_FOREACH(a, &authusers, next) { 414f67bedddSMatthias Schmidt if (strcmp(a->host, host) == 0) { 415f67bedddSMatthias Schmidt do_auth = 1; 416f67bedddSMatthias Schmidt break; 417f67bedddSMatthias Schmidt } 418f67bedddSMatthias Schmidt } 419f67bedddSMatthias Schmidt 420f67bedddSMatthias Schmidt if (do_auth == 1) { 421b558d098SMatthias Schmidt /* 422b558d098SMatthias Schmidt * Check if the user wants plain text login without using 423b558d098SMatthias Schmidt * encryption. 424b558d098SMatthias Schmidt */ 425b558d098SMatthias Schmidt syslog(LOG_INFO, "%s: Use SMTP authentication", 426b558d098SMatthias Schmidt it->queueid); 427f67bedddSMatthias Schmidt error = smtp_login(it, fd, a->login, a->password); 428f67bedddSMatthias Schmidt if (error < 0) { 429f67bedddSMatthias Schmidt syslog(LOG_ERR, "%s: remote delivery failed:" 430f67bedddSMatthias Schmidt " SMTP login failed: %m", it->queueid); 4315868e44cSSimon Schubert asprintf(errmsgc, "SMTP login to %s failed", host); 432f67bedddSMatthias Schmidt return (-1); 433f67bedddSMatthias Schmidt } 434f67bedddSMatthias Schmidt /* SMTP login is not available, so try without */ 435f67bedddSMatthias Schmidt else if (error > 0) 43699d6192fSSimon Schubert syslog(LOG_WARNING, "%s: SMTP login not available." 437b558d098SMatthias Schmidt " Try without", it->queueid); 438b558d098SMatthias Schmidt } 439f67bedddSMatthias Schmidt 4407dfd2fd8SSimon Schubert #define READ_REMOTE_CHECK(c, exp) \ 4417dfd2fd8SSimon Schubert res = read_remote(fd, 0, NULL); \ 4427dfd2fd8SSimon Schubert if (res == 5) { \ 4437dfd2fd8SSimon Schubert syslog(LOG_ERR, "%s: remote delivery failed: " \ 4447dfd2fd8SSimon Schubert c " failed: %s", it->queueid, neterr); \ 4455868e44cSSimon Schubert asprintf(errmsgc, "%s did not like our " c ":\n%s", \ 44639f7c85aSSimon Schubert host, neterr); \ 4477dfd2fd8SSimon Schubert return (-1); \ 4487dfd2fd8SSimon Schubert } else if (res != exp) { \ 44999d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: " \ 4507dfd2fd8SSimon Schubert c " failed: %s", it->queueid, neterr); \ 4517dfd2fd8SSimon Schubert return (1); \ 452f67bedddSMatthias Schmidt } 453f67bedddSMatthias Schmidt 4547dfd2fd8SSimon Schubert send_remote_command(fd, "MAIL FROM:<%s>", it->sender); 4557dfd2fd8SSimon Schubert READ_REMOTE_CHECK("MAIL FROM", 2); 4567dfd2fd8SSimon Schubert 457f67bedddSMatthias Schmidt send_remote_command(fd, "RCPT TO:<%s>", it->addr); 4587dfd2fd8SSimon Schubert READ_REMOTE_CHECK("RCPT TO", 2); 459f67bedddSMatthias Schmidt 460f67bedddSMatthias Schmidt send_remote_command(fd, "DATA"); 4617dfd2fd8SSimon Schubert READ_REMOTE_CHECK("DATA", 3); 462f67bedddSMatthias Schmidt 463f67bedddSMatthias Schmidt if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) { 464a5a8a1a4SSimon Schubert syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s", 465a5a8a1a4SSimon Schubert it->queueid, neterr); 466f67bedddSMatthias Schmidt return (1); 467f67bedddSMatthias Schmidt } 468f67bedddSMatthias Schmidt 4690caaabf6SSimon Schubert error = 0; 470f67bedddSMatthias Schmidt while (!feof(it->queuef)) { 471f67bedddSMatthias Schmidt if (fgets(line, sizeof(line), it->queuef) == NULL) 472f67bedddSMatthias Schmidt break; 473f67bedddSMatthias Schmidt linelen = strlen(line); 474f67bedddSMatthias Schmidt if (linelen == 0 || line[linelen - 1] != '\n') { 475f67bedddSMatthias Schmidt syslog(LOG_CRIT, "%s: remote delivery failed:" 476f67bedddSMatthias Schmidt "corrupted queue file", it->queueid); 4775868e44cSSimon Schubert *errmsg = "corrupted queue file"; 478f67bedddSMatthias Schmidt error = -1; 479f67bedddSMatthias Schmidt goto out; 480f67bedddSMatthias Schmidt } 481f67bedddSMatthias Schmidt 482f67bedddSMatthias Schmidt /* Remove trailing \n's and escape leading dots */ 483f67bedddSMatthias Schmidt trim_line(line); 484f67bedddSMatthias Schmidt 485f67bedddSMatthias Schmidt /* 486f67bedddSMatthias Schmidt * If the first character is a dot, we escape it so the line 487f67bedddSMatthias Schmidt * length increases 488f67bedddSMatthias Schmidt */ 489f67bedddSMatthias Schmidt if (line[0] == '.') 490f67bedddSMatthias Schmidt linelen++; 491f67bedddSMatthias Schmidt 492f67bedddSMatthias Schmidt if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) { 49399d6192fSSimon Schubert syslog(LOG_NOTICE, "%s: remote delivery deferred: " 494f67bedddSMatthias Schmidt "write error", it->queueid); 495f67bedddSMatthias Schmidt error = 1; 496f67bedddSMatthias Schmidt goto out; 497f67bedddSMatthias Schmidt } 498f67bedddSMatthias Schmidt } 499f67bedddSMatthias Schmidt 500f67bedddSMatthias Schmidt send_remote_command(fd, "."); 5017dfd2fd8SSimon Schubert READ_REMOTE_CHECK("final DATA", 2); 502f67bedddSMatthias Schmidt 503f67bedddSMatthias Schmidt send_remote_command(fd, "QUIT"); 5046cfc247dSSimon Schubert if (read_remote(fd, 0, NULL) != 2) 50599d6192fSSimon Schubert syslog(LOG_INFO, "%s: remote delivery succeeded but " 506a5a8a1a4SSimon Schubert "QUIT failed: %s", it->queueid, neterr); 507f67bedddSMatthias Schmidt out: 508f67bedddSMatthias Schmidt 50910eeb0dfSSimon Schubert close_connection(fd); 510f67bedddSMatthias Schmidt return (error); 511f67bedddSMatthias Schmidt } 512f67bedddSMatthias Schmidt 513