mailwrapper.c revision 1.8 1 /* $NetBSD: mailwrapper.c,v 1.8 2003/03/08 22:57:51 mjl 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
42 struct arglist {
43 size_t argc, maxc;
44 char **argv;
45 };
46
47 int main __P((int, char *[], char *[]));
48
49 static void initarg __P((struct arglist *));
50 static void addarg __P((struct arglist *, const char *, int));
51
52 static void
53 initarg(al)
54 struct arglist *al;
55 {
56 al->argc = 0;
57 al->maxc = 10;
58 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
59 err(1, "malloc");
60 }
61
62 static void
63 addarg(al, arg, copy)
64 struct arglist *al;
65 const char *arg;
66 int copy;
67 {
68 if (al->argc == al->maxc) {
69 al->maxc <<= 1;
70 if ((al->argv = realloc(al->argv,
71 al->maxc * sizeof(char *))) == NULL)
72 err(1, "realloc");
73 }
74 if (copy) {
75 if ((al->argv[al->argc++] = strdup(arg)) == NULL)
76 err(1, "strdup");
77 } else
78 al->argv[al->argc++] = (char *)arg;
79 }
80
81 int
82 main(argc, argv, envp)
83 int argc;
84 char *argv[];
85 char *envp[];
86 {
87 FILE *config;
88 char *line, *cp, *from, *to, *ap;
89 size_t len, lineno = 0;
90 int i;
91 struct arglist al;
92
93 initarg(&al);
94 addarg(&al, argv[0], 0);
95
96 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
97 err(1, "can't open %s", _PATH_MAILERCONF);
98
99 for (;;) {
100 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
101 if (feof(config))
102 errx(1, "no mapping in %s", _PATH_MAILERCONF);
103 err(1, "fparseln");
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, al.argv, envp);
142 err(1, "execing %s", to);
143 /*NOTREACHED*/
144 parse_error:
145 errx(1, "parse error in %s at line %lu",
146 _PATH_MAILERCONF, (u_long)lineno);
147 /*NOTREACHED*/
148 }
149