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