mailwrapper.c revision 1.3 1 /* $NetBSD: mailwrapper.c,v 1.3 1999/05/29 18:18:15 christos 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 #include <util.h>
40
41 #define _PATH_MAILERCONF "/etc/mailer.conf"
42
43 struct arglist {
44 size_t argc, maxc;
45 char **argv;
46 };
47
48 int main __P((int, char *[], char *[]));
49
50 static void initarg __P((struct arglist *));
51 static void addarg __P((struct arglist *, const char *, int));
52 static void freearg __P((struct arglist *, int));
53
54 extern const char *__progname; /* from crt0.o */
55
56 static void
57 initarg(al)
58 struct arglist *al;
59 {
60 al->argc = 0;
61 al->maxc = 10;
62 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
63 err(1, "mailwrapper");
64 }
65
66 static void
67 addarg(al, arg, copy)
68 struct arglist *al;
69 const char *arg;
70 int copy;
71 {
72 if (al->argc == al->maxc) {
73 al->maxc <<= 1;
74 if ((al->argv = realloc(al->argv,
75 al->maxc * sizeof(char *))) == NULL)
76 err(1, "mailwrapper");
77 }
78 if (copy) {
79 if ((al->argv[al->argc++] = strdup(arg)) == NULL)
80 err(1, "mailwrapper:");
81 } else
82 al->argv[al->argc++] = (char *)arg;
83 }
84
85 static void
86 freearg(al, copy)
87 struct arglist *al;
88 int copy;
89 {
90 size_t i;
91 if (copy)
92 for (i = 0; i < al->argc; i++)
93 free(al->argv[i]);
94 free(al->argv);
95 }
96
97 int
98 main(argc, argv, envp)
99 int argc;
100 char *argv[];
101 char *envp[];
102 {
103 FILE *config;
104 char *line, *cp, *from, *to, *ap;
105 size_t len, lineno = 0;
106 struct arglist al;
107
108 initarg(&al);
109 for (len = 0; len < argc; len++)
110 addarg(&al, argv[len], 0);
111
112 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
113 err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF);
114
115 for (;;) {
116 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
117 if (feof(config))
118 errx(1, "mailwrapper: no mapping in %s",
119 _PATH_MAILERCONF);
120 err(1, "mailwrapper");
121 }
122
123 #define WS " \t\n"
124 cp = line;
125
126 cp += strspn(cp, WS);
127 if (cp[0] == '\0') {
128 /* empty line */
129 free(line);
130 continue;
131 }
132
133 if ((from = strsep(&cp, WS)) == NULL)
134 goto parse_error;
135
136 cp += strspn(cp, WS);
137
138 if ((to = strsep(&cp, WS)) == NULL)
139 goto parse_error;
140
141 if (strcmp(from, __progname) == 0) {
142 for (ap = strsep(&cp, WS); ap != NULL;
143 ap = strsep(&cp, WS))
144 if (*ap)
145 addarg(&al, ap, 0);
146 break;
147 }
148
149 free(line);
150 }
151
152 (void)fclose(config);
153
154 execve(to, al.argv, envp);
155 freearg(&al, 0);
156 free(line);
157 err(1, "mailwrapper: execing %s", to);
158 /*NOTREACHED*/
159 parse_error:
160 freearg(&al, 0);
161 free(line);
162 errx(1, "mailwrapper: parse error in %s at line %lu",
163 _PATH_MAILERCONF, (u_long)lineno);
164 /*NOTREACHED*/
165 }
166