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
36 #include <sys/param.h>
37 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
44 #include <openssl/ssl.h>
45 #include <openssl/err.h>
57 static jmp_buf timeout_alarm;
58 char neterr[BUF_SIZE];
66 while ((nerr = ERR_get_error()) != 0)
69 return (ERR_error_string(oerr, NULL));
75 (void)signo; /* so that gcc doesn't complain */
76 longjmp(timeout_alarm, 1);
80 send_remote_command(int fd, const char* fmt, ...)
89 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
91 if (s == sizeof(cmd) - 2 || s < 0) {
92 strcpy(neterr, "Internal error: oversized command string");
96 /* We *know* there are at least two more bytes available */
100 if (((config.features & SECURETRANS) != 0) &&
101 ((config.features & NOSSL) == 0)) {
102 while ((s = SSL_write(config.ssl, (const char*)cmd, len)) <= 0) {
103 s = SSL_get_error(config.ssl, s);
104 if (s != SSL_ERROR_WANT_READ &&
105 s != SSL_ERROR_WANT_WRITE) {
106 strncpy(neterr, ssl_errstr(), sizeof(neterr));
114 n = write(fd, cmd + pos, len - pos);
125 read_remote(int fd, int extbufsize, char *extbuf)
130 int done = 0, status = 0, extbufpos = 0;
132 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
133 snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
137 if (setjmp(timeout_alarm) != 0) {
138 snprintf(neterr, sizeof(neterr), "Timeout reached");
144 * Remote reading code from femail.c written by Henning Brauer of
145 * OpenBSD and released under a BSD style license.
147 for (len = pos = 0; !done; ) {
150 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
151 memmove(buff, buff + pos, len - pos);
154 if (((config.features & SECURETRANS) != 0) &&
155 (config.features & NOSSL) == 0) {
156 if ((rlen = SSL_read(config.ssl, buff + len,
157 sizeof(buff) - len)) == -1) {
158 strncpy(neterr, ssl_errstr(), sizeof(neterr));
162 if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) {
163 strncpy(neterr, strerror(errno), sizeof(neterr));
170 * If there is an external buffer with a size bigger than zero
171 * and as long as there is space in the external buffer and
172 * there are new characters read from the mailserver
173 * copy them to the external buffer
175 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
177 /* do not write over the bounds of the buffer */
178 if(extbufpos + rlen > (extbufsize - 1)) {
179 rlen = extbufsize - extbufpos;
181 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
184 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
190 if (buff[pos] == ' ') {
192 } else if (buff[pos] != '-') {
193 strcpy(neterr, "invalid syntax in reply from server");
198 for (; pos < len && buff[pos - 1] != '\n'; pos++)
205 while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
207 snprintf(neterr, sizeof(neterr), "%s", buff);
213 * Handle SMTP authentication
216 smtp_login(int fd, char *login, char* password)
221 res = smtp_auth_md5(fd, login, password);
224 } else if (res == -2) {
226 * If the return code is -2, then then the login attempt failed,
227 * do not try other login mechanisms
232 if ((config.features & INSECURE) != 0 ||
233 (config.features & SECURETRANS) != 0) {
234 /* Send AUTH command according to RFC 2554 */
235 send_remote_command(fd, "AUTH LOGIN");
236 if (read_remote(fd, 0, NULL) != 3) {
237 syslog(LOG_NOTICE, "remote delivery deferred:"
238 " AUTH login not available: %s",
243 len = base64_encode(login, strlen(login), &temp);
246 syslog(LOG_ERR, "can not encode auth reply: %m");
250 send_remote_command(fd, "%s", temp);
252 res = read_remote(fd, 0, NULL);
254 syslog(LOG_NOTICE, "remote delivery %s: AUTH login failed: %s",
255 res == 5 ? "failed" : "deferred", neterr);
256 return (res == 5 ? -1 : 1);
259 len = base64_encode(password, strlen(password), &temp);
263 send_remote_command(fd, "%s", temp);
265 res = read_remote(fd, 0, NULL);
267 syslog(LOG_NOTICE, "remote delivery %s: Authentication failed: %s",
268 res == 5 ? "failed" : "deferred", neterr);
269 return (res == 5 ? -1 : 1);
272 syslog(LOG_WARNING, "non-encrypted SMTP login is disabled in config, so skipping it. ");
280 open_connection(struct mx_hostentry *h)
284 syslog(LOG_INFO, "trying remote delivery to %s [%s] pref %d",
285 h->host, h->addr, h->pref);
287 fd = socket(h->ai.ai_family, h->ai.ai_socktype, h->ai.ai_protocol);
289 syslog(LOG_INFO, "socket for %s [%s] failed: %m",
294 if (connect(fd, (struct sockaddr *)&h->sa, h->ai.ai_addrlen) < 0) {
295 syslog(LOG_INFO, "connect to %s [%s] failed: %m",
305 close_connection(int fd)
307 if (config.ssl != NULL) {
308 if (((config.features & SECURETRANS) != 0) &&
309 ((config.features & NOSSL) == 0))
310 SSL_shutdown(config.ssl);
312 SSL_free(config.ssl);
319 deliver_to_host(struct qitem *it, struct mx_hostentry *host, void *errmsgc)
324 int fd, error = 0, do_auth = 0, res = 0;
326 if (fseek(it->mailf, 0, SEEK_SET) != 0) {
327 asprintf(errmsgc, "can not seek: %s", strerror(errno));
331 fd = open_connection(host);
335 #define READ_REMOTE_CHECK(c, exp) \
336 res = read_remote(fd, 0, NULL); \
338 syslog(LOG_ERR, "remote delivery to %s [%s] failed after %s: %s", \
339 host->host, host->addr, c, neterr); \
340 asprintf(errmsgc, "%s [%s] did not like our %s:\n%s", \
341 host->host, host->addr, c, neterr); \
343 } else if (res != exp) { \
344 syslog(LOG_NOTICE, "remote delivery deferred: %s [%s] failed after %s: %s", \
345 host->host, host->addr, c, neterr); \
349 /* Check first reply from remote host */
350 config.features |= NOSSL;
351 READ_REMOTE_CHECK("connect", 2);
353 config.features &= ~NOSSL;
355 if ((config.features & SECURETRANS) != 0) {
356 error = smtp_init_crypto(fd, config.features);
358 syslog(LOG_DEBUG, "SSL initialization successful");
363 /* XXX allow HELO fallback */
364 /* XXX record ESMTP keywords */
365 send_remote_command(fd, "EHLO %s", hostname());
366 READ_REMOTE_CHECK("EHLO", 2);
369 * Use SMTP authentication if the user defined an entry for the remote
372 SLIST_FOREACH(a, &authusers, next) {
373 if (strcmp(a->host, host->host) == 0) {
381 * Check if the user wants plain text login without using
384 syslog(LOG_INFO, "using SMTP authentication for user %s", a->login);
385 error = smtp_login(fd, a->login, a->password);
387 syslog(LOG_ERR, "remote delivery failed:"
388 " SMTP login failed: %m");
389 asprintf(errmsgc, "SMTP login to %s failed", host->host);
392 /* SMTP login is not available, so try without */
393 else if (error > 0) {
394 syslog(LOG_WARNING, "SMTP login not available. Trying without.");
398 /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */
399 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
400 READ_REMOTE_CHECK("MAIL FROM", 2);
402 /* XXX send ESMTP ORCPT */
403 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
404 READ_REMOTE_CHECK("RCPT TO", 2);
406 send_remote_command(fd, "DATA");
407 READ_REMOTE_CHECK("DATA", 3);
410 while (!feof(it->mailf)) {
411 if (fgets(line, sizeof(line), it->mailf) == NULL)
413 linelen = strlen(line);
414 if (linelen == 0 || line[linelen - 1] != '\n') {
415 syslog(LOG_CRIT, "remote delivery failed: corrupted queue file");
416 *(const char **)errmsgc = "corrupted queue file";
421 /* Remove trailing \n's and escape leading dots */
425 * If the first character is a dot, we escape it so the line
431 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
432 syslog(LOG_NOTICE, "remote delivery deferred: write error");
438 send_remote_command(fd, ".");
439 READ_REMOTE_CHECK("final DATA", 2);
441 send_remote_command(fd, "QUIT");
442 if (read_remote(fd, 0, NULL) != 2)
443 syslog(LOG_INFO, "remote delivery succeeded but QUIT failed: %s", neterr);
446 close_connection(fd);
451 deliver_remote(struct qitem *it, const char **errmsg)
453 /* asprintf can't take const */
454 void *errmsgc = __DECONST(char **, errmsg);
455 struct mx_hostentry *hosts, *h;
458 int error = 1, smarthost = 0;
460 host = strrchr(it->addr, '@');
461 /* Should not happen */
463 asprintf(errmsgc, "Internal error: badly formed address %s",
467 /* Step over the @ */
473 /* Smarthost support? */
474 if (config.smarthost != NULL) {
475 host = config.smarthost;
477 syslog(LOG_INFO, "using smarthost (%s:%i)", host, port);
481 error = dns_get_mx_list(host, port, &hosts, smarthost);
483 syslog(LOG_NOTICE, "remote delivery %s: DNS failure (%s)",
484 error < 0 ? "failed" : "deferred",
489 for (h = hosts; *h->host != 0; h++) {
490 switch (deliver_to_host(it, h, errmsgc)) {