xref: /titanic_51/usr/src/cmd/mailwrapper/mailwrapper.c (revision b59c4a48daf5a1863ecac763711b497b2f8321e4)
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  * 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 <compat.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <sysexits.h>
42 #include <syslog.h>
43 #include <limits.h>
44 
45 #include "pathnames.h"
46 
47 struct arglist {
48 	size_t argc, maxc;
49 	char **argv;
50 };
51 
52 int main(int, char *[], char *[]);
53 
54 static void initarg(struct arglist *);
55 static void addarg(struct arglist *, const char *);
56 
57 static void
58 initarg(struct arglist *al)
59 {
60 	al->argc = 0;
61 	al->maxc = 10;
62 	if ((al->argv = calloc(al->maxc, sizeof (char *))) == NULL)
63 		err(EX_TEMPFAIL, "calloc");
64 }
65 
66 static void
67 addarg(struct arglist *al, const char *arg)
68 {
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(EX_TEMPFAIL, "realloc");
75 	}
76 	if (arg == NULL)
77 		al->argv[al->argc++] = NULL;
78 	else if ((al->argv[al->argc++] = strdup(arg)) == NULL)
79 		err(EX_TEMPFAIL, "strdup");
80 }
81 
82 int
83 main(int argc, char *argv[], char *envp[])
84 {
85 	FILE *config;
86 	char *line, *cp, *from, *to, *ap;
87 	char progname[PATH_MAX+1];
88 	const char *name;
89 	size_t len, lineno = 0;
90 	int i;
91 	struct arglist al;
92 
93 	/* change __progname to mailwrapper so we get sensible error messages */
94 	name = getprogname();
95 	if (name) {
96 		strncpy(progname, name, sizeof (progname));
97 	} else {
98 		err(EX_OSERR, "cannot get program name");
99 	}
100 	setprogname("mailwrapper");
101 
102 	initarg(&al);
103 	addarg(&al, argv[0]);
104 
105 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
106 		addarg(&al, NULL);
107 		openlog(getprogname(), LOG_PID, LOG_MAIL);
108 		syslog(LOG_INFO, "cannot open %s, using %s as default MTA",
109 		    _PATH_MAILERCONF, _PATH_DEFAULTMTA);
110 		closelog();
111 		execve(_PATH_DEFAULTMTA, al.argv, envp);
112 		err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA);
113 		/*NOTREACHED*/
114 	}
115 
116 	for (;;) {
117 		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
118 			if (feof(config))
119 				errx(EX_CONFIG, "no mapping for %s in %s",
120 				    progname, _PATH_MAILERCONF);
121 			err(EX_CONFIG, "cannot parse line %lu",
122 			    (ulong_t)lineno);
123 		}
124 
125 #define	WS	" \t\n"
126 		cp = line;
127 
128 		cp += strspn(cp, WS);
129 		if (cp[0] == '\0') {
130 			/* empty line */
131 			free(line);
132 			continue;
133 		}
134 
135 		if ((from = strsep(&cp, WS)) == NULL || cp == NULL)
136 			goto parse_error;
137 
138 		cp += strspn(cp, WS);
139 
140 		if ((to = strsep(&cp, WS)) == NULL)
141 			goto parse_error;
142 
143 		if (strcmp(from, progname) == 0) {
144 			for (ap = strsep(&cp, WS); ap != NULL;
145 			    ap = strsep(&cp, WS)) {
146 				if (*ap)
147 					addarg(&al, ap);
148 			}
149 			break;
150 		}
151 
152 		free(line);
153 	}
154 
155 	(void) fclose(config);
156 
157 	for (i = 1; i < argc; i++)
158 		addarg(&al, argv[i]);
159 
160 	addarg(&al, NULL);
161 	execve(to, al.argv, envp);
162 	err(EX_OSERR, "cannot exec %s", to);
163 	/*NOTREACHED*/
164 parse_error:
165 	errx(EX_CONFIG, "parse error in %s at line %lu",
166 	    _PATH_MAILERCONF, (ulong_t)lineno);
167 	/*NOTREACHED*/
168 	return (0);
169 }
170