2 * This module implements a simple access control language that is based on
3 * host (or domain) names, NIS (host) netgroup names, IP addresses (or
4 * network numbers) and daemon process names. When a match is found the
5 * search is terminated, and depending on whether PROCESS_OPTIONS is defined,
6 * a list of options is executed or an optional shell command is executed.
8 * Host and user names are looked up on demand, provided that suitable endpoint
9 * information is available as sockaddr_in structures or TLI netbufs. As a
10 * side effect, the pattern matching process may change the contents of
11 * request structure fields.
13 * Diagnostics are reported through syslog(3).
15 * Compile with -DNETGROUP if your library provides support for netgroups.
17 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
23 static char sccsid[] = "@(#) hosts_access.c 1.21 97/02/12 02:13:22";
26 /* System libraries. */
28 #include <sys/types.h>
30 typedef uint32_t u_int32_t;
32 #include <sys/param.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
50 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
59 extern jmp_buf tcpd_buf;
61 /* Delimiters for lists of daemons or clients. */
63 static char sep[] = ", \t\r\n";
65 /* Constants to be used in assignments only, not in comparisons... */
71 * These variables are globally visible so that they can be redirected in
75 char *hosts_allow_table = HOSTS_ALLOW;
76 char *hosts_deny_table = HOSTS_DENY;
77 int hosts_access_verbose = 0;
80 * In a long-running process, we are not at liberty to just go away.
83 int resident = (-1); /* -1, 0: unknown; +1: yes */
85 /* Forward declarations. */
87 static int table_match(char *table, struct request_info *request);
88 static int list_match(char *list, struct request_info *request,
89 int (*match_fn)(char *, struct request_info *));
90 static int server_match(char *tok, struct request_info *request);
91 static int client_match(char *tok, struct request_info *request);
92 static int host_match(char *tok, struct host_info *host);
93 static int string_match(char *tok, char *string);
94 static int masked_match(char *net_tok, char *mask_tok, char *string);
96 static int masked_match4(char *net_tok, char *mask_tok, char *string);
97 static int masked_match6(char *net_tok, char *mask_tok, char *string);
100 /* Size of logical line buffer. */
104 /* definition to be used from workarounds.c */
106 int yp_get_default_domain(char **);
109 /* hosts_access - host access control facility */
111 int hosts_access(request)
112 struct request_info *request;
117 * If the (daemon, client) pair is matched by an entry in the file
118 * /etc/hosts.allow, access is granted. Otherwise, if the (daemon,
119 * client) pair is matched by an entry in the file /etc/hosts.deny,
120 * access is denied. Otherwise, access is granted. A non-existent
121 * access-control file is treated as an empty file.
123 * After a rule has been matched, the optional language extensions may
124 * decide to grant or refuse service anyway. Or, while a rule is being
125 * processed, a serious error is found, and it seems better to play safe
126 * and deny service. All this is done by jumping back into the
127 * hosts_access() routine, bypassing the regular return from the
128 * table_match() function calls below.
133 verdict = setjmp(tcpd_buf);
135 return (verdict == AC_PERMIT);
136 if (table_match(hosts_allow_table, request))
138 if (table_match(hosts_deny_table, request))
143 /* table_match - match table entries with (daemon, client) pair */
145 static int table_match(table, request)
147 struct request_info *request;
150 char sv_list[BUFLEN]; /* becomes list of daemons */
151 char *cl_list; /* becomes list of clients */
152 char *sh_cmd; /* becomes optional shell command */
154 struct tcpd_context saved_context;
157 saved_context = tcpd_context; /* stupid compilers */
160 * Between the fopen() and fclose() calls, avoid jumps that may cause
161 * file descriptor leaks.
164 if ((fp = fopen(table, "r")) != 0) {
165 tcpd_context.file = table;
166 tcpd_context.line = 0;
167 while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) {
168 if (sv_list[strlen(sv_list) - 1] != '\n') {
169 tcpd_warn("missing newline or line too long");
172 /* Ignore anything after unescaped # character */
173 for (cp = strchr(sv_list, '#'); cp != NULL;) {
174 if (cp > sv_list && cp[-1] == '\\') {
175 cp = strchr(cp + 1, '#');
181 if (sv_list[strspn(sv_list, " \t\r\n")] == 0)
183 if ((cl_list = split_at(sv_list, ':')) == 0) {
184 tcpd_warn("missing \":\" separator");
187 sh_cmd = split_at(cl_list, ':');
188 match = list_match(sv_list, request, server_match)
189 && list_match(cl_list, request, client_match);
192 } else if (errno != ENOENT) {
193 tcpd_warn("cannot open %s: %m", table);
196 if (hosts_access_verbose > 1)
197 syslog(LOG_DEBUG, "matched: %s line %d",
198 tcpd_context.file, tcpd_context.line);
200 #ifdef PROCESS_OPTIONS
201 process_options(sh_cmd, request);
204 shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request));
208 tcpd_context = saved_context;
212 /* list_match - match a request against a list of patterns with exceptions */
214 static int list_match(char *list, struct request_info *request,
215 int (*match_fn)(char *, struct request_info *))
220 * Process tokens one at a time. We have exhausted all possible matches
221 * when we reach an "EXCEPT" token or the end of the list. If we do find
222 * a match, look for an "EXCEPT" list and recurse to determine whether
223 * the match is affected by any exceptions.
226 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
227 if (STR_EQ(tok, "EXCEPT")) /* EXCEPT: give up */
229 if (match_fn(tok, request)) { /* YES: look for exceptions */
230 while ((tok = strtok((char *) 0, sep)) && STR_NE(tok, "EXCEPT"))
232 return (tok == 0 || list_match((char *) 0, request, match_fn) == 0);
238 /* server_match - match server information */
240 static int server_match(tok, request)
242 struct request_info *request;
246 if ((host = split_at(tok + 1, '@')) == 0) { /* plain daemon */
247 return (string_match(tok, eval_daemon(request)));
248 } else { /* daemon@host */
249 return (string_match(tok, eval_daemon(request))
250 && host_match(host, request->server));
254 /* client_match - match client information */
256 static int client_match(tok, request)
258 struct request_info *request;
262 if ((host = split_at(tok + 1, '@')) == 0) { /* plain host */
263 return (host_match(tok, request->client));
264 } else { /* user@host */
265 return (host_match(host, request->client)
266 && string_match(tok, eval_user(request)));
270 /* hostfile_match - look up host patterns from file */
272 static int hostfile_match(path, host)
274 struct host_info *host;
280 if ((fp = fopen(path, "r")) != 0) {
281 while (fscanf(fp, "%s", tok) == 1 && !(match = host_match(tok, host)))
284 } else if (errno != ENOENT) {
285 tcpd_warn("open %s: %m", path);
290 /* host_match - match host name and/or address against pattern */
292 static int host_match(tok, host)
294 struct host_info *host;
299 * This code looks a little hairy because we want to avoid unnecessary
302 * The KNOWN pattern requires that both address AND name be known; some
303 * patterns are specific to host names or to host addresses; all other
304 * patterns are satisfied when either the address OR the name match.
307 if (tok[0] == '@') { /* netgroup: look it up */
309 static char *mydomain = 0;
311 yp_get_default_domain(&mydomain);
312 return (innetgr(tok + 1, eval_hostname(host), (char *) 0, mydomain));
314 tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */
317 } else if (tok[0] == '/') { /* /file hack */
318 return (hostfile_match(tok, host));
319 } else if (STR_EQ(tok, "KNOWN")) { /* check address and name */
320 char *name = eval_hostname(host);
321 return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name));
322 } else if (STR_EQ(tok, "LOCAL")) { /* local: no dots in name */
323 char *name = eval_hostname(host);
324 return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name));
325 } else if ((mask = split_at(tok, '/')) != 0) { /* net/mask */
326 return (masked_match(tok, mask, eval_hostaddr(host)));
327 } else { /* anything else */
328 return (string_match(tok, eval_hostaddr(host))
329 || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host))));
333 /* string_match - match string against pattern */
335 static int string_match(tok, string)
342 /* convert IPv4 mapped IPv6 address to IPv4 address */
343 if (STRN_EQ(string, "::ffff:", 7)
344 && dot_quad_addr(string + 7) != INADDR_NONE) {
348 if (tok[0] == '.') { /* suffix */
349 n = strlen(string) - strlen(tok);
350 return (n > 0 && STR_EQ(tok, string + n));
351 } else if (STR_EQ(tok, "ALL")) { /* all: match any */
353 } else if (STR_EQ(tok, "KNOWN")) { /* not unknown */
354 return (STR_NE(string, unknown));
355 } else if (tok[(n = strlen(tok)) - 1] == '.') { /* prefix */
356 return (STRN_EQ(tok, string, n));
357 } else { /* exact match */
359 struct addrinfo hints, *res;
360 struct sockaddr_in6 pat, addr;
365 if (*tok == '[' && tok[len - 1] == ']') {
368 memset(&hints, 0, sizeof(hints));
369 hints.ai_family = AF_INET6;
370 hints.ai_socktype = SOCK_STREAM;
371 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
372 if ((ret = getaddrinfo(tok + 1, NULL, &hints, &res)) == 0) {
373 memcpy(&pat, res->ai_addr, sizeof(pat));
377 if (ret != 0 || getaddrinfo(string, NULL, &hints, &res) != 0)
379 memcpy(&addr, res->ai_addr, sizeof(addr));
381 if (pat.sin6_scope_id != 0 &&
382 addr.sin6_scope_id != pat.sin6_scope_id)
384 return (!memcmp(&pat.sin6_addr, &addr.sin6_addr,
385 sizeof(struct in6_addr)));
389 return (STR_EQ(tok, string));
393 /* masked_match - match address against netnumber/netmask */
396 static int masked_match(net_tok, mask_tok, string)
401 return (masked_match4(net_tok, mask_tok, string) ||
402 masked_match6(net_tok, mask_tok, string));
405 static int masked_match4(net_tok, mask_tok, string)
407 static int masked_match(net_tok, mask_tok, string)
424 * Disallow forms other than dotted quad: the treatment that inet_addr()
425 * gives to forms with less than four components is inconsistent with the
426 * access control language. John P. Rouillard <rouilj@cs.umb.edu>.
429 if ((addr = dot_quad_addr(string)) == INADDR_NONE)
431 if ((net = dot_quad_addr(net_tok)) == INADDR_NONE
432 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) {
434 tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok);
436 return (NO); /* not tcpd_jump() */
438 return ((addr & mask) == net);
442 static int masked_match6(net_tok, mask_tok, string)
447 struct addrinfo hints, *res;
448 struct sockaddr_in6 net, addr;
450 int len, mask_len, i = 0;
453 memset(&hints, 0, sizeof(hints));
454 hints.ai_family = AF_INET6;
455 hints.ai_socktype = SOCK_STREAM;
456 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
457 if (getaddrinfo(string, NULL, &hints, &res) != 0)
459 memcpy(&addr, res->ai_addr, sizeof(addr));
462 if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
463 if ((*(u_int32_t *)&net.sin6_addr.s6_addr[12] = dot_quad_addr(net_tok)) == INADDR_NONE
464 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE)
466 return ((*(u_int32_t *)&addr.sin6_addr.s6_addr[12] & mask) == *(u_int32_t *)&net.sin6_addr.s6_addr[12]);
469 /* match IPv6 address against netnumber/prefixlen */
470 len = strlen(net_tok);
471 if (*net_tok != '[' || net_tok[len - 1] != ']')
473 ch = net_tok[len - 1];
474 net_tok[len - 1] = '\0';
475 if (getaddrinfo(net_tok + 1, NULL, &hints, &res) != 0) {
476 net_tok[len - 1] = ch;
479 memcpy(&net, res->ai_addr, sizeof(net));
481 net_tok[len - 1] = ch;
482 if ((mask_len = atoi(mask_tok)) < 0 || mask_len > 128)
485 if (net.sin6_scope_id != 0 && addr.sin6_scope_id != net.sin6_scope_id)
487 while (mask_len > 0) {
489 mask = htonl(~(0xffffffff >> mask_len));
490 if ((*(u_int32_t *)&addr.sin6_addr.s6_addr[i] & mask) != (*(u_int32_t *)&net.sin6_addr.s6_addr[i] & mask))
494 if (*(u_int32_t *)&addr.sin6_addr.s6_addr[i] != *(u_int32_t *)&net.sin6_addr.s6_addr[i])