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>
64 static void deliver(struct qitem *);
66 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
67 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
68 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
69 char username[USERNAME_SIZE];
71 const char *logident_base;
72 char errmsg[ERRMSG_SIZE];
74 static int daemonize = 1;
75 static int doqueue = 0;
77 struct config config = {
80 .aliases = "/etc/aliases",
81 .spooldir = "/var/spool/dma",
86 .masquerade_host = NULL,
87 .masquerade_user = NULL,
92 sighup_handler(int signo)
94 (void)signo; /* so that gcc doesn't complain */
98 set_from(struct queue *queue, const char *osender)
105 } else if (getenv("EMAIL") != NULL) {
106 addr = getenv("EMAIL");
108 if (config.masquerade_user)
109 addr = config.masquerade_user;
114 if (!strchr(addr, '@')) {
115 const char *from_host = hostname();
117 if (config.masquerade_host)
118 from_host = config.masquerade_host;
120 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
123 sender = strdup(addr);
128 if (strchr(sender, '\n') != NULL) {
133 queue->sender = sender;
140 yyin = fopen(config.aliases, "r");
143 * Non-existing aliases file is not a fatal error
147 /* Other problems are. */
151 return (-1); /* fatal error, probably malloc() */
157 do_alias(struct queue *queue, const char *addr)
163 LIST_FOREACH(al, &aliases, next) {
164 if (strcmp(al->alias, addr) != 0)
166 SLIST_FOREACH(sit, &al->dests, next) {
167 if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
177 add_recp(struct queue *queue, const char *str, int expand)
179 struct qitem *it, *tit;
184 it = calloc(1, sizeof(*it));
187 it->addr = strdup(str);
188 if (it->addr == NULL)
191 it->sender = queue->sender;
192 host = strrchr(it->addr, '@');
194 (strcmp(host + 1, hostname()) == 0 ||
195 strcmp(host + 1, "localhost") == 0)) {
198 LIST_FOREACH(tit, &queue->queue, next) {
199 /* weed out duplicate dests */
200 if (strcmp(tit->addr, it->addr) == 0) {
206 LIST_INSERT_HEAD(&queue->queue, it, next);
209 * Do local delivery if there is no @.
210 * Do not do local delivery when NULLCLIENT is set.
212 if (strrchr(it->addr, '@') == NULL && (config.features & NULLCLIENT) == 0) {
215 aliased = do_alias(queue, it->addr);
216 if (!aliased && expand == EXPAND_WILDCARD)
217 aliased = do_alias(queue, "*");
221 LIST_REMOVE(it, next);
223 /* Local destination, check */
224 pw = getpwnam(it->addr);
227 /* XXX read .forward */
243 static struct qitem *
244 go_background(struct queue *queue)
250 if (daemonize && daemon(0, 0) != 0) {
251 syslog(LOG_ERR, "can not daemonize: %m");
256 bzero(&sa, sizeof(sa));
257 sa.sa_handler = SIG_IGN;
258 sigaction(SIGCHLD, &sa, NULL);
260 LIST_FOREACH(it, &queue->queue, next) {
261 /* No need to fork for the last dest */
262 if (LIST_NEXT(it, next) == NULL)
268 syslog(LOG_ERR, "can not fork: %m");
276 * return and deliver mail
280 * If necessary, acquire the queue and * mail files.
281 * If this fails, we probably were raced by another
282 * process. It is okay to be raced if we're supposed
283 * to flush the queue.
285 setlogident("%s", it->queueid);
286 switch (acquirespool(it)) {
292 syslog(LOG_WARNING, "could not lock queue file");
297 dropspool(queue, it);
310 syslog(LOG_CRIT, "reached dead code");
315 deliver(struct qitem *it)
318 unsigned int backoff = MIN_RETRY, slept;
322 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
325 syslog(LOG_INFO, "<%s> trying delivery", it->addr);
328 error = deliver_remote(it);
330 error = deliver_local(it);
334 syslog(LOG_INFO, "<%s> delivery successful", it->addr);
339 if (stat(it->queuefn, &st) != 0) {
340 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
343 if (gettimeofday(&now, NULL) == 0 &&
344 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
345 snprintf(errmsg, sizeof(errmsg),
346 "Could not deliver for the last %d seconds. Giving up.",
350 for (slept = 0; slept < backoff;) {
351 slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT);
352 if (flushqueue_since(slept)) {
357 if (slept >= backoff) {
358 /* pick the next backoff between [1.5, 2.5) times backoff */
359 backoff = backoff + backoff / 2 + random() % backoff;
360 if (backoff > MAX_RETRY)
376 run_queue(struct queue *queue)
380 if (LIST_EMPTY(&queue->queue))
383 it = go_background(queue);
389 show_queue(struct queue *queue)
392 int locked = 0; /* XXX */
394 if (LIST_EMPTY(&queue->queue)) {
395 printf("Mail queue is empty\n");
399 LIST_FOREACH(it, &queue->queue, next) {
400 printf("ID\t: %s%s\n"
405 it->sender, it->addr);
407 if (LIST_NEXT(it, next) != NULL)
416 * - use group permissions
417 * - proper sysexit codes
421 main(int argc, char **argv)
423 struct sigaction act;
427 int nodot = 0, showq = 0, queue_only = 0;
428 int recp_from_header = 0;
433 * We never run as root. If called by root, drop permissions
436 if (geteuid() == 0 || getuid() == 0) {
440 pw = getpwnam(DMA_ROOT_USER);
443 errx(EX_CONFIG, "user '%s' not found", DMA_ROOT_USER);
445 err(EX_OSERR, "cannot drop root privileges");
448 if (setuid(pw->pw_uid) != 0)
449 err(EX_OSERR, "cannot drop root privileges");
451 if (geteuid() == 0 || getuid() == 0)
452 errx(EX_OSERR, "cannot drop root privileges");
458 bzero(&queue, sizeof(queue));
459 LIST_INIT(&queue.queue);
461 if (strcmp(basename(argv[0]), "mailq") == 0) {
465 errx(EX_USAGE, "invalid arguments");
467 } else if (strcmp(argv[0], "newaliases") == 0) {
468 logident_base = "dma";
469 setlogident("%s", logident_base);
471 if (read_aliases() != 0)
472 errx(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
477 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
480 /* -AX is being ignored, except for -A{c,m} */
481 if (optarg[0] == 'c' || optarg[0] == 'm') {
484 /* else FALLTRHOUGH */
486 /* -bX is being ignored, except for -bp */
487 if (optarg[0] == 'p') {
490 } else if (optarg[0] == 'q') {
494 /* else FALLTRHOUGH */
499 logident_base = optarg;
507 recp_from_header = 1;
511 /* -oX is being ignored, except for -oi */
512 if (optarg[0] != 'i')
514 /* else FALLTRHOUGH */
522 /* Don't let getopt slup up other arguments */
523 if (optarg && *optarg == '-')
528 /* Ignored options */
551 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
559 if (argc != 0 && (showq || doqueue))
560 errx(EX_USAGE, "sending mail and queue operations are mutually exclusive");
562 if (showq + doqueue > 1)
563 errx(EX_USAGE, "conflicting queue operations");
566 if (logident_base == NULL)
567 logident_base = "dma";
568 setlogident("%s", logident_base);
570 act.sa_handler = sighup_handler;
572 sigemptyset(&act.sa_mask);
573 if (sigaction(SIGHUP, &act, NULL) != 0)
574 syslog(LOG_WARNING, "can not set signal handler: %m");
576 parse_conf(CONF_PATH "/dma.conf");
578 if (config.authpath != NULL)
579 parse_authfile(config.authpath);
582 if (load_queue(&queue) < 0)
583 errlog(EX_NOINPUT, "can not load queue");
590 if (load_queue(&queue) < 0)
591 errlog(EX_NOINPUT, "can not load queue");
596 if (read_aliases() != 0)
597 errlog(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
599 if ((sender = set_from(&queue, sender)) == NULL)
600 errlog(EX_SOFTWARE, "set_from()");
602 if (newspoolf(&queue) != 0)
603 errlog(EX_CANTCREAT, "can not create temp file in `%s'", config.spooldir);
605 setlogident("%s", queue.id);
607 for (i = 0; i < argc; i++) {
608 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
609 errlogx(EX_DATAERR, "invalid recipient `%s'", argv[i]);
612 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
613 errlogx(EX_NOINPUT, "no recipients");
615 if (readmail(&queue, nodot, recp_from_header) != 0)
616 errlog(EX_NOINPUT, "can not read mail");
618 if (LIST_EMPTY(&queue.queue))
619 errlogx(EX_NOINPUT, "no recipients");
621 if (linkspool(&queue) != 0)
622 errlog(EX_CANTCREAT, "can not create spools");
624 /* From here on the mail is safe. */
626 if (config.features & DEFER || queue_only)