]>
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>
63 static void deliver(struct qitem
*);
64 static int add_recp(struct queue
*, const char *, const char *, int);
66 struct aliases aliases
= LIST_HEAD_INITIALIZER(aliases
);
67 static struct strlist tmpfs
= SLIST_HEAD_INITIALIZER(tmpfs
);
68 struct virtusers virtusers
= LIST_HEAD_INITIALIZER(virtusers
);
69 struct authusers authusers
= LIST_HEAD_INITIALIZER(authusers
);
70 static int daemonize
= 1;
71 struct config
*config
;
72 static const char *username
;
74 static struct strlist seenmsg
[16][16];
80 static char name
[MAXHOSTNAMELEN
+1];
88 if (config
->mailname
!= NULL
&& config
->mailname
[0] != '\0') {
89 snprintf(name
, sizeof(name
), "%s", config
->mailname
);
93 if (config
->mailnamefile
!= NULL
&& config
->mailnamefile
[0] != '\0') {
94 fp
= fopen(config
->mailnamefile
, "r");
96 if (fgets(name
, sizeof(name
), fp
) != NULL
) {
99 (name
[len
- 1] == '\r' ||
100 name
[len
- 1] == '\n'))
102 if (name
[0] != '\0') {
110 if (gethostname(name
, sizeof(name
)) != 0)
111 strcpy(name
, "(unknown hostname)");
117 check_username(const char *name
, uid_t ckuid
)
123 pwd
= getpwnam(name
);
124 if (pwd
== NULL
|| pwd
->pw_uid
!= ckuid
)
136 username
= check_username(getlogin(), uid
);
137 if (username
!= NULL
)
139 username
= check_username(getenv("LOGNAME"), uid
);
140 if (username
!= NULL
)
142 username
= check_username(getenv("USER"), uid
);
143 if (username
!= NULL
)
146 if (pwd
!= NULL
&& pwd
->pw_name
!= NULL
&& pwd
->pw_name
[0] != '\0' &&
147 (u
= strdup(pwd
->pw_name
)) != NULL
) {
148 username
= check_username(u
, uid
);
149 if (username
!= NULL
)
154 asprintf(__DECONST(void *, &username
), "%ld", (long)uid
);
155 if (username
!= NULL
)
157 username
= "unknown-or-invalid-username";
161 set_from(const char *osender
)
166 if ((config
->features
& VIRTUAL
) != 0) {
167 SLIST_FOREACH(v
, &virtusers
, next
) {
168 if (strcmp(v
->login
, username
) == 0) {
169 sender
= strdup(v
->address
);
178 sender
= strdup(osender
);
182 if (asprintf(&sender
, "%s@%s", username
, hostname()) <= 0)
186 if (strchr(sender
, '\n') != NULL
) {
198 yyin
= fopen(config
->aliases
, "r");
200 return (0); /* not fatal */
202 return (-1); /* fatal error, probably malloc() */
208 add_recp(struct queue
*queue
, const char *str
, const char *sender
, int expand
)
210 struct qitem
*it
, *tit
;
217 it
= calloc(1, sizeof(*it
));
220 it
->addr
= strdup(str
);
221 if (it
->addr
== NULL
)
225 host
= strrchr(it
->addr
, '@');
227 (strcmp(host
+ 1, hostname()) == 0 ||
228 strcmp(host
+ 1, "localhost") == 0)) {
231 LIST_FOREACH(tit
, &queue
->queue
, next
) {
232 /* weed out duplicate dests */
233 if (strcmp(tit
->addr
, it
->addr
) == 0) {
239 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
240 if (strrchr(it
->addr
, '@') == NULL
) {
243 LIST_FOREACH(al
, &aliases
, next
) {
244 if (strcmp(al
->alias
, it
->addr
) != 0)
246 SLIST_FOREACH(sit
, &al
->dests
, next
) {
247 if (add_recp(queue
, sit
->str
, sender
, 1) != 0)
253 LIST_REMOVE(it
, next
);
255 /* Local destination, check */
256 pw
= getpwnam(it
->addr
);
279 SLIST_FOREACH(t
, &tmpfs
, next
) {
285 gentempf(struct queue
*queue
)
291 if (snprintf(fn
, sizeof(fn
), "%s/%s", config
->spooldir
, "tmp_XXXXXXXXXX") <= 0)
296 if (flock(fd
, LOCK_EX
) == -1)
299 queue
->tmpf
= strdup(fn
);
300 if (queue
->tmpf
== NULL
) {
304 t
= malloc(sizeof(*t
));
306 t
->str
= queue
->tmpf
;
307 SLIST_INSERT_HEAD(&tmpfs
, t
, next
);
313 open_locked(const char *fname
, int flags
)
318 fd
= open(fname
, flags
, 0);
321 if (flock(fd
, LOCK_EX
|((flags
& O_NONBLOCK
)? LOCK_NB
: 0)) < 0) {
329 return(open(fname
, flags
|O_EXLOCK
));
337 * queue-id1 envelope-to1
338 * queue-id2 envelope-to2
343 * queue ids are unique, formed from the inode of the spool file
344 * and a unique identifier.
347 preparespool(struct queue
*queue
, const char *sender
)
349 char line
[1000]; /* by RFC2822 */
356 error
= snprintf(line
, sizeof(line
), "%s\n", sender
);
357 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
361 if (write(queue
->mailfd
, line
, error
) != error
)
364 queuef
= fdopen(queue
->mailfd
, "r+");
369 * Assign queue id to each dest.
371 if (fstat(queue
->mailfd
, &st
) != 0)
373 queue
->id
= st
.st_ino
;
374 LIST_FOREACH(it
, &queue
->queue
, next
) {
375 if (asprintf(&it
->queueid
, "%"PRIxMAX
".%"PRIxPTR
,
376 queue
->id
, (uintptr_t)it
) <= 0)
378 if (asprintf(&it
->queuefn
, "%s/%s",
379 config
->spooldir
, it
->queueid
) <= 0)
381 /* File may not exist yet */
382 if (stat(it
->queuefn
, &st
) == 0)
385 error
= snprintf(line
, sizeof(line
), "%s %s\n",
386 it
->queueid
, it
->addr
);
387 if (error
< 0 || (size_t)error
>= sizeof(line
))
389 if (write(queue
->mailfd
, line
, error
) != error
)
393 if (write(queue
->mailfd
, line
, 1) != 1)
396 hdrlen
= lseek(queue
->mailfd
, 0, SEEK_CUR
);
397 LIST_FOREACH(it
, &queue
->queue
, next
) {
411 error
= strftime(str
, sizeof(str
), "%a, %d %b %Y %T %z",
414 strcpy(str
, "(date fail)");
419 readmail(struct queue
*queue
, const char *sender
, int nodot
)
421 char line
[1000]; /* by RFC2822 */
425 error
= snprintf(line
, sizeof(line
), "\
426 Received: from %s (uid %d)\n\
427 \t(envelope-from %s)\n\
436 if (error
< 0 || (size_t)error
>= sizeof(line
))
438 if (write(queue
->mailfd
, line
, error
) != error
)
441 while (!feof(stdin
)) {
442 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
444 linelen
= strlen(line
);
445 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
446 errno
= EINVAL
; /* XXX mark permanent errors */
449 if (!nodot
&& linelen
== 2 && line
[0] == '.')
451 if ((size_t)write(queue
->mailfd
, line
, linelen
) != linelen
)
454 if (fsync(queue
->mailfd
) != 0)
460 linkspool(struct queue
*queue
)
464 LIST_FOREACH(it
, &queue
->queue
, next
) {
465 if (link(queue
->tmpf
, it
->queuefn
) != 0)
472 LIST_FOREACH(it
, &queue
->queue
, next
) {
478 static struct qitem
*
479 go_background(struct queue
*queue
)
486 if (daemonize
&& daemon(0, 0) != 0) {
487 syslog(LOG_ERR
, "can not daemonize: %m");
492 bzero(&sa
, sizeof(sa
));
493 sa
.sa_flags
= SA_NOCLDWAIT
;
494 sa
.sa_handler
= SIG_IGN
;
495 sigaction(SIGCHLD
, &sa
, NULL
);
497 LIST_FOREACH(it
, &queue
->queue
, next
) {
498 /* No need to fork for the last dest */
499 if (LIST_NEXT(it
, next
) == NULL
)
505 syslog(LOG_ERR
, "can not fork: %m");
513 * return and deliver mail
516 * We have to prevent sharing of fds between children, so
517 * we have to re-open the queue file.
519 newqf
= fopen(it
->queuefn
, "r");
521 syslog(LOG_ERR
, "can not re-open queue file `%s': %m",
539 syslog(LOG_CRIT
, "reached dead code");
544 bounce(struct qitem
*it
, const char *reason
)
546 struct queue bounceq
;
552 /* Don't bounce bounced mails */
553 if (it
->sender
[0] == 0) {
554 syslog(LOG_INFO
, "%s: can not bounce a bounce message, discarding",
559 syslog(LOG_ERR
, "%s: delivery failed, bouncing",
562 LIST_INIT(&bounceq
.queue
);
563 if (add_recp(&bounceq
, it
->sender
, "", 1) != 0)
565 if (gentempf(&bounceq
) != 0)
567 if (preparespool(&bounceq
, "") != 0)
570 bit
= LIST_FIRST(&bounceq
.queue
);
571 error
= fprintf(bit
->queuef
, "\
572 Received: from MAILER-DAEMON\n\
576 X-Original-To: <%s>\n\
577 From: MAILER-DAEMON <>\n\
579 Subject: Mail delivery failed\n\
580 Message-Id: <%"PRIxMAX
"@%s>\n\
583 This is the %s at %s.\n\
585 There was an error delivering your mail to <%s>.\n\
597 bounceq
.id
, hostname(),
602 config
->features
& FULLBOUNCE
? "Original message follows.":
603 "Message headers follow.");
606 if (fflush(bit
->queuef
) != 0)
609 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0)
611 if (config
->features
& FULLBOUNCE
) {
612 while ((pos
= fread(line
, 1, sizeof(line
), it
->queuef
)) > 0) {
613 if ((size_t)write(bounceq
.mailfd
, line
, pos
) != pos
)
617 while (!feof(it
->queuef
)) {
618 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
622 if ((size_t)write(bounceq
.mailfd
, line
, strlen(line
)) != strlen(line
))
626 if (fsync(bounceq
.mailfd
) != 0)
628 if (linkspool(&bounceq
) != 0)
635 bit
= go_background(&bounceq
);
640 syslog(LOG_CRIT
, "%s: error creating bounce: %m", it
->queueid
);
646 deliver_local(struct qitem
*it
, const char **errmsg
)
654 time_t now
= time(NULL
);
656 error
= snprintf(fn
, sizeof(fn
), "%s/%s", _PATH_MAILDIR
, it
->addr
);
657 if (error
< 0 || (size_t)error
>= sizeof(fn
)) {
658 syslog(LOG_NOTICE
, "%s: local delivery deferred: %m",
663 /* mailx removes users mailspool file if empty, so open with O_CREAT */
664 mbox
= open_locked(fn
, O_WRONLY
| O_APPEND
| O_CREAT
);
666 syslog(LOG_NOTICE
, "%s: local delivery deferred: can not open `%s': %m",
670 mboxlen
= lseek(mbox
, 0, SEEK_CUR
);
672 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0) {
673 syslog(LOG_NOTICE
, "%s: local delivery deferred: can not seek: %m",
678 error
= snprintf(line
, sizeof(line
), "From %s\t%s", it
->sender
, ctime(&now
));
679 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
680 syslog(LOG_NOTICE
, "%s: local delivery deferred: can not write header: %m",
684 if (write(mbox
, line
, error
) != error
)
687 while (!feof(it
->queuef
)) {
688 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
690 linelen
= strlen(line
);
691 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
692 syslog(LOG_CRIT
, "%s: local delivery failed: corrupted queue file",
694 *errmsg
= "corrupted queue file";
699 if (strncmp(line
, "From ", 5) == 0) {
700 const char *gt
= ">";
702 if (write(mbox
, gt
, 1) != 1)
705 if ((size_t)write(mbox
, line
, linelen
) != linelen
)
709 if (write(mbox
, line
, 1) != 1)
715 syslog(LOG_ERR
, "%s: local delivery failed: write error: %m",
719 if (ftruncate(mbox
, mboxlen
) != 0)
720 syslog(LOG_WARNING
, "%s: error recovering mbox `%s': %m",
727 deliver(struct qitem
*it
)
730 unsigned int backoff
= MIN_RETRY
;
731 const char *errmsg
= "unknown bounce reason";
735 syslog(LOG_INFO
, "%s: mail from=<%s> to=<%s>",
736 it
->queueid
, it
->sender
, it
->addr
);
739 syslog(LOG_INFO
, "%s: trying delivery",
743 error
= deliver_remote(it
, &errmsg
);
745 error
= deliver_local(it
, &errmsg
);
750 syslog(LOG_INFO
, "%s: delivery successful",
755 if (stat(it
->queuefn
, &st
) != 0) {
756 syslog(LOG_ERR
, "%s: lost queue file `%s'",
757 it
->queueid
, it
->queuefn
);
760 if (gettimeofday(&now
, NULL
) == 0 &&
761 (now
.tv_sec
- st
.st_mtimespec
.tv_sec
> MAX_TIMEOUT
)) {
762 asprintf(__DECONST(void *, &errmsg
),
763 "Could not deliver for the last %d seconds. Giving up.",
769 if (backoff
> MAX_RETRY
)
789 return (c
- 'A' + 10);
791 return (c
- 'a' + 10);
799 for (i
= 0; i
< 16; i
++)
800 for (j
= 0; j
< 16; j
++)
801 SLIST_INIT(&seenmsg
[i
][j
]);
805 seen(const char *msgid
)
812 p
= strchr(msgid
, '.');
817 i
= c2x(msgid
[len
- 2]);
818 j
= c2x(msgid
[len
- 1]);
819 } else if (len
== 1) {
825 if (i
< 0 || i
>= 16 || j
< 0 || j
>= 16)
826 errx(1, "INTERNAL ERROR: bad seen code for msgid %s", msgid
);
827 SLIST_FOREACH(t
, &seenmsg
[i
][j
], next
)
828 if (!strncmp(t
->str
, msgid
, len
))
830 t
= malloc(sizeof(*t
));
832 errx(1, "Could not allocate %lu bytes",
833 (unsigned long)(sizeof(*t
)));
834 t
->str
= strdup(msgid
);
836 errx(1, "Could not duplicate msgid %s", msgid
);
837 SLIST_INSERT_HEAD(&seenmsg
[i
][j
], t
, next
);
842 load_queue(struct queue
*queue
, int ignorelock
)
846 //struct queue queue, itmqueue;
847 struct queue itmqueue
;
858 int fd
, locked
, seenit
;
860 LIST_INIT(&queue
->queue
);
862 spooldir
= opendir(config
->spooldir
);
863 if (spooldir
== NULL
)
864 err(1, "reading queue");
867 while ((de
= readdir(spooldir
)) != NULL
) {
873 LIST_INIT(&itmqueue
.queue
);
875 /* ignore temp files */
876 if (strncmp(de
->d_name
, "tmp_", 4) == 0 ||
877 de
->d_type
!= DT_REG
)
879 if (asprintf(&queuefn
, "%s/%s", config
->spooldir
, de
->d_name
) < 0)
881 seenit
= seen(de
->d_name
);
883 fd
= open_locked(queuefn
, O_RDONLY
|O_NONBLOCK
);
885 /* Ignore locked files */
886 if (errno
!= EWOULDBLOCK
)
888 if (!ignorelock
|| seenit
)
890 fd
= open(queuefn
, O_RDONLY
);
896 queuef
= fdopen(fd
, "r");
899 if (fgets(line
, sizeof(line
), queuef
) == NULL
||
902 line
[strlen(line
) - 1] = 0; /* chop newline */
903 sender
= strdup(line
);
908 if (fgets(line
, sizeof(line
), queuef
) == NULL
||
913 line
[strlen(line
) - 1] = 0;
914 queueid
= strdup(line
);
917 addr
= strchr(queueid
, ' ');
923 if (asprintf(&fn
, "%s/%s", config
->spooldir
, queueid
) < 0)
925 /* Item has already been delivered? */
926 if (stat(fn
, &st
) != 0)
928 if (add_recp(&itmqueue
, addr
, sender
, 0) != 0)
930 it
= LIST_FIRST(&itmqueue
.queue
);
932 it
->queueid
= queueid
;
937 if (LIST_EMPTY(&itmqueue
.queue
)) {
938 warnx("queue file without items: `%s'", queuefn
);
941 hdrlen
= ftell(queuef
);
942 while ((it
= LIST_FIRST(&itmqueue
.queue
)) != NULL
) {
944 LIST_REMOVE(it
, next
);
945 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
950 warn("reading queue: `%s'", queuefn
);
966 err(1, "reading queue");
970 run_queue(struct queue
*queue
)
974 if (LIST_EMPTY(&queue
->queue
))
977 it
= go_background(queue
);
983 show_queue(struct queue
*queue
)
987 if (LIST_EMPTY(&queue
->queue
)) {
988 printf("Mail queue is empty\n");
992 LIST_FOREACH(it
, &queue
->queue
, next
) {
996 To\t: %s\n--\n", it
->queueid
, it
->locked
? "*": "", it
->sender
, it
->addr
);
1003 * - alias processing
1004 * - use group permissions
1005 * - proper sysexit codes
1009 main(int argc
, char **argv
)
1011 char *sender
= NULL
;
1015 struct queue lqueue
;
1017 int nodot
= 0, doqueue
= 0, showq
= 0;
1020 LIST_INIT(&queue
.queue
);
1021 snprintf(tag
, 254, "dma");
1023 if (strcmp(argv
[0], "mailq") == 0) {
1027 errx(1, "invalid arguments");
1032 while ((ch
= getopt(argc
, argv
, "A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
1035 /* -AX is being ignored, except for -A{c,m} */
1036 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
1039 /* else FALLTRHOUGH */
1041 /* -bX is being ignored, except for -bp */
1042 if (optarg
[0] == 'p') {
1046 /* else FALLTRHOUGH */
1052 snprintf(tag
, 254, "%s", optarg
);
1060 /* -oX is being ignored, except for -oi */
1061 if (optarg
[0] != 'i')
1063 /* else FALLTRHOUGH */
1074 /* Ignored options */
1098 openlog(tag
, LOG_PID
, LOG_MAIL
);
1101 config
= malloc(sizeof(struct config
));
1103 errx(1, "Cannot allocate enough memory");
1105 memset(config
, 0, sizeof(struct config
));
1106 if (parse_conf(CONF_PATH
, config
) < 0) {
1108 errx(1, "reading config file");
1111 if (config
->features
& VIRTUAL
)
1112 if (parse_virtuser(config
->virtualpath
) < 0)
1113 errx(1, "error reading virtual user file: %s",
1114 config
->virtualpath
);
1116 if (parse_authfile(config
->authpath
) < 0)
1117 err(1, "reading SMTP authentication file");
1121 errx(1, "sending mail and displaying queue is"
1122 " mutually exclusive");
1123 load_queue(&lqueue
, 1);
1124 show_queue(&lqueue
);
1130 errx(1, "sending mail and queue pickup is mutually exclusive");
1131 load_queue(&lqueue
, 0);
1136 if (read_aliases() != 0)
1137 err(1, "reading aliases");
1139 if ((sender
= set_from(sender
)) == NULL
)
1140 err(1, "setting from address");
1142 for (i
= 0; i
< argc
; i
++) {
1143 if (add_recp(&queue
, argv
[i
], sender
, 1) != 0)
1144 errx(1, "invalid recipient `%s'\n", argv
[i
]);
1147 if (LIST_EMPTY(&queue
.queue
))
1148 errx(1, "no recipients");
1150 if (gentempf(&queue
) != 0)
1151 err(1, "create temp file");
1153 if (preparespool(&queue
, sender
) != 0)
1154 err(1, "creating spools (1)");
1156 if (readmail(&queue
, sender
, nodot
) != 0)
1157 err(1, "reading mail");
1159 if (linkspool(&queue
) != 0)
1160 err(1, "creating spools (2)");
1162 /* From here on the mail is safe. */
1164 if (config
->features
& DEFER
)
1167 it
= go_background(&queue
);