1 /* $OpenBSD: mailwrapper.c,v 1.6 1999/12/17 05:06:28 mickey Exp $ */
2 /* $NetBSD: mailwrapper.c,v 1.3 1999/05/29 18:18:15 christos Exp $ */
6 * Perry E. Metzger. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * 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 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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
47 #include "pathnames.h"
54 int main(int, char *[], char *[]);
56 static void initarg(struct arglist *);
57 static void addarg(struct arglist *, const char *, int);
58 static void freearg(struct arglist *, int);
60 extern const char *__progname; /* from crt0.o */
68 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
80 if (al->argc == al->maxc) {
83 if ((argv2 = realloc(al->argv,
84 al->maxc * sizeof(char *))) == NULL) {
94 if ((al->argv[al->argc++] = strdup(arg)) == NULL)
97 al->argv[al->argc++] = (char *)arg;
107 for (i = 0; i < al->argc; i++)
113 main(argc, argv, envp)
119 char *line, *cp, *from, *to, *ap;
120 size_t len, lineno = 0;
124 for (len = 0; len < argc; len++)
125 addarg(&al, argv[len], 0);
127 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
128 addarg(&al, NULL, 0);
129 openlog("mailwrapper", LOG_PID, LOG_MAIL);
130 syslog(LOG_INFO, "can't open %s, using %s as default MTA",
131 _PATH_MAILERCONF, _PATH_DEFAULTMTA);
133 execve(_PATH_DEFAULTMTA, al.argv, envp);
135 err(1, "execing %s", _PATH_DEFAULTMTA);
140 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
142 errx(1, "no mapping in %s", _PATH_MAILERCONF);
143 err(1, "can't parse line %lu", (u_long)lineno);
149 cp += strspn(cp, WS);
156 if ((from = strsep(&cp, WS)) == NULL)
159 cp += strspn(cp, WS);
161 if ((to = strsep(&cp, WS)) == NULL)
164 if (strcmp(from, __progname) == 0) {
165 for (ap = strsep(&cp, WS); ap != NULL;
166 ap = strsep(&cp, WS))
175 (void)fclose(config);
177 addarg(&al, NULL, 0);
178 execve(to, al.argv, envp);
180 warn("execing %s", to);
187 errx(1, "parse error in %s at line %lu",
188 _PATH_MAILERCONF, (u_long)lineno);