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