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