1287247a8SAlexander Pyhalov /* $OpenBSD: mailwrapper.c,v 1.18 2007/11/06 14:39:19 otto Exp $ */ 2287247a8SAlexander Pyhalov /* $NetBSD: mailwrapper.c,v 1.9 2003/03/09 08:10:43 mjl Exp $ */ 3287247a8SAlexander Pyhalov 4287247a8SAlexander Pyhalov /* 5287247a8SAlexander Pyhalov * Copyright (c) 1998 6287247a8SAlexander Pyhalov * Perry E. Metzger. All rights reserved. 7287247a8SAlexander Pyhalov * 8287247a8SAlexander Pyhalov * Redistribution and use in source and binary forms, with or without 9287247a8SAlexander Pyhalov * modification, are permitted provided that the following conditions 10287247a8SAlexander Pyhalov * are met: 11287247a8SAlexander Pyhalov * 1. Redistributions of source code must retain the above copyright 12287247a8SAlexander Pyhalov * notice, this list of conditions and the following disclaimer. 13287247a8SAlexander Pyhalov * 2. Redistributions in binary form must reproduce the above copyright 14287247a8SAlexander Pyhalov * notice, this list of conditions and the following disclaimer in the 15287247a8SAlexander Pyhalov * documentation and/or other materials provided with the distribution. 16287247a8SAlexander Pyhalov * 3. All advertising materials mentioning features or use of this software 17287247a8SAlexander Pyhalov * must display the following acknowledgment: 18287247a8SAlexander Pyhalov * This product includes software developed for the NetBSD Project 19287247a8SAlexander Pyhalov * by Perry E. Metzger. 20287247a8SAlexander Pyhalov * 4. The name of the author may not be used to endorse or promote products 21287247a8SAlexander Pyhalov * derived from this software without specific prior written permission. 22287247a8SAlexander Pyhalov * 23287247a8SAlexander Pyhalov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24287247a8SAlexander Pyhalov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25287247a8SAlexander Pyhalov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26287247a8SAlexander Pyhalov * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27287247a8SAlexander Pyhalov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28287247a8SAlexander Pyhalov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29287247a8SAlexander Pyhalov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30287247a8SAlexander Pyhalov * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31287247a8SAlexander Pyhalov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32287247a8SAlexander Pyhalov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33287247a8SAlexander Pyhalov */ 34287247a8SAlexander Pyhalov 35287247a8SAlexander Pyhalov #include <compat.h> 36287247a8SAlexander Pyhalov #include <err.h> 37287247a8SAlexander Pyhalov #include <stdio.h> 38287247a8SAlexander Pyhalov #include <string.h> 39287247a8SAlexander Pyhalov #include <unistd.h> 40287247a8SAlexander Pyhalov #include <stdlib.h> 41287247a8SAlexander Pyhalov #include <sysexits.h> 42287247a8SAlexander Pyhalov #include <syslog.h> 43287247a8SAlexander Pyhalov #include <limits.h> 44287247a8SAlexander Pyhalov 45287247a8SAlexander Pyhalov #include "pathnames.h" 46287247a8SAlexander Pyhalov 47287247a8SAlexander Pyhalov struct arglist { 48287247a8SAlexander Pyhalov size_t argc, maxc; 49287247a8SAlexander Pyhalov char **argv; 50287247a8SAlexander Pyhalov }; 51287247a8SAlexander Pyhalov 52287247a8SAlexander Pyhalov int main(int, char *[], char *[]); 53287247a8SAlexander Pyhalov 54287247a8SAlexander Pyhalov static void initarg(struct arglist *); 55287247a8SAlexander Pyhalov static void addarg(struct arglist *, const char *); 56287247a8SAlexander Pyhalov 57287247a8SAlexander Pyhalov static void 58287247a8SAlexander Pyhalov initarg(struct arglist *al) 59287247a8SAlexander Pyhalov { 60287247a8SAlexander Pyhalov al->argc = 0; 61287247a8SAlexander Pyhalov al->maxc = 10; 62287247a8SAlexander Pyhalov if ((al->argv = calloc(al->maxc, sizeof (char *))) == NULL) 63287247a8SAlexander Pyhalov err(EX_TEMPFAIL, "calloc"); 64287247a8SAlexander Pyhalov } 65287247a8SAlexander Pyhalov 66287247a8SAlexander Pyhalov static void 67287247a8SAlexander Pyhalov addarg(struct arglist *al, const char *arg) 68287247a8SAlexander Pyhalov { 69287247a8SAlexander Pyhalov 70287247a8SAlexander Pyhalov if (al->argc == al->maxc) { 71287247a8SAlexander Pyhalov al->maxc <<= 1; 72287247a8SAlexander Pyhalov al->argv = realloc(al->argv, al->maxc * sizeof (char *)); 73287247a8SAlexander Pyhalov if (al->argv == NULL) 74287247a8SAlexander Pyhalov err(EX_TEMPFAIL, "realloc"); 75287247a8SAlexander Pyhalov } 76287247a8SAlexander Pyhalov if (arg == NULL) 77287247a8SAlexander Pyhalov al->argv[al->argc++] = NULL; 78287247a8SAlexander Pyhalov else if ((al->argv[al->argc++] = strdup(arg)) == NULL) 79287247a8SAlexander Pyhalov err(EX_TEMPFAIL, "strdup"); 80287247a8SAlexander Pyhalov } 81287247a8SAlexander Pyhalov 82287247a8SAlexander Pyhalov int 83287247a8SAlexander Pyhalov main(int argc, char *argv[], char *envp[]) 84287247a8SAlexander Pyhalov { 85287247a8SAlexander Pyhalov FILE *config; 86287247a8SAlexander Pyhalov char *line, *cp, *from, *to, *ap; 87287247a8SAlexander Pyhalov char progname[PATH_MAX+1]; 88287247a8SAlexander Pyhalov const char *name; 89287247a8SAlexander Pyhalov size_t len, lineno = 0; 90287247a8SAlexander Pyhalov int i; 91287247a8SAlexander Pyhalov struct arglist al; 92287247a8SAlexander Pyhalov 93287247a8SAlexander Pyhalov /* change __progname to mailwrapper so we get sensible error messages */ 94287247a8SAlexander Pyhalov name = getprogname(); 95287247a8SAlexander Pyhalov if (name) { 96287247a8SAlexander Pyhalov strncpy(progname, name, sizeof (progname)); 97287247a8SAlexander Pyhalov } else { 98287247a8SAlexander Pyhalov err(EX_OSERR, "cannot get program name"); 99287247a8SAlexander Pyhalov } 100287247a8SAlexander Pyhalov setprogname("mailwrapper"); 101287247a8SAlexander Pyhalov 102287247a8SAlexander Pyhalov initarg(&al); 103287247a8SAlexander Pyhalov addarg(&al, argv[0]); 104287247a8SAlexander Pyhalov 105287247a8SAlexander Pyhalov if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL) { 106*53f6595fSSebastian Wiedenroth for (i = 1; i < argc; i++) 107*53f6595fSSebastian Wiedenroth addarg(&al, argv[i]); 108287247a8SAlexander Pyhalov addarg(&al, NULL); 109287247a8SAlexander Pyhalov openlog(getprogname(), LOG_PID, LOG_MAIL); 110287247a8SAlexander Pyhalov syslog(LOG_INFO, "cannot open %s, using %s as default MTA", 111287247a8SAlexander Pyhalov _PATH_MAILERCONF, _PATH_DEFAULTMTA); 112287247a8SAlexander Pyhalov closelog(); 113287247a8SAlexander Pyhalov execve(_PATH_DEFAULTMTA, al.argv, envp); 114287247a8SAlexander Pyhalov err(EX_OSERR, "cannot exec %s", _PATH_DEFAULTMTA); 115287247a8SAlexander Pyhalov /*NOTREACHED*/ 116287247a8SAlexander Pyhalov } 117287247a8SAlexander Pyhalov 118287247a8SAlexander Pyhalov for (;;) { 119287247a8SAlexander Pyhalov if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL) { 120287247a8SAlexander Pyhalov if (feof(config)) 121287247a8SAlexander Pyhalov errx(EX_CONFIG, "no mapping for %s in %s", 122287247a8SAlexander Pyhalov progname, _PATH_MAILERCONF); 123287247a8SAlexander Pyhalov err(EX_CONFIG, "cannot parse line %lu", 124287247a8SAlexander Pyhalov (ulong_t)lineno); 125287247a8SAlexander Pyhalov } 126287247a8SAlexander Pyhalov 127287247a8SAlexander Pyhalov #define WS " \t\n" 128287247a8SAlexander Pyhalov cp = line; 129287247a8SAlexander Pyhalov 130287247a8SAlexander Pyhalov cp += strspn(cp, WS); 131287247a8SAlexander Pyhalov if (cp[0] == '\0') { 132287247a8SAlexander Pyhalov /* empty line */ 133287247a8SAlexander Pyhalov free(line); 134287247a8SAlexander Pyhalov continue; 135287247a8SAlexander Pyhalov } 136287247a8SAlexander Pyhalov 137287247a8SAlexander Pyhalov if ((from = strsep(&cp, WS)) == NULL || cp == NULL) 138287247a8SAlexander Pyhalov goto parse_error; 139287247a8SAlexander Pyhalov 140287247a8SAlexander Pyhalov cp += strspn(cp, WS); 141287247a8SAlexander Pyhalov 142287247a8SAlexander Pyhalov if ((to = strsep(&cp, WS)) == NULL) 143287247a8SAlexander Pyhalov goto parse_error; 144287247a8SAlexander Pyhalov 145287247a8SAlexander Pyhalov if (strcmp(from, progname) == 0) { 146287247a8SAlexander Pyhalov for (ap = strsep(&cp, WS); ap != NULL; 147287247a8SAlexander Pyhalov ap = strsep(&cp, WS)) { 148287247a8SAlexander Pyhalov if (*ap) 149287247a8SAlexander Pyhalov addarg(&al, ap); 150287247a8SAlexander Pyhalov } 151287247a8SAlexander Pyhalov break; 152287247a8SAlexander Pyhalov } 153287247a8SAlexander Pyhalov 154287247a8SAlexander Pyhalov free(line); 155287247a8SAlexander Pyhalov } 156287247a8SAlexander Pyhalov 157287247a8SAlexander Pyhalov (void) fclose(config); 158287247a8SAlexander Pyhalov 159287247a8SAlexander Pyhalov for (i = 1; i < argc; i++) 160287247a8SAlexander Pyhalov addarg(&al, argv[i]); 161287247a8SAlexander Pyhalov 162287247a8SAlexander Pyhalov addarg(&al, NULL); 163287247a8SAlexander Pyhalov execve(to, al.argv, envp); 164287247a8SAlexander Pyhalov err(EX_OSERR, "cannot exec %s", to); 165287247a8SAlexander Pyhalov /*NOTREACHED*/ 166287247a8SAlexander Pyhalov parse_error: 167287247a8SAlexander Pyhalov errx(EX_CONFIG, "parse error in %s at line %lu", 168287247a8SAlexander Pyhalov _PATH_MAILERCONF, (ulong_t)lineno); 169287247a8SAlexander Pyhalov /*NOTREACHED*/ 170287247a8SAlexander Pyhalov return (0); 171287247a8SAlexander Pyhalov } 172