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