dma: restructure logging
[dragonfly.git] / libexec / dma / net.c
CommitLineData
f67beddd
MS
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
6 * Germany.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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
17 * distribution.
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.
21 *
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
33 * SUCH DAMAGE.
34 *
bc7baf1d 35 * $DragonFly: src/libexec/dma/net.c,v 1.9 2008/09/30 17:47:21 swildner Exp $
f67beddd
MS
36 */
37
38#include <sys/param.h>
39#include <sys/queue.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <netinet/in.h>
44#include <arpa/inet.h>
45
f67beddd 46#include <openssl/ssl.h>
e2c88018 47#include <openssl/err.h>
f67beddd 48
bc7baf1d 49#include <err.h>
a5a8a1a4 50#include <errno.h>
f67beddd
MS
51#include <netdb.h>
52#include <setjmp.h>
53#include <signal.h>
54#include <syslog.h>
55#include <unistd.h>
56
57#include "dma.h"
58
f67beddd 59static jmp_buf timeout_alarm;
a5a8a1a4 60char neterr[BUF_SIZE];
f67beddd 61
e2c88018
SS
62char *
63ssl_errstr(void)
64{
65 long oerr, nerr;
66
67 oerr = 0;
68 while ((nerr = ERR_get_error()) != 0)
69 oerr = nerr;
70
71 return (ERR_error_string(oerr, NULL));
72}
73
f67beddd 74static void
dba19026 75sig_alarm(int signo __unused)
f67beddd
MS
76{
77 longjmp(timeout_alarm, 1);
78}
79
80ssize_t
81send_remote_command(int fd, const char* fmt, ...)
82{
83 va_list va;
84 char cmd[4096];
2922fd2b
SS
85 size_t len, pos;
86 int s;
87 ssize_t n;
f67beddd
MS
88
89 va_start(va, fmt);
2922fd2b
SS
90 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
91 va_end(va);
e2c88018
SS
92 if (s == sizeof(cmd) - 2 || s < 0) {
93 strcpy(neterr, "Internal error: oversized command string");
94 return (-1);
95 }
96
2922fd2b
SS
97 /* We *know* there are at least two more bytes available */
98 strcat(cmd, "\r\n");
99 len = strlen(cmd);
f67beddd
MS
100
101 if (((config->features & SECURETRANS) != 0) &&
6ef9fe01 102 ((config->features & NOSSL) == 0)) {
2922fd2b
SS
103 while ((s = SSL_write(config->ssl, (const char*)cmd, len)) <= 0) {
104 s = SSL_get_error(config->ssl, s);
105 if (s != SSL_ERROR_WANT_READ &&
e2c88018
SS
106 s != SSL_ERROR_WANT_WRITE) {
107 strncpy(neterr, ssl_errstr(), sizeof(neterr));
2922fd2b 108 return (-1);
e2c88018 109 }
2922fd2b 110 }
f67beddd
MS
111 }
112 else {
2922fd2b
SS
113 pos = 0;
114 while (pos < len) {
115 n = write(fd, cmd + pos, len - pos);
116 if (n < 0)
117 return (-1);
118 pos += n;
119 }
f67beddd 120 }
f67beddd 121
2922fd2b 122 return (len);
f67beddd
MS
123}
124
6ef9fe01 125int
7b68d8ae 126read_remote(int fd, int extbufsize, char *extbuf)
f67beddd 127{
6ef9fe01
MS
128 ssize_t rlen = 0;
129 size_t pos, len;
130 char buff[BUF_SIZE];
7b68d8ae 131 int done = 0, status = 0, extbufpos = 0;
f67beddd
MS
132
133 if (signal(SIGALRM, sig_alarm) == SIG_ERR) {
a5a8a1a4
SS
134 snprintf(neterr, sizeof(neterr), "SIGALRM error: %s",
135 strerror(errno));
e2c88018 136 return (-1);
f67beddd
MS
137 }
138 if (setjmp(timeout_alarm) != 0) {
a5a8a1a4 139 snprintf(neterr, sizeof(neterr), "Timeout reached");
e2c88018 140 return (-1);
f67beddd
MS
141 }
142 alarm(CON_TIMEOUT);
143
144 /*
6ef9fe01
MS
145 * Remote reading code from femail.c written by Henning Brauer of
146 * OpenBSD and released under a BSD style license.
f67beddd 147 */
6ef9fe01 148 for (len = pos = 0; !done; ) {
7b68d8ae 149 rlen = 0;
6ef9fe01
MS
150 if (pos == 0 ||
151 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
152 memmove(buff, buff + pos, len - pos);
153 len -= pos;
154 pos = 0;
155 if (((config->features & SECURETRANS) != 0) &&
156 (config->features & NOSSL) == 0) {
157 if ((rlen = SSL_read(config->ssl, buff + len,
e2c88018
SS
158 sizeof(buff) - len)) == -1) {
159 strncpy(neterr, ssl_errstr(), sizeof(neterr));
160 return (-1);
161 }
6ef9fe01 162 } else {
e2c88018
SS
163 if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) {
164 strncpy(neterr, strerror(errno), sizeof(neterr));
165 return (-1);
166 }
6ef9fe01
MS
167 }
168 len += rlen;
169 }
7b68d8ae
MS
170 /*
171 * If there is an external buffer with a size bigger than zero
172 * and as long as there is space in the external buffer and
173 * there are new characters read from the mailserver
174 * copy them to the external buffer
175 */
176 if (extbufpos <= (extbufsize - 1) && rlen && extbufsize > 0
177 && extbuf != NULL) {
178 /* do not write over the bounds of the buffer */
179 if(extbufpos + rlen > (extbufsize - 1)) {
180 rlen = extbufsize - extbufpos;
181 }
182 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
183 extbufpos += rlen;
184 }
6ef9fe01
MS
185 for (; pos < len && buff[pos] >= '0' && buff[pos] <= '9'; pos++)
186 ; /* Do nothing */
f67beddd 187
6ef9fe01
MS
188 if (pos == len)
189 return (0);
f67beddd 190
e2c88018 191 if (buff[pos] == ' ') {
6ef9fe01 192 done = 1;
e2c88018
SS
193 } else if (buff[pos] != '-') {
194 strcpy(neterr, "invalid syntax in reply from server");
195 return (-1);
196 }
f67beddd 197
6ef9fe01
MS
198 /* skip up to \n */
199 for (; pos < len && buff[pos - 1] != '\n'; pos++)
200 ; /* Do nothing */
f67beddd 201
f67beddd 202 }
6ef9fe01
MS
203 alarm(0);
204
a5a8a1a4
SS
205 buff[len] = '\0';
206 while (len > 0 && (buff[len - 1] == '\r' || buff[len - 1] == '\n'))
207 buff[--len] = '\0';
208 snprintf(neterr, sizeof(neterr), "%s", buff);
6ef9fe01
MS
209 status = atoi(buff);
210 return (status/100);
f67beddd
MS
211}
212
213/*
214 * Handle SMTP authentication
f67beddd
MS
215 */
216static int
405f48ee 217smtp_login(int fd, char *login, char* password)
f67beddd 218{
f67beddd 219 char *temp;
6ef9fe01 220 int len, res = 0;
f67beddd 221
405f48ee 222 res = smtp_auth_md5(fd, login, password);
7b68d8ae
MS
223 if (res == 0) {
224 return (0);
225 } else if (res == -2) {
226 /*
227 * If the return code is -2, then then the login attempt failed,
228 * do not try other login mechanisms
229 */
5d7fe8bb 230 return (1);
f67beddd
MS
231 }
232
bf83173b
SS
233 if ((config->features & INSECURE) != 0 ||
234 (config->features & SECURETRANS) != 0) {
7b68d8ae
MS
235 /* Send AUTH command according to RFC 2554 */
236 send_remote_command(fd, "AUTH LOGIN");
237 if (read_remote(fd, 0, NULL) != 3) {
405f48ee 238 syslog(LOG_NOTICE, "remote delivery deferred:"
a5a8a1a4 239 " AUTH login not available: %s",
405f48ee 240 neterr);
7b68d8ae
MS
241 return (1);
242 }
f67beddd 243
7b68d8ae 244 len = base64_encode(login, strlen(login), &temp);
5d7fe8bb
SS
245 if (len < 0) {
246encerr:
405f48ee 247 syslog(LOG_ERR, "can not encode auth reply: %m");
5d7fe8bb
SS
248 return (1);
249 }
f67beddd 250
7b68d8ae 251 send_remote_command(fd, "%s", temp);
5d7fe8bb 252 free(temp);
e2c88018
SS
253 res = read_remote(fd, 0, NULL);
254 if (res != 3) {
405f48ee
SS
255 syslog(LOG_NOTICE, "remote delivery %s: AUTH login failed: %s",
256 res == 5 ? "failed" : "deferred", neterr);
e2c88018 257 return (res == 5 ? -1 : 1);
7b68d8ae 258 }
f67beddd 259
7b68d8ae 260 len = base64_encode(password, strlen(password), &temp);
5d7fe8bb
SS
261 if (len < 0)
262 goto encerr;
7b68d8ae
MS
263
264 send_remote_command(fd, "%s", temp);
5d7fe8bb 265 free(temp);
7b68d8ae 266 res = read_remote(fd, 0, NULL);
e2c88018 267 if (res != 2) {
405f48ee
SS
268 syslog(LOG_NOTICE, "remote delivery %s: Authentication failed: %s",
269 res == 5 ? "failed" : "deferred", neterr);
e2c88018 270 return (res == 5 ? -1 : 1);
7b68d8ae
MS
271 }
272 } else {
405f48ee 273 syslog(LOG_WARNING, "non-encrypted SMTP login is disabled in config, so skipping it. ");
7b68d8ae 274 return (1);
f67beddd
MS
275 }
276
277 return (0);
278}
279
280static int
405f48ee 281open_connection(const char *host)
f67beddd 282{
f67beddd
MS
283 struct addrinfo hints, *res, *res0;
284 char servname[128];
285 const char *errmsg = NULL;
f67beddd
MS
286 int fd, error = 0, port;
287
dba19026 288 if (config->port != 0)
f67beddd
MS
289 port = config->port;
290 else
291 port = SMTP_PORT;
292
7b68d8ae 293 /* FIXME get MX record of host */
f67beddd
MS
294 /* Shamelessly taken from getaddrinfo(3) */
295 memset(&hints, 0, sizeof(hints));
296 hints.ai_family = PF_UNSPEC;
297 hints.ai_socktype = SOCK_STREAM;
298 hints.ai_protocol = IPPROTO_TCP;
299
300 snprintf(servname, sizeof(servname), "%d", port);
301 error = getaddrinfo(host, servname, &hints, &res0);
302 if (error) {
405f48ee 303 syslog(LOG_NOTICE, "remote delivery deferred: %s", gai_strerror(error));
f67beddd
MS
304 return (-1);
305 }
306 fd = -1;
307 for (res = res0; res; res = res->ai_next) {
308 fd=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
309 if (fd < 0) {
310 errmsg = "socket failed";
311 continue;
312 }
313 if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
314 errmsg = "connect failed";
315 close(fd);
316 fd = -1;
317 continue;
318 }
319 break;
320 }
321 if (fd < 0) {
405f48ee
SS
322 syslog(LOG_NOTICE, "remote delivery deferred: %s (%s:%s)",
323 errmsg, host, servname);
f67beddd
MS
324 freeaddrinfo(res0);
325 return (-1);
326 }
327 freeaddrinfo(res0);
f67beddd
MS
328 return (fd);
329}
330
10eeb0df
SS
331static void
332close_connection(int fd)
333{
334 if (((config->features & SECURETRANS) != 0) &&
335 ((config->features & NOSSL) == 0))
336 SSL_shutdown(config->ssl);
337
338 if (config->ssl != NULL)
339 SSL_free(config->ssl);
340
341 close(fd);
342}
343
f67beddd 344int
5868e44c 345deliver_remote(struct qitem *it, const char **errmsg)
f67beddd
MS
346{
347 struct authuser *a;
6ef9fe01
MS
348 char *host, line[1000];
349 int fd, error = 0, do_auth = 0, res = 0;
f67beddd 350 size_t linelen;
5868e44c
SS
351 /* asprintf can't take const */
352 void *errmsgc = __DECONST(char **, errmsg);
f67beddd
MS
353
354 host = strrchr(it->addr, '@');
355 /* Should not happen */
39f7c85a 356 if (host == NULL) {
5868e44c 357 asprintf(errmsgc, "Internal error: badly formed address %s",
39f7c85a 358 it->addr);
f67beddd 359 return(-1);
39f7c85a 360 } else {
f67beddd
MS
361 /* Step over the @ */
362 host++;
39f7c85a 363 }
f67beddd
MS
364
365 /* Smarthost support? */
366 if (config->smarthost != NULL && strlen(config->smarthost) > 0) {
405f48ee
SS
367 syslog(LOG_INFO, "using smarthost (%s:%i)",
368 config->smarthost, config->port);
f67beddd
MS
369 host = config->smarthost;
370 }
371
405f48ee 372 fd = open_connection(host);
f67beddd 373 if (fd < 0)
4a23bd3d 374 return (1);
f67beddd 375
6ef9fe01
MS
376 /* Check first reply from remote host */
377 config->features |= NOSSL;
7b68d8ae 378 res = read_remote(fd, 0, NULL);
6ef9fe01 379 if (res != 2) {
405f48ee 380 syslog(LOG_WARNING, "Invalid initial response: %i", res);
6ef9fe01
MS
381 return(1);
382 }
383 config->features &= ~NOSSL;
384
f67beddd 385 if ((config->features & SECURETRANS) != 0) {
405f48ee 386 error = smtp_init_crypto(fd, config->features);
f67beddd 387 if (error >= 0)
405f48ee 388 syslog(LOG_DEBUG, "SSL initialization successful");
f67beddd
MS
389 else
390 goto out;
391 }
66674731
SS
392
393 send_remote_command(fd, "EHLO %s", hostname());
394 if (read_remote(fd, 0, NULL) != 2) {
405f48ee 395 syslog(LOG_WARNING, "remote delivery deferred: EHLO failed: %s", neterr);
66674731
SS
396 asprintf(errmsgc, "%s did not like our EHLO:\n%s",
397 host, neterr);
398 return (-1);
f67beddd
MS
399 }
400
401 /*
402 * Use SMTP authentication if the user defined an entry for the remote
403 * or smarthost
404 */
405 SLIST_FOREACH(a, &authusers, next) {
406 if (strcmp(a->host, host) == 0) {
407 do_auth = 1;
408 break;
409 }
410 }
411
412 if (do_auth == 1) {
b558d098
MS
413 /*
414 * Check if the user wants plain text login without using
415 * encryption.
416 */
405f48ee
SS
417 syslog(LOG_INFO, "using SMTP authentication");
418 error = smtp_login(fd, a->login, a->password);
7b68d8ae 419 if (error < 0) {
405f48ee
SS
420 syslog(LOG_ERR, "remote delivery failed:"
421 " SMTP login failed: %m");
5868e44c 422 asprintf(errmsgc, "SMTP login to %s failed", host);
4a23bd3d 423 return (-1);
b558d098 424 }
7b68d8ae 425 /* SMTP login is not available, so try without */
405f48ee
SS
426 else if (error > 0) {
427 syslog(LOG_WARNING, "SMTP login not available. Trying without.");
428 }
f67beddd
MS
429 }
430
7dfd2fd8
SS
431#define READ_REMOTE_CHECK(c, exp) \
432 res = read_remote(fd, 0, NULL); \
433 if (res == 5) { \
405f48ee
SS
434 syslog(LOG_ERR, "remote delivery failed: " \
435 c " failed: %s", neterr); \
5868e44c 436 asprintf(errmsgc, "%s did not like our " c ":\n%s", \
39f7c85a 437 host, neterr); \
7dfd2fd8
SS
438 return (-1); \
439 } else if (res != exp) { \
405f48ee
SS
440 syslog(LOG_NOTICE, "remote delivery deferred: " \
441 c " failed: %s", neterr); \
7dfd2fd8 442 return (1); \
f67beddd
MS
443 }
444
7dfd2fd8
SS
445 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
446 READ_REMOTE_CHECK("MAIL FROM", 2);
447
4a23bd3d 448 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
7dfd2fd8 449 READ_REMOTE_CHECK("RCPT TO", 2);
f67beddd
MS
450
451 send_remote_command(fd, "DATA");
7dfd2fd8 452 READ_REMOTE_CHECK("DATA", 3);
f67beddd 453
f4e61a9f 454 if (fseek(it->mailf, it->hdrlen, SEEK_SET) != 0) {
405f48ee 455 syslog(LOG_ERR, "remote delivery deferred: cannot seek: %s", neterr);
f67beddd
MS
456 return (1);
457 }
458
0caaabf6 459 error = 0;
f4e61a9f
SS
460 while (!feof(it->mailf)) {
461 if (fgets(line, sizeof(line), it->mailf) == NULL)
f67beddd
MS
462 break;
463 linelen = strlen(line);
464 if (linelen == 0 || line[linelen - 1] != '\n') {
405f48ee 465 syslog(LOG_CRIT, "remote delivery failed: corrupted queue file");
5868e44c 466 *errmsg = "corrupted queue file";
f67beddd
MS
467 error = -1;
468 goto out;
469 }
470
471 /* Remove trailing \n's and escape leading dots */
472 trim_line(line);
473
474 /*
475 * If the first character is a dot, we escape it so the line
476 * length increases
477 */
478 if (line[0] == '.')
479 linelen++;
480
481 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
405f48ee 482 syslog(LOG_NOTICE, "remote delivery deferred: write error");
f67beddd
MS
483 error = 1;
484 goto out;
485 }
486 }
487
488 send_remote_command(fd, ".");
7dfd2fd8 489 READ_REMOTE_CHECK("final DATA", 2);
f67beddd
MS
490
491 send_remote_command(fd, "QUIT");
6cfc247d 492 if (read_remote(fd, 0, NULL) != 2)
405f48ee 493 syslog(LOG_INFO, "remote delivery succeeded but QUIT failed: %s", neterr);
f67beddd
MS
494out:
495
10eeb0df 496 close_connection(fd);
f67beddd
MS
497 return (error);
498}
499