dma: restructure logging
[dragonfly.git] / libexec / dma / dma.c
CommitLineData
f67beddd
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.
33 *
bc7baf1d 34 * $DragonFly: src/libexec/dma/dma.c,v 1.5 2008/09/30 17:47:21 swildner Exp $
f67beddd
MS
35 */
36
37#include <sys/param.h>
38#include <sys/queue.h>
39#include <sys/stat.h>
40#include <sys/types.h>
bc7baf1d 41#include <sys/wait.h>
f67beddd 42
f67beddd
MS
43#include <dirent.h>
44#include <err.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <inttypes.h>
48#include <netdb.h>
49#include <paths.h>
50#include <pwd.h>
51#include <signal.h>
52#include <stdarg.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <syslog.h>
57#include <unistd.h>
58
59#include "dma.h"
60
61
62
4a23bd3d 63static void deliver(struct qitem *);
f67beddd
MS
64
65struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
f4e61a9f 66struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
f67beddd
MS
67struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
68struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
69static int daemonize = 1;
70struct config *config;
f8633e71
SS
71static const char *username;
72static uid_t uid;
405f48ee 73static const char *logident_base;
f67beddd 74
144b337d 75
f4e61a9f 76const char *
f67beddd
MS
77hostname(void)
78{
79 static char name[MAXHOSTNAMELEN+1];
5754971c
SS
80 int initialized = 0;
81 FILE *fp;
82 size_t len;
f67beddd 83
5754971c
SS
84 if (initialized)
85 return (name);
86
87 if (config->mailname != NULL && config->mailname[0] != '\0') {
88 snprintf(name, sizeof(name), "%s", config->mailname);
89 initialized = 1;
90 return (name);
91 }
92 if (config->mailnamefile != NULL && config->mailnamefile[0] != '\0') {
93 fp = fopen(config->mailnamefile, "r");
94 if (fp != NULL) {
95 if (fgets(name, sizeof(name), fp) != NULL) {
96 len = strlen(name);
97 while (len > 0 &&
98 (name[len - 1] == '\r' ||
99 name[len - 1] == '\n'))
100 name[--len] = '\0';
101 if (name[0] != '\0') {
102 initialized = 1;
103 return (name);
104 }
105 }
106 fclose(fp);
107 }
108 }
f67beddd
MS
109 if (gethostname(name, sizeof(name)) != 0)
110 strcpy(name, "(unknown hostname)");
5754971c 111 initialized = 1;
f67beddd
MS
112 return name;
113}
114
405f48ee
SS
115static void
116setlogident(const char *fmt, ...)
117{
118 char *tag = NULL;
119
120 if (fmt != NULL) {
121 va_list ap;
122 char *sufx;
123
124 va_start(ap, fmt);
125 vasprintf(&sufx, fmt, ap);
126 if (sufx != NULL) {
127 asprintf(&tag, "%s[%s]", logident_base, sufx);
128 free(sufx);
129 }
130 va_end(ap);
131 }
132 closelog();
133 openlog(tag != NULL ? tag : logident_base, 0, LOG_MAIL);
134}
135
f8633e71
SS
136static const char *
137check_username(const char *name, uid_t ckuid)
138{
139 struct passwd *pwd;
140
141 if (name == NULL)
142 return (NULL);
143 pwd = getpwnam(name);
144 if (pwd == NULL || pwd->pw_uid != ckuid)
145 return (NULL);
146 return (name);
147}
148
149static void
150set_username(void)
151{
152 struct passwd *pwd;
431e1ddf 153 char *u = NULL;
f8633e71
SS
154
155 uid = getuid();
156 username = check_username(getlogin(), uid);
431e1ddf
SS
157 if (username != NULL)
158 return;
159 username = check_username(getenv("LOGNAME"), uid);
160 if (username != NULL)
161 return;
162 username = check_username(getenv("USER"), uid);
163 if (username != NULL)
164 return;
165 pwd = getpwuid(uid);
166 if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0] != '\0' &&
167 (u = strdup(pwd->pw_name)) != NULL) {
168 username = check_username(u, uid);
169 if (username != NULL)
170 return;
171 else
172 free(u);
f8633e71 173 }
431e1ddf
SS
174 asprintf(__DECONST(void *, &username), "%ld", (long)uid);
175 if (username != NULL)
176 return;
177 username = "unknown-or-invalid-username";
f8633e71
SS
178}
179
f67beddd
MS
180static char *
181set_from(const char *osender)
182{
183 struct virtuser *v;
184 char *sender;
185
186 if ((config->features & VIRTUAL) != 0) {
187 SLIST_FOREACH(v, &virtusers, next) {
f8633e71 188 if (strcmp(v->login, username) == 0) {
f67beddd
MS
189 sender = strdup(v->address);
190 if (sender == NULL)
191 return(NULL);
192 goto out;
193 }
194 }
195 }
196
197 if (osender) {
198 sender = strdup(osender);
199 if (sender == NULL)
4a23bd3d 200 return (NULL);
f67beddd 201 } else {
f8633e71 202 if (asprintf(&sender, "%s@%s", username, hostname()) <= 0)
4a23bd3d 203 return (NULL);
f67beddd
MS
204 }
205
206 if (strchr(sender, '\n') != NULL) {
207 errno = EINVAL;
4a23bd3d 208 return (NULL);
f67beddd
MS
209 }
210
211out:
4a23bd3d 212 return (sender);
f67beddd
MS
213}
214
215static int
216read_aliases(void)
217{
218 yyin = fopen(config->aliases, "r");
219 if (yyin == NULL)
4a23bd3d 220 return (0); /* not fatal */
f67beddd 221 if (yyparse())
4a23bd3d 222 return (-1); /* fatal error, probably malloc() */
f67beddd 223 fclose(yyin);
4a23bd3d 224 return (0);
f67beddd
MS
225}
226
f4e61a9f 227int
f67beddd
MS
228add_recp(struct queue *queue, const char *str, const char *sender, int expand)
229{
230 struct qitem *it, *tit;
231 struct stritem *sit;
232 struct alias *al;
233 struct passwd *pw;
234 char *host;
235 int aliased = 0;
236
237 it = calloc(1, sizeof(*it));
238 if (it == NULL)
4a23bd3d 239 return (-1);
f67beddd
MS
240 it->addr = strdup(str);
241 if (it->addr == NULL)
4a23bd3d 242 return (-1);
f67beddd
MS
243
244 it->sender = sender;
245 host = strrchr(it->addr, '@');
246 if (host != NULL &&
247 (strcmp(host + 1, hostname()) == 0 ||
248 strcmp(host + 1, "localhost") == 0)) {
249 *host = 0;
250 }
251 LIST_FOREACH(tit, &queue->queue, next) {
252 /* weed out duplicate dests */
253 if (strcmp(tit->addr, it->addr) == 0) {
254 free(it->addr);
255 free(it);
4a23bd3d 256 return (0);
f67beddd
MS
257 }
258 }
259 LIST_INSERT_HEAD(&queue->queue, it, next);
260 if (strrchr(it->addr, '@') == NULL) {
4a23bd3d 261 it->remote = 0;
f67beddd
MS
262 if (expand) {
263 LIST_FOREACH(al, &aliases, next) {
264 if (strcmp(al->alias, it->addr) != 0)
265 continue;
266 SLIST_FOREACH(sit, &al->dests, next) {
4a23bd3d
MS
267 if (add_recp(queue, sit->str, sender, 1) != 0)
268 return (-1);
f67beddd
MS
269 }
270 aliased = 1;
271 }
272 if (aliased) {
273 LIST_REMOVE(it, next);
274 } else {
4a23bd3d 275 /* Local destination, check */
f67beddd
MS
276 pw = getpwnam(it->addr);
277 if (pw == NULL)
278 goto out;
13e288a1 279 /* XXX read .forward */
4a23bd3d 280 endpwent();
f67beddd
MS
281 }
282 }
283 } else {
4a23bd3d 284 it->remote = 1;
f67beddd
MS
285 }
286
4a23bd3d 287 return (0);
f67beddd
MS
288
289out:
290 free(it->addr);
291 free(it);
4a23bd3d 292 return (-1);
f67beddd
MS
293}
294
295static void
296deltmp(void)
297{
298 struct stritem *t;
299
300 SLIST_FOREACH(t, &tmpfs, next) {
301 unlink(t->str);
302 }
303}
304
f4e61a9f
SS
305int
306open_locked(const char *fname, int flags, ...)
f67beddd 307{
f4e61a9f 308 int mode = 0;
f67beddd 309
f4e61a9f
SS
310 if (flags & O_CREAT) {
311 va_list ap;
312 va_start(ap, flags);
313 mode = va_arg(ap, int);
314 va_end(ap);
f67beddd 315 }
f67beddd 316
144b337d
SS
317#ifndef O_EXLOCK
318 int fd, save_errno;
319
f4e61a9f 320 fd = open(fname, flags, mode);
144b337d
SS
321 if (fd < 0)
322 return(fd);
323 if (flock(fd, LOCK_EX|((flags & O_NONBLOCK)? LOCK_NB: 0)) < 0) {
324 save_errno = errno;
325 close(fd);
326 errno = save_errno;
327 return(-1);
328 }
329 return(fd);
330#else
f4e61a9f 331 return(open(fname, flags|O_EXLOCK, mode));
144b337d
SS
332#endif
333}
334
f67beddd
MS
335static char *
336rfc822date(void)
337{
338 static char str[50];
339 size_t error;
340 time_t now;
341
342 now = time(NULL);
343 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
344 localtime(&now));
345 if (error == 0)
346 strcpy(str, "(date fail)");
4a23bd3d 347 return (str);
f67beddd
MS
348}
349
350static int
af80a74d
SS
351strprefixcmp(const char *str, const char *prefix)
352{
353 return (strncasecmp(str, prefix, strlen(prefix)));
354}
355
356static int
f67beddd
MS
357readmail(struct queue *queue, const char *sender, int nodot)
358{
359 char line[1000]; /* by RFC2822 */
360 size_t linelen;
361 int error;
af80a74d
SS
362 int had_headers = 0;
363 int had_from = 0;
364 int had_messagid = 0;
365 int had_date = 0;
f67beddd 366
7b5c40d5
SS
367 error = snprintf(line, sizeof(line),
368 "Received: from %s (uid %d)\n"
369 "\t(envelope-from %s)\n"
405f48ee 370 "\tid %s\n"
7b5c40d5
SS
371 "\tby %s (%s)\n"
372 "\t%s\n",
f8633e71 373 username, uid,
f67beddd
MS
374 sender,
375 queue->id,
376 hostname(), VERSION,
377 rfc822date());
378 if (error < 0 || (size_t)error >= sizeof(line))
4a23bd3d 379 return (-1);
f67beddd 380 if (write(queue->mailfd, line, error) != error)
4a23bd3d 381 return (-1);
f67beddd
MS
382
383 while (!feof(stdin)) {
384 if (fgets(line, sizeof(line), stdin) == NULL)
385 break;
386 linelen = strlen(line);
387 if (linelen == 0 || line[linelen - 1] != '\n') {
388 errno = EINVAL; /* XXX mark permanent errors */
4a23bd3d 389 return (-1);
f67beddd 390 }
af80a74d
SS
391 if (!had_headers) {
392 if (strprefixcmp(line, "Date:") == 0)
393 had_date = 1;
394 else if (strprefixcmp(line, "Message-Id:") == 0)
395 had_messagid = 1;
396 else if (strprefixcmp(line, "From:") == 0)
397 had_from = 1;
398 }
399 if (strcmp(line, "\n") == 0 && !had_headers) {
400 had_headers = 1;
401 while (!had_date || !had_messagid || !had_from) {
402 if (!had_date) {
403 had_date = 1;
404 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
405 } else if (!had_messagid) {
406 /* XXX better msgid, assign earlier and log? */
407 had_messagid = 1;
405f48ee 408 snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
af80a74d
SS
409 queue->id, hostname());
410 } else if (!had_from) {
411 had_from = 1;
412 snprintf(line, sizeof(line), "From: <%s>\n", sender);
413 }
414 if ((size_t)write(queue->mailfd, line, strlen(line)) != strlen(line))
415 return (-1);
416 }
417 strcpy(line, "\n");
418 }
f67beddd
MS
419 if (!nodot && linelen == 2 && line[0] == '.')
420 break;
421 if ((size_t)write(queue->mailfd, line, linelen) != linelen)
4a23bd3d 422 return (-1);
f67beddd
MS
423 }
424 if (fsync(queue->mailfd) != 0)
4a23bd3d 425 return (-1);
405f48ee
SS
426
427 syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
428 username, uid, sender);
429
4a23bd3d 430 return (0);
f67beddd
MS
431}
432
4a23bd3d
MS
433static struct qitem *
434go_background(struct queue *queue)
f67beddd
MS
435{
436 struct sigaction sa;
437 struct qitem *it;
438 pid_t pid;
439
440 if (daemonize && daemon(0, 0) != 0) {
4a23bd3d 441 syslog(LOG_ERR, "can not daemonize: %m");
f67beddd
MS
442 exit(1);
443 }
444 daemonize = 0;
4a23bd3d 445
f67beddd
MS
446 bzero(&sa, sizeof(sa));
447 sa.sa_flags = SA_NOCLDWAIT;
448 sa.sa_handler = SIG_IGN;
449 sigaction(SIGCHLD, &sa, NULL);
450
4dcaa51b 451 LIST_FOREACH(it, &queue->queue, next) {
4a23bd3d 452 /* No need to fork for the last dest */
405f48ee
SS
453 if (LIST_NEXT(it, next) == NULL) {
454 setlogident("%s", it->queueid);
4a23bd3d 455 return (it);
405f48ee 456 }
4a23bd3d 457
f67beddd
MS
458 pid = fork();
459 switch (pid) {
460 case -1:
461 syslog(LOG_ERR, "can not fork: %m");
462 exit(1);
463 break;
464
465 case 0:
466 /*
467 * Child:
468 *
469 * return and deliver mail
470 */
405f48ee 471 setlogident("%s", it->queueid);
4a23bd3d 472 return (it);
f67beddd
MS
473
474 default:
475 /*
476 * Parent:
477 *
478 * fork next child
479 */
480 break;
481 }
482 }
483
484 syslog(LOG_CRIT, "reached dead code");
485 exit(1);
486}
487
488static void
5868e44c 489bounce(struct qitem *it, const char *reason)
f67beddd
MS
490{
491 struct queue bounceq;
492 struct qitem *bit;
493 char line[1000];
5ca28cf6 494 size_t pos;
f67beddd
MS
495 int error;
496
497 /* Don't bounce bounced mails */
498 if (it->sender[0] == 0) {
405f48ee 499 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
f67beddd
MS
500 exit(1);
501 }
502
f67beddd
MS
503 LIST_INIT(&bounceq.queue);
504 if (add_recp(&bounceq, it->sender, "", 1) != 0)
505 goto fail;
f4e61a9f 506 if (newspoolf(&bounceq, "") != 0)
f67beddd 507 goto fail;
405f48ee
SS
508
509 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
510 setlogident("%s", bounceq.id);
511
f67beddd 512 bit = LIST_FIRST(&bounceq.queue);
f4e61a9f 513 error = fprintf(bit->mailf,
7b5c40d5 514 "Received: from MAILER-DAEMON\n"
405f48ee 515 "\tid %s\n"
7b5c40d5
SS
516 "\tby %s (%s)\n"
517 "\t%s\n"
518 "X-Original-To: <%s>\n"
519 "From: MAILER-DAEMON <>\n"
520 "To: %s\n"
521 "Subject: Mail delivery failed\n"
405f48ee 522 "Message-Id: <%s@%s>\n"
7b5c40d5
SS
523 "Date: %s\n"
524 "\n"
525 "This is the %s at %s.\n"
526 "\n"
527 "There was an error delivering your mail to <%s>.\n"
528 "\n"
529 "%s\n"
530 "\n"
531 "%s\n"
532 "\n",
f67beddd
MS
533 bounceq.id,
534 hostname(), VERSION,
535 rfc822date(),
536 it->addr,
537 it->sender,
538 bounceq.id, hostname(),
539 rfc822date(),
540 VERSION, hostname(),
541 it->addr,
5ca28cf6 542 reason,
7b5c40d5
SS
543 config->features & FULLBOUNCE ?
544 "Original message follows." :
545 "Message headers follow.");
f67beddd
MS
546 if (error < 0)
547 goto fail;
f4e61a9f 548 if (fflush(bit->mailf) != 0)
f67beddd
MS
549 goto fail;
550
f4e61a9f 551 if (fseek(it->mailf, it->hdrlen, SEEK_SET) != 0)
f67beddd 552 goto fail;
5ca28cf6 553 if (config->features & FULLBOUNCE) {
f4e61a9f 554 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
5ca28cf6
SS
555 if ((size_t)write(bounceq.mailfd, line, pos) != pos)
556 goto fail;
557 }
558 } else {
f4e61a9f
SS
559 while (!feof(it->mailf)) {
560 if (fgets(line, sizeof(line), it->mailf) == NULL)
5ca28cf6
SS
561 break;
562 if (line[0] == '\n')
563 break;
564 if ((size_t)write(bounceq.mailfd, line, strlen(line)) != strlen(line))
565 goto fail;
566 }
f67beddd
MS
567 }
568 if (fsync(bounceq.mailfd) != 0)
569 goto fail;
570 if (linkspool(&bounceq) != 0)
571 goto fail;
572 /* bounce is safe */
573
f4e61a9f 574 delqueue(it);
f67beddd 575
4a23bd3d
MS
576 bit = go_background(&bounceq);
577 deliver(bit);
f67beddd
MS
578 /* NOTREACHED */
579
580fail:
405f48ee 581 syslog(LOG_CRIT, "error creating bounce: %m");
f4e61a9f 582 delqueue(it);
f67beddd
MS
583 exit(1);
584}
585
f67beddd 586static void
4a23bd3d 587deliver(struct qitem *it)
f67beddd
MS
588{
589 int error;
590 unsigned int backoff = MIN_RETRY;
5868e44c 591 const char *errmsg = "unknown bounce reason";
f67beddd
MS
592 struct timeval now;
593 struct stat st;
594
f67beddd 595retry:
405f48ee 596 syslog(LOG_INFO, "trying delivery");
f67beddd 597
4a23bd3d
MS
598 if (it->remote)
599 error = deliver_remote(it, &errmsg);
600 else
f67beddd
MS
601 error = deliver_local(it, &errmsg);
602
603 switch (error) {
604 case 0:
f4e61a9f 605 delqueue(it);
405f48ee 606 syslog(LOG_INFO, "delivery successful");
f67beddd
MS
607 exit(0);
608
609 case 1:
610 if (stat(it->queuefn, &st) != 0) {
405f48ee 611 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
f67beddd
MS
612 exit(1);
613 }
614 if (gettimeofday(&now, NULL) == 0 &&
615 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
5868e44c 616 asprintf(__DECONST(void *, &errmsg),
4a23bd3d 617 "Could not deliver for the last %d seconds. Giving up.",
39f7c85a 618 MAX_TIMEOUT);
f67beddd
MS
619 goto bounce;
620 }
621 sleep(backoff);
622 backoff *= 2;
623 if (backoff > MAX_RETRY)
624 backoff = MAX_RETRY;
625 goto retry;
626
627 case -1:
628 default:
629 break;
630 }
631
632bounce:
4a23bd3d 633 bounce(it, errmsg);
f67beddd
MS
634 /* NOTREACHED */
635}
636
f67beddd
MS
637static void
638run_queue(struct queue *queue)
639{
4a23bd3d
MS
640 struct qitem *it;
641
f67beddd
MS
642 if (LIST_EMPTY(&queue->queue))
643 return;
644
4a23bd3d
MS
645 it = go_background(queue);
646 deliver(it);
f67beddd
MS
647 /* NOTREACHED */
648}
649
650static void
651show_queue(struct queue *queue)
652{
653 struct qitem *it;
654
655 if (LIST_EMPTY(&queue->queue)) {
656 printf("Mail queue is empty\n");
657 return;
658 }
659
660 LIST_FOREACH(it, &queue->queue, next) {
7b5c40d5
SS
661 printf("ID\t: %s%s\n"
662 "From\t: %s\n"
663 "To\t: %s\n"
664 "--\n",
665 it->queueid,
666 it->locked ? "*" : "",
667 it->sender, it->addr);
f67beddd
MS
668 }
669}
670
671/*
672 * TODO:
673 *
674 * - alias processing
675 * - use group permissions
676 * - proper sysexit codes
677 */
678
4a23bd3d
MS
679int
680main(int argc, char **argv)
f67beddd
MS
681{
682 char *sender = NULL;
4a23bd3d 683 struct qitem *it;
f67beddd
MS
684 struct queue queue;
685 struct queue lqueue;
f67beddd
MS
686 int i, ch;
687 int nodot = 0, doqueue = 0, showq = 0;
688
689 atexit(deltmp);
690 LIST_INIT(&queue.queue);
f67beddd 691
eb870081
SS
692 if (strcmp(argv[0], "mailq") == 0) {
693 argv++; argc--;
694 showq = 1;
695 if (argc != 0)
696 errx(1, "invalid arguments");
697 goto skipopts;
698 }
699
ae3c3bbd 700 opterr = 0;
98dfc119 701 while ((ch = getopt(argc, argv, "A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
f67beddd
MS
702 switch (ch) {
703 case 'A':
704 /* -AX is being ignored, except for -A{c,m} */
705 if (optarg[0] == 'c' || optarg[0] == 'm') {
706 break;
707 }
708 /* else FALLTRHOUGH */
709 case 'b':
710 /* -bX is being ignored, except for -bp */
711 if (optarg[0] == 'p') {
712 showq = 1;
713 break;
714 }
715 /* else FALLTRHOUGH */
716 case 'D':
717 daemonize = 0;
718 break;
719 case 'L':
405f48ee 720 logident_base = optarg;
f67beddd
MS
721 break;
722 case 'f':
723 case 'r':
724 sender = optarg;
725 break;
726
727 case 'o':
728 /* -oX is being ignored, except for -oi */
729 if (optarg[0] != 'i')
730 break;
731 /* else FALLTRHOUGH */
ae3c3bbd
MS
732 case 'O':
733 break;
f67beddd
MS
734 case 'i':
735 nodot = 1;
736 break;
737
738 case 'q':
739 doqueue = 1;
740 break;
741
98dfc119
SS
742 /* Ignored options */
743 case 'B':
744 case 'C':
745 case 'd':
746 case 'F':
747 case 'h':
748 case 'N':
749 case 'n':
750 case 'R':
751 case 'U':
752 case 'V':
753 case 'v':
754 case 'X':
755 break;
756
f67beddd
MS
757 default:
758 exit(1);
759 }
760 }
761 argc -= optind;
762 argv += optind;
ae3c3bbd 763 opterr = 1;
f67beddd 764
13e288a1
SS
765 if (argc != 0 && (showq || doqueue))
766 errx(1, "sending mail and queue operations are mutually exclusive");
767
768 if (showq + doqueue > 1)
769 errx(1, "conflicting queue operations");
770
eb870081 771skipopts:
405f48ee
SS
772 if (logident_base == NULL)
773 logident_base = "dma";
774 setlogident(NULL);
f8633e71 775 set_username();
f67beddd 776
569443a9
SS
777 /* XXX fork root here */
778
13e288a1 779 config = calloc(1, sizeof(*config));
f67beddd 780 if (config == NULL)
e2c88018 781 err(1, NULL);
f67beddd 782
f4e61a9f 783 if (parse_conf(CONF_PATH) < 0) {
f67beddd 784 free(config);
e2c88018 785 err(1, "can not read config file");
f67beddd
MS
786 }
787
788 if (config->features & VIRTUAL)
4a23bd3d 789 if (parse_virtuser(config->virtualpath) < 0)
e2c88018 790 err(1, "can not read virtual user file `%s'",
f67beddd
MS
791 config->virtualpath);
792
4a23bd3d 793 if (parse_authfile(config->authpath) < 0)
e2c88018 794 err(1, "can not read SMTP authentication file");
f67beddd
MS
795
796 if (showq) {
4b331f9f 797 load_queue(&lqueue, 1);
f67beddd 798 show_queue(&lqueue);
4a23bd3d 799 return (0);
f67beddd
MS
800 }
801
802 if (doqueue) {
4b331f9f 803 load_queue(&lqueue, 0);
f67beddd 804 run_queue(&lqueue);
4a23bd3d 805 return (0);
f67beddd
MS
806 }
807
4a23bd3d 808 if (read_aliases() != 0)
e2c88018 809 err(1, "can not read aliases file `%s'", config->aliases);
f67beddd 810
4a23bd3d 811 if ((sender = set_from(sender)) == NULL)
e2c88018 812 err(1, NULL);
f67beddd
MS
813
814 for (i = 0; i < argc; i++) {
4a23bd3d 815 if (add_recp(&queue, argv[i], sender, 1) != 0)
e2c88018 816 errx(1, "invalid recipient `%s'", argv[i]);
f67beddd
MS
817 }
818
4a23bd3d 819 if (LIST_EMPTY(&queue.queue))
f67beddd
MS
820 errx(1, "no recipients");
821
f4e61a9f 822 if (newspoolf(&queue, sender) != 0)
e2c88018 823 err(1, "can not create temp file");
4a23bd3d 824
405f48ee
SS
825 setlogident("%s", queue.id);
826
4a23bd3d 827 if (readmail(&queue, sender, nodot) != 0)
e2c88018 828 err(1, "can not read mail");
f67beddd 829
4a23bd3d 830 if (linkspool(&queue) != 0)
f4e61a9f 831 err(1, "can not create spools");
f67beddd
MS
832
833 /* From here on the mail is safe. */
834
835 if (config->features & DEFER)
4a23bd3d 836 return (0);
f67beddd 837
4a23bd3d
MS
838 it = go_background(&queue);
839 deliver(it);
f67beddd
MS
840
841 /* NOTREACHED */
4a23bd3d 842 return (0);
4dcaa51b 843}