]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mailwrapper/mailwrapper.c
Fix the previous revision, it suffered from an incomplete change to the
[FreeBSD/FreeBSD.git] / usr.sbin / mailwrapper / mailwrapper.c
1 /*      $OpenBSD: mailwrapper.c,v 1.18 2007/11/06 14:39:19 otto Exp $   */
2 /*      $NetBSD: mailwrapper.c,v 1.9 2003/03/09 08:10:43 mjl Exp $      */
3
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
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 <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41
42 #include <err.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <libutil.h>
49 #include <sysexits.h>
50 #include <syslog.h>
51
52 #include "pathnames.h"
53
54 struct arglist {
55         size_t argc, maxc;
56         char **argv;
57 };
58
59 int main(int, char *[], char *[]);
60
61 static void initarg(struct arglist *);
62 static void addarg(struct arglist *, const char *);
63
64 static void
65 initarg(struct arglist *al)
66 {
67         al->argc = 0;
68         al->maxc = 10;
69         if ((al->argv = calloc(al->maxc, sizeof(char *))) == NULL)
70                 err(EX_TEMPFAIL, "calloc");
71 }
72
73 static void
74 addarg(struct arglist *al, const char *arg)
75 {
76
77         if (al->argc == al->maxc) {
78                 al->maxc <<= 1;
79                 al->argv = realloc(al->argv, al->maxc * sizeof(char *));
80                 if (al->argv == NULL)
81                         err(EX_TEMPFAIL, "realloc");
82         }
83         if (arg == NULL)
84                 al->argv[al->argc++] = NULL;
85         else if ((al->argv[al->argc++] = strdup(arg)) == NULL)
86                 err(EX_TEMPFAIL, "strdup");
87 }
88
89 int
90 main(int argc, char *argv[], char *envp[])
91 {
92         FILE *config;
93         char *line, *cp, *from, *to, *ap;
94         const char *progname;
95         char localmailerconf[MAXPATHLEN];
96         const char *mailerconf;
97         size_t len, lineno = 0;
98         int i;
99         struct arglist al;
100
101         /* change __progname to mailwrapper so we get sensible error messages */
102         progname = getprogname();
103         setprogname("mailwrapper");
104
105         initarg(&al);
106         addarg(&al, argv[0]);
107
108         if ((len = getlocalbase(localmailerconf, MAXPATHLEN)) <= 0) 
109                 err(EX_OSERR, "cannot determine local path");
110
111         strlcat(localmailerconf, "/etc/mail/mailer.conf", MAXPATHLEN);
112
113         mailerconf = localmailerconf;
114         if ((config = fopen(localmailerconf, "r")) == NULL)
115                 mailerconf = _PATH_MAILERCONF;
116
117         if (config == NULL && ((config = fopen(mailerconf, "r")) == NULL)) {
118                 addarg(&al, NULL);
119                 openlog(getprogname(), LOG_PID, LOG_MAIL);
120                 syslog(LOG_INFO, "cannot open %s, using %s as default MTA",
121                     mailerconf, _PATH_DEFAULTMTA);
122                 closelog();
123                 execve(_PATH_DEFAULTMTA, al.argv, envp);
124                 err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA);
125                 /*NOTREACHED*/
126         }
127
128         for (;;) {
129                 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
130                         if (feof(config))
131                                 errx(EX_CONFIG, "no mapping in %s", mailerconf);
132                         err(EX_CONFIG, "cannot parse line %lu", (u_long)lineno);
133                 }
134
135 #define WS      " \t\n"
136                 cp = line;
137
138                 cp += strspn(cp, WS);
139                 if (cp[0] == '\0') {
140                         /* empty line */
141                         free(line);
142                         continue;
143                 }
144
145                 if ((from = strsep(&cp, WS)) == NULL || cp == NULL)
146                         goto parse_error;
147
148                 cp += strspn(cp, WS);
149
150                 if ((to = strsep(&cp, WS)) == NULL)
151                         goto parse_error;
152
153                 if (strcmp(from, progname) == 0) {
154                         for (ap = strsep(&cp, WS); ap != NULL;
155                              ap = strsep(&cp, WS)) {
156                                 if (*ap)
157                                     addarg(&al, ap);
158                         }
159                         break;
160                 }
161
162                 free(line);
163         }
164
165         (void)fclose(config);
166
167         for (i = 1; i < argc; i++)
168                 addarg(&al, argv[i]);
169
170         addarg(&al, NULL);
171         execve(to, al.argv, envp);
172         err(EX_OSERR, "cannot exec %s", to);
173         /*NOTREACHED*/
174 parse_error:
175         errx(EX_CONFIG, "parse error in %s at line %lu",
176             mailerconf, (u_long)lineno);
177         /*NOTREACHED*/
178 }