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