]>
git.saurik.com Git - apple/network_cmds.git/blob - ypserv.tproj/acl.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: acl.c,v 1.5 1997/08/05 09:26:55 maja Exp $ */
27 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Mats O Jansson
41 * 4. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char rcsid
[] = "$OpenBSD: acl.c,v 1.5 1997/08/05 09:26:55 maja Exp $";
61 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
74 static struct aclent
*acl_root
= NULL
;
76 static int acl_read_line(fp
, buf
, size
)
84 /* Read a line, and remove any comment, trim space */
87 while (fgets(buf
, size
, fp
)) {
90 if ((*c
== '#') || (*c
== '\n')) {
99 if ((isspace(l
) != 0) && (isspace(*c
) != 0)) {
102 l
= *c
++; *p
= l
; p
++;
109 if (isspace(*p
) != 0) {
117 } while (size
> 0 && !feof(fp
));
123 yp_acl_check_host(addr
)
124 struct in_addr
*addr
;
130 if ((addr
->s_addr
& p
->s_mask
) == p
->s_addr
) {
139 acl_add_net(allow
,addr
,mask
)
141 struct in_addr
*addr
,*mask
;
144 struct aclent
*acl
,*p
;
146 acl
= (struct aclent
*) malloc((unsigned) sizeof(struct aclent
));
150 acl
->s_addr
= addr
->s_addr
;
151 acl
->s_mask
= mask
->s_addr
;
153 if (acl_root
== NULL
) {
157 while (p
->next
!= NULL
)
165 acl_add_host(allow
,addr
)
167 struct in_addr
*addr
;
171 mask
.s_addr
= htonl(0xffffffff);
173 acl_add_net(allow
,addr
,&mask
);
180 char data_line
[1024];
187 struct in_addr addr
,mask
,*host_addr
;
188 struct hostent
*host
;
190 FILE *data_file
= NULL
;
193 data_file
= fopen(file
,"r");
196 while ((data_file
!= NULL
) &&
197 (acl_read_line(data_file
,data_line
,sizeof(data_line
)))) {
201 len
= strlen(data_line
);
206 p
= (char *) &data_line
;
208 /* State 1: Initial State */
211 addr
.s_addr
= mask
.s_addr
= 0;
213 k
= p
; i
= 0; /* save start of verb */
214 while ((*p
!= '\0') &&
215 (!isspace(*p
= tolower(*p
)))) {
223 if (strcmp(k
,"allow") == 0) {
228 if (strcmp(k
,"deny") == 0) {
233 if (state
== ACLS_INIT
) {
237 /* State 2: allow row */
238 /* State 3: deny row */
241 ((state
== ACLS_ALLOW
) || (state
== ACLS_DENY
))) {
243 k
= p
; i
= 0; /* save start of verb */
244 while ((*p
!= '\0') &&
245 (!isspace(*p
= tolower(*p
)))) {
253 if (strcmp(k
,"all") == 0) {
254 state
= state
+ ACLD_ALL
;
257 if (strcmp(k
,"host") == 0) {
258 state
= state
+ ACLD_HOST
;
261 if (strcmp(k
,"net") == 0) {
262 state
= state
+ ACLD_NET
;
265 if ((state
== ACLS_ALLOW
) || (state
== ACLS_DENY
)) {
271 if ((state
== ACLS_ALLOW
) || (state
== ACLS_DENY
)) {
275 /* State 4 & 5: all state, remove any comment */
278 ((state
== ACLS_ALLOW_ALL
) || (state
== ACLS_DENY_ALL
))) {
279 acl_add_net(allow
,&addr
,&mask
);
283 /* State 6 & 7: host line */
284 /* State 8 & 9: net line */
287 (state
>= ACLS_ALLOW_HOST
) && (state
<= ACLS_DENY_NET
)) {
289 k
= p
; i
= 0; /* save start of verb */
290 while ((*p
!= '\0') &&
291 (!isspace(*p
= tolower(*p
)))) {
299 if ((state
== ACLS_ALLOW_HOST
) || (state
== ACLS_DENY_HOST
)) {
300 if ((*k
>= '0') && (*k
<= '9')) {
301 (void)inet_aton(k
,&addr
);
302 acl_add_host(allow
,&addr
);
303 state
= state
+ ACLD_HOST_DONE
;
305 host
= gethostbyname(k
);
309 if (host
->h_addrtype
== AF_INET
) {
310 while ((host_addr
= (struct in_addr
*) *host
->h_addr_list
++)
312 acl_add_host(allow
,host_addr
);
314 state
= state
+ ACLD_HOST_DONE
;
319 if ((state
== ACLS_ALLOW_NET
) || (state
== ACLS_DENY_NET
)) {
320 if ((*k
>= '0') && (*k
<= '9')) {
321 (void)inet_aton(k
,&addr
);
322 state
= state
+ ACLD_NET_DONE
;
324 net
= getnetbyname(k
);
328 addr
.s_addr
= ntohl(net
->n_net
);
329 state
= state
+ ACLD_NET_DONE
;
336 if ((state
>= ACLS_ALLOW_HOST
) && (state
<= ACLS_DENY_NET
)) {
340 /* State 10 & 11: allow/deny host line */
343 ((state
== ACLS_ALLOW_HOST_DONE
) || (state
== ACLS_DENY_HOST_DONE
))) {
347 /* State 12 & 13: allow/deny net line */
350 ((state
== ACLS_ALLOW_NET_DONE
) || (state
== ACLS_DENY_NET_DONE
))) {
351 mask
.s_addr
= htonl(0xffffff00);
352 if (ntohl(addr
.s_addr
) < 0xc0000000) {
353 mask
.s_addr
= htonl(0xffff0000);
355 if (ntohl(addr
.s_addr
) < 0x80000000) {
356 mask
.s_addr
= htonl(0xff000000);
358 acl_add_net(allow
,&addr
,&mask
);
363 ((state
== ACLS_ALLOW_NET_DONE
) || (state
== ACLS_DENY_NET_DONE
))) {
365 k
= p
; i
= 0; /* save start of verb */
366 while ((*p
!= '\0') &&
367 (!isspace(*p
= tolower(*p
)))) {
375 if (strcmp(k
,"netmask") == 0) {
376 state
= state
+ ACLD_NET_MASK
;
379 if ((state
== ACLS_ALLOW_NET_DONE
) || (state
== ACLS_DENY_NET_DONE
)) {
380 state
= ACLE_NONETMASK
;
385 /* State 14 & 15: allow/deny net netmask line */
388 ((state
== ACLS_ALLOW_NET_MASK
) || (state
== ACLS_DENY_NET_MASK
))) {
390 k
= p
; i
= 0; /* save start of verb */
391 while ((*p
!= '\0') &&
392 (!isspace(*p
= tolower(*p
)))) {
400 if ((state
== ACLS_ALLOW_NET_MASK
) || (state
== ACLS_DENY_NET_MASK
)) {
401 if ((*k
>= '0') && (*k
<= '9')) {
402 (void)inet_aton(k
,&mask
);
403 state
= state
+ ACLD_NET_EOL
;
405 net
= getnetbyname(k
);
409 mask
.s_addr
= ntohl(net
->n_net
);
410 state
= state
+ ACLD_NET_EOL
;
417 if ((state
== ACLS_ALLOW_NET_MASK
) || (state
== ACLS_DENY_NET_MASK
)) {
421 /* State 16 & 17: allow/deny host line */
424 ((state
== ACLS_ALLOW_NET_EOL
) || (state
== ACLS_DENY_NET_EOL
))) {
425 acl_add_net(allow
,&addr
,&mask
);
431 fprintf(stderr
,"acl: excpected \"netmask\" missing at line %d\n",line_no
);
435 fprintf(stderr
,"acl: unknown network at line %d\n",line_no
);
439 fprintf(stderr
,"acl: unknown host at line %d\n",line_no
);
443 fprintf(stderr
,"acl: unknown verb at line %d\n",line_no
);
447 fprintf(stderr
,"acl: unknown secondary verb at line %d\n",line_no
);
451 fprintf(stderr
,"acl: unexpected end of line at line %d\n",line_no
);
457 fprintf(stderr
,"acl: unexpected state %d %s\n",state
,k
);
462 if (data_file
!= NULL
) {
463 (void)fflush(stderr
);
464 (void)fclose(data_file
);
467 /* Always add a last allow all if file don't exists or */
468 /* the file doesn't cover all cases. */
470 addr
.s_addr
= mask
.s_addr
= 0;
472 acl_add_net(allow
,&addr
,&mask
);
479 yp_acl_securenet(file
)
482 char data_line
[1024];
489 struct in_addr addr
,mask
;
491 FILE *data_file
= NULL
;
494 data_file
= fopen(file
,"r");
497 /* Always add a localhost allow first, to be compatable with sun */
499 addr
.s_addr
= htonl(0x7f000001);
500 mask
.s_addr
= htonl(0xffffffff);
502 acl_add_net(allow
,&addr
,&mask
);
504 while ((data_file
!= NULL
) &&
505 (acl_read_line(data_file
,data_line
,sizeof(data_line
)))) {
509 len
= strlen(data_line
);
514 p
= (char *) &data_line
;
516 /* State 1: Initial State */
519 addr
.s_addr
= mask
.s_addr
= 0;
521 k
= p
; i
= 0; /* save start of verb */
522 while ((*p
!= '\0') &&
523 (!isspace(*p
= tolower(*p
)))) {
529 state
= ACLS_ALLOW_NET_MASK
;
532 if (state
== ACLS_INIT
) {
536 if (state
== ACLS_ALLOW_NET_MASK
) {
538 if ((*k
>= '0') && (*k
<= '9')) {
539 (void)inet_aton(k
,&mask
);
540 state
= ACLS_ALLOW_NET
;
542 net
= getnetbyname(k
);
546 mask
.s_addr
= ntohl(net
->n_net
);
547 state
= ACLS_ALLOW_NET
;
551 k
= p
; i
= 0; /* save start of verb */
552 while ((*p
!= '\0') &&
553 (!isspace(*p
= tolower(*p
)))) {
562 if ((state
== ACLS_ALLOW_NET_MASK
)) {
566 if (state
== ACLS_ALLOW_NET
) {
568 if ((*k
>= '0') && (*k
<= '9')) {
569 (void)inet_aton(k
,&addr
);
570 state
= ACLS_ALLOW_NET_EOL
;
572 net
= getnetbyname(k
);
576 addr
.s_addr
= ntohl(net
->n_net
);
577 state
= ACLS_ALLOW_NET_EOL
;
582 if ((state
== ACLS_ALLOW_NET
)) {
586 if ((*p
== '\0') && (state
== ACLS_ALLOW_NET_EOL
)) {
587 acl_add_net(allow
,&addr
,&mask
);
594 fprintf(stderr
,"securenet: unknown network at line %d\n",line_no
);
598 fprintf(stderr
,"securenet: unexpected end of line at line %d\n",line_no
);
604 fprintf(stderr
,"securenet: unexpected state %d %s\n",state
,k
);
609 if (data_file
!= NULL
) {
610 (void)fflush(stderr
);
611 (void)fclose(data_file
);
613 /* Always add a last deny all if file exists */
615 addr
.s_addr
= mask
.s_addr
= 0;
617 acl_add_net(allow
,&addr
,&mask
);
621 /* Always add a last allow all if file don't exists */
623 addr
.s_addr
= mask
.s_addr
= 0;
625 acl_add_net(allow
,&addr
,&mask
);
636 while (acl_root
!= NULL
) {