xref: /netbsd-src/usr.sbin/mailwrapper/mailwrapper.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: mailwrapper.c,v 1.2 1999/02/20 22:10:07 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1998
5  * 	Perry E. Metzger.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgment:
17  *	This product includes software developed for the NetBSD Project
18  *	by Perry E. Metzger.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <err.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <util.h>
40 
41 #define _PATH_MAILERCONF	"/etc/mailer.conf"
42 
43 int main __P((int, char *[], char *[]));
44 
45 extern const char *__progname;	/* from crt0.o */
46 
47 int
48 main(argc, argv, envp)
49 	int argc;
50 	char *argv[];
51 	char *envp[];
52 {
53 	FILE *config;
54 	char *line, *cp, *from, *to;
55 	size_t len, lineno = 0;
56 
57 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
58 		err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF);
59 
60 	for (;;) {
61 		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
62 			if (feof(config))
63 				errx(1, "mailwrapper: no mapping in %s",
64 				    _PATH_MAILERCONF);
65 			err(1, "mailwrapper");
66 		}
67 
68 #define	WHITESPACE	" \t"
69 #define	PARSE_ERROR \
70 		errx(1, "mailwrapper: parse error in %s at line %lu", \
71 		    _PATH_MAILERCONF, (u_long)lineno)
72 
73 		cp = line;
74 
75 		cp += strspn(cp, WHITESPACE);
76 		if (cp[0] == '\0') {
77 			/* empty line */
78 			free(line);
79 			continue;
80 		}
81 
82 		if ((from = strsep(&cp, WHITESPACE)) == NULL)
83 			PARSE_ERROR;
84 
85 		cp += strspn(cp, WHITESPACE);
86 
87 		if ((to = strsep(&cp, WHITESPACE)) == NULL)
88 			PARSE_ERROR;
89 
90 		if (cp != NULL) {
91 			cp += strspn(cp, WHITESPACE);
92 			if (cp[0] != '\0')
93 				PARSE_ERROR;
94 		}
95 
96 		if (strcmp(from, __progname) == 0)
97 			break;
98 
99 		free(line);
100 	}
101 
102 	fclose(config);
103 
104 	execve(to, argv, envp);
105 
106 	err(1, "mailwrapper: execing %s", to);
107 }
108