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