xref: /dflybsd-src/libexec/dma/net.c (revision 666747319bfbc2e270ddb694805eac1f5f3075a6)
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 #ifdef HAVE_CRYPTO
47f67bedddSMatthias Schmidt #include <openssl/ssl.h>
48f67bedddSMatthias Schmidt #endif /* HAVE_CRYPTO */
49f67bedddSMatthias Schmidt 
50bc7baf1dSSascha Wildner #include <err.h>
51a5a8a1a4SSimon Schubert #include <errno.h>
52f67bedddSMatthias Schmidt #include <netdb.h>
53f67bedddSMatthias Schmidt #include <setjmp.h>
54f67bedddSMatthias Schmidt #include <signal.h>
55f67bedddSMatthias Schmidt #include <syslog.h>
56f67bedddSMatthias Schmidt #include <unistd.h>
57f67bedddSMatthias Schmidt 
58f67bedddSMatthias Schmidt #include "dma.h"
59f67bedddSMatthias Schmidt 
60f67bedddSMatthias Schmidt extern struct config *config;
61f67bedddSMatthias Schmidt extern struct authusers authusers;
62f67bedddSMatthias Schmidt static jmp_buf timeout_alarm;
63a5a8a1a4SSimon Schubert char neterr[BUF_SIZE];
64f67bedddSMatthias Schmidt 
65f67bedddSMatthias Schmidt static void
66dba19026SMatthias Schmidt sig_alarm(int signo __unused)
67f67bedddSMatthias Schmidt {
68f67bedddSMatthias Schmidt 	longjmp(timeout_alarm, 1);
69f67bedddSMatthias Schmidt }
70f67bedddSMatthias Schmidt 
71f67bedddSMatthias Schmidt ssize_t
72f67bedddSMatthias Schmidt send_remote_command(int fd, const char* fmt, ...)
73f67bedddSMatthias Schmidt {
74f67bedddSMatthias Schmidt 	va_list va;
75f67bedddSMatthias Schmidt 	char cmd[4096];
762922fd2bSSimon Schubert 	size_t len, pos;
772922fd2bSSimon Schubert 	int s;
782922fd2bSSimon Schubert 	ssize_t n;
79f67bedddSMatthias Schmidt 
80f67bedddSMatthias Schmidt 	va_start(va, fmt);
812922fd2bSSimon Schubert 	s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
822922fd2bSSimon Schubert 	va_end(va);
832922fd2bSSimon Schubert 	if (s == sizeof(cmd) - 2 || s < 0)
842922fd2bSSimon Schubert 		errx(1, "Internal error: oversized command string");
852922fd2bSSimon Schubert 	/* We *know* there are at least two more bytes available */
862922fd2bSSimon Schubert 	strcat(cmd, "\r\n");
872922fd2bSSimon Schubert 	len = strlen(cmd);
88f67bedddSMatthias Schmidt 
89f67bedddSMatthias Schmidt 	if (((config->features & SECURETRANS) != 0) &&
906ef9fe01SMatthias Schmidt 	    ((config->features & NOSSL) == 0)) {
912922fd2bSSimon Schubert 		while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) {
922922fd2bSSimon Schubert 			s = SSL_get_error(config->ssl, s);
932922fd2bSSimon Schubert 			if (s != SSL_ERROR_WANT_READ &&
942922fd2bSSimon Schubert 			    s != SSL_ERROR_WANT_WRITE)
952922fd2bSSimon Schubert 				return (-1);
962922fd2bSSimon Schubert 		}
97f67bedddSMatthias Schmidt 	}
98f67bedddSMatthias Schmidt 	else {
992922fd2bSSimon Schubert 		pos = 0;
1002922fd2bSSimon Schubert 		while (pos < len) {
1012922fd2bSSimon Schubert 			n = write(fd, cmd + pos, len - pos);
1022922fd2bSSimon Schubert 			if (n < 0)
1032922fd2bSSimon Schubert 				return (-1);
1042922fd2bSSimon Schubert 			pos += n;
105f67bedddSMatthias Schmidt 		}
1062922fd2bSSimon Schubert 	}
107f67bedddSMatthias Schmidt 
1082922fd2bSSimon Schubert 	return (len);
109f67bedddSMatthias Schmidt }
110f67bedddSMatthias Schmidt 
1116ef9fe01SMatthias Schmidt int
1127b68d8aeSMatthias Schmidt read_remote(int fd, int extbufsize, char *extbuf)
113f67bedddSMatthias Schmidt {
1146ef9fe01SMatthias Schmidt 	ssize_t rlen = 0;
1156ef9fe01SMatthias Schmidt 	size_t pos, len;
1166ef9fe01SMatthias Schmidt 	char buff[BUF_SIZE];
1177b68d8aeSMatthias Schmidt 	int done = 0, status = 0, extbufpos = 0;
118f67bedddSMatthias Schmidt 
119f67bedddSMatthias Schmidt 	if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
120a5a8a1a4SSimon Schubert 		snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
121a5a8a1a4SSimon Schubert 		    strerror(errno));
122a5a8a1a4SSimon Schubert 		return (1);
123f67bedddSMatthias Schmidt 	}
124f67bedddSMatthias Schmidt 	if (setjmp(timeout_alarm) != 0) {
125a5a8a1a4SSimon Schubert 		snprintf(neterr, sizeof(neterr), "Timeout reached");
126f67bedddSMatthias Schmidt 		return (1);
127f67bedddSMatthias Schmidt 	}
128f67bedddSMatthias Schmidt 	alarm(CON_TIMEOUT);
129f67bedddSMatthias Schmidt 
130f67bedddSMatthias Schmidt 	/*
1316ef9fe01SMatthias Schmidt 	 * Remote reading code from femail.c written by Henning Brauer of
1326ef9fe01SMatthias Schmidt 	 * OpenBSD and released under a BSD style license.
133f67bedddSMatthias Schmidt 	 */
1346ef9fe01SMatthias Schmidt 	for (len = pos = 0; !done; ) {
1357b68d8aeSMatthias Schmidt 		rlen = 0;
1366ef9fe01SMatthias Schmidt 		if (pos == 0 ||
1376ef9fe01SMatthias Schmidt 		    (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
1386ef9fe01SMatthias Schmidt 			memmove(buff, buff + pos, len - pos);
1396ef9fe01SMatthias Schmidt 			len -= pos;
1406ef9fe01SMatthias Schmidt 			pos = 0;
141f67bedddSMatthias Schmidt 			if (((config->features & SECURETRANS) != 0) &&
1426ef9fe01SMatthias Schmidt 			    (config->features & NOSSL) == 0) {
1436ef9fe01SMatthias Schmidt 				if ((rlen = SSL_read(config->ssl, buff + len,
1446ef9fe01SMatthias Schmidt 				    sizeof(buff) - len)) == -1)
1456ef9fe01SMatthias Schmidt 					err(1, "read");
1466ef9fe01SMatthias Schmidt 			} else {
1476ef9fe01SMatthias Schmidt 				if ((rlen = read(fd, buff + len,
1486ef9fe01SMatthias Schmidt 				    sizeof(buff) - len)) == -1)
1496ef9fe01SMatthias Schmidt 					err(1, "read");
1506ef9fe01SMatthias Schmidt 			}
1516ef9fe01SMatthias Schmidt 			len += rlen;
1526ef9fe01SMatthias Schmidt 		}
1537b68d8aeSMatthias Schmidt 		/*
1547b68d8aeSMatthias Schmidt 		 * If there is an external buffer with a size bigger than zero
1557b68d8aeSMatthias Schmidt 		 * and as long as there is space in the external buffer and
1567b68d8aeSMatthias Schmidt 		 * there are new characters read from the mailserver
1577b68d8aeSMatthias Schmidt 		 * copy them to the external buffer
1587b68d8aeSMatthias Schmidt 		 */
1597b68d8aeSMatthias Schmidt 		if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
1607b68d8aeSMatthias Schmidt 		    && extbuf != NULL) {
1617b68d8aeSMatthias Schmidt 			/* do not write over the bounds of the buffer */
1627b68d8aeSMatthias Schmidt 			if(extbufpos + rlen > (extbufsize - 1)) {
1637b68d8aeSMatthias Schmidt 				rlen = extbufsize - extbufpos;
1647b68d8aeSMatthias Schmidt 			}
1657b68d8aeSMatthias Schmidt 			memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
1667b68d8aeSMatthias Schmidt 			extbufpos += rlen;
1677b68d8aeSMatthias Schmidt 		}
1686ef9fe01SMatthias Schmidt 		for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
1696ef9fe01SMatthias Schmidt 			; /* Do nothing */
170f67bedddSMatthias Schmidt 
1716ef9fe01SMatthias Schmidt 		if (pos == len)
1726ef9fe01SMatthias Schmidt 			return (0);
1736ef9fe01SMatthias Schmidt 
1746ef9fe01SMatthias Schmidt 		if (buff[pos] == ' ')
1756ef9fe01SMatthias Schmidt 			done = 1;
1766ef9fe01SMatthias Schmidt 		else if (buff[pos] != '-')
1776ef9fe01SMatthias Schmidt 			syslog(LOG_ERR, "invalid syntax in reply from server");
1786ef9fe01SMatthias Schmidt 
1796ef9fe01SMatthias Schmidt 		/* skip up to \n */
1806ef9fe01SMatthias Schmidt 		for (; pos < len && buff[pos - 1] != '\n'; pos++)
1816ef9fe01SMatthias Schmidt 			; /* Do nothing */
1826ef9fe01SMatthias Schmidt 
1836ef9fe01SMatthias Schmidt 	}
184f67bedddSMatthias Schmidt 	alarm(0);
185f67bedddSMatthias Schmidt 
186a5a8a1a4SSimon Schubert 	buff[len] = '\0';
187a5a8a1a4SSimon Schubert 	while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
188a5a8a1a4SSimon Schubert 		buff[--len] = '\0';
189a5a8a1a4SSimon Schubert 	snprintf(neterr, sizeof(neterr), "%s", buff);
1906ef9fe01SMatthias Schmidt 	status = atoi(buff);
1916ef9fe01SMatthias Schmidt 	return (status/100);
192f67bedddSMatthias Schmidt }
193f67bedddSMatthias Schmidt 
194f67bedddSMatthias Schmidt /*
195f67bedddSMatthias Schmidt  * Handle SMTP authentication
196f67bedddSMatthias Schmidt  */
197f67bedddSMatthias Schmidt static int
198f67bedddSMatthias Schmidt smtp_login(struct qitem *it, int fd, char *login, char* password)
199f67bedddSMatthias Schmidt {
200f67bedddSMatthias Schmidt 	char *temp;
2016ef9fe01SMatthias Schmidt 	int len, res = 0;
202f67bedddSMatthias Schmidt 
2037b68d8aeSMatthias Schmidt #ifdef HAVE_CRYPTO
2047b68d8aeSMatthias Schmidt 	res = smtp_auth_md5(it, fd, login, password);
2057b68d8aeSMatthias Schmidt 	if (res == 0) {
2067b68d8aeSMatthias Schmidt 		return (0);
2077b68d8aeSMatthias Schmidt 	} else if (res == -2) {
2087b68d8aeSMatthias Schmidt 	/*
2097b68d8aeSMatthias Schmidt 	 * If the return code is -2, then then the login attempt failed,
2107b68d8aeSMatthias Schmidt 	 * do not try other login mechanisms
2117b68d8aeSMatthias Schmidt 	 */
2127b68d8aeSMatthias Schmidt 		return (-1);
2137b68d8aeSMatthias Schmidt 	}
2147b68d8aeSMatthias Schmidt #endif /* HAVE_CRYPTO */
2157b68d8aeSMatthias Schmidt 
2167b68d8aeSMatthias Schmidt 	if ((config->features & INSECURE) != 0) {
217f67bedddSMatthias Schmidt 		/* Send AUTH command according to RFC 2554 */
218f67bedddSMatthias Schmidt 		send_remote_command(fd, "AUTH LOGIN");
2197b68d8aeSMatthias Schmidt 		if (read_remote(fd, 0, NULL) != 3) {
220f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred:"
221a5a8a1a4SSimon Schubert 					" AUTH login not available: %s",
222a5a8a1a4SSimon Schubert 					it->queueid, neterr);
223f67bedddSMatthias Schmidt 			return (1);
224f67bedddSMatthias Schmidt 		}
225f67bedddSMatthias Schmidt 
226f67bedddSMatthias Schmidt 		len = base64_encode(login, strlen(login), &temp);
227f67bedddSMatthias Schmidt 		if (len <= 0)
228f67bedddSMatthias Schmidt 			return (-1);
229f67bedddSMatthias Schmidt 
230f67bedddSMatthias Schmidt 		send_remote_command(fd, "%s", temp);
2317b68d8aeSMatthias Schmidt 		if (read_remote(fd, 0, NULL) != 3) {
232f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred:"
233a5a8a1a4SSimon Schubert 					" AUTH login failed: %s", it->queueid,
234a5a8a1a4SSimon Schubert 					neterr);
235f67bedddSMatthias Schmidt 			return (-1);
236f67bedddSMatthias Schmidt 		}
237f67bedddSMatthias Schmidt 
238f67bedddSMatthias Schmidt 		len = base64_encode(password, strlen(password), &temp);
239f67bedddSMatthias Schmidt 		if (len <= 0)
240f67bedddSMatthias Schmidt 			return (-1);
241f67bedddSMatthias Schmidt 
242f67bedddSMatthias Schmidt 		send_remote_command(fd, "%s", temp);
2437b68d8aeSMatthias Schmidt 		res = read_remote(fd, 0, NULL);
2446ef9fe01SMatthias Schmidt 		if (res == 5) {
2456ef9fe01SMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery failed:"
246a5a8a1a4SSimon Schubert 					" Authentication failed: %s",
247a5a8a1a4SSimon Schubert 					it->queueid, neterr);
2486ef9fe01SMatthias Schmidt 			return (-1);
2496ef9fe01SMatthias Schmidt 		} else if (res != 2) {
2506ef9fe01SMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery failed:"
251a5a8a1a4SSimon Schubert 					" AUTH password failed: %s",
252a5a8a1a4SSimon Schubert 					it->queueid, neterr);
253f67bedddSMatthias Schmidt 			return (-1);
254f67bedddSMatthias Schmidt 		}
2557b68d8aeSMatthias Schmidt 	} else {
2567b68d8aeSMatthias Schmidt 		syslog(LOG_ERR, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
2577b68d8aeSMatthias Schmidt 				it->queueid);
2587b68d8aeSMatthias Schmidt 		return (1);
2597b68d8aeSMatthias Schmidt 	}
260f67bedddSMatthias Schmidt 
261f67bedddSMatthias Schmidt 	return (0);
262f67bedddSMatthias Schmidt }
263f67bedddSMatthias Schmidt 
264f67bedddSMatthias Schmidt static int
265f67bedddSMatthias Schmidt open_connection(struct qitem *it, const char *host)
266f67bedddSMatthias Schmidt {
267f67bedddSMatthias Schmidt 	struct addrinfo hints, *res, *res0;
268f67bedddSMatthias Schmidt 	char servname[128];
269f67bedddSMatthias Schmidt 	const char *errmsg = NULL;
270f67bedddSMatthias Schmidt 	int fd, error = 0, port;
271f67bedddSMatthias Schmidt 
272dba19026SMatthias Schmidt 	if (config->port != 0)
273f67bedddSMatthias Schmidt 		port = config->port;
274f67bedddSMatthias Schmidt 	else
275f67bedddSMatthias Schmidt 		port = SMTP_PORT;
276f67bedddSMatthias Schmidt 
2777b68d8aeSMatthias Schmidt 	/* FIXME get MX record of host */
278f67bedddSMatthias Schmidt 	/* Shamelessly taken from getaddrinfo(3) */
279f67bedddSMatthias Schmidt 	memset(&hints, 0, sizeof(hints));
280f67bedddSMatthias Schmidt 	hints.ai_family = PF_UNSPEC;
281f67bedddSMatthias Schmidt 	hints.ai_socktype = SOCK_STREAM;
282f67bedddSMatthias Schmidt 	hints.ai_protocol = IPPROTO_TCP;
283f67bedddSMatthias Schmidt 
284f67bedddSMatthias Schmidt 	snprintf(servname, sizeof(servname), "%d", port);
285f67bedddSMatthias Schmidt 	error = getaddrinfo(host, servname, &hints, &res0);
286f67bedddSMatthias Schmidt 	if (error) {
287f67bedddSMatthias Schmidt 		syslog(LOG_ERR, "%s: remote delivery deferred: "
288f67bedddSMatthias Schmidt 		       "%s: %m", it->queueid, gai_strerror(error));
289f67bedddSMatthias Schmidt 		return (-1);
290f67bedddSMatthias Schmidt 	}
291f67bedddSMatthias Schmidt 	fd = -1;
292f67bedddSMatthias Schmidt 	for (res = res0; res; res = res->ai_next) {
293f67bedddSMatthias Schmidt 		fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
294f67bedddSMatthias Schmidt 		if (fd < 0) {
295f67bedddSMatthias Schmidt 			errmsg = "socket failed";
296f67bedddSMatthias Schmidt 			continue;
297f67bedddSMatthias Schmidt 		}
298f67bedddSMatthias Schmidt 		if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
299f67bedddSMatthias Schmidt 			errmsg = "connect failed";
300f67bedddSMatthias Schmidt 			close(fd);
301f67bedddSMatthias Schmidt 			fd = -1;
302f67bedddSMatthias Schmidt 			continue;
303f67bedddSMatthias Schmidt 		}
304f67bedddSMatthias Schmidt 		break;
305f67bedddSMatthias Schmidt 	}
306f67bedddSMatthias Schmidt 	if (fd < 0) {
307f67bedddSMatthias Schmidt 		syslog(LOG_ERR, "%s: remote delivery deferred: %s (%s:%s)",
308f67bedddSMatthias Schmidt 			it->queueid, errmsg, host, servname);
309f67bedddSMatthias Schmidt 		freeaddrinfo(res0);
310f67bedddSMatthias Schmidt 		return (-1);
311f67bedddSMatthias Schmidt 	}
312f67bedddSMatthias Schmidt 	freeaddrinfo(res0);
313f67bedddSMatthias Schmidt 	return (fd);
314f67bedddSMatthias Schmidt }
315f67bedddSMatthias Schmidt 
316f67bedddSMatthias Schmidt int
3175868e44cSSimon Schubert deliver_remote(struct qitem *it, const char **errmsg)
318f67bedddSMatthias Schmidt {
319f67bedddSMatthias Schmidt 	struct authuser *a;
3206ef9fe01SMatthias Schmidt 	char *host, line[1000];
3216ef9fe01SMatthias Schmidt 	int fd, error = 0, do_auth = 0, res = 0;
322f67bedddSMatthias Schmidt 	size_t linelen;
3235868e44cSSimon Schubert 	/* asprintf can't take const */
3245868e44cSSimon Schubert 	void *errmsgc = __DECONST(char **, errmsg);
325f67bedddSMatthias Schmidt 
326f67bedddSMatthias Schmidt 	host = strrchr(it->addr, '@');
327f67bedddSMatthias Schmidt 	/* Should not happen */
32839f7c85aSSimon Schubert 	if (host == NULL) {
3295868e44cSSimon Schubert 		asprintf(errmsgc, "Internal error: badly formed address %s",
33039f7c85aSSimon Schubert 		    it->addr);
331f67bedddSMatthias Schmidt 		return(-1);
33239f7c85aSSimon Schubert 	} else {
333f67bedddSMatthias Schmidt 		/* Step over the @ */
334f67bedddSMatthias Schmidt 		host++;
33539f7c85aSSimon Schubert 	}
336f67bedddSMatthias Schmidt 
337f67bedddSMatthias Schmidt 	/* Smarthost support? */
338f67bedddSMatthias Schmidt 	if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
3394a23bd3dSMatthias Schmidt 		syslog(LOG_INFO, "%s: using smarthost (%s:%i)",
3404a23bd3dSMatthias Schmidt 		       it->queueid, config->smarthost, config->port);
341f67bedddSMatthias Schmidt 		host = config->smarthost;
342f67bedddSMatthias Schmidt 	}
343f67bedddSMatthias Schmidt 
344f67bedddSMatthias Schmidt 	fd = open_connection(it, host);
345f67bedddSMatthias Schmidt 	if (fd < 0)
346f67bedddSMatthias Schmidt 		return (1);
347f67bedddSMatthias Schmidt 
3486ef9fe01SMatthias Schmidt 	/* Check first reply from remote host */
3496ef9fe01SMatthias Schmidt 	config->features |= NOSSL;
3507b68d8aeSMatthias Schmidt 	res = read_remote(fd, 0, NULL);
3516ef9fe01SMatthias Schmidt 	if (res != 2) {
3526ef9fe01SMatthias Schmidt 		syslog(LOG_INFO, "%s: Invalid initial response: %i",
3536ef9fe01SMatthias Schmidt 			it->queueid, res);
3546ef9fe01SMatthias Schmidt 		return(1);
3556ef9fe01SMatthias Schmidt 	}
3566ef9fe01SMatthias Schmidt 	config->features &= ~NOSSL;
3576ef9fe01SMatthias Schmidt 
358f67bedddSMatthias Schmidt #ifdef HAVE_CRYPTO
359f67bedddSMatthias Schmidt 	if ((config->features & SECURETRANS) != 0) {
360f67bedddSMatthias Schmidt 		error = smtp_init_crypto(it, fd, config->features);
361f67bedddSMatthias Schmidt 		if (error >= 0)
3620ca0cd25SSascha Wildner 			syslog(LOG_INFO, "%s: SSL initialization successful",
363f67bedddSMatthias Schmidt 				it->queueid);
364f67bedddSMatthias Schmidt 		else
365f67bedddSMatthias Schmidt 			goto out;
366f67bedddSMatthias Schmidt 	}
367f67bedddSMatthias Schmidt #endif /* HAVE_CRYPTO */
368*66674731SSimon Schubert 
369f67bedddSMatthias Schmidt 	send_remote_command(fd, "EHLO %s", hostname());
3707b68d8aeSMatthias Schmidt 	if (read_remote(fd, 0, NULL) != 2) {
371f67bedddSMatthias Schmidt 		syslog(LOG_ERR, "%s: remote delivery deferred: "
372a5a8a1a4SSimon Schubert 		       " EHLO failed: %s", it->queueid, neterr);
3735868e44cSSimon Schubert 		asprintf(errmsgc, "%s did not like our EHLO:\n%s",
37439f7c85aSSimon Schubert 		    host, neterr);
375f67bedddSMatthias Schmidt 		return (-1);
376f67bedddSMatthias Schmidt 	}
377f67bedddSMatthias Schmidt 
378f67bedddSMatthias Schmidt 	/*
379f67bedddSMatthias Schmidt 	 * Use SMTP authentication if the user defined an entry for the remote
380f67bedddSMatthias Schmidt 	 * or smarthost
381f67bedddSMatthias Schmidt 	 */
382f67bedddSMatthias Schmidt 	SLIST_FOREACH(a, &authusers, next) {
383f67bedddSMatthias Schmidt 		if (strcmp(a->host, host) == 0) {
384f67bedddSMatthias Schmidt 			do_auth = 1;
385f67bedddSMatthias Schmidt 			break;
386f67bedddSMatthias Schmidt 		}
387f67bedddSMatthias Schmidt 	}
388f67bedddSMatthias Schmidt 
389f67bedddSMatthias Schmidt 	if (do_auth == 1) {
390b558d098SMatthias Schmidt 		/*
391b558d098SMatthias Schmidt 		 * Check if the user wants plain text login without using
392b558d098SMatthias Schmidt 		 * encryption.
393b558d098SMatthias Schmidt 		 */
394b558d098SMatthias Schmidt 		syslog(LOG_INFO, "%s: Use SMTP authentication",
395b558d098SMatthias Schmidt 				it->queueid);
396f67bedddSMatthias Schmidt 		error = smtp_login(it, fd, a->login, a->password);
397f67bedddSMatthias Schmidt 		if (error < 0) {
398f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery failed:"
399f67bedddSMatthias Schmidt 					" SMTP login failed: %m", it->queueid);
4005868e44cSSimon Schubert 			asprintf(errmsgc, "SMTP login to %s failed", host);
401f67bedddSMatthias Schmidt 			return (-1);
402f67bedddSMatthias Schmidt 		}
403f67bedddSMatthias Schmidt 		/* SMTP login is not available, so try without */
404f67bedddSMatthias Schmidt 		else if (error > 0)
405b558d098SMatthias Schmidt 			syslog(LOG_ERR, "%s: SMTP login not available."
406b558d098SMatthias Schmidt 					" Try without", it->queueid);
407b558d098SMatthias Schmidt 	}
408f67bedddSMatthias Schmidt 
4097dfd2fd8SSimon Schubert #define READ_REMOTE_CHECK(c, exp)	\
4107dfd2fd8SSimon Schubert 	res = read_remote(fd, 0, NULL); \
4117dfd2fd8SSimon Schubert 	if (res == 5) { \
4127dfd2fd8SSimon Schubert 		syslog(LOG_ERR, "%s: remote delivery failed: " \
4137dfd2fd8SSimon Schubert 		       c " failed: %s", it->queueid, neterr); \
4145868e44cSSimon Schubert 		asprintf(errmsgc, "%s did not like our " c ":\n%s", \
41539f7c85aSSimon Schubert 		    host, neterr); \
4167dfd2fd8SSimon Schubert 		return (-1); \
4177dfd2fd8SSimon Schubert 	} else if (res != exp) { \
4187dfd2fd8SSimon Schubert 		syslog(LOG_ERR, "%s: remote delivery deferred: " \
4197dfd2fd8SSimon Schubert 		       c " failed: %s", it->queueid, neterr); \
4207dfd2fd8SSimon Schubert 		return (1); \
421f67bedddSMatthias Schmidt 	}
422f67bedddSMatthias Schmidt 
4237dfd2fd8SSimon Schubert 	send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
4247dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("MAIL FROM", 2);
4257dfd2fd8SSimon Schubert 
426f67bedddSMatthias Schmidt 	send_remote_command(fd, "RCPT TO:<%s>", it->addr);
4277dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("RCPT TO", 2);
428f67bedddSMatthias Schmidt 
429f67bedddSMatthias Schmidt 	send_remote_command(fd, "DATA");
4307dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("DATA", 3);
431f67bedddSMatthias Schmidt 
432f67bedddSMatthias Schmidt 	if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
433a5a8a1a4SSimon Schubert 		syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s",
434a5a8a1a4SSimon Schubert 		       it->queueid, neterr);
435f67bedddSMatthias Schmidt 		return (1);
436f67bedddSMatthias Schmidt 	}
437f67bedddSMatthias Schmidt 
438f67bedddSMatthias Schmidt 	while (!feof(it->queuef)) {
439f67bedddSMatthias Schmidt 		if (fgets(line, sizeof(line), it->queuef) == NULL)
440f67bedddSMatthias Schmidt 			break;
441f67bedddSMatthias Schmidt 		linelen = strlen(line);
442f67bedddSMatthias Schmidt 		if (linelen == 0 || line[linelen - 1] != '\n') {
443f67bedddSMatthias Schmidt 			syslog(LOG_CRIT, "%s: remote delivery failed:"
444f67bedddSMatthias Schmidt 				"corrupted queue file", it->queueid);
4455868e44cSSimon Schubert 			*errmsg = "corrupted queue file";
446f67bedddSMatthias Schmidt 			error = -1;
447f67bedddSMatthias Schmidt 			goto out;
448f67bedddSMatthias Schmidt 		}
449f67bedddSMatthias Schmidt 
450f67bedddSMatthias Schmidt 		/* Remove trailing \n's and escape leading dots */
451f67bedddSMatthias Schmidt 		trim_line(line);
452f67bedddSMatthias Schmidt 
453f67bedddSMatthias Schmidt 		/*
454f67bedddSMatthias Schmidt 		 * If the first character is a dot, we escape it so the line
455f67bedddSMatthias Schmidt 		 * length increases
456f67bedddSMatthias Schmidt 		*/
457f67bedddSMatthias Schmidt 		if (line[0] == '.')
458f67bedddSMatthias Schmidt 			linelen++;
459f67bedddSMatthias Schmidt 
460f67bedddSMatthias Schmidt 		if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
461f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred: "
462f67bedddSMatthias Schmidt 				"write error", it->queueid);
463f67bedddSMatthias Schmidt 			error = 1;
464f67bedddSMatthias Schmidt 			goto out;
465f67bedddSMatthias Schmidt 		}
466f67bedddSMatthias Schmidt 	}
467f67bedddSMatthias Schmidt 
468f67bedddSMatthias Schmidt 	send_remote_command(fd, ".");
4697dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("final DATA", 2);
470f67bedddSMatthias Schmidt 
471f67bedddSMatthias Schmidt 	send_remote_command(fd, "QUIT");
4726cfc247dSSimon Schubert 	if (read_remote(fd, 0, NULL) != 2)
4736cfc247dSSimon Schubert 		syslog(LOG_WARNING, "%s: remote delivery succeeded but "
474a5a8a1a4SSimon Schubert 		       "QUIT failed: %s", it->queueid, neterr);
475f67bedddSMatthias Schmidt out:
476f67bedddSMatthias Schmidt 
477f67bedddSMatthias Schmidt 	close(fd);
478f67bedddSMatthias Schmidt 	return (error);
479f67bedddSMatthias Schmidt }
480f67bedddSMatthias Schmidt 
481