2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
40 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
47 #include <openssl/ssl.h>
48 #include <openssl/err.h>
62 char neterr[ERRMSG_SIZE];
70 while ((nerr = ERR_get_error()) != 0)
73 return (ERR_error_string(oerr, NULL));
77 send_remote_command(int fd, const char* fmt, ...)
86 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
88 if (s == sizeof(cmd) - 2 || s < 0) {
89 strcpy(neterr, "Internal error: oversized command string");
93 /* We *know* there are at least two more bytes available */
97 if (((config.features & SECURETRANS) != 0) &&
98 ((config.features & NOSSL) == 0)) {
99 while ((s = SSL_write(config.ssl, (const char*)cmd, len)) <= 0) {
100 s = SSL_get_error(config.ssl, s);
101 if (s != SSL_ERROR_WANT_READ &&
102 s != SSL_ERROR_WANT_WRITE) {
103 strncpy(neterr, ssl_errstr(), sizeof(neterr));
111 n = write(fd, cmd + pos, len - pos);
122 read_remote(int fd, int extbufsize, char *extbuf)
125 size_t pos, len, copysize;
127 int done = 0, status = 0, status_running = 0, extbufpos = 0;
128 enum { parse_status, parse_spacedash, parse_rest } parsestate;
130 if (do_timeout(CON_TIMEOUT, 1) != 0) {
131 snprintf(neterr, sizeof(neterr), "Timeout reached");
136 * Remote reading code from femail.c written by Henning Brauer of
137 * OpenBSD and released under a BSD style license.
141 parsestate = parse_status;
143 while (!(done && parsestate == parse_status)) {
146 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
147 memmove(buff, buff + pos, len - pos);
150 if (((config.features & SECURETRANS) != 0) &&
151 (config.features & NOSSL) == 0) {
152 if ((rlen = SSL_read(config.ssl, buff + len, sizeof(buff) - len)) == -1) {
153 strncpy(neterr, ssl_errstr(), sizeof(neterr));
157 if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) {
158 strncpy(neterr, strerror(errno), sizeof(neterr));
164 copysize = sizeof(neterr) - strlen(neterr) - 1;
167 strncat(neterr, buff, copysize);
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 > 0 && extbufsize > 0 && extbuf != NULL) {
176 /* do not write over the bounds of the buffer */
177 if(extbufpos + rlen > (extbufsize - 1)) {
178 rlen = extbufsize - extbufpos;
180 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
187 switch (parsestate) {
189 for (; pos < len; pos++) {
190 if (isdigit(buff[pos])) {
191 status_running = status_running * 10 + (buff[pos] - '0');
193 status = status_running;
195 parsestate = parse_spacedash;
201 case parse_spacedash:
209 /* XXX read capabilities */
213 strcpy(neterr, "invalid syntax in reply from server");
218 parsestate = parse_rest;
223 for (; pos < len; pos++) {
224 if (buff[pos] == '\n') {
226 parsestate = parse_status;
236 /* chop off trailing newlines */
237 while (neterr[0] != 0 && strchr("\r\n", neterr[strlen(neterr) - 1]) != 0)
238 neterr[strlen(neterr) - 1] = 0;
248 * Handle SMTP authentication
251 smtp_login(int fd, char *login, char* password)
256 res = smtp_auth_md5(fd, login, password);
259 } else if (res == -2) {
261 * If the return code is -2, then then the login attempt failed,
262 * do not try other login mechanisms
267 if ((config.features & INSECURE) != 0 ||
268 (config.features & SECURETRANS) != 0) {
269 /* Send AUTH command according to RFC 2554 */
270 send_remote_command(fd, "AUTH LOGIN");
271 if (read_remote(fd, 0, NULL) != 3) {
272 syslog(LOG_NOTICE, "remote delivery deferred:"
273 " AUTH login not available: %s",
278 len = base64_encode(login, strlen(login), &temp);
281 syslog(LOG_ERR, "can not encode auth reply: %m");
285 send_remote_command(fd, "%s", temp);
287 res = read_remote(fd, 0, NULL);
289 syslog(LOG_NOTICE, "remote delivery %s: AUTH login failed: %s",
290 res == 5 ? "failed" : "deferred", neterr);
291 return (res == 5 ? -1 : 1);
294 len = base64_encode(password, strlen(password), &temp);
298 send_remote_command(fd, "%s", temp);
300 res = read_remote(fd, 0, NULL);
302 syslog(LOG_NOTICE, "remote delivery %s: Authentication failed: %s",
303 res == 5 ? "failed" : "deferred", neterr);
304 return (res == 5 ? -1 : 1);
307 syslog(LOG_WARNING, "non-encrypted SMTP login is disabled in config, so skipping it. ");
315 open_connection(struct mx_hostentry *h)
319 syslog(LOG_INFO, "trying remote delivery to %s [%s] pref %d",
320 h->host, h->addr, h->pref);
322 fd = socket(h->ai.ai_family, h->ai.ai_socktype, h->ai.ai_protocol);
324 syslog(LOG_INFO, "socket for %s [%s] failed: %m",
329 if (connect(fd, (struct sockaddr *)&h->sa, h->ai.ai_addrlen) < 0) {
330 syslog(LOG_INFO, "connect to %s [%s] failed: %m",
340 close_connection(int fd)
342 if (config.ssl != NULL) {
343 if (((config.features & SECURETRANS) != 0) &&
344 ((config.features & NOSSL) == 0))
345 SSL_shutdown(config.ssl);
346 SSL_free(config.ssl);
353 deliver_to_host(struct qitem *it, struct mx_hostentry *host)
358 int fd, error = 0, do_auth = 0, res = 0;
360 if (fseek(it->mailf, 0, SEEK_SET) != 0) {
361 snprintf(errmsg, sizeof(errmsg), "can not seek: %s", strerror(errno));
365 fd = open_connection(host);
369 #define READ_REMOTE_CHECK(c, exp) \
370 res = read_remote(fd, 0, NULL); \
372 syslog(LOG_ERR, "remote delivery to %s [%s] failed after %s: %s", \
373 host->host, host->addr, c, neterr); \
374 snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s", \
375 host->host, host->addr, c, neterr); \
378 } else if (res != exp) { \
379 syslog(LOG_NOTICE, "remote delivery deferred: %s [%s] failed after %s: %s", \
380 host->host, host->addr, c, neterr); \
385 /* Check first reply from remote host */
386 if ((config.features & SECURETRANS) == 0 ||
387 (config.features & STARTTLS) != 0) {
388 config.features |= NOSSL;
389 READ_REMOTE_CHECK("connect", 2);
391 config.features &= ~NOSSL;
394 if ((config.features & SECURETRANS) != 0) {
395 error = smtp_init_crypto(fd, config.features);
397 syslog(LOG_DEBUG, "SSL initialization successful");
401 if ((config.features & STARTTLS) == 0)
402 READ_REMOTE_CHECK("connect", 2);
405 /* XXX allow HELO fallback */
406 /* XXX record ESMTP keywords */
407 send_remote_command(fd, "EHLO %s", hostname());
408 READ_REMOTE_CHECK("EHLO", 2);
411 * Use SMTP authentication if the user defined an entry for the remote
414 SLIST_FOREACH(a, &authusers, next) {
415 if (strcmp(a->host, host->host) == 0) {
423 * Check if the user wants plain text login without using
426 syslog(LOG_INFO, "using SMTP authentication for user %s", a->login);
427 error = smtp_login(fd, a->login, a->password);
429 syslog(LOG_ERR, "remote delivery failed:"
430 " SMTP login failed: %m");
431 snprintf(errmsg, sizeof(errmsg), "SMTP login to %s failed", host->host);
435 /* SMTP login is not available, so try without */
436 else if (error > 0) {
437 syslog(LOG_WARNING, "SMTP login not available. Trying without.");
441 /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */
442 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
443 READ_REMOTE_CHECK("MAIL FROM", 2);
445 /* XXX send ESMTP ORCPT */
446 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
447 READ_REMOTE_CHECK("RCPT TO", 2);
449 send_remote_command(fd, "DATA");
450 READ_REMOTE_CHECK("DATA", 3);
453 while (!feof(it->mailf)) {
454 if (fgets(line, sizeof(line), it->mailf) == NULL)
456 linelen = strlen(line);
457 if (linelen == 0 || line[linelen - 1] != '\n') {
458 syslog(LOG_CRIT, "remote delivery failed: corrupted queue file");
459 snprintf(errmsg, sizeof(errmsg), "corrupted queue file");
464 /* Remove trailing \n's and escape leading dots */
468 * If the first character is a dot, we escape it so the line
474 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
475 syslog(LOG_NOTICE, "remote delivery deferred: write error");
481 send_remote_command(fd, ".");
482 READ_REMOTE_CHECK("final DATA", 2);
484 send_remote_command(fd, "QUIT");
485 if (read_remote(fd, 0, NULL) != 2)
486 syslog(LOG_INFO, "remote delivery succeeded but QUIT failed: %s", neterr);
489 close_connection(fd);
494 deliver_remote(struct qitem *it)
496 struct mx_hostentry *hosts, *h;
499 int error = 1, smarthost = 0;
503 /* Smarthost support? */
504 if (config.smarthost != NULL) {
505 host = config.smarthost;
507 syslog(LOG_INFO, "using smarthost (%s:%i)", host, port);
510 host = strrchr(it->addr, '@');
511 /* Should not happen */
513 snprintf(errmsg, sizeof(errmsg), "Internal error: badly formed address %s",
517 /* Step over the @ */
522 error = dns_get_mx_list(host, port, &hosts, smarthost);
524 snprintf(errmsg, sizeof(errmsg), "DNS lookup failure: host %s not found", host);
525 syslog(LOG_NOTICE, "remote delivery %s: DNS lookup failure: host %s not found",
526 error < 0 ? "failed" : "deferred",
531 for (h = hosts; *h->host != 0; h++) {
532 switch (deliver_to_host(it, h)) {