1/*	$NetBSD: mailwrapper.c,v 1.11 2018/01/23 21:06:25 sevan Exp $	*/
2
3/*
4 * Copyright (c) 1998
5 * 	Perry E. Metzger.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgment:
17 *	This product includes software developed for the NetBSD Project
18 *	by Perry E. Metzger.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <err.h>
35#include <stdio.h>
36#include <string.h>
37#include <stdlib.h>
38#include <unistd.h>
39
40#define _PATH_MAILERCONF	"/etc/mailer.conf"
41
42struct arglist {
43	size_t argc, maxc;
44	const char **argv;
45};
46
47static void initarg(struct arglist *);
48static void addarg(struct arglist *, const char *, int);
49
50static void
51initarg(struct arglist *al)
52{
53	al->argc = 0;
54	al->maxc = 10;
55	if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
56		/*
57		 * This (using err("mailwrapper")) is intentional.
58		 * Mailwrapper plays ugly games with argv[0] and thus it
59		 * is often difficult for people to know that the error
60		 * isn't from "mailq" or "sendmail" but from mailwrapper
61		 * -- having mailwrapper add an indication that it was really
62		 * mailwrapper running was a requested feature.
63		 */
64		err(1, "mailwrapper");
65}
66
67static void
68addarg(struct arglist *al, const char *arg, int copy)
69{
70	if (al->argc == al->maxc) {
71	    al->maxc <<= 1;
72	    if ((al->argv = realloc(al->argv,
73		al->maxc * sizeof(char *))) == NULL)
74		    err(1, "mailwrapper");
75	}
76	if (copy) {
77		if ((al->argv[al->argc++] = strdup(arg)) == NULL)
78			err(1, "mailwrapper:");
79	} else
80		al->argv[al->argc++] = arg;
81}
82
83int
84main(int argc, char *argv[], char *envp[])
85{
86	FILE *config;
87	char *line, *cp, *from, *to, *ap;
88	size_t len, lineno = 0;
89	int i;
90	struct arglist al;
91
92	initarg(&al);
93	addarg(&al, argv[0], 0);
94
95	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
96		err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF);
97
98	for (;;) {
99		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
100			if (feof(config))
101				errx(1, "mailwrapper: no mapping in %s",
102				    _PATH_MAILERCONF);
103			err(1, "mailwrapper");
104		}
105
106#define	WS	" \t\n"
107		cp = line;
108
109		cp += strspn(cp, WS);
110		if (cp[0] == '\0') {
111			/* empty line */
112			free(line);
113			continue;
114		}
115
116		if ((from = strsep(&cp, WS)) == NULL)
117			goto parse_error;
118
119		cp += strspn(cp, WS);
120
121		if ((to = strsep(&cp, WS)) == NULL)
122			goto parse_error;
123
124		if (strcmp(from, getprogname()) == 0) {
125			for (ap = strsep(&cp, WS); ap != NULL;
126			    ap = strsep(&cp, WS))
127			    if (*ap)
128				    addarg(&al, ap, 0);
129			break;
130		}
131
132		free(line);
133	}
134
135	(void)fclose(config);
136
137	for (i = 1; i < argc; i++)
138		addarg(&al, argv[i], 0);
139
140	addarg(&al, NULL, 0);
141	execve(to, __UNCONST(al.argv), envp);
142	err(1, "mailwrapper: execing %s", to);
143	/*NOTREACHED*/
144parse_error:
145	errx(1, "mailwrapper: parse error in %s at line %lu",
146	    _PATH_MAILERCONF, (u_long)lineno);
147	/*NOTREACHED*/
148}
149