]>
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
)
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
);
205 if (strrchr(it
->addr
, '@') == NULL
) {
208 aliased
= do_alias(queue
, it
->addr
);
209 if (!aliased
&& expand
== EXPAND_WILDCARD
)
210 aliased
= do_alias(queue
, "*");
214 LIST_REMOVE(it
, next
);
216 /* Local destination, check */
217 pw
= getpwnam(it
->addr
);
220 /* XXX read .forward */
236 static struct qitem
*
237 go_background(struct queue
*queue
)
243 if (daemonize
&& daemon(0, 0) != 0) {
244 syslog(LOG_ERR
, "can not daemonize: %m");
249 bzero(&sa
, sizeof(sa
));
250 sa
.sa_handler
= SIG_IGN
;
251 sigaction(SIGCHLD
, &sa
, NULL
);
253 LIST_FOREACH(it
, &queue
->queue
, next
) {
254 /* No need to fork for the last dest */
255 if (LIST_NEXT(it
, next
) == NULL
)
261 syslog(LOG_ERR
, "can not fork: %m");
269 * return and deliver mail
273 * If necessary, acquire the queue and * mail files.
274 * If this fails, we probably were raced by another
275 * process. It is okay to be raced if we're supposed
276 * to flush the queue.
278 setlogident("%s", it
->queueid
);
279 switch (acquirespool(it
)) {
285 syslog(LOG_WARNING
, "could not lock queue file");
290 dropspool(queue
, it
);
303 syslog(LOG_CRIT
, "reached dead code");
308 deliver(struct qitem
*it
)
311 unsigned int backoff
= MIN_RETRY
, slept
;
315 snprintf(errmsg
, sizeof(errmsg
), "unknown bounce reason");
318 syslog(LOG_INFO
, "trying delivery");
321 error
= deliver_remote(it
);
323 error
= deliver_local(it
);
328 syslog(LOG_INFO
, "delivery successful");
332 if (stat(it
->queuefn
, &st
) != 0) {
333 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
336 if (gettimeofday(&now
, NULL
) == 0 &&
337 (now
.tv_sec
- st
.st_mtim
.tv_sec
> MAX_TIMEOUT
)) {
338 snprintf(errmsg
, sizeof(errmsg
),
339 "Could not deliver for the last %d seconds. Giving up.",
343 for (slept
= 0; slept
< backoff
;) {
344 slept
+= SLEEP_TIMEOUT
- sleep(SLEEP_TIMEOUT
);
345 if (flushqueue_since(slept
)) {
350 if (slept
>= backoff
) {
351 /* pick the next backoff between [1.5, 2.5) times backoff */
352 backoff
= backoff
+ backoff
/ 2 + random() % backoff
;
353 if (backoff
> MAX_RETRY
)
369 run_queue(struct queue
*queue
)
373 if (LIST_EMPTY(&queue
->queue
))
376 it
= go_background(queue
);
382 show_queue(struct queue
*queue
)
385 int locked
= 0; /* XXX */
387 if (LIST_EMPTY(&queue
->queue
)) {
388 printf("Mail queue is empty\n");
392 LIST_FOREACH(it
, &queue
->queue
, next
) {
393 printf("ID\t: %s%s\n"
398 it
->sender
, it
->addr
);
400 if (LIST_NEXT(it
, next
) != NULL
)
409 * - use group permissions
410 * - proper sysexit codes
414 main(int argc
, char **argv
)
416 struct sigaction act
;
420 int nodot
= 0, showq
= 0, queue_only
= 0;
421 int recp_from_header
= 0;
426 * We never run as root. If called by root, drop permissions
429 if (geteuid() == 0 || getuid() == 0) {
433 pw
= getpwnam(DMA_ROOT_USER
);
436 errx(1, "user '%s' not found", DMA_ROOT_USER
);
438 err(1, "cannot drop root privileges");
441 if (setuid(pw
->pw_uid
) != 0)
442 err(1, "cannot drop root privileges");
444 if (geteuid() == 0 || getuid() == 0)
445 errx(1, "cannot drop root privileges");
451 bzero(&queue
, sizeof(queue
));
452 LIST_INIT(&queue
.queue
);
454 if (strcmp(argv
[0], "mailq") == 0) {
458 errx(1, "invalid arguments");
460 } else if (strcmp(argv
[0], "newaliases") == 0) {
461 logident_base
= "dma";
464 if (read_aliases() != 0)
465 errx(1, "could not parse aliases file `%s'", config
.aliases
);
470 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
473 /* -AX is being ignored, except for -A{c,m} */
474 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
477 /* else FALLTRHOUGH */
479 /* -bX is being ignored, except for -bp */
480 if (optarg
[0] == 'p') {
483 } else if (optarg
[0] == 'q') {
487 /* else FALLTRHOUGH */
492 logident_base
= optarg
;
500 recp_from_header
= 1;
504 /* -oX is being ignored, except for -oi */
505 if (optarg
[0] != 'i')
507 /* else FALLTRHOUGH */
515 /* Don't let getopt slup up other arguments */
516 if (optarg
&& *optarg
== '-')
521 /* Ignored options */
544 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
552 if (argc
!= 0 && (showq
|| doqueue
))
553 errx(1, "sending mail and queue operations are mutually exclusive");
555 if (showq
+ doqueue
> 1)
556 errx(1, "conflicting queue operations");
559 if (logident_base
== NULL
)
560 logident_base
= "dma";
563 act
.sa_handler
= sighup_handler
;
565 sigemptyset(&act
.sa_mask
);
566 if (sigaction(SIGHUP
, &act
, NULL
) != 0)
567 syslog(LOG_WARNING
, "can not set signal handler: %m");
569 parse_conf(CONF_PATH
"/dma.conf");
571 if (config
.authpath
!= NULL
)
572 parse_authfile(config
.authpath
);
575 if (load_queue(&queue
) < 0)
576 errlog(1, "can not load queue");
583 if (load_queue(&queue
) < 0)
584 errlog(1, "can not load queue");
589 if (read_aliases() != 0)
590 errlog(1, "could not parse aliases file `%s'", config
.aliases
);
592 if ((sender
= set_from(&queue
, sender
)) == NULL
)
595 if (newspoolf(&queue
) != 0)
596 errlog(1, "can not create temp file in `%s'", config
.spooldir
);
598 setlogident("%s", queue
.id
);
600 for (i
= 0; i
< argc
; i
++) {
601 if (add_recp(&queue
, argv
[i
], EXPAND_WILDCARD
) != 0)
602 errlogx(1, "invalid recipient `%s'", argv
[i
]);
605 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
606 errlogx(1, "no recipients");
608 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
609 errlog(1, "can not read mail");
611 if (LIST_EMPTY(&queue
.queue
))
612 errlogx(1, "no recipients");
614 if (linkspool(&queue
) != 0)
615 errlog(1, "can not create spools");
617 /* From here on the mail is safe. */
619 if (config
.features
& DEFER
|| queue_only
)