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