]>
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;
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 add_recp(struct queue
*queue
, const char *str
, int expand
)
150 struct qitem
*it
, *tit
;
157 it
= calloc(1, sizeof(*it
));
160 it
->addr
= strdup(str
);
161 if (it
->addr
== NULL
)
164 it
->sender
= queue
->sender
;
165 host
= strrchr(it
->addr
, '@');
167 (strcmp(host
+ 1, hostname()) == 0 ||
168 strcmp(host
+ 1, "localhost") == 0)) {
171 LIST_FOREACH(tit
, &queue
->queue
, next
) {
172 /* weed out duplicate dests */
173 if (strcmp(tit
->addr
, it
->addr
) == 0) {
179 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
180 if (strrchr(it
->addr
, '@') == NULL
) {
183 LIST_FOREACH(al
, &aliases
, next
) {
184 if (strcmp(al
->alias
, it
->addr
) != 0)
186 SLIST_FOREACH(sit
, &al
->dests
, next
) {
187 if (add_recp(queue
, sit
->str
, 1) != 0)
193 LIST_REMOVE(it
, next
);
195 /* Local destination, check */
196 pw
= getpwnam(it
->addr
);
199 /* XXX read .forward */
215 static struct qitem
*
216 go_background(struct queue
*queue
)
222 if (daemonize
&& daemon(0, 0) != 0) {
223 syslog(LOG_ERR
, "can not daemonize: %m");
228 bzero(&sa
, sizeof(sa
));
229 sa
.sa_handler
= SIG_IGN
;
230 sigaction(SIGCHLD
, &sa
, NULL
);
232 LIST_FOREACH(it
, &queue
->queue
, next
) {
233 /* No need to fork for the last dest */
234 if (LIST_NEXT(it
, next
) == NULL
)
240 syslog(LOG_ERR
, "can not fork: %m");
248 * return and deliver mail
252 * If necessary, acquire the queue and * mail files.
253 * If this fails, we probably were raced by another
256 setlogident("%s", it
->queueid
);
257 if (acquirespool(it
) < 0)
259 dropspool(queue
, it
);
272 syslog(LOG_CRIT
, "reached dead code");
277 deliver(struct qitem
*it
)
280 unsigned int backoff
= MIN_RETRY
;
284 snprintf(errmsg
, sizeof(errmsg
), "unknown bounce reason");
287 syslog(LOG_INFO
, "trying delivery");
290 error
= deliver_remote(it
);
292 error
= deliver_local(it
);
297 syslog(LOG_INFO
, "delivery successful");
301 if (stat(it
->queuefn
, &st
) != 0) {
302 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
305 if (gettimeofday(&now
, NULL
) == 0 &&
306 (now
.tv_sec
- st
.st_mtim
.tv_sec
> MAX_TIMEOUT
)) {
307 snprintf(errmsg
, sizeof(errmsg
),
308 "Could not deliver for the last %d seconds. Giving up.",
312 if (sleep(backoff
) == 0) {
313 /* pick the next backoff between [1.5, 2.5) times backoff */
314 backoff
= backoff
+ backoff
/ 2 + random() % backoff
;
315 if (backoff
> MAX_RETRY
)
331 run_queue(struct queue
*queue
)
335 if (LIST_EMPTY(&queue
->queue
))
338 it
= go_background(queue
);
344 show_queue(struct queue
*queue
)
347 int locked
= 0; /* XXX */
349 if (LIST_EMPTY(&queue
->queue
)) {
350 printf("Mail queue is empty\n");
354 LIST_FOREACH(it
, &queue
->queue
, next
) {
355 printf("ID\t: %s%s\n"
360 it
->sender
, it
->addr
);
362 if (LIST_NEXT(it
, next
) != NULL
)
371 * - use group permissions
372 * - proper sysexit codes
376 main(int argc
, char **argv
)
378 struct sigaction act
;
382 int nodot
= 0, doqueue
= 0, showq
= 0, queue_only
= 0;
383 int recp_from_header
= 0;
388 * We never run as root. If called by root, drop permissions
391 if (geteuid() == 0 || getuid() == 0) {
394 pw
= getpwnam(DMA_ROOT_USER
);
396 err(1, "cannot drop root privileges");
398 if (setuid(pw
->pw_uid
) != 0)
399 err(1, "cannot drop root privileges");
401 if (geteuid() == 0 || getuid() == 0)
402 errx(1, "cannot drop root privileges");
408 bzero(&queue
, sizeof(queue
));
409 LIST_INIT(&queue
.queue
);
411 if (strcmp(argv
[0], "mailq") == 0) {
415 errx(1, "invalid arguments");
420 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
423 /* -AX is being ignored, except for -A{c,m} */
424 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
427 /* else FALLTRHOUGH */
429 /* -bX is being ignored, except for -bp */
430 if (optarg
[0] == 'p') {
433 } else if (optarg
[0] == 'q') {
437 /* else FALLTRHOUGH */
442 logident_base
= optarg
;
450 recp_from_header
= 1;
454 /* -oX is being ignored, except for -oi */
455 if (optarg
[0] != 'i')
457 /* else FALLTRHOUGH */
468 /* Ignored options */
491 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
499 if (argc
!= 0 && (showq
|| doqueue
))
500 errx(1, "sending mail and queue operations are mutually exclusive");
502 if (showq
+ doqueue
> 1)
503 errx(1, "conflicting queue operations");
506 if (logident_base
== NULL
)
507 logident_base
= "dma";
510 act
.sa_handler
= sighup_handler
;
512 sigemptyset(&act
.sa_mask
);
513 if (sigaction(SIGHUP
, &act
, NULL
) != 0)
514 syslog(LOG_WARNING
, "can not set signal handler: %m");
516 parse_conf(CONF_PATH
"/dma.conf");
518 if (config
.authpath
!= NULL
)
519 parse_authfile(config
.authpath
);
522 if (load_queue(&queue
) < 0)
523 errlog(1, "can not load queue");
529 if (load_queue(&queue
) < 0)
530 errlog(1, "can not load queue");
535 if (read_aliases() != 0)
536 errlog(1, "can not read aliases file `%s'", config
.aliases
);
538 if ((sender
= set_from(&queue
, sender
)) == NULL
)
541 if (newspoolf(&queue
) != 0)
542 errlog(1, "can not create temp file");
544 setlogident("%s", queue
.id
);
546 for (i
= 0; i
< argc
; i
++) {
547 if (add_recp(&queue
, argv
[i
], 1) != 0)
548 errlogx(1, "invalid recipient `%s'", argv
[i
]);
551 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
552 errlogx(1, "no recipients");
554 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
555 errlog(1, "can not read mail");
557 if (LIST_EMPTY(&queue
.queue
))
558 errlogx(1, "no recipients");
560 if (linkspool(&queue
) != 0)
561 errlog(1, "can not create spools");
563 /* From here on the mail is safe. */
565 if (config
.features
& DEFER
|| queue_only
)