1 /* $NetBSD: mailwrapper.c,v 1.9 2003/03/09 08:10:43 mjl Exp $ */
5 * Perry E. Metzger. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed for the NetBSD Project
18 * by Perry E. Metzger.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #define _PATH_MAILERCONF "/etc/mailer.conf"
47 int main
__P((int, char *[], char *[]));
49 static void initarg
__P((struct arglist
*));
50 static void addarg
__P((struct arglist
*, const char *, int));
58 if ((al
->argv
= malloc(al
->maxc
* sizeof(char *))) == NULL
)
60 * This (using err("mailwrapper")) is intentional.
61 * Mailwrapper plays ugly games with argv[0] and thus it
62 * is often difficult for people to know that the error
63 * isn't from "mailq" or "sendmail" but from mailwrapper
64 * -- having mailwrapper add an indication that it was really
65 * mailwrapper running was a requested feature.
67 err(1, "mailwrapper");
76 if (al
->argc
== al
->maxc
) {
78 if ((al
->argv
= realloc(al
->argv
,
79 al
->maxc
* sizeof(char *))) == NULL
)
80 err(1, "mailwrapper");
83 if ((al
->argv
[al
->argc
++] = strdup(arg
)) == NULL
)
84 err(1, "mailwrapper:");
86 al
->argv
[al
->argc
++] = arg
;
90 main(argc
, argv
, envp
)
96 char *line
, *cp
, *from
, *to
, *ap
;
97 size_t len
, lineno
= 0;
102 addarg(&al
, argv
[0], 0);
104 if ((config
= fopen(_PATH_MAILERCONF
, "r")) == NULL
)
105 err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF
);
108 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
) {
110 errx(1, "mailwrapper: no mapping in %s",
112 err(1, "mailwrapper");
118 cp
+= strspn(cp
, WS
);
125 if ((from
= strsep(&cp
, WS
)) == NULL
)
128 cp
+= strspn(cp
, WS
);
130 if ((to
= strsep(&cp
, WS
)) == NULL
)
133 if (strcmp(from
, getprogname()) == 0) {
134 for (ap
= strsep(&cp
, WS
); ap
!= NULL
;
135 ap
= strsep(&cp
, WS
))
144 (void)fclose(config
);
146 for (i
= 1; i
< argc
; i
++)
147 addarg(&al
, argv
[i
], 0);
149 addarg(&al
, NULL
, 0);
150 execve(to
, __UNCONST(al
.argv
), envp
);
151 err(1, "mailwrapper: execing %s", to
);
154 errx(1, "mailwrapper: parse error in %s at line %lu",
155 _PATH_MAILERCONF
, (u_long
)lineno
);