2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
9 * This module implements a simple access control language that is based on
10 * host (or domain) names, NIS (host) netgroup names, IP addresses (or
11 * network numbers) and daemon process names. When a match is found the
12 * search is terminated, and depending on whether PROCESS_OPTIONS is defined,
13 * a list of options is executed or an optional shell command is executed.
15 * Host and user names are looked up on demand, provided that suitable endpoint
16 * information is available as sockaddr_in structures or TLI netbufs. As a
17 * side effect, the pattern matching process may change the contents of
18 * request structure fields.
20 * Diagnostics are reported through syslog(3).
22 * Compile with -DNETGROUP if your library provides support for netgroups.
24 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
28 static char sccsid
[] = "@(#) hosts_access.c 1.21 97/02/12 02:13:22";
31 /* System libraries. */
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <rpcsvc/ypclnt.h>
52 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
61 extern jmp_buf tcpd_buf
;
63 /* Delimiters for lists of daemons or clients. */
65 static char sep
[] = ", \t\r\n";
67 /* Constants to be used in assignments only, not in comparisons... */
73 * These variables are globally visible so that they can be redirected in
77 char *hosts_allow_table
= HOSTS_ALLOW
;
78 char *hosts_deny_table
= HOSTS_DENY
;
79 int hosts_access_verbose
= 0;
82 * In a long-running process, we are not at liberty to just go away.
85 int resident
= (-1); /* -1, 0: unknown; +1: yes */
87 /* Forward declarations. */
89 static int table_match();
90 static int list_match();
91 static int server_match();
92 static int client_match();
93 static int host_match();
94 static int string_match();
95 static int masked_match();
97 static void ipv6_mask();
100 /* Size of logical line buffer. */
104 /* hosts_access - host access control facility */
106 int hosts_access(request
)
107 struct request_info
*request
;
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.
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.
128 verdict
= setjmp(tcpd_buf
);
130 return (verdict
== AC_PERMIT
);
131 if (table_match(hosts_allow_table
, request
))
133 if (table_match(hosts_deny_table
, request
))
138 /* table_match - match table entries with (daemon, client) pair */
140 static int table_match(table
, request
)
142 struct request_info
*request
;
145 char sv_list
[BUFLEN
]; /* becomes list of daemons */
146 char *cl_list
; /* becomes list of clients */
147 char *sh_cmd
; /* becomes optional shell command */
149 struct tcpd_context saved_context
;
151 saved_context
= tcpd_context
; /* stupid compilers */
154 * Between the fopen() and fclose() calls, avoid jumps that may cause
155 * file descriptor leaks.
158 if ((fp
= fopen(table
, "r")) != 0) {
159 tcpd_context
.file
= table
;
160 tcpd_context
.line
= 0;
161 while (match
== NO
&& xgets(sv_list
, sizeof(sv_list
), fp
) != 0) {
162 if (sv_list
[strlen(sv_list
) - 1] != '\n') {
163 tcpd_warn("missing newline or line too long");
166 if (sv_list
[0] == '#' || sv_list
[strspn(sv_list
, " \t\r\n")] == 0)
168 if ((cl_list
= split_at(skip_ipv6_addrs(sv_list
), ':')) == 0) {
169 tcpd_warn("missing \":\" separator");
172 sh_cmd
= split_at(skip_ipv6_addrs(cl_list
), ':');
173 match
= list_match(sv_list
, request
, server_match
)
174 && list_match(cl_list
, request
, client_match
);
177 } else if (errno
!= ENOENT
) {
178 tcpd_warn("cannot open %s: %m", table
);
181 if (hosts_access_verbose
> 1)
182 syslog(LOG_DEBUG
, "matched: %s line %d",
183 tcpd_context
.file
, tcpd_context
.line
);
185 #ifdef PROCESS_OPTIONS
186 process_options(sh_cmd
, request
);
189 shell_cmd(percent_x(cmd
, sizeof(cmd
), sh_cmd
, request
));
193 tcpd_context
= saved_context
;
197 /* list_match - match a request against a list of patterns with exceptions */
199 static int list_match(list
, request
, match_fn
)
201 struct request_info
*request
;
207 * Process tokens one at a time. We have exhausted all possible matches
208 * when we reach an "EXCEPT" token or the end of the list. If we do find
209 * a match, look for an "EXCEPT" list and recurse to determine whether
210 * the match is affected by any exceptions.
213 for (tok
= strtok(list
, sep
); tok
!= 0; tok
= strtok((char *) 0, sep
)) {
214 if (STR_EQ(tok
, "EXCEPT")) /* EXCEPT: give up */
216 if (match_fn(tok
, request
)) { /* YES: look for exceptions */
217 while ((tok
= strtok((char *) 0, sep
)) && STR_NE(tok
, "EXCEPT"))
219 return (tok
== 0 || list_match((char *) 0, request
, match_fn
) == 0);
225 /* server_match - match server information */
227 static int server_match(tok
, request
)
229 struct request_info
*request
;
233 if ((host
= split_at(tok
+ 1, '@')) == 0) { /* plain daemon */
234 return (string_match(tok
, eval_daemon(request
)));
235 } else { /* daemon@host */
236 return (string_match(tok
, eval_daemon(request
))
237 && host_match(host
, request
->server
));
241 /* client_match - match client information */
243 static int client_match(tok
, request
)
245 struct request_info
*request
;
249 if ((host
= split_at(tok
+ 1, '@')) == 0) { /* plain host */
250 return (host_match(tok
, request
->client
));
251 } else { /* user@host */
252 return (host_match(host
, request
->client
)
253 && string_match(tok
, eval_user(request
)));
257 /* host_match - match host name and/or address against pattern */
259 static int host_match(tok
, host
)
261 struct host_info
*host
;
266 * This code looks a little hairy because we want to avoid unnecessary
269 * The KNOWN pattern requires that both address AND name be known; some
270 * patterns are specific to host names or to host addresses; all other
271 * patterns are satisfied when either the address OR the name match.
274 if (tok
[0] == '@') { /* netgroup: look it up */
276 static char *mydomain
= 0;
278 yp_get_default_domain(&mydomain
);
279 return (innetgr(tok
+ 1, eval_hostname(host
), (char *) 0, mydomain
));
281 tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */
284 } else if (STR_EQ(tok
, "KNOWN")) { /* check address and name */
285 char *name
= eval_hostname(host
);
286 return (STR_NE(eval_hostaddr(host
), unknown
) && HOSTNAME_KNOWN(name
));
287 } else if (STR_EQ(tok
, "LOCAL")) { /* local: no dots in name */
288 char *name
= eval_hostname(host
);
289 return (strchr(name
, '.') == 0 && HOSTNAME_KNOWN(name
));
291 } else if (tok
[0] == '[') { /* IPv6 address */
292 struct in6_addr in6
, hostin6
, *hip
;
295 int mask
= IPV6_ABITS
;
298 * In some cases we don't get the sockaddr, only the addr.
299 * We use inet_pton to convert it to its binary representation
300 * and match against that.
302 if (host
->sin
== NULL
) {
303 if (host
->addr
== NULL
||
304 inet_pton(AF_INET6
, host
->addr
, &hostin6
) != 1) {
309 if (SGFAM(host
->sin
) != AF_INET6
)
311 hip
= &host
->sin
->sg_sin6
.sin6_addr
;
314 if (cbr
= strchr(tok
, ']'))
318 * A /nnn prefix specifies how many bits of the address we
321 if (slash
= strchr(tok
, '/')) {
323 mask
= atoi(slash
+1);
324 if (mask
< 0 || mask
> IPV6_ABITS
) {
325 tcpd_warn("bad IP6 prefix specification");
328 /* Copy, because we need to modify it below */
329 if (host
->sin
!= NULL
) {
330 hostin6
= host
->sin
->sg_sin6
.sin6_addr
;
335 if (cbr
== NULL
|| inet_pton(AF_INET6
, tok
+1, &in6
) != 1) {
336 tcpd_warn("bad IP6 address specification");
340 * Zero the bits we're not interested in in both addresses
341 * then compare. Note that we take a copy of the host info
344 if (mask
!= IPV6_ABITS
) {
345 ipv6_mask(&in6
, mask
);
346 ipv6_mask(hip
, mask
);
348 if (memcmp(&in6
, hip
, sizeof(in6
)) == 0)
352 } else if ((mask
= split_at(tok
, '/')) != 0) { /* net/mask */
353 return (masked_match(tok
, mask
, eval_hostaddr(host
)));
354 } else { /* anything else */
355 return (string_match(tok
, eval_hostaddr(host
))
356 || (NOT_INADDR(tok
) && string_match(tok
, eval_hostname(host
))));
360 /* string_match - match string against pattern */
362 static int string_match(tok
, string
)
368 if (tok
[0] == '.') { /* suffix */
369 n
= strlen(string
) - strlen(tok
);
370 return (n
> 0 && STR_EQ(tok
, string
+ n
));
371 } else if (STR_EQ(tok
, "ALL")) { /* all: match any */
373 } else if (STR_EQ(tok
, "KNOWN")) { /* not unknown */
374 return (STR_NE(string
, unknown
));
375 } else if (tok
[(n
= strlen(tok
)) - 1] == '.') { /* prefix */
376 return (STRN_EQ(tok
, string
, n
));
377 } else { /* exact match */
378 return (STR_EQ(tok
, string
));
382 /* masked_match - match address against netnumber/netmask */
384 static int masked_match(net_tok
, mask_tok
, string
)
394 * Disallow forms other than dotted quad: the treatment that inet_addr()
395 * gives to forms with less than four components is inconsistent with the
396 * access control language. John P. Rouillard <rouilj@cs.umb.edu>.
399 if ((addr
= dot_quad_addr(string
)) == INADDR_NONE
)
401 if ((net
= dot_quad_addr(net_tok
)) == INADDR_NONE
402 || (mask
= dot_quad_addr(mask_tok
)) == INADDR_NONE
) {
403 tcpd_warn("bad net/mask expression: %s/%s", net_tok
, mask_tok
);
404 return (NO
); /* not tcpd_jump() */
406 return ((addr
& mask
) == net
);
411 * Function that zeros all but the first "maskbits" bits of the IPV6 address
412 * This function can be made generic by specifying an address length as
413 * extra parameter. (So Wietse can implement 1.2.3.4/16)
415 static void ipv6_mask(in6p
, maskbits
)
416 struct in6_addr
*in6p
;
419 uchar_t
*p
= (uchar_t
*) in6p
;
421 if (maskbits
< 0 || maskbits
>= IPV6_ABITS
)
428 *p
++ &= 0xff << (8 - maskbits
);
430 while (p
< (((uchar_t
*) in6p
)) + sizeof(*in6p
))