1 /*	$OpenBSD: mailwrapper.c,v 1.18 2007/11/06 14:39:19 otto 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 = calloc(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 	if (al->argc == al->maxc) {
71 		al->maxc <<= 1;
72 		al->argv = realloc(al->argv, al->maxc * sizeof(char *));
73 		if (al->argv == NULL)
74 			err(1, "realloc");
75 	}
76 	if (copy) {
77 		if ((al->argv[al->argc++] = strdup(arg)) == NULL)
78 			err(1, "strdup");
79 	} else
80 		al->argv[al->argc++] = (char *)arg;
81 }
82 
83 int
84 main(int argc, char *argv[], char *envp[])
85 {
86 	FILE *config;
87 	char *line, *cp, *from, *to, *ap;
88 	const char *progname;
89 	size_t len, lineno = 0;
90 	struct arglist al;
91 
92 	/* change __progname to mailwrapper so we get sensible error messages */
93 	progname = __progname;
94 	__progname = "mailwrapper";
95 
96 	initarg(&al);
97 	for (len = 0; len < argc; len++)
98 		addarg(&al, argv[len], 0);
99 
100 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
101 		addarg(&al, NULL, 0);
102 		openlog(__progname, LOG_PID, LOG_MAIL);
103 		syslog(LOG_INFO, "cannot open %s, using %s as default MTA",
104 		    _PATH_MAILERCONF, _PATH_DEFAULTMTA);
105 		closelog();
106 		execve(_PATH_DEFAULTMTA, al.argv, envp);
107 		err(1, "cannot exec %s", _PATH_DEFAULTMTA);
108 		/*NOTREACHED*/
109 	}
110 
111 	for (;;) {
112 		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
113 			if (feof(config))
114 				errx(1, "no mapping in %s", _PATH_MAILERCONF);
115 			err(1, "fparseln");
116 		}
117 
118 #define	WS	" \t\n"
119 		cp = line;
120 
121 		cp += strspn(cp, WS);
122 		if (cp[0] == '\0') {
123 			/* empty line */
124 			free(line);
125 			continue;
126 		}
127 
128 		if ((from = strsep(&cp, WS)) == NULL || cp == NULL)
129 			goto parse_error;
130 
131 		cp += strspn(cp, WS);
132 
133 		if ((to = strsep(&cp, WS)) == NULL)
134 			goto parse_error;
135 
136 		if (strcmp(from, progname) == 0) {
137 			for (ap = strsep(&cp, WS); ap != NULL;
138 			    ap = strsep(&cp, WS))
139 				if (*ap)
140 					addarg(&al, ap, 0);
141 			break;
142 		}
143 
144 		free(line);
145 	}
146 
147 	(void)fclose(config);
148 
149 	addarg(&al, NULL, 0);
150 
151 	execve(to, al.argv, envp);
152 	err(1, "cannot exec %s", to);
153 	/*NOTREACHED*/
154 parse_error:
155 	errx(1, "parse error in %s at line %lu",
156 	    _PATH_MAILERCONF, (u_long)lineno);
157 	/*NOTREACHED*/
158 }
159