xref: /netbsd-src/usr.sbin/mailwrapper/mailwrapper.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: mailwrapper.c,v 1.1 1998/12/25 22:06:59 perry Exp $	*/
2 
3 /*
4  * Copyright (c) 1998
5  * 	Perry E. Metzger.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgment:
17  *	This product includes software developed for the NetBSD Project
18  *	by Perry E. Metzger.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <ctype.h>
35 #include <err.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #define _PATH_MAILERCONF	"/etc/mailer.conf"
41 
42 int main __P((int, char *[], char *[]));
43 
44 int
45 main(argc, argv, envp)
46 	int argc;
47 	char *argv[];
48 	char *envp[];
49 {
50 	FILE *config;
51 	char buf[BUFSIZ], from[BUFSIZ], to[BUFSIZ], orig[BUFSIZ];
52 	char *tmp;
53 
54 	if ((config = fopen(_PATH_MAILERCONF, "r")) == NULL)
55 		err(1, "mailswitch: can't open %s", _PATH_MAILERCONF);
56 
57 	if (strlen(argv[0]) > (BUFSIZ - 1))
58 		errx(1, "mailswitch: invoking command pathname is too long.");
59 
60 	tmp = strrchr(argv[0], '/');
61 	if (tmp == NULL)
62 		strcpy(orig, argv[0]);
63 	else
64 		strcpy(orig, ++tmp);
65 
66 	while (1) {
67 		if (fgets(buf, BUFSIZ, config) == NULL)
68 			errx(1, "mailswitch: %s contains no mapping for %s",
69 			    _PATH_MAILERCONF, orig);
70 
71 		if (buf[0] == '#')
72 			continue;
73 
74 		if (sscanf(buf, "%s %s", from, to) != 2)
75 			continue;
76 
77 		if (strcmp(from, orig) == 0)
78 			break;
79 	}
80 
81 	fclose(config);
82 
83 	execve(to, argv, envp);
84 
85 	err(1, "mailswitch: execing %s:", to);
86 }
87