1 /* $OpenBSD: mailwrapper.c,v 1.16 2004/07/06 03:38:14 millert Exp $ */ 2 /* $NetBSD: mailwrapper.c,v 1.9 2003/03/09 08:10:43 mjl Exp $ */ 3 /* $FreeBSD: src/usr.sbin/mailwrapper/mailwrapper.c,v 1.9 2003/07/06 12:44:11 charnier Exp $ */ 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 <syslog.h> 42 #include <stdarg.h> 43 44 #include "pathnames.h" 45 46 struct arglist { 47 size_t argc, maxc; 48 char **argv; 49 }; 50 51 static void initarg(struct arglist *); 52 static void addarg(struct arglist *, char *, int); 53 54 static void 55 initarg(struct arglist *al) 56 { 57 al->argc = 0; 58 al->maxc = 10; 59 if ((al->argv = malloc(al->maxc * sizeof(char *))) == NULL) 60 err(1, NULL); 61 } 62 63 static void 64 addarg(struct arglist *al, char *arg, int copy) 65 { 66 if (al->argc == al->maxc) { 67 al->maxc <<= 1; 68 al->argv = realloc(al->argv, al->maxc * sizeof(char *)); 69 if (al->argv == NULL) 70 err(1, NULL); 71 } 72 if (copy) { 73 if ((al->argv[al->argc++] = strdup(arg)) == NULL) 74 err(1, NULL); 75 } else 76 al->argv[al->argc++] = arg; 77 } 78 79 int 80 main(int argc, char **argv, char **envp) 81 { 82 FILE *config; 83 char *line, *cp, *from, *to, *ap; 84 const char *progname; 85 size_t len, lineno = 0; 86 int i; 87 struct arglist al; 88 89 /* change progname to mailwrapper so we get sensible error messages */ 90 progname = getprogname(); 91 setprogname("mailwrapper"); 92 93 if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) { 94 openlog("mailwrapper", LOG_PID, LOG_MAIL); 95 syslog(LOG_INFO, "can't open %s, using %s as default MTA", 96 _PATH_MAILERCONF, _PATH_DEFAULTMTA); 97 closelog(); 98 execve(_PATH_DEFAULTMTA, argv, envp); 99 err(1, "cannot exec %s", _PATH_DEFAULTMTA); 100 /*NOTREACHED*/ 101 } 102 103 initarg(&al); 104 addarg(&al, argv[0], 0); 105 106 for (;;) { 107 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { 108 if (feof(config)) 109 errx(1, "no mapping in %s", _PATH_MAILERCONF); 110 err(1, NULL); 111 } 112 113 #define WS " \t\n" 114 cp = line; 115 116 cp += strspn(cp, WS); 117 if (cp[0] == '\0') { 118 /* empty line */ 119 free(line); 120 continue; 121 } 122 123 if ((from = strsep(&cp, WS)) == NULL) 124 goto parse_error; 125 126 cp += strspn(cp, WS); 127 128 if ((to = strsep(&cp, WS)) == NULL) 129 goto parse_error; 130 131 if (strcmp(from, progname) == 0) { 132 for (ap = strsep(&cp, WS); ap != NULL; 133 ap = strsep(&cp, WS)) 134 if (*ap) 135 addarg(&al, ap, 0); 136 break; 137 } 138 139 free(line); 140 } 141 142 fclose(config); 143 144 for (i = 1; i < argc; i++) 145 addarg(&al, argv[i], 0); 146 addarg(&al, NULL, 0); 147 148 execve(to, al.argv, envp); 149 err(1, "cannot exec %s", to); 150 /*NOTREACHED*/ 151 parse_error: 152 errx(1, "parse error in %s at line %lu", 153 _PATH_MAILERCONF, (u_long)lineno); 154 /*NOTREACHED*/ 155 } 156