]> git.ipfire.org Git - people/ms/dma.git/blame - dma.c
dma: don't set it->mailf too early
[people/ms/dma.git] / dma.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 Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
86e4d161
MS
33 */
34
35#include <sys/param.h>
36#include <sys/queue.h>
37#include <sys/stat.h>
38#include <sys/types.h>
c507d897 39#include <sys/wait.h>
86e4d161 40
86e4d161
MS
41#include <dirent.h>
42#include <err.h>
43#include <errno.h>
44#include <fcntl.h>
45#include <inttypes.h>
86e4d161
MS
46#include <paths.h>
47#include <pwd.h>
48#include <signal.h>
49#include <stdarg.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <syslog.h>
54#include <unistd.h>
55
56#include "dma.h"
57
58
59
e0a95d28 60static void deliver(struct qitem *);
86e4d161
MS
61
62struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
fd1de51a 63struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
86e4d161
MS
64struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
65struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
86e4d161 66struct config *config;
87bdbc01 67const char *username;
de13a881 68const char *logident_base;
86e4d161 69
87bdbc01 70static int daemonize = 1;
dd561a09 71
86e4d161
MS
72static char *
73set_from(const char *osender)
74{
75 struct virtuser *v;
76 char *sender;
77
78 if ((config->features & VIRTUAL) != 0) {
79 SLIST_FOREACH(v, &virtusers, next) {
dd561a09 80 if (strcmp(v->login, username) == 0) {
86e4d161
MS
81 sender = strdup(v->address);
82 if (sender == NULL)
83 return(NULL);
84 goto out;
85 }
86 }
87 }
88
89 if (osender) {
90 sender = strdup(osender);
91 if (sender == NULL)
e0a95d28 92 return (NULL);
86e4d161 93 } else {
dd561a09 94 if (asprintf(&sender, "%s@%s", username, hostname()) <= 0)
e0a95d28 95 return (NULL);
86e4d161
MS
96 }
97
98 if (strchr(sender, '\n') != NULL) {
99 errno = EINVAL;
e0a95d28 100 return (NULL);
86e4d161
MS
101 }
102
103out:
e0a95d28 104 return (sender);
86e4d161
MS
105}
106
107static int
108read_aliases(void)
109{
110 yyin = fopen(config->aliases, "r");
111 if (yyin == NULL)
e0a95d28 112 return (0); /* not fatal */
86e4d161 113 if (yyparse())
e0a95d28 114 return (-1); /* fatal error, probably malloc() */
86e4d161 115 fclose(yyin);
e0a95d28 116 return (0);
86e4d161
MS
117}
118
fd1de51a 119int
86e4d161
MS
120add_recp(struct queue *queue, const char *str, const char *sender, int expand)
121{
122 struct qitem *it, *tit;
123 struct stritem *sit;
124 struct alias *al;
125 struct passwd *pw;
126 char *host;
127 int aliased = 0;
128
129 it = calloc(1, sizeof(*it));
130 if (it == NULL)
e0a95d28 131 return (-1);
86e4d161
MS
132 it->addr = strdup(str);
133 if (it->addr == NULL)
e0a95d28 134 return (-1);
86e4d161
MS
135
136 it->sender = sender;
137 host = strrchr(it->addr, '@');
138 if (host != NULL &&
139 (strcmp(host + 1, hostname()) == 0 ||
140 strcmp(host + 1, "localhost") == 0)) {
141 *host = 0;
142 }
143 LIST_FOREACH(tit, &queue->queue, next) {
144 /* weed out duplicate dests */
145 if (strcmp(tit->addr, it->addr) == 0) {
146 free(it->addr);
147 free(it);
e0a95d28 148 return (0);
86e4d161
MS
149 }
150 }
151 LIST_INSERT_HEAD(&queue->queue, it, next);
152 if (strrchr(it->addr, '@') == NULL) {
e0a95d28 153 it->remote = 0;
86e4d161
MS
154 if (expand) {
155 LIST_FOREACH(al, &aliases, next) {
156 if (strcmp(al->alias, it->addr) != 0)
157 continue;
158 SLIST_FOREACH(sit, &al->dests, next) {
e0a95d28
MS
159 if (add_recp(queue, sit->str, sender, 1) != 0)
160 return (-1);
86e4d161
MS
161 }
162 aliased = 1;
163 }
164 if (aliased) {
165 LIST_REMOVE(it, next);
166 } else {
e0a95d28 167 /* Local destination, check */
86e4d161
MS
168 pw = getpwnam(it->addr);
169 if (pw == NULL)
170 goto out;
8d291e4d 171 /* XXX read .forward */
e0a95d28 172 endpwent();
86e4d161
MS
173 }
174 }
175 } else {
e0a95d28 176 it->remote = 1;
86e4d161
MS
177 }
178
e0a95d28 179 return (0);
86e4d161
MS
180
181out:
182 free(it->addr);
183 free(it);
e0a95d28 184 return (-1);
86e4d161
MS
185}
186
86e4d161
MS
187static int
188readmail(struct queue *queue, const char *sender, int nodot)
189{
190 char line[1000]; /* by RFC2822 */
191 size_t linelen;
87bdbc01 192 size_t error;
ea5f5903
SS
193 int had_headers = 0;
194 int had_from = 0;
195 int had_messagid = 0;
196 int had_date = 0;
86e4d161 197
87bdbc01 198 error = fprintf(queue->mailf,
5896fefc
SS
199 "Received: from %s (uid %d)\n"
200 "\t(envelope-from %s)\n"
4d5af2b0 201 "\tid %s\n"
5896fefc
SS
202 "\tby %s (%s)\n"
203 "\t%s\n",
87bdbc01 204 username, getuid(),
86e4d161
MS
205 sender,
206 queue->id,
207 hostname(), VERSION,
208 rfc822date());
87bdbc01 209 if ((ssize_t)error < 0)
e0a95d28 210 return (-1);
86e4d161
MS
211
212 while (!feof(stdin)) {
213 if (fgets(line, sizeof(line), stdin) == NULL)
214 break;
215 linelen = strlen(line);
216 if (linelen == 0 || line[linelen - 1] != '\n') {
217 errno = EINVAL; /* XXX mark permanent errors */
e0a95d28 218 return (-1);
86e4d161 219 }
ea5f5903
SS
220 if (!had_headers) {
221 if (strprefixcmp(line, "Date:") == 0)
222 had_date = 1;
223 else if (strprefixcmp(line, "Message-Id:") == 0)
224 had_messagid = 1;
225 else if (strprefixcmp(line, "From:") == 0)
226 had_from = 1;
227 }
228 if (strcmp(line, "\n") == 0 && !had_headers) {
229 had_headers = 1;
230 while (!had_date || !had_messagid || !had_from) {
231 if (!had_date) {
232 had_date = 1;
233 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
234 } else if (!had_messagid) {
235 /* XXX better msgid, assign earlier and log? */
236 had_messagid = 1;
4d5af2b0 237 snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
ea5f5903
SS
238 queue->id, hostname());
239 } else if (!had_from) {
240 had_from = 1;
241 snprintf(line, sizeof(line), "From: <%s>\n", sender);
242 }
87bdbc01 243 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
ea5f5903
SS
244 return (-1);
245 }
246 strcpy(line, "\n");
247 }
86e4d161
MS
248 if (!nodot && linelen == 2 && line[0] == '.')
249 break;
87bdbc01 250 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
e0a95d28 251 return (-1);
86e4d161 252 }
4d5af2b0 253
e0a95d28 254 return (0);
86e4d161
MS
255}
256
e0a95d28
MS
257static struct qitem *
258go_background(struct queue *queue)
86e4d161
MS
259{
260 struct sigaction sa;
261 struct qitem *it;
262 pid_t pid;
263
264 if (daemonize && daemon(0, 0) != 0) {
e0a95d28 265 syslog(LOG_ERR, "can not daemonize: %m");
86e4d161
MS
266 exit(1);
267 }
268 daemonize = 0;
e0a95d28 269
86e4d161
MS
270 bzero(&sa, sizeof(sa));
271 sa.sa_flags = SA_NOCLDWAIT;
272 sa.sa_handler = SIG_IGN;
273 sigaction(SIGCHLD, &sa, NULL);
274
94389971 275 LIST_FOREACH(it, &queue->queue, next) {
e0a95d28 276 /* No need to fork for the last dest */
87bdbc01
SS
277 if (LIST_NEXT(it, next) == NULL)
278 goto retit;
e0a95d28 279
86e4d161
MS
280 pid = fork();
281 switch (pid) {
282 case -1:
283 syslog(LOG_ERR, "can not fork: %m");
284 exit(1);
285 break;
286
287 case 0:
288 /*
289 * Child:
290 *
291 * return and deliver mail
292 */
87bdbc01
SS
293retit:
294 /*
295 * If necessary, aquire the queue and * mail files.
296 * If this fails, we probably were raced by another
297 * process.
298 */
4d5af2b0 299 setlogident("%s", it->queueid);
87bdbc01
SS
300 if (aquirespool(it) < 0)
301 exit(1);
302 dropspool(queue, it);
e0a95d28 303 return (it);
86e4d161
MS
304
305 default:
306 /*
307 * Parent:
308 *
309 * fork next child
310 */
311 break;
312 }
313 }
314
315 syslog(LOG_CRIT, "reached dead code");
316 exit(1);
317}
318
319static void
09da7b5e 320bounce(struct qitem *it, const char *reason)
86e4d161
MS
321{
322 struct queue bounceq;
323 struct qitem *bit;
324 char line[1000];
8ee63931 325 size_t pos;
86e4d161
MS
326 int error;
327
328 /* Don't bounce bounced mails */
329 if (it->sender[0] == 0) {
4d5af2b0 330 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
86e4d161
MS
331 exit(1);
332 }
333
86e4d161 334 LIST_INIT(&bounceq.queue);
0e9693ec
SS
335 if (add_recp(&bounceq, it->sender, "", 1) != 0)
336 goto fail;
337
fd1de51a 338 if (newspoolf(&bounceq, "") != 0)
86e4d161 339 goto fail;
4d5af2b0
SS
340
341 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
342 setlogident("%s", bounceq.id);
343
87bdbc01 344 error = fprintf(bounceq.mailf,
5896fefc 345 "Received: from MAILER-DAEMON\n"
4d5af2b0 346 "\tid %s\n"
5896fefc
SS
347 "\tby %s (%s)\n"
348 "\t%s\n"
349 "X-Original-To: <%s>\n"
350 "From: MAILER-DAEMON <>\n"
351 "To: %s\n"
352 "Subject: Mail delivery failed\n"
4d5af2b0 353 "Message-Id: <%s@%s>\n"
5896fefc
SS
354 "Date: %s\n"
355 "\n"
356 "This is the %s at %s.\n"
357 "\n"
358 "There was an error delivering your mail to <%s>.\n"
359 "\n"
360 "%s\n"
361 "\n"
362 "%s\n"
363 "\n",
86e4d161
MS
364 bounceq.id,
365 hostname(), VERSION,
366 rfc822date(),
367 it->addr,
368 it->sender,
369 bounceq.id, hostname(),
370 rfc822date(),
371 VERSION, hostname(),
372 it->addr,
8ee63931 373 reason,
5896fefc
SS
374 config->features & FULLBOUNCE ?
375 "Original message follows." :
376 "Message headers follow.");
86e4d161
MS
377 if (error < 0)
378 goto fail;
87bdbc01 379
fd1de51a 380 if (fseek(it->mailf, it->hdrlen, SEEK_SET) != 0)
86e4d161 381 goto fail;
8ee63931 382 if (config->features & FULLBOUNCE) {
fd1de51a 383 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
87bdbc01 384 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
8ee63931
SS
385 goto fail;
386 }
387 } else {
fd1de51a
SS
388 while (!feof(it->mailf)) {
389 if (fgets(line, sizeof(line), it->mailf) == NULL)
8ee63931
SS
390 break;
391 if (line[0] == '\n')
392 break;
87bdbc01 393 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
8ee63931
SS
394 goto fail;
395 }
86e4d161 396 }
87bdbc01 397
0e9693ec 398 if (linkspool(&bounceq, "") != 0)
86e4d161
MS
399 goto fail;
400 /* bounce is safe */
401
fd1de51a 402 delqueue(it);
86e4d161 403
e0a95d28
MS
404 bit = go_background(&bounceq);
405 deliver(bit);
86e4d161
MS
406 /* NOTREACHED */
407
408fail:
4d5af2b0 409 syslog(LOG_CRIT, "error creating bounce: %m");
fd1de51a 410 delqueue(it);
86e4d161
MS
411 exit(1);
412}
413
86e4d161 414static void
e0a95d28 415deliver(struct qitem *it)
86e4d161
MS
416{
417 int error;
418 unsigned int backoff = MIN_RETRY;
09da7b5e 419 const char *errmsg = "unknown bounce reason";
86e4d161
MS
420 struct timeval now;
421 struct stat st;
422
86e4d161 423retry:
4d5af2b0 424 syslog(LOG_INFO, "trying delivery");
86e4d161 425
e0a95d28
MS
426 if (it->remote)
427 error = deliver_remote(it, &errmsg);
428 else
86e4d161
MS
429 error = deliver_local(it, &errmsg);
430
431 switch (error) {
432 case 0:
fd1de51a 433 delqueue(it);
4d5af2b0 434 syslog(LOG_INFO, "delivery successful");
86e4d161
MS
435 exit(0);
436
437 case 1:
438 if (stat(it->queuefn, &st) != 0) {
4d5af2b0 439 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
86e4d161
MS
440 exit(1);
441 }
442 if (gettimeofday(&now, NULL) == 0 &&
443 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
09da7b5e 444 asprintf(__DECONST(void *, &errmsg),
e0a95d28 445 "Could not deliver for the last %d seconds. Giving up.",
a43346d5 446 MAX_TIMEOUT);
86e4d161
MS
447 goto bounce;
448 }
449 sleep(backoff);
450 backoff *= 2;
451 if (backoff > MAX_RETRY)
452 backoff = MAX_RETRY;
453 goto retry;
454
455 case -1:
456 default:
457 break;
458 }
459
460bounce:
e0a95d28 461 bounce(it, errmsg);
86e4d161
MS
462 /* NOTREACHED */
463}
464
86e4d161
MS
465static void
466run_queue(struct queue *queue)
467{
e0a95d28
MS
468 struct qitem *it;
469
86e4d161
MS
470 if (LIST_EMPTY(&queue->queue))
471 return;
472
e0a95d28
MS
473 it = go_background(queue);
474 deliver(it);
86e4d161
MS
475 /* NOTREACHED */
476}
477
478static void
479show_queue(struct queue *queue)
480{
481 struct qitem *it;
87bdbc01 482 int locked = 0; /* XXX */
86e4d161
MS
483
484 if (LIST_EMPTY(&queue->queue)) {
485 printf("Mail queue is empty\n");
486 return;
487 }
488
489 LIST_FOREACH(it, &queue->queue, next) {
5896fefc
SS
490 printf("ID\t: %s%s\n"
491 "From\t: %s\n"
492 "To\t: %s\n"
493 "--\n",
494 it->queueid,
87bdbc01 495 locked ? "*" : "",
5896fefc 496 it->sender, it->addr);
86e4d161
MS
497 }
498}
499
500/*
501 * TODO:
502 *
503 * - alias processing
504 * - use group permissions
505 * - proper sysexit codes
506 */
507
e0a95d28
MS
508int
509main(int argc, char **argv)
86e4d161
MS
510{
511 char *sender = NULL;
e0a95d28 512 struct qitem *it;
86e4d161
MS
513 struct queue queue;
514 struct queue lqueue;
86e4d161
MS
515 int i, ch;
516 int nodot = 0, doqueue = 0, showq = 0;
517
518 atexit(deltmp);
519 LIST_INIT(&queue.queue);
86e4d161 520
0f779ba6
SS
521 if (strcmp(argv[0], "mailq") == 0) {
522 argv++; argc--;
523 showq = 1;
524 if (argc != 0)
525 errx(1, "invalid arguments");
526 goto skipopts;
527 }
528
87fa1cc5 529 opterr = 0;
f514a0d9 530 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
86e4d161
MS
531 switch (ch) {
532 case 'A':
533 /* -AX is being ignored, except for -A{c,m} */
534 if (optarg[0] == 'c' || optarg[0] == 'm') {
535 break;
536 }
537 /* else FALLTRHOUGH */
538 case 'b':
539 /* -bX is being ignored, except for -bp */
540 if (optarg[0] == 'p') {
541 showq = 1;
542 break;
543 }
544 /* else FALLTRHOUGH */
545 case 'D':
546 daemonize = 0;
547 break;
548 case 'L':
4d5af2b0 549 logident_base = optarg;
86e4d161
MS
550 break;
551 case 'f':
552 case 'r':
553 sender = optarg;
554 break;
555
556 case 'o':
557 /* -oX is being ignored, except for -oi */
558 if (optarg[0] != 'i')
559 break;
560 /* else FALLTRHOUGH */
87fa1cc5
MS
561 case 'O':
562 break;
86e4d161
MS
563 case 'i':
564 nodot = 1;
565 break;
566
567 case 'q':
568 doqueue = 1;
569 break;
570
b6bb4e2d
SS
571 /* Ignored options */
572 case 'B':
573 case 'C':
574 case 'd':
575 case 'F':
576 case 'h':
577 case 'N':
578 case 'n':
579 case 'R':
580 case 'U':
581 case 'V':
582 case 'v':
583 case 'X':
584 break;
585
f514a0d9
SS
586 case ':':
587 if (optopt == 'q') {
588 doqueue = 1;
589 break;
590 }
591 /* FALLTHROUGH */
592
86e4d161 593 default:
f514a0d9 594 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
86e4d161
MS
595 exit(1);
596 }
597 }
598 argc -= optind;
599 argv += optind;
87fa1cc5 600 opterr = 1;
86e4d161 601
8d291e4d
SS
602 if (argc != 0 && (showq || doqueue))
603 errx(1, "sending mail and queue operations are mutually exclusive");
604
605 if (showq + doqueue > 1)
606 errx(1, "conflicting queue operations");
607
0f779ba6 608skipopts:
4d5af2b0
SS
609 if (logident_base == NULL)
610 logident_base = "dma";
611 setlogident(NULL);
dd561a09 612 set_username();
86e4d161 613
4602c807
SS
614 /* XXX fork root here */
615
8d291e4d 616 config = calloc(1, sizeof(*config));
86e4d161 617 if (config == NULL)
de13a881 618 errlog(1, NULL);
86e4d161 619
fd1de51a 620 if (parse_conf(CONF_PATH) < 0) {
86e4d161 621 free(config);
de13a881 622 errlog(1, "can not read config file");
86e4d161
MS
623 }
624
625 if (config->features & VIRTUAL)
e0a95d28 626 if (parse_virtuser(config->virtualpath) < 0)
de13a881 627 errlog(1, "can not read virtual user file `%s'",
86e4d161
MS
628 config->virtualpath);
629
e0a95d28 630 if (parse_authfile(config->authpath) < 0)
de13a881 631 errlog(1, "can not read SMTP authentication file");
86e4d161
MS
632
633 if (showq) {
de13a881
SS
634 if (load_queue(&lqueue) < 0)
635 errlog(1, "can not load queue");
86e4d161 636 show_queue(&lqueue);
e0a95d28 637 return (0);
86e4d161
MS
638 }
639
640 if (doqueue) {
de13a881
SS
641 if (load_queue(&lqueue) < 0)
642 errlog(1, "can not load queue");
86e4d161 643 run_queue(&lqueue);
e0a95d28 644 return (0);
86e4d161
MS
645 }
646
e0a95d28 647 if (read_aliases() != 0)
de13a881 648 errlog(1, "can not read aliases file `%s'", config->aliases);
86e4d161 649
e0a95d28 650 if ((sender = set_from(sender)) == NULL)
de13a881 651 errlog(1, NULL);
86e4d161
MS
652
653 for (i = 0; i < argc; i++) {
e0a95d28 654 if (add_recp(&queue, argv[i], sender, 1) != 0)
de13a881 655 errlogx(1, "invalid recipient `%s'", argv[i]);
86e4d161
MS
656 }
657
e0a95d28 658 if (LIST_EMPTY(&queue.queue))
de13a881 659 errlogx(1, "no recipients");
86e4d161 660
fd1de51a 661 if (newspoolf(&queue, sender) != 0)
de13a881 662 errlog(1, "can not create temp file");
e0a95d28 663
4d5af2b0
SS
664 setlogident("%s", queue.id);
665
e0a95d28 666 if (readmail(&queue, sender, nodot) != 0)
de13a881 667 errlog(1, "can not read mail");
86e4d161 668
87bdbc01 669 if (linkspool(&queue, sender) != 0)
de13a881 670 errlog(1, "can not create spools");
86e4d161
MS
671
672 /* From here on the mail is safe. */
673
674 if (config->features & DEFER)
e0a95d28 675 return (0);
86e4d161 676
e0a95d28
MS
677 it = go_background(&queue);
678 deliver(it);
86e4d161
MS
679
680 /* NOTREACHED */
e0a95d28 681 return (0);
94389971 682}