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