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>
58 extern struct config *config;
59 extern struct authusers authusers;
60 static jmp_buf timeout_alarm;
61 char neterr[BUF_SIZE];
64 sig_alarm(int signo __unused)
66 longjmp(timeout_alarm, 1);
70 send_remote_command(int fd, const char* fmt, ...)
79 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
81 if (s == sizeof(cmd) - 2 || s < 0)
82 errx(1, "Internal error: oversized command string");
83 /* We *know* there are at least two more bytes available */
87 if (((config->features & SECURETRANS) != 0) &&
88 ((config->features & NOSSL) == 0)) {
89 while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) {
90 s = SSL_get_error(config->ssl, s);
91 if (s != SSL_ERROR_WANT_READ &&
92 s != SSL_ERROR_WANT_WRITE)
99 n = write(fd, cmd + pos, len - pos);
110 read_remote(int fd, int extbufsize, char *extbuf)
115 int done = 0, status = 0, extbufpos = 0;
117 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
118 snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
122 if (setjmp(timeout_alarm) != 0) {
123 snprintf(neterr, sizeof(neterr), "Timeout reached");
129 * Remote reading code from femail.c written by Henning Brauer of
130 * OpenBSD and released under a BSD style license.
132 for (len = pos = 0; !done; ) {
135 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
136 memmove(buff, buff + pos, len - pos);
139 if (((config->features & SECURETRANS) != 0) &&
140 (config->features & NOSSL) == 0) {
141 if ((rlen = SSL_read(config->ssl, buff + len,
142 sizeof(buff) - len)) == -1)
145 if ((rlen = read(fd, buff + len,
146 sizeof(buff) - len)) == -1)
152 * If there is an external buffer with a size bigger than zero
153 * and as long as there is space in the external buffer and
154 * there are new characters read from the mailserver
155 * copy them to the external buffer
157 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
159 /* do not write over the bounds of the buffer */
160 if(extbufpos + rlen > (extbufsize - 1)) {
161 rlen = extbufsize - extbufpos;
163 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
166 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
172 if (buff[pos] == ' ')
174 else if (buff[pos] != '-')
175 syslog(LOG_ERR, "invalid syntax in reply from server");
178 for (; pos < len && buff[pos - 1] != '\n'; pos++)
185 while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
187 snprintf(neterr, sizeof(neterr), "%s", buff);
193 * Handle SMTP authentication
196 smtp_login(struct qitem *it, int fd, char *login, char* password)
201 res = smtp_auth_md5(it, fd, login, password);
204 } else if (res == -2) {
206 * If the return code is -2, then then the login attempt failed,
207 * do not try other login mechanisms
212 if ((config->features & INSECURE) != 0) {
213 /* Send AUTH command according to RFC 2554 */
214 send_remote_command(fd, "AUTH LOGIN");
215 if (read_remote(fd, 0, NULL) != 3) {
216 syslog(LOG_NOTICE, "%s: remote delivery deferred:"
217 " AUTH login not available: %s",
218 it->queueid, neterr);
222 len = base64_encode(login, strlen(login), &temp);
226 send_remote_command(fd, "%s", temp);
227 if (read_remote(fd, 0, NULL) != 3) {
228 syslog(LOG_NOTICE, "%s: remote delivery deferred:"
229 " AUTH login failed: %s", it->queueid,
234 len = base64_encode(password, strlen(password), &temp);
238 send_remote_command(fd, "%s", temp);
239 res = read_remote(fd, 0, NULL);
241 syslog(LOG_NOTICE, "%s: remote delivery failed:"
242 " Authentication failed: %s",
243 it->queueid, neterr);
245 } else if (res != 2) {
246 syslog(LOG_NOTICE, "%s: remote delivery failed:"
247 " AUTH password failed: %s",
248 it->queueid, neterr);
252 syslog(LOG_WARNING, "%s: non-encrypted SMTP login is disabled in config, so skipping it. ",
261 open_connection(struct qitem *it, const char *host)
263 struct addrinfo hints, *res, *res0;
265 const char *errmsg = NULL;
266 int fd, error = 0, port;
268 if (config->port != 0)
273 /* FIXME get MX record of host */
274 /* Shamelessly taken from getaddrinfo(3) */
275 memset(&hints, 0, sizeof(hints));
276 hints.ai_family = PF_UNSPEC;
277 hints.ai_socktype = SOCK_STREAM;
278 hints.ai_protocol = IPPROTO_TCP;
280 snprintf(servname, sizeof(servname), "%d", port);
281 error = getaddrinfo(host, servname, &hints, &res0);
283 syslog(LOG_NOTICE, "%s: remote delivery deferred: "
284 "%s: %m", it->queueid, gai_strerror(error));
288 for (res = res0; res; res = res->ai_next) {
289 fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
291 errmsg = "socket failed";
294 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
295 errmsg = "connect failed";
303 syslog(LOG_NOTICE, "%s: remote delivery deferred: %s (%s:%s)",
304 it->queueid, errmsg, host, servname);
313 deliver_remote(struct qitem *it, const char **errmsg)
316 char *host, line[1000];
317 int fd, error = 0, do_auth = 0, res = 0;
319 /* asprintf can't take const */
320 void *errmsgc = __DECONST(char **, errmsg);
322 host = strrchr(it->addr, '@');
323 /* Should not happen */
325 asprintf(errmsgc, "Internal error: badly formed address %s",
329 /* Step over the @ */
333 /* Smarthost support? */
334 if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
335 syslog(LOG_INFO, "%s: using smarthost (%s:%i)",
336 it->queueid, config->smarthost, config->port);
337 host = config->smarthost;
340 fd = open_connection(it, host);
344 /* Check first reply from remote host */
345 config->features |= NOSSL;
346 res = read_remote(fd, 0, NULL);
348 syslog(LOG_WARNING, "%s: Invalid initial response: %i",
352 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",
363 send_remote_command(fd, "EHLO %s", hostname());
364 if (read_remote(fd, 0, NULL) != 2) {
365 syslog(LOG_WARNING, "%s: remote delivery deferred: "
366 " EHLO failed: %s", it->queueid, neterr);
367 asprintf(errmsgc, "%s did not like our EHLO:\n%s",
373 * Use SMTP authentication if the user defined an entry for the remote
376 SLIST_FOREACH(a, &authusers, next) {
377 if (strcmp(a->host, host) == 0) {
385 * Check if the user wants plain text login without using
388 syslog(LOG_INFO, "%s: Use SMTP authentication",
390 error = smtp_login(it, fd, a->login, a->password);
392 syslog(LOG_ERR, "%s: remote delivery failed:"
393 " SMTP login failed: %m", it->queueid);
394 asprintf(errmsgc, "SMTP login to %s failed", host);
397 /* SMTP login is not available, so try without */
399 syslog(LOG_WARNING, "%s: SMTP login not available."
400 " Try without", it->queueid);
403 #define READ_REMOTE_CHECK(c, exp) \
404 res = read_remote(fd, 0, NULL); \
406 syslog(LOG_ERR, "%s: remote delivery failed: " \
407 c " failed: %s", it->queueid, neterr); \
408 asprintf(errmsgc, "%s did not like our " c ":\n%s", \
411 } else if (res != exp) { \
412 syslog(LOG_NOTICE, "%s: remote delivery deferred: " \
413 c " failed: %s", it->queueid, neterr); \
417 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
418 READ_REMOTE_CHECK("MAIL FROM", 2);
420 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
421 READ_REMOTE_CHECK("RCPT TO", 2);
423 send_remote_command(fd, "DATA");
424 READ_REMOTE_CHECK("DATA", 3);
426 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
427 syslog(LOG_ERR, "%s: remote delivery deferred: cannot seek: %s",
428 it->queueid, neterr);
433 while (!feof(it->queuef)) {
434 if (fgets(line, sizeof(line), it->queuef) == NULL)
436 linelen = strlen(line);
437 if (linelen == 0 || line[linelen - 1] != '\n') {
438 syslog(LOG_CRIT, "%s: remote delivery failed:"
439 "corrupted queue file", it->queueid);
440 *errmsg = "corrupted queue file";
445 /* Remove trailing \n's and escape leading dots */
449 * If the first character is a dot, we escape it so the line
455 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
456 syslog(LOG_NOTICE, "%s: remote delivery deferred: "
457 "write error", it->queueid);
463 send_remote_command(fd, ".");
464 READ_REMOTE_CHECK("final DATA", 2);
466 send_remote_command(fd, "QUIT");
467 if (read_remote(fd, 0, NULL) != 2)
468 syslog(LOG_INFO, "%s: remote delivery succeeded but "
469 "QUIT failed: %s", it->queueid, neterr);