1 /* $OpenBSD: mailwrapper.c,v 1.18 2007/11/06 14:39:19 otto Exp $ */ 2 /* $NetBSD: mailwrapper.c,v 1.9 2003/03/09 08:10:43 mjl 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 <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 40 #include <err.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <stdlib.h> 45 #include <libutil.h> 46 #include <sysexits.h> 47 #include <syslog.h> 48 49 #include "pathnames.h" 50 51 struct arglist { 52 size_t argc, maxc; 53 char **argv; 54 }; 55 56 int main(int, char *[], char *[]); 57 58 static void initarg(struct arglist *); 59 static void addarg(struct arglist *, const char *); 60 61 static void 62 initarg(struct arglist *al) 63 { 64 al->argc = 0; 65 al->maxc = 10; 66 if ((al->argv = calloc(al->maxc, sizeof(char *))) == NULL) 67 err(EX_TEMPFAIL, "calloc"); 68 } 69 70 static void 71 addarg(struct arglist *al, const char *arg) 72 { 73 74 if (al->argc == al->maxc) { 75 al->maxc <<= 1; 76 al->argv = realloc(al->argv, al->maxc * sizeof(char *)); 77 if (al->argv == NULL) 78 err(EX_TEMPFAIL, "realloc"); 79 } 80 if (arg == NULL) 81 al->argv[al->argc++] = NULL; 82 else if ((al->argv[al->argc++] = strdup(arg)) == NULL) 83 err(EX_TEMPFAIL, "strdup"); 84 } 85 86 int 87 main(int argc, char *argv[], char *envp[]) 88 { 89 FILE *config; 90 char *line, *cp, *from, *to, *ap; 91 const char *progname; 92 char localmailerconf[MAXPATHLEN]; 93 const char *mailerconf; 94 size_t len, lineno = 0; 95 int i; 96 struct arglist al; 97 98 /* change __progname to mailwrapper so we get sensible error messages */ 99 progname = getprogname(); 100 setprogname("mailwrapper"); 101 102 initarg(&al); 103 addarg(&al, argv[0]); 104 105 snprintf(localmailerconf, MAXPATHLEN, "%s/etc/mail/mailer.conf", 106 getenv("LOCALBASE") ? getenv("LOCALBASE") : "/usr/local"); 107 108 mailerconf = localmailerconf; 109 if ((config = fopen(localmailerconf, "r")) == NULL) 110 mailerconf = _PATH_MAILERCONF; 111 112 if (config == NULL && ((config = fopen(mailerconf, "r")) == NULL)) { 113 addarg(&al, NULL); 114 openlog(getprogname(), LOG_PID, LOG_MAIL); 115 syslog(LOG_INFO, "cannot open %s, using %s as default MTA", 116 mailerconf, _PATH_DEFAULTMTA); 117 closelog(); 118 execve(_PATH_DEFAULTMTA, al.argv, envp); 119 err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA); 120 /*NOTREACHED*/ 121 } 122 123 for (;;) { 124 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { 125 if (feof(config)) 126 errx(EX_CONFIG, "no mapping in %s", mailerconf); 127 err(EX_CONFIG, "cannot parse line %lu", (u_long)lineno); 128 } 129 130 #define WS " \t\n" 131 cp = line; 132 133 cp += strspn(cp, WS); 134 if (cp[0] == '\0') { 135 /* empty line */ 136 free(line); 137 continue; 138 } 139 140 if ((from = strsep(&cp, WS)) == NULL || cp == NULL) 141 goto parse_error; 142 143 cp += strspn(cp, WS); 144 145 if ((to = strsep(&cp, WS)) == NULL) 146 goto parse_error; 147 148 if (strcmp(from, progname) == 0) { 149 for (ap = strsep(&cp, WS); ap != NULL; 150 ap = strsep(&cp, WS)) { 151 if (*ap) 152 addarg(&al, ap); 153 } 154 break; 155 } 156 157 free(line); 158 } 159 160 (void)fclose(config); 161 162 for (i = 1; i < argc; i++) 163 addarg(&al, argv[i]); 164 165 addarg(&al, NULL); 166 execve(to, al.argv, envp); 167 err(EX_OSERR, "cannot exec %s", to); 168 /*NOTREACHED*/ 169 parse_error: 170 errx(EX_CONFIG, "parse error in %s at line %lu", 171 mailerconf, (u_long)lineno); 172 /*NOTREACHED*/ 173 } 174