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 $ */
3 /* $FreeBSD: src/usr.sbin/mailwrapper/mailwrapper.c,v 1.4.2.3 2001/10/01 12:52:47 dd Exp $ */
4 /* $DragonFly: src/usr.sbin/mailwrapper/mailwrapper.c,v 1.4 2003/11/22 11:38:13 eirikn Exp $ */
8 * Perry E. Metzger. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
46 #include "pathnames.h"
53 int main(int, char *[], char *[]);
55 static void initarg(struct arglist *);
56 static void addarg(struct arglist *, const char *, int);
57 static void freearg(struct arglist *, int);
59 extern const char *__progname; /* from crt0.o */
62 initarg(struct arglist *al)
66 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
71 addarg(struct arglist *al, const char *arg, int copy)
75 if (al->argc == al->maxc) {
78 if ((argv2 = realloc(al->argv,
79 al->maxc * sizeof(char *))) == NULL) {
89 if ((al->argv[al->argc++] = strdup(arg)) == NULL)
92 al->argv[al->argc++] = (char *)arg;
96 freearg(struct arglist *al, int copy)
100 for (i = 0; i < al->argc; i++)
106 main(int argc, char **argv, char **envp)
109 char *line, *cp, *from, *to, *ap;
110 size_t len, lineno = 0;
114 for (len = 0; len < argc; len++)
115 addarg(&al, argv[len], 0);
117 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
118 addarg(&al, NULL, 0);
119 openlog("mailwrapper", LOG_PID, LOG_MAIL);
120 syslog(LOG_INFO, "can't open %s, using %s as default MTA",
121 _PATH_MAILERCONF, _PATH_DEFAULTMTA);
123 execve(_PATH_DEFAULTMTA, al.argv, envp);
125 err(1, "execing %s", _PATH_DEFAULTMTA);
130 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
132 errx(1, "no mapping in %s", _PATH_MAILERCONF);
133 err(1, "can't parse line %lu", (u_long)lineno);
139 cp += strspn(cp, WS);
146 if ((from = strsep(&cp, WS)) == NULL)
149 cp += strspn(cp, WS);
151 if ((to = strsep(&cp, WS)) == NULL)
154 if (strcmp(from, __progname) == 0) {
155 for (ap = strsep(&cp, WS); ap != NULL;
156 ap = strsep(&cp, WS))
165 (void)fclose(config);
167 addarg(&al, NULL, 0);
168 execve(to, al.argv, envp);
170 warn("execing %s", to);
177 errx(1, "parse error in %s at line %lu",
178 _PATH_MAILERCONF, (u_long)lineno);