xref: /dflybsd-src/libexec/dma/net.c (revision 7dfd2fd85ea3a54ef432289373f57655c543cf7e)
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];
76f67bedddSMatthias Schmidt 	ssize_t len = 0;
77f67bedddSMatthias Schmidt 
78f67bedddSMatthias Schmidt 	va_start(va, fmt);
79f67bedddSMatthias Schmidt 	vsprintf(cmd, fmt, va);
80f67bedddSMatthias Schmidt 
81f67bedddSMatthias Schmidt 	if (((config->features & SECURETRANS) != 0) &&
826ef9fe01SMatthias Schmidt 	    ((config->features & NOSSL) == 0)) {
83f67bedddSMatthias Schmidt 		len = SSL_write(config->ssl, (const char*)cmd, strlen(cmd));
84f67bedddSMatthias Schmidt 		SSL_write(config->ssl, "\r\n", 2);
85f67bedddSMatthias Schmidt 	}
86f67bedddSMatthias Schmidt 	else {
87f67bedddSMatthias Schmidt 		len = write(fd, cmd, strlen(cmd));
88f67bedddSMatthias Schmidt 		write(fd, "\r\n", 2);
89f67bedddSMatthias Schmidt 	}
90f67bedddSMatthias Schmidt 	va_end(va);
91f67bedddSMatthias Schmidt 
92f67bedddSMatthias Schmidt 	return (len+2);
93f67bedddSMatthias Schmidt }
94f67bedddSMatthias Schmidt 
956ef9fe01SMatthias Schmidt int
967b68d8aeSMatthias Schmidt read_remote(int fd, int extbufsize, char *extbuf)
97f67bedddSMatthias Schmidt {
986ef9fe01SMatthias Schmidt 	ssize_t rlen = 0;
996ef9fe01SMatthias Schmidt 	size_t pos, len;
1006ef9fe01SMatthias Schmidt 	char buff[BUF_SIZE];
1017b68d8aeSMatthias Schmidt 	int done = 0, status = 0, extbufpos = 0;
102f67bedddSMatthias Schmidt 
103f67bedddSMatthias Schmidt 	if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
104a5a8a1a4SSimon Schubert 		snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
105a5a8a1a4SSimon Schubert 		    strerror(errno));
106a5a8a1a4SSimon Schubert 		return (1);
107f67bedddSMatthias Schmidt 	}
108f67bedddSMatthias Schmidt 	if (setjmp(timeout_alarm) != 0) {
109a5a8a1a4SSimon Schubert 		snprintf(neterr, sizeof(neterr), "Timeout reached");
110f67bedddSMatthias Schmidt 		return (1);
111f67bedddSMatthias Schmidt 	}
112f67bedddSMatthias Schmidt 	alarm(CON_TIMEOUT);
113f67bedddSMatthias Schmidt 
114f67bedddSMatthias Schmidt 	/*
1156ef9fe01SMatthias Schmidt 	 * Remote reading code from femail.c written by Henning Brauer of
1166ef9fe01SMatthias Schmidt 	 * OpenBSD and released under a BSD style license.
117f67bedddSMatthias Schmidt 	 */
1186ef9fe01SMatthias Schmidt 	for (len = pos = 0; !done; ) {
1197b68d8aeSMatthias Schmidt 		rlen = 0;
1206ef9fe01SMatthias Schmidt 		if (pos == 0 ||
1216ef9fe01SMatthias Schmidt 		    (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
1226ef9fe01SMatthias Schmidt 			memmove(buff, buff + pos, len - pos);
1236ef9fe01SMatthias Schmidt 			len -= pos;
1246ef9fe01SMatthias Schmidt 			pos = 0;
125f67bedddSMatthias Schmidt 			if (((config->features & SECURETRANS) != 0) &&
1266ef9fe01SMatthias Schmidt 			    (config->features & NOSSL) == 0) {
1276ef9fe01SMatthias Schmidt 				if ((rlen = SSL_read(config->ssl, buff + len,
1286ef9fe01SMatthias Schmidt 				    sizeof(buff) - len)) == -1)
1296ef9fe01SMatthias Schmidt 					err(1, "read");
1306ef9fe01SMatthias Schmidt 			} else {
1316ef9fe01SMatthias Schmidt 				if ((rlen = read(fd, buff + len,
1326ef9fe01SMatthias Schmidt 				    sizeof(buff) - len)) == -1)
1336ef9fe01SMatthias Schmidt 					err(1, "read");
1346ef9fe01SMatthias Schmidt 			}
1356ef9fe01SMatthias Schmidt 			len += rlen;
1366ef9fe01SMatthias Schmidt 		}
1377b68d8aeSMatthias Schmidt 		/*
1387b68d8aeSMatthias Schmidt 		 * If there is an external buffer with a size bigger than zero
1397b68d8aeSMatthias Schmidt 		 * and as long as there is space in the external buffer and
1407b68d8aeSMatthias Schmidt 		 * there are new characters read from the mailserver
1417b68d8aeSMatthias Schmidt 		 * copy them to the external buffer
1427b68d8aeSMatthias Schmidt 		 */
1437b68d8aeSMatthias Schmidt 		if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
1447b68d8aeSMatthias Schmidt 		    && extbuf != NULL) {
1457b68d8aeSMatthias Schmidt 			/* do not write over the bounds of the buffer */
1467b68d8aeSMatthias Schmidt 			if(extbufpos + rlen > (extbufsize - 1)) {
1477b68d8aeSMatthias Schmidt 				rlen = extbufsize - extbufpos;
1487b68d8aeSMatthias Schmidt 			}
1497b68d8aeSMatthias Schmidt 			memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
1507b68d8aeSMatthias Schmidt 			extbufpos += rlen;
1517b68d8aeSMatthias Schmidt 		}
1526ef9fe01SMatthias Schmidt 		for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
1536ef9fe01SMatthias Schmidt 			; /* Do nothing */
154f67bedddSMatthias Schmidt 
1556ef9fe01SMatthias Schmidt 		if (pos == len)
1566ef9fe01SMatthias Schmidt 			return (0);
1576ef9fe01SMatthias Schmidt 
1586ef9fe01SMatthias Schmidt 		if (buff[pos] == ' ')
1596ef9fe01SMatthias Schmidt 			done = 1;
1606ef9fe01SMatthias Schmidt 		else if (buff[pos] != '-')
1616ef9fe01SMatthias Schmidt 			syslog(LOG_ERR, "invalid syntax in reply from server");
1626ef9fe01SMatthias Schmidt 
1636ef9fe01SMatthias Schmidt 		/* skip up to \n */
1646ef9fe01SMatthias Schmidt 		for (; pos < len && buff[pos - 1] != '\n'; pos++)
1656ef9fe01SMatthias Schmidt 			; /* Do nothing */
1666ef9fe01SMatthias Schmidt 
1676ef9fe01SMatthias Schmidt 	}
168f67bedddSMatthias Schmidt 	alarm(0);
169f67bedddSMatthias Schmidt 
170a5a8a1a4SSimon Schubert 	buff[len] = '\0';
171a5a8a1a4SSimon Schubert 	while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
172a5a8a1a4SSimon Schubert 		buff[--len] = '\0';
173a5a8a1a4SSimon Schubert 	snprintf(neterr, sizeof(neterr), "%s", buff);
1746ef9fe01SMatthias Schmidt 	status = atoi(buff);
1756ef9fe01SMatthias Schmidt 	return (status/100);
176f67bedddSMatthias Schmidt }
177f67bedddSMatthias Schmidt 
178f67bedddSMatthias Schmidt /*
179f67bedddSMatthias Schmidt  * Handle SMTP authentication
180f67bedddSMatthias Schmidt  */
181f67bedddSMatthias Schmidt static int
182f67bedddSMatthias Schmidt smtp_login(struct qitem *it, int fd, char *login, char* password)
183f67bedddSMatthias Schmidt {
184f67bedddSMatthias Schmidt 	char *temp;
1856ef9fe01SMatthias Schmidt 	int len, res = 0;
186f67bedddSMatthias Schmidt 
1877b68d8aeSMatthias Schmidt #ifdef HAVE_CRYPTO
1887b68d8aeSMatthias Schmidt 	res = smtp_auth_md5(it, fd, login, password);
1897b68d8aeSMatthias Schmidt 	if (res == 0) {
1907b68d8aeSMatthias Schmidt 		return (0);
1917b68d8aeSMatthias Schmidt 	} else if (res == -2) {
1927b68d8aeSMatthias Schmidt 	/*
1937b68d8aeSMatthias Schmidt 	 * If the return code is -2, then then the login attempt failed,
1947b68d8aeSMatthias Schmidt 	 * do not try other login mechanisms
1957b68d8aeSMatthias Schmidt 	 */
1967b68d8aeSMatthias Schmidt 		return (-1);
1977b68d8aeSMatthias Schmidt 	}
1987b68d8aeSMatthias Schmidt #endif /* HAVE_CRYPTO */
1997b68d8aeSMatthias Schmidt 
2007b68d8aeSMatthias Schmidt 	if ((config->features & INSECURE) != 0) {
201f67bedddSMatthias Schmidt 		/* Send AUTH command according to RFC 2554 */
202f67bedddSMatthias Schmidt 		send_remote_command(fd, "AUTH LOGIN");
2037b68d8aeSMatthias Schmidt 		if (read_remote(fd, 0, NULL) != 3) {
204f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred:"
205a5a8a1a4SSimon Schubert 					" AUTH login not available: %s",
206a5a8a1a4SSimon Schubert 					it->queueid, neterr);
207f67bedddSMatthias Schmidt 			return (1);
208f67bedddSMatthias Schmidt 		}
209f67bedddSMatthias Schmidt 
210f67bedddSMatthias Schmidt 		len = base64_encode(login, strlen(login), &temp);
211f67bedddSMatthias Schmidt 		if (len <= 0)
212f67bedddSMatthias Schmidt 			return (-1);
213f67bedddSMatthias Schmidt 
214f67bedddSMatthias Schmidt 		send_remote_command(fd, "%s", temp);
2157b68d8aeSMatthias Schmidt 		if (read_remote(fd, 0, NULL) != 3) {
216f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred:"
217a5a8a1a4SSimon Schubert 					" AUTH login failed: %s", it->queueid,
218a5a8a1a4SSimon Schubert 					neterr);
219f67bedddSMatthias Schmidt 			return (-1);
220f67bedddSMatthias Schmidt 		}
221f67bedddSMatthias Schmidt 
222f67bedddSMatthias Schmidt 		len = base64_encode(password, strlen(password), &temp);
223f67bedddSMatthias Schmidt 		if (len <= 0)
224f67bedddSMatthias Schmidt 			return (-1);
225f67bedddSMatthias Schmidt 
226f67bedddSMatthias Schmidt 		send_remote_command(fd, "%s", temp);
2277b68d8aeSMatthias Schmidt 		res = read_remote(fd, 0, NULL);
2286ef9fe01SMatthias Schmidt 		if (res == 5) {
2296ef9fe01SMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery failed:"
230a5a8a1a4SSimon Schubert 					" Authentication failed: %s",
231a5a8a1a4SSimon Schubert 					it->queueid, neterr);
2326ef9fe01SMatthias Schmidt 			return (-1);
2336ef9fe01SMatthias Schmidt 		} else if (res != 2) {
2346ef9fe01SMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery failed:"
235a5a8a1a4SSimon Schubert 					" AUTH password failed: %s",
236a5a8a1a4SSimon Schubert 					it->queueid, neterr);
237f67bedddSMatthias Schmidt 			return (-1);
238f67bedddSMatthias Schmidt 		}
2397b68d8aeSMatthias Schmidt 	} else {
2407b68d8aeSMatthias Schmidt 		syslog(LOG_ERR, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
2417b68d8aeSMatthias Schmidt 				it->queueid);
2427b68d8aeSMatthias Schmidt 		return (1);
2437b68d8aeSMatthias Schmidt 	}
244f67bedddSMatthias Schmidt 
245f67bedddSMatthias Schmidt 	return (0);
246f67bedddSMatthias Schmidt }
247f67bedddSMatthias Schmidt 
248f67bedddSMatthias Schmidt static int
249f67bedddSMatthias Schmidt open_connection(struct qitem *it, const char *host)
250f67bedddSMatthias Schmidt {
251f67bedddSMatthias Schmidt 	struct addrinfo hints, *res, *res0;
252f67bedddSMatthias Schmidt 	char servname[128];
253f67bedddSMatthias Schmidt 	const char *errmsg = NULL;
254f67bedddSMatthias Schmidt 	int fd, error = 0, port;
255f67bedddSMatthias Schmidt 
256dba19026SMatthias Schmidt 	if (config->port != 0)
257f67bedddSMatthias Schmidt 		port = config->port;
258f67bedddSMatthias Schmidt 	else
259f67bedddSMatthias Schmidt 		port = SMTP_PORT;
260f67bedddSMatthias Schmidt 
2617b68d8aeSMatthias Schmidt 	/* FIXME get MX record of host */
262f67bedddSMatthias Schmidt 	/* Shamelessly taken from getaddrinfo(3) */
263f67bedddSMatthias Schmidt 	memset(&hints, 0, sizeof(hints));
264f67bedddSMatthias Schmidt 	hints.ai_family = PF_UNSPEC;
265f67bedddSMatthias Schmidt 	hints.ai_socktype = SOCK_STREAM;
266f67bedddSMatthias Schmidt 	hints.ai_protocol = IPPROTO_TCP;
267f67bedddSMatthias Schmidt 
268f67bedddSMatthias Schmidt 	snprintf(servname, sizeof(servname), "%d", port);
269f67bedddSMatthias Schmidt 	error = getaddrinfo(host, servname, &hints, &res0);
270f67bedddSMatthias Schmidt 	if (error) {
271f67bedddSMatthias Schmidt 		syslog(LOG_ERR, "%s: remote delivery deferred: "
272f67bedddSMatthias Schmidt 		       "%s: %m", it->queueid, gai_strerror(error));
273f67bedddSMatthias Schmidt 		return (-1);
274f67bedddSMatthias Schmidt 	}
275f67bedddSMatthias Schmidt 	fd = -1;
276f67bedddSMatthias Schmidt 	for (res = res0; res; res = res->ai_next) {
277f67bedddSMatthias Schmidt 		fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
278f67bedddSMatthias Schmidt 		if (fd < 0) {
279f67bedddSMatthias Schmidt 			errmsg = "socket failed";
280f67bedddSMatthias Schmidt 			continue;
281f67bedddSMatthias Schmidt 		}
282f67bedddSMatthias Schmidt 		if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
283f67bedddSMatthias Schmidt 			errmsg = "connect failed";
284f67bedddSMatthias Schmidt 			close(fd);
285f67bedddSMatthias Schmidt 			fd = -1;
286f67bedddSMatthias Schmidt 			continue;
287f67bedddSMatthias Schmidt 		}
288f67bedddSMatthias Schmidt 		break;
289f67bedddSMatthias Schmidt 	}
290f67bedddSMatthias Schmidt 	if (fd < 0) {
291f67bedddSMatthias Schmidt 		syslog(LOG_ERR, "%s: remote delivery deferred: %s (%s:%s)",
292f67bedddSMatthias Schmidt 			it->queueid, errmsg, host, servname);
293f67bedddSMatthias Schmidt 		freeaddrinfo(res0);
294f67bedddSMatthias Schmidt 		return (-1);
295f67bedddSMatthias Schmidt 	}
296f67bedddSMatthias Schmidt 	freeaddrinfo(res0);
297f67bedddSMatthias Schmidt 	return (fd);
298f67bedddSMatthias Schmidt }
299f67bedddSMatthias Schmidt 
300f67bedddSMatthias Schmidt int
3014a23bd3dSMatthias Schmidt deliver_remote(struct qitem *it, const char **errmsg)
302f67bedddSMatthias Schmidt {
303f67bedddSMatthias Schmidt 	struct authuser *a;
3046ef9fe01SMatthias Schmidt 	char *host, line[1000];
3056ef9fe01SMatthias Schmidt 	int fd, error = 0, do_auth = 0, res = 0;
306f67bedddSMatthias Schmidt 	size_t linelen;
307f67bedddSMatthias Schmidt 
308f67bedddSMatthias Schmidt 	host = strrchr(it->addr, '@');
309f67bedddSMatthias Schmidt 	/* Should not happen */
310f67bedddSMatthias Schmidt 	if (host == NULL)
311f67bedddSMatthias Schmidt 		return(-1);
312f67bedddSMatthias Schmidt 	else
313f67bedddSMatthias Schmidt 		/* Step over the @ */
314f67bedddSMatthias Schmidt 		host++;
315f67bedddSMatthias Schmidt 
316f67bedddSMatthias Schmidt 	/* Smarthost support? */
317f67bedddSMatthias Schmidt 	if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
3184a23bd3dSMatthias Schmidt 		syslog(LOG_INFO, "%s: using smarthost (%s:%i)",
3194a23bd3dSMatthias Schmidt 		       it->queueid, config->smarthost, config->port);
320f67bedddSMatthias Schmidt 		host = config->smarthost;
321f67bedddSMatthias Schmidt 	}
322f67bedddSMatthias Schmidt 
323f67bedddSMatthias Schmidt 	fd = open_connection(it, host);
324f67bedddSMatthias Schmidt 	if (fd < 0)
325f67bedddSMatthias Schmidt 		return (1);
326f67bedddSMatthias Schmidt 
3276ef9fe01SMatthias Schmidt 	/* Check first reply from remote host */
3286ef9fe01SMatthias Schmidt 	config->features |= NOSSL;
3297b68d8aeSMatthias Schmidt 	res = read_remote(fd, 0, NULL);
3306ef9fe01SMatthias Schmidt 	if (res != 2) {
3316ef9fe01SMatthias Schmidt 		syslog(LOG_INFO, "%s: Invalid initial response: %i",
3326ef9fe01SMatthias Schmidt 			it->queueid, res);
3336ef9fe01SMatthias Schmidt 		return(1);
3346ef9fe01SMatthias Schmidt 	}
3356ef9fe01SMatthias Schmidt 	config->features &= ~NOSSL;
3366ef9fe01SMatthias Schmidt 
337f67bedddSMatthias Schmidt #ifdef HAVE_CRYPTO
338f67bedddSMatthias Schmidt 	if ((config->features & SECURETRANS) != 0) {
339f67bedddSMatthias Schmidt 		error = smtp_init_crypto(it, fd, config->features);
340f67bedddSMatthias Schmidt 		if (error >= 0)
3410ca0cd25SSascha Wildner 			syslog(LOG_INFO, "%s: SSL initialization successful",
342f67bedddSMatthias Schmidt 				it->queueid);
343f67bedddSMatthias Schmidt 		else
344f67bedddSMatthias Schmidt 			goto out;
345f67bedddSMatthias Schmidt 	}
346f67bedddSMatthias Schmidt 
347f67bedddSMatthias Schmidt 	/*
348f67bedddSMatthias Schmidt 	 * If the user doesn't want STARTTLS, but SSL encryption, we
349f67bedddSMatthias Schmidt 	 * have to enable SSL first, then send EHLO
350f67bedddSMatthias Schmidt 	 */
351f67bedddSMatthias Schmidt 	if (((config->features & STARTTLS) == 0) &&
352f67bedddSMatthias Schmidt 	    ((config->features & SECURETRANS) != 0)) {
353f67bedddSMatthias Schmidt 		send_remote_command(fd, "EHLO %s", hostname());
3547b68d8aeSMatthias Schmidt 		if (read_remote(fd, 0, NULL) != 2) {
355f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred: "
356a5a8a1a4SSimon Schubert 			       " EHLO failed: %s", it->queueid, neterr);
357f67bedddSMatthias Schmidt 			return (-1);
358f67bedddSMatthias Schmidt 		}
359f67bedddSMatthias Schmidt 	}
360f67bedddSMatthias Schmidt #endif /* HAVE_CRYPTO */
361f67bedddSMatthias Schmidt 	if (((config->features & SECURETRANS) == 0)) {
362f67bedddSMatthias Schmidt 		send_remote_command(fd, "EHLO %s", hostname());
3637b68d8aeSMatthias Schmidt 		if (read_remote(fd, 0, NULL) != 2) {
364f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred: "
365a5a8a1a4SSimon Schubert 			       " EHLO failed: %s", it->queueid, neterr);
366f67bedddSMatthias Schmidt 			return (-1);
367f67bedddSMatthias Schmidt 		}
368f67bedddSMatthias Schmidt 	}
369f67bedddSMatthias Schmidt 
370f67bedddSMatthias Schmidt 	/*
371f67bedddSMatthias Schmidt 	 * Use SMTP authentication if the user defined an entry for the remote
372f67bedddSMatthias Schmidt 	 * or smarthost
373f67bedddSMatthias Schmidt 	 */
374f67bedddSMatthias Schmidt 	SLIST_FOREACH(a, &authusers, next) {
375f67bedddSMatthias Schmidt 		if (strcmp(a->host, host) == 0) {
376f67bedddSMatthias Schmidt 			do_auth = 1;
377f67bedddSMatthias Schmidt 			break;
378f67bedddSMatthias Schmidt 		}
379f67bedddSMatthias Schmidt 	}
380f67bedddSMatthias Schmidt 
381f67bedddSMatthias Schmidt 	if (do_auth == 1) {
382b558d098SMatthias Schmidt 		/*
383b558d098SMatthias Schmidt 		 * Check if the user wants plain text login without using
384b558d098SMatthias Schmidt 		 * encryption.
385b558d098SMatthias Schmidt 		 */
386b558d098SMatthias Schmidt 		syslog(LOG_INFO, "%s: Use SMTP authentication",
387b558d098SMatthias Schmidt 				it->queueid);
388f67bedddSMatthias Schmidt 		error = smtp_login(it, fd, a->login, a->password);
389f67bedddSMatthias Schmidt 		if (error < 0) {
390f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery failed:"
391f67bedddSMatthias Schmidt 					" SMTP login failed: %m", it->queueid);
392f67bedddSMatthias Schmidt 			return (-1);
393f67bedddSMatthias Schmidt 		}
394f67bedddSMatthias Schmidt 		/* SMTP login is not available, so try without */
395f67bedddSMatthias Schmidt 		else if (error > 0)
396b558d098SMatthias Schmidt 			syslog(LOG_ERR, "%s: SMTP login not available."
397b558d098SMatthias Schmidt 					" Try without", it->queueid);
398b558d098SMatthias Schmidt 	}
399f67bedddSMatthias Schmidt 
400*7dfd2fd8SSimon Schubert #define READ_REMOTE_CHECK(c, exp)	\
401*7dfd2fd8SSimon Schubert 	res = read_remote(fd, 0, NULL); \
402*7dfd2fd8SSimon Schubert 	if (res == 5) { \
403*7dfd2fd8SSimon Schubert 		syslog(LOG_ERR, "%s: remote delivery failed: " \
404*7dfd2fd8SSimon Schubert 		       c " failed: %s", it->queueid, neterr); \
405*7dfd2fd8SSimon Schubert 		return (-1); \
406*7dfd2fd8SSimon Schubert 	} else if (res != exp) { \
407*7dfd2fd8SSimon Schubert 		syslog(LOG_ERR, "%s: remote delivery deferred: " \
408*7dfd2fd8SSimon Schubert 		       c " failed: %s", it->queueid, neterr); \
409*7dfd2fd8SSimon Schubert 		return (1); \
410f67bedddSMatthias Schmidt 	}
411f67bedddSMatthias Schmidt 
412*7dfd2fd8SSimon Schubert 	send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
413*7dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("MAIL FROM", 2);
414*7dfd2fd8SSimon Schubert 
415f67bedddSMatthias Schmidt 	send_remote_command(fd, "RCPT TO:<%s>", it->addr);
416*7dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("RCPT TO", 2);
417f67bedddSMatthias Schmidt 
418f67bedddSMatthias Schmidt 	send_remote_command(fd, "DATA");
419*7dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("DATA", 3);
420f67bedddSMatthias Schmidt 
421f67bedddSMatthias Schmidt 	if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
422a5a8a1a4SSimon Schubert 		syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s",
423a5a8a1a4SSimon Schubert 		       it->queueid, neterr);
424f67bedddSMatthias Schmidt 		return (1);
425f67bedddSMatthias Schmidt 	}
426f67bedddSMatthias Schmidt 
427f67bedddSMatthias Schmidt 	while (!feof(it->queuef)) {
428f67bedddSMatthias Schmidt 		if (fgets(line, sizeof(line), it->queuef) == NULL)
429f67bedddSMatthias Schmidt 			break;
430f67bedddSMatthias Schmidt 		linelen = strlen(line);
431f67bedddSMatthias Schmidt 		if (linelen == 0 || line[linelen - 1] != '\n') {
432f67bedddSMatthias Schmidt 			syslog(LOG_CRIT, "%s: remote delivery failed:"
433f67bedddSMatthias Schmidt 				"corrupted queue file", it->queueid);
434f67bedddSMatthias Schmidt 			*errmsg = "corrupted queue file";
435f67bedddSMatthias Schmidt 			error = -1;
436f67bedddSMatthias Schmidt 			goto out;
437f67bedddSMatthias Schmidt 		}
438f67bedddSMatthias Schmidt 
439f67bedddSMatthias Schmidt 		/* Remove trailing \n's and escape leading dots */
440f67bedddSMatthias Schmidt 		trim_line(line);
441f67bedddSMatthias Schmidt 
442f67bedddSMatthias Schmidt 		/*
443f67bedddSMatthias Schmidt 		 * If the first character is a dot, we escape it so the line
444f67bedddSMatthias Schmidt 		 * length increases
445f67bedddSMatthias Schmidt 		*/
446f67bedddSMatthias Schmidt 		if (line[0] == '.')
447f67bedddSMatthias Schmidt 			linelen++;
448f67bedddSMatthias Schmidt 
449f67bedddSMatthias Schmidt 		if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
450f67bedddSMatthias Schmidt 			syslog(LOG_ERR, "%s: remote delivery deferred: "
451f67bedddSMatthias Schmidt 				"write error", it->queueid);
452f67bedddSMatthias Schmidt 			error = 1;
453f67bedddSMatthias Schmidt 			goto out;
454f67bedddSMatthias Schmidt 		}
455f67bedddSMatthias Schmidt 	}
456f67bedddSMatthias Schmidt 
457f67bedddSMatthias Schmidt 	send_remote_command(fd, ".");
458*7dfd2fd8SSimon Schubert 	READ_REMOTE_CHECK("final DATA", 2);
459f67bedddSMatthias Schmidt 
460f67bedddSMatthias Schmidt 	send_remote_command(fd, "QUIT");
4616cfc247dSSimon Schubert 	if (read_remote(fd, 0, NULL) != 2)
4626cfc247dSSimon Schubert 		syslog(LOG_WARNING, "%s: remote delivery succeeded but "
463a5a8a1a4SSimon Schubert 		       "QUIT failed: %s", it->queueid, neterr);
464f67bedddSMatthias Schmidt out:
465f67bedddSMatthias Schmidt 
466f67bedddSMatthias Schmidt 	close(fd);
467f67bedddSMatthias Schmidt 	return (error);
468f67bedddSMatthias Schmidt }
469f67bedddSMatthias Schmidt 
470