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 static void deliver(struct qitem *);
65 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
66 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
67 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
68 char username[USERNAME_SIZE];
70 const char *logident_base;
71 char errmsg[ERRMSG_SIZE];
73 static int daemonize = 1;
74 static int doqueue = 0;
76 struct config config = {
79 .aliases = "/etc/aliases",
80 .spooldir = "/var/spool/dma",
85 .masquerade_host = NULL,
86 .masquerade_user = NULL,
91 sighup_handler(int signo)
93 (void)signo; /* so that gcc doesn't complain */
97 set_from(struct queue *queue, const char *osender)
104 } else if (getenv("EMAIL") != NULL) {
105 addr = getenv("EMAIL");
107 if (config.masquerade_user)
108 addr = config.masquerade_user;
113 if (!strchr(addr, '@')) {
114 const char *from_host = hostname();
116 if (config.masquerade_host)
117 from_host = config.masquerade_host;
119 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
122 sender = strdup(addr);
127 if (strchr(sender, '\n') != NULL) {
132 queue->sender = sender;
139 yyin = fopen(config.aliases, "r");
142 * Non-existing aliases file is not a fatal error
146 /* Other problems are. */
150 return (-1); /* fatal error, probably malloc() */
156 do_alias(struct queue *queue, const char *addr)
162 LIST_FOREACH(al, &aliases, next) {
163 if (strcmp(al->alias, addr) != 0)
165 SLIST_FOREACH(sit, &al->dests, next) {
166 if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
176 add_recp(struct queue *queue, const char *str, int expand)
178 struct qitem *it, *tit;
183 it = calloc(1, sizeof(*it));
186 it->addr = strdup(str);
187 if (it->addr == NULL)
190 it->sender = queue->sender;
191 host = strrchr(it->addr, '@');
193 (strcmp(host + 1, hostname()) == 0 ||
194 strcmp(host + 1, "localhost") == 0)) {
197 LIST_FOREACH(tit, &queue->queue, next) {
198 /* weed out duplicate dests */
199 if (strcmp(tit->addr, it->addr) == 0) {
205 LIST_INSERT_HEAD(&queue->queue, it, next);
208 * Do local delivery if there is no @.
209 * Do not do local delivery when NULLCLIENT is set.
211 if (strrchr(it->addr, '@') == NULL && (config.features & NULLCLIENT) == 0) {
214 aliased = do_alias(queue, it->addr);
215 if (!aliased && expand == EXPAND_WILDCARD)
216 aliased = do_alias(queue, "*");
220 LIST_REMOVE(it, next);
222 /* Local destination, check */
223 pw = getpwnam(it->addr);
226 /* XXX read .forward */
242 static struct qitem *
243 go_background(struct queue *queue)
249 if (daemonize && daemon(0, 0) != 0) {
250 syslog(LOG_ERR, "can not daemonize: %m");
255 bzero(&sa, sizeof(sa));
256 sa.sa_handler = SIG_IGN;
257 sigaction(SIGCHLD, &sa, NULL);
259 LIST_FOREACH(it, &queue->queue, next) {
260 /* No need to fork for the last dest */
261 if (LIST_NEXT(it, next) == NULL)
267 syslog(LOG_ERR, "can not fork: %m");
275 * return and deliver mail
279 * If necessary, acquire the queue and * mail files.
280 * If this fails, we probably were raced by another
281 * process. It is okay to be raced if we're supposed
282 * to flush the queue.
284 setlogident("%s", it->queueid);
285 switch (acquirespool(it)) {
291 syslog(LOG_WARNING, "could not lock queue file");
296 dropspool(queue, it);
309 syslog(LOG_CRIT, "reached dead code");
314 deliver(struct qitem *it)
317 unsigned int backoff = MIN_RETRY, slept;
321 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
324 syslog(LOG_INFO, "<%s> trying delivery", it->addr);
327 error = deliver_remote(it);
329 error = deliver_local(it);
334 syslog(LOG_INFO, "<%s> delivery successful", it->addr);
338 if (stat(it->queuefn, &st) != 0) {
339 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
342 if (gettimeofday(&now, NULL) == 0 &&
343 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
344 snprintf(errmsg, sizeof(errmsg),
345 "Could not deliver for the last %d seconds. Giving up.",
349 for (slept = 0; slept < backoff;) {
350 slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT);
351 if (flushqueue_since(slept)) {
356 if (slept >= backoff) {
357 /* pick the next backoff between [1.5, 2.5) times backoff */
358 backoff = backoff + backoff / 2 + random() % backoff;
359 if (backoff > MAX_RETRY)
375 run_queue(struct queue *queue)
379 if (LIST_EMPTY(&queue->queue))
382 it = go_background(queue);
388 show_queue(struct queue *queue)
391 int locked = 0; /* XXX */
393 if (LIST_EMPTY(&queue->queue)) {
394 printf("Mail queue is empty\n");
398 LIST_FOREACH(it, &queue->queue, next) {
399 printf("ID\t: %s%s\n"
404 it->sender, it->addr);
406 if (LIST_NEXT(it, next) != NULL)
415 * - use group permissions
416 * - proper sysexit codes
420 main(int argc, char **argv)
422 struct sigaction act;
426 int nodot = 0, showq = 0, queue_only = 0;
427 int recp_from_header = 0;
432 * We never run as root. If called by root, drop permissions
435 if (geteuid() == 0 || getuid() == 0) {
439 pw = getpwnam(DMA_ROOT_USER);
442 errx(EX_CONFIG, "user '%s' not found", DMA_ROOT_USER);
444 err(EX_OSERR, "cannot drop root privileges");
447 if (setuid(pw->pw_uid) != 0)
448 err(EX_OSERR, "cannot drop root privileges");
450 if (geteuid() == 0 || getuid() == 0)
451 errx(EX_OSERR, "cannot drop root privileges");
457 bzero(&queue, sizeof(queue));
458 LIST_INIT(&queue.queue);
460 if (strcmp(argv[0], "mailq") == 0) {
464 errx(EX_USAGE, "invalid arguments");
466 } else if (strcmp(argv[0], "newaliases") == 0) {
467 logident_base = "dma";
468 setlogident("%s", logident_base);
470 if (read_aliases() != 0)
471 errx(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
476 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
479 /* -AX is being ignored, except for -A{c,m} */
480 if (optarg[0] == 'c' || optarg[0] == 'm') {
483 /* else FALLTRHOUGH */
485 /* -bX is being ignored, except for -bp */
486 if (optarg[0] == 'p') {
489 } else if (optarg[0] == 'q') {
493 /* else FALLTRHOUGH */
498 logident_base = optarg;
506 recp_from_header = 1;
510 /* -oX is being ignored, except for -oi */
511 if (optarg[0] != 'i')
513 /* else FALLTRHOUGH */
521 /* Don't let getopt slup up other arguments */
522 if (optarg && *optarg == '-')
527 /* Ignored options */
550 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
558 if (argc != 0 && (showq || doqueue))
559 errx(EX_USAGE, "sending mail and queue operations are mutually exclusive");
561 if (showq + doqueue > 1)
562 errx(EX_USAGE, "conflicting queue operations");
565 if (logident_base == NULL)
566 logident_base = "dma";
567 setlogident("%s", logident_base);
569 act.sa_handler = sighup_handler;
571 sigemptyset(&act.sa_mask);
572 if (sigaction(SIGHUP, &act, NULL) != 0)
573 syslog(LOG_WARNING, "can not set signal handler: %m");
575 parse_conf(CONF_PATH "/dma.conf");
577 if (config.authpath != NULL)
578 parse_authfile(config.authpath);
581 if (load_queue(&queue) < 0)
582 errlog(EX_NOINPUT, "can not load queue");
589 if (load_queue(&queue) < 0)
590 errlog(EX_NOINPUT, "can not load queue");
595 if (read_aliases() != 0)
596 errlog(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
598 if ((sender = set_from(&queue, sender)) == NULL)
599 errlog(EX_SOFTWARE, "set_from()");
601 if (newspoolf(&queue) != 0)
602 errlog(EX_CANTCREAT, "can not create temp file in `%s'", config.spooldir);
604 setlogident("%s", queue.id);
606 for (i = 0; i < argc; i++) {
607 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
608 errlogx(EX_DATAERR, "invalid recipient `%s'", argv[i]);
611 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
612 errlogx(EX_NOINPUT, "no recipients");
614 if (readmail(&queue, nodot, recp_from_header) != 0)
615 errlog(EX_NOINPUT, "can not read mail");
617 if (LIST_EMPTY(&queue.queue))
618 errlogx(EX_NOINPUT, "no recipients");
620 if (linkspool(&queue) != 0)
621 errlog(EX_CANTCREAT, "can not create spools");
623 /* From here on the mail is safe. */
625 if (config.features & DEFER || queue_only)