]>
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
34 * $DragonFly: src/libexec/dma/dma.c,v 1.5 2008/09/30 17:47:21 swildner Exp $
37 #include <sys/param.h>
38 #include <sys/queue.h>
40 #include <sys/types.h>
44 #include <openssl/ssl.h>
45 #endif /* HAVE_CRYPTO */
67 static void deliver(struct qitem
*);
68 static int add_recp(struct queue
*, const char *, const char *, int);
70 struct aliases aliases
= LIST_HEAD_INITIALIZER(aliases
);
71 static struct strlist tmpfs
= SLIST_HEAD_INITIALIZER(tmpfs
);
72 struct virtusers virtusers
= LIST_HEAD_INITIALIZER(virtusers
);
73 struct authusers authusers
= LIST_HEAD_INITIALIZER(authusers
);
74 static int daemonize
= 1;
75 struct config
*config
;
76 static struct strlist seenmsg
[16][16];
82 static char name
[MAXHOSTNAMELEN
+1];
84 if (gethostname(name
, sizeof(name
)) != 0)
85 strcpy(name
, "(unknown hostname)");
91 set_from(const char *osender
)
96 if ((config
->features
& VIRTUAL
) != 0) {
97 SLIST_FOREACH(v
, &virtusers
, next
) {
98 if (strcmp(v
->login
, getlogin()) == 0) {
99 sender
= strdup(v
->address
);
108 sender
= strdup(osender
);
112 if (asprintf(&sender
, "%s@%s", getlogin(), hostname()) <= 0)
116 if (strchr(sender
, '\n') != NULL
) {
128 yyin
= fopen(config
->aliases
, "r");
130 return (0); /* not fatal */
132 return (-1); /* fatal error, probably malloc() */
138 add_recp(struct queue
*queue
, const char *str
, const char *sender
, int expand
)
140 struct qitem
*it
, *tit
;
147 it
= calloc(1, sizeof(*it
));
150 it
->addr
= strdup(str
);
151 if (it
->addr
== NULL
)
155 host
= strrchr(it
->addr
, '@');
157 (strcmp(host
+ 1, hostname()) == 0 ||
158 strcmp(host
+ 1, "localhost") == 0)) {
161 LIST_FOREACH(tit
, &queue
->queue
, next
) {
162 /* weed out duplicate dests */
163 if (strcmp(tit
->addr
, it
->addr
) == 0) {
169 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
170 if (strrchr(it
->addr
, '@') == NULL
) {
173 LIST_FOREACH(al
, &aliases
, next
) {
174 if (strcmp(al
->alias
, it
->addr
) != 0)
176 SLIST_FOREACH(sit
, &al
->dests
, next
) {
177 if (add_recp(queue
, sit
->str
, sender
, 1) != 0)
183 LIST_REMOVE(it
, next
);
185 /* Local destination, check */
186 pw
= getpwnam(it
->addr
);
209 SLIST_FOREACH(t
, &tmpfs
, next
) {
215 gentempf(struct queue
*queue
)
221 if (snprintf(fn
, sizeof(fn
), "%s/%s", config
->spooldir
, "tmp_XXXXXXXXXX") <= 0)
227 queue
->tmpf
= strdup(fn
);
228 if (queue
->tmpf
== NULL
) {
232 t
= malloc(sizeof(*t
));
234 t
->str
= queue
->tmpf
;
235 SLIST_INSERT_HEAD(&tmpfs
, t
, next
);
241 open_locked(const char *fname
, int flags
)
246 fd
= open(fname
, flags
, 0);
249 if (flock(fd
, LOCK_EX
|((flags
& O_NONBLOCK
)? LOCK_NB
: 0)) < 0) {
257 return(open(fname
, flags
|O_EXLOCK
));
265 * queue-id1 envelope-to1
266 * queue-id2 envelope-to2
271 * queue ids are unique, formed from the inode of the spool file
272 * and a unique identifier.
275 preparespool(struct queue
*queue
, const char *sender
)
277 char line
[1000]; /* by RFC2822 */
284 error
= snprintf(line
, sizeof(line
), "%s\n", sender
);
285 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
289 if (write(queue
->mailfd
, line
, error
) != error
)
292 queuef
= fdopen(queue
->mailfd
, "r+");
297 * Assign queue id to each dest.
299 if (fstat(queue
->mailfd
, &st
) != 0)
301 queue
->id
= st
.st_ino
;
302 LIST_FOREACH(it
, &queue
->queue
, next
) {
303 if (asprintf(&it
->queueid
, "%"PRIxMAX
".%"PRIxPTR
,
304 queue
->id
, (uintptr_t)it
) <= 0)
306 if (asprintf(&it
->queuefn
, "%s/%s",
307 config
->spooldir
, it
->queueid
) <= 0)
309 /* File may not exist yet */
310 if (stat(it
->queuefn
, &st
) == 0)
313 error
= snprintf(line
, sizeof(line
), "%s %s\n",
314 it
->queueid
, it
->addr
);
315 if (error
< 0 || (size_t)error
>= sizeof(line
))
317 if (write(queue
->mailfd
, line
, error
) != error
)
321 if (write(queue
->mailfd
, line
, 1) != 1)
324 hdrlen
= lseek(queue
->mailfd
, 0, SEEK_CUR
);
325 LIST_FOREACH(it
, &queue
->queue
, next
) {
339 error
= strftime(str
, sizeof(str
), "%a, %d %b %Y %T %z",
342 strcpy(str
, "(date fail)");
347 readmail(struct queue
*queue
, const char *sender
, int nodot
)
349 char line
[1000]; /* by RFC2822 */
353 error
= snprintf(line
, sizeof(line
), "\
354 Received: from %s (uid %d)\n\
355 \t(envelope-from %s)\n\
359 getlogin(), getuid(),
364 if (error
< 0 || (size_t)error
>= sizeof(line
))
366 if (write(queue
->mailfd
, line
, error
) != error
)
369 while (!feof(stdin
)) {
370 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
372 linelen
= strlen(line
);
373 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
374 errno
= EINVAL
; /* XXX mark permanent errors */
377 if (!nodot
&& linelen
== 2 && line
[0] == '.')
379 if ((size_t)write(queue
->mailfd
, line
, linelen
) != linelen
)
382 if (fsync(queue
->mailfd
) != 0)
388 linkspool(struct queue
*queue
)
392 LIST_FOREACH(it
, &queue
->queue
, next
) {
393 if (link(queue
->tmpf
, it
->queuefn
) != 0)
400 LIST_FOREACH(it
, &queue
->queue
, next
) {
406 static struct qitem
*
407 go_background(struct queue
*queue
)
413 if (daemonize
&& daemon(0, 0) != 0) {
414 syslog(LOG_ERR
, "can not daemonize: %m");
419 bzero(&sa
, sizeof(sa
));
420 sa
.sa_flags
= SA_NOCLDWAIT
;
421 sa
.sa_handler
= SIG_IGN
;
422 sigaction(SIGCHLD
, &sa
, NULL
);
424 LIST_FOREACH(it
, &queue
->queue
, next
) {
425 /* No need to fork for the last dest */
426 if (LIST_NEXT(it
, next
) == NULL
)
432 syslog(LOG_ERR
, "can not fork: %m");
440 * return and deliver mail
454 syslog(LOG_CRIT
, "reached dead code");
459 bounce(struct qitem
*it
, const char *reason
)
461 struct queue bounceq
;
466 /* Don't bounce bounced mails */
467 if (it
->sender
[0] == 0) {
468 syslog(LOG_CRIT
, "%s: delivery panic: can't bounce a bounce",
473 syslog(LOG_ERR
, "%s: delivery failed, bouncing",
476 LIST_INIT(&bounceq
.queue
);
477 if (add_recp(&bounceq
, it
->sender
, "", 1) != 0)
479 if (gentempf(&bounceq
) != 0)
481 if (preparespool(&bounceq
, "") != 0)
484 bit
= LIST_FIRST(&bounceq
.queue
);
485 error
= fprintf(bit
->queuef
, "\
486 Received: from MAILER-DAEMON\n\
490 X-Original-To: <%s>\n\
491 From: MAILER-DAEMON <>\n\
493 Subject: Mail delivery failed\n\
494 Message-Id: <%"PRIxMAX
"@%s>\n\
497 This is the %s at %s.\n\
499 There was an error delivering your mail to <%s>.\n\
503 Message headers follow.\n\
511 bounceq
.id
, hostname(),
518 if (fflush(bit
->queuef
) != 0)
521 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0)
523 while (!feof(it
->queuef
)) {
524 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
528 write(bounceq
.mailfd
, line
, strlen(line
));
530 if (fsync(bounceq
.mailfd
) != 0)
532 if (linkspool(&bounceq
) != 0)
539 bit
= go_background(&bounceq
);
544 syslog(LOG_CRIT
, "%s: error creating bounce: %m", it
->queueid
);
550 deliver_local(struct qitem
*it
, const char **errmsg
)
558 time_t now
= time(NULL
);
560 error
= snprintf(fn
, sizeof(fn
), "%s/%s", _PATH_MAILDIR
, it
->addr
);
561 if (error
< 0 || (size_t)error
>= sizeof(fn
)) {
562 syslog(LOG_ERR
, "%s: local delivery deferred: %m",
567 /* mailx removes users mailspool file if empty, so open with O_CREAT */
568 mbox
= open_locked(fn
, O_WRONLY
| O_APPEND
| O_CREAT
);
570 syslog(LOG_ERR
, "%s: local delivery deferred: can not open `%s': %m",
574 mboxlen
= lseek(mbox
, 0, SEEK_CUR
);
576 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0) {
577 syslog(LOG_ERR
, "%s: local delivery deferred: can not seek: %m",
582 error
= snprintf(line
, sizeof(line
), "From %s\t%s", it
->sender
, ctime(&now
));
583 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
584 syslog(LOG_ERR
, "%s: local delivery deferred: can not write header: %m",
588 if (write(mbox
, line
, error
) != error
)
591 while (!feof(it
->queuef
)) {
592 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
594 linelen
= strlen(line
);
595 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
596 syslog(LOG_CRIT
, "%s: local delivery failed: corrupted queue file",
598 *errmsg
= "corrupted queue file";
603 if (strncmp(line
, "From ", 5) == 0) {
604 const char *gt
= ">";
606 if (write(mbox
, gt
, 1) != 1)
609 if ((size_t)write(mbox
, line
, linelen
) != linelen
)
613 if (write(mbox
, line
, 1) != 1)
619 syslog(LOG_ERR
, "%s: local delivery failed: write error: %m",
623 if (ftruncate(mbox
, mboxlen
) != 0)
624 syslog(LOG_WARNING
, "%s: error recovering mbox `%s': %m",
631 deliver(struct qitem
*it
)
634 unsigned int backoff
= MIN_RETRY
;
635 const char *errmsg
= "unknown bounce reason";
639 syslog(LOG_INFO
, "%s: mail from=<%s> to=<%s>",
640 it
->queueid
, it
->sender
, it
->addr
);
643 syslog(LOG_INFO
, "%s: trying delivery",
647 error
= deliver_remote(it
, &errmsg
);
649 error
= deliver_local(it
, &errmsg
);
654 syslog(LOG_INFO
, "%s: delivery successful",
659 if (stat(it
->queuefn
, &st
) != 0) {
660 syslog(LOG_ERR
, "%s: lost queue file `%s'",
661 it
->queueid
, it
->queuefn
);
664 if (gettimeofday(&now
, NULL
) == 0 &&
665 (now
.tv_sec
- st
.st_mtimespec
.tv_sec
> MAX_TIMEOUT
)) {
669 "Could not deliver for the last %d seconds. Giving up.",
676 if (backoff
> MAX_RETRY
)
696 return (c
- 'A' + 10);
698 return (c
- 'a' + 10);
706 for (i
= 0; i
< 16; i
++)
707 for (j
= 0; j
< 16; j
++)
708 SLIST_INIT(&seenmsg
[i
][j
]);
712 seen(const char *msgid
)
719 p
= strchr(msgid
, '.');
724 i
= c2x(msgid
[len
- 2]);
725 j
= c2x(msgid
[len
- 1]);
726 } else if (len
== 1) {
732 if (i
< 0 || i
>= 16 || j
< 0 || j
>= 16)
733 errx(1, "INTERNAL ERROR: bad seen code for msgid %s", msgid
);
734 SLIST_FOREACH(t
, &seenmsg
[i
][j
], next
)
735 if (!strncmp(t
->str
, msgid
, len
))
737 t
= malloc(sizeof(*t
));
739 errx(1, "Could not allocate %lu bytes",
740 (unsigned long)(sizeof(*t
)));
741 t
->str
= strdup(msgid
);
743 errx(1, "Could not duplicate msgid %s", msgid
);
744 SLIST_INSERT_HEAD(&seenmsg
[i
][j
], t
, next
);
749 load_queue(struct queue
*queue
, int ignorelock
)
753 //struct queue queue, itmqueue;
754 struct queue itmqueue
;
765 int fd
, locked
, seenit
;
767 LIST_INIT(&queue
->queue
);
769 spooldir
= opendir(config
->spooldir
);
770 if (spooldir
== NULL
)
771 err(1, "reading queue");
774 while ((de
= readdir(spooldir
)) != NULL
) {
780 LIST_INIT(&itmqueue
.queue
);
782 /* ignore temp files */
783 if (strncmp(de
->d_name
, "tmp_", 4) == 0 ||
784 de
->d_type
!= DT_REG
)
786 if (asprintf(&queuefn
, "%s/%s", config
->spooldir
, de
->d_name
) < 0)
788 seenit
= seen(de
->d_name
);
790 fd
= open_locked(queuefn
, O_RDONLY
|O_NONBLOCK
);
792 /* Ignore locked files */
793 if (errno
!= EWOULDBLOCK
)
795 if (!ignorelock
|| seenit
)
797 fd
= open(queuefn
, O_RDONLY
);
803 queuef
= fdopen(fd
, "r");
806 if (fgets(line
, sizeof(line
), queuef
) == NULL
||
809 line
[strlen(line
) - 1] = 0; /* chop newline */
810 sender
= strdup(line
);
815 if (fgets(line
, sizeof(line
), queuef
) == NULL
||
820 line
[strlen(line
) - 1] = 0;
821 queueid
= strdup(line
);
824 addr
= strchr(queueid
, ' ');
830 if (asprintf(&fn
, "%s/%s", config
->spooldir
, queueid
) < 0)
832 /* Item has already been delivered? */
833 if (stat(fn
, &st
) != 0)
835 if (add_recp(&itmqueue
, addr
, sender
, 0) != 0)
837 it
= LIST_FIRST(&itmqueue
.queue
);
839 it
->queueid
= queueid
;
844 if (LIST_EMPTY(&itmqueue
.queue
)) {
845 warnx("queue file without items: `%s'", queuefn
);
848 hdrlen
= ftell(queuef
);
849 while ((it
= LIST_FIRST(&itmqueue
.queue
)) != NULL
) {
851 LIST_REMOVE(it
, next
);
852 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
857 warn("reading queue: `%s'", queuefn
);
873 err(1, "reading queue");
877 run_queue(struct queue
*queue
)
881 if (LIST_EMPTY(&queue
->queue
))
884 it
= go_background(queue
);
890 show_queue(struct queue
*queue
)
894 if (LIST_EMPTY(&queue
->queue
)) {
895 printf("Mail queue is empty\n");
899 LIST_FOREACH(it
, &queue
->queue
, next
) {
903 To\t: %s\n--\n", it
->queueid
, it
->locked
? "*": "", it
->sender
, it
->addr
);
911 * - use group permissions
912 * - proper sysexit codes
916 main(int argc
, char **argv
)
924 int nodot
= 0, doqueue
= 0, showq
= 0;
927 LIST_INIT(&queue
.queue
);
928 snprintf(tag
, 254, "dma");
931 while ((ch
= getopt(argc
, argv
, "A:b:Df:iL:o:O:q:r:")) != -1) {
934 /* -AX is being ignored, except for -A{c,m} */
935 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
938 /* else FALLTRHOUGH */
940 /* -bX is being ignored, except for -bp */
941 if (optarg
[0] == 'p') {
945 /* else FALLTRHOUGH */
951 snprintf(tag
, 254, "%s", optarg
);
959 /* -oX is being ignored, except for -oi */
960 if (optarg
[0] != 'i')
962 /* else FALLTRHOUGH */
981 openlog(tag
, LOG_PID
| LOG_PERROR
, LOG_MAIL
);
983 config
= malloc(sizeof(struct config
));
985 errx(1, "Cannot allocate enough memory");
987 memset(config
, 0, sizeof(struct config
));
988 if (parse_conf(CONF_PATH
, config
) < 0) {
990 errx(1, "reading config file");
993 if (config
->features
& VIRTUAL
)
994 if (parse_virtuser(config
->virtualpath
) < 0)
995 errx(1, "error reading virtual user file: %s",
996 config
->virtualpath
);
998 if (parse_authfile(config
->authpath
) < 0)
999 err(1, "reading SMTP authentication file");
1003 errx(1, "sending mail and displaying queue is"
1004 " mutually exclusive");
1005 load_queue(&lqueue
, 1);
1006 show_queue(&lqueue
);
1012 errx(1, "sending mail and queue pickup is mutually exclusive");
1013 load_queue(&lqueue
, 0);
1018 if (read_aliases() != 0)
1019 err(1, "reading aliases");
1021 if ((sender
= set_from(sender
)) == NULL
)
1022 err(1, "setting from address");
1024 for (i
= 0; i
< argc
; i
++) {
1025 if (add_recp(&queue
, argv
[i
], sender
, 1) != 0)
1026 errx(1, "invalid recipient `%s'\n", argv
[i
]);
1029 if (LIST_EMPTY(&queue
.queue
))
1030 errx(1, "no recipients");
1032 if (gentempf(&queue
) != 0)
1033 err(1, "create temp file");
1035 if (preparespool(&queue
, sender
) != 0)
1036 err(1, "creating spools (1)");
1038 if (readmail(&queue
, sender
, nodot
) != 0)
1039 err(1, "reading mail");
1041 if (linkspool(&queue
) != 0)
1042 err(1, "creating spools (2)");
1044 /* From here on the mail is safe. */
1046 if (config
->features
& DEFER
)
1049 it
= go_background(&queue
);