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