1 /* $OpenBSD: mailwrapper.c,v 1.9 2001/08/08 00:47:52 jakob Exp $ */ 2 /* $NetBSD: mailwrapper.c,v 1.2 1999/02/20 22:10:07 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1998 6 * Perry E. Metzger. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgment: 18 * This product includes software developed for the NetBSD Project 19 * by Perry E. Metzger. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <err.h> 36 #include <stdio.h> 37 #include <string.h> 38 #include <stdlib.h> 39 #include <syslog.h> 40 #include <unistd.h> 41 #include <util.h> 42 43 #define _PATH_MAILERCONF "/etc/mailer.conf" 44 #define _PATH_DEFAULTMTA "/usr/libexec/sendmail/sendmail" 45 46 struct arglist { 47 size_t argc, maxc; 48 char **argv; 49 }; 50 51 int main __P((int, char *[], char *[])); 52 53 static void initarg __P((struct arglist *)); 54 static void addarg __P((struct arglist *, const char *, int)); 55 static void freearg __P((struct arglist *, int)); 56 57 extern const char *__progname; /* from crt0.o */ 58 59 static void 60 initarg(al) 61 struct arglist *al; 62 { 63 al->argc = 0; 64 al->maxc = 10; 65 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL) 66 err(1, "mailwrapper"); 67 } 68 69 static void 70 addarg(al, arg, copy) 71 struct arglist *al; 72 const char *arg; 73 int copy; 74 { 75 char **argv2; 76 77 if (al->argc == al->maxc) { 78 al->maxc <<= 1; 79 80 if ((argv2 = realloc(al->argv, 81 al->maxc * sizeof(char *))) == NULL) { 82 if (al->argv) 83 free(al->argv); 84 al->argv = NULL; 85 err(1, "mailwrapper"); 86 } else { 87 al->argv = argv2; 88 } 89 } 90 if (copy) { 91 if ((al->argv[al->argc++] = strdup(arg)) == NULL) 92 err(1, "mailwrapper"); 93 } else 94 al->argv[al->argc++] = (char *)arg; 95 } 96 97 static void 98 freearg(al, copy) 99 struct arglist *al; 100 int copy; 101 { 102 size_t i; 103 if (copy) 104 for (i = 0; i < al->argc; i++) 105 free(al->argv[i]); 106 free(al->argv); 107 } 108 109 int 110 main(argc, argv, envp) 111 int argc; 112 char *argv[]; 113 char *envp[]; 114 { 115 FILE *config; 116 char *line, *cp, *from, *to, *ap; 117 size_t len, lineno = 0; 118 struct arglist al; 119 120 initarg(&al); 121 for (len = 0; len < argc; len++) 122 addarg(&al, argv[len], 0); 123 124 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) { 125 addarg(&al, NULL, 0); 126 openlog("mailwrapper", LOG_PID, LOG_MAIL); 127 syslog(LOG_INFO, "can't open %s, using %s as default MTA", 128 _PATH_MAILERCONF, _PATH_DEFAULTMTA); 129 closelog(); 130 execve(_PATH_DEFAULTMTA, al.argv, envp); 131 freearg(&al, 0); 132 err(1, "mailwrapper: execing %s", _PATH_DEFAULTMTA); 133 /*NOTREACHED*/ 134 } 135 136 for (;;) { 137 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { 138 if (feof(config)) 139 errx(1, "mailwrapper: no mapping in %s", 140 _PATH_MAILERCONF); 141 err(1, "mailwrapper"); 142 } 143 144 #define WS " \t\n" 145 cp = line; 146 147 cp += strspn(cp, WS); 148 if (cp[0] == '\0') { 149 /* empty line */ 150 free(line); 151 continue; 152 } 153 154 if ((from = strsep(&cp, WS)) == NULL) 155 goto parse_error; 156 157 cp += strspn(cp, WS); 158 159 if ((to = strsep(&cp, WS)) == NULL) 160 goto parse_error; 161 162 if (strcmp(from, __progname) == 0) { 163 for (ap = strsep(&cp, WS); ap != NULL; 164 ap = strsep(&cp, WS)) 165 if (*ap) 166 addarg(&al, ap, 0); 167 break; 168 } 169 170 free(line); 171 } 172 173 (void)fclose(config); 174 175 addarg(&al, NULL, 0); 176 177 execve(to, al.argv, envp); 178 freearg(&al, 0); 179 free(line); 180 err(1, "mailwrapper: execing %s", to); 181 /*NOTREACHED*/ 182 parse_error: 183 freearg(&al, 0); 184 free(line); 185 errx(1, "mailwrapper: parse error in %s at line %lu", 186 _PATH_MAILERCONF, (u_long)lineno); 187 /*NOTREACHED*/ 188 } 189