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 */
188 readmail(struct queue
*queue
, const char *sender
, int nodot
)
190 char line
[1000]; /* by RFC2822 */
195 int had_messagid
= 0;
198 error
= fprintf(queue
->mailf
,
199 "Received: from %s (uid %d)\n"
200 "\t(envelope-from %s)\n"
209 if ((ssize_t
)error
< 0)
212 while (!feof(stdin
)) {
213 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
215 linelen
= strlen(line
);
216 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
217 errno
= EINVAL
; /* XXX mark permanent errors */
221 if (strprefixcmp(line
, "Date:") == 0)
223 else if (strprefixcmp(line
, "Message-Id:") == 0)
225 else if (strprefixcmp(line
, "From:") == 0)
228 if (strcmp(line
, "\n") == 0 && !had_headers
) {
230 while (!had_date
|| !had_messagid
|| !had_from
) {
233 snprintf(line
, sizeof(line
), "Date: %s\n", rfc822date());
234 } else if (!had_messagid
) {
235 /* XXX better msgid, assign earlier and log? */
237 snprintf(line
, sizeof(line
), "Message-Id: <%s@%s>\n",
238 queue
->id
, hostname());
239 } else if (!had_from
) {
241 snprintf(line
, sizeof(line
), "From: <%s>\n", sender
);
243 if (fwrite(line
, strlen(line
), 1, queue
->mailf
) != 1)
248 if (!nodot
&& linelen
== 2 && line
[0] == '.')
250 if (fwrite(line
, strlen(line
), 1, queue
->mailf
) != 1)
257 static struct qitem
*
258 go_background(struct queue
*queue
)
264 if (daemonize
&& daemon(0, 0) != 0) {
265 syslog(LOG_ERR
, "can not daemonize: %m");
270 bzero(&sa
, sizeof(sa
));
271 sa
.sa_flags
= SA_NOCLDWAIT
;
272 sa
.sa_handler
= SIG_IGN
;
273 sigaction(SIGCHLD
, &sa
, NULL
);
275 LIST_FOREACH(it
, &queue
->queue
, next
) {
276 /* No need to fork for the last dest */
277 if (LIST_NEXT(it
, next
) == NULL
)
283 syslog(LOG_ERR
, "can not fork: %m");
291 * return and deliver mail
295 * If necessary, aquire the queue and * mail files.
296 * If this fails, we probably were raced by another
299 setlogident("%s", it
->queueid
);
300 if (aquirespool(it
) < 0)
302 dropspool(queue
, it
);
315 syslog(LOG_CRIT
, "reached dead code");
320 bounce(struct qitem
*it
, const char *reason
)
322 struct queue bounceq
;
328 /* Don't bounce bounced mails */
329 if (it
->sender
[0] == 0) {
330 syslog(LOG_INFO
, "can not bounce a bounce message, discarding");
334 LIST_INIT(&bounceq
.queue
);
335 if (add_recp(&bounceq
, it
->sender
, "", 1) != 0)
338 if (newspoolf(&bounceq
, "") != 0)
341 syslog(LOG_ERR
, "delivery failed, bouncing as %s", bounceq
.id
);
342 setlogident("%s", bounceq
.id
);
344 error
= fprintf(bounceq
.mailf
,
345 "Received: from MAILER-DAEMON\n"
349 "X-Original-To: <%s>\n"
350 "From: MAILER-DAEMON <>\n"
352 "Subject: Mail delivery failed\n"
353 "Message-Id: <%s@%s>\n"
356 "This is the %s at %s.\n"
358 "There was an error delivering your mail to <%s>.\n"
369 bounceq
.id
, hostname(),
374 config
->features
& FULLBOUNCE
?
375 "Original message follows." :
376 "Message headers follow.");
380 if (fseek(it
->mailf
, it
->hdrlen
, SEEK_SET
) != 0)
382 if (config
->features
& FULLBOUNCE
) {
383 while ((pos
= fread(line
, 1, sizeof(line
), it
->mailf
)) > 0) {
384 if (fwrite(line
, 1, pos
, bounceq
.mailf
) != pos
)
388 while (!feof(it
->mailf
)) {
389 if (fgets(line
, sizeof(line
), it
->mailf
) == NULL
)
393 if (fwrite(line
, strlen(line
), 1, bounceq
.mailf
) != 1)
398 if (linkspool(&bounceq
, "") != 0)
404 bit
= go_background(&bounceq
);
409 syslog(LOG_CRIT
, "error creating bounce: %m");
415 deliver(struct qitem
*it
)
418 unsigned int backoff
= MIN_RETRY
;
419 const char *errmsg
= "unknown bounce reason";
424 syslog(LOG_INFO
, "trying delivery");
427 error
= deliver_remote(it
, &errmsg
);
429 error
= deliver_local(it
, &errmsg
);
434 syslog(LOG_INFO
, "delivery successful");
438 if (stat(it
->queuefn
, &st
) != 0) {
439 syslog(LOG_ERR
, "lost queue file `%s'", it
->queuefn
);
442 if (gettimeofday(&now
, NULL
) == 0 &&
443 (now
.tv_sec
- st
.st_mtimespec
.tv_sec
> MAX_TIMEOUT
)) {
444 asprintf(__DECONST(void *, &errmsg
),
445 "Could not deliver for the last %d seconds. Giving up.",
451 if (backoff
> MAX_RETRY
)
466 run_queue(struct queue
*queue
)
470 if (LIST_EMPTY(&queue
->queue
))
473 it
= go_background(queue
);
479 show_queue(struct queue
*queue
)
482 int locked
= 0; /* XXX */
484 if (LIST_EMPTY(&queue
->queue
)) {
485 printf("Mail queue is empty\n");
489 LIST_FOREACH(it
, &queue
->queue
, next
) {
490 printf("ID\t: %s%s\n"
496 it
->sender
, it
->addr
);
504 * - use group permissions
505 * - proper sysexit codes
509 main(int argc
, char **argv
)
516 int nodot
= 0, doqueue
= 0, showq
= 0;
519 LIST_INIT(&queue
.queue
);
521 if (strcmp(argv
[0], "mailq") == 0) {
525 errx(1, "invalid arguments");
530 while ((ch
= getopt(argc
, argv
, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
533 /* -AX is being ignored, except for -A{c,m} */
534 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
537 /* else FALLTRHOUGH */
539 /* -bX is being ignored, except for -bp */
540 if (optarg
[0] == 'p') {
544 /* else FALLTRHOUGH */
549 logident_base
= optarg
;
557 /* -oX is being ignored, except for -oi */
558 if (optarg
[0] != 'i')
560 /* else FALLTRHOUGH */
571 /* Ignored options */
594 fprintf(stderr
, "invalid argument: `-%c'\n", optopt
);
602 if (argc
!= 0 && (showq
|| doqueue
))
603 errx(1, "sending mail and queue operations are mutually exclusive");
605 if (showq
+ doqueue
> 1)
606 errx(1, "conflicting queue operations");
609 if (logident_base
== NULL
)
610 logident_base
= "dma";
614 /* XXX fork root here */
616 config
= calloc(1, sizeof(*config
));
620 if (parse_conf(CONF_PATH
) < 0) {
622 errlog(1, "can not read config file");
625 if (config
->features
& VIRTUAL
)
626 if (parse_virtuser(config
->virtualpath
) < 0)
627 errlog(1, "can not read virtual user file `%s'",
628 config
->virtualpath
);
630 if (parse_authfile(config
->authpath
) < 0)
631 errlog(1, "can not read SMTP authentication file");
634 if (load_queue(&lqueue
) < 0)
635 errlog(1, "can not load queue");
641 if (load_queue(&lqueue
) < 0)
642 errlog(1, "can not load queue");
647 if (read_aliases() != 0)
648 errlog(1, "can not read aliases file `%s'", config
->aliases
);
650 if ((sender
= set_from(sender
)) == NULL
)
653 for (i
= 0; i
< argc
; i
++) {
654 if (add_recp(&queue
, argv
[i
], sender
, 1) != 0)
655 errlogx(1, "invalid recipient `%s'", argv
[i
]);
658 if (LIST_EMPTY(&queue
.queue
))
659 errlogx(1, "no recipients");
661 if (newspoolf(&queue
, sender
) != 0)
662 errlog(1, "can not create temp file");
664 setlogident("%s", queue
.id
);
666 if (readmail(&queue
, sender
, nodot
) != 0)
667 errlog(1, "can not read mail");
669 if (linkspool(&queue
, sender
) != 0)
670 errlog(1, "can not create spools");
672 /* From here on the mail is safe. */
674 if (config
->features
& DEFER
)
677 it
= go_background(&queue
);