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