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;
74 struct config config = {
77 .aliases = "/etc/aliases",
78 .spooldir = "/var/spool/dma",
83 .masquerade_host = NULL,
84 .masquerade_user = NULL,
89 sighup_handler(int signo)
91 (void)signo; /* so that gcc doesn't complain */
95 set_from(struct queue *queue, const char *osender)
100 sender = strdup(osender);
103 } else if (getenv("EMAIL") != NULL) {
104 sender = strdup(getenv("EMAIL"));
108 const char *from_user = username;
109 const char *from_host = hostname();
111 if (config.masquerade_user)
112 from_user = config.masquerade_user;
113 if (config.masquerade_host)
114 from_host = config.masquerade_host;
115 if (asprintf(&sender, "%s@%s", from_user, from_host) <= 0)
119 if (strchr(sender, '\n') != NULL) {
124 queue->sender = sender;
131 yyin = fopen(config.aliases, "r");
134 * Non-existing aliases file is not a fatal error
138 /* Other problems are. */
142 return (-1); /* fatal error, probably malloc() */
148 do_alias(struct queue *queue, const char *addr)
154 LIST_FOREACH(al, &aliases, next) {
155 if (strcmp(al->alias, addr) != 0)
157 SLIST_FOREACH(sit, &al->dests, next) {
158 if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
168 add_recp(struct queue *queue, const char *str, int expand)
170 struct qitem *it, *tit;
175 it = calloc(1, sizeof(*it));
178 it->addr = strdup(str);
179 if (it->addr == NULL)
182 it->sender = queue->sender;
183 host = strrchr(it->addr, '@');
185 (strcmp(host + 1, hostname()) == 0 ||
186 strcmp(host + 1, "localhost") == 0)) {
189 LIST_FOREACH(tit, &queue->queue, next) {
190 /* weed out duplicate dests */
191 if (strcmp(tit->addr, it->addr) == 0) {
197 LIST_INSERT_HEAD(&queue->queue, it, next);
198 if (strrchr(it->addr, '@') == NULL) {
201 aliased = do_alias(queue, it->addr);
202 if (!aliased && expand == EXPAND_WILDCARD)
203 aliased = do_alias(queue, "*");
207 LIST_REMOVE(it, next);
209 /* Local destination, check */
210 pw = getpwnam(it->addr);
213 /* XXX read .forward */
229 static struct qitem *
230 go_background(struct queue *queue)
236 if (daemonize && daemon(0, 0) != 0) {
237 syslog(LOG_ERR, "can not daemonize: %m");
242 bzero(&sa, sizeof(sa));
243 sa.sa_handler = SIG_IGN;
244 sigaction(SIGCHLD, &sa, NULL);
246 LIST_FOREACH(it, &queue->queue, next) {
247 /* No need to fork for the last dest */
248 if (LIST_NEXT(it, next) == NULL)
254 syslog(LOG_ERR, "can not fork: %m");
262 * return and deliver mail
266 * If necessary, acquire the queue and * mail files.
267 * If this fails, we probably were raced by another
270 setlogident("%s", it->queueid);
271 if (acquirespool(it) < 0)
273 dropspool(queue, it);
286 syslog(LOG_CRIT, "reached dead code");
291 deliver(struct qitem *it)
294 unsigned int backoff = MIN_RETRY;
298 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
301 syslog(LOG_INFO, "trying delivery");
304 error = deliver_remote(it);
306 error = deliver_local(it);
311 syslog(LOG_INFO, "delivery successful");
315 if (stat(it->queuefn, &st) != 0) {
316 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
319 if (gettimeofday(&now, NULL) == 0 &&
320 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
321 snprintf(errmsg, sizeof(errmsg),
322 "Could not deliver for the last %d seconds. Giving up.",
326 if (sleep(backoff) == 0) {
327 /* pick the next backoff between [1.5, 2.5) times backoff */
328 backoff = backoff + backoff / 2 + random() % backoff;
329 if (backoff > MAX_RETRY)
345 run_queue(struct queue *queue)
349 if (LIST_EMPTY(&queue->queue))
352 it = go_background(queue);
358 show_queue(struct queue *queue)
361 int locked = 0; /* XXX */
363 if (LIST_EMPTY(&queue->queue)) {
364 printf("Mail queue is empty\n");
368 LIST_FOREACH(it, &queue->queue, next) {
369 printf("ID\t: %s%s\n"
374 it->sender, it->addr);
376 if (LIST_NEXT(it, next) != NULL)
385 * - use group permissions
386 * - proper sysexit codes
390 main(int argc, char **argv)
392 struct sigaction act;
396 int nodot = 0, doqueue = 0, showq = 0, queue_only = 0;
397 int recp_from_header = 0;
402 * We never run as root. If called by root, drop permissions
405 if (geteuid() == 0 || getuid() == 0) {
408 pw = getpwnam(DMA_ROOT_USER);
410 err(1, "cannot drop root privileges");
412 if (setuid(pw->pw_uid) != 0)
413 err(1, "cannot drop root privileges");
415 if (geteuid() == 0 || getuid() == 0)
416 errx(1, "cannot drop root privileges");
422 bzero(&queue, sizeof(queue));
423 LIST_INIT(&queue.queue);
425 if (strcmp(argv[0], "mailq") == 0) {
429 errx(1, "invalid arguments");
434 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
437 /* -AX is being ignored, except for -A{c,m} */
438 if (optarg[0] == 'c' || optarg[0] == 'm') {
441 /* else FALLTRHOUGH */
443 /* -bX is being ignored, except for -bp */
444 if (optarg[0] == 'p') {
447 } else if (optarg[0] == 'q') {
451 /* else FALLTRHOUGH */
456 logident_base = optarg;
464 recp_from_header = 1;
468 /* -oX is being ignored, except for -oi */
469 if (optarg[0] != 'i')
471 /* else FALLTRHOUGH */
482 /* Ignored options */
505 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
513 if (argc != 0 && (showq || doqueue))
514 errx(1, "sending mail and queue operations are mutually exclusive");
516 if (showq + doqueue > 1)
517 errx(1, "conflicting queue operations");
520 if (logident_base == NULL)
521 logident_base = "dma";
524 act.sa_handler = sighup_handler;
526 sigemptyset(&act.sa_mask);
527 if (sigaction(SIGHUP, &act, NULL) != 0)
528 syslog(LOG_WARNING, "can not set signal handler: %m");
530 parse_conf(CONF_PATH "/dma.conf");
532 if (config.authpath != NULL)
533 parse_authfile(config.authpath);
536 if (load_queue(&queue) < 0)
537 errlog(1, "can not load queue");
543 if (load_queue(&queue) < 0)
544 errlog(1, "can not load queue");
549 if (read_aliases() != 0)
550 errlog(1, "can not read aliases file `%s'", config.aliases);
552 if ((sender = set_from(&queue, sender)) == NULL)
555 if (newspoolf(&queue) != 0)
556 errlog(1, "can not create temp file");
558 setlogident("%s", queue.id);
560 for (i = 0; i < argc; i++) {
561 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
562 errlogx(1, "invalid recipient `%s'", argv[i]);
565 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
566 errlogx(1, "no recipients");
568 if (readmail(&queue, nodot, recp_from_header) != 0)
569 errlog(1, "can not read mail");
571 if (LIST_EMPTY(&queue.queue))
572 errlogx(1, "no recipients");
574 if (linkspool(&queue) != 0)
575 errlog(1, "can not create spools");
577 /* From here on the mail is safe. */
579 if (config.features & DEFER || queue_only)