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