]>
git.ipfire.org Git - people/ms/dma.git/blob - dma.c
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>
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
)
101 sender
= strdup(osender
);
104 } else if (getenv("EMAIL") != NULL
) {
105 sender
= strdup(getenv("EMAIL"));
109 const char *from_user
= username
;
110 const char *from_host
= hostname();
112 if (config
.masquerade_user
)
113 from_user
= config
.masquerade_user
;
114 if (config
.masquerade_host
)
115 from_host
= config
.masquerade_host
;
116 if (asprintf(&sender
, "%s@%s", from_user
, from_host
) <= 0)
120 if (strchr(sender
, '\n') != NULL
) {
125 queue
->sender
= sender
;
132 yyin
= fopen(config
.aliases
, "r");
135 * Non-existing aliases file is not a fatal error
139 /* Other problems are. */
143 return (-1); /* fatal error, probably malloc() */
149 do_alias(struct queue
*queue
, const char *addr
)
155 LIST_FOREACH(al
, &aliases
, next
) {
156 if (strcmp(al
->alias
, addr
) != 0)
158 SLIST_FOREACH(sit
, &al
->dests
, next
) {
159 if (add_recp(queue
, sit
->str
, EXPAND_ADDR
) != 0)
169 add_recp(struct queue
*queue
, const char *str
, int expand
)
171 struct qitem
*it
, *tit
;
176 it
= calloc(1, sizeof(*it
));
179 it
->addr
= strdup(str
);
180 if (it
->addr
== NULL
)
183 it
->sender
= queue
->sender
;
184 host
= strrchr(it
->addr
, '@');
186 (strcmp(host
+ 1, hostname()) == 0 ||
187 strcmp(host
+ 1, "localhost") == 0)) {
190 LIST_FOREACH(tit
, &queue
->queue
, next
) {
191 /* weed out duplicate dests */
192 if (strcmp(tit
->addr
, it
->addr
) == 0) {
198 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
199 if (strrchr(it
->addr
, '@') == NULL
) {
202 aliased
= do_alias(queue
, it
->addr
);
203 if (!aliased
&& expand
== EXPAND_WILDCARD
)
204 aliased
= do_alias(queue
, "*");
208 LIST_REMOVE(it
, next
);
210 /* Local destination, check */
211 pw
= getpwnam(it
->addr
);
214 /* XXX read .forward */
230 static struct qitem
*
231 go_background(struct queue
*queue
)
237 if (daemonize
&& daemon(0, 0) != 0) {
238 syslog(LOG_ERR
, "can not daemonize: %m");
243 bzero(&sa
, sizeof(sa
));
244 sa
.sa_handler
= SIG_IGN
;
245 sigaction(SIGCHLD
, &sa
, NULL
);
247 LIST_FOREACH(it
, &queue
->queue
, next
) {
248 /* No need to fork for the last dest */
249 if (LIST_NEXT(it
, next
) == NULL
)
255 syslog(LOG_ERR
, "can not fork: %m");
263 * return and deliver mail
267 * If necessary, acquire the queue and * mail files.
268 * If this fails, we probably were raced by another
269 * process. It is okay to be raced if we're supposed
270 * to flush the queue.
272 setlogident("%s", it
->queueid
);
273 switch (acquirespool(it
)) {
279 syslog(LOG_WARNING
, "could not lock queue file");
284 dropspool(queue
, it
);
297 syslog(LOG_CRIT
, "reached dead code");
302 deliver(struct qitem
*it
)
305 unsigned int backoff
= MIN_RETRY
, slept
;
309 snprintf(errmsg
, sizeof(errmsg
), "unknown bounce reason");
312 syslog(LOG_INFO
, "trying delivery");
315 error
= deliver_remote(it
);
317 error
= deliver_local(it
);
322 syslog(LOG_INFO
, "delivery successful");
326 if (stat(it
->queuefn
, &st
) != 0) {
327 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
330 if (gettimeofday(&now
, NULL
) == 0 &&
331 (now
.tv_sec
- st
.st_mtim
.tv_sec
> MAX_TIMEOUT
)) {
332 snprintf(errmsg
, sizeof(errmsg
),
333 "Could not deliver for the last %d seconds. Giving up.",
337 for (slept
= 0; slept
< backoff
;) {
338 slept
+= SLEEP_TIMEOUT
- sleep(SLEEP_TIMEOUT
);
339 if (flushqueue_since(slept
)) {
344 if (slept
>= backoff
) {
345 /* pick the next backoff between [1.5, 2.5) times backoff */
346 backoff
= backoff
+ backoff
/ 2 + random() % backoff
;
347 if (backoff
> MAX_RETRY
)
363 run_queue(struct queue
*queue
)
367 if (LIST_EMPTY(&queue
->queue
))
370 it
= go_background(queue
);
376 show_queue(struct queue
*queue
)
379 int locked
= 0; /* XXX */
381 if (LIST_EMPTY(&queue
->queue
)) {
382 printf("Mail queue is empty\n");
386 LIST_FOREACH(it
, &queue
->queue
, next
) {
387 printf("ID\t: %s%s\n"
392 it
->sender
, it
->addr
);
394 if (LIST_NEXT(it
, next
) != NULL
)
403 * - use group permissions
404 * - proper sysexit codes
408 main(int argc
, char **argv
)
410 struct sigaction act
;
414 int nodot
= 0, showq
= 0, queue_only
= 0;
415 int recp_from_header
= 0;
420 * We never run as root. If called by root, drop permissions
423 if (geteuid() == 0 || getuid() == 0) {
427 pw
= getpwnam(DMA_ROOT_USER
);
430 errx(1, "user '%s' not found", DMA_ROOT_USER
);
432 err(1, "cannot drop root privileges");
435 if (setuid(pw
->pw_uid
) != 0)
436 err(1, "cannot drop root privileges");
438 if (geteuid() == 0 || getuid() == 0)
439 errx(1, "cannot drop root privileges");
445 bzero(&queue
, sizeof(queue
));
446 LIST_INIT(&queue
.queue
);
448 if (strcmp(argv
[0], "mailq") == 0) {
452 errx(1, "invalid arguments");
457 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
460 /* -AX is being ignored, except for -A{c,m} */
461 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
464 /* else FALLTRHOUGH */
466 /* -bX is being ignored, except for -bp */
467 if (optarg
[0] == 'p') {
470 } else if (optarg
[0] == 'q') {
474 /* else FALLTRHOUGH */
479 logident_base
= optarg
;
487 recp_from_header
= 1;
491 /* -oX is being ignored, except for -oi */
492 if (optarg
[0] != 'i')
494 /* else FALLTRHOUGH */
502 /* Don't let getopt slup up other arguments */
503 if (optarg
&& *optarg
== '-')
508 /* Ignored options */
531 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
539 if (argc
!= 0 && (showq
|| doqueue
))
540 errx(1, "sending mail and queue operations are mutually exclusive");
542 if (showq
+ doqueue
> 1)
543 errx(1, "conflicting queue operations");
546 if (logident_base
== NULL
)
547 logident_base
= "dma";
550 act
.sa_handler
= sighup_handler
;
552 sigemptyset(&act
.sa_mask
);
553 if (sigaction(SIGHUP
, &act
, NULL
) != 0)
554 syslog(LOG_WARNING
, "can not set signal handler: %m");
556 parse_conf(CONF_PATH
"/dma.conf");
558 if (config
.authpath
!= NULL
)
559 parse_authfile(config
.authpath
);
562 if (load_queue(&queue
) < 0)
563 errlog(1, "can not load queue");
570 if (load_queue(&queue
) < 0)
571 errlog(1, "can not load queue");
576 if (read_aliases() != 0)
577 errlog(1, "can not read aliases file `%s'", config
.aliases
);
579 if ((sender
= set_from(&queue
, sender
)) == NULL
)
582 if (newspoolf(&queue
) != 0)
583 errlog(1, "can not create temp file in `%s'", config
.spooldir
);
585 setlogident("%s", queue
.id
);
587 for (i
= 0; i
< argc
; i
++) {
588 if (add_recp(&queue
, argv
[i
], EXPAND_WILDCARD
) != 0)
589 errlogx(1, "invalid recipient `%s'", argv
[i
]);
592 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
593 errlogx(1, "no recipients");
595 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
596 errlog(1, "can not read mail");
598 if (LIST_EMPTY(&queue
.queue
))
599 errlogx(1, "no recipients");
601 if (linkspool(&queue
) != 0)
602 errlog(1, "can not create spools");
604 /* From here on the mail is safe. */
606 if (config
.features
& DEFER
|| queue_only
)