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