]>
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
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/queue.h>
59 static void deliver(struct qitem
*);
61 struct aliases aliases
= LIST_HEAD_INITIALIZER(aliases
);
62 struct strlist tmpfs
= SLIST_HEAD_INITIALIZER(tmpfs
);
63 struct virtusers virtusers
= LIST_HEAD_INITIALIZER(virtusers
);
64 struct authusers authusers
= LIST_HEAD_INITIALIZER(authusers
);
66 const char *logident_base
;
68 static int daemonize
= 1;
70 struct config config
= {
73 .aliases
= "/var/mail/aliases",
74 .spooldir
= "/var/spool/dma",
85 set_from(struct queue
*queue
, const char *osender
)
90 if ((config
.features
& VIRTUAL
) != 0) {
91 SLIST_FOREACH(v
, &virtusers
, next
) {
92 if (strcmp(v
->login
, username
) == 0) {
93 sender
= strdup(v
->address
);
102 sender
= strdup(osender
);
105 } else if (getenv("EMAIL") != NULL
) {
106 sender
= strdup(getenv("EMAIL"));
110 if (asprintf(&sender
, "%s@%s", username
, hostname()) <= 0)
114 if (strchr(sender
, '\n') != NULL
) {
120 queue
->sender
= sender
;
127 yyin
= fopen(config
.aliases
, "r");
130 * Non-existing aliases file is not a fatal error
134 /* Other problems are. */
138 return (-1); /* fatal error, probably malloc() */
144 add_recp(struct queue
*queue
, const char *str
, int expand
)
146 struct qitem
*it
, *tit
;
153 it
= calloc(1, sizeof(*it
));
156 it
->addr
= strdup(str
);
157 if (it
->addr
== NULL
)
160 it
->sender
= queue
->sender
;
161 host
= strrchr(it
->addr
, '@');
163 (strcmp(host
+ 1, hostname()) == 0 ||
164 strcmp(host
+ 1, "localhost") == 0)) {
167 LIST_FOREACH(tit
, &queue
->queue
, next
) {
168 /* weed out duplicate dests */
169 if (strcmp(tit
->addr
, it
->addr
) == 0) {
175 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
176 if (strrchr(it
->addr
, '@') == NULL
) {
179 LIST_FOREACH(al
, &aliases
, next
) {
180 if (strcmp(al
->alias
, it
->addr
) != 0)
182 SLIST_FOREACH(sit
, &al
->dests
, next
) {
183 if (add_recp(queue
, sit
->str
, 1) != 0)
189 LIST_REMOVE(it
, next
);
191 /* Local destination, check */
192 pw
= getpwnam(it
->addr
);
195 /* XXX read .forward */
211 static struct qitem
*
212 go_background(struct queue
*queue
)
218 if (daemonize
&& daemon(0, 0) != 0) {
219 syslog(LOG_ERR
, "can not daemonize: %m");
224 bzero(&sa
, sizeof(sa
));
225 sa
.sa_flags
= SA_NOCLDWAIT
;
226 sa
.sa_handler
= SIG_IGN
;
227 sigaction(SIGCHLD
, &sa
, NULL
);
229 LIST_FOREACH(it
, &queue
->queue
, next
) {
230 /* No need to fork for the last dest */
231 if (LIST_NEXT(it
, next
) == NULL
)
237 syslog(LOG_ERR
, "can not fork: %m");
245 * return and deliver mail
249 * If necessary, acquire the queue and * mail files.
250 * If this fails, we probably were raced by another
253 setlogident("%s", it
->queueid
);
254 if (acquirespool(it
) < 0)
256 dropspool(queue
, it
);
269 syslog(LOG_CRIT
, "reached dead code");
274 deliver(struct qitem
*it
)
277 unsigned int backoff
= MIN_RETRY
;
278 const char *errmsg
= "unknown bounce reason";
283 syslog(LOG_INFO
, "trying delivery");
286 error
= deliver_remote(it
, &errmsg
);
288 error
= deliver_local(it
, &errmsg
);
293 syslog(LOG_INFO
, "delivery successful");
297 if (stat(it
->queuefn
, &st
) != 0) {
298 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
301 if (gettimeofday(&now
, NULL
) == 0 &&
302 (now
.tv_sec
- st
.st_mtim
.tv_sec
> MAX_TIMEOUT
)) {
303 asprintf(__DECONST(void *, &errmsg
),
304 "Could not deliver for the last %d seconds. Giving up.",
310 if (backoff
> MAX_RETRY
)
325 run_queue(struct queue
*queue
)
329 if (LIST_EMPTY(&queue
->queue
))
332 it
= go_background(queue
);
338 show_queue(struct queue
*queue
)
341 int locked
= 0; /* XXX */
343 if (LIST_EMPTY(&queue
->queue
)) {
344 printf("Mail queue is empty\n");
348 LIST_FOREACH(it
, &queue
->queue
, next
) {
349 printf("ID\t: %s%s\n"
354 it
->sender
, it
->addr
);
356 if (LIST_NEXT(it
, next
) != NULL
)
365 * - use group permissions
366 * - proper sysexit codes
370 main(int argc
, char **argv
)
375 int nodot
= 0, doqueue
= 0, showq
= 0, queue_only
= 0;
376 int recp_from_header
= 0;
380 bzero(&queue
, sizeof(queue
));
381 LIST_INIT(&queue
.queue
);
383 if (strcmp(argv
[0], "mailq") == 0) {
387 errx(1, "invalid arguments");
392 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
395 /* -AX is being ignored, except for -A{c,m} */
396 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
399 /* else FALLTRHOUGH */
401 /* -bX is being ignored, except for -bp */
402 if (optarg
[0] == 'p') {
405 } else if (optarg
[0] == 'q') {
409 /* else FALLTRHOUGH */
414 logident_base
= optarg
;
422 recp_from_header
= 1;
426 /* -oX is being ignored, except for -oi */
427 if (optarg
[0] != 'i')
429 /* else FALLTRHOUGH */
440 /* Ignored options */
463 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
471 if (argc
!= 0 && (showq
|| doqueue
))
472 errx(1, "sending mail and queue operations are mutually exclusive");
474 if (showq
+ doqueue
> 1)
475 errx(1, "conflicting queue operations");
478 if (logident_base
== NULL
)
479 logident_base
= "dma";
483 /* XXX fork root here */
485 parse_conf(CONF_PATH
);
487 if (config
.features
& VIRTUAL
) {
488 if (config
.virtualpath
== NULL
)
489 errlogx(1, "no virtuser file specified, but VIRTUAL configured");
490 parse_virtuser(config
.virtualpath
);
493 if (config
.authpath
!= NULL
)
494 parse_authfile(config
.authpath
);
497 if (load_queue(&queue
) < 0)
498 errlog(1, "can not load queue");
504 if (load_queue(&queue
) < 0)
505 errlog(1, "can not load queue");
510 if (read_aliases() != 0)
511 errlog(1, "can not read aliases file `%s'", config
.aliases
);
513 if ((sender
= set_from(&queue
, sender
)) == NULL
)
516 if (newspoolf(&queue
) != 0)
517 errlog(1, "can not create temp file");
519 setlogident("%s", queue
.id
);
521 for (i
= 0; i
< argc
; i
++) {
522 if (add_recp(&queue
, argv
[i
], 1) != 0)
523 errlogx(1, "invalid recipient `%s'", argv
[i
]);
526 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
527 errlogx(1, "no recipients");
529 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
530 errlog(1, "can not read mail");
532 if (LIST_EMPTY(&queue
.queue
))
533 errlogx(1, "no recipients");
535 if (linkspool(&queue
) != 0)
536 errlog(1, "can not create spools");
538 /* From here on the mail is safe. */
540 if (config
.features
& DEFER
|| queue_only
)