]>
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 const char *username
;
78 static struct strlist seenmsg
[16][16];
84 static char name
[MAXHOSTNAMELEN
+1];
92 if (config
->mailname
!= NULL
&& config
->mailname
[0] != '\0') {
93 snprintf(name
, sizeof(name
), "%s", config
->mailname
);
97 if (config
->mailnamefile
!= NULL
&& config
->mailnamefile
[0] != '\0') {
98 fp
= fopen(config
->mailnamefile
, "r");
100 if (fgets(name
, sizeof(name
), fp
) != NULL
) {
103 (name
[len
- 1] == '\r' ||
104 name
[len
- 1] == '\n'))
106 if (name
[0] != '\0') {
114 if (gethostname(name
, sizeof(name
)) != 0)
115 strcpy(name
, "(unknown hostname)");
121 check_username(const char *name
, uid_t ckuid
)
127 pwd
= getpwnam(name
);
128 if (pwd
== NULL
|| pwd
->pw_uid
!= ckuid
)
140 username
= check_username(getlogin(), uid
);
141 if (username
== NULL
)
142 username
= check_username(getenv("LOGNAME"), uid
);
143 if (username
== NULL
)
144 username
= check_username(getenv("USER"), uid
);
145 if (username
== NULL
) {
147 if (pwd
!= NULL
&& pwd
->pw_name
!= NULL
&&
148 pwd
->pw_name
[0] != '\0')
149 username
= check_username(strdup(pwd
->pw_name
), uid
);
151 if (username
== NULL
) {
152 asprintf(&u
, "%ld", (long)uid
);
155 if (username
== NULL
)
156 username
= "unknown-or-invalid-username";
160 set_from(const char *osender
)
165 if ((config
->features
& VIRTUAL
) != 0) {
166 SLIST_FOREACH(v
, &virtusers
, next
) {
167 if (strcmp(v
->login
, username
) == 0) {
168 sender
= strdup(v
->address
);
177 sender
= strdup(osender
);
181 if (asprintf(&sender
, "%s@%s", username
, hostname()) <= 0)
185 if (strchr(sender
, '\n') != NULL
) {
197 yyin
= fopen(config
->aliases
, "r");
199 return (0); /* not fatal */
201 return (-1); /* fatal error, probably malloc() */
207 add_recp(struct queue
*queue
, const char *str
, const char *sender
, int expand
)
209 struct qitem
*it
, *tit
;
216 it
= calloc(1, sizeof(*it
));
219 it
->addr
= strdup(str
);
220 if (it
->addr
== NULL
)
224 host
= strrchr(it
->addr
, '@');
226 (strcmp(host
+ 1, hostname()) == 0 ||
227 strcmp(host
+ 1, "localhost") == 0)) {
230 LIST_FOREACH(tit
, &queue
->queue
, next
) {
231 /* weed out duplicate dests */
232 if (strcmp(tit
->addr
, it
->addr
) == 0) {
238 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
239 if (strrchr(it
->addr
, '@') == NULL
) {
242 LIST_FOREACH(al
, &aliases
, next
) {
243 if (strcmp(al
->alias
, it
->addr
) != 0)
245 SLIST_FOREACH(sit
, &al
->dests
, next
) {
246 if (add_recp(queue
, sit
->str
, sender
, 1) != 0)
252 LIST_REMOVE(it
, next
);
254 /* Local destination, check */
255 pw
= getpwnam(it
->addr
);
278 SLIST_FOREACH(t
, &tmpfs
, next
) {
284 gentempf(struct queue
*queue
)
290 if (snprintf(fn
, sizeof(fn
), "%s/%s", config
->spooldir
, "tmp_XXXXXXXXXX") <= 0)
295 if (flock(fd
, LOCK_EX
) == -1)
298 queue
->tmpf
= strdup(fn
);
299 if (queue
->tmpf
== NULL
) {
303 t
= malloc(sizeof(*t
));
305 t
->str
= queue
->tmpf
;
306 SLIST_INSERT_HEAD(&tmpfs
, t
, next
);
312 open_locked(const char *fname
, int flags
)
317 fd
= open(fname
, flags
, 0);
320 if (flock(fd
, LOCK_EX
|((flags
& O_NONBLOCK
)? LOCK_NB
: 0)) < 0) {
328 return(open(fname
, flags
|O_EXLOCK
));
336 * queue-id1 envelope-to1
337 * queue-id2 envelope-to2
342 * queue ids are unique, formed from the inode of the spool file
343 * and a unique identifier.
346 preparespool(struct queue
*queue
, const char *sender
)
348 char line
[1000]; /* by RFC2822 */
355 error
= snprintf(line
, sizeof(line
), "%s\n", sender
);
356 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
360 if (write(queue
->mailfd
, line
, error
) != error
)
363 queuef
= fdopen(queue
->mailfd
, "r+");
368 * Assign queue id to each dest.
370 if (fstat(queue
->mailfd
, &st
) != 0)
372 queue
->id
= st
.st_ino
;
373 LIST_FOREACH(it
, &queue
->queue
, next
) {
374 if (asprintf(&it
->queueid
, "%"PRIxMAX
".%"PRIxPTR
,
375 queue
->id
, (uintptr_t)it
) <= 0)
377 if (asprintf(&it
->queuefn
, "%s/%s",
378 config
->spooldir
, it
->queueid
) <= 0)
380 /* File may not exist yet */
381 if (stat(it
->queuefn
, &st
) == 0)
384 error
= snprintf(line
, sizeof(line
), "%s %s\n",
385 it
->queueid
, it
->addr
);
386 if (error
< 0 || (size_t)error
>= sizeof(line
))
388 if (write(queue
->mailfd
, line
, error
) != error
)
392 if (write(queue
->mailfd
, line
, 1) != 1)
395 hdrlen
= lseek(queue
->mailfd
, 0, SEEK_CUR
);
396 LIST_FOREACH(it
, &queue
->queue
, next
) {
410 error
= strftime(str
, sizeof(str
), "%a, %d %b %Y %T %z",
413 strcpy(str
, "(date fail)");
418 readmail(struct queue
*queue
, const char *sender
, int nodot
)
420 char line
[1000]; /* by RFC2822 */
424 error
= snprintf(line
, sizeof(line
), "\
425 Received: from %s (uid %d)\n\
426 \t(envelope-from %s)\n\
435 if (error
< 0 || (size_t)error
>= sizeof(line
))
437 if (write(queue
->mailfd
, line
, error
) != error
)
440 while (!feof(stdin
)) {
441 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
443 linelen
= strlen(line
);
444 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
445 errno
= EINVAL
; /* XXX mark permanent errors */
448 if (!nodot
&& linelen
== 2 && line
[0] == '.')
450 if ((size_t)write(queue
->mailfd
, line
, linelen
) != linelen
)
453 if (fsync(queue
->mailfd
) != 0)
459 linkspool(struct queue
*queue
)
463 LIST_FOREACH(it
, &queue
->queue
, next
) {
464 if (link(queue
->tmpf
, it
->queuefn
) != 0)
471 LIST_FOREACH(it
, &queue
->queue
, next
) {
477 static struct qitem
*
478 go_background(struct queue
*queue
)
484 if (daemonize
&& daemon(0, 0) != 0) {
485 syslog(LOG_ERR
, "can not daemonize: %m");
490 bzero(&sa
, sizeof(sa
));
491 sa
.sa_flags
= SA_NOCLDWAIT
;
492 sa
.sa_handler
= SIG_IGN
;
493 sigaction(SIGCHLD
, &sa
, NULL
);
495 LIST_FOREACH(it
, &queue
->queue
, next
) {
496 /* No need to fork for the last dest */
497 if (LIST_NEXT(it
, next
) == NULL
)
503 syslog(LOG_ERR
, "can not fork: %m");
511 * return and deliver mail
525 syslog(LOG_CRIT
, "reached dead code");
530 bounce(struct qitem
*it
, char *reason
)
532 struct queue bounceq
;
538 /* Don't bounce bounced mails */
539 if (it
->sender
[0] == 0) {
540 syslog(LOG_CRIT
, "%s: delivery panic: can't bounce a bounce",
545 syslog(LOG_ERR
, "%s: delivery failed, bouncing",
548 LIST_INIT(&bounceq
.queue
);
549 if (add_recp(&bounceq
, it
->sender
, "", 1) != 0)
551 if (gentempf(&bounceq
) != 0)
553 if (preparespool(&bounceq
, "") != 0)
556 bit
= LIST_FIRST(&bounceq
.queue
);
557 error
= fprintf(bit
->queuef
, "\
558 Received: from MAILER-DAEMON\n\
562 X-Original-To: <%s>\n\
563 From: MAILER-DAEMON <>\n\
565 Subject: Mail delivery failed\n\
566 Message-Id: <%"PRIxMAX
"@%s>\n\
569 This is the %s at %s.\n\
571 There was an error delivering your mail to <%s>.\n\
583 bounceq
.id
, hostname(),
588 config
->features
& FULLBOUNCE
? "Original message follows.":
589 "Message headers follow.");
593 if (fflush(bit
->queuef
) != 0)
596 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0)
598 if (config
->features
& FULLBOUNCE
) {
599 while ((pos
= fread(line
, 1, sizeof(line
), it
->queuef
)) > 0) {
600 if ((size_t)write(bounceq
.mailfd
, line
, pos
) != pos
)
604 while (!feof(it
->queuef
)) {
605 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
609 if ((size_t)write(bounceq
.mailfd
, line
, strlen(line
)) != strlen(line
))
613 if (fsync(bounceq
.mailfd
) != 0)
615 if (linkspool(&bounceq
) != 0)
622 bit
= go_background(&bounceq
);
627 syslog(LOG_CRIT
, "%s: error creating bounce: %m", it
->queueid
);
633 deliver_local(struct qitem
*it
, char **errmsg
)
641 time_t now
= time(NULL
);
643 error
= snprintf(fn
, sizeof(fn
), "%s/%s", _PATH_MAILDIR
, it
->addr
);
644 if (error
< 0 || (size_t)error
>= sizeof(fn
)) {
645 syslog(LOG_ERR
, "%s: local delivery deferred: %m",
650 /* mailx removes users mailspool file if empty, so open with O_CREAT */
651 mbox
= open_locked(fn
, O_WRONLY
| O_APPEND
| O_CREAT
);
653 syslog(LOG_ERR
, "%s: local delivery deferred: can not open `%s': %m",
657 mboxlen
= lseek(mbox
, 0, SEEK_CUR
);
659 if (fseek(it
->queuef
, it
->hdrlen
, SEEK_SET
) != 0) {
660 syslog(LOG_ERR
, "%s: local delivery deferred: can not seek: %m",
665 error
= snprintf(line
, sizeof(line
), "From %s\t%s", it
->sender
, ctime(&now
));
666 if (error
< 0 || (size_t)error
>= sizeof(line
)) {
667 syslog(LOG_ERR
, "%s: local delivery deferred: can not write header: %m",
671 if (write(mbox
, line
, error
) != error
)
674 while (!feof(it
->queuef
)) {
675 if (fgets(line
, sizeof(line
), it
->queuef
) == NULL
)
677 linelen
= strlen(line
);
678 if (linelen
== 0 || line
[linelen
- 1] != '\n') {
679 syslog(LOG_CRIT
, "%s: local delivery failed: corrupted queue file",
681 *errmsg
= strdup("corrupted queue file");
686 if (strncmp(line
, "From ", 5) == 0) {
687 const char *gt
= ">";
689 if (write(mbox
, gt
, 1) != 1)
692 if ((size_t)write(mbox
, line
, linelen
) != linelen
)
696 if (write(mbox
, line
, 1) != 1)
702 syslog(LOG_ERR
, "%s: local delivery failed: write error: %m",
706 if (ftruncate(mbox
, mboxlen
) != 0)
707 syslog(LOG_WARNING
, "%s: error recovering mbox `%s': %m",
714 deliver(struct qitem
*it
)
717 unsigned int backoff
= MIN_RETRY
;
718 char *errmsg
= strdup("unknown bounce reason");
722 syslog(LOG_INFO
, "%s: mail from=<%s> to=<%s>",
723 it
->queueid
, it
->sender
, it
->addr
);
726 syslog(LOG_INFO
, "%s: trying delivery",
730 error
= deliver_remote(it
, &errmsg
);
732 error
= deliver_local(it
, &errmsg
);
737 syslog(LOG_INFO
, "%s: delivery successful",
742 if (stat(it
->queuefn
, &st
) != 0) {
743 syslog(LOG_ERR
, "%s: lost queue file `%s'",
744 it
->queueid
, it
->queuefn
);
747 if (gettimeofday(&now
, NULL
) == 0 &&
748 (now
.tv_sec
- st
.st_mtimespec
.tv_sec
> MAX_TIMEOUT
)) {
750 "Could not deliver for the last %d seconds. Giving up.",
756 if (backoff
> MAX_RETRY
)
776 return (c
- 'A' + 10);
778 return (c
- 'a' + 10);
786 for (i
= 0; i
< 16; i
++)
787 for (j
= 0; j
< 16; j
++)
788 SLIST_INIT(&seenmsg
[i
][j
]);
792 seen(const char *msgid
)
799 p
= strchr(msgid
, '.');
804 i
= c2x(msgid
[len
- 2]);
805 j
= c2x(msgid
[len
- 1]);
806 } else if (len
== 1) {
812 if (i
< 0 || i
>= 16 || j
< 0 || j
>= 16)
813 errx(1, "INTERNAL ERROR: bad seen code for msgid %s", msgid
);
814 SLIST_FOREACH(t
, &seenmsg
[i
][j
], next
)
815 if (!strncmp(t
->str
, msgid
, len
))
817 t
= malloc(sizeof(*t
));
819 errx(1, "Could not allocate %lu bytes",
820 (unsigned long)(sizeof(*t
)));
821 t
->str
= strdup(msgid
);
823 errx(1, "Could not duplicate msgid %s", msgid
);
824 SLIST_INSERT_HEAD(&seenmsg
[i
][j
], t
, next
);
829 load_queue(struct queue
*queue
, int ignorelock
)
833 //struct queue queue, itmqueue;
834 struct queue itmqueue
;
845 int fd
, locked
, seenit
;
847 LIST_INIT(&queue
->queue
);
849 spooldir
= opendir(config
->spooldir
);
850 if (spooldir
== NULL
)
851 err(1, "reading queue");
854 while ((de
= readdir(spooldir
)) != NULL
) {
860 LIST_INIT(&itmqueue
.queue
);
862 /* ignore temp files */
863 if (strncmp(de
->d_name
, "tmp_", 4) == 0 ||
864 de
->d_type
!= DT_REG
)
866 if (asprintf(&queuefn
, "%s/%s", config
->spooldir
, de
->d_name
) < 0)
868 seenit
= seen(de
->d_name
);
870 fd
= open_locked(queuefn
, O_RDONLY
|O_NONBLOCK
);
872 /* Ignore locked files */
873 if (errno
!= EWOULDBLOCK
)
875 if (!ignorelock
|| seenit
)
877 fd
= open(queuefn
, O_RDONLY
);
883 queuef
= fdopen(fd
, "r");
886 if (fgets(line
, sizeof(line
), queuef
) == NULL
||
889 line
[strlen(line
) - 1] = 0; /* chop newline */
890 sender
= strdup(line
);
895 if (fgets(line
, sizeof(line
), queuef
) == NULL
||
900 line
[strlen(line
) - 1] = 0;
901 queueid
= strdup(line
);
904 addr
= strchr(queueid
, ' ');
910 if (asprintf(&fn
, "%s/%s", config
->spooldir
, queueid
) < 0)
912 /* Item has already been delivered? */
913 if (stat(fn
, &st
) != 0)
915 if (add_recp(&itmqueue
, addr
, sender
, 0) != 0)
917 it
= LIST_FIRST(&itmqueue
.queue
);
919 it
->queueid
= queueid
;
924 if (LIST_EMPTY(&itmqueue
.queue
)) {
925 warnx("queue file without items: `%s'", queuefn
);
928 hdrlen
= ftell(queuef
);
929 while ((it
= LIST_FIRST(&itmqueue
.queue
)) != NULL
) {
931 LIST_REMOVE(it
, next
);
932 LIST_INSERT_HEAD(&queue
->queue
, it
, next
);
937 warn("reading queue: `%s'", queuefn
);
953 err(1, "reading queue");
957 run_queue(struct queue
*queue
)
961 if (LIST_EMPTY(&queue
->queue
))
964 it
= go_background(queue
);
970 show_queue(struct queue
*queue
)
974 if (LIST_EMPTY(&queue
->queue
)) {
975 printf("Mail queue is empty\n");
979 LIST_FOREACH(it
, &queue
->queue
, next
) {
983 To\t: %s\n--\n", it
->queueid
, it
->locked
? "*": "", it
->sender
, it
->addr
);
991 * - use group permissions
992 * - proper sysexit codes
996 main(int argc
, char **argv
)
1002 struct queue lqueue
;
1004 int nodot
= 0, doqueue
= 0, showq
= 0;
1007 LIST_INIT(&queue
.queue
);
1008 snprintf(tag
, 254, "dma");
1011 while ((ch
= getopt(argc
, argv
, "A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:UV:vX:")) != -1) {
1014 /* -AX is being ignored, except for -A{c,m} */
1015 if (optarg
[0] == 'c' || optarg
[0] == 'm') {
1018 /* else FALLTRHOUGH */
1020 /* -bX is being ignored, except for -bp */
1021 if (optarg
[0] == 'p') {
1025 /* else FALLTRHOUGH */
1031 snprintf(tag
, 254, "%s", optarg
);
1039 /* -oX is being ignored, except for -oi */
1040 if (optarg
[0] != 'i')
1042 /* else FALLTRHOUGH */
1053 /* Ignored options */
1076 openlog(tag
, LOG_PID
| LOG_PERROR
, LOG_MAIL
);
1079 config
= malloc(sizeof(struct config
));
1081 errx(1, "Cannot allocate enough memory");
1083 memset(config
, 0, sizeof(struct config
));
1084 if (parse_conf(CONF_PATH
, config
) < 0) {
1086 errx(1, "reading config file");
1089 if (config
->features
& VIRTUAL
)
1090 if (parse_virtuser(config
->virtualpath
) < 0)
1091 errx(1, "error reading virtual user file: %s",
1092 config
->virtualpath
);
1094 if (parse_authfile(config
->authpath
) < 0)
1095 err(1, "reading SMTP authentication file");
1099 errx(1, "sending mail and displaying queue is"
1100 " mutually exclusive");
1101 load_queue(&lqueue
, 1);
1102 show_queue(&lqueue
);
1108 errx(1, "sending mail and queue pickup is mutually exclusive");
1109 load_queue(&lqueue
, 0);
1114 if (read_aliases() != 0)
1115 err(1, "reading aliases");
1117 if ((sender
= set_from(sender
)) == NULL
)
1118 err(1, "setting from address");
1120 for (i
= 0; i
< argc
; i
++) {
1121 if (add_recp(&queue
, argv
[i
], sender
, 1) != 0)
1122 errx(1, "invalid recipient `%s'\n", argv
[i
]);
1125 if (LIST_EMPTY(&queue
.queue
))
1126 errx(1, "no recipients");
1128 if (gentempf(&queue
) != 0)
1129 err(1, "create temp file");
1131 if (preparespool(&queue
, sender
) != 0)
1132 err(1, "creating spools (1)");
1134 if (readmail(&queue
, sender
, nodot
) != 0)
1135 err(1, "reading mail");
1137 if (linkspool(&queue
) != 0)
1138 err(1, "creating spools (2)");
1140 /* From here on the mail is safe. */
1142 if (config
->features
& DEFER
)
1145 it
= go_background(&queue
);