Home | History | Annotate | Line # | Download | only in mailwrapper
      1  1.11     sevan /*	$NetBSD: mailwrapper.c,v 1.11 2018/01/23 21:06:25 sevan Exp $	*/
      2   1.1     perry 
      3   1.1     perry /*
      4   1.1     perry  * Copyright (c) 1998
      5   1.1     perry  * 	Perry E. Metzger.  All rights reserved.
      6   1.1     perry  *
      7   1.1     perry  * Redistribution and use in source and binary forms, with or without
      8   1.1     perry  * modification, are permitted provided that the following conditions
      9   1.1     perry  * are met:
     10   1.1     perry  * 1. Redistributions of source code must retain the above copyright
     11   1.1     perry  *    notice, this list of conditions and the following disclaimer.
     12   1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     perry  *    documentation and/or other materials provided with the distribution.
     15   1.1     perry  * 3. All advertising materials mentioning features or use of this software
     16   1.1     perry  *    must display the following acknowledgment:
     17   1.1     perry  *	This product includes software developed for the NetBSD Project
     18   1.1     perry  *	by Perry E. Metzger.
     19   1.1     perry  * 4. The name of the author may not be used to endorse or promote products
     20   1.1     perry  *    derived from this software without specific prior written permission.
     21   1.1     perry  *
     22   1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1     perry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1     perry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1     perry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1     perry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1     perry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1     perry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1     perry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1     perry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1     perry  */
     33   1.1     perry 
     34   1.1     perry #include <err.h>
     35   1.1     perry #include <stdio.h>
     36   1.1     perry #include <string.h>
     37   1.2   thorpej #include <stdlib.h>
     38   1.1     perry #include <unistd.h>
     39   1.1     perry 
     40   1.1     perry #define _PATH_MAILERCONF	"/etc/mailer.conf"
     41   1.1     perry 
     42   1.3  christos struct arglist {
     43   1.3  christos 	size_t argc, maxc;
     44  1.10     lukem 	const char **argv;
     45   1.3  christos };
     46   1.3  christos 
     47  1.11     sevan static void initarg(struct arglist *);
     48  1.11     sevan static void addarg(struct arglist *, const char *, int);
     49   1.3  christos 
     50   1.3  christos static void
     51  1.11     sevan initarg(struct arglist *al)
     52   1.3  christos {
     53   1.3  christos 	al->argc = 0;
     54   1.3  christos 	al->maxc = 10;
     55   1.3  christos 	if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL)
     56   1.9       mjl 		/*
     57   1.9       mjl 		 * This (using err("mailwrapper")) is intentional.
     58   1.9       mjl 		 * Mailwrapper plays ugly games with argv[0] and thus it
     59   1.9       mjl 		 * is often difficult for people to know that the error
     60   1.9       mjl 		 * isn't from "mailq" or "sendmail" but from mailwrapper
     61   1.9       mjl 		 * -- having mailwrapper add an indication that it was really
     62   1.9       mjl 		 * mailwrapper running was a requested feature.
     63   1.9       mjl 		 */
     64   1.9       mjl 		err(1, "mailwrapper");
     65   1.3  christos }
     66   1.3  christos 
     67   1.3  christos static void
     68  1.11     sevan addarg(struct arglist *al, const char *arg, int copy)
     69   1.3  christos {
     70   1.3  christos 	if (al->argc == al->maxc) {
     71   1.3  christos 	    al->maxc <<= 1;
     72   1.3  christos 	    if ((al->argv = realloc(al->argv,
     73   1.3  christos 		al->maxc * sizeof(char *))) == NULL)
     74   1.9       mjl 		    err(1, "mailwrapper");
     75   1.3  christos 	}
     76   1.3  christos 	if (copy) {
     77   1.3  christos 		if ((al->argv[al->argc++] = strdup(arg)) == NULL)
     78   1.9       mjl 			err(1, "mailwrapper:");
     79   1.3  christos 	} else
     80  1.10     lukem 		al->argv[al->argc++] = arg;
     81   1.3  christos }
     82   1.3  christos 
     83   1.1     perry int
     84  1.11     sevan main(int argc, char *argv[], char *envp[])
     85   1.1     perry {
     86   1.1     perry 	FILE *config;
     87   1.3  christos 	char *line, *cp, *from, *to, *ap;
     88   1.2   thorpej 	size_t len, lineno = 0;
     89   1.7        is 	int i;
     90   1.3  christos 	struct arglist al;
     91   1.3  christos 
     92   1.3  christos 	initarg(&al);
     93   1.7        is 	addarg(&al, argv[0], 0);
     94   1.2   thorpej 
     95   1.1     perry 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
     96   1.9       mjl 		err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF);
     97   1.1     perry 
     98   1.2   thorpej 	for (;;) {
     99   1.2   thorpej 		if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) {
    100   1.2   thorpej 			if (feof(config))
    101   1.9       mjl 				errx(1, "mailwrapper: no mapping in %s",
    102   1.9       mjl 				    _PATH_MAILERCONF);
    103   1.9       mjl 			err(1, "mailwrapper");
    104   1.2   thorpej 		}
    105   1.2   thorpej 
    106   1.3  christos #define	WS	" \t\n"
    107   1.2   thorpej 		cp = line;
    108   1.2   thorpej 
    109   1.3  christos 		cp += strspn(cp, WS);
    110   1.2   thorpej 		if (cp[0] == '\0') {
    111   1.2   thorpej 			/* empty line */
    112   1.2   thorpej 			free(line);
    113   1.1     perry 			continue;
    114   1.2   thorpej 		}
    115   1.2   thorpej 
    116   1.3  christos 		if ((from = strsep(&cp, WS)) == NULL)
    117   1.3  christos 			goto parse_error;
    118   1.2   thorpej 
    119   1.3  christos 		cp += strspn(cp, WS);
    120   1.1     perry 
    121   1.3  christos 		if ((to = strsep(&cp, WS)) == NULL)
    122   1.3  christos 			goto parse_error;
    123   1.2   thorpej 
    124   1.5       cgd 		if (strcmp(from, getprogname()) == 0) {
    125   1.3  christos 			for (ap = strsep(&cp, WS); ap != NULL;
    126   1.3  christos 			    ap = strsep(&cp, WS))
    127   1.3  christos 			    if (*ap)
    128   1.3  christos 				    addarg(&al, ap, 0);
    129   1.3  christos 			break;
    130   1.2   thorpej 		}
    131   1.1     perry 
    132   1.2   thorpej 		free(line);
    133   1.1     perry 	}
    134   1.1     perry 
    135   1.3  christos 	(void)fclose(config);
    136   1.7        is 
    137   1.7        is 	for (i = 1; i < argc; i++)
    138   1.7        is 		addarg(&al, argv[i], 0);
    139   1.2   thorpej 
    140   1.4   msaitoh 	addarg(&al, NULL, 0);
    141  1.10     lukem 	execve(to, __UNCONST(al.argv), envp);
    142   1.9       mjl 	err(1, "mailwrapper: execing %s", to);
    143   1.3  christos 	/*NOTREACHED*/
    144   1.3  christos parse_error:
    145   1.9       mjl 	errx(1, "mailwrapper: parse error in %s at line %lu",
    146   1.3  christos 	    _PATH_MAILERCONF, (u_long)lineno);
    147   1.3  christos 	/*NOTREACHED*/
    148   1.1     perry }
    149