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