1 /* $NetBSD: mailwrapper.c,v 1.4 2000/11/16 08:33:33 msaitoh 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 extern const char *__progname; /* from crt0.o */ 54 55 static void 56 initarg(al) 57 struct arglist *al; 58 { 59 al->argc = 0; 60 al->maxc = 10; 61 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL) 62 err(1, "mailwrapper"); 63 } 64 65 static void 66 addarg(al, arg, copy) 67 struct arglist *al; 68 const char *arg; 69 int copy; 70 { 71 if (al->argc == al->maxc) { 72 al->maxc <<= 1; 73 if ((al->argv = realloc(al->argv, 74 al->maxc * sizeof(char *))) == NULL) 75 err(1, "mailwrapper"); 76 } 77 if (copy) { 78 if ((al->argv[al->argc++] = strdup(arg)) == NULL) 79 err(1, "mailwrapper:"); 80 } else 81 al->argv[al->argc++] = (char *)arg; 82 } 83 84 int 85 main(argc, argv, envp) 86 int argc; 87 char *argv[]; 88 char *envp[]; 89 { 90 FILE *config; 91 char *line, *cp, *from, *to, *ap; 92 size_t len, lineno = 0; 93 struct arglist al; 94 95 initarg(&al); 96 for (len = 0; len < argc; len++) 97 addarg(&al, argv[len], 0); 98 99 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) 100 err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF); 101 102 for (;;) { 103 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { 104 if (feof(config)) 105 errx(1, "mailwrapper: no mapping in %s", 106 _PATH_MAILERCONF); 107 err(1, "mailwrapper"); 108 } 109 110 #define WS " \t\n" 111 cp = line; 112 113 cp += strspn(cp, WS); 114 if (cp[0] == '\0') { 115 /* empty line */ 116 free(line); 117 continue; 118 } 119 120 if ((from = strsep(&cp, WS)) == NULL) 121 goto parse_error; 122 123 cp += strspn(cp, WS); 124 125 if ((to = strsep(&cp, WS)) == NULL) 126 goto parse_error; 127 128 if (strcmp(from, __progname) == 0) { 129 for (ap = strsep(&cp, WS); ap != NULL; 130 ap = strsep(&cp, WS)) 131 if (*ap) 132 addarg(&al, ap, 0); 133 break; 134 } 135 136 free(line); 137 } 138 139 (void)fclose(config); 140 141 addarg(&al, NULL, 0); 142 execve(to, al.argv, envp); 143 err(1, "mailwrapper: execing %s", to); 144 /*NOTREACHED*/ 145 parse_error: 146 errx(1, "mailwrapper: parse error in %s at line %lu", 147 _PATH_MAILERCONF, (u_long)lineno); 148 /*NOTREACHED*/ 149 } 150