1 |
|
|
/* $OpenBSD: mailwrapper.c,v 1.23 2017/06/14 16:32:11 anton Exp $ */ |
2 |
|
|
/* $NetBSD: mailwrapper.c,v 1.2 1999/02/20 22:10:07 thorpej 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 <err.h> |
36 |
|
|
#include <stdio.h> |
37 |
|
|
#include <string.h> |
38 |
|
|
#include <stdlib.h> |
39 |
|
|
#include <syslog.h> |
40 |
|
|
#include <unistd.h> |
41 |
|
|
#include <util.h> |
42 |
|
|
|
43 |
|
|
#define _PATH_MAILERCONF "/etc/mailer.conf" |
44 |
|
|
#define _PATH_DEFAULTMTA "/usr/sbin/smtpctl" |
45 |
|
|
|
46 |
|
|
struct arglist { |
47 |
|
|
size_t argc, maxc; |
48 |
|
|
char **argv; |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
static void initarg(struct arglist *); |
52 |
|
|
static void addarg(struct arglist *, const char *); |
53 |
|
|
|
54 |
|
|
extern const char *__progname; /* from crt0.o */ |
55 |
|
|
|
56 |
|
|
static void |
57 |
|
|
initarg(struct arglist *al) |
58 |
|
|
{ |
59 |
|
|
al->argc = 0; |
60 |
|
|
al->maxc = 10; |
61 |
|
|
if ((al->argv = calloc(al->maxc, sizeof(char *))) == NULL) |
62 |
|
|
err(1, "malloc"); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
static void |
66 |
|
|
addarg(struct arglist *al, const char *arg) |
67 |
|
|
{ |
68 |
|
|
if (al->argc == al->maxc) { |
69 |
|
|
al->maxc <<= 1; |
70 |
|
|
al->argv = reallocarray(al->argv, al->maxc, sizeof(char *)); |
71 |
|
|
if (al->argv == NULL) |
72 |
|
|
err(1, "realloc"); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
al->argv[al->argc++] = (char *)arg; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
int |
79 |
|
|
main(int argc, char *argv[]) |
80 |
|
|
{ |
81 |
|
|
FILE *config; |
82 |
|
|
char *line, *cp, *from, *to, *ap; |
83 |
|
|
const char *progname; |
84 |
|
|
size_t len, lineno = 0; |
85 |
|
|
struct arglist al; |
86 |
|
|
|
87 |
|
|
if (pledge("stdio rpath exec flock cpath wpath", NULL) == -1) |
88 |
|
|
err(1, "pledge"); |
89 |
|
|
|
90 |
|
|
/* change __progname to mailwrapper so we get sensible error messages */ |
91 |
|
|
progname = __progname; |
92 |
|
|
__progname = "mailwrapper"; |
93 |
|
|
|
94 |
|
|
initarg(&al); |
95 |
|
|
for (len = 0; len < argc; len++) |
96 |
|
|
addarg(&al, argv[len]); |
97 |
|
|
|
98 |
|
|
config = fopen(_PATH_MAILERCONF, "r"); |
99 |
|
|
|
100 |
|
|
if (pledge("stdio exec flock rpath cpath wpath", NULL) == -1) |
101 |
|
|
err(1, "pledge"); |
102 |
|
|
|
103 |
|
|
if (config == NULL) { |
104 |
|
|
addarg(&al, NULL); |
105 |
|
|
openlog(__progname, LOG_PID, LOG_MAIL); |
106 |
|
|
syslog(LOG_INFO, "cannot open %s, using %s as default MTA", |
107 |
|
|
_PATH_MAILERCONF, _PATH_DEFAULTMTA); |
108 |
|
|
closelog(); |
109 |
|
|
execv(_PATH_DEFAULTMTA, al.argv); |
110 |
|
|
err(1, "cannot exec %s", _PATH_DEFAULTMTA); |
111 |
|
|
/*NOTREACHED*/ |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
for (;;) { |
115 |
|
|
if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { |
116 |
|
|
if (feof(config)) |
117 |
|
|
errx(1, "no mapping in %s", _PATH_MAILERCONF); |
118 |
|
|
err(1, "fparseln"); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
#define WS " \t\n" |
122 |
|
|
cp = line; |
123 |
|
|
|
124 |
|
|
cp += strspn(cp, WS); |
125 |
|
|
if (cp[0] == '\0') { |
126 |
|
|
/* empty line */ |
127 |
|
|
free(line); |
128 |
|
|
continue; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
if ((from = strsep(&cp, WS)) == NULL || cp == NULL) |
132 |
|
|
goto parse_error; |
133 |
|
|
|
134 |
|
|
cp += strspn(cp, WS); |
135 |
|
|
|
136 |
|
|
if ((to = strsep(&cp, WS)) == NULL) |
137 |
|
|
goto parse_error; |
138 |
|
|
|
139 |
|
|
if (strcmp(from, progname) == 0) { |
140 |
|
|
for (ap = strsep(&cp, WS); ap != NULL; |
141 |
|
|
ap = strsep(&cp, WS)) |
142 |
|
|
if (*ap) |
143 |
|
|
addarg(&al, ap); |
144 |
|
|
break; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
free(line); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
(void)fclose(config); |
151 |
|
|
|
152 |
|
|
addarg(&al, NULL); |
153 |
|
|
|
154 |
|
|
execv(to, al.argv); |
155 |
|
|
err(1, "cannot exec %s", to); |
156 |
|
|
/*NOTREACHED*/ |
157 |
|
|
parse_error: |
158 |
|
|
errx(1, "parse error in %s at line %lu", |
159 |
|
|
_PATH_MAILERCONF, (u_long)lineno); |
160 |
|
|
/*NOTREACHED*/ |
161 |
|
|
} |