xref: /illumos-gate/usr/src/lib/libwrap/options.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate  /*
7*7c478bd9Sstevel@tonic-gate   * General skeleton for adding options to the access control language. The
8*7c478bd9Sstevel@tonic-gate   * features offered by this module are documented in the hosts_options(5)
9*7c478bd9Sstevel@tonic-gate   * manual page (source file: hosts_options.5, "nroff -man" format).
10*7c478bd9Sstevel@tonic-gate   *
11*7c478bd9Sstevel@tonic-gate   * Notes and warnings for those who want to add features:
12*7c478bd9Sstevel@tonic-gate   *
13*7c478bd9Sstevel@tonic-gate   * In case of errors, abort options processing and deny access. There are too
14*7c478bd9Sstevel@tonic-gate   * many irreversible side effects to make error recovery feasible. For
15*7c478bd9Sstevel@tonic-gate   * example, it makes no sense to continue after we have already changed the
16*7c478bd9Sstevel@tonic-gate   * userid.
17*7c478bd9Sstevel@tonic-gate   *
18*7c478bd9Sstevel@tonic-gate   * In case of errors, do not terminate the process: the routines might be
19*7c478bd9Sstevel@tonic-gate   * called from a long-running daemon that should run forever. Instead, call
20*7c478bd9Sstevel@tonic-gate   * tcpd_jump() which does a non-local goto back into the hosts_access()
21*7c478bd9Sstevel@tonic-gate   * routine.
22*7c478bd9Sstevel@tonic-gate   *
23*7c478bd9Sstevel@tonic-gate   * In case of severe errors, use clean_exit() instead of directly calling
24*7c478bd9Sstevel@tonic-gate   * exit(), or the inetd may loop on an UDP request.
25*7c478bd9Sstevel@tonic-gate   *
26*7c478bd9Sstevel@tonic-gate   * In verification mode (for example, with the "tcpdmatch" command) the
27*7c478bd9Sstevel@tonic-gate   * "dry_run" flag is set. In this mode, an option function should just "say"
28*7c478bd9Sstevel@tonic-gate   * what it is going to do instead of really doing it.
29*7c478bd9Sstevel@tonic-gate   *
30*7c478bd9Sstevel@tonic-gate   * Some option functions do not return (for example, the twist option passes
31*7c478bd9Sstevel@tonic-gate   * control to another program). In verification mode (dry_run flag is set)
32*7c478bd9Sstevel@tonic-gate   * such options should clear the "dry_run" flag to inform the caller of this
33*7c478bd9Sstevel@tonic-gate   * course of action.
34*7c478bd9Sstevel@tonic-gate   */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #ifndef lint
37*7c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#) options.c 1.17 96/02/11 17:01:31";
38*7c478bd9Sstevel@tonic-gate #endif
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /* System libraries. */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
47*7c478bd9Sstevel@tonic-gate #include <netdb.h>
48*7c478bd9Sstevel@tonic-gate #include <stdio.h>
49*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
50*7c478bd9Sstevel@tonic-gate #include <unistd.h>
51*7c478bd9Sstevel@tonic-gate #include <syslog.h>
52*7c478bd9Sstevel@tonic-gate #include <pwd.h>
53*7c478bd9Sstevel@tonic-gate #include <grp.h>
54*7c478bd9Sstevel@tonic-gate #include <ctype.h>
55*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
56*7c478bd9Sstevel@tonic-gate #include <string.h>
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #ifndef MAXPATHNAMELEN
59*7c478bd9Sstevel@tonic-gate #define MAXPATHNAMELEN  BUFSIZ
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* Local stuff. */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include "tcpd.h"
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /* Options runtime support. */
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate int     dry_run = 0;			/* flag set in verification mode */
69*7c478bd9Sstevel@tonic-gate extern jmp_buf tcpd_buf;		/* tcpd_jump() support */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate /* Options parser support. */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static char whitespace_eq[] = "= \t\r\n";
74*7c478bd9Sstevel@tonic-gate #define whitespace (whitespace_eq + 1)
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static char *get_field();		/* chew :-delimited field off string */
77*7c478bd9Sstevel@tonic-gate static char *chop_string();		/* strip leading and trailing blanks */
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /* List of functions that implement the options. Add yours here. */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static void user_option();		/* execute "user name.group" option */
82*7c478bd9Sstevel@tonic-gate static void group_option();		/* execute "group name" option */
83*7c478bd9Sstevel@tonic-gate static void umask_option();		/* execute "umask mask" option */
84*7c478bd9Sstevel@tonic-gate static void linger_option();		/* execute "linger time" option */
85*7c478bd9Sstevel@tonic-gate static void keepalive_option();		/* execute "keepalive" option */
86*7c478bd9Sstevel@tonic-gate static void spawn_option();		/* execute "spawn command" option */
87*7c478bd9Sstevel@tonic-gate static void twist_option();		/* execute "twist command" option */
88*7c478bd9Sstevel@tonic-gate static void rfc931_option();		/* execute "rfc931" option */
89*7c478bd9Sstevel@tonic-gate static void setenv_option();		/* execute "setenv name value" */
90*7c478bd9Sstevel@tonic-gate static void nice_option();		/* execute "nice" option */
91*7c478bd9Sstevel@tonic-gate static void severity_option();		/* execute "severity value" */
92*7c478bd9Sstevel@tonic-gate static void allow_option();		/* execute "allow" option */
93*7c478bd9Sstevel@tonic-gate static void deny_option();		/* execute "deny" option */
94*7c478bd9Sstevel@tonic-gate static void banners_option();		/* execute "banners path" option */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate /* Structure of the options table. */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate struct option {
99*7c478bd9Sstevel@tonic-gate     char   *name;			/* keyword name, case is ignored */
100*7c478bd9Sstevel@tonic-gate     void  (*func) ();			/* function that does the real work */
101*7c478bd9Sstevel@tonic-gate     int     flags;			/* see below... */
102*7c478bd9Sstevel@tonic-gate };
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate #define NEED_ARG	(1<<1)		/* option requires argument */
105*7c478bd9Sstevel@tonic-gate #define USE_LAST	(1<<2)		/* option must be last */
106*7c478bd9Sstevel@tonic-gate #define OPT_ARG		(1<<3)		/* option has optional argument */
107*7c478bd9Sstevel@tonic-gate #define EXPAND_ARG	(1<<4)		/* do %x expansion on argument */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate #define need_arg(o)	((o)->flags & NEED_ARG)
110*7c478bd9Sstevel@tonic-gate #define opt_arg(o)	((o)->flags & OPT_ARG)
111*7c478bd9Sstevel@tonic-gate #define permit_arg(o)	((o)->flags & (NEED_ARG | OPT_ARG))
112*7c478bd9Sstevel@tonic-gate #define use_last(o)	((o)->flags & USE_LAST)
113*7c478bd9Sstevel@tonic-gate #define expand_arg(o)	((o)->flags & EXPAND_ARG)
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate /* List of known keywords. Add yours here. */
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static struct option option_table[] = {
118*7c478bd9Sstevel@tonic-gate     "user", user_option, NEED_ARG,
119*7c478bd9Sstevel@tonic-gate     "group", group_option, NEED_ARG,
120*7c478bd9Sstevel@tonic-gate     "umask", umask_option, NEED_ARG,
121*7c478bd9Sstevel@tonic-gate     "linger", linger_option, NEED_ARG,
122*7c478bd9Sstevel@tonic-gate     "keepalive", keepalive_option, 0,
123*7c478bd9Sstevel@tonic-gate     "spawn", spawn_option, NEED_ARG | EXPAND_ARG,
124*7c478bd9Sstevel@tonic-gate     "twist", twist_option, NEED_ARG | EXPAND_ARG | USE_LAST,
125*7c478bd9Sstevel@tonic-gate     "rfc931", rfc931_option, OPT_ARG,
126*7c478bd9Sstevel@tonic-gate     "setenv", setenv_option, NEED_ARG | EXPAND_ARG,
127*7c478bd9Sstevel@tonic-gate     "nice", nice_option, OPT_ARG,
128*7c478bd9Sstevel@tonic-gate     "severity", severity_option, NEED_ARG,
129*7c478bd9Sstevel@tonic-gate     "allow", allow_option, USE_LAST,
130*7c478bd9Sstevel@tonic-gate     "deny", deny_option, USE_LAST,
131*7c478bd9Sstevel@tonic-gate     "banners", banners_option, NEED_ARG,
132*7c478bd9Sstevel@tonic-gate     0,
133*7c478bd9Sstevel@tonic-gate };
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /* process_options - process access control options */
136*7c478bd9Sstevel@tonic-gate 
process_options(options,request)137*7c478bd9Sstevel@tonic-gate void    process_options(options, request)
138*7c478bd9Sstevel@tonic-gate char   *options;
139*7c478bd9Sstevel@tonic-gate struct request_info *request;
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate     char   *key;
142*7c478bd9Sstevel@tonic-gate     char   *value;
143*7c478bd9Sstevel@tonic-gate     char   *curr_opt;
144*7c478bd9Sstevel@tonic-gate     char   *next_opt;
145*7c478bd9Sstevel@tonic-gate     struct option *op;
146*7c478bd9Sstevel@tonic-gate     char    bf[BUFSIZ];
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate     for (curr_opt = get_field(options); curr_opt; curr_opt = next_opt) {
149*7c478bd9Sstevel@tonic-gate 	next_opt = get_field((char *) 0);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/*
152*7c478bd9Sstevel@tonic-gate 	 * Separate the option into name and value parts. For backwards
153*7c478bd9Sstevel@tonic-gate 	 * compatibility we ignore exactly one '=' between name and value.
154*7c478bd9Sstevel@tonic-gate 	 */
155*7c478bd9Sstevel@tonic-gate 	curr_opt = chop_string(curr_opt);
156*7c478bd9Sstevel@tonic-gate 	if (*(value = curr_opt + strcspn(curr_opt, whitespace_eq))) {
157*7c478bd9Sstevel@tonic-gate 	    if (*value != '=') {
158*7c478bd9Sstevel@tonic-gate 		*value++ = 0;
159*7c478bd9Sstevel@tonic-gate 		value += strspn(value, whitespace);
160*7c478bd9Sstevel@tonic-gate 	    }
161*7c478bd9Sstevel@tonic-gate 	    if (*value == '=') {
162*7c478bd9Sstevel@tonic-gate 		*value++ = 0;
163*7c478bd9Sstevel@tonic-gate 		value += strspn(value, whitespace);
164*7c478bd9Sstevel@tonic-gate 	    }
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 	if (*value == 0)
167*7c478bd9Sstevel@tonic-gate 	    value = 0;
168*7c478bd9Sstevel@tonic-gate 	key = curr_opt;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	/*
171*7c478bd9Sstevel@tonic-gate 	 * Disallow missing option names (and empty option fields).
172*7c478bd9Sstevel@tonic-gate 	 */
173*7c478bd9Sstevel@tonic-gate 	if (*key == 0)
174*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("missing option name");
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/*
177*7c478bd9Sstevel@tonic-gate 	 * Lookup the option-specific info and do some common error checks.
178*7c478bd9Sstevel@tonic-gate 	 * Delegate option-specific processing to the specific functions.
179*7c478bd9Sstevel@tonic-gate 	 */
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	for (op = option_table; op->name && STR_NE(op->name, key); op++)
182*7c478bd9Sstevel@tonic-gate 	     /* VOID */ ;
183*7c478bd9Sstevel@tonic-gate 	if (op->name == 0)
184*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("bad option name: \"%s\"", key);
185*7c478bd9Sstevel@tonic-gate 	if (!value && need_arg(op))
186*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("option \"%s\" requires value", key);
187*7c478bd9Sstevel@tonic-gate 	if (value && !permit_arg(op))
188*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("option \"%s\" requires no value", key);
189*7c478bd9Sstevel@tonic-gate 	if (next_opt && use_last(op))
190*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("option \"%s\" must be at end", key);
191*7c478bd9Sstevel@tonic-gate 	if (value && expand_arg(op))
192*7c478bd9Sstevel@tonic-gate 	    value = chop_string(percent_x(bf, sizeof(bf), value, request));
193*7c478bd9Sstevel@tonic-gate 	if (hosts_access_verbose)
194*7c478bd9Sstevel@tonic-gate 	    syslog(LOG_DEBUG, "option:   %s %s", key, value ? value : "");
195*7c478bd9Sstevel@tonic-gate 	(*(op->func)) (value, request);
196*7c478bd9Sstevel@tonic-gate     }
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /* allow_option - grant access */
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
202*7c478bd9Sstevel@tonic-gate 
allow_option(value,request)203*7c478bd9Sstevel@tonic-gate static void allow_option(value, request)
204*7c478bd9Sstevel@tonic-gate char   *value;
205*7c478bd9Sstevel@tonic-gate struct request_info *request;
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate     longjmp(tcpd_buf, AC_PERMIT);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate /* deny_option - deny access */
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
213*7c478bd9Sstevel@tonic-gate 
deny_option(value,request)214*7c478bd9Sstevel@tonic-gate static void deny_option(value, request)
215*7c478bd9Sstevel@tonic-gate char   *value;
216*7c478bd9Sstevel@tonic-gate struct request_info *request;
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate     longjmp(tcpd_buf, AC_DENY);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate /* banners_option - expand %<char>, terminate each line with CRLF */
222*7c478bd9Sstevel@tonic-gate 
banners_option(value,request)223*7c478bd9Sstevel@tonic-gate static void banners_option(value, request)
224*7c478bd9Sstevel@tonic-gate char   *value;
225*7c478bd9Sstevel@tonic-gate struct request_info *request;
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate     char    path[MAXPATHNAMELEN];
228*7c478bd9Sstevel@tonic-gate     char    ibuf[BUFSIZ];
229*7c478bd9Sstevel@tonic-gate     char    obuf[2 * BUFSIZ];
230*7c478bd9Sstevel@tonic-gate     struct stat st;
231*7c478bd9Sstevel@tonic-gate     int     ch;
232*7c478bd9Sstevel@tonic-gate     FILE   *fp;
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate     sprintf(path, "%s/%s", value, eval_daemon(request));
235*7c478bd9Sstevel@tonic-gate     if ((fp = fopen(path, "r")) != 0) {
236*7c478bd9Sstevel@tonic-gate 	while ((ch = fgetc(fp)) == 0)
237*7c478bd9Sstevel@tonic-gate 	    write(request->fd, "", 1);
238*7c478bd9Sstevel@tonic-gate 	ungetc(ch, fp);
239*7c478bd9Sstevel@tonic-gate 	while (fgets(ibuf, sizeof(ibuf) - 1, fp)) {
240*7c478bd9Sstevel@tonic-gate 	    if (split_at(ibuf, '\n'))
241*7c478bd9Sstevel@tonic-gate 		strcat(ibuf, "\r\n");
242*7c478bd9Sstevel@tonic-gate 	    percent_x(obuf, sizeof(obuf), ibuf, request);
243*7c478bd9Sstevel@tonic-gate 	    write(request->fd, obuf, strlen(obuf));
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	fclose(fp);
246*7c478bd9Sstevel@tonic-gate     } else if (stat(value, &st) < 0) {
247*7c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: %m", value);
248*7c478bd9Sstevel@tonic-gate     }
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate /* group_option - switch group id */
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
254*7c478bd9Sstevel@tonic-gate 
group_option(value,request)255*7c478bd9Sstevel@tonic-gate static void group_option(value, request)
256*7c478bd9Sstevel@tonic-gate char   *value;
257*7c478bd9Sstevel@tonic-gate struct request_info *request;
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate     struct group *grp;
260*7c478bd9Sstevel@tonic-gate     struct group *getgrnam();
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate     if ((grp = getgrnam(value)) == 0)
263*7c478bd9Sstevel@tonic-gate 	tcpd_jump("unknown group: \"%s\"", value);
264*7c478bd9Sstevel@tonic-gate     endgrent();
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate     if (dry_run == 0 && setgid(grp->gr_gid))
267*7c478bd9Sstevel@tonic-gate 	tcpd_jump("setgid(%s): %m", value);
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate /* user_option - switch user id */
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
273*7c478bd9Sstevel@tonic-gate 
user_option(value,request)274*7c478bd9Sstevel@tonic-gate static void user_option(value, request)
275*7c478bd9Sstevel@tonic-gate char   *value;
276*7c478bd9Sstevel@tonic-gate struct request_info *request;
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate     struct passwd *pwd;
279*7c478bd9Sstevel@tonic-gate     struct passwd *getpwnam();
280*7c478bd9Sstevel@tonic-gate     char   *group;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate     if ((group = split_at(value, '.')) != 0)
283*7c478bd9Sstevel@tonic-gate 	group_option(group, request);
284*7c478bd9Sstevel@tonic-gate     if ((pwd = getpwnam(value)) == 0)
285*7c478bd9Sstevel@tonic-gate 	tcpd_jump("unknown user: \"%s\"", value);
286*7c478bd9Sstevel@tonic-gate     endpwent();
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate     if (dry_run == 0 && setuid(pwd->pw_uid))
289*7c478bd9Sstevel@tonic-gate 	tcpd_jump("setuid(%s): %m", value);
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate /* umask_option - set file creation mask */
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
295*7c478bd9Sstevel@tonic-gate 
umask_option(value,request)296*7c478bd9Sstevel@tonic-gate static void umask_option(value, request)
297*7c478bd9Sstevel@tonic-gate char   *value;
298*7c478bd9Sstevel@tonic-gate struct request_info *request;
299*7c478bd9Sstevel@tonic-gate {
300*7c478bd9Sstevel@tonic-gate     unsigned mask;
301*7c478bd9Sstevel@tonic-gate     char    junk;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate     if (sscanf(value, "%o%c", &mask, &junk) != 1 || (mask & 0777) != mask)
304*7c478bd9Sstevel@tonic-gate 	tcpd_jump("bad umask value: \"%s\"", value);
305*7c478bd9Sstevel@tonic-gate     (void) umask(mask);
306*7c478bd9Sstevel@tonic-gate }
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate /* spawn_option - spawn a shell command and wait */
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
311*7c478bd9Sstevel@tonic-gate 
spawn_option(value,request)312*7c478bd9Sstevel@tonic-gate static void spawn_option(value, request)
313*7c478bd9Sstevel@tonic-gate char   *value;
314*7c478bd9Sstevel@tonic-gate struct request_info *request;
315*7c478bd9Sstevel@tonic-gate {
316*7c478bd9Sstevel@tonic-gate     if (dry_run == 0)
317*7c478bd9Sstevel@tonic-gate 	shell_cmd(value);
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
323*7c478bd9Sstevel@tonic-gate 
linger_option(value,request)324*7c478bd9Sstevel@tonic-gate static void linger_option(value, request)
325*7c478bd9Sstevel@tonic-gate char   *value;
326*7c478bd9Sstevel@tonic-gate struct request_info *request;
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate     struct linger linger;
329*7c478bd9Sstevel@tonic-gate     char    junk;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate     if (sscanf(value, "%d%c", &linger.l_linger, &junk) != 1
332*7c478bd9Sstevel@tonic-gate 	|| linger.l_linger < 0)
333*7c478bd9Sstevel@tonic-gate 	tcpd_jump("bad linger value: \"%s\"", value);
334*7c478bd9Sstevel@tonic-gate     if (dry_run == 0) {
335*7c478bd9Sstevel@tonic-gate 	linger.l_onoff = (linger.l_linger != 0);
336*7c478bd9Sstevel@tonic-gate 	if (setsockopt(request->fd, SOL_SOCKET, SO_LINGER, (char *) &linger,
337*7c478bd9Sstevel@tonic-gate 		       sizeof(linger)) < 0)
338*7c478bd9Sstevel@tonic-gate 	    tcpd_warn("setsockopt SO_LINGER %d: %m", linger.l_linger);
339*7c478bd9Sstevel@tonic-gate     }
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate /* keepalive_option - set the socket keepalive option */
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
345*7c478bd9Sstevel@tonic-gate 
keepalive_option(value,request)346*7c478bd9Sstevel@tonic-gate static void keepalive_option(value, request)
347*7c478bd9Sstevel@tonic-gate char   *value;
348*7c478bd9Sstevel@tonic-gate struct request_info *request;
349*7c478bd9Sstevel@tonic-gate {
350*7c478bd9Sstevel@tonic-gate     static int on = 1;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate     if (dry_run == 0 && setsockopt(request->fd, SOL_SOCKET, SO_KEEPALIVE,
353*7c478bd9Sstevel@tonic-gate 				   (char *) &on, sizeof(on)) < 0)
354*7c478bd9Sstevel@tonic-gate 	tcpd_warn("setsockopt SO_KEEPALIVE: %m");
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate /* nice_option - set nice value */
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
360*7c478bd9Sstevel@tonic-gate 
nice_option(value,request)361*7c478bd9Sstevel@tonic-gate static void nice_option(value, request)
362*7c478bd9Sstevel@tonic-gate char   *value;
363*7c478bd9Sstevel@tonic-gate struct request_info *request;
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate     int     niceval = 10;
366*7c478bd9Sstevel@tonic-gate     char    junk;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate     if (value != 0 && sscanf(value, "%d%c", &niceval, &junk) != 1)
369*7c478bd9Sstevel@tonic-gate 	tcpd_jump("bad nice value: \"%s\"", value);
370*7c478bd9Sstevel@tonic-gate     if (dry_run == 0 && nice(niceval) < 0)
371*7c478bd9Sstevel@tonic-gate 	tcpd_warn("nice(%d): %m", niceval);
372*7c478bd9Sstevel@tonic-gate }
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate /* twist_option - replace process by shell command */
375*7c478bd9Sstevel@tonic-gate 
twist_option(value,request)376*7c478bd9Sstevel@tonic-gate static void twist_option(value, request)
377*7c478bd9Sstevel@tonic-gate char   *value;
378*7c478bd9Sstevel@tonic-gate struct request_info *request;
379*7c478bd9Sstevel@tonic-gate {
380*7c478bd9Sstevel@tonic-gate     char   *error;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate     if (dry_run != 0) {
383*7c478bd9Sstevel@tonic-gate 	dry_run = 0;
384*7c478bd9Sstevel@tonic-gate     } else {
385*7c478bd9Sstevel@tonic-gate 	if (resident > 0)
386*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("twist option in resident process");
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	syslog(deny_severity, "twist %s to %s", eval_client(request), value);
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	/* Before switching to the shell, set up stdin, stdout and stderr. */
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	if (maybe_dup2(request->fd, 0) != 0 ||
395*7c478bd9Sstevel@tonic-gate 	    maybe_dup2(request->fd, 1) != 1 ||
396*7c478bd9Sstevel@tonic-gate 	    maybe_dup2(request->fd, 2) != 2) {
397*7c478bd9Sstevel@tonic-gate 	    error = "twist_option: dup: %m";
398*7c478bd9Sstevel@tonic-gate 	} else {
399*7c478bd9Sstevel@tonic-gate 	    if (request->fd > 2)
400*7c478bd9Sstevel@tonic-gate 		close(request->fd);
401*7c478bd9Sstevel@tonic-gate 	    (void) execl("/bin/sh", "sh", "-c", value, (char *) 0);
402*7c478bd9Sstevel@tonic-gate 	    error = "twist_option: /bin/sh: %m";
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	/* Something went wrong: we MUST terminate the process. */
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 	tcpd_warn(error);
408*7c478bd9Sstevel@tonic-gate 	clean_exit(request);
409*7c478bd9Sstevel@tonic-gate     }
410*7c478bd9Sstevel@tonic-gate }
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate /* rfc931_option - look up remote user name */
413*7c478bd9Sstevel@tonic-gate 
rfc931_option(value,request)414*7c478bd9Sstevel@tonic-gate static void rfc931_option(value, request)
415*7c478bd9Sstevel@tonic-gate char   *value;
416*7c478bd9Sstevel@tonic-gate struct request_info *request;
417*7c478bd9Sstevel@tonic-gate {
418*7c478bd9Sstevel@tonic-gate     int     timeout;
419*7c478bd9Sstevel@tonic-gate     char    junk;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate     if (value != 0) {
422*7c478bd9Sstevel@tonic-gate 	if (sscanf(value, "%d%c", &timeout, &junk) != 1 || timeout <= 0)
423*7c478bd9Sstevel@tonic-gate 	    tcpd_jump("bad rfc931 timeout: \"%s\"", value);
424*7c478bd9Sstevel@tonic-gate 	rfc931_timeout = timeout;
425*7c478bd9Sstevel@tonic-gate     }
426*7c478bd9Sstevel@tonic-gate     (void) eval_user(request);
427*7c478bd9Sstevel@tonic-gate }
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate /* setenv_option - set environment variable */
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
432*7c478bd9Sstevel@tonic-gate 
setenv_option(value,request)433*7c478bd9Sstevel@tonic-gate static void setenv_option(value, request)
434*7c478bd9Sstevel@tonic-gate char   *value;
435*7c478bd9Sstevel@tonic-gate struct request_info *request;
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate     extern int setenv(const char *, const char *, int);
438*7c478bd9Sstevel@tonic-gate     char   *var_value;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate     if (*(var_value = value + strcspn(value, whitespace)))
441*7c478bd9Sstevel@tonic-gate 	*var_value++ = 0;
442*7c478bd9Sstevel@tonic-gate     if (setenv(chop_string(value), chop_string(var_value), 1))
443*7c478bd9Sstevel@tonic-gate 	tcpd_jump("memory allocation failure");
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate  /*
447*7c478bd9Sstevel@tonic-gate   * The severity option goes last because it comes with a huge amount of ugly
448*7c478bd9Sstevel@tonic-gate   * #ifdefs and tables.
449*7c478bd9Sstevel@tonic-gate   */
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate struct syslog_names {
452*7c478bd9Sstevel@tonic-gate     char   *name;
453*7c478bd9Sstevel@tonic-gate     int     value;
454*7c478bd9Sstevel@tonic-gate };
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate static struct syslog_names log_fac[] = {
457*7c478bd9Sstevel@tonic-gate #ifdef LOG_KERN
458*7c478bd9Sstevel@tonic-gate     "kern", LOG_KERN,
459*7c478bd9Sstevel@tonic-gate #endif
460*7c478bd9Sstevel@tonic-gate #ifdef LOG_USER
461*7c478bd9Sstevel@tonic-gate     "user", LOG_USER,
462*7c478bd9Sstevel@tonic-gate #endif
463*7c478bd9Sstevel@tonic-gate #ifdef LOG_MAIL
464*7c478bd9Sstevel@tonic-gate     "mail", LOG_MAIL,
465*7c478bd9Sstevel@tonic-gate #endif
466*7c478bd9Sstevel@tonic-gate #ifdef LOG_DAEMON
467*7c478bd9Sstevel@tonic-gate     "daemon", LOG_DAEMON,
468*7c478bd9Sstevel@tonic-gate #endif
469*7c478bd9Sstevel@tonic-gate #ifdef LOG_AUTH
470*7c478bd9Sstevel@tonic-gate     "auth", LOG_AUTH,
471*7c478bd9Sstevel@tonic-gate #endif
472*7c478bd9Sstevel@tonic-gate #ifdef LOG_LPR
473*7c478bd9Sstevel@tonic-gate     "lpr", LOG_LPR,
474*7c478bd9Sstevel@tonic-gate #endif
475*7c478bd9Sstevel@tonic-gate #ifdef LOG_NEWS
476*7c478bd9Sstevel@tonic-gate     "news", LOG_NEWS,
477*7c478bd9Sstevel@tonic-gate #endif
478*7c478bd9Sstevel@tonic-gate #ifdef LOG_UUCP
479*7c478bd9Sstevel@tonic-gate     "uucp", LOG_UUCP,
480*7c478bd9Sstevel@tonic-gate #endif
481*7c478bd9Sstevel@tonic-gate #ifdef LOG_CRON
482*7c478bd9Sstevel@tonic-gate     "cron", LOG_CRON,
483*7c478bd9Sstevel@tonic-gate #endif
484*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL0
485*7c478bd9Sstevel@tonic-gate     "local0", LOG_LOCAL0,
486*7c478bd9Sstevel@tonic-gate #endif
487*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL1
488*7c478bd9Sstevel@tonic-gate     "local1", LOG_LOCAL1,
489*7c478bd9Sstevel@tonic-gate #endif
490*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL2
491*7c478bd9Sstevel@tonic-gate     "local2", LOG_LOCAL2,
492*7c478bd9Sstevel@tonic-gate #endif
493*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL3
494*7c478bd9Sstevel@tonic-gate     "local3", LOG_LOCAL3,
495*7c478bd9Sstevel@tonic-gate #endif
496*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL4
497*7c478bd9Sstevel@tonic-gate     "local4", LOG_LOCAL4,
498*7c478bd9Sstevel@tonic-gate #endif
499*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL5
500*7c478bd9Sstevel@tonic-gate     "local5", LOG_LOCAL5,
501*7c478bd9Sstevel@tonic-gate #endif
502*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL6
503*7c478bd9Sstevel@tonic-gate     "local6", LOG_LOCAL6,
504*7c478bd9Sstevel@tonic-gate #endif
505*7c478bd9Sstevel@tonic-gate #ifdef LOG_LOCAL7
506*7c478bd9Sstevel@tonic-gate     "local7", LOG_LOCAL7,
507*7c478bd9Sstevel@tonic-gate #endif
508*7c478bd9Sstevel@tonic-gate     0,
509*7c478bd9Sstevel@tonic-gate };
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate static struct syslog_names log_sev[] = {
512*7c478bd9Sstevel@tonic-gate #ifdef LOG_EMERG
513*7c478bd9Sstevel@tonic-gate     "emerg", LOG_EMERG,
514*7c478bd9Sstevel@tonic-gate #endif
515*7c478bd9Sstevel@tonic-gate #ifdef LOG_ALERT
516*7c478bd9Sstevel@tonic-gate     "alert", LOG_ALERT,
517*7c478bd9Sstevel@tonic-gate #endif
518*7c478bd9Sstevel@tonic-gate #ifdef LOG_CRIT
519*7c478bd9Sstevel@tonic-gate     "crit", LOG_CRIT,
520*7c478bd9Sstevel@tonic-gate #endif
521*7c478bd9Sstevel@tonic-gate #ifdef LOG_ERR
522*7c478bd9Sstevel@tonic-gate     "err", LOG_ERR,
523*7c478bd9Sstevel@tonic-gate #endif
524*7c478bd9Sstevel@tonic-gate #ifdef LOG_WARNING
525*7c478bd9Sstevel@tonic-gate     "warning", LOG_WARNING,
526*7c478bd9Sstevel@tonic-gate #endif
527*7c478bd9Sstevel@tonic-gate #ifdef LOG_NOTICE
528*7c478bd9Sstevel@tonic-gate     "notice", LOG_NOTICE,
529*7c478bd9Sstevel@tonic-gate #endif
530*7c478bd9Sstevel@tonic-gate #ifdef LOG_INFO
531*7c478bd9Sstevel@tonic-gate     "info", LOG_INFO,
532*7c478bd9Sstevel@tonic-gate #endif
533*7c478bd9Sstevel@tonic-gate #ifdef LOG_DEBUG
534*7c478bd9Sstevel@tonic-gate     "debug", LOG_DEBUG,
535*7c478bd9Sstevel@tonic-gate #endif
536*7c478bd9Sstevel@tonic-gate     0,
537*7c478bd9Sstevel@tonic-gate };
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate /* severity_map - lookup facility or severity value */
540*7c478bd9Sstevel@tonic-gate 
severity_map(table,name)541*7c478bd9Sstevel@tonic-gate static int severity_map(table, name)
542*7c478bd9Sstevel@tonic-gate struct syslog_names *table;
543*7c478bd9Sstevel@tonic-gate char   *name;
544*7c478bd9Sstevel@tonic-gate {
545*7c478bd9Sstevel@tonic-gate     struct syslog_names *t;
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate     for (t = table; t->name; t++)
548*7c478bd9Sstevel@tonic-gate 	if (STR_EQ(t->name, name))
549*7c478bd9Sstevel@tonic-gate 	    return (t->value);
550*7c478bd9Sstevel@tonic-gate     tcpd_jump("bad syslog facility or severity: \"%s\"", name);
551*7c478bd9Sstevel@tonic-gate     /* NOTREACHED */
552*7c478bd9Sstevel@tonic-gate }
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate /* severity_option - change logging severity for this event (Dave Mitchell) */
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
557*7c478bd9Sstevel@tonic-gate 
severity_option(value,request)558*7c478bd9Sstevel@tonic-gate static void severity_option(value, request)
559*7c478bd9Sstevel@tonic-gate char   *value;
560*7c478bd9Sstevel@tonic-gate struct request_info *request;
561*7c478bd9Sstevel@tonic-gate {
562*7c478bd9Sstevel@tonic-gate     char   *level = split_at(value, '.');
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate     allow_severity = deny_severity = level ?
565*7c478bd9Sstevel@tonic-gate 	severity_map(log_fac, value) | severity_map(log_sev, level) :
566*7c478bd9Sstevel@tonic-gate 	severity_map(log_sev, value);
567*7c478bd9Sstevel@tonic-gate }
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate /* get_field - return pointer to next field in string */
570*7c478bd9Sstevel@tonic-gate 
get_field(string)571*7c478bd9Sstevel@tonic-gate static char *get_field(string)
572*7c478bd9Sstevel@tonic-gate char   *string;
573*7c478bd9Sstevel@tonic-gate {
574*7c478bd9Sstevel@tonic-gate     static char *last = "";
575*7c478bd9Sstevel@tonic-gate     char   *src;
576*7c478bd9Sstevel@tonic-gate     char   *dst;
577*7c478bd9Sstevel@tonic-gate     char   *ret;
578*7c478bd9Sstevel@tonic-gate     int     ch;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate     /*
581*7c478bd9Sstevel@tonic-gate      * This function returns pointers to successive fields within a given
582*7c478bd9Sstevel@tonic-gate      * string. ":" is the field separator; warn if the rule ends in one. It
583*7c478bd9Sstevel@tonic-gate      * replaces a "\:" sequence by ":", without treating the result of
584*7c478bd9Sstevel@tonic-gate      * substitution as field terminator. A null argument means resume search
585*7c478bd9Sstevel@tonic-gate      * where the previous call terminated. This function destroys its
586*7c478bd9Sstevel@tonic-gate      * argument.
587*7c478bd9Sstevel@tonic-gate      *
588*7c478bd9Sstevel@tonic-gate      * Work from explicit source or from memory. While processing \: we
589*7c478bd9Sstevel@tonic-gate      * overwrite the input. This way we do not have to maintain buffers for
590*7c478bd9Sstevel@tonic-gate      * copies of input fields.
591*7c478bd9Sstevel@tonic-gate      */
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate     src = dst = ret = (string ? string : last);
594*7c478bd9Sstevel@tonic-gate     if (src[0] == 0)
595*7c478bd9Sstevel@tonic-gate 	return (0);
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate     while (ch = *src) {
598*7c478bd9Sstevel@tonic-gate 	if (ch == ':') {
599*7c478bd9Sstevel@tonic-gate 	    if (*++src == 0)
600*7c478bd9Sstevel@tonic-gate 		tcpd_warn("rule ends in \":\"");
601*7c478bd9Sstevel@tonic-gate 	    break;
602*7c478bd9Sstevel@tonic-gate 	}
603*7c478bd9Sstevel@tonic-gate 	if (ch == '\\' && src[1] == ':')
604*7c478bd9Sstevel@tonic-gate 	    src++;
605*7c478bd9Sstevel@tonic-gate 	*dst++ = *src++;
606*7c478bd9Sstevel@tonic-gate     }
607*7c478bd9Sstevel@tonic-gate     last = src;
608*7c478bd9Sstevel@tonic-gate     *dst = 0;
609*7c478bd9Sstevel@tonic-gate     return (ret);
610*7c478bd9Sstevel@tonic-gate }
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate /* chop_string - strip leading and trailing blanks from string */
613*7c478bd9Sstevel@tonic-gate 
chop_string(string)614*7c478bd9Sstevel@tonic-gate static char *chop_string(string)
615*7c478bd9Sstevel@tonic-gate register char *string;
616*7c478bd9Sstevel@tonic-gate {
617*7c478bd9Sstevel@tonic-gate     char   *start = 0;
618*7c478bd9Sstevel@tonic-gate     char   *end;
619*7c478bd9Sstevel@tonic-gate     char   *cp;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate     for (cp = string; *cp; cp++) {
622*7c478bd9Sstevel@tonic-gate 	if (!isspace(*cp)) {
623*7c478bd9Sstevel@tonic-gate 	    if (start == 0)
624*7c478bd9Sstevel@tonic-gate 		start = cp;
625*7c478bd9Sstevel@tonic-gate 	    end = cp;
626*7c478bd9Sstevel@tonic-gate 	}
627*7c478bd9Sstevel@tonic-gate     }
628*7c478bd9Sstevel@tonic-gate     return (start ? (end[1] = 0, start) : cp);
629*7c478bd9Sstevel@tonic-gate }
630