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 $ */
6 * Perry E. Metzger. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
45 #include "pathnames.h"
52 int main(int, char *[], char *[]);
54 static void initarg(struct arglist
*);
55 static void addarg(struct arglist
*, const char *);
58 initarg(struct arglist
*al
)
62 if ((al
->argv
= calloc(al
->maxc
, sizeof (char *))) == NULL
)
63 err(EX_TEMPFAIL
, "calloc");
67 addarg(struct arglist
*al
, const char *arg
)
70 if (al
->argc
== al
->maxc
) {
72 al
->argv
= reallocarray(al
->argv
, al
->maxc
, sizeof (char *));
74 err(EX_TEMPFAIL
, "realloc");
77 al
->argv
[al
->argc
++] = NULL
;
78 else if ((al
->argv
[al
->argc
++] = strdup(arg
)) == NULL
)
79 err(EX_TEMPFAIL
, "strdup");
83 main(int argc
, char *argv
[], char *envp
[])
86 char *line
, *cp
, *from
, *to
, *ap
;
87 char progname
[PATH_MAX
+1];
89 size_t len
, lineno
= 0;
93 /* change __progname to mailwrapper so we get sensible error messages */
96 strncpy(progname
, name
, sizeof (progname
));
98 err(EX_OSERR
, "cannot get program name");
100 setprogname("mailwrapper");
103 addarg(&al
, argv
[0]);
105 if ((config
= fopen(_PATH_MAILERCONF
, "r")) == NULL
) {
106 for (i
= 1; i
< argc
; i
++)
107 addarg(&al
, argv
[i
]);
109 openlog(getprogname(), LOG_PID
, LOG_MAIL
);
110 syslog(LOG_INFO
, "cannot open %s, using %s as default MTA",
111 _PATH_MAILERCONF
, _PATH_DEFAULTMTA
);
113 execve(_PATH_DEFAULTMTA
, al
.argv
, envp
);
114 err(EX_OSERR
, "cannot exec %s", _PATH_DEFAULTMTA
);
119 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
) {
121 errx(EX_CONFIG
, "no mapping for %s in %s",
122 progname
, _PATH_MAILERCONF
);
123 err(EX_CONFIG
, "cannot parse line %lu",
130 cp
+= strspn(cp
, WS
);
137 if ((from
= strsep(&cp
, WS
)) == NULL
|| cp
== NULL
)
140 cp
+= strspn(cp
, WS
);
142 if ((to
= strsep(&cp
, WS
)) == NULL
)
145 if (strcmp(from
, progname
) == 0) {
146 for (ap
= strsep(&cp
, WS
); ap
!= NULL
;
147 ap
= strsep(&cp
, WS
)) {
157 (void) fclose(config
);
159 for (i
= 1; i
< argc
; i
++)
160 addarg(&al
, argv
[i
]);
163 execve(to
, al
.argv
, envp
);
164 err(EX_OSERR
, "cannot exec %s", to
);
167 errx(EX_CONFIG
, "parse error in %s at line %lu",
168 _PATH_MAILERCONF
, (ulong_t
)lineno
);