1 /* $NetBSD: mailwrapper.c,v 1.10 2009/04/16 07:13:16 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 const 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 /* 60 * This (using err("mailwrapper")) is intentional. 61 * Mailwrapper plays ugly games with argv[0] and thus it 62 * is often difficult for people to know that the error 63 * isn't from "mailq" or "sendmail" but from mailwrapper 64 * -- having mailwrapper add an indication that it was really 65 * mailwrapper running was a requested feature. 66 */ 67 err(1, "mailwrapper"); 68 } 69 70 static void 71 addarg(al, arg, copy) 72 struct arglist *al; 73 const char *arg; 74 int copy; 75 { 76 if (al->argc == al->maxc) { 77 al->maxc <<= 1; 78 if ((al->argv = realloc(al->argv, 79 al->maxc * sizeof(char *))) == NULL) 80 err(1, "mailwrapper"); 81 } 82 if (copy) { 83 if ((al->argv[al->argc++] = strdup(arg)) == NULL) 84 err(1, "mailwrapper:"); 85 } else 86 al->argv[al->argc++] = arg; 87 } 88 89 int 90 main(argc, argv, envp) 91 int argc; 92 char *argv[]; 93 char *envp[]; 94 { 95 FILE *config; 96 char *line, *cp, *from, *to, *ap; 97 size_t len, lineno = 0; 98 int i; 99 struct arglist al; 100 101 initarg(&al); 102 addarg(&al, argv[0], 0); 103 104 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) 105 err(1, "mailwrapper: can't open %s", _PATH_MAILERCONF); 106 107 for (;;) { 108 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { 109 if (feof(config)) 110 errx(1, "mailwrapper: no mapping in %s", 111 _PATH_MAILERCONF); 112 err(1, "mailwrapper"); 113 } 114 115 #define WS " \t\n" 116 cp = line; 117 118 cp += strspn(cp, WS); 119 if (cp[0] == '\0') { 120 /* empty line */ 121 free(line); 122 continue; 123 } 124 125 if ((from = strsep(&cp, WS)) == NULL) 126 goto parse_error; 127 128 cp += strspn(cp, WS); 129 130 if ((to = strsep(&cp, WS)) == NULL) 131 goto parse_error; 132 133 if (strcmp(from, getprogname()) == 0) { 134 for (ap = strsep(&cp, WS); ap != NULL; 135 ap = strsep(&cp, WS)) 136 if (*ap) 137 addarg(&al, ap, 0); 138 break; 139 } 140 141 free(line); 142 } 143 144 (void)fclose(config); 145 146 for (i = 1; i < argc; i++) 147 addarg(&al, argv[i], 0); 148 149 addarg(&al, NULL, 0); 150 execve(to, __UNCONST(al.argv), envp); 151 err(1, "mailwrapper: execing %s", to); 152 /*NOTREACHED*/ 153 parse_error: 154 errx(1, "mailwrapper: parse error in %s at line %lu", 155 _PATH_MAILERCONF, (u_long)lineno); 156 /*NOTREACHED*/ 157 } 158