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