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