Home | History | Annotate | Line # | Download | only in libwrap
hosts_access.c revision 1.5
      1 /*	$NetBSD: hosts_access.c,v 1.5 1999/01/18 20:21:19 christos Exp $	*/
      2 
      3  /*
      4   * This module implements a simple access control language that is based on
      5   * host (or domain) names, NIS (host) netgroup names, IP addresses (or
      6   * network numbers) and daemon process names. When a match is found the
      7   * search is terminated, and depending on whether PROCESS_OPTIONS is defined,
      8   * a list of options is executed or an optional shell command is executed.
      9   *
     10   * Host and user names are looked up on demand, provided that suitable endpoint
     11   * information is available as sockaddr_in structures or TLI netbufs. As a
     12   * side effect, the pattern matching process may change the contents of
     13   * request structure fields.
     14   *
     15   * Diagnostics are reported through syslog(3).
     16   *
     17   * Compile with -DNETGROUP if your library provides support for netgroups.
     18   *
     19   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
     20   */
     21 
     22 #include <sys/cdefs.h>
     23 #ifndef lint
     24 #if 0
     25 static char sccsid[] = "@(#) hosts_access.c 1.20 96/02/11 17:01:27";
     26 #else
     27 __RCSID("$NetBSD: hosts_access.c,v 1.5 1999/01/18 20:21:19 christos Exp $");
     28 #endif
     29 #endif
     30 
     31 /* System libraries. */
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 #include <netinet/in.h>
     36 #include <arpa/inet.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <syslog.h>
     40 #include <ctype.h>
     41 #include <errno.h>
     42 #include <setjmp.h>
     43 #include <string.h>
     44 #include <netdb.h>
     45 #ifdef  NETGROUP
     46 #include <netgroup.h>
     47 #include <rpcsvc/ypclnt.h>
     48 #endif
     49 
     50 extern int errno;
     51 
     52 #ifndef	INADDR_NONE
     53 #define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
     54 #endif
     55 
     56 /* Local stuff. */
     57 
     58 #include "tcpd.h"
     59 
     60 /* Error handling. */
     61 
     62 extern jmp_buf tcpd_buf;
     63 
     64 /* Delimiters for lists of daemons or clients. */
     65 
     66 static char sep[] = ", \t\r\n";
     67 
     68 /* Constants to be used in assignments only, not in comparisons... */
     69 
     70 #define	YES		1
     71 #define	NO		0
     72 
     73  /*
     74   * These variables are globally visible so that they can be redirected in
     75   * verification mode.
     76   */
     77 
     78 char   *hosts_allow_table = HOSTS_ALLOW;
     79 char   *hosts_deny_table = HOSTS_DENY;
     80 int     hosts_access_verbose = 0;
     81 
     82  /*
     83   * In a long-running process, we are not at liberty to just go away.
     84   */
     85 
     86 int     resident = (-1);		/* -1, 0: unknown; +1: yes */
     87 
     88 /* Forward declarations. */
     89 
     90 static int table_match __P((char *, struct request_info *));
     91 static int list_match __P((char *, struct request_info *,
     92     int (*)(char *, struct request_info *)));
     93 static int server_match __P((char *, struct request_info *));
     94 static int client_match __P((char *, struct request_info *));
     95 static int host_match __P((char *, struct host_info *));
     96 static int rbl_match __P((char *, char *));
     97 static int string_match __P((char *, char *));
     98 static int masked_match __P((char *, char *, char *));
     99 
    100 /* Size of logical line buffer. */
    101 
    102 #define	BUFLEN 2048
    103 
    104 /* hosts_access - host access control facility */
    105 
    106 int     hosts_access(request)
    107 struct request_info *request;
    108 {
    109     int     verdict;
    110 
    111     /*
    112      * If the (daemon, client) pair is matched by an entry in the file
    113      * /etc/hosts.allow, access is granted. Otherwise, if the (daemon,
    114      * client) pair is matched by an entry in the file /etc/hosts.deny,
    115      * access is denied. Otherwise, access is granted. A non-existent
    116      * access-control file is treated as an empty file.
    117      *
    118      * After a rule has been matched, the optional language extensions may
    119      * decide to grant or refuse service anyway. Or, while a rule is being
    120      * processed, a serious error is found, and it seems better to play safe
    121      * and deny service. All this is done by jumping back into the
    122      * hosts_access() routine, bypassing the regular return from the
    123      * table_match() function calls below.
    124      */
    125 
    126     if (resident <= 0)
    127 	resident++;
    128     if ((verdict = setjmp(tcpd_buf)) != 0)
    129 	return (verdict == AC_PERMIT);
    130     if (table_match(hosts_allow_table, request))
    131 	return (YES);
    132     if (table_match(hosts_deny_table, request))
    133 	return (NO);
    134     return (YES);
    135 }
    136 
    137 /* table_match - match table entries with (daemon, client) pair */
    138 
    139 static int table_match(table, request)
    140 char   *table;
    141 struct request_info *request;
    142 {
    143     FILE   *fp;
    144     char    sv_list[BUFLEN];		/* becomes list of daemons */
    145     char   *cl_list;			/* becomes list of clients */
    146     char   *sh_cmd = NULL;		/* becomes optional shell command */
    147     int     match = NO;
    148     struct tcpd_context saved_context;
    149 
    150     saved_context = tcpd_context;		/* stupid compilers */
    151 
    152     /*
    153      * Between the fopen() and fclose() calls, avoid jumps that may cause
    154      * file descriptor leaks.
    155      */
    156 
    157     if ((fp = fopen(table, "r")) != 0) {
    158 	tcpd_context.file = table;
    159 	tcpd_context.line = 0;
    160 	while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) {
    161 	    if (sv_list[strlen(sv_list) - 1] != '\n') {
    162 		tcpd_warn("missing newline or line too long");
    163 		continue;
    164 	    }
    165 	    if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0)
    166 		continue;
    167 	    if ((cl_list = split_at(sv_list, ':')) == 0) {
    168 		tcpd_warn("missing \":\" separator");
    169 		continue;
    170 	    }
    171 	    sh_cmd = split_at(cl_list, ':');
    172 	    match = list_match(sv_list, request, server_match)
    173 		&& list_match(cl_list, request, client_match);
    174 	}
    175 	(void) fclose(fp);
    176     } else if (errno != ENOENT) {
    177 	tcpd_warn("cannot open %s: %m", table);
    178     }
    179     if (match) {
    180 	if (hosts_access_verbose > 1)
    181 	    syslog(LOG_DEBUG, "matched:  %s line %d",
    182 		   tcpd_context.file, tcpd_context.line);
    183 	if (sh_cmd) {
    184 #ifdef PROCESS_OPTIONS
    185 	    process_options(sh_cmd, request);
    186 #else
    187 	    char    cmd[BUFSIZ];
    188 	    shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request));
    189 #endif
    190 	}
    191     }
    192     tcpd_context = saved_context;
    193     return (match);
    194 }
    195 
    196 /* list_match - match a request against a list of patterns with exceptions */
    197 
    198 static int list_match(list, request, match_fn)
    199 char   *list;
    200 struct request_info *request;
    201 int   (*match_fn) __P((char *, struct request_info *));
    202 {
    203     char   *tok;
    204 
    205     /*
    206      * Process tokens one at a time. We have exhausted all possible matches
    207      * when we reach an "EXCEPT" token or the end of the list. If we do find
    208      * a match, look for an "EXCEPT" list and recurse to determine whether
    209      * the match is affected by any exceptions.
    210      */
    211 
    212     for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
    213 	if (STR_EQ(tok, "EXCEPT"))		/* EXCEPT: give up */
    214 	    return (NO);
    215 	if (match_fn(tok, request)) {		/* YES: look for exceptions */
    216 	    while ((tok = strtok((char *) 0, sep)) && STR_NE(tok, "EXCEPT"))
    217 		 /* VOID */ ;
    218 	    return (tok == 0 || list_match((char *) 0, request, match_fn) == 0);
    219 	}
    220     }
    221     return (NO);
    222 }
    223 
    224 /* server_match - match server information */
    225 
    226 static int server_match(tok, request)
    227 char   *tok;
    228 struct request_info *request;
    229 {
    230     char   *host;
    231 
    232     if ((host = split_at(tok + 1, '@')) == 0) {	/* plain daemon */
    233 	return (string_match(tok, eval_daemon(request)));
    234     } else {					/* daemon@host */
    235 	return (string_match(tok, eval_daemon(request))
    236 		&& host_match(host, request->server));
    237     }
    238 }
    239 
    240 /* client_match - match client information */
    241 
    242 static int client_match(tok, request)
    243 char   *tok;
    244 struct request_info *request;
    245 {
    246     char   *host;
    247 
    248     if ((host = split_at(tok + 1, '@')) == 0) {	/* plain host */
    249 	return (host_match(tok, request->client));
    250     } else {					/* user@host */
    251 	return (host_match(host, request->client)
    252 		&& string_match(tok, eval_user(request)));
    253     }
    254 }
    255 
    256 /* host_match - match host name and/or address against pattern */
    257 
    258 static int host_match(tok, host)
    259 char   *tok;
    260 struct host_info *host;
    261 {
    262     char   *mask;
    263 
    264     /*
    265      * This code looks a little hairy because we want to avoid unnecessary
    266      * hostname lookups.
    267      *
    268      * The KNOWN pattern requires that both address AND name be known; some
    269      * patterns are specific to host names or to host addresses; all other
    270      * patterns are satisfied when either the address OR the name match.
    271      */
    272 
    273     if (tok[0] == '@') {			/* netgroup: look it up */
    274 #ifdef  NETGROUP
    275 	static char *mydomain = 0;
    276 	if (mydomain == 0)
    277 	    yp_get_default_domain(&mydomain);
    278 	return (innetgr(tok + 1, eval_hostname(host), (char *) 0, mydomain));
    279 #else
    280 	tcpd_warn("netgroup support is disabled");	/* not tcpd_jump() */
    281 	return (NO);
    282 #endif
    283     } else if (STR_EQ(tok, "KNOWN")) {		/* check address and name */
    284 	char   *name = eval_hostname(host);
    285 	return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name));
    286     } else if (STR_EQ(tok, "LOCAL")) {		/* local: no dots in name */
    287 	char   *name = eval_hostname(host);
    288 	return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name));
    289     } else if (strncmp(tok, "{RBL}.", 6) == 0) { /* RBL lookup in domain */
    290 	return rbl_match(tok+6, eval_hostaddr(host));
    291     } else if ((mask = split_at(tok, '/')) != 0) {	/* net/mask */
    292 	return (masked_match(tok, mask, eval_hostaddr(host)));
    293     } else {					/* anything else */
    294 	return (string_match(tok, eval_hostaddr(host))
    295 	    || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host))));
    296     }
    297 }
    298 
    299 /* rbl_match() - match host by looking up in RBL domain */
    300 
    301 static int rbl_match(rbl_domain, rbl_hostaddr)
    302 char   *rbl_domain;				/* RBL domain */
    303 char   *rbl_hostaddr;				/* hostaddr */
    304 {
    305     char *rbl_name;
    306     unsigned long host_address;
    307     int ret = NO;
    308     size_t len = strlen(rbl_domain) + (4 * 4) + 2;
    309 
    310     if ((host_address = dot_quad_addr(rbl_hostaddr)) == INADDR_NONE) {
    311 	tcpd_warn("unable to convert %s to address", rbl_hostaddr);
    312 	return (NO);
    313     }
    314     /*  construct the rbl name to look up */
    315     if ((rbl_name = malloc(len)) == NULL) {
    316 	tcpd_jump("not enough memory to build RBL name for %s in %s", rbl_hostaddr, rbl_domain);
    317 	/* NOTREACHED */
    318     }
    319     snprintf(rbl_name, len, "%u.%u.%u.%u.%s",
    320 	    (unsigned int) ((host_address) & 0xff),
    321 	    (unsigned int) ((host_address >> 8) & 0xff),
    322 	    (unsigned int) ((host_address >> 16) & 0xff),
    323 	    (unsigned int) ((host_address >> 24) & 0xff),
    324 	    rbl_domain);
    325     /* look it up */
    326     if (gethostbyname(rbl_name) != NULL) {
    327 	/* successful lookup - they're on the RBL list */
    328 	ret = YES;
    329     }
    330     free(rbl_name);
    331 
    332     return ret;
    333 }
    334 
    335 /* string_match - match string against pattern */
    336 
    337 static int string_match(tok, string)
    338 char   *tok;
    339 char   *string;
    340 {
    341     int     n;
    342 
    343     if (tok[0] == '.') {			/* suffix */
    344 	n = strlen(string) - strlen(tok);
    345 	return (n > 0 && STR_EQ(tok, string + n));
    346     } else if (STR_EQ(tok, "ALL")) {		/* all: match any */
    347 	return (YES);
    348     } else if (STR_EQ(tok, "KNOWN")) {		/* not unknown */
    349 	return (STR_NE(string, unknown));
    350     } else if (tok[(n = strlen(tok)) - 1] == '.') {	/* prefix */
    351 	return (STRN_EQ(tok, string, n));
    352     } else {					/* exact match */
    353 	return (STR_EQ(tok, string));
    354     }
    355 }
    356 
    357 /* masked_match - match address against netnumber/netmask */
    358 
    359 static int masked_match(net_tok, mask_tok, string)
    360 char   *net_tok;
    361 char   *mask_tok;
    362 char   *string;
    363 {
    364     unsigned long net;
    365     unsigned long mask;
    366     unsigned long addr;
    367 
    368     /*
    369      * Disallow forms other than dotted quad: the treatment that inet_addr()
    370      * gives to forms with less than four components is inconsistent with the
    371      * access control language. John P. Rouillard <rouilj (at) cs.umb.edu>.
    372      */
    373 
    374     if ((addr = dot_quad_addr(string)) == INADDR_NONE)
    375 	return (NO);
    376     if ((net = dot_quad_addr(net_tok)) == INADDR_NONE
    377 	|| (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) {
    378 	tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok);
    379 	return (NO);				/* not tcpd_jump() */
    380     }
    381     return ((addr & mask) == net);
    382 }
    383