]> git.ipfire.org Git - people/ms/dma.git/blob - dma.c
dma: more loudly note that we should query for MX records
[people/ms/dma.git] / dma.c
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.
33 */
34
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40
41 #include <dirent.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <inttypes.h>
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
60 static void deliver(struct qitem *);
61
62 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
63 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
64 struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
65 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
66 struct config *config;
67 const char *username;
68 const char *logident_base;
69
70 static int daemonize = 1;
71
72 static char *
73 set_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) {
80 if (strcmp(v->login, username) == 0) {
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)
92 return (NULL);
93 } else {
94 if (asprintf(&sender, "%s@%s", username, hostname()) <= 0)
95 return (NULL);
96 }
97
98 if (strchr(sender, '\n') != NULL) {
99 errno = EINVAL;
100 return (NULL);
101 }
102
103 out:
104 return (sender);
105 }
106
107 static int
108 read_aliases(void)
109 {
110 yyin = fopen(config->aliases, "r");
111 if (yyin == NULL)
112 return (0); /* not fatal */
113 if (yyparse())
114 return (-1); /* fatal error, probably malloc() */
115 fclose(yyin);
116 return (0);
117 }
118
119 int
120 add_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)
131 return (-1);
132 it->addr = strdup(str);
133 if (it->addr == NULL)
134 return (-1);
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);
148 return (0);
149 }
150 }
151 LIST_INSERT_HEAD(&queue->queue, it, next);
152 if (strrchr(it->addr, '@') == NULL) {
153 it->remote = 0;
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) {
159 if (add_recp(queue, sit->str, sender, 1) != 0)
160 return (-1);
161 }
162 aliased = 1;
163 }
164 if (aliased) {
165 LIST_REMOVE(it, next);
166 } else {
167 /* Local destination, check */
168 pw = getpwnam(it->addr);
169 if (pw == NULL)
170 goto out;
171 /* XXX read .forward */
172 endpwent();
173 }
174 }
175 } else {
176 it->remote = 1;
177 }
178
179 return (0);
180
181 out:
182 free(it->addr);
183 free(it);
184 return (-1);
185 }
186
187 static int
188 readmail(struct queue *queue, const char *sender, int nodot)
189 {
190 char line[1000]; /* by RFC2822 */
191 size_t linelen;
192 size_t error;
193 int had_headers = 0;
194 int had_from = 0;
195 int had_messagid = 0;
196 int had_date = 0;
197
198 error = fprintf(queue->mailf,
199 "Received: from %s (uid %d)\n"
200 "\t(envelope-from %s)\n"
201 "\tid %s\n"
202 "\tby %s (%s)\n"
203 "\t%s\n",
204 username, getuid(),
205 sender,
206 queue->id,
207 hostname(), VERSION,
208 rfc822date());
209 if ((ssize_t)error < 0)
210 return (-1);
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 */
218 return (-1);
219 }
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;
237 snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
238 queue->id, hostname());
239 } else if (!had_from) {
240 had_from = 1;
241 snprintf(line, sizeof(line), "From: <%s>\n", sender);
242 }
243 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
244 return (-1);
245 }
246 strcpy(line, "\n");
247 }
248 if (!nodot && linelen == 2 && line[0] == '.')
249 break;
250 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
251 return (-1);
252 }
253
254 return (0);
255 }
256
257 static struct qitem *
258 go_background(struct queue *queue)
259 {
260 struct sigaction sa;
261 struct qitem *it;
262 pid_t pid;
263
264 if (daemonize && daemon(0, 0) != 0) {
265 syslog(LOG_ERR, "can not daemonize: %m");
266 exit(1);
267 }
268 daemonize = 0;
269
270 bzero(&sa, sizeof(sa));
271 sa.sa_flags = SA_NOCLDWAIT;
272 sa.sa_handler = SIG_IGN;
273 sigaction(SIGCHLD, &sa, NULL);
274
275 LIST_FOREACH(it, &queue->queue, next) {
276 /* No need to fork for the last dest */
277 if (LIST_NEXT(it, next) == NULL)
278 goto retit;
279
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 */
293 retit:
294 /*
295 * If necessary, aquire the queue and * mail files.
296 * If this fails, we probably were raced by another
297 * process.
298 */
299 setlogident("%s", it->queueid);
300 if (aquirespool(it) < 0)
301 exit(1);
302 dropspool(queue, it);
303 return (it);
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
319 static void
320 bounce(struct qitem *it, const char *reason)
321 {
322 struct queue bounceq;
323 struct qitem *bit;
324 char line[1000];
325 size_t pos;
326 int error;
327
328 /* Don't bounce bounced mails */
329 if (it->sender[0] == 0) {
330 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
331 exit(1);
332 }
333
334 LIST_INIT(&bounceq.queue);
335 if (add_recp(&bounceq, it->sender, "", 1) != 0)
336 goto fail;
337
338 if (newspoolf(&bounceq, "") != 0)
339 goto fail;
340
341 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
342 setlogident("%s", bounceq.id);
343
344 error = fprintf(bounceq.mailf,
345 "Received: from MAILER-DAEMON\n"
346 "\tid %s\n"
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"
353 "Message-Id: <%s@%s>\n"
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",
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,
373 reason,
374 config->features & FULLBOUNCE ?
375 "Original message follows." :
376 "Message headers follow.");
377 if (error < 0)
378 goto fail;
379
380 if (fseek(it->mailf, it->hdrlen, SEEK_SET) != 0)
381 goto fail;
382 if (config->features & FULLBOUNCE) {
383 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
384 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
385 goto fail;
386 }
387 } else {
388 while (!feof(it->mailf)) {
389 if (fgets(line, sizeof(line), it->mailf) == NULL)
390 break;
391 if (line[0] == '\n')
392 break;
393 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
394 goto fail;
395 }
396 }
397
398 if (linkspool(&bounceq, "") != 0)
399 goto fail;
400 /* bounce is safe */
401
402 delqueue(it);
403
404 bit = go_background(&bounceq);
405 deliver(bit);
406 /* NOTREACHED */
407
408 fail:
409 syslog(LOG_CRIT, "error creating bounce: %m");
410 delqueue(it);
411 exit(1);
412 }
413
414 static void
415 deliver(struct qitem *it)
416 {
417 int error;
418 unsigned int backoff = MIN_RETRY;
419 const char *errmsg = "unknown bounce reason";
420 struct timeval now;
421 struct stat st;
422
423 retry:
424 syslog(LOG_INFO, "trying delivery");
425
426 if (it->remote)
427 error = deliver_remote(it, &errmsg);
428 else
429 error = deliver_local(it, &errmsg);
430
431 switch (error) {
432 case 0:
433 delqueue(it);
434 syslog(LOG_INFO, "delivery successful");
435 exit(0);
436
437 case 1:
438 if (stat(it->queuefn, &st) != 0) {
439 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
440 exit(1);
441 }
442 if (gettimeofday(&now, NULL) == 0 &&
443 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
444 asprintf(__DECONST(void *, &errmsg),
445 "Could not deliver for the last %d seconds. Giving up.",
446 MAX_TIMEOUT);
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
460 bounce:
461 bounce(it, errmsg);
462 /* NOTREACHED */
463 }
464
465 static void
466 run_queue(struct queue *queue)
467 {
468 struct qitem *it;
469
470 if (LIST_EMPTY(&queue->queue))
471 return;
472
473 it = go_background(queue);
474 deliver(it);
475 /* NOTREACHED */
476 }
477
478 static void
479 show_queue(struct queue *queue)
480 {
481 struct qitem *it;
482 int locked = 0; /* XXX */
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) {
490 printf("ID\t: %s%s\n"
491 "From\t: %s\n"
492 "To\t: %s\n"
493 "--\n",
494 it->queueid,
495 locked ? "*" : "",
496 it->sender, it->addr);
497 }
498 }
499
500 /*
501 * TODO:
502 *
503 * - alias processing
504 * - use group permissions
505 * - proper sysexit codes
506 */
507
508 int
509 main(int argc, char **argv)
510 {
511 char *sender = NULL;
512 struct qitem *it;
513 struct queue queue;
514 struct queue lqueue;
515 int i, ch;
516 int nodot = 0, doqueue = 0, showq = 0;
517
518 atexit(deltmp);
519 LIST_INIT(&queue.queue);
520
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
529 opterr = 0;
530 while ((ch = getopt(argc, argv, "A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
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':
549 logident_base = optarg;
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 */
561 case 'O':
562 break;
563 case 'i':
564 nodot = 1;
565 break;
566
567 case 'q':
568 doqueue = 1;
569 break;
570
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
586 default:
587 exit(1);
588 }
589 }
590 argc -= optind;
591 argv += optind;
592 opterr = 1;
593
594 if (argc != 0 && (showq || doqueue))
595 errx(1, "sending mail and queue operations are mutually exclusive");
596
597 if (showq + doqueue > 1)
598 errx(1, "conflicting queue operations");
599
600 skipopts:
601 if (logident_base == NULL)
602 logident_base = "dma";
603 setlogident(NULL);
604 set_username();
605
606 /* XXX fork root here */
607
608 config = calloc(1, sizeof(*config));
609 if (config == NULL)
610 errlog(1, NULL);
611
612 if (parse_conf(CONF_PATH) < 0) {
613 free(config);
614 errlog(1, "can not read config file");
615 }
616
617 if (config->features & VIRTUAL)
618 if (parse_virtuser(config->virtualpath) < 0)
619 errlog(1, "can not read virtual user file `%s'",
620 config->virtualpath);
621
622 if (parse_authfile(config->authpath) < 0)
623 errlog(1, "can not read SMTP authentication file");
624
625 if (showq) {
626 if (load_queue(&lqueue) < 0)
627 errlog(1, "can not load queue");
628 show_queue(&lqueue);
629 return (0);
630 }
631
632 if (doqueue) {
633 if (load_queue(&lqueue) < 0)
634 errlog(1, "can not load queue");
635 run_queue(&lqueue);
636 return (0);
637 }
638
639 if (read_aliases() != 0)
640 errlog(1, "can not read aliases file `%s'", config->aliases);
641
642 if ((sender = set_from(sender)) == NULL)
643 errlog(1, NULL);
644
645 for (i = 0; i < argc; i++) {
646 if (add_recp(&queue, argv[i], sender, 1) != 0)
647 errlogx(1, "invalid recipient `%s'", argv[i]);
648 }
649
650 if (LIST_EMPTY(&queue.queue))
651 errlogx(1, "no recipients");
652
653 if (newspoolf(&queue, sender) != 0)
654 errlog(1, "can not create temp file");
655
656 setlogident("%s", queue.id);
657
658 if (readmail(&queue, sender, nodot) != 0)
659 errlog(1, "can not read mail");
660
661 if (linkspool(&queue, sender) != 0)
662 errlog(1, "can not create spools");
663
664 /* From here on the mail is safe. */
665
666 if (config->features & DEFER)
667 return (0);
668
669 it = go_background(&queue);
670 deliver(it);
671
672 /* NOTREACHED */
673 return (0);
674 }