]>
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
];
68 const char *logident_base
;
69 char errmsg
[ERRMSG_SIZE
];
71 static int daemonize
= 1;
73 struct config config
= {
76 .aliases
= "/etc/aliases",
77 .spooldir
= "/var/spool/dma",
86 sighup_handler(int signo
)
88 (void)signo
; /* so that gcc doesn't complain */
92 set_from(struct queue
*queue
, const char *osender
)
97 sender
= strdup(osender
);
100 } else if (getenv("EMAIL") != NULL
) {
101 sender
= strdup(getenv("EMAIL"));
105 if (asprintf(&sender
, "%s@%s", username
, hostname()) <= 0)
109 if (strchr(sender
, '\n') != NULL
) {
114 queue
->sender
= sender
;
121 yyin
= fopen(config
.aliases
, "r");
124 * Non-existing aliases file is not a fatal error
128 /* Other problems are. */
132 return (-1); /* fatal error, probably malloc() */
138 add_recp(struct queue
*queue
, const char *str
, int expand
)
140 struct qitem
*it
, *tit
;
147 it
= calloc(1, sizeof(*it
));
150 it
->addr
= strdup(str
);
151 if (it
->addr
== NULL
)
154 it
->sender
= queue
->sender
;
155 host
= strrchr(it
->addr
, '@');
157 (strcmp(host
+ 1, hostname()) == 0 ||
158 strcmp(host
+ 1, "localhost") == 0)) {
161 LIST_FOREACH(tit
, &queue
->queue
, next
) {
162 /* weed out duplicate dests */
163 if (strcmp(tit
->addr
, it
->addr
) == 0) {
169 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
170 if (strrchr(it
->addr
, '@') == NULL
) {
173 LIST_FOREACH(al
, &aliases
, next
) {
174 if (strcmp(al
->alias
, it
->addr
) != 0)
176 SLIST_FOREACH(sit
, &al
->dests
, next
) {
177 if (add_recp(queue
, sit
->str
, 1) != 0)
183 LIST_REMOVE(it
, next
);
185 /* Local destination, check */
186 pw
= getpwnam(it
->addr
);
189 /* XXX read .forward */
205 static struct qitem
*
206 go_background(struct queue
*queue
)
212 if (daemonize
&& daemon(0, 0) != 0) {
213 syslog(LOG_ERR
, "can not daemonize: %m");
218 bzero(&sa
, sizeof(sa
));
220 sa
.sa_flags
= SA_NOCLDWAIT
;
222 sa
.sa_handler
= SIG_IGN
;
223 sigaction(SIGCHLD
, &sa
, NULL
);
225 LIST_FOREACH(it
, &queue
->queue
, next
) {
226 /* No need to fork for the last dest */
227 if (LIST_NEXT(it
, next
) == NULL
)
233 syslog(LOG_ERR
, "can not fork: %m");
241 * return and deliver mail
245 * If necessary, acquire the queue and * mail files.
246 * If this fails, we probably were raced by another
249 setlogident("%s", it
->queueid
);
250 if (acquirespool(it
) < 0)
252 dropspool(queue
, it
);
265 syslog(LOG_CRIT
, "reached dead code");
270 deliver(struct qitem
*it
)
273 unsigned int backoff
= MIN_RETRY
;
277 snprintf(errmsg
, sizeof(errmsg
), "unknown bounce reason");
280 syslog(LOG_INFO
, "trying delivery");
283 error
= deliver_remote(it
);
285 error
= deliver_local(it
);
290 syslog(LOG_INFO
, "delivery successful");
294 if (stat(it
->queuefn
, &st
) != 0) {
295 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
298 if (gettimeofday(&now
, NULL
) == 0 &&
299 (now
.tv_sec
- st
.st_mtim
.tv_sec
> MAX_TIMEOUT
)) {
300 snprintf(errmsg
, sizeof(errmsg
),
301 "Could not deliver for the last %d seconds. Giving up.",
305 if (sleep(backoff
) == 0) {
306 /* pick the next backoff between [1.5, 2.5) times backoff */
307 backoff
= backoff
+ backoff
/ 2 + random() % backoff
;
308 if (backoff
> MAX_RETRY
)
324 run_queue(struct queue
*queue
)
328 if (LIST_EMPTY(&queue
->queue
))
331 it
= go_background(queue
);
337 show_queue(struct queue
*queue
)
340 int locked
= 0; /* XXX */
342 if (LIST_EMPTY(&queue
->queue
)) {
343 printf("Mail queue is empty\n");
347 LIST_FOREACH(it
, &queue
->queue
, next
) {
348 printf("ID\t: %s%s\n"
353 it
->sender
, it
->addr
);
355 if (LIST_NEXT(it
, next
) != NULL
)
364 * - use group permissions
365 * - proper sysexit codes
369 main(int argc
, char **argv
)
371 struct sigaction act
;
375 int nodot
= 0, doqueue
= 0, showq
= 0, queue_only
= 0;
376 int recp_from_header
= 0;
381 bzero(&queue
, sizeof(queue
));
382 LIST_INIT(&queue
.queue
);
384 if (strcmp(argv
[0], "mailq") == 0) {
388 errx(1, "invalid arguments");
393 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
396 /* -AX is being ignored, except for -A{c,m} */
397 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
400 /* else FALLTRHOUGH */
402 /* -bX is being ignored, except for -bp */
403 if (optarg
[0] == 'p') {
406 } else if (optarg
[0] == 'q') {
410 /* else FALLTRHOUGH */
415 logident_base
= optarg
;
423 recp_from_header
= 1;
427 /* -oX is being ignored, except for -oi */
428 if (optarg
[0] != 'i')
430 /* else FALLTRHOUGH */
441 /* Ignored options */
464 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
472 if (argc
!= 0 && (showq
|| doqueue
))
473 errx(1, "sending mail and queue operations are mutually exclusive");
475 if (showq
+ doqueue
> 1)
476 errx(1, "conflicting queue operations");
479 if (logident_base
== NULL
)
480 logident_base
= "dma";
484 /* XXX fork root here */
486 act
.sa_handler
= sighup_handler
;
488 sigemptyset(&act
.sa_mask
);
489 if (sigaction(SIGHUP
, &act
, NULL
) != 0)
490 syslog(LOG_WARNING
, "can not set signal handler: %m");
492 parse_conf(CONF_PATH
);
494 if (config
.authpath
!= NULL
)
495 parse_authfile(config
.authpath
);
498 if (load_queue(&queue
) < 0)
499 errlog(1, "can not load queue");
505 if (load_queue(&queue
) < 0)
506 errlog(1, "can not load queue");
511 if (read_aliases() != 0)
512 errlog(1, "can not read aliases file `%s'", config
.aliases
);
514 if ((sender
= set_from(&queue
, sender
)) == NULL
)
517 if (newspoolf(&queue
) != 0)
518 errlog(1, "can not create temp file");
520 setlogident("%s", queue
.id
);
522 for (i
= 0; i
< argc
; i
++) {
523 if (add_recp(&queue
, argv
[i
], 1) != 0)
524 errlogx(1, "invalid recipient `%s'", argv
[i
]);
527 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
528 errlogx(1, "no recipients");
530 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
531 errlog(1, "can not read mail");
533 if (LIST_EMPTY(&queue
.queue
))
534 errlogx(1, "no recipients");
536 if (linkspool(&queue
) != 0)
537 errlog(1, "can not create spools");
539 /* From here on the mail is safe. */
541 if (config
.features
& DEFER
|| queue_only
)