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