xref: /openbsd-src/usr.sbin/mailwrapper/mailwrapper.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: mailwrapper.c,v 1.15 2003/03/09 01:24:26 millert Exp $	*/
2 /*	$NetBSD: mailwrapper.c,v 1.2 1999/02/20 22:10:07 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1998
6  * 	Perry E. Metzger.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgment:
18  *	This product includes software developed for the NetBSD Project
19  *	by Perry E. Metzger.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <err.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <syslog.h>
40 #include <unistd.h>
41 #include <util.h>
42 
43 #define _PATH_MAILERCONF	"/etc/mailer.conf"
44 #define _PATH_DEFAULTMTA	"/usr/libexec/sendmail/sendmail"
45 
46 struct arglist {
47 	size_t argc, maxc;
48 	char **argv;
49 };
50 
51 int main(int, char *[], char *[]);
52 
53 static void initarg(struct arglist *);
54 static void addarg(struct arglist *, const char *, int);
55 
56 extern const char *__progname;	/* from crt0.o */
57 
58 static void
59 initarg(struct arglist *al)
60 {
61 	al->argc = 0;
62 	al->maxc = 10;
63 	if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
64 		err(1, "malloc");
65 }
66 
67 static void
68 addarg(struct arglist *al, const char *arg, int copy)
69 {
70 	char **argv2;
71 
72 	if (al->argc == al->maxc) {
73 		al->maxc <<= 1;
74 
75 		if ((argv2 = realloc(al->argv,
76 		    al->maxc * sizeof(char *))) == NULL) {
77 			if (al->argv)
78 				free(al->argv);
79 			al->argv = NULL;
80 			err(1, "realloc");
81 		} else {
82 			al->argv = argv2;
83 		}
84 	}
85 	if (copy) {
86 		if ((al->argv[al->argc++] = strdup(arg)) == NULL)
87 			err(1, "strdup");
88 	} else
89 		al->argv[al->argc++] = (char *)arg;
90 }
91 
92 int
93 main(int argc, char *argv[], char *envp[])
94 {
95 	FILE *config;
96 	char *line, *cp, *from, *to, *ap;
97 	const char *progname;
98 	size_t len, lineno = 0;
99 	struct arglist al;
100 
101 	/* change __progname to mailwrapper so we get sensible error messages */
102 	progname = __progname;
103 	__progname = "mailwrapper";
104 
105 	initarg(&al);
106 	for (len = 0; len < argc; len++)
107 		addarg(&al, argv[len], 0);
108 
109 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
110 		addarg(&al, NULL, 0);
111 		openlog(__progname, LOG_PID, LOG_MAIL);
112 		syslog(LOG_INFO, "cannot open %s, using %s as default MTA",
113 		    _PATH_MAILERCONF, _PATH_DEFAULTMTA);
114 		closelog();
115 		execve(_PATH_DEFAULTMTA, al.argv, envp);
116 		err(1, "cannot exec %s", _PATH_DEFAULTMTA);
117 		/*NOTREACHED*/
118 	}
119 
120 	for (;;) {
121 		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
122 			if (feof(config))
123 				errx(1, "no mapping in %s", _PATH_MAILERCONF);
124 			err(1, "fparseln");
125 		}
126 
127 #define	WS	" \t\n"
128 		cp = line;
129 
130 		cp += strspn(cp, WS);
131 		if (cp[0] == '\0') {
132 			/* empty line */
133 			free(line);
134 			continue;
135 		}
136 
137 		if ((from = strsep(&cp, WS)) == NULL)
138 			goto parse_error;
139 
140 		cp += strspn(cp, WS);
141 
142 		if ((to = strsep(&cp, WS)) == NULL)
143 			goto parse_error;
144 
145 		if (strcmp(from, progname) == 0) {
146 			for (ap = strsep(&cp, WS); ap != NULL;
147 			    ap = strsep(&cp, WS))
148 				if (*ap)
149 					addarg(&al, ap, 0);
150 			break;
151 		}
152 
153 		free(line);
154 	}
155 
156 	(void)fclose(config);
157 
158 	addarg(&al, NULL, 0);
159 
160 	execve(to, al.argv, envp);
161 	err(1, "cannot exec %s", to);
162 	/*NOTREACHED*/
163 parse_error:
164 	errx(1, "parse error in %s at line %lu",
165 	    _PATH_MAILERCONF, (u_long)lineno);
166 	/*NOTREACHED*/
167 }
168