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