]> git.ipfire.org Git - people/ms/dma.git/blame - net.c
Merge pull request #57 from mtremer/better-tls
[people/ms/dma.git] / net.c
CommitLineData
86e4d161 1/*
e458e5f0 2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
86e4d161
MS
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
6 * by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg,
7 * Germany.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
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
18 * distribution.
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.
22 *
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
34 * SUCH DAMAGE.
86e4d161
MS
35 */
36
9b921550
SS
37#include "dfcompat.h"
38
86e4d161
MS
39#include <sys/param.h>
40#include <sys/queue.h>
41#include <sys/stat.h>
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46
86e4d161 47#include <openssl/ssl.h>
53885e5a 48#include <openssl/err.h>
86e4d161 49
8a6f2a49 50#include <ctype.h>
c507d897 51#include <err.h>
7a73c463 52#include <errno.h>
86e4d161
MS
53#include <netdb.h>
54#include <setjmp.h>
55#include <signal.h>
56#include <syslog.h>
57#include <unistd.h>
58
59#include "dma.h"
60
249ee966 61char neterr[ERRMSG_SIZE];
86e4d161 62
53885e5a
SS
63char *
64ssl_errstr(void)
65{
66 long oerr, nerr;
67
68 oerr = 0;
69 while ((nerr = ERR_get_error()) != 0)
70 oerr = nerr;
71
72 return (ERR_error_string(oerr, NULL));
73}
74
86e4d161
MS
75ssize_t
76send_remote_command(int fd, const char* fmt, ...)
77{
78 va_list va;
79 char cmd[4096];
5b84c25b
SS
80 size_t len, pos;
81 int s;
82 ssize_t n;
86e4d161
MS
83
84 va_start(va, fmt);
5b84c25b
SS
85 s = vsnprintf(cmd, sizeof(cmd) - 2, fmt, va);
86 va_end(va);
53885e5a
SS
87 if (s == sizeof(cmd) - 2 || s < 0) {
88 strcpy(neterr, "Internal error: oversized command string");
89 return (-1);
90 }
91
5b84c25b
SS
92 /* We *know* there are at least two more bytes available */
93 strcat(cmd, "\r\n");
94 len = strlen(cmd);
86e4d161 95
a0c4afa6
SS
96 if (((config.features & SECURETRANS) != 0) &&
97 ((config.features & NOSSL) == 0)) {
98 while ((s = SSL_write(config.ssl, (const char*)cmd, len)) <= 0) {
99 s = SSL_get_error(config.ssl, s);
5b84c25b 100 if (s != SSL_ERROR_WANT_READ &&
53885e5a
SS
101 s != SSL_ERROR_WANT_WRITE) {
102 strncpy(neterr, ssl_errstr(), sizeof(neterr));
5b84c25b 103 return (-1);
53885e5a 104 }
5b84c25b 105 }
86e4d161
MS
106 }
107 else {
5b84c25b
SS
108 pos = 0;
109 while (pos < len) {
110 n = write(fd, cmd + pos, len - pos);
111 if (n < 0)
112 return (-1);
113 pos += n;
114 }
86e4d161 115 }
86e4d161 116
5b84c25b 117 return (len);
86e4d161
MS
118}
119
50c8e68a 120int
bf28fcc6 121read_remote(int fd, int extbufsize, char *extbuf)
86e4d161 122{
50c8e68a 123 ssize_t rlen = 0;
8a6f2a49 124 size_t pos, len, copysize;
50c8e68a 125 char buff[BUF_SIZE];
8a6f2a49
SS
126 int done = 0, status = 0, status_running = 0, extbufpos = 0;
127 enum { parse_status, parse_spacedash, parse_rest } parsestate;
86e4d161 128
8a6f2a49 129 if (do_timeout(CON_TIMEOUT, 1) != 0) {
7a73c463 130 snprintf(neterr, sizeof(neterr), "Timeout reached");
53885e5a 131 return (-1);
86e4d161 132 }
86e4d161
MS
133
134 /*
50c8e68a
MS
135 * Remote reading code from femail.c written by Henning Brauer of
136 * OpenBSD and released under a BSD style license.
86e4d161 137 */
8a6f2a49
SS
138 len = 0;
139 pos = 0;
140 parsestate = parse_status;
141 neterr[0] = 0;
142 while (!(done && parsestate == parse_status)) {
bf28fcc6 143 rlen = 0;
50c8e68a
MS
144 if (pos == 0 ||
145 (pos > 0 && memchr(buff + pos, '\n', len - pos) == NULL)) {
146 memmove(buff, buff + pos, len - pos);
147 len -= pos;
148 pos = 0;
a0c4afa6
SS
149 if (((config.features & SECURETRANS) != 0) &&
150 (config.features & NOSSL) == 0) {
8a6f2a49 151 if ((rlen = SSL_read(config.ssl, buff + len, sizeof(buff) - len)) == -1) {
53885e5a 152 strncpy(neterr, ssl_errstr(), sizeof(neterr));
8a6f2a49 153 goto error;
53885e5a 154 }
50c8e68a 155 } else {
53885e5a
SS
156 if ((rlen = read(fd, buff + len, sizeof(buff) - len)) == -1) {
157 strncpy(neterr, strerror(errno), sizeof(neterr));
8a6f2a49 158 goto error;
53885e5a 159 }
50c8e68a
MS
160 }
161 len += rlen;
8a6f2a49
SS
162
163 copysize = sizeof(neterr) - strlen(neterr) - 1;
164 if (copysize > len)
165 copysize = len;
166 strncat(neterr, buff, copysize);
50c8e68a 167 }
bf28fcc6
MS
168 /*
169 * If there is an external buffer with a size bigger than zero
170 * and as long as there is space in the external buffer and
171 * there are new characters read from the mailserver
172 * copy them to the external buffer
173 */
8a6f2a49 174 if (extbufpos <= (extbufsize - 1) && rlen > 0 && extbufsize > 0 && extbuf != NULL) {
bf28fcc6
MS
175 /* do not write over the bounds of the buffer */
176 if(extbufpos + rlen > (extbufsize - 1)) {
177 rlen = extbufsize - extbufpos;
178 }
179 memcpy(extbuf + extbufpos, buff + len - rlen, rlen);
180 extbufpos += rlen;
181 }
86e4d161 182
50c8e68a 183 if (pos == len)
8a6f2a49 184 continue;
86e4d161 185
8a6f2a49
SS
186 switch (parsestate) {
187 case parse_status:
188 for (; pos < len; pos++) {
189 if (isdigit(buff[pos])) {
190 status_running = status_running * 10 + (buff[pos] - '0');
191 } else {
192 status = status_running;
193 status_running = 0;
194 parsestate = parse_spacedash;
195 break;
196 }
197 }
198 continue;
199
200 case parse_spacedash:
201 switch (buff[pos]) {
202 case ' ':
203 done = 1;
204 break;
86e4d161 205
8a6f2a49
SS
206 case '-':
207 /* ignore */
208 /* XXX read capabilities */
209 break;
210
211 default:
212 strcpy(neterr, "invalid syntax in reply from server");
213 goto error;
214 }
215
216 pos++;
217 parsestate = parse_rest;
218 continue;
219
220 case parse_rest:
221 /* skip up to \n */
222 for (; pos < len; pos++) {
223 if (buff[pos] == '\n') {
224 pos++;
225 parsestate = parse_status;
226 break;
227 }
228 }
229 }
86e4d161 230
86e4d161 231 }
50c8e68a 232
8a6f2a49
SS
233 do_timeout(0, 0);
234
235 /* chop off trailing newlines */
236 while (neterr[0] != 0 && strchr("\r\n", neterr[strlen(neterr) - 1]) != 0)
237 neterr[strlen(neterr) - 1] = 0;
238
50c8e68a 239 return (status/100);
8a6f2a49
SS
240
241error:
242 do_timeout(0, 0);
243 return (-1);
86e4d161
MS
244}
245
246/*
247 * Handle SMTP authentication
86e4d161
MS
248 */
249static int
4d5af2b0 250smtp_login(int fd, char *login, char* password)
86e4d161 251{
86e4d161 252 char *temp;
50c8e68a 253 int len, res = 0;
86e4d161 254
4d5af2b0 255 res = smtp_auth_md5(fd, login, password);
bf28fcc6
MS
256 if (res == 0) {
257 return (0);
258 } else if (res == -2) {
259 /*
89702b7f 260 * If the return code is -2, then then the login attempt failed,
bf28fcc6
MS
261 * do not try other login mechanisms
262 */
196b1b32 263 return (1);
86e4d161
MS
264 }
265
a0c4afa6
SS
266 if ((config.features & INSECURE) != 0 ||
267 (config.features & SECURETRANS) != 0) {
bf28fcc6
MS
268 /* Send AUTH command according to RFC 2554 */
269 send_remote_command(fd, "AUTH LOGIN");
270 if (read_remote(fd, 0, NULL) != 3) {
4d5af2b0 271 syslog(LOG_NOTICE, "remote delivery deferred:"
7a73c463 272 " AUTH login not available: %s",
4d5af2b0 273 neterr);
bf28fcc6
MS
274 return (1);
275 }
86e4d161 276
bf28fcc6 277 len = base64_encode(login, strlen(login), &temp);
196b1b32
SS
278 if (len < 0) {
279encerr:
4d5af2b0 280 syslog(LOG_ERR, "can not encode auth reply: %m");
196b1b32
SS
281 return (1);
282 }
86e4d161 283
bf28fcc6 284 send_remote_command(fd, "%s", temp);
196b1b32 285 free(temp);
53885e5a
SS
286 res = read_remote(fd, 0, NULL);
287 if (res != 3) {
4d5af2b0
SS
288 syslog(LOG_NOTICE, "remote delivery %s: AUTH login failed: %s",
289 res == 5 ? "failed" : "deferred", neterr);
53885e5a 290 return (res == 5 ? -1 : 1);
bf28fcc6 291 }
86e4d161 292
bf28fcc6 293 len = base64_encode(password, strlen(password), &temp);
196b1b32
SS
294 if (len < 0)
295 goto encerr;
bf28fcc6
MS
296
297 send_remote_command(fd, "%s", temp);
196b1b32 298 free(temp);
bf28fcc6 299 res = read_remote(fd, 0, NULL);
53885e5a 300 if (res != 2) {
4d5af2b0
SS
301 syslog(LOG_NOTICE, "remote delivery %s: Authentication failed: %s",
302 res == 5 ? "failed" : "deferred", neterr);
53885e5a 303 return (res == 5 ? -1 : 1);
bf28fcc6
MS
304 }
305 } else {
4d5af2b0 306 syslog(LOG_WARNING, "non-encrypted SMTP login is disabled in config, so skipping it. ");
bf28fcc6 307 return (1);
86e4d161
MS
308 }
309
310 return (0);
311}
312
313static int
8cbdbd34 314open_connection(struct mx_hostentry *h)
86e4d161 315{
8cbdbd34
SS
316 int fd;
317
318 syslog(LOG_INFO, "trying remote delivery to %s [%s] pref %d",
319 h->host, h->addr, h->pref);
320
321 fd = socket(h->ai.ai_family, h->ai.ai_socktype, h->ai.ai_protocol);
322 if (fd < 0) {
323 syslog(LOG_INFO, "socket for %s [%s] failed: %m",
324 h->host, h->addr);
86e4d161
MS
325 return (-1);
326 }
8cbdbd34 327
32d3ec65 328 if (connect(fd, (struct sockaddr *)&h->sa, h->ai.ai_addrlen) < 0) {
8cbdbd34
SS
329 syslog(LOG_INFO, "connect to %s [%s] failed: %m",
330 h->host, h->addr);
331 close(fd);
86e4d161
MS
332 return (-1);
333 }
8cbdbd34 334
86e4d161
MS
335 return (fd);
336}
337
8c365457
SS
338static void
339close_connection(int fd)
340{
f75033bb
PP
341 if (config.ssl != NULL) {
342 if (((config.features & SECURETRANS) != 0) &&
343 ((config.features & NOSSL) == 0))
344 SSL_shutdown(config.ssl);
a0c4afa6 345 SSL_free(config.ssl);
f75033bb 346 }
8c365457
SS
347
348 close(fd);
349}
350
8cbdbd34 351static int
249ee966 352deliver_to_host(struct qitem *it, struct mx_hostentry *host)
86e4d161
MS
353{
354 struct authuser *a;
8cbdbd34 355 char line[1000];
86e4d161 356 size_t linelen;
8cbdbd34 357 int fd, error = 0, do_auth = 0, res = 0;
86e4d161 358
6ddd63e1 359 if (fseek(it->mailf, 0, SEEK_SET) != 0) {
249ee966 360 snprintf(errmsg, sizeof(errmsg), "can not seek: %s", strerror(errno));
40069c2f
SS
361 return (-1);
362 }
363
4d5af2b0 364 fd = open_connection(host);
86e4d161 365 if (fd < 0)
e0a95d28 366 return (1);
86e4d161 367
8cbdbd34
SS
368#define READ_REMOTE_CHECK(c, exp) \
369 res = read_remote(fd, 0, NULL); \
370 if (res == 5) { \
371 syslog(LOG_ERR, "remote delivery to %s [%s] failed after %s: %s", \
372 host->host, host->addr, c, neterr); \
249ee966 373 snprintf(errmsg, sizeof(errmsg), "%s [%s] did not like our %s:\n%s", \
8cbdbd34 374 host->host, host->addr, c, neterr); \
b059bcd6 375 error = -1; \
376 goto out; \
8cbdbd34
SS
377 } else if (res != exp) { \
378 syslog(LOG_NOTICE, "remote delivery deferred: %s [%s] failed after %s: %s", \
379 host->host, host->addr, c, neterr); \
b059bcd6 380 error = 1; \
381 goto out; \
8cbdbd34
SS
382 }
383
b295f51d 384 /* Check first reply from remote host */
ec04827a
PP
385 if ((config.features & SECURETRANS) == 0 ||
386 (config.features & STARTTLS) != 0) {
387 config.features |= NOSSL;
388 READ_REMOTE_CHECK("connect", 2);
b295f51d 389
ec04827a
PP
390 config.features &= ~NOSSL;
391 }
b295f51d
SS
392
393 if ((config.features & SECURETRANS) != 0) {
394 error = smtp_init_crypto(fd, config.features);
a9d456bc 395 if (error == 0)
b295f51d
SS
396 syslog(LOG_DEBUG, "SSL initialization successful");
397 else
398 goto out;
ec04827a
PP
399
400 if ((config.features & STARTTLS) == 0)
401 READ_REMOTE_CHECK("connect", 2);
b295f51d
SS
402 }
403
8945ce6c
SS
404 /* XXX allow HELO fallback */
405 /* XXX record ESMTP keywords */
f3105067 406 send_remote_command(fd, "EHLO %s", hostname());
8cbdbd34 407 READ_REMOTE_CHECK("EHLO", 2);
86e4d161
MS
408
409 /*
410 * Use SMTP authentication if the user defined an entry for the remote
411 * or smarthost
412 */
413 SLIST_FOREACH(a, &authusers, next) {
8cbdbd34 414 if (strcmp(a->host, host->host) == 0) {
86e4d161
MS
415 do_auth = 1;
416 break;
417 }
418 }
419
420 if (do_auth == 1) {
dfce7456
MS
421 /*
422 * Check if the user wants plain text login without using
423 * encryption.
424 */
a0c4afa6 425 syslog(LOG_INFO, "using SMTP authentication for user %s", a->login);
4d5af2b0 426 error = smtp_login(fd, a->login, a->password);
bf28fcc6 427 if (error < 0) {
4d5af2b0
SS
428 syslog(LOG_ERR, "remote delivery failed:"
429 " SMTP login failed: %m");
249ee966 430 snprintf(errmsg, sizeof(errmsg), "SMTP login to %s failed", host->host);
b059bcd6 431 error = -1;
432 goto out;
dfce7456 433 }
bf28fcc6 434 /* SMTP login is not available, so try without */
4d5af2b0
SS
435 else if (error > 0) {
436 syslog(LOG_WARNING, "SMTP login not available. Trying without.");
437 }
86e4d161
MS
438 }
439
8945ce6c 440 /* XXX send ESMTP ENVID, RET (FULL/HDRS) and 8BITMIME */
c68aff7a
SS
441 send_remote_command(fd, "MAIL FROM:<%s>", it->sender);
442 READ_REMOTE_CHECK("MAIL FROM", 2);
443
8945ce6c 444 /* XXX send ESMTP ORCPT */
e0a95d28 445 send_remote_command(fd, "RCPT TO:<%s>", it->addr);
c68aff7a 446 READ_REMOTE_CHECK("RCPT TO", 2);
86e4d161
MS
447
448 send_remote_command(fd, "DATA");
c68aff7a 449 READ_REMOTE_CHECK("DATA", 3);
86e4d161 450
3a7b3ec9 451 error = 0;
fd1de51a
SS
452 while (!feof(it->mailf)) {
453 if (fgets(line, sizeof(line), it->mailf) == NULL)
86e4d161
MS
454 break;
455 linelen = strlen(line);
456 if (linelen == 0 || line[linelen - 1] != '\n') {
4d5af2b0 457 syslog(LOG_CRIT, "remote delivery failed: corrupted queue file");
249ee966 458 snprintf(errmsg, sizeof(errmsg), "corrupted queue file");
86e4d161
MS
459 error = -1;
460 goto out;
461 }
462
463 /* Remove trailing \n's and escape leading dots */
464 trim_line(line);
465
466 /*
467 * If the first character is a dot, we escape it so the line
468 * length increases
469 */
470 if (line[0] == '.')
471 linelen++;
472
473 if (send_remote_command(fd, "%s", line) != (ssize_t)linelen+1) {
4d5af2b0 474 syslog(LOG_NOTICE, "remote delivery deferred: write error");
86e4d161
MS
475 error = 1;
476 goto out;
477 }
478 }
479
480 send_remote_command(fd, ".");
c68aff7a 481 READ_REMOTE_CHECK("final DATA", 2);
86e4d161
MS
482
483 send_remote_command(fd, "QUIT");
83f3cc40 484 if (read_remote(fd, 0, NULL) != 2)
4d5af2b0 485 syslog(LOG_INFO, "remote delivery succeeded but QUIT failed: %s", neterr);
86e4d161
MS
486out:
487
8c365457 488 close_connection(fd);
86e4d161
MS
489 return (error);
490}
491
8cbdbd34 492int
249ee966 493deliver_remote(struct qitem *it)
8cbdbd34 494{
8cbdbd34 495 struct mx_hostentry *hosts, *h;
a0c4afa6 496 const char *host;
8cbdbd34
SS
497 int port;
498 int error = 1, smarthost = 0;
499
8cbdbd34
SS
500 port = SMTP_PORT;
501
502 /* Smarthost support? */
a0c4afa6
SS
503 if (config.smarthost != NULL) {
504 host = config.smarthost;
505 port = config.port;
506 syslog(LOG_INFO, "using smarthost (%s:%i)", host, port);
8cbdbd34 507 smarthost = 1;
89702b7f
SS
508 } else {
509 host = strrchr(it->addr, '@');
510 /* Should not happen */
511 if (host == NULL) {
512 snprintf(errmsg, sizeof(errmsg), "Internal error: badly formed address %s",
513 it->addr);
514 return(-1);
515 } else {
516 /* Step over the @ */
517 host++;
518 }
8cbdbd34
SS
519 }
520
521 error = dns_get_mx_list(host, port, &hosts, smarthost);
522 if (error) {
5317696b
SS
523 snprintf(errmsg, sizeof(errmsg), "DNS lookup failure: host %s not found", host);
524 syslog(LOG_NOTICE, "remote delivery %s: DNS lookup failure: host %s not found",
8cbdbd34
SS
525 error < 0 ? "failed" : "deferred",
526 host);
527 return (error);
528 }
529
530 for (h = hosts; *h->host != 0; h++) {
249ee966 531 switch (deliver_to_host(it, h)) {
8cbdbd34
SS
532 case 0:
533 /* success */
534 error = 0;
535 goto out;
536 case 1:
537 /* temp failure */
538 error = 1;
539 break;
540 default:
541 /* perm failure */
542 error = -1;
543 goto out;
544 }
545 }
546out:
547 free(hosts);
548
549 return (error);
550}