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