2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $DragonFly: src/libexec/dma/net.c,v 1.9 2008/09/30 17:47:21 swildner Exp $
38 #include <sys/param.h>
39 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <openssl/ssl.h>
48 #endif /* HAVE_CRYPTO */
60 extern struct config *config;
61 extern struct authusers authusers;
62 static jmp_buf timeout_alarm;
63 char neterr[BUF_SIZE];
66 sig_alarm(int signo __unused)
68 longjmp(timeout_alarm, 1);
72 send_remote_command(int fd, const char* fmt, ...)
81 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
83 if (s == sizeof(cmd) - 2 || s < 0)
84 errx(1, "Internal error: oversized command string");
85 /* We *know* there are at least two more bytes available */
89 if (((config->features & SECURETRANS) != 0) &&
90 ((config->features & NOSSL) == 0)) {
91 while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) {
92 s = SSL_get_error(config->ssl, s);
93 if (s != SSL_ERROR_WANT_READ &&
94 s != SSL_ERROR_WANT_WRITE)
101 n = write(fd, cmd + pos, len - pos);
112 read_remote(int fd, int extbufsize, char *extbuf)
117 int done = 0, status = 0, extbufpos = 0;
119 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
120 snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
124 if (setjmp(timeout_alarm) != 0) {
125 snprintf(neterr, sizeof(neterr), "Timeout reached");
131 * Remote reading code from femail.c written by Henning Brauer of
132 * OpenBSD and released under a BSD style license.
134 for (len = pos = 0; !done; ) {
137 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
138 memmove(buff, buff + pos, len - pos);
141 if (((config->features & SECURETRANS) != 0) &&
142 (config->features & NOSSL) == 0) {
143 if ((rlen = SSL_read(config->ssl, buff + len,
144 sizeof(buff) - len)) == -1)
147 if ((rlen = read(fd, buff + len,
148 sizeof(buff) - len)) == -1)
154 * If there is an external buffer with a size bigger than zero
155 * and as long as there is space in the external buffer and
156 * there are new characters read from the mailserver
157 * copy them to the external buffer
159 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
161 /* do not write over the bounds of the buffer */
162 if(extbufpos + rlen > (extbufsize - 1)) {
163 rlen = extbufsize - extbufpos;
165 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
168 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
174 if (buff[pos] == ' ')
176 else if (buff[pos] != '-')
177 syslog(LOG_ERR, "invalid syntax in reply from server");
180 for (; pos < len && buff[pos - 1] != '\n'; pos++)
187 while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
189 snprintf(neterr, sizeof(neterr), "%s", buff);
195 * Handle SMTP authentication
198 smtp_login(struct qitem *it, int fd, char *login, char* password)
204 res = smtp_auth_md5(it, fd, login, password);
207 } else if (res == -2) {
209 * If the return code is -2, then then the login attempt failed,
210 * do not try other login mechanisms
214 #endif /* HAVE_CRYPTO */
216 if ((config->features & INSECURE) != 0) {
217 /* Send AUTH command according to RFC 2554 */
218 send_remote_command(fd, "AUTH LOGIN");
219 if (read_remote(fd, 0, NULL) != 3) {
220 syslog(LOG_ERR, "%s: remote delivery deferred:"
221 " AUTH login not available: %s",
222 it->queueid, neterr);
226 len = base64_encode(login, strlen(login), &temp);
230 send_remote_command(fd, "%s", temp);
231 if (read_remote(fd, 0, NULL) != 3) {
232 syslog(LOG_ERR, "%s: remote delivery deferred:"
233 " AUTH login failed: %s", it->queueid,
238 len = base64_encode(password, strlen(password), &temp);
242 send_remote_command(fd, "%s", temp);
243 res = read_remote(fd, 0, NULL);
245 syslog(LOG_ERR, "%s: remote delivery failed:"
246 " Authentication failed: %s",
247 it->queueid, neterr);
249 } else if (res != 2) {
250 syslog(LOG_ERR, "%s: remote delivery failed:"
251 " AUTH password failed: %s",
252 it->queueid, neterr);
256 syslog(LOG_ERR, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
265 open_connection(struct qitem *it, const char *host)
267 struct addrinfo hints, *res, *res0;
269 const char *errmsg = NULL;
270 int fd, error = 0, port;
272 if (config->port != 0)
277 /* FIXME get MX record of host */
278 /* Shamelessly taken from getaddrinfo(3) */
279 memset(&hints, 0, sizeof(hints));
280 hints.ai_family = PF_UNSPEC;
281 hints.ai_socktype = SOCK_STREAM;
282 hints.ai_protocol = IPPROTO_TCP;
284 snprintf(servname, sizeof(servname), "%d", port);
285 error = getaddrinfo(host, servname, &hints, &res0);
287 syslog(LOG_ERR, "%s: remote delivery deferred: "
288 "%s: %m", it->queueid, gai_strerror(error));
292 for (res = res0; res; res = res->ai_next) {
293 fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
295 errmsg = "socket failed";
298 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
299 errmsg = "connect failed";
307 syslog(LOG_ERR, "%s: remote delivery deferred: %s (%s:%s)",
308 it->queueid, errmsg, host, servname);
317 deliver_remote(struct qitem *it, const char **errmsg)
320 char *host, line[1000];
321 int fd, error = 0, do_auth = 0, res = 0;
324 host = strrchr(it->addr, '@');
325 /* Should not happen */
329 /* Step over the @ */
332 /* Smarthost support? */
333 if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
334 syslog(LOG_INFO, "%s: using smarthost (%s:%i)",
335 it->queueid, config->smarthost, config->port);
336 host = config->smarthost;
339 fd = open_connection(it, host);
343 /* Check first reply from remote host */
344 config->features |= NOSSL;
345 res = read_remote(fd, 0, NULL);
347 syslog(LOG_INFO, "%s: Invalid initial response: %i",
351 config->features &= ~NOSSL;
354 if ((config->features & SECURETRANS) != 0) {
355 error = smtp_init_crypto(it, fd, config->features);
357 syslog(LOG_INFO, "%s: SSL initialization successful",
364 * If the user doesn't want STARTTLS, but SSL encryption, we
365 * have to enable SSL first, then send EHLO
367 if (((config->features & STARTTLS) == 0) &&
368 ((config->features & SECURETRANS) != 0)) {
369 send_remote_command(fd, "EHLO %s", hostname());
370 if (read_remote(fd, 0, NULL) != 2) {
371 syslog(LOG_ERR, "%s: remote delivery deferred: "
372 " EHLO failed: %s", it->queueid, neterr);
376 #endif /* HAVE_CRYPTO */
377 if (((config->features & SECURETRANS) == 0)) {
378 send_remote_command(fd, "EHLO %s", hostname());
379 if (read_remote(fd, 0, NULL) != 2) {
380 syslog(LOG_ERR, "%s: remote delivery deferred: "
381 " EHLO failed: %s", it->queueid, neterr);
387 * Use SMTP authentication if the user defined an entry for the remote
390 SLIST_FOREACH(a, &authusers, next) {
391 if (strcmp(a->host, host) == 0) {
399 * Check if the user wants plain text login without using
402 syslog(LOG_INFO, "%s: Use SMTP authentication",
404 error = smtp_login(it, fd, a->login, a->password);
406 syslog(LOG_ERR, "%s: remote delivery failed:"
407 " SMTP login failed: %m", it->queueid);
410 /* SMTP login is not available, so try without */
412 syslog(LOG_ERR, "%s: SMTP login not available."
413 " Try without", it->queueid);
416 #define READ_REMOTE_CHECK(c, exp) \
417 res = read_remote(fd, 0, NULL); \
419 syslog(LOG_ERR, "%s: remote delivery failed: " \
420 c " failed: %s", it->queueid, neterr); \
422 } else if (res != exp) { \
423 syslog(LOG_ERR, "%s: remote delivery deferred: " \
424 c " failed: %s", it->queueid, neterr); \
428 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
429 READ_REMOTE_CHECK("MAIL FROM", 2);
431 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
432 READ_REMOTE_CHECK("RCPT TO", 2);
434 send_remote_command(fd, "DATA");
435 READ_REMOTE_CHECK("DATA", 3);
437 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
438 syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s",
439 it->queueid, neterr);
443 while (!feof(it->queuef)) {
444 if (fgets(line, sizeof(line), it->queuef) == NULL)
446 linelen = strlen(line);
447 if (linelen == 0 || line[linelen - 1] != '\n') {
448 syslog(LOG_CRIT, "%s: remote delivery failed:"
449 "corrupted queue file", it->queueid);
450 *errmsg = "corrupted queue file";
455 /* Remove trailing \n's and escape leading dots */
459 * If the first character is a dot, we escape it so the line
465 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
466 syslog(LOG_ERR, "%s: remote delivery deferred: "
467 "write error", it->queueid);
473 send_remote_command(fd, ".");
474 READ_REMOTE_CHECK("final DATA", 2);
476 send_remote_command(fd, "QUIT");
477 if (read_remote(fd, 0, NULL) != 2)
478 syslog(LOG_WARNING, "%s: remote delivery succeeded but "
479 "QUIT failed: %s", it->queueid, neterr);