hammer.8: Fix typo.
[dragonfly.git] / libexec / dma / dma.c
blob511b936fd663bce5624d095a6b8e2a4885fb4524
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
34 * $DragonFly: src/libexec/dma/dma.c,v 1.5 2008/09/30 17:47:21 swildner Exp $
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>
43 #ifdef HAVE_CRYPTO
44 #include <openssl/ssl.h>
45 #endif /* HAVE_CRYPTO */
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>
63 #include "dma.h"
67 static void deliver(struct qitem *);
68 static int add_recp(struct queue *, const char *, const char *, int);
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;
77 char *
78 hostname(void)
80 static char name[MAXHOSTNAMELEN+1];
82 if (gethostname(name, sizeof(name)) != 0)
83 strcpy(name, "(unknown hostname)");
85 return name;
88 static char *
89 set_from(const char *osender)
91 struct virtuser *v;
92 char *sender;
94 if ((config->features & VIRTUAL) != 0) {
95 SLIST_FOREACH(v, &virtusers, next) {
96 if (strcmp(v->login, getlogin()) == 0) {
97 sender = strdup(v->address);
98 if (sender == NULL)
99 return(NULL);
100 goto out;
105 if (osender) {
106 sender = strdup(osender);
107 if (sender == NULL)
108 return (NULL);
109 } else {
110 if (asprintf(&sender, "%s@%s", getlogin(), hostname()) <= 0)
111 return (NULL);
114 if (strchr(sender, '\n') != NULL) {
115 errno = EINVAL;
116 return (NULL);
119 out:
120 return (sender);
123 static int
124 read_aliases(void)
126 yyin = fopen(config->aliases, "r");
127 if (yyin == NULL)
128 return (0); /* not fatal */
129 if (yyparse())
130 return (-1); /* fatal error, probably malloc() */
131 fclose(yyin);
132 return (0);
135 static int
136 add_recp(struct queue *queue, const char *str, const char *sender, int expand)
138 struct qitem *it, *tit;
139 struct stritem *sit;
140 struct alias *al;
141 struct passwd *pw;
142 char *host;
143 int aliased = 0;
145 it = calloc(1, sizeof(*it));
146 if (it == NULL)
147 return (-1);
148 it->addr = strdup(str);
149 if (it->addr == NULL)
150 return (-1);
152 it->sender = sender;
153 host = strrchr(it->addr, '@');
154 if (host != NULL &&
155 (strcmp(host + 1, hostname()) == 0 ||
156 strcmp(host + 1, "localhost") == 0)) {
157 *host = 0;
159 LIST_FOREACH(tit, &queue->queue, next) {
160 /* weed out duplicate dests */
161 if (strcmp(tit->addr, it->addr) == 0) {
162 free(it->addr);
163 free(it);
164 return (0);
167 LIST_INSERT_HEAD(&queue->queue, it, next);
168 if (strrchr(it->addr, '@') == NULL) {
169 it->remote = 0;
170 if (expand) {
171 LIST_FOREACH(al, &aliases, next) {
172 if (strcmp(al->alias, it->addr) != 0)
173 continue;
174 SLIST_FOREACH(sit, &al->dests, next) {
175 if (add_recp(queue, sit->str, sender, 1) != 0)
176 return (-1);
178 aliased = 1;
180 if (aliased) {
181 LIST_REMOVE(it, next);
182 } else {
183 /* Local destination, check */
184 pw = getpwnam(it->addr);
185 if (pw == NULL)
186 goto out;
187 endpwent();
190 } else {
191 it->remote = 1;
194 return (0);
196 out:
197 free(it->addr);
198 free(it);
199 return (-1);
202 static void
203 deltmp(void)
205 struct stritem *t;
207 SLIST_FOREACH(t, &tmpfs, next) {
208 unlink(t->str);
212 static int
213 gentempf(struct queue *queue)
215 char fn[PATH_MAX+1];
216 struct stritem *t;
217 int fd;
219 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
220 return (-1);
221 fd = mkstemp(fn);
222 if (fd < 0)
223 return (-1);
224 queue->mailfd = fd;
225 queue->tmpf = strdup(fn);
226 if (queue->tmpf == NULL) {
227 unlink(fn);
228 return (-1);
230 t = malloc(sizeof(*t));
231 if (t != NULL) {
232 t->str = queue->tmpf;
233 SLIST_INSERT_HEAD(&tmpfs, t, next);
235 return (0);
239 * spool file format:
241 * envelope-from
242 * queue-id1 envelope-to1
243 * queue-id2 envelope-to2
244 * ...
245 * <empty line>
246 * mail data
248 * queue ids are unique, formed from the inode of the spool file
249 * and a unique identifier.
251 static int
252 preparespool(struct queue *queue, const char *sender)
254 char line[1000]; /* by RFC2822 */
255 struct stat st;
256 int error;
257 struct qitem *it;
258 FILE *queuef;
259 off_t hdrlen;
261 error = snprintf(line, sizeof(line), "%s\n", sender);
262 if (error < 0 || (size_t)error >= sizeof(line)) {
263 errno = E2BIG;
264 return (-1);
266 if (write(queue->mailfd, line, error) != error)
267 return (-1);
269 queuef = fdopen(queue->mailfd, "r+");
270 if (queuef == NULL)
271 return (-1);
274 * Assign queue id to each dest.
276 if (fstat(queue->mailfd, &st) != 0)
277 return (-1);
278 queue->id = st.st_ino;
279 LIST_FOREACH(it, &queue->queue, next) {
280 if (asprintf(&it->queueid, "%"PRIxMAX".%"PRIxPTR,
281 queue->id, (uintptr_t)it) <= 0)
282 return (-1);
283 if (asprintf(&it->queuefn, "%s/%s",
284 config->spooldir, it->queueid) <= 0)
285 return (-1);
286 /* File may not exist yet */
287 if (stat(it->queuefn, &st) == 0)
288 return (-1);
289 it->queuef = queuef;
290 error = snprintf(line, sizeof(line), "%s %s\n",
291 it->queueid, it->addr);
292 if (error < 0 || (size_t)error >= sizeof(line))
293 return (-1);
294 if (write(queue->mailfd, line, error) != error)
295 return (-1);
297 line[0] = '\n';
298 if (write(queue->mailfd, line, 1) != 1)
299 return (-1);
301 hdrlen = lseek(queue->mailfd, 0, SEEK_CUR);
302 LIST_FOREACH(it, &queue->queue, next) {
303 it->hdrlen = hdrlen;
305 return (0);
308 static char *
309 rfc822date(void)
311 static char str[50];
312 size_t error;
313 time_t now;
315 now = time(NULL);
316 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
317 localtime(&now));
318 if (error == 0)
319 strcpy(str, "(date fail)");
320 return (str);
323 static int
324 readmail(struct queue *queue, const char *sender, int nodot)
326 char line[1000]; /* by RFC2822 */
327 size_t linelen;
328 int error;
330 error = snprintf(line, sizeof(line), "\
331 Received: from %s (uid %d)\n\
332 \t(envelope-from %s)\n\
333 \tid %"PRIxMAX"\n\
334 \tby %s (%s)\n\
335 \t%s\n",
336 getlogin(), getuid(),
337 sender,
338 queue->id,
339 hostname(), VERSION,
340 rfc822date());
341 if (error < 0 || (size_t)error >= sizeof(line))
342 return (-1);
343 if (write(queue->mailfd, line, error) != error)
344 return (-1);
346 while (!feof(stdin)) {
347 if (fgets(line, sizeof(line), stdin) == NULL)
348 break;
349 linelen = strlen(line);
350 if (linelen == 0 || line[linelen - 1] != '\n') {
351 errno = EINVAL; /* XXX mark permanent errors */
352 return (-1);
354 if (!nodot && linelen == 2 && line[0] == '.')
355 break;
356 if ((size_t)write(queue->mailfd, line, linelen) != linelen)
357 return (-1);
359 if (fsync(queue->mailfd) != 0)
360 return (-1);
361 return (0);
364 static int
365 linkspool(struct queue *queue)
367 struct qitem *it;
369 LIST_FOREACH(it, &queue->queue, next) {
370 if (link(queue->tmpf, it->queuefn) != 0)
371 goto delfiles;
373 unlink(queue->tmpf);
374 return (0);
376 delfiles:
377 LIST_FOREACH(it, &queue->queue, next) {
378 unlink(it->queuefn);
380 return (-1);
383 static struct qitem *
384 go_background(struct queue *queue)
386 struct sigaction sa;
387 struct qitem *it;
388 pid_t pid;
390 if (daemonize && daemon(0, 0) != 0) {
391 syslog(LOG_ERR, "can not daemonize: %m");
392 exit(1);
394 daemonize = 0;
396 bzero(&sa, sizeof(sa));
397 sa.sa_flags = SA_NOCLDWAIT;
398 sa.sa_handler = SIG_IGN;
399 sigaction(SIGCHLD, &sa, NULL);
401 LIST_FOREACH(it, &queue->queue, next) {
402 /* No need to fork for the last dest */
403 if (LIST_NEXT(it, next) == NULL)
404 return (it);
406 pid = fork();
407 switch (pid) {
408 case -1:
409 syslog(LOG_ERR, "can not fork: %m");
410 exit(1);
411 break;
413 case 0:
415 * Child:
417 * return and deliver mail
419 return (it);
421 default:
423 * Parent:
425 * fork next child
427 break;
431 syslog(LOG_CRIT, "reached dead code");
432 exit(1);
435 static void
436 bounce(struct qitem *it, const char *reason)
438 struct queue bounceq;
439 struct qitem *bit;
440 char line[1000];
441 int error;
443 /* Don't bounce bounced mails */
444 if (it->sender[0] == 0) {
445 syslog(LOG_CRIT, "%s: delivery panic: can't bounce a bounce",
446 it->queueid);
447 exit(1);
450 syslog(LOG_ERR, "%s: delivery failed, bouncing",
451 it->queueid);
453 LIST_INIT(&bounceq.queue);
454 if (add_recp(&bounceq, it->sender, "", 1) != 0)
455 goto fail;
456 if (gentempf(&bounceq) != 0)
457 goto fail;
458 if (preparespool(&bounceq, "") != 0)
459 goto fail;
461 bit = LIST_FIRST(&bounceq.queue);
462 error = fprintf(bit->queuef, "\
463 Received: from MAILER-DAEMON\n\
464 \tid %"PRIxMAX"\n\
465 \tby %s (%s)\n\
466 \t%s\n\
467 X-Original-To: <%s>\n\
468 From: MAILER-DAEMON <>\n\
469 To: %s\n\
470 Subject: Mail delivery failed\n\
471 Message-Id: <%"PRIxMAX"@%s>\n\
472 Date: %s\n\
474 This is the %s at %s.\n\
476 There was an error delivering your mail to <%s>.\n\
478 %s\n\
480 Message headers follow.\n\
483 bounceq.id,
484 hostname(), VERSION,
485 rfc822date(),
486 it->addr,
487 it->sender,
488 bounceq.id, hostname(),
489 rfc822date(),
490 VERSION, hostname(),
491 it->addr,
492 reason);
493 if (error < 0)
494 goto fail;
495 if (fflush(bit->queuef) != 0)
496 goto fail;
498 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0)
499 goto fail;
500 while (!feof(it->queuef)) {
501 if (fgets(line, sizeof(line), it->queuef) == NULL)
502 break;
503 if (line[0] == '\n')
504 break;
505 write(bounceq.mailfd, line, strlen(line));
507 if (fsync(bounceq.mailfd) != 0)
508 goto fail;
509 if (linkspool(&bounceq) != 0)
510 goto fail;
511 /* bounce is safe */
513 unlink(it->queuefn);
514 fclose(it->queuef);
516 bit = go_background(&bounceq);
517 deliver(bit);
518 /* NOTREACHED */
520 fail:
521 syslog(LOG_CRIT, "%s: error creating bounce: %m", it->queueid);
522 unlink(it->queuefn);
523 exit(1);
526 static int
527 deliver_local(struct qitem *it, const char **errmsg)
529 char fn[PATH_MAX+1];
530 char line[1000];
531 size_t linelen;
532 int mbox;
533 int error;
534 off_t mboxlen;
535 time_t now = time(NULL);
537 error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, it->addr);
538 if (error < 0 || (size_t)error >= sizeof(fn)) {
539 syslog(LOG_ERR, "%s: local delivery deferred: %m",
540 it->queueid);
541 return (1);
544 /* mailx removes users mailspool file if empty, so open with O_CREAT */
545 mbox = open(fn, O_WRONLY | O_EXLOCK | O_APPEND | O_CREAT);
546 if (mbox < 0) {
547 syslog(LOG_ERR, "%s: local delivery deferred: can not open `%s': %m",
548 it->queueid, fn);
549 return (1);
551 mboxlen = lseek(mbox, 0, SEEK_CUR);
553 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
554 syslog(LOG_ERR, "%s: local delivery deferred: can not seek: %m",
555 it->queueid);
556 return (1);
559 error = snprintf(line, sizeof(line), "From %s\t%s", it->sender, ctime(&now));
560 if (error < 0 || (size_t)error >= sizeof(line)) {
561 syslog(LOG_ERR, "%s: local delivery deferred: can not write header: %m",
562 it->queueid);
563 return (1);
565 if (write(mbox, line, error) != error)
566 goto wrerror;
568 while (!feof(it->queuef)) {
569 if (fgets(line, sizeof(line), it->queuef) == NULL)
570 break;
571 linelen = strlen(line);
572 if (linelen == 0 || line[linelen - 1] != '\n') {
573 syslog(LOG_CRIT, "%s: local delivery failed: corrupted queue file",
574 it->queueid);
575 *errmsg = "corrupted queue file";
576 error = -1;
577 goto chop;
580 if (strncmp(line, "From ", 5) == 0) {
581 const char *gt = ">";
583 if (write(mbox, gt, 1) != 1)
584 goto wrerror;
586 if ((size_t)write(mbox, line, linelen) != linelen)
587 goto wrerror;
589 line[0] = '\n';
590 if (write(mbox, line, 1) != 1)
591 goto wrerror;
592 close(mbox);
593 return (0);
595 wrerror:
596 syslog(LOG_ERR, "%s: local delivery failed: write error: %m",
597 it->queueid);
598 error = 1;
599 chop:
600 if (ftruncate(mbox, mboxlen) != 0)
601 syslog(LOG_WARNING, "%s: error recovering mbox `%s': %m",
602 it->queueid, fn);
603 close(mbox);
604 return (error);
607 static void
608 deliver(struct qitem *it)
610 int error;
611 unsigned int backoff = MIN_RETRY;
612 const char *errmsg = "unknown bounce reason";
613 struct timeval now;
614 struct stat st;
616 syslog(LOG_INFO, "%s: mail from=<%s> to=<%s>",
617 it->queueid, it->sender, it->addr);
619 retry:
620 syslog(LOG_INFO, "%s: trying delivery",
621 it->queueid);
623 if (it->remote)
624 error = deliver_remote(it, &errmsg);
625 else
626 error = deliver_local(it, &errmsg);
628 switch (error) {
629 case 0:
630 unlink(it->queuefn);
631 syslog(LOG_INFO, "%s: delivery successful",
632 it->queueid);
633 exit(0);
635 case 1:
636 if (stat(it->queuefn, &st) != 0) {
637 syslog(LOG_ERR, "%s: lost queue file `%s'",
638 it->queueid, it->queuefn);
639 exit(1);
641 if (gettimeofday(&now, NULL) == 0 &&
642 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
643 char *msg;
645 if (asprintf(&msg,
646 "Could not deliver for the last %d seconds. Giving up.",
647 MAX_TIMEOUT) > 0)
648 errmsg = msg;
649 goto bounce;
651 sleep(backoff);
652 backoff *= 2;
653 if (backoff > MAX_RETRY)
654 backoff = MAX_RETRY;
655 goto retry;
657 case -1:
658 default:
659 break;
662 bounce:
663 bounce(it, errmsg);
664 /* NOTREACHED */
667 static void
668 load_queue(struct queue *queue)
670 struct stat st;
671 struct qitem *it;
672 //struct queue queue, itmqueue;
673 struct queue itmqueue;
674 DIR *spooldir;
675 struct dirent *de;
676 char line[1000];
677 char *fn;
678 FILE *queuef;
679 char *sender;
680 char *addr;
681 char *queueid;
682 char *queuefn;
683 off_t hdrlen;
684 int fd;
686 LIST_INIT(&queue->queue);
688 spooldir = opendir(config->spooldir);
689 if (spooldir == NULL)
690 err(1, "reading queue");
692 while ((de = readdir(spooldir)) != NULL) {
693 sender = NULL;
694 queuef = NULL;
695 queueid = NULL;
696 queuefn = NULL;
697 fn = NULL;
698 LIST_INIT(&itmqueue.queue);
700 /* ignore temp files */
701 if (strncmp(de->d_name, "tmp_", 4) == 0 ||
702 de->d_type != DT_REG)
703 continue;
704 if (asprintf(&queuefn, "%s/%s", config->spooldir, de->d_name) < 0)
705 goto fail;
706 fd = open(queuefn, O_RDONLY|O_EXLOCK|O_NONBLOCK);
707 if (fd < 0) {
708 /* Ignore locked files */
709 if (errno == EWOULDBLOCK)
710 continue;
711 goto skip_item;
714 queuef = fdopen(fd, "r");
715 if (queuef == NULL)
716 goto skip_item;
717 if (fgets(line, sizeof(line), queuef) == NULL ||
718 line[0] == 0)
719 goto skip_item;
720 line[strlen(line) - 1] = 0; /* chop newline */
721 sender = strdup(line);
722 if (sender == NULL)
723 goto skip_item;
725 for (;;) {
726 if (fgets(line, sizeof(line), queuef) == NULL ||
727 line[0] == 0)
728 goto skip_item;
729 if (line[0] == '\n')
730 break;
731 line[strlen(line) - 1] = 0;
732 queueid = strdup(line);
733 if (queueid == NULL)
734 goto skip_item;
735 addr = strchr(queueid, ' ');
736 if (addr == NULL)
737 goto skip_item;
738 *addr++ = 0;
739 if (fn != NULL)
740 free(fn);
741 if (asprintf(&fn, "%s/%s", config->spooldir, queueid) < 0)
742 goto skip_item;
743 /* Item has already been delivered? */
744 if (stat(fn, &st) != 0)
745 continue;
746 if (add_recp(&itmqueue, addr, sender, 0) != 0)
747 goto skip_item;
748 it = LIST_FIRST(&itmqueue.queue);
749 it->queuef = queuef;
750 it->queueid = queueid;
751 it->queuefn = fn;
752 fn = NULL;
754 if (LIST_EMPTY(&itmqueue.queue)) {
755 warnx("queue file without items: `%s'", queuefn);
756 goto skip_item2;
758 hdrlen = ftell(queuef);
759 while ((it = LIST_FIRST(&itmqueue.queue)) != NULL) {
760 it->hdrlen = hdrlen;
761 LIST_REMOVE(it, next);
762 LIST_INSERT_HEAD(&queue->queue, it, next);
764 continue;
766 skip_item:
767 warn("reading queue: `%s'", queuefn);
768 skip_item2:
769 if (sender != NULL)
770 free(sender);
771 if (queuefn != NULL)
772 free(queuefn);
773 if (fn != NULL)
774 free(fn);
775 if (queueid != NULL)
776 free(queueid);
777 close(fd);
779 closedir(spooldir);
780 return;
782 fail:
783 err(1, "reading queue");
786 static void
787 run_queue(struct queue *queue)
789 struct qitem *it;
791 if (LIST_EMPTY(&queue->queue))
792 return;
794 it = go_background(queue);
795 deliver(it);
796 /* NOTREACHED */
799 static void
800 show_queue(struct queue *queue)
802 struct qitem *it;
804 if (LIST_EMPTY(&queue->queue)) {
805 printf("Mail queue is empty\n");
806 return;
809 LIST_FOREACH(it, &queue->queue, next) {
810 printf("\
811 ID\t: %s\n\
812 From\t: %s\n\
813 To\t: %s\n--\n", it->queueid, it->sender, it->addr);
818 * TODO:
820 * - alias processing
821 * - use group permissions
822 * - proper sysexit codes
826 main(int argc, char **argv)
828 char *sender = NULL;
829 char tag[255];
830 struct qitem *it;
831 struct queue queue;
832 struct queue lqueue;
833 int i, ch;
834 int nodot = 0, doqueue = 0, showq = 0;
836 atexit(deltmp);
837 LIST_INIT(&queue.queue);
838 snprintf(tag, 254, "dma");
840 opterr = 0;
841 while ((ch = getopt(argc, argv, "A:b:Df:iL:o:O:q:r:")) != -1) {
842 switch (ch) {
843 case 'A':
844 /* -AX is being ignored, except for -A{c,m} */
845 if (optarg[0] == 'c' || optarg[0] == 'm') {
846 break;
848 /* else FALLTRHOUGH */
849 case 'b':
850 /* -bX is being ignored, except for -bp */
851 if (optarg[0] == 'p') {
852 showq = 1;
853 break;
855 /* else FALLTRHOUGH */
856 case 'D':
857 daemonize = 0;
858 break;
859 case 'L':
860 if (optarg != NULL)
861 snprintf(tag, 254, "%s", optarg);
862 break;
863 case 'f':
864 case 'r':
865 sender = optarg;
866 break;
868 case 'o':
869 /* -oX is being ignored, except for -oi */
870 if (optarg[0] != 'i')
871 break;
872 /* else FALLTRHOUGH */
873 case 'O':
874 break;
875 case 'i':
876 nodot = 1;
877 break;
879 case 'q':
880 doqueue = 1;
881 break;
883 default:
884 exit(1);
887 argc -= optind;
888 argv += optind;
889 opterr = 1;
891 openlog(tag, LOG_PID | LOG_PERROR, LOG_MAIL);
893 config = malloc(sizeof(struct config));
894 if (config == NULL)
895 errx(1, "Cannot allocate enough memory");
897 memset(config, 0, sizeof(struct config));
898 if (parse_conf(CONF_PATH, config) < 0) {
899 free(config);
900 errx(1, "reading config file");
903 if (config->features & VIRTUAL)
904 if (parse_virtuser(config->virtualpath) < 0)
905 errx(1, "error reading virtual user file: %s",
906 config->virtualpath);
908 if (parse_authfile(config->authpath) < 0)
909 err(1, "reading SMTP authentication file");
911 if (showq) {
912 if (argc != 0)
913 errx(1, "sending mail and displaying queue is"
914 " mutually exclusive");
915 load_queue(&lqueue);
916 show_queue(&lqueue);
917 return (0);
920 if (doqueue) {
921 if (argc != 0)
922 errx(1, "sending mail and queue pickup is mutually exclusive");
923 load_queue(&lqueue);
924 run_queue(&lqueue);
925 return (0);
928 if (read_aliases() != 0)
929 err(1, "reading aliases");
931 if ((sender = set_from(sender)) == NULL)
932 err(1, "setting from address");
934 for (i = 0; i < argc; i++) {
935 if (add_recp(&queue, argv[i], sender, 1) != 0)
936 errx(1, "invalid recipient `%s'\n", argv[i]);
939 if (LIST_EMPTY(&queue.queue))
940 errx(1, "no recipients");
942 if (gentempf(&queue) != 0)
943 err(1, "create temp file");
945 if (preparespool(&queue, sender) != 0)
946 err(1, "creating spools (1)");
948 if (readmail(&queue, sender, nodot) != 0)
949 err(1, "reading mail");
951 if (linkspool(&queue) != 0)
952 err(1, "creating spools (2)");
954 /* From here on the mail is safe. */
956 if (config->features & DEFER)
957 return (0);
959 it = go_background(&queue);
960 deliver(it);
962 /* NOTREACHED */
963 return (0);