Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:29:57 dillon 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 __P((int, char *[], char *[]));
54
55 static void initarg __P((struct arglist *));
56 static void addarg __P((struct arglist *, const char *, int));
57 static void freearg __P((struct arglist *, int));
58
59 extern const char *__progname;  /* from crt0.o */
60
61 static void
62 initarg(al)
63         struct arglist *al;
64 {
65         al->argc = 0;
66         al->maxc = 10;
67         if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
68                 err(1, NULL);
69 }
70
71 static void
72 addarg(al, arg, copy)
73         struct arglist *al;
74         const char *arg;
75         int copy;
76 {
77         char **argv2;
78
79         if (al->argc == al->maxc) {
80                 al->maxc <<= 1;
81
82                 if ((argv2 = realloc(al->argv,
83                     al->maxc * sizeof(char *))) == NULL) {
84                         if (al->argv)
85                                 free(al->argv);
86                         al->argv = NULL;
87                         err(1, NULL);
88                 } else {
89                         al->argv = argv2;
90                 }
91         }
92         if (copy) {
93                 if ((al->argv[al->argc++] = strdup(arg)) == NULL)
94                         err(1, NULL);
95         } else
96                 al->argv[al->argc++] = (char *)arg;
97 }
98
99 static void
100 freearg(al, copy)
101         struct arglist *al;
102         int copy;
103 {
104         size_t i;
105         if (copy)
106                 for (i = 0; i < al->argc; i++)
107                         free(al->argv[i]);
108         free(al->argv);
109 }
110
111 int
112 main(argc, argv, envp)
113         int argc;
114         char *argv[];
115         char *envp[];
116 {
117         FILE *config;
118         char *line, *cp, *from, *to, *ap;
119         size_t len, lineno = 0;
120         struct arglist al;
121
122         initarg(&al);
123         for (len = 0; len < argc; len++)
124                 addarg(&al, argv[len], 0);
125
126         if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) {
127                 addarg(&al, NULL, 0);
128                 openlog("mailwrapper", LOG_PID, LOG_MAIL);
129                 syslog(LOG_INFO, "can't open %s, using %s as default MTA",
130                     _PATH_MAILERCONF, _PATH_DEFAULTMTA);
131                 closelog();
132                 execve(_PATH_DEFAULTMTA, al.argv, envp);
133                 freearg(&al, 0);
134                 err(1, "execing %s", _PATH_DEFAULTMTA);
135                 /*NOTREACHED*/
136         }
137
138         for (;;) {
139                 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
140                         if (feof(config))
141                                 errx(1, "no mapping in %s", _PATH_MAILERCONF);
142                         err(1, "can't parse line %lu", (u_long)lineno);
143                 }
144
145 #define WS      " \t\n"
146                 cp = line;
147
148                 cp += strspn(cp, WS);
149                 if (cp[0] == '\0') {
150                         /* empty line */
151                         free(line);
152                         continue;
153                 }
154
155                 if ((from = strsep(&cp, WS)) == NULL)
156                         goto parse_error;
157
158                 cp += strspn(cp, WS);
159
160                 if ((to = strsep(&cp, WS)) == NULL)
161                         goto parse_error;
162
163                 if (strcmp(from, __progname) == 0) {
164                         for (ap = strsep(&cp, WS); ap != NULL; 
165                             ap = strsep(&cp, WS))
166                             if (*ap)
167                                     addarg(&al, ap, 0);
168                         break;
169                 }
170
171                 free(line);
172         }
173
174         (void)fclose(config);
175
176         addarg(&al, NULL, 0);
177         execve(to, al.argv, envp);
178         freearg(&al, 0);
179         warn("execing %s", to);
180         free(line);
181         exit(1);
182         /*NOTREACHED*/
183 parse_error:
184         freearg(&al, 0);
185         free(line);
186         errx(1, "parse error in %s at line %lu",
187             _PATH_MAILERCONF, (u_long)lineno);
188         /*NOTREACHED*/
189 }