2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/queue.h>
63 extern int yyparse(void);
66 static void deliver(struct qitem *);
68 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
69 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
70 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
71 char username[USERNAME_SIZE];
73 const char *logident_base;
74 char errmsg[ERRMSG_SIZE];
76 static int daemonize = 1;
77 static int doqueue = 0;
79 struct config config = {
82 .aliases = "/etc/aliases",
83 .spooldir = "/var/spool/dma",
88 .masquerade_host = NULL,
89 .masquerade_user = NULL,
95 sighup_handler(int signo)
97 (void)signo; /* so that gcc doesn't complain */
101 set_from(struct queue *queue, const char *osender)
106 if (config.masquerade_user) {
107 addr = config.masquerade_user;
108 } else if (osender) {
110 } else if (getenv("EMAIL") != NULL) {
111 addr = getenv("EMAIL");
116 if (!strchr(addr, '@')) {
117 const char *from_host = hostname();
119 if (config.masquerade_host)
120 from_host = config.masquerade_host;
122 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
125 sender = strdup(addr);
130 if (strchr(sender, '\n') != NULL) {
135 queue->sender = sender;
142 yyin = fopen(config.aliases, "r");
145 * Non-existing aliases file is not a fatal error
149 /* Other problems are. */
153 return (-1); /* fatal error, probably malloc() */
159 do_alias(struct queue *queue, const char *addr)
165 LIST_FOREACH(al, &aliases, next) {
166 if (strcmp(al->alias, addr) != 0)
168 SLIST_FOREACH(sit, &al->dests, next) {
169 if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
179 add_recp(struct queue *queue, const char *str, int expand)
181 struct qitem *it, *tit;
186 it = calloc(1, sizeof(*it));
189 it->addr = strdup(str);
190 if (it->addr == NULL)
193 it->sender = queue->sender;
194 host = strrchr(it->addr, '@');
196 (strcmp(host + 1, hostname()) == 0 ||
197 strcmp(host + 1, "localhost") == 0)) {
200 LIST_FOREACH(tit, &queue->queue, next) {
201 /* weed out duplicate dests */
202 if (strcmp(tit->addr, it->addr) == 0) {
208 LIST_INSERT_HEAD(&queue->queue, it, next);
211 * Do local delivery if there is no @.
212 * Do not do local delivery when NULLCLIENT is set.
214 if (strrchr(it->addr, '@') == NULL && (config.features & NULLCLIENT) == 0) {
217 aliased = do_alias(queue, it->addr);
218 if (!aliased && expand == EXPAND_WILDCARD)
219 aliased = do_alias(queue, "*");
223 LIST_REMOVE(it, next);
225 /* Local destination, check */
226 pw = getpwnam(it->addr);
229 /* XXX read .forward */
245 static struct qitem *
246 go_background(struct queue *queue)
252 if (daemonize && daemon(0, 0) != 0) {
253 syslog(LOG_ERR, "can not daemonize: %m");
258 bzero(&sa, sizeof(sa));
259 sa.sa_handler = SIG_IGN;
260 sigaction(SIGCHLD, &sa, NULL);
262 LIST_FOREACH(it, &queue->queue, next) {
263 /* No need to fork for the last dest */
264 if (LIST_NEXT(it, next) == NULL)
270 syslog(LOG_ERR, "can not fork: %m");
278 * return and deliver mail
282 * If necessary, acquire the queue and * mail files.
283 * If this fails, we probably were raced by another
284 * process. It is okay to be raced if we're supposed
285 * to flush the queue.
287 setlogident("%s", it->queueid);
288 switch (acquirespool(it)) {
294 syslog(LOG_WARNING, "could not lock queue file");
299 dropspool(queue, it);
312 syslog(LOG_CRIT, "reached dead code");
317 deliver(struct qitem *it)
320 unsigned int backoff = MIN_RETRY, slept;
324 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
327 syslog(LOG_INFO, "<%s> trying delivery", it->addr);
330 error = deliver_remote(it);
332 error = deliver_local(it);
336 syslog(LOG_INFO, "<%s> delivery successful", it->addr);
341 if (stat(it->queuefn, &st) != 0) {
342 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
345 if (gettimeofday(&now, NULL) == 0 &&
346 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
347 snprintf(errmsg, sizeof(errmsg),
348 "Could not deliver for the last %d seconds. Giving up.",
352 for (slept = 0; slept < backoff;) {
353 slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT);
354 if (flushqueue_since(slept)) {
359 if (slept >= backoff) {
360 /* pick the next backoff between [1.5, 2.5) times backoff */
361 backoff = backoff + backoff / 2 + random() % backoff;
362 if (backoff > MAX_RETRY)
378 run_queue(struct queue *queue)
382 if (LIST_EMPTY(&queue->queue))
385 it = go_background(queue);
391 show_queue(struct queue *queue)
394 int locked = 0; /* XXX */
396 if (LIST_EMPTY(&queue->queue)) {
397 printf("Mail queue is empty\n");
401 LIST_FOREACH(it, &queue->queue, next) {
402 printf("ID\t: %s%s\n"
407 it->sender, it->addr);
409 if (LIST_NEXT(it, next) != NULL)
418 * - use group permissions
419 * - proper sysexit codes
423 main(int argc, char **argv)
425 struct sigaction act;
427 char *own_name = NULL;
430 int nodot = 0, showq = 0, queue_only = 0, newaliases = 0;
431 int recp_from_header = 0;
436 * We never run as root. If called by root, drop permissions
439 if (geteuid() == 0 || getuid() == 0) {
443 pw = getpwnam(DMA_ROOT_USER);
446 errx(EX_CONFIG, "user '%s' not found", DMA_ROOT_USER);
448 err(EX_OSERR, "cannot drop root privileges");
451 if (setuid(pw->pw_uid) != 0)
452 err(EX_OSERR, "cannot drop root privileges");
454 if (geteuid() == 0 || getuid() == 0)
455 errx(EX_OSERR, "cannot drop root privileges");
461 bzero(&queue, sizeof(queue));
462 LIST_INIT(&queue.queue);
464 own_name = basename(argv[0]);
466 if (strcmp(own_name, "mailq") == 0) {
469 if (argc != 0 && strcmp(argv[0], "-Ac") != 0)
470 errx(EX_USAGE, "invalid arguments");
472 } else if (strcmp(own_name, "newaliases") == 0) {
475 } else if (strcmp(own_name, "hoststat") == 0 ||
476 strcmp(own_name, "purgestat") == 0) {
481 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
484 /* -AX is being ignored, except for -A{c,m} */
485 if (optarg[0] == 'c' || optarg[0] == 'm') {
488 /* Else FALLTHROUGH */
490 /* -bX is being ignored, except for -bp */
491 if (optarg[0] == 'p') {
494 } else if (optarg[0] == 'q') {
498 /* Else FALLTHROUGH */
503 logident_base = optarg;
511 recp_from_header = 1;
515 /* -oX is being ignored, except for -oi */
516 if (optarg[0] != 'i')
518 /* Else FALLTHROUGH */
524 /* Don't let getopt slup up other arguments */
525 if (optarg && *optarg == '-')
530 /* Ignored options */
551 /* Else FALLTHROUGH */
554 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
562 if (argc != 0 && (showq || doqueue))
563 errx(EX_USAGE, "sending mail and queue operations are mutually exclusive");
565 if (showq + doqueue > 1)
566 errx(EX_USAGE, "conflicting queue operations");
569 if (logident_base == NULL)
570 logident_base = "dma";
573 act.sa_handler = sighup_handler;
575 sigemptyset(&act.sa_mask);
576 if (sigaction(SIGHUP, &act, NULL) != 0)
577 syslog(LOG_WARNING, "can not set signal handler: %m");
579 parse_conf(CONF_PATH "/dma.conf");
581 if (config.authpath != NULL)
582 parse_authfile(config.authpath);
585 if (load_queue(&queue) < 0)
586 errlog(EX_NOINPUT, "can not load queue");
593 if (load_queue(&queue) < 0)
594 errlog(EX_NOINPUT, "can not load queue");
599 if (read_aliases() != 0)
600 errlog(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
605 if ((sender = set_from(&queue, sender)) == NULL)
606 errlog(EX_SOFTWARE, NULL);
608 if (newspoolf(&queue) != 0)
609 errlog(EX_CANTCREAT, "can not create temp file in `%s'", config.spooldir);
611 setlogident("%s", queue.id);
613 for (i = 0; i < argc; i++) {
614 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
615 errlogx(EX_DATAERR, "invalid recipient `%s'", argv[i]);
618 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
619 errlogx(EX_NOINPUT, "no recipients");
621 if (readmail(&queue, nodot, recp_from_header) != 0)
622 errlog(EX_NOINPUT, "can not read mail");
624 if (LIST_EMPTY(&queue.queue))
625 errlogx(EX_NOINPUT, "no recipients");
627 if (linkspool(&queue) != 0)
628 errlog(EX_CANTCREAT, "can not create spools");
630 /* From here on the mail is safe. */
632 if (config.features & DEFER || queue_only)