Home | History | Annotate | Line # | Download | only in mailwrapper
mailwrapper.c revision 1.5
      1 /*	$NetBSD: mailwrapper.c,v 1.5 2001/02/19 23:22:44 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998
      5  * 	Perry E. Metzger.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     21  *
     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.
     32  */
     33 
     34 #include <err.h>
     35 #include <stdio.h>
     36 #include <string.h>
     37 #include <stdlib.h>
     38 #include <unistd.h>
     39 #include <util.h>
     40 
     41 #define _PATH_MAILERCONF	"/etc/mailer.conf"
     42 
     43 struct arglist {
     44 	size_t argc, maxc;
     45 	char **argv;
     46 };
     47 
     48 int main __P((int, char *[], char *[]));
     49 
     50 static void initarg __P((struct arglist *));
     51 static void addarg __P((struct arglist *, const char *, int));
     52 
     53 static void
     54 initarg(al)
     55 	struct arglist *al;
     56 {
     57 	al->argc = 0;
     58 	al->maxc = 10;
     59 	if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
     60 		err(1, "mailwrapper");
     61 }
     62 
     63 static void
     64 addarg(al, arg, copy)
     65 	struct arglist *al;
     66 	const char *arg;
     67 	int copy;
     68 {
     69 	if (al->argc == al->maxc) {
     70 	    al->maxc <<= 1;
     71 	    if ((al->argv = realloc(al->argv,
     72 		al->maxc * sizeof(char *))) == NULL)
     73 		    err(1, "mailwrapper");
     74 	}
     75 	if (copy) {
     76 		if ((al->argv[al->argc++] = strdup(arg)) == NULL)
     77 			err(1, "mailwrapper:");
     78 	} else
     79 		al->argv[al->argc++] = (char *)arg;
     80 }
     81 
     82 int
     83 main(argc, argv, envp)
     84 	int argc;
     85 	char *argv[];
     86 	char *envp[];
     87 {
     88 	FILE *config;
     89 	char *line, *cp, *from, *to, *ap;
     90 	size_t len, lineno = 0;
     91 	struct arglist al;
     92 
     93 	initarg(&al);
     94 	for (len = 0; len < argc; len++)
     95 		addarg(&al, argv[len], 0);
     96 
     97 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
     98 		err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF);
     99 
    100 	for (;;) {
    101 		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
    102 			if (feof(config))
    103 				errx(1, "mailwrapper: no mapping in %s",
    104 				    _PATH_MAILERCONF);
    105 			err(1, "mailwrapper");
    106 		}
    107 
    108 #define	WS	" \t\n"
    109 		cp = line;
    110 
    111 		cp += strspn(cp, WS);
    112 		if (cp[0] == '\0') {
    113 			/* empty line */
    114 			free(line);
    115 			continue;
    116 		}
    117 
    118 		if ((from = strsep(&cp, WS)) == NULL)
    119 			goto parse_error;
    120 
    121 		cp += strspn(cp, WS);
    122 
    123 		if ((to = strsep(&cp, WS)) == NULL)
    124 			goto parse_error;
    125 
    126 		if (strcmp(from, getprogname()) == 0) {
    127 			for (ap = strsep(&cp, WS); ap != NULL;
    128 			    ap = strsep(&cp, WS))
    129 			    if (*ap)
    130 				    addarg(&al, ap, 0);
    131 			break;
    132 		}
    133 
    134 		free(line);
    135 	}
    136 
    137 	(void)fclose(config);
    138 
    139 	addarg(&al, NULL, 0);
    140 	execve(to, al.argv, envp);
    141 	err(1, "mailwrapper: execing %s", to);
    142 	/*NOTREACHED*/
    143 parse_error:
    144 	errx(1, "mailwrapper: parse error in %s at line %lu",
    145 	    _PATH_MAILERCONF, (u_long)lineno);
    146 	/*NOTREACHED*/
    147 }
    148