2009-04-16 11:13:16 +04:00
|
|
|
/* $NetBSD: mailwrapper.c,v 1.10 2009/04/16 07:13:16 lukem Exp $ */
|
1998-12-26 01:06:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998
|
|
|
|
* Perry E. Metzger. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgment:
|
|
|
|
* This product includes software developed for the NetBSD Project
|
|
|
|
* by Perry E. Metzger.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1999-02-21 01:10:07 +03:00
|
|
|
#include <stdlib.h>
|
1998-12-26 01:06:58 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define _PATH_MAILERCONF "/etc/mailer.conf"
|
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
struct arglist {
|
|
|
|
size_t argc, maxc;
|
2009-04-16 11:13:16 +04:00
|
|
|
const char **argv;
|
1999-05-29 22:18:15 +04:00
|
|
|
};
|
|
|
|
|
1998-12-26 01:06:58 +03:00
|
|
|
int main __P((int, char *[], char *[]));
|
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
static void initarg __P((struct arglist *));
|
|
|
|
static void addarg __P((struct arglist *, const char *, int));
|
|
|
|
|
|
|
|
static void
|
|
|
|
initarg(al)
|
|
|
|
struct arglist *al;
|
|
|
|
{
|
|
|
|
al->argc = 0;
|
|
|
|
al->maxc = 10;
|
|
|
|
if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
|
2003-03-09 11:10:43 +03:00
|
|
|
/*
|
|
|
|
* This (using err("mailwrapper")) is intentional.
|
|
|
|
* Mailwrapper plays ugly games with argv[0] and thus it
|
|
|
|
* is often difficult for people to know that the error
|
|
|
|
* isn't from "mailq" or "sendmail" but from mailwrapper
|
|
|
|
* -- having mailwrapper add an indication that it was really
|
|
|
|
* mailwrapper running was a requested feature.
|
|
|
|
*/
|
|
|
|
err(1, "mailwrapper");
|
1999-05-29 22:18:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addarg(al, arg, copy)
|
|
|
|
struct arglist *al;
|
|
|
|
const char *arg;
|
|
|
|
int copy;
|
|
|
|
{
|
|
|
|
if (al->argc == al->maxc) {
|
|
|
|
al->maxc <<= 1;
|
|
|
|
if ((al->argv = realloc(al->argv,
|
|
|
|
al->maxc * sizeof(char *))) == NULL)
|
2003-03-09 11:10:43 +03:00
|
|
|
err(1, "mailwrapper");
|
1999-05-29 22:18:15 +04:00
|
|
|
}
|
|
|
|
if (copy) {
|
|
|
|
if ((al->argv[al->argc++] = strdup(arg)) == NULL)
|
2003-03-09 11:10:43 +03:00
|
|
|
err(1, "mailwrapper:");
|
1999-05-29 22:18:15 +04:00
|
|
|
} else
|
2009-04-16 11:13:16 +04:00
|
|
|
al->argv[al->argc++] = arg;
|
1999-05-29 22:18:15 +04:00
|
|
|
}
|
|
|
|
|
1998-12-26 01:06:58 +03:00
|
|
|
int
|
|
|
|
main(argc, argv, envp)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
char *envp[];
|
|
|
|
{
|
|
|
|
FILE *config;
|
1999-05-29 22:18:15 +04:00
|
|
|
char *line, *cp, *from, *to, *ap;
|
1999-02-21 01:10:07 +03:00
|
|
|
size_t len, lineno = 0;
|
2003-02-18 00:18:46 +03:00
|
|
|
int i;
|
1999-05-29 22:18:15 +04:00
|
|
|
struct arglist al;
|
|
|
|
|
|
|
|
initarg(&al);
|
2003-02-18 00:18:46 +03:00
|
|
|
addarg(&al, argv[0], 0);
|
1999-02-21 01:10:07 +03:00
|
|
|
|
1998-12-26 01:06:58 +03:00
|
|
|
if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
|
2003-03-09 11:10:43 +03:00
|
|
|
err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF);
|
1999-02-21 01:10:07 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
|
|
|
|
if (feof(config))
|
2003-03-09 11:10:43 +03:00
|
|
|
errx(1, "mailwrapper: no mapping in %s",
|
|
|
|
_PATH_MAILERCONF);
|
|
|
|
err(1, "mailwrapper");
|
1999-02-21 01:10:07 +03:00
|
|
|
}
|
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
#define WS " \t\n"
|
1999-02-21 01:10:07 +03:00
|
|
|
cp = line;
|
1998-12-26 01:06:58 +03:00
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
cp += strspn(cp, WS);
|
1999-02-21 01:10:07 +03:00
|
|
|
if (cp[0] == '\0') {
|
|
|
|
/* empty line */
|
|
|
|
free(line);
|
1998-12-26 01:06:58 +03:00
|
|
|
continue;
|
1999-02-21 01:10:07 +03:00
|
|
|
}
|
1998-12-26 01:06:58 +03:00
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
if ((from = strsep(&cp, WS)) == NULL)
|
|
|
|
goto parse_error;
|
1999-02-21 01:10:07 +03:00
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
cp += strspn(cp, WS);
|
1999-02-21 01:10:07 +03:00
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
if ((to = strsep(&cp, WS)) == NULL)
|
|
|
|
goto parse_error;
|
1999-02-21 01:10:07 +03:00
|
|
|
|
2001-02-20 02:22:40 +03:00
|
|
|
if (strcmp(from, getprogname()) == 0) {
|
1999-05-29 22:18:15 +04:00
|
|
|
for (ap = strsep(&cp, WS); ap != NULL;
|
|
|
|
ap = strsep(&cp, WS))
|
|
|
|
if (*ap)
|
|
|
|
addarg(&al, ap, 0);
|
1998-12-26 01:06:58 +03:00
|
|
|
break;
|
1999-05-29 22:18:15 +04:00
|
|
|
}
|
1999-02-21 01:10:07 +03:00
|
|
|
|
|
|
|
free(line);
|
1998-12-26 01:06:58 +03:00
|
|
|
}
|
|
|
|
|
1999-05-29 22:18:15 +04:00
|
|
|
(void)fclose(config);
|
1999-02-21 01:10:07 +03:00
|
|
|
|
2003-02-18 00:18:46 +03:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
addarg(&al, argv[i], 0);
|
|
|
|
|
2000-11-16 11:33:33 +03:00
|
|
|
addarg(&al, NULL, 0);
|
2009-04-16 11:13:16 +04:00
|
|
|
execve(to, __UNCONST(al.argv), envp);
|
2003-03-09 11:10:43 +03:00
|
|
|
err(1, "mailwrapper: execing %s", to);
|
1999-05-29 22:18:15 +04:00
|
|
|
/*NOTREACHED*/
|
|
|
|
parse_error:
|
2003-03-09 11:10:43 +03:00
|
|
|
errx(1, "mailwrapper: parse error in %s at line %lu",
|
1999-05-29 22:18:15 +04:00
|
|
|
_PATH_MAILERCONF, (u_long)lineno);
|
|
|
|
/*NOTREACHED*/
|
1998-12-26 01:06:58 +03:00
|
|
|
}
|