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