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
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
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
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/queue.h>
61 extern int yyparse(void);
62 static void deliver(struct qitem *);
64 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
65 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
66 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
67 char username[USERNAME_SIZE];
69 const char *logident_base;
70 char errmsg[ERRMSG_SIZE];
72 static int daemonize = 1;
73 static int doqueue = 0;
75 struct config config = {
78 .aliases = "/etc/aliases",
79 .spooldir = "/var/spool/dma",
84 .masquerade_host = NULL,
85 .masquerade_user = NULL,
90 sighup_handler(int signo)
92 (void)signo; /* so that gcc doesn't complain */
96 set_from(struct queue *queue, const char *osender)
103 } else if (getenv("EMAIL") != NULL) {
104 addr = getenv("EMAIL");
106 if (config.masquerade_user)
107 addr = config.masquerade_user;
112 if (!strchr(addr, '@')) {
113 const char *from_host = hostname();
115 if (config.masquerade_host)
116 from_host = config.masquerade_host;
118 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
121 sender = strdup(addr);
126 if (strchr(sender, '\n') != NULL) {
131 queue->sender = sender;
138 yyin = fopen(config.aliases, "r");
141 * Non-existing aliases file is not a fatal error
145 /* Other problems are. */
149 return (-1); /* fatal error, probably malloc() */
155 do_alias(struct queue *queue, const char *addr)
161 LIST_FOREACH(al, &aliases, next) {
162 if (strcmp(al->alias, addr) != 0)
164 SLIST_FOREACH(sit, &al->dests, next) {
165 if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
175 add_recp(struct queue *queue, const char *str, int expand)
177 struct qitem *it, *tit;
182 it = calloc(1, sizeof(*it));
185 it->addr = strdup(str);
186 if (it->addr == NULL)
189 it->sender = queue->sender;
190 host = strrchr(it->addr, '@');
192 (strcmp(host + 1, hostname()) == 0 ||
193 strcmp(host + 1, "localhost") == 0)) {
196 LIST_FOREACH(tit, &queue->queue, next) {
197 /* weed out duplicate dests */
198 if (strcmp(tit->addr, it->addr) == 0) {
204 LIST_INSERT_HEAD(&queue->queue, it, next);
207 * Do local delivery if there is no @.
208 * Do not do local delivery when NULLCLIENT is set.
210 if (strrchr(it->addr, '@') == NULL && (config.features & NULLCLIENT) == 0) {
213 aliased = do_alias(queue, it->addr);
214 if (!aliased && expand == EXPAND_WILDCARD)
215 aliased = do_alias(queue, "*");
219 LIST_REMOVE(it, next);
221 /* Local destination, check */
222 pw = getpwnam(it->addr);
225 /* XXX read .forward */
241 static struct qitem *
242 go_background(struct queue *queue)
248 if (daemonize && daemon(0, 0) != 0) {
249 syslog(LOG_ERR, "can not daemonize: %m");
254 bzero(&sa, sizeof(sa));
255 sa.sa_handler = SIG_IGN;
256 sigaction(SIGCHLD, &sa, NULL);
258 LIST_FOREACH(it, &queue->queue, next) {
259 /* No need to fork for the last dest */
260 if (LIST_NEXT(it, next) == NULL)
266 syslog(LOG_ERR, "can not fork: %m");
274 * return and deliver mail
278 * If necessary, acquire the queue and * mail files.
279 * If this fails, we probably were raced by another
280 * process. It is okay to be raced if we're supposed
281 * to flush the queue.
283 setlogident("%s", it->queueid);
284 switch (acquirespool(it)) {
290 syslog(LOG_WARNING, "could not lock queue file");
295 dropspool(queue, it);
308 syslog(LOG_CRIT, "reached dead code");
313 deliver(struct qitem *it)
316 unsigned int backoff = MIN_RETRY, slept;
320 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
323 syslog(LOG_INFO, "trying delivery");
326 error = deliver_remote(it);
328 error = deliver_local(it);
333 syslog(LOG_INFO, "delivery successful");
337 if (stat(it->queuefn, &st) != 0) {
338 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
341 if (gettimeofday(&now, NULL) == 0 &&
342 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
343 snprintf(errmsg, sizeof(errmsg),
344 "Could not deliver for the last %d seconds. Giving up.",
348 for (slept = 0; slept < backoff;) {
349 slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT);
350 if (flushqueue_since(slept)) {
355 if (slept >= backoff) {
356 /* pick the next backoff between [1.5, 2.5) times backoff */
357 backoff = backoff + backoff / 2 + random() % backoff;
358 if (backoff > MAX_RETRY)
374 run_queue(struct queue *queue)
378 if (LIST_EMPTY(&queue->queue))
381 it = go_background(queue);
387 show_queue(struct queue *queue)
390 int locked = 0; /* XXX */
392 if (LIST_EMPTY(&queue->queue)) {
393 printf("Mail queue is empty\n");
397 LIST_FOREACH(it, &queue->queue, next) {
398 printf("ID\t: %s%s\n"
403 it->sender, it->addr);
405 if (LIST_NEXT(it, next) != NULL)
414 * - use group permissions
415 * - proper sysexit codes
419 main(int argc, char **argv)
421 struct sigaction act;
425 int nodot = 0, showq = 0, queue_only = 0;
426 int recp_from_header = 0;
431 * We never run as root. If called by root, drop permissions
434 if (geteuid() == 0 || getuid() == 0) {
438 pw = getpwnam(DMA_ROOT_USER);
441 errx(1, "user '%s' not found", DMA_ROOT_USER);
443 err(1, "cannot drop root privileges");
446 if (setuid(pw->pw_uid) != 0)
447 err(1, "cannot drop root privileges");
449 if (geteuid() == 0 || getuid() == 0)
450 errx(1, "cannot drop root privileges");
456 bzero(&queue, sizeof(queue));
457 LIST_INIT(&queue.queue);
459 if (strcmp(argv[0], "mailq") == 0) {
462 if (argc != 0 && strcmp(argv[0], "-Ac") != 0)
463 errx(1, "invalid arguments");
465 } else if (strcmp(argv[0], "newaliases") == 0) {
466 logident_base = "dma";
469 if (read_aliases() != 0)
470 errx(1, "could not parse aliases file `%s'", config.aliases);
472 } else if (strcmp(argv[0], "hoststat") == 0 ||
473 strcmp(argv[0], "purgestat") == 0) {
478 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
481 /* -AX is being ignored, except for -A{c,m} */
482 if (optarg[0] == 'c' || optarg[0] == 'm') {
485 /* else FALLTHROUGH */
487 /* -bX is being ignored, except for -bp */
488 if (optarg[0] == 'p') {
491 } else if (optarg[0] == 'q') {
495 /* else FALLTHROUGH */
500 logident_base = optarg;
508 recp_from_header = 1;
512 /* -oX is being ignored, except for -oi */
513 if (optarg[0] != 'i')
515 /* else FALLTHROUGH */
523 /* Don't let getopt slup up other arguments */
524 if (optarg && *optarg == '-')
529 /* Ignored options */
552 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
560 if (argc != 0 && (showq || doqueue))
561 errx(1, "sending mail and queue operations are mutually exclusive");
563 if (showq + doqueue > 1)
564 errx(1, "conflicting queue operations");
567 if (logident_base == NULL)
568 logident_base = "dma";
571 act.sa_handler = sighup_handler;
573 sigemptyset(&act.sa_mask);
574 if (sigaction(SIGHUP, &act, NULL) != 0)
575 syslog(LOG_WARNING, "can not set signal handler: %m");
577 parse_conf(CONF_PATH "/dma.conf");
579 if (config.authpath != NULL)
580 parse_authfile(config.authpath);
583 if (load_queue(&queue) < 0)
584 errlog(1, "can not load queue");
591 if (load_queue(&queue) < 0)
592 errlog(1, "can not load queue");
597 if (read_aliases() != 0)
598 errlog(1, "could not parse aliases file `%s'", config.aliases);
600 if ((sender = set_from(&queue, sender)) == NULL)
603 if (newspoolf(&queue) != 0)
604 errlog(1, "can not create temp file in `%s'", config.spooldir);
606 setlogident("%s", queue.id);
608 for (i = 0; i < argc; i++) {
609 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
610 errlogx(1, "invalid recipient `%s'", argv[i]);
613 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
614 errlogx(1, "no recipients");
616 if (readmail(&queue, nodot, recp_from_header) != 0)
617 errlog(1, "can not read mail");
619 if (LIST_EMPTY(&queue.queue))
620 errlogx(1, "no recipients");
622 if (linkspool(&queue) != 0)
623 errlog(1, "can not create spools");
625 /* From here on the mail is safe. */
627 if (config.features & DEFER || queue_only)