2 * General skeleton for adding options to the access control language. The
3 * features offered by this module are documented in the hosts_options(5)
4 * manual page (source file: hosts_options.5, "nroff -man" format).
6 * Notes and warnings for those who want to add features:
8 * In case of errors, abort options processing and deny access. There are too
9 * many irreversible side effects to make error recovery feasible. For
10 * example, it makes no sense to continue after we have already changed the
13 * In case of errors, do not terminate the process: the routines might be
14 * called from a long-running daemon that should run forever. Instead, call
15 * tcpd_jump() which does a non-local goto back into the hosts_access()
18 * In case of severe errors, use clean_exit() instead of directly calling
19 * exit(), or the inetd may loop on an UDP request.
21 * In verification mode (for example, with the "tcpdmatch" command) the
22 * "dry_run" flag is set. In this mode, an option function should just "say"
23 * what it is going to do instead of really doing it.
25 * Some option functions do not return (for example, the twist option passes
26 * control to another program). In verification mode (dry_run flag is set)
27 * such options should clear the "dry_run" flag to inform the caller of this
32 static char sccsid[] = "@(#) options.c 1.17 96/02/11 17:01:31";
35 /* System libraries. */
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
51 #ifndef MAXPATHNAMELEN
52 #define MAXPATHNAMELEN BUFSIZ
59 /* Options runtime support. */
61 int dry_run = 0; /* flag set in verification mode */
62 extern jmp_buf tcpd_buf; /* tcpd_jump() support */
64 /* Options parser support. */
66 static char whitespace_eq[] = "= \t\r\n";
67 #define whitespace (whitespace_eq + 1)
69 static char *get_field(); /* chew :-delimited field off string */
70 static char *chop_string(); /* strip leading and trailing blanks */
72 /* List of functions that implement the options. Add yours here. */
74 static void user_option(); /* execute "user name.group" option */
75 static void group_option(); /* execute "group name" option */
76 static void umask_option(); /* execute "umask mask" option */
77 static void linger_option(); /* execute "linger time" option */
78 static void keepalive_option(); /* execute "keepalive" option */
79 static void spawn_option(); /* execute "spawn command" option */
80 static void twist_option(); /* execute "twist command" option */
81 static void rfc931_option(); /* execute "rfc931" option */
82 static void setenv_option(); /* execute "setenv name value" */
83 static void nice_option(); /* execute "nice" option */
84 static void severity_option(); /* execute "severity value" */
85 static void allow_option(); /* execute "allow" option */
86 static void deny_option(); /* execute "deny" option */
87 static void banners_option(); /* execute "banners path" option */
89 /* Structure of the options table. */
92 char *name; /* keyword name, case is ignored */
93 void (*func) (); /* function that does the real work */
94 int flags; /* see below... */
97 #define NEED_ARG (1<<1) /* option requires argument */
98 #define USE_LAST (1<<2) /* option must be last */
99 #define OPT_ARG (1<<3) /* option has optional argument */
100 #define EXPAND_ARG (1<<4) /* do %x expansion on argument */
102 #define need_arg(o) ((o)->flags & NEED_ARG)
103 #define opt_arg(o) ((o)->flags & OPT_ARG)
104 #define permit_arg(o) ((o)->flags & (NEED_ARG | OPT_ARG))
105 #define use_last(o) ((o)->flags & USE_LAST)
106 #define expand_arg(o) ((o)->flags & EXPAND_ARG)
108 /* List of known keywords. Add yours here. */
110 static struct option option_table[] = {
111 "user", user_option, NEED_ARG,
112 "group", group_option, NEED_ARG,
113 "umask", umask_option, NEED_ARG,
114 "linger", linger_option, NEED_ARG,
115 "keepalive", keepalive_option, 0,
116 "spawn", spawn_option, NEED_ARG | EXPAND_ARG,
117 "twist", twist_option, NEED_ARG | EXPAND_ARG | USE_LAST,
118 "rfc931", rfc931_option, OPT_ARG,
119 "setenv", setenv_option, NEED_ARG | EXPAND_ARG,
120 "nice", nice_option, OPT_ARG,
121 "severity", severity_option, NEED_ARG,
122 "allow", allow_option, USE_LAST,
123 "deny", deny_option, USE_LAST,
124 "banners", banners_option, NEED_ARG,
128 /* process_options - process access control options */
130 void process_options(options, request)
132 struct request_info *request;
141 for (curr_opt = get_field(options); curr_opt; curr_opt = next_opt) {
142 next_opt = get_field((char *) 0);
145 * Separate the option into name and value parts. For backwards
146 * compatibility we ignore exactly one '=' between name and value.
148 curr_opt = chop_string(curr_opt);
149 if (*(value = curr_opt + strcspn(curr_opt, whitespace_eq))) {
152 value += strspn(value, whitespace);
156 value += strspn(value, whitespace);
164 * Disallow missing option names (and empty option fields).
167 tcpd_jump("missing option name");
170 * Lookup the option-specific info and do some common error checks.
171 * Delegate option-specific processing to the specific functions.
174 for (op = option_table; op->name && STR_NE(op->name, key); op++)
177 tcpd_jump("bad option name: \"%s\"", key);
178 if (!value && need_arg(op))
179 tcpd_jump("option \"%s\" requires value", key);
180 if (value && !permit_arg(op))
181 tcpd_jump("option \"%s\" requires no value", key);
182 if (next_opt && use_last(op))
183 tcpd_jump("option \"%s\" must be at end", key);
184 if (value && expand_arg(op))
185 value = chop_string(percent_x(bf, sizeof(bf), value, request));
186 if (hosts_access_verbose)
187 syslog(LOG_DEBUG, "option: %s %s", key, value ? value : "");
188 (*(op->func)) (value, request);
192 /* allow_option - grant access */
196 static void allow_option(value, request)
198 struct request_info *request;
200 longjmp(tcpd_buf, AC_PERMIT);
203 /* deny_option - deny access */
207 static void deny_option(value, request)
209 struct request_info *request;
211 longjmp(tcpd_buf, AC_DENY);
214 /* banners_option - expand %<char>, terminate each line with CRLF */
216 static void banners_option(value, request)
218 struct request_info *request;
220 char path[MAXPATHNAMELEN];
222 char obuf[2 * BUFSIZ];
227 sprintf(path, "%s/%s", value, eval_daemon(request));
228 if ((fp = fopen(path, "r")) != 0) {
229 while ((ch = fgetc(fp)) == 0)
230 write(request->fd, "", 1);
232 while (fgets(ibuf, sizeof(ibuf) - 1, fp)) {
233 if (split_at(ibuf, '\n'))
234 strcat(ibuf, "\r\n");
235 percent_x(obuf, sizeof(obuf), ibuf, request);
236 write(request->fd, obuf, strlen(obuf));
239 } else if (stat(value, &st) < 0) {
240 tcpd_warn("%s: %m", value);
244 /* group_option - switch group id */
248 static void group_option(value, request)
250 struct request_info *request;
253 struct group *getgrnam();
255 if ((grp = getgrnam(value)) == 0)
256 tcpd_jump("unknown group: \"%s\"", value);
259 if (dry_run == 0 && setgid(grp->gr_gid))
260 tcpd_jump("setgid(%s): %m", value);
263 /* user_option - switch user id */
267 static void user_option(value, request)
269 struct request_info *request;
272 struct passwd *getpwnam();
275 if ((group = split_at(value, '.')) != 0)
276 group_option(group, request);
277 if ((pwd = getpwnam(value)) == 0)
278 tcpd_jump("unknown user: \"%s\"", value);
281 if (dry_run == 0 && setuid(pwd->pw_uid))
282 tcpd_jump("setuid(%s): %m", value);
285 /* umask_option - set file creation mask */
289 static void umask_option(value, request)
291 struct request_info *request;
296 if (sscanf(value, "%o%c", &mask, &junk) != 1 || (mask & 0777) != mask)
297 tcpd_jump("bad umask value: \"%s\"", value);
301 /* spawn_option - spawn a shell command and wait */
305 static void spawn_option(value, request)
307 struct request_info *request;
313 /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
317 static void linger_option(value, request)
319 struct request_info *request;
321 struct linger linger;
324 if (sscanf(value, "%d%c", &linger.l_linger, &junk) != 1
325 || linger.l_linger < 0)
326 tcpd_jump("bad linger value: \"%s\"", value);
328 linger.l_onoff = (linger.l_linger != 0);
329 if (setsockopt(request->fd, SOL_SOCKET, SO_LINGER, (char *) &linger,
331 tcpd_warn("setsockopt SO_LINGER %d: %m", linger.l_linger);
335 /* keepalive_option - set the socket keepalive option */
339 static void keepalive_option(value, request)
341 struct request_info *request;
345 if (dry_run == 0 && setsockopt(request->fd, SOL_SOCKET, SO_KEEPALIVE,
346 (char *) &on, sizeof(on)) < 0)
347 tcpd_warn("setsockopt SO_KEEPALIVE: %m");
350 /* nice_option - set nice value */
354 static void nice_option(value, request)
356 struct request_info *request;
361 if (value != 0 && sscanf(value, "%d%c", &niceval, &junk) != 1)
362 tcpd_jump("bad nice value: \"%s\"", value);
363 if (dry_run == 0 && nice(niceval) < 0)
364 tcpd_warn("nice(%d): %m", niceval);
367 /* twist_option - replace process by shell command */
369 static void twist_option(value, request)
371 struct request_info *request;
379 tcpd_jump("twist option in resident process");
381 syslog(deny_severity, "twist %s to %s", eval_client(request), value);
383 /* Before switching to the shell, set up stdin, stdout and stderr. */
385 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
387 if (maybe_dup2(request->fd, 0) != 0 ||
388 maybe_dup2(request->fd, 1) != 1 ||
389 maybe_dup2(request->fd, 2) != 2) {
390 error = "twist_option: dup: %m";
394 (void) execl("/bin/sh", "sh", "-c", value, (char *) 0);
395 error = "twist_option: /bin/sh: %m";
398 /* Something went wrong: we MUST terminate the process. */
405 /* rfc931_option - look up remote user name */
407 static void rfc931_option(value, request)
409 struct request_info *request;
415 if (sscanf(value, "%d%c", &timeout, &junk) != 1 || timeout <= 0)
416 tcpd_jump("bad rfc931 timeout: \"%s\"", value);
417 rfc931_timeout = timeout;
419 (void) eval_user(request);
422 /* setenv_option - set environment variable */
426 static void setenv_option(value, request)
428 struct request_info *request;
432 if (*(var_value = value + strcspn(value, whitespace)))
434 if (setenv(chop_string(value), chop_string(var_value), 1))
435 tcpd_jump("memory allocation failure");
439 * The severity option goes last because it comes with a huge amount of ugly
440 * #ifdefs and tables.
443 struct syslog_names {
448 static struct syslog_names log_fac[] = {
459 "daemon", LOG_DAEMON,
477 "local0", LOG_LOCAL0,
480 "local1", LOG_LOCAL1,
483 "local2", LOG_LOCAL2,
486 "local3", LOG_LOCAL3,
489 "local4", LOG_LOCAL4,
492 "local5", LOG_LOCAL5,
495 "local6", LOG_LOCAL6,
498 "local7", LOG_LOCAL7,
503 static struct syslog_names log_sev[] = {
517 "warning", LOG_WARNING,
520 "notice", LOG_NOTICE,
531 /* severity_map - lookup facility or severity value */
533 static int severity_map(table, name)
534 struct syslog_names *table;
537 struct syslog_names *t;
539 for (t = table; t->name; t++)
540 if (STR_EQ(t->name, name))
542 tcpd_jump("bad syslog facility or severity: \"%s\"", name);
546 /* severity_option - change logging severity for this event (Dave Mitchell) */
550 static void severity_option(value, request)
552 struct request_info *request;
554 char *level = split_at(value, '.');
556 allow_severity = deny_severity = level ?
557 severity_map(log_fac, value) | severity_map(log_sev, level) :
558 severity_map(log_sev, value);
561 /* get_field - return pointer to next field in string */
563 static char *get_field(string)
566 static char *last = "";
573 * This function returns pointers to successive fields within a given
574 * string. ":" is the field separator; warn if the rule ends in one. It
575 * replaces a "\:" sequence by ":", without treating the result of
576 * substitution as field terminator. A null argument means resume search
577 * where the previous call terminated. This function destroys its
580 * Work from explicit source or from memory. While processing \: we
581 * overwrite the input. This way we do not have to maintain buffers for
582 * copies of input fields.
585 src = dst = ret = (string ? string : last);
592 tcpd_warn("rule ends in \":\"");
595 if (ch == '\\' && src[1] == ':')
604 /* chop_string - strip leading and trailing blanks from string */
606 static char *chop_string(string)
607 register char *string;
613 for (cp = string; *cp; cp++) {
620 return (start ? (end[1] = 0, start) : cp);