]>
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/queue.h>
38 #include <sys/types.h>
60 static void deliver(struct qitem
*);
62 struct aliases aliases
= LIST_HEAD_INITIALIZER(aliases
);
63 struct strlist tmpfs
= SLIST_HEAD_INITIALIZER(tmpfs
);
64 struct virtusers virtusers
= LIST_HEAD_INITIALIZER(virtusers
);
65 struct authusers authusers
= LIST_HEAD_INITIALIZER(authusers
);
66 struct config
*config
;
68 const char *logident_base
;
70 static int daemonize
= 1;
73 set_from(struct queue
*queue
, const char *osender
)
78 if ((config
->features
& VIRTUAL
) != 0) {
79 SLIST_FOREACH(v
, &virtusers
, next
) {
80 if (strcmp(v
->login
, username
) == 0) {
81 sender
= strdup(v
->address
);
90 sender
= strdup(osender
);
94 if (asprintf(&sender
, "%s@%s", username
, hostname()) <= 0)
98 if (strchr(sender
, '\n') != NULL
) {
104 queue
->sender
= sender
;
111 yyin
= fopen(config
->aliases
, "r");
113 return (0); /* not fatal */
115 return (-1); /* fatal error, probably malloc() */
121 add_recp(struct queue
*queue
, const char *str
, int expand
)
123 struct qitem
*it
, *tit
;
130 it
= calloc(1, sizeof(*it
));
133 it
->addr
= strdup(str
);
134 if (it
->addr
== NULL
)
137 it
->sender
= queue
->sender
;
138 host
= strrchr(it
->addr
, '@');
140 (strcmp(host
+ 1, hostname()) == 0 ||
141 strcmp(host
+ 1, "localhost") == 0)) {
144 LIST_FOREACH(tit
, &queue
->queue
, next
) {
145 /* weed out duplicate dests */
146 if (strcmp(tit
->addr
, it
->addr
) == 0) {
152 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
153 if (strrchr(it
->addr
, '@') == NULL
) {
156 LIST_FOREACH(al
, &aliases
, next
) {
157 if (strcmp(al
->alias
, it
->addr
) != 0)
159 SLIST_FOREACH(sit
, &al
->dests
, next
) {
160 if (add_recp(queue
, sit
->str
, 1) != 0)
166 LIST_REMOVE(it
, next
);
168 /* Local destination, check */
169 pw
= getpwnam(it
->addr
);
172 /* XXX read .forward */
188 static struct qitem
*
189 go_background(struct queue
*queue
)
195 if (daemonize
&& daemon(0, 0) != 0) {
196 syslog(LOG_ERR
, "can not daemonize: %m");
201 bzero(&sa
, sizeof(sa
));
202 sa
.sa_flags
= SA_NOCLDWAIT
;
203 sa
.sa_handler
= SIG_IGN
;
204 sigaction(SIGCHLD
, &sa
, NULL
);
206 LIST_FOREACH(it
, &queue
->queue
, next
) {
207 /* No need to fork for the last dest */
208 if (LIST_NEXT(it
, next
) == NULL
)
214 syslog(LOG_ERR
, "can not fork: %m");
222 * return and deliver mail
226 * If necessary, aquire the queue and * mail files.
227 * If this fails, we probably were raced by another
230 setlogident("%s", it
->queueid
);
231 if (aquirespool(it
) < 0)
233 dropspool(queue
, it
);
246 syslog(LOG_CRIT
, "reached dead code");
251 deliver(struct qitem
*it
)
254 unsigned int backoff
= MIN_RETRY
;
255 const char *errmsg
= "unknown bounce reason";
260 syslog(LOG_INFO
, "trying delivery");
263 error
= deliver_remote(it
, &errmsg
);
265 error
= deliver_local(it
, &errmsg
);
270 syslog(LOG_INFO
, "delivery successful");
274 if (stat(it
->queuefn
, &st
) != 0) {
275 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
278 if (gettimeofday(&now
, NULL
) == 0 &&
279 (now
.tv_sec
- st
.st_mtimespec
.tv_sec
> MAX_TIMEOUT
)) {
280 asprintf(__DECONST(void *, &errmsg
),
281 "Could not deliver for the last %d seconds. Giving up.",
287 if (backoff
> MAX_RETRY
)
302 run_queue(struct queue
*queue
)
306 if (LIST_EMPTY(&queue
->queue
))
309 it
= go_background(queue
);
315 show_queue(struct queue
*queue
)
318 int locked
= 0; /* XXX */
320 if (LIST_EMPTY(&queue
->queue
)) {
321 printf("Mail queue is empty\n");
325 LIST_FOREACH(it
, &queue
->queue
, next
) {
326 printf("ID\t: %s%s\n"
331 it
->sender
, it
->addr
);
333 if (LIST_NEXT(it
, next
) != NULL
)
342 * - use group permissions
343 * - proper sysexit codes
347 main(int argc
, char **argv
)
352 int nodot
= 0, doqueue
= 0, showq
= 0, queue_only
= 0;
353 int recp_from_header
= 0;
357 bzero(&queue
, sizeof(queue
));
358 LIST_INIT(&queue
.queue
);
360 if (strcmp(argv
[0], "mailq") == 0) {
364 errx(1, "invalid arguments");
369 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
372 /* -AX is being ignored, except for -A{c,m} */
373 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
376 /* else FALLTRHOUGH */
378 /* -bX is being ignored, except for -bp */
379 if (optarg
[0] == 'p') {
382 } else if (optarg
[0] == 'q') {
386 /* else FALLTRHOUGH */
391 logident_base
= optarg
;
399 recp_from_header
= 1;
403 /* -oX is being ignored, except for -oi */
404 if (optarg
[0] != 'i')
406 /* else FALLTRHOUGH */
417 /* Ignored options */
440 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
448 if (argc
!= 0 && (showq
|| doqueue
))
449 errx(1, "sending mail and queue operations are mutually exclusive");
451 if (showq
+ doqueue
> 1)
452 errx(1, "conflicting queue operations");
455 if (logident_base
== NULL
)
456 logident_base
= "dma";
460 /* XXX fork root here */
462 config
= calloc(1, sizeof(*config
));
466 if (parse_conf(CONF_PATH
) < 0) {
468 errlog(1, "can not read config file");
471 if (config
->features
& VIRTUAL
)
472 if (parse_virtuser(config
->virtualpath
) < 0)
473 errlog(1, "can not read virtual user file `%s'",
474 config
->virtualpath
);
476 if (parse_authfile(config
->authpath
) < 0)
477 errlog(1, "can not read SMTP authentication file");
480 if (load_queue(&queue
) < 0)
481 errlog(1, "can not load queue");
487 if (load_queue(&queue
) < 0)
488 errlog(1, "can not load queue");
493 if (read_aliases() != 0)
494 errlog(1, "can not read aliases file `%s'", config
->aliases
);
496 if ((sender
= set_from(&queue
, sender
)) == NULL
)
499 if (newspoolf(&queue
) != 0)
500 errlog(1, "can not create temp file");
502 setlogident("%s", queue
.id
);
504 for (i
= 0; i
< argc
; i
++) {
505 if (add_recp(&queue
, argv
[i
], 1) != 0)
506 errlogx(1, "invalid recipient `%s'", argv
[i
]);
509 if (LIST_EMPTY(&queue
.queue
) && !recp_from_header
)
510 errlogx(1, "no recipients");
512 if (readmail(&queue
, nodot
, recp_from_header
) != 0)
513 errlog(1, "can not read mail");
515 if (LIST_EMPTY(&queue
.queue
))
516 errlogx(1, "no recipients");
518 if (linkspool(&queue
) != 0)
519 errlog(1, "can not create spools");
521 /* From here on the mail is safe. */
523 if (config
->features
& DEFER
|| queue_only
)