]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mailwrapper/mailwrapper.c
This commit was generated by cvs2svn to compensate for changes in r156701,
[FreeBSD/FreeBSD.git] / usr.sbin / mailwrapper / mailwrapper.c
1 /*      $OpenBSD: mailwrapper.c,v 1.6 1999/12/17 05:06:28 mickey Exp $  */
2 /*      $NetBSD: mailwrapper.c,v 1.3 1999/05/29 18:18:15 christos 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 <stdlib.h>
42 #include <unistd.h>
43 #include <libutil.h>
44 #include <syslog.h>
45 #include <stdarg.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 *, int);
58 static void freearg(struct arglist *, int);
59
60 extern const char *__progname;  /* from crt0.o */
61
62 static void
63 initarg(al)
64         struct arglist *al;
65 {
66         al->argc = 0;
67         al->maxc = 10;
68         if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
69                 err(1, NULL);
70 }
71
72 static void
73 addarg(al, arg, copy)
74         struct arglist *al;
75         const char *arg;
76         int copy;
77 {
78         char **argv2;
79
80         if (al->argc == al->maxc) {
81                 al->maxc <<= 1;
82
83                 if ((argv2 = realloc(al->argv,
84                     al->maxc * sizeof(char *))) == NULL) {
85                         if (al->argv)
86                                 free(al->argv);
87                         al->argv = NULL;
88                         err(1, NULL);
89                 } else {
90                         al->argv = argv2;
91                 }
92         }
93         if (copy) {
94                 if ((al->argv[al->argc++] = strdup(arg)) == NULL)
95                         err(1, NULL);
96         } else
97                 al->argv[al->argc++] = (char *)arg;
98 }
99
100 static void
101 freearg(al, copy)
102         struct arglist *al;
103         int copy;
104 {
105         size_t i;
106         if (copy)
107                 for (i = 0; i < al->argc; i++)
108                         free(al->argv[i]);
109         free(al->argv);
110 }
111
112 int
113 main(argc, argv, envp)
114         int argc;
115         char *argv[];
116         char *envp[];
117 {
118         FILE *config;
119         char *line, *cp, *from, *to, *ap;
120         size_t len, lineno = 0;
121         struct arglist al;
122
123         initarg(&al);
124         for (len = 0; len < argc; len++)
125                 addarg(&al, argv[len], 0);
126
127         if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
128                 addarg(&al, NULL, 0);
129                 openlog("mailwrapper", LOG_PID, LOG_MAIL);
130                 syslog(LOG_INFO, "can't open %s, using %s as default MTA",
131                     _PATH_MAILERCONF, _PATH_DEFAULTMTA);
132                 closelog();
133                 execve(_PATH_DEFAULTMTA, al.argv, envp);
134                 freearg(&al, 0);
135                 err(1, "execing %s", _PATH_DEFAULTMTA);
136                 /*NOTREACHED*/
137         }
138
139         for (;;) {
140                 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
141                         if (feof(config))
142                                 errx(1, "no mapping in %s", _PATH_MAILERCONF);
143                         err(1, "can't parse line %lu", (u_long)lineno);
144                 }
145
146 #define WS      " \t\n"
147                 cp = line;
148
149                 cp += strspn(cp, WS);
150                 if (cp[0] == '\0') {
151                         /* empty line */
152                         free(line);
153                         continue;
154                 }
155
156                 if ((from = strsep(&cp, WS)) == NULL)
157                         goto parse_error;
158
159                 cp += strspn(cp, WS);
160
161                 if ((to = strsep(&cp, WS)) == NULL)
162                         goto parse_error;
163
164                 if (strcmp(from, __progname) == 0) {
165                         for (ap = strsep(&cp, WS); ap != NULL; 
166                             ap = strsep(&cp, WS))
167                             if (*ap)
168                                     addarg(&al, ap, 0);
169                         break;
170                 }
171
172                 free(line);
173         }
174
175         (void)fclose(config);
176
177         addarg(&al, NULL, 0);
178         execve(to, al.argv, envp);
179         freearg(&al, 0);
180         warn("execing %s", to);
181         free(line);
182         exit(1);
183         /*NOTREACHED*/
184 parse_error:
185         freearg(&al, 0);
186         free(line);
187         errx(1, "parse error in %s at line %lu",
188             _PATH_MAILERCONF, (u_long)lineno);
189         /*NOTREACHED*/
190 }