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>
46 #include <openssl/ssl.h>
47 #include <openssl/err.h>
59 extern struct config *config;
60 extern struct authusers authusers;
61 static jmp_buf timeout_alarm;
62 char neterr[BUF_SIZE];
70 while ((nerr = ERR_get_error()) != 0)
73 return (ERR_error_string(oerr, NULL));
77 sig_alarm(int signo __unused)
79 longjmp(timeout_alarm, 1);
83 send_remote_command(int fd, const char* fmt, ...)
92 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
94 if (s == sizeof(cmd) - 2 || s < 0) {
95 strcpy(neterr, "Internal error: oversized command string");
99 /* We *know* there are at least two more bytes available */
103 if (((config->features & SECURETRANS) != 0) &&
104 ((config->features & NOSSL) == 0)) {
105 while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) {
106 s = SSL_get_error(config->ssl, s);
107 if (s != SSL_ERROR_WANT_READ &&
108 s != SSL_ERROR_WANT_WRITE) {
109 strncpy(neterr, ssl_errstr(), sizeof(neterr));
117 n = write(fd, cmd + pos, len - pos);
128 read_remote(int fd, int extbufsize, char *extbuf)
133 int done = 0, status = 0, extbufpos = 0;
135 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
136 snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
140 if (setjmp(timeout_alarm) != 0) {
141 snprintf(neterr, sizeof(neterr), "Timeout reached");
147 * Remote reading code from femail.c written by Henning Brauer of
148 * OpenBSD and released under a BSD style license.
150 for (len = pos = 0; !done; ) {
153 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
154 memmove(buff, buff + pos, len - pos);
157 if (((config->features & SECURETRANS) != 0) &&
158 (config->features & NOSSL) == 0) {
159 if ((rlen = SSL_read(config->ssl, buff + len,
160 sizeof(buff) - len)) == -1) {
161 strncpy(neterr, ssl_errstr(), sizeof(neterr));
165 if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) {
166 strncpy(neterr, strerror(errno), sizeof(neterr));
173 * If there is an external buffer with a size bigger than zero
174 * and as long as there is space in the external buffer and
175 * there are new characters read from the mailserver
176 * copy them to the external buffer
178 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
180 /* do not write over the bounds of the buffer */
181 if(extbufpos + rlen > (extbufsize - 1)) {
182 rlen = extbufsize - extbufpos;
184 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
187 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
193 if (buff[pos] == ' ') {
195 } else if (buff[pos] != '-') {
196 strcpy(neterr, "invalid syntax in reply from server");
201 for (; pos < len && buff[pos - 1] != '\n'; pos++)
208 while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
210 snprintf(neterr, sizeof(neterr), "%s", buff);
216 * Handle SMTP authentication
219 smtp_login(struct qitem *it, int fd, char *login, char* password)
224 res = smtp_auth_md5(it, fd, login, password);
227 } else if (res == -2) {
229 * If the return code is -2, then then the login attempt failed,
230 * do not try other login mechanisms
235 if ((config->features & INSECURE) != 0 ||
236 (config->features & SECURETRANS) != 0) {
237 /* Send AUTH command according to RFC 2554 */
238 send_remote_command(fd, "AUTH LOGIN");
239 if (read_remote(fd, 0, NULL) != 3) {
240 syslog(LOG_NOTICE, "%s: remote delivery deferred:"
241 " AUTH login not available: %s",
242 it->queueid, neterr);
246 len = base64_encode(login, strlen(login), &temp);
249 syslog(LOG_ERR, "%s: can not encode auth reply: %m",
254 send_remote_command(fd, "%s", temp);
256 res = read_remote(fd, 0, NULL);
258 syslog(LOG_NOTICE, "%s: remote delivery %s: AUTH login failed: %s",
259 it->queueid, res == 5 ? "failed" : "deferred", neterr);
260 return (res == 5 ? -1 : 1);
263 len = base64_encode(password, strlen(password), &temp);
267 send_remote_command(fd, "%s", temp);
269 res = read_remote(fd, 0, NULL);
271 syslog(LOG_NOTICE, "%s: remote delivery %s: Authentication failed: %s",
272 it->queueid, res == 5 ? "failed" : "deferred", neterr);
273 return (res == 5 ? -1 : 1);
276 syslog(LOG_WARNING, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
285 open_connection(struct qitem *it, const char *host)
287 struct addrinfo hints, *res, *res0;
289 const char *errmsg = NULL;
290 int fd, error = 0, port;
292 if (config->port != 0)
297 /* FIXME get MX record of host */
298 /* Shamelessly taken from getaddrinfo(3) */
299 memset(&hints, 0, sizeof(hints));
300 hints.ai_family = PF_UNSPEC;
301 hints.ai_socktype = SOCK_STREAM;
302 hints.ai_protocol = IPPROTO_TCP;
304 snprintf(servname, sizeof(servname), "%d", port);
305 error = getaddrinfo(host, servname, &hints, &res0);
307 syslog(LOG_NOTICE, "%s: remote delivery deferred: "
308 "%s: %m", it->queueid, gai_strerror(error));
312 for (res = res0; res; res = res->ai_next) {
313 fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
315 errmsg = "socket failed";
318 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
319 errmsg = "connect failed";
327 syslog(LOG_NOTICE, "%s: remote delivery deferred: %s (%s:%s)",
328 it->queueid, errmsg, host, servname);
337 close_connection(int fd)
339 if (((config->features & SECURETRANS) != 0) &&
340 ((config->features & NOSSL) == 0))
341 SSL_shutdown(config->ssl);
343 if (config->ssl != NULL)
344 SSL_free(config->ssl);
350 deliver_remote(struct qitem *it, const char **errmsg)
353 char *host, line[1000];
354 int fd, error = 0, do_auth = 0, res = 0;
356 /* asprintf can't take const */
357 void *errmsgc = __DECONST(char **, errmsg);
359 host = strrchr(it->addr, '@');
360 /* Should not happen */
362 asprintf(errmsgc, "Internal error: badly formed address %s",
366 /* Step over the @ */
370 /* Smarthost support? */
371 if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
372 syslog(LOG_INFO, "%s: using smarthost (%s:%i)",
373 it->queueid, config->smarthost, config->port);
374 host = config->smarthost;
377 fd = open_connection(it, host);
381 /* Check first reply from remote host */
382 config->features |= NOSSL;
383 res = read_remote(fd, 0, NULL);
385 syslog(LOG_WARNING, "%s: Invalid initial response: %i",
389 config->features &= ~NOSSL;
391 if ((config->features & SECURETRANS) != 0) {
392 error = smtp_init_crypto(it, fd, config->features);
394 syslog(LOG_DEBUG, "%s: SSL initialization successful",
400 send_remote_command(fd, "EHLO %s", hostname());
401 if (read_remote(fd, 0, NULL) != 2) {
402 syslog(LOG_WARNING, "%s: remote delivery deferred: "
403 " EHLO failed: %s", it->queueid, neterr);
404 asprintf(errmsgc, "%s did not like our EHLO:\n%s",
410 * Use SMTP authentication if the user defined an entry for the remote
413 SLIST_FOREACH(a, &authusers, next) {
414 if (strcmp(a->host, host) == 0) {
422 * Check if the user wants plain text login without using
425 syslog(LOG_INFO, "%s: Use SMTP authentication",
427 error = smtp_login(it, fd, a->login, a->password);
429 syslog(LOG_ERR, "%s: remote delivery failed:"
430 " SMTP login failed: %m", it->queueid);
431 asprintf(errmsgc, "SMTP login to %s failed", host);
434 /* SMTP login is not available, so try without */
436 syslog(LOG_WARNING, "%s: SMTP login not available."
437 " Try without", it->queueid);
440 #define READ_REMOTE_CHECK(c, exp) \
441 res = read_remote(fd, 0, NULL); \
443 syslog(LOG_ERR, "%s: remote delivery failed: " \
444 c " failed: %s", it->queueid, neterr); \
445 asprintf(errmsgc, "%s did not like our " c ":\n%s", \
448 } else if (res != exp) { \
449 syslog(LOG_NOTICE, "%s: remote delivery deferred: " \
450 c " failed: %s", it->queueid, neterr); \
454 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
455 READ_REMOTE_CHECK("MAIL FROM", 2);
457 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
458 READ_REMOTE_CHECK("RCPT TO", 2);
460 send_remote_command(fd, "DATA");
461 READ_REMOTE_CHECK("DATA", 3);
463 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
464 syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s",
465 it->queueid, neterr);
470 while (!feof(it->queuef)) {
471 if (fgets(line, sizeof(line), it->queuef) == NULL)
473 linelen = strlen(line);
474 if (linelen == 0 || line[linelen - 1] != '\n') {
475 syslog(LOG_CRIT, "%s: remote delivery failed:"
476 "corrupted queue file", it->queueid);
477 *errmsg = "corrupted queue file";
482 /* Remove trailing \n's and escape leading dots */
486 * If the first character is a dot, we escape it so the line
492 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
493 syslog(LOG_NOTICE, "%s: remote delivery deferred: "
494 "write error", it->queueid);
500 send_remote_command(fd, ".");
501 READ_REMOTE_CHECK("final DATA", 2);
503 send_remote_command(fd, "QUIT");
504 if (read_remote(fd, 0, NULL) != 2)
505 syslog(LOG_INFO, "%s: remote delivery succeeded but "
506 "QUIT failed: %s", it->queueid, neterr);
509 close_connection(fd);