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>
62 extern int yyparse(void);
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
, "trying delivery");
327 error
= deliver_remote(it
);
329 error
= deliver_local(it
);
334 syslog(LOG_INFO
, "delivery successful");
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(1, "user '%s' not found", DMA_ROOT_USER
);
444 err(1, "cannot drop root privileges");
447 if (setuid(pw
->pw_uid
) != 0)
448 err(1, "cannot drop root privileges");
450 if (geteuid() == 0 || getuid() == 0)
451 errx(1, "cannot drop root privileges");
457 bzero(&queue
, sizeof(queue
));
458 LIST_INIT(&queue
.queue
);
460 if (strcmp(argv
[0], "mailq") == 0) {
463 if (argc
!= 0 && strcmp(argv
[0], "-Ac") != 0)
464 errx(1, "invalid arguments");
466 } else if (strcmp(argv
[0], "newaliases") == 0) {
467 logident_base
= "dma";
470 if (read_aliases() != 0)
471 errx(1, "could not parse aliases file `%s'", config
.aliases
);
473 } else if (strcmp(argv
[0], "hoststat") == 0 ||
474 strcmp(argv
[0], "purgestat") == 0) {
479 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
482 /* -AX is being ignored, except for -A{c,m} */
483 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
486 /* else FALLTHROUGH */
488 /* -bX is being ignored, except for -bp */
489 if (optarg
[0] == 'p') {
492 } else if (optarg
[0] == 'q') {
496 /* else FALLTHROUGH */
501 logident_base
= optarg
;
509 recp_from_header
= 1;
513 /* -oX is being ignored, except for -oi */
514 if (optarg
[0] != 'i')
516 /* else FALLTHROUGH */
524 /* Don't let getopt slup up other arguments */
525 if (optarg
&& *optarg
== '-')
530 /* Ignored options */
553 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
561 if (argc
!= 0 && (showq
|| doqueue
))
562 errx(1, "sending mail and queue operations are mutually exclusive");
564 if (showq
+ doqueue
> 1)
565 errx(1, "conflicting queue operations");
568 if (logident_base
== NULL
)
569 logident_base
= "dma";
572 act
.sa_handler
= sighup_handler
;
574 sigemptyset(&act
.sa_mask
);
575 if (sigaction(SIGHUP
, &act
, NULL
) != 0)
576 syslog(LOG_WARNING
, "can not set signal handler: %m");
578 parse_conf(CONF_PATH
"/dma.conf");
580 if (config
.authpath
!= NULL
)
581 parse_authfile(config
.authpath
);
584 if (load_queue(&queue
) < 0)
585 errlog(1, "can not load queue");
592 if (load_queue(&queue
) < 0)
593 errlog(1, "can not load queue");
598 if (read_aliases() != 0)
599 errlog(1, "could not parse aliases file `%s'", config
.aliases
);
601 if ((sender
= set_from(&queue
, sender
)) == NULL
)
604 if (newspoolf(&queue
) != 0)
605 errlog(1, "can not create temp file in `%s'", config
.spooldir
);
607 setlogident("%s", queue
.id
);
609 for (i
= 0; i
< argc
; i
++) {
610 if (add_recp(&queue
, argv
[i
], EXPAND_WILDCARD
) != 0)
611 errlogx(1, "invalid recipient `%s'", argv
[i
]);
614 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
615 errlogx(1, "no recipients");
617 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
618 errlog(1, "can not read mail");
620 if (LIST_EMPTY(&queue
.queue
))
621 errlogx(1, "no recipients");
623 if (linkspool(&queue
) != 0)
624 errlog(1, "can not create spools");
626 /* From here on the mail is safe. */
628 if (config
.features
& DEFER
|| queue_only
)