]> git.ipfire.org Git - people/ms/dma.git/blob - dma.c
dma: add FULLBOUNCE config option
[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 * $DragonFly: src/libexec/dma/dma.c,v 1.5 2008/09/30 17:47:21 swildner Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42
43 #ifdef HAVE_CRYPTO
44 #include <openssl/ssl.h>
45 #endif /* HAVE_CRYPTO */
46
47 #include <dirent.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <inttypes.h>
52 #include <netdb.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <syslog.h>
61 #include <unistd.h>
62
63 #include "dma.h"
64
65
66
67 static void deliver(struct qitem *);
68 static int add_recp(struct queue *, const char *, const char *, int);
69
70 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
71 static struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
72 struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
73 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
74 static int daemonize = 1;
75 struct config *config;
76 static struct strlist seenmsg[16][16];
77
78
79 char *
80 hostname(void)
81 {
82 static char name[MAXHOSTNAMELEN+1];
83
84 if (gethostname(name, sizeof(name)) != 0)
85 strcpy(name, "(unknown hostname)");
86
87 return name;
88 }
89
90 static char *
91 set_from(const char *osender)
92 {
93 struct virtuser *v;
94 char *sender;
95
96 if ((config->features & VIRTUAL) != 0) {
97 SLIST_FOREACH(v, &virtusers, next) {
98 if (strcmp(v->login, getlogin()) == 0) {
99 sender = strdup(v->address);
100 if (sender == NULL)
101 return(NULL);
102 goto out;
103 }
104 }
105 }
106
107 if (osender) {
108 sender = strdup(osender);
109 if (sender == NULL)
110 return (NULL);
111 } else {
112 if (asprintf(&sender, "%s@%s", getlogin(), hostname()) <= 0)
113 return (NULL);
114 }
115
116 if (strchr(sender, '\n') != NULL) {
117 errno = EINVAL;
118 return (NULL);
119 }
120
121 out:
122 return (sender);
123 }
124
125 static int
126 read_aliases(void)
127 {
128 yyin = fopen(config->aliases, "r");
129 if (yyin == NULL)
130 return (0); /* not fatal */
131 if (yyparse())
132 return (-1); /* fatal error, probably malloc() */
133 fclose(yyin);
134 return (0);
135 }
136
137 static int
138 add_recp(struct queue *queue, const char *str, const char *sender, int expand)
139 {
140 struct qitem *it, *tit;
141 struct stritem *sit;
142 struct alias *al;
143 struct passwd *pw;
144 char *host;
145 int aliased = 0;
146
147 it = calloc(1, sizeof(*it));
148 if (it == NULL)
149 return (-1);
150 it->addr = strdup(str);
151 if (it->addr == NULL)
152 return (-1);
153
154 it->sender = sender;
155 host = strrchr(it->addr, '@');
156 if (host != NULL &&
157 (strcmp(host + 1, hostname()) == 0 ||
158 strcmp(host + 1, "localhost") == 0)) {
159 *host = 0;
160 }
161 LIST_FOREACH(tit, &queue->queue, next) {
162 /* weed out duplicate dests */
163 if (strcmp(tit->addr, it->addr) == 0) {
164 free(it->addr);
165 free(it);
166 return (0);
167 }
168 }
169 LIST_INSERT_HEAD(&queue->queue, it, next);
170 if (strrchr(it->addr, '@') == NULL) {
171 it->remote = 0;
172 if (expand) {
173 LIST_FOREACH(al, &aliases, next) {
174 if (strcmp(al->alias, it->addr) != 0)
175 continue;
176 SLIST_FOREACH(sit, &al->dests, next) {
177 if (add_recp(queue, sit->str, sender, 1) != 0)
178 return (-1);
179 }
180 aliased = 1;
181 }
182 if (aliased) {
183 LIST_REMOVE(it, next);
184 } else {
185 /* Local destination, check */
186 pw = getpwnam(it->addr);
187 if (pw == NULL)
188 goto out;
189 endpwent();
190 }
191 }
192 } else {
193 it->remote = 1;
194 }
195
196 return (0);
197
198 out:
199 free(it->addr);
200 free(it);
201 return (-1);
202 }
203
204 static void
205 deltmp(void)
206 {
207 struct stritem *t;
208
209 SLIST_FOREACH(t, &tmpfs, next) {
210 unlink(t->str);
211 }
212 }
213
214 static int
215 gentempf(struct queue *queue)
216 {
217 char fn[PATH_MAX+1];
218 struct stritem *t;
219 int fd;
220
221 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
222 return (-1);
223 fd = mkstemp(fn);
224 if (fd < 0)
225 return (-1);
226 if (flock(fd, LOCK_EX) == -1)
227 return (-1);
228 queue->mailfd = fd;
229 queue->tmpf = strdup(fn);
230 if (queue->tmpf == NULL) {
231 unlink(fn);
232 return (-1);
233 }
234 t = malloc(sizeof(*t));
235 if (t != NULL) {
236 t->str = queue->tmpf;
237 SLIST_INSERT_HEAD(&tmpfs, t, next);
238 }
239 return (0);
240 }
241
242 static int
243 open_locked(const char *fname, int flags)
244 {
245 #ifndef O_EXLOCK
246 int fd, save_errno;
247
248 fd = open(fname, flags, 0);
249 if (fd < 0)
250 return(fd);
251 if (flock(fd, LOCK_EX|((flags & O_NONBLOCK)? LOCK_NB: 0)) < 0) {
252 save_errno = errno;
253 close(fd);
254 errno = save_errno;
255 return(-1);
256 }
257 return(fd);
258 #else
259 return(open(fname, flags|O_EXLOCK));
260 #endif
261 }
262
263 /*
264 * spool file format:
265 *
266 * envelope-from
267 * queue-id1 envelope-to1
268 * queue-id2 envelope-to2
269 * ...
270 * <empty line>
271 * mail data
272 *
273 * queue ids are unique, formed from the inode of the spool file
274 * and a unique identifier.
275 */
276 static int
277 preparespool(struct queue *queue, const char *sender)
278 {
279 char line[1000]; /* by RFC2822 */
280 struct stat st;
281 int error;
282 struct qitem *it;
283 FILE *queuef;
284 off_t hdrlen;
285
286 error = snprintf(line, sizeof(line), "%s\n", sender);
287 if (error < 0 || (size_t)error >= sizeof(line)) {
288 errno = E2BIG;
289 return (-1);
290 }
291 if (write(queue->mailfd, line, error) != error)
292 return (-1);
293
294 queuef = fdopen(queue->mailfd, "r+");
295 if (queuef == NULL)
296 return (-1);
297
298 /*
299 * Assign queue id to each dest.
300 */
301 if (fstat(queue->mailfd, &st) != 0)
302 return (-1);
303 queue->id = st.st_ino;
304 LIST_FOREACH(it, &queue->queue, next) {
305 if (asprintf(&it->queueid, "%"PRIxMAX".%"PRIxPTR,
306 queue->id, (uintptr_t)it) <= 0)
307 return (-1);
308 if (asprintf(&it->queuefn, "%s/%s",
309 config->spooldir, it->queueid) <= 0)
310 return (-1);
311 /* File may not exist yet */
312 if (stat(it->queuefn, &st) == 0)
313 return (-1);
314 it->queuef = queuef;
315 error = snprintf(line, sizeof(line), "%s %s\n",
316 it->queueid, it->addr);
317 if (error < 0 || (size_t)error >= sizeof(line))
318 return (-1);
319 if (write(queue->mailfd, line, error) != error)
320 return (-1);
321 }
322 line[0] = '\n';
323 if (write(queue->mailfd, line, 1) != 1)
324 return (-1);
325
326 hdrlen = lseek(queue->mailfd, 0, SEEK_CUR);
327 LIST_FOREACH(it, &queue->queue, next) {
328 it->hdrlen = hdrlen;
329 }
330 return (0);
331 }
332
333 static char *
334 rfc822date(void)
335 {
336 static char str[50];
337 size_t error;
338 time_t now;
339
340 now = time(NULL);
341 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
342 localtime(&now));
343 if (error == 0)
344 strcpy(str, "(date fail)");
345 return (str);
346 }
347
348 static int
349 readmail(struct queue *queue, const char *sender, int nodot)
350 {
351 char line[1000]; /* by RFC2822 */
352 size_t linelen;
353 int error;
354
355 error = snprintf(line, sizeof(line), "\
356 Received: from %s (uid %d)\n\
357 \t(envelope-from %s)\n\
358 \tid %"PRIxMAX"\n\
359 \tby %s (%s)\n\
360 \t%s\n",
361 getlogin(), getuid(),
362 sender,
363 queue->id,
364 hostname(), VERSION,
365 rfc822date());
366 if (error < 0 || (size_t)error >= sizeof(line))
367 return (-1);
368 if (write(queue->mailfd, line, error) != error)
369 return (-1);
370
371 while (!feof(stdin)) {
372 if (fgets(line, sizeof(line), stdin) == NULL)
373 break;
374 linelen = strlen(line);
375 if (linelen == 0 || line[linelen - 1] != '\n') {
376 errno = EINVAL; /* XXX mark permanent errors */
377 return (-1);
378 }
379 if (!nodot && linelen == 2 && line[0] == '.')
380 break;
381 if ((size_t)write(queue->mailfd, line, linelen) != linelen)
382 return (-1);
383 }
384 if (fsync(queue->mailfd) != 0)
385 return (-1);
386 return (0);
387 }
388
389 static int
390 linkspool(struct queue *queue)
391 {
392 struct qitem *it;
393
394 LIST_FOREACH(it, &queue->queue, next) {
395 if (link(queue->tmpf, it->queuefn) != 0)
396 goto delfiles;
397 }
398 unlink(queue->tmpf);
399 return (0);
400
401 delfiles:
402 LIST_FOREACH(it, &queue->queue, next) {
403 unlink(it->queuefn);
404 }
405 return (-1);
406 }
407
408 static struct qitem *
409 go_background(struct queue *queue)
410 {
411 struct sigaction sa;
412 struct qitem *it;
413 pid_t pid;
414
415 if (daemonize && daemon(0, 0) != 0) {
416 syslog(LOG_ERR, "can not daemonize: %m");
417 exit(1);
418 }
419 daemonize = 0;
420
421 bzero(&sa, sizeof(sa));
422 sa.sa_flags = SA_NOCLDWAIT;
423 sa.sa_handler = SIG_IGN;
424 sigaction(SIGCHLD, &sa, NULL);
425
426 LIST_FOREACH(it, &queue->queue, next) {
427 /* No need to fork for the last dest */
428 if (LIST_NEXT(it, next) == NULL)
429 return (it);
430
431 pid = fork();
432 switch (pid) {
433 case -1:
434 syslog(LOG_ERR, "can not fork: %m");
435 exit(1);
436 break;
437
438 case 0:
439 /*
440 * Child:
441 *
442 * return and deliver mail
443 */
444 return (it);
445
446 default:
447 /*
448 * Parent:
449 *
450 * fork next child
451 */
452 break;
453 }
454 }
455
456 syslog(LOG_CRIT, "reached dead code");
457 exit(1);
458 }
459
460 static void
461 bounce(struct qitem *it, char *reason)
462 {
463 struct queue bounceq;
464 struct qitem *bit;
465 char line[1000];
466 size_t pos;
467 int error;
468
469 /* Don't bounce bounced mails */
470 if (it->sender[0] == 0) {
471 syslog(LOG_CRIT, "%s: delivery panic: can't bounce a bounce",
472 it->queueid);
473 exit(1);
474 }
475
476 syslog(LOG_ERR, "%s: delivery failed, bouncing",
477 it->queueid);
478
479 LIST_INIT(&bounceq.queue);
480 if (add_recp(&bounceq, it->sender, "", 1) != 0)
481 goto fail;
482 if (gentempf(&bounceq) != 0)
483 goto fail;
484 if (preparespool(&bounceq, "") != 0)
485 goto fail;
486
487 bit = LIST_FIRST(&bounceq.queue);
488 error = fprintf(bit->queuef, "\
489 Received: from MAILER-DAEMON\n\
490 \tid %"PRIxMAX"\n\
491 \tby %s (%s)\n\
492 \t%s\n\
493 X-Original-To: <%s>\n\
494 From: MAILER-DAEMON <>\n\
495 To: %s\n\
496 Subject: Mail delivery failed\n\
497 Message-Id: <%"PRIxMAX"@%s>\n\
498 Date: %s\n\
499 \n\
500 This is the %s at %s.\n\
501 \n\
502 There was an error delivering your mail to <%s>.\n\
503 \n\
504 %s\n\
505 \n\
506 %s\n\
507 \n\
508 ",
509 bounceq.id,
510 hostname(), VERSION,
511 rfc822date(),
512 it->addr,
513 it->sender,
514 bounceq.id, hostname(),
515 rfc822date(),
516 VERSION, hostname(),
517 it->addr,
518 reason,
519 config->features & FULLBOUNCE? "Original message follows.":
520 "Message headers follow.");
521 free(reason);
522 if (error < 0)
523 goto fail;
524 if (fflush(bit->queuef) != 0)
525 goto fail;
526
527 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0)
528 goto fail;
529 if (config->features & FULLBOUNCE) {
530 while ((pos = fread(line, 1, sizeof(line), it->queuef)) > 0) {
531 if ((size_t)write(bounceq.mailfd, line, pos) != pos)
532 goto fail;
533 }
534 } else {
535 while (!feof(it->queuef)) {
536 if (fgets(line, sizeof(line), it->queuef) == NULL)
537 break;
538 if (line[0] == '\n')
539 break;
540 if ((size_t)write(bounceq.mailfd, line, strlen(line)) != strlen(line))
541 goto fail;
542 }
543 }
544 if (fsync(bounceq.mailfd) != 0)
545 goto fail;
546 if (linkspool(&bounceq) != 0)
547 goto fail;
548 /* bounce is safe */
549
550 unlink(it->queuefn);
551 fclose(it->queuef);
552
553 bit = go_background(&bounceq);
554 deliver(bit);
555 /* NOTREACHED */
556
557 fail:
558 syslog(LOG_CRIT, "%s: error creating bounce: %m", it->queueid);
559 unlink(it->queuefn);
560 exit(1);
561 }
562
563 static int
564 deliver_local(struct qitem *it, char **errmsg)
565 {
566 char fn[PATH_MAX+1];
567 char line[1000];
568 size_t linelen;
569 int mbox;
570 int error;
571 off_t mboxlen;
572 time_t now = time(NULL);
573
574 error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, it->addr);
575 if (error < 0 || (size_t)error >= sizeof(fn)) {
576 syslog(LOG_ERR, "%s: local delivery deferred: %m",
577 it->queueid);
578 return (1);
579 }
580
581 /* mailx removes users mailspool file if empty, so open with O_CREAT */
582 mbox = open_locked(fn, O_WRONLY | O_APPEND | O_CREAT);
583 if (mbox < 0) {
584 syslog(LOG_ERR, "%s: local delivery deferred: can not open `%s': %m",
585 it->queueid, fn);
586 return (1);
587 }
588 mboxlen = lseek(mbox, 0, SEEK_CUR);
589
590 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
591 syslog(LOG_ERR, "%s: local delivery deferred: can not seek: %m",
592 it->queueid);
593 return (1);
594 }
595
596 error = snprintf(line, sizeof(line), "From %s\t%s", it->sender, ctime(&now));
597 if (error < 0 || (size_t)error >= sizeof(line)) {
598 syslog(LOG_ERR, "%s: local delivery deferred: can not write header: %m",
599 it->queueid);
600 return (1);
601 }
602 if (write(mbox, line, error) != error)
603 goto wrerror;
604
605 while (!feof(it->queuef)) {
606 if (fgets(line, sizeof(line), it->queuef) == NULL)
607 break;
608 linelen = strlen(line);
609 if (linelen == 0 || line[linelen - 1] != '\n') {
610 syslog(LOG_CRIT, "%s: local delivery failed: corrupted queue file",
611 it->queueid);
612 *errmsg = strdup("corrupted queue file");
613 error = -1;
614 goto chop;
615 }
616
617 if (strncmp(line, "From ", 5) == 0) {
618 const char *gt = ">";
619
620 if (write(mbox, gt, 1) != 1)
621 goto wrerror;
622 }
623 if ((size_t)write(mbox, line, linelen) != linelen)
624 goto wrerror;
625 }
626 line[0] = '\n';
627 if (write(mbox, line, 1) != 1)
628 goto wrerror;
629 close(mbox);
630 return (0);
631
632 wrerror:
633 syslog(LOG_ERR, "%s: local delivery failed: write error: %m",
634 it->queueid);
635 error = 1;
636 chop:
637 if (ftruncate(mbox, mboxlen) != 0)
638 syslog(LOG_WARNING, "%s: error recovering mbox `%s': %m",
639 it->queueid, fn);
640 close(mbox);
641 return (error);
642 }
643
644 static void
645 deliver(struct qitem *it)
646 {
647 int error;
648 unsigned int backoff = MIN_RETRY;
649 char *errmsg = strdup("unknown bounce reason");
650 struct timeval now;
651 struct stat st;
652
653 syslog(LOG_INFO, "%s: mail from=<%s> to=<%s>",
654 it->queueid, it->sender, it->addr);
655
656 retry:
657 syslog(LOG_INFO, "%s: trying delivery",
658 it->queueid);
659
660 if (it->remote)
661 error = deliver_remote(it, &errmsg);
662 else
663 error = deliver_local(it, &errmsg);
664
665 switch (error) {
666 case 0:
667 unlink(it->queuefn);
668 syslog(LOG_INFO, "%s: delivery successful",
669 it->queueid);
670 exit(0);
671
672 case 1:
673 if (stat(it->queuefn, &st) != 0) {
674 syslog(LOG_ERR, "%s: lost queue file `%s'",
675 it->queueid, it->queuefn);
676 exit(1);
677 }
678 if (gettimeofday(&now, NULL) == 0 &&
679 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
680 asprintf(&errmsg,
681 "Could not deliver for the last %d seconds. Giving up.",
682 MAX_TIMEOUT);
683 goto bounce;
684 }
685 sleep(backoff);
686 backoff *= 2;
687 if (backoff > MAX_RETRY)
688 backoff = MAX_RETRY;
689 goto retry;
690
691 case -1:
692 default:
693 break;
694 }
695
696 bounce:
697 bounce(it, errmsg);
698 /* NOTREACHED */
699 }
700
701 static int
702 c2x(char c)
703 {
704 if (c <= '9')
705 return (c - '0');
706 else if (c <= 'F')
707 return (c - 'A' + 10);
708 else
709 return (c - 'a' + 10);
710 }
711
712 static void
713 seen_init(void)
714 {
715 int i, j;
716
717 for (i = 0; i < 16; i++)
718 for (j = 0; j < 16; j++)
719 SLIST_INIT(&seenmsg[i][j]);
720 }
721
722 static int
723 seen(const char *msgid)
724 {
725 const char *p;
726 size_t len;
727 int i, j;
728 struct stritem *t;
729
730 p = strchr(msgid, '.');
731 if (p == NULL)
732 return (0);
733 len = p - msgid;
734 if (len >= 2) {
735 i = c2x(msgid[len - 2]);
736 j = c2x(msgid[len - 1]);
737 } else if (len == 1) {
738 i = c2x(msgid[0]);
739 j = 0;
740 } else {
741 i = j = 0;
742 }
743 if (i < 0 || i >= 16 || j < 0 || j >= 16)
744 errx(1, "INTERNAL ERROR: bad seen code for msgid %s", msgid);
745 SLIST_FOREACH(t, &seenmsg[i][j], next)
746 if (!strncmp(t->str, msgid, len))
747 return (1);
748 t = malloc(sizeof(*t));
749 if (t == NULL)
750 errx(1, "Could not allocate %lu bytes",
751 (unsigned long)(sizeof(*t)));
752 t->str = strdup(msgid);
753 if (t->str == NULL)
754 errx(1, "Could not duplicate msgid %s", msgid);
755 SLIST_INSERT_HEAD(&seenmsg[i][j], t, next);
756 return (0);
757 }
758
759 static void
760 load_queue(struct queue *queue, int ignorelock)
761 {
762 struct stat st;
763 struct qitem *it;
764 //struct queue queue, itmqueue;
765 struct queue itmqueue;
766 DIR *spooldir;
767 struct dirent *de;
768 char line[1000];
769 char *fn;
770 FILE *queuef;
771 char *sender;
772 char *addr;
773 char *queueid;
774 char *queuefn;
775 off_t hdrlen;
776 int fd, locked, seenit;
777
778 LIST_INIT(&queue->queue);
779
780 spooldir = opendir(config->spooldir);
781 if (spooldir == NULL)
782 err(1, "reading queue");
783
784 seen_init();
785 while ((de = readdir(spooldir)) != NULL) {
786 sender = NULL;
787 queuef = NULL;
788 queueid = NULL;
789 queuefn = NULL;
790 fn = NULL;
791 LIST_INIT(&itmqueue.queue);
792
793 /* ignore temp files */
794 if (strncmp(de->d_name, "tmp_", 4) == 0 ||
795 de->d_type != DT_REG)
796 continue;
797 if (asprintf(&queuefn, "%s/%s", config->spooldir, de->d_name) < 0)
798 goto fail;
799 seenit = seen(de->d_name);
800 locked = 0;
801 fd = open_locked(queuefn, O_RDONLY|O_NONBLOCK);
802 if (fd < 0) {
803 /* Ignore locked files */
804 if (errno != EWOULDBLOCK)
805 goto skip_item;
806 if (!ignorelock || seenit)
807 continue;
808 fd = open(queuefn, O_RDONLY);
809 if (fd < 0)
810 goto skip_item;
811 locked = 1;
812 }
813
814 queuef = fdopen(fd, "r");
815 if (queuef == NULL)
816 goto skip_item;
817 if (fgets(line, sizeof(line), queuef) == NULL ||
818 line[0] == 0)
819 goto skip_item;
820 line[strlen(line) - 1] = 0; /* chop newline */
821 sender = strdup(line);
822 if (sender == NULL)
823 goto skip_item;
824
825 for (;;) {
826 if (fgets(line, sizeof(line), queuef) == NULL ||
827 line[0] == 0)
828 goto skip_item;
829 if (line[0] == '\n')
830 break;
831 line[strlen(line) - 1] = 0;
832 queueid = strdup(line);
833 if (queueid == NULL)
834 goto skip_item;
835 addr = strchr(queueid, ' ');
836 if (addr == NULL)
837 goto skip_item;
838 *addr++ = 0;
839 if (fn != NULL)
840 free(fn);
841 if (asprintf(&fn, "%s/%s", config->spooldir, queueid) < 0)
842 goto skip_item;
843 /* Item has already been delivered? */
844 if (stat(fn, &st) != 0)
845 continue;
846 if (add_recp(&itmqueue, addr, sender, 0) != 0)
847 goto skip_item;
848 it = LIST_FIRST(&itmqueue.queue);
849 it->queuef = queuef;
850 it->queueid = queueid;
851 it->queuefn = fn;
852 it->locked = locked;
853 fn = NULL;
854 }
855 if (LIST_EMPTY(&itmqueue.queue)) {
856 warnx("queue file without items: `%s'", queuefn);
857 goto skip_item2;
858 }
859 hdrlen = ftell(queuef);
860 while ((it = LIST_FIRST(&itmqueue.queue)) != NULL) {
861 it->hdrlen = hdrlen;
862 LIST_REMOVE(it, next);
863 LIST_INSERT_HEAD(&queue->queue, it, next);
864 }
865 continue;
866
867 skip_item:
868 warn("reading queue: `%s'", queuefn);
869 skip_item2:
870 if (sender != NULL)
871 free(sender);
872 if (queuefn != NULL)
873 free(queuefn);
874 if (fn != NULL)
875 free(fn);
876 if (queueid != NULL)
877 free(queueid);
878 close(fd);
879 }
880 closedir(spooldir);
881 return;
882
883 fail:
884 err(1, "reading queue");
885 }
886
887 static void
888 run_queue(struct queue *queue)
889 {
890 struct qitem *it;
891
892 if (LIST_EMPTY(&queue->queue))
893 return;
894
895 it = go_background(queue);
896 deliver(it);
897 /* NOTREACHED */
898 }
899
900 static void
901 show_queue(struct queue *queue)
902 {
903 struct qitem *it;
904
905 if (LIST_EMPTY(&queue->queue)) {
906 printf("Mail queue is empty\n");
907 return;
908 }
909
910 LIST_FOREACH(it, &queue->queue, next) {
911 printf("\
912 ID\t: %s%s\n\
913 From\t: %s\n\
914 To\t: %s\n--\n", it->queueid, it->locked? "*": "", it->sender, it->addr);
915 }
916 }
917
918 /*
919 * TODO:
920 *
921 * - alias processing
922 * - use group permissions
923 * - proper sysexit codes
924 */
925
926 int
927 main(int argc, char **argv)
928 {
929 char *sender = NULL;
930 char tag[255];
931 struct qitem *it;
932 struct queue queue;
933 struct queue lqueue;
934 int i, ch;
935 int nodot = 0, doqueue = 0, showq = 0;
936
937 atexit(deltmp);
938 LIST_INIT(&queue.queue);
939 snprintf(tag, 254, "dma");
940
941 opterr = 0;
942 while ((ch = getopt(argc, argv, "A:b:Df:iL:o:O:q:r:")) != -1) {
943 switch (ch) {
944 case 'A':
945 /* -AX is being ignored, except for -A{c,m} */
946 if (optarg[0] == 'c' || optarg[0] == 'm') {
947 break;
948 }
949 /* else FALLTRHOUGH */
950 case 'b':
951 /* -bX is being ignored, except for -bp */
952 if (optarg[0] == 'p') {
953 showq = 1;
954 break;
955 }
956 /* else FALLTRHOUGH */
957 case 'D':
958 daemonize = 0;
959 break;
960 case 'L':
961 if (optarg != NULL)
962 snprintf(tag, 254, "%s", optarg);
963 break;
964 case 'f':
965 case 'r':
966 sender = optarg;
967 break;
968
969 case 'o':
970 /* -oX is being ignored, except for -oi */
971 if (optarg[0] != 'i')
972 break;
973 /* else FALLTRHOUGH */
974 case 'O':
975 break;
976 case 'i':
977 nodot = 1;
978 break;
979
980 case 'q':
981 doqueue = 1;
982 break;
983
984 default:
985 exit(1);
986 }
987 }
988 argc -= optind;
989 argv += optind;
990 opterr = 1;
991
992 openlog(tag, LOG_PID | LOG_PERROR, LOG_MAIL);
993
994 config = malloc(sizeof(struct config));
995 if (config == NULL)
996 errx(1, "Cannot allocate enough memory");
997
998 memset(config, 0, sizeof(struct config));
999 if (parse_conf(CONF_PATH, config) < 0) {
1000 free(config);
1001 errx(1, "reading config file");
1002 }
1003
1004 if (config->features & VIRTUAL)
1005 if (parse_virtuser(config->virtualpath) < 0)
1006 errx(1, "error reading virtual user file: %s",
1007 config->virtualpath);
1008
1009 if (parse_authfile(config->authpath) < 0)
1010 err(1, "reading SMTP authentication file");
1011
1012 if (showq) {
1013 if (argc != 0)
1014 errx(1, "sending mail and displaying queue is"
1015 " mutually exclusive");
1016 load_queue(&lqueue, 1);
1017 show_queue(&lqueue);
1018 return (0);
1019 }
1020
1021 if (doqueue) {
1022 if (argc != 0)
1023 errx(1, "sending mail and queue pickup is mutually exclusive");
1024 load_queue(&lqueue, 0);
1025 run_queue(&lqueue);
1026 return (0);
1027 }
1028
1029 if (read_aliases() != 0)
1030 err(1, "reading aliases");
1031
1032 if ((sender = set_from(sender)) == NULL)
1033 err(1, "setting from address");
1034
1035 for (i = 0; i < argc; i++) {
1036 if (add_recp(&queue, argv[i], sender, 1) != 0)
1037 errx(1, "invalid recipient `%s'\n", argv[i]);
1038 }
1039
1040 if (LIST_EMPTY(&queue.queue))
1041 errx(1, "no recipients");
1042
1043 if (gentempf(&queue) != 0)
1044 err(1, "create temp file");
1045
1046 if (preparespool(&queue, sender) != 0)
1047 err(1, "creating spools (1)");
1048
1049 if (readmail(&queue, sender, nodot) != 0)
1050 err(1, "reading mail");
1051
1052 if (linkspool(&queue) != 0)
1053 err(1, "creating spools (2)");
1054
1055 /* From here on the mail is safe. */
1056
1057 if (config->features & DEFER)
1058 return (0);
1059
1060 it = go_background(&queue);
1061 deliver(it);
1062
1063 /* NOTREACHED */
1064 return (0);
1065 }