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.8 2008/09/16 17:57:23 matthias 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 */
58 extern struct config *config;
59 extern struct authusers authusers;
60 static jmp_buf timeout_alarm;
63 sig_alarm(int signo __unused)
65 longjmp(timeout_alarm, 1);
69 send_remote_command(int fd, const char* fmt, ...)
76 vsprintf(cmd, fmt, va);
78 if (((config->features & SECURETRANS) != 0) &&
79 ((config->features & NOSSL) == 0)) {
80 len = SSL_write(config->ssl, (const char*)cmd, strlen(cmd));
81 SSL_write(config->ssl, "\r\n", 2);
84 len = write(fd, cmd, strlen(cmd));
93 read_remote(int fd, int extbufsize, char *extbuf)
98 int done = 0, status = 0, extbufpos = 0;
100 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
101 syslog(LOG_ERR, "SIGALRM error: %m");
103 if (setjmp(timeout_alarm) != 0) {
104 syslog(LOG_ERR, "Timeout reached");
110 * Remote reading code from femail.c written by Henning Brauer of
111 * OpenBSD and released under a BSD style license.
113 for (len = pos = 0; !done; ) {
116 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
117 memmove(buff, buff + pos, len - pos);
120 if (((config->features & SECURETRANS) != 0) &&
121 (config->features & NOSSL) == 0) {
122 if ((rlen = SSL_read(config->ssl, buff + len,
123 sizeof(buff) - len)) == -1)
126 if ((rlen = read(fd, buff + len,
127 sizeof(buff) - len)) == -1)
133 * If there is an external buffer with a size bigger than zero
134 * and as long as there is space in the external buffer and
135 * there are new characters read from the mailserver
136 * copy them to the external buffer
138 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
140 /* do not write over the bounds of the buffer */
141 if(extbufpos + rlen > (extbufsize - 1)) {
142 rlen = extbufsize - extbufpos;
144 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
147 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
153 if (buff[pos] == ' ')
155 else if (buff[pos] != '-')
156 syslog(LOG_ERR, "invalid syntax in reply from server");
159 for (; pos < len && buff[pos - 1] != '\n'; pos++)
170 * Handle SMTP authentication
173 smtp_login(struct qitem *it, int fd, char *login, char* password)
179 res = smtp_auth_md5(it, fd, login, password);
182 } else if (res == -2) {
184 * If the return code is -2, then then the login attempt failed,
185 * do not try other login mechanisms
189 #endif /* HAVE_CRYPTO */
191 if ((config->features & INSECURE) != 0) {
192 /* Send AUTH command according to RFC 2554 */
193 send_remote_command(fd, "AUTH LOGIN");
194 if (read_remote(fd, 0, NULL) != 3) {
195 syslog(LOG_ERR, "%s: remote delivery deferred:"
196 " AUTH login not available: %m", it->queueid);
200 len = base64_encode(login, strlen(login), &temp);
204 send_remote_command(fd, "%s", temp);
205 if (read_remote(fd, 0, NULL) != 3) {
206 syslog(LOG_ERR, "%s: remote delivery deferred:"
207 " AUTH login failed: %m", it->queueid);
211 len = base64_encode(password, strlen(password), &temp);
215 send_remote_command(fd, "%s", temp);
216 res = read_remote(fd, 0, NULL);
218 syslog(LOG_ERR, "%s: remote delivery failed:"
219 " Authentication failed: %m", it->queueid);
221 } else if (res != 2) {
222 syslog(LOG_ERR, "%s: remote delivery failed:"
223 " AUTH password failed: %m", it->queueid);
227 syslog(LOG_ERR, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
236 open_connection(struct qitem *it, const char *host)
238 struct addrinfo hints, *res, *res0;
240 const char *errmsg = NULL;
241 int fd, error = 0, port;
243 if (config->port != 0)
248 /* FIXME get MX record of host */
249 /* Shamelessly taken from getaddrinfo(3) */
250 memset(&hints, 0, sizeof(hints));
251 hints.ai_family = PF_UNSPEC;
252 hints.ai_socktype = SOCK_STREAM;
253 hints.ai_protocol = IPPROTO_TCP;
255 snprintf(servname, sizeof(servname), "%d", port);
256 error = getaddrinfo(host, servname, &hints, &res0);
258 syslog(LOG_ERR, "%s: remote delivery deferred: "
259 "%s: %m", it->queueid, gai_strerror(error));
263 for (res = res0; res; res = res->ai_next) {
264 fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
266 errmsg = "socket failed";
269 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
270 errmsg = "connect failed";
278 syslog(LOG_ERR, "%s: remote delivery deferred: %s (%s:%s)",
279 it->queueid, errmsg, host, servname);
288 copy_qitem(struct qitem *target, struct qitem *it) {
289 target->sender = it->sender;
290 target->addr = it->addr;
291 target->pipeuser = it->pipeuser;
292 target->queuefn = it->queuefn;
293 target->queueid = it->queueid;
294 target->queuef = it->queuef;
295 target->hdrlen = it->hdrlen;
300 deliver_remote(struct qitem *it, const char **errmsg, struct queue **queue)
303 char *host, line[1000];
304 int fd, error = 0, do_auth = 0, res = 0;
307 host = strrchr(it->addr, '@');
308 /* Should not happen */
312 /* Step over the @ */
315 /* Smarthost support? */
316 if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
317 syslog(LOG_INFO, "%s: using smarthost (%s)",
318 it->queueid, config->smarthost);
319 host = config->smarthost;
322 fd = open_connection(it, host);
326 /* Check first reply from remote host */
327 config->features |= NOSSL;
328 res = read_remote(fd, 0, NULL);
330 syslog(LOG_INFO, "%s: Invalid initial response: %i",
334 config->features &= ~NOSSL;
337 if ((config->features & SECURETRANS) != 0) {
338 error = smtp_init_crypto(it, fd, config->features);
340 syslog(LOG_INFO, "%s: SSL initialization successful",
347 * If the user doesn't want STARTTLS, but SSL encryption, we
348 * have to enable SSL first, then send EHLO
350 if (((config->features & STARTTLS) == 0) &&
351 ((config->features & SECURETRANS) != 0)) {
352 send_remote_command(fd, "EHLO %s", hostname());
353 if (read_remote(fd, 0, NULL) != 2) {
354 syslog(LOG_ERR, "%s: remote delivery deferred: "
355 " EHLO failed: %m", it->queueid);
359 #endif /* HAVE_CRYPTO */
360 if (((config->features & SECURETRANS) == 0)) {
361 send_remote_command(fd, "EHLO %s", hostname());
362 if (read_remote(fd, 0, NULL) != 2) {
363 syslog(LOG_ERR, "%s: remote delivery deferred: "
364 " EHLO failed: %m", it->queueid);
370 * Use SMTP authentication if the user defined an entry for the remote
373 SLIST_FOREACH(a, &authusers, next) {
374 if (strcmp(a->host, host) == 0) {
382 * Check if the user wants plain text login without using
385 syslog(LOG_INFO, "%s: Use SMTP authentication",
387 error = smtp_login(it, fd, a->login, a->password);
389 syslog(LOG_ERR, "%s: remote delivery failed:"
390 " SMTP login failed: %m", it->queueid);
393 /* SMTP login is not available, so try without */
395 syslog(LOG_ERR, "%s: SMTP login not available."
396 " Try without", it->queueid);
399 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
400 if (read_remote(fd, 0, NULL) != 2) {
401 syslog(LOG_ERR, "%s: remote delivery deferred:"
402 " MAIL FROM failed: %m", it->queueid);
407 /* without given queue send only to one receipient */
408 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
409 if (read_remote(fd, 0, NULL) != 2) {
410 syslog(LOG_ERR, "%s: remote delivery deferred:"
411 " RCPT TO failed: %m", it->queueid);
415 /* Iterate over all recepients and open only one connection */
418 * -delivered addresses
421 * these 3 queues are given in **queues (+the first queue with all the qitems to send)
426 int rcpt_success = 0;
428 LIST_FOREACH(tit, &queue[0]->queue, next) {
430 qit = calloc(1, sizeof(struct qitem));
431 copy_qitem(qit, tit);
433 if (stat(tit->queuefn, &st) != 0) {
434 syslog(LOG_ERR, "%s: lost queue file `%s'",
435 tit->queueid, tit->queuefn);
436 /* drop qitem and mark it as successfully sent */
437 LIST_INSERT_HEAD(&queue[1]->queue, qit, next);
441 send_remote_command(fd, "RCPT TO:<%s>", tit->addr);
442 switch (read_remote(fd, 0, NULL)) {
443 case 2: /* everythings fine, receipient accepted */
444 /* add item to temporary queue, these items will be deleted in deliver_smarthost */
446 LIST_INSERT_HEAD(&queue[1]->queue, qit, next);
447 syslog(LOG_INFO, "%s: mail from=<%s> to=<%s>",
448 tit->queueid, tit->sender, tit->addr);
450 case 4: /* temporary error, try again later */
451 /* add item to a temporary queue, these items will be tried again */
452 LIST_INSERT_HEAD(&queue[2]->queue, qit, next);
453 syslog(LOG_INFO, "%s: mail from=<%s> to=<%s>",
454 tit->queueid, tit->sender, tit->addr);
455 syslog(LOG_ERR, "%s: remote delivery deferred:"
456 " RCPT TO failed: %m", tit->queueid);
458 case 5: /* permanent error, bounce */
459 /* add item to a queue, which will be returned to deliver_smarthost */
460 LIST_INSERT_HEAD(&queue[3]->queue, qit, next);
461 syslog(LOG_INFO, "%s: mail from=<%s> to=<%s>",
462 tit->queueid, tit->sender, tit->addr);
463 syslog(LOG_ERR, "%s: remote delivery failed:"
464 " RCPT TO failed: %m", tit->queueid);
468 /* if there was no successful RCPT TO, return _WITHOUT_ error */
473 send_remote_command(fd, "DATA");
474 if (read_remote(fd, 0, NULL) != 3) {
475 syslog(LOG_ERR, "%s: remote delivery deferred:"
476 " DATA failed: %m", it->queueid);
480 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
481 syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %m",
486 while (!feof(it->queuef)) {
487 if (fgets(line, sizeof(line), it->queuef) == NULL)
489 linelen = strlen(line);
490 if (linelen == 0 || line[linelen - 1] != '\n') {
491 syslog(LOG_CRIT, "%s: remote delivery failed:"
492 "corrupted queue file", it->queueid);
493 *errmsg = "corrupted queue file";
498 /* Remove trailing \n's and escape leading dots */
502 * If the first character is a dot, we escape it so the line
508 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
509 syslog(LOG_ERR, "%s: remote delivery deferred: "
510 "write error", it->queueid);
516 send_remote_command(fd, ".");
517 if (read_remote(fd, 0, NULL) != 2) {
518 syslog(LOG_ERR, "%s: remote delivery deferred: %m",
523 send_remote_command(fd, "QUIT");
524 if (read_remote(fd, 0, NULL) != 2) {
525 syslog(LOG_ERR, "%s: remote delivery deferred: "
526 "QUIT failed: %m", it->queueid);