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