]>
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 virtusers virtusers
= LIST_HEAD_INITIALIZER(virtusers
);
67 struct authusers authusers
= LIST_HEAD_INITIALIZER(authusers
);
69 const char *logident_base
;
71 static int daemonize
= 1;
73 struct config config
= {
76 .aliases
= "/var/mail/aliases",
77 .spooldir
= "/var/spool/dma",
88 set_from(struct queue
*queue
, const char *osender
)
93 if ((config
.features
& VIRTUAL
) != 0) {
94 SLIST_FOREACH(v
, &virtusers
, next
) {
95 if (strcmp(v
->login
, username
) == 0) {
96 sender
= strdup(v
->address
);
105 sender
= strdup(osender
);
108 } else if (getenv("EMAIL") != NULL
) {
109 sender
= strdup(getenv("EMAIL"));
113 if (asprintf(&sender
, "%s@%s", username
, hostname()) <= 0)
117 if (strchr(sender
, '\n') != NULL
) {
123 queue
->sender
= sender
;
130 yyin
= fopen(config
.aliases
, "r");
133 * Non-existing aliases file is not a fatal error
137 /* Other problems are. */
141 return (-1); /* fatal error, probably malloc() */
147 add_recp(struct queue
*queue
, const char *str
, int expand
)
149 struct qitem
*it
, *tit
;
156 it
= calloc(1, sizeof(*it
));
159 it
->addr
= strdup(str
);
160 if (it
->addr
== NULL
)
163 it
->sender
= queue
->sender
;
164 host
= strrchr(it
->addr
, '@');
166 (strcmp(host
+ 1, hostname()) == 0 ||
167 strcmp(host
+ 1, "localhost") == 0)) {
170 LIST_FOREACH(tit
, &queue
->queue
, next
) {
171 /* weed out duplicate dests */
172 if (strcmp(tit
->addr
, it
->addr
) == 0) {
178 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
179 if (strrchr(it
->addr
, '@') == NULL
) {
182 LIST_FOREACH(al
, &aliases
, next
) {
183 if (strcmp(al
->alias
, it
->addr
) != 0)
185 SLIST_FOREACH(sit
, &al
->dests
, next
) {
186 if (add_recp(queue
, sit
->str
, 1) != 0)
192 LIST_REMOVE(it
, next
);
194 /* Local destination, check */
195 pw
= getpwnam(it
->addr
);
198 /* XXX read .forward */
214 static struct qitem
*
215 go_background(struct queue
*queue
)
221 if (daemonize
&& daemon(0, 0) != 0) {
222 syslog(LOG_ERR
, "can not daemonize: %m");
227 bzero(&sa
, sizeof(sa
));
228 sa
.sa_flags
= SA_NOCLDWAIT
;
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
;
281 const char *errmsg
= "unknown bounce reason";
286 syslog(LOG_INFO
, "trying delivery");
289 error
= deliver_remote(it
, &errmsg
);
291 error
= deliver_local(it
, &errmsg
);
296 syslog(LOG_INFO
, "delivery successful");
300 if (stat(it
->queuefn
, &st
) != 0) {
301 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
304 if (gettimeofday(&now
, NULL
) == 0 &&
305 (now
.tv_sec
- st
.st_mtim
.tv_sec
> MAX_TIMEOUT
)) {
306 asprintf(__DECONST(void *, &errmsg
),
307 "Could not deliver for the last %d seconds. Giving up.",
313 if (backoff
> MAX_RETRY
)
328 run_queue(struct queue
*queue
)
332 if (LIST_EMPTY(&queue
->queue
))
335 it
= go_background(queue
);
341 show_queue(struct queue
*queue
)
344 int locked
= 0; /* XXX */
346 if (LIST_EMPTY(&queue
->queue
)) {
347 printf("Mail queue is empty\n");
351 LIST_FOREACH(it
, &queue
->queue
, next
) {
352 printf("ID\t: %s%s\n"
357 it
->sender
, it
->addr
);
359 if (LIST_NEXT(it
, next
) != NULL
)
368 * - use group permissions
369 * - proper sysexit codes
373 main(int argc
, char **argv
)
378 int nodot
= 0, doqueue
= 0, showq
= 0, queue_only
= 0;
379 int recp_from_header
= 0;
383 bzero(&queue
, sizeof(queue
));
384 LIST_INIT(&queue
.queue
);
386 if (strcmp(argv
[0], "mailq") == 0) {
390 errx(1, "invalid arguments");
395 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
398 /* -AX is being ignored, except for -A{c,m} */
399 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
402 /* else FALLTRHOUGH */
404 /* -bX is being ignored, except for -bp */
405 if (optarg
[0] == 'p') {
408 } else if (optarg
[0] == 'q') {
412 /* else FALLTRHOUGH */
417 logident_base
= optarg
;
425 recp_from_header
= 1;
429 /* -oX is being ignored, except for -oi */
430 if (optarg
[0] != 'i')
432 /* else FALLTRHOUGH */
443 /* Ignored options */
466 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
474 if (argc
!= 0 && (showq
|| doqueue
))
475 errx(1, "sending mail and queue operations are mutually exclusive");
477 if (showq
+ doqueue
> 1)
478 errx(1, "conflicting queue operations");
481 if (logident_base
== NULL
)
482 logident_base
= "dma";
486 /* XXX fork root here */
488 parse_conf(CONF_PATH
);
490 if (config
.features
& VIRTUAL
) {
491 if (config
.virtualpath
== NULL
)
492 errlogx(1, "no virtuser file specified, but VIRTUAL configured");
493 parse_virtuser(config
.virtualpath
);
496 if (config
.authpath
!= NULL
)
497 parse_authfile(config
.authpath
);
500 if (load_queue(&queue
) < 0)
501 errlog(1, "can not load queue");
507 if (load_queue(&queue
) < 0)
508 errlog(1, "can not load queue");
513 if (read_aliases() != 0)
514 errlog(1, "can not read aliases file `%s'", config
.aliases
);
516 if ((sender
= set_from(&queue
, sender
)) == NULL
)
519 if (newspoolf(&queue
) != 0)
520 errlog(1, "can not create temp file");
522 setlogident("%s", queue
.id
);
524 for (i
= 0; i
< argc
; i
++) {
525 if (add_recp(&queue
, argv
[i
], 1) != 0)
526 errlogx(1, "invalid recipient `%s'", argv
[i
]);
529 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
530 errlogx(1, "no recipients");
532 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
533 errlog(1, "can not read mail");
535 if (LIST_EMPTY(&queue
.queue
))
536 errlogx(1, "no recipients");
538 if (linkspool(&queue
) != 0)
539 errlog(1, "can not create spools");
541 /* From here on the mail is safe. */
543 if (config
.features
& DEFER
|| queue_only
)