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(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) {
110 yyin = fopen(config->aliases, "r");
112 return (0); /* not fatal */
114 return (-1); /* fatal error, probably malloc() */
120 add_recp(struct queue *queue, const char *str, const char *sender, int expand)
122 struct qitem *it, *tit;
129 it = calloc(1, sizeof(*it));
132 it->addr = strdup(str);
133 if (it->addr == NULL)
137 host = strrchr(it->addr, '@');
139 (strcmp(host + 1, hostname()) == 0 ||
140 strcmp(host + 1, "localhost") == 0)) {
143 LIST_FOREACH(tit, &queue->queue, next) {
144 /* weed out duplicate dests */
145 if (strcmp(tit->addr, it->addr) == 0) {
151 LIST_INSERT_HEAD(&queue->queue, it, next);
152 if (strrchr(it->addr, '@') == NULL) {
155 LIST_FOREACH(al, &aliases, next) {
156 if (strcmp(al->alias, it->addr) != 0)
158 SLIST_FOREACH(sit, &al->dests, next) {
159 if (add_recp(queue, sit->str, sender, 1) != 0)
165 LIST_REMOVE(it, next);
167 /* Local destination, check */
168 pw = getpwnam(it->addr);
171 /* XXX read .forward */
187 static struct qitem *
188 go_background(struct queue *queue)
194 if (daemonize && daemon(0, 0) != 0) {
195 syslog(LOG_ERR, "can not daemonize: %m");
200 bzero(&sa, sizeof(sa));
201 sa.sa_flags = SA_NOCLDWAIT;
202 sa.sa_handler = SIG_IGN;
203 sigaction(SIGCHLD, &sa, NULL);
205 LIST_FOREACH(it, &queue->queue, next) {
206 /* No need to fork for the last dest */
207 if (LIST_NEXT(it, next) == NULL)
213 syslog(LOG_ERR, "can not fork: %m");
221 * return and deliver mail
225 * If necessary, aquire the queue and * mail files.
226 * If this fails, we probably were raced by another
229 setlogident("%s", it->queueid);
230 if (aquirespool(it) < 0)
232 dropspool(queue, it);
245 syslog(LOG_CRIT, "reached dead code");
250 deliver(struct qitem *it)
253 unsigned int backoff = MIN_RETRY;
254 const char *errmsg = "unknown bounce reason";
259 syslog(LOG_INFO, "trying delivery");
262 error = deliver_remote(it, &errmsg);
264 error = deliver_local(it, &errmsg);
269 syslog(LOG_INFO, "delivery successful");
273 if (stat(it->queuefn, &st) != 0) {
274 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
277 if (gettimeofday(&now, NULL) == 0 &&
278 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
279 asprintf(__DECONST(void *, &errmsg),
280 "Could not deliver for the last %d seconds. Giving up.",
286 if (backoff > MAX_RETRY)
301 run_queue(struct queue *queue)
305 if (LIST_EMPTY(&queue->queue))
308 it = go_background(queue);
314 show_queue(struct queue *queue)
317 int locked = 0; /* XXX */
319 if (LIST_EMPTY(&queue->queue)) {
320 printf("Mail queue is empty\n");
324 LIST_FOREACH(it, &queue->queue, next) {
325 printf("ID\t: %s%s\n"
331 it->sender, it->addr);
339 * - use group permissions
340 * - proper sysexit codes
344 main(int argc, char **argv)
350 int nodot = 0, doqueue = 0, showq = 0;
353 LIST_INIT(&queue.queue);
355 if (strcmp(argv[0], "mailq") == 0) {
359 errx(1, "invalid arguments");
364 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
367 /* -AX is being ignored, except for -A{c,m} */
368 if (optarg[0] == 'c' || optarg[0] == 'm') {
371 /* else FALLTRHOUGH */
373 /* -bX is being ignored, except for -bp */
374 if (optarg[0] == 'p') {
378 /* else FALLTRHOUGH */
383 logident_base = optarg;
391 /* -oX is being ignored, except for -oi */
392 if (optarg[0] != 'i')
394 /* else FALLTRHOUGH */
405 /* Ignored options */
428 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
436 if (argc != 0 && (showq || doqueue))
437 errx(1, "sending mail and queue operations are mutually exclusive");
439 if (showq + doqueue > 1)
440 errx(1, "conflicting queue operations");
443 if (logident_base == NULL)
444 logident_base = "dma";
448 /* XXX fork root here */
450 config = calloc(1, sizeof(*config));
454 if (parse_conf(CONF_PATH) < 0) {
456 errlog(1, "can not read config file");
459 if (config->features & VIRTUAL)
460 if (parse_virtuser(config->virtualpath) < 0)
461 errlog(1, "can not read virtual user file `%s'",
462 config->virtualpath);
464 if (parse_authfile(config->authpath) < 0)
465 errlog(1, "can not read SMTP authentication file");
468 if (load_queue(&lqueue) < 0)
469 errlog(1, "can not load queue");
475 if (load_queue(&lqueue) < 0)
476 errlog(1, "can not load queue");
481 if (read_aliases() != 0)
482 errlog(1, "can not read aliases file `%s'", config->aliases);
484 if ((sender = set_from(sender)) == NULL)
487 for (i = 0; i < argc; i++) {
488 if (add_recp(&queue, argv[i], sender, 1) != 0)
489 errlogx(1, "invalid recipient `%s'", argv[i]);
492 if (LIST_EMPTY(&queue.queue))
493 errlogx(1, "no recipients");
495 if (newspoolf(&queue, sender) != 0)
496 errlog(1, "can not create temp file");
498 setlogident("%s", queue.id);
500 if (readmail(&queue, sender, nodot) != 0)
501 errlog(1, "can not read mail");
503 if (linkspool(&queue, sender) != 0)
504 errlog(1, "can not create spools");
506 /* From here on the mail is safe. */
508 if (config->features & DEFER)