]>
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 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $OpenBSD: acl.c,v 1.5 1997/08/05 09:26:55 maja Exp $ */
26 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Mats O Jansson
40 * 4. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 static char rcsid
[] = "$OpenBSD: acl.c,v 1.5 1997/08/05 09:26:55 maja Exp $";
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
73 static struct aclent
*acl_root
= NULL
;
75 static int acl_read_line(fp
, buf
, size
)
83 /* Read a line, and remove any comment, trim space */
86 while (fgets(buf
, size
, fp
)) {
89 if ((*c
== '#') || (*c
== '\n')) {
98 if ((isspace(l
) != 0) && (isspace(*c
) != 0)) {
101 l
= *c
++; *p
= l
; p
++;
108 if (isspace(*p
) != 0) {
116 } while (size
> 0 && !feof(fp
));
122 yp_acl_check_host(addr
)
123 struct in_addr
*addr
;
129 if ((addr
->s_addr
& p
->s_mask
) == p
->s_addr
) {
138 acl_add_net(allow
,addr
,mask
)
140 struct in_addr
*addr
,*mask
;
143 struct aclent
*acl
,*p
;
145 acl
= (struct aclent
*) malloc((unsigned) sizeof(struct aclent
));
149 acl
->s_addr
= addr
->s_addr
;
150 acl
->s_mask
= mask
->s_addr
;
152 if (acl_root
== NULL
) {
156 while (p
->next
!= NULL
)
164 acl_add_host(allow
,addr
)
166 struct in_addr
*addr
;
170 mask
.s_addr
= htonl(0xffffffff);
172 acl_add_net(allow
,addr
,&mask
);
179 char data_line
[1024];
186 struct in_addr addr
,mask
,*host_addr
;
187 struct hostent
*host
;
189 FILE *data_file
= NULL
;
192 data_file
= fopen(file
,"r");
195 while ((data_file
!= NULL
) &&
196 (acl_read_line(data_file
,data_line
,sizeof(data_line
)))) {
200 len
= strlen(data_line
);
205 p
= (char *) &data_line
;
207 /* State 1: Initial State */
210 addr
.s_addr
= mask
.s_addr
= 0;
212 k
= p
; i
= 0; /* save start of verb */
213 while ((*p
!= '\0') &&
214 (!isspace(*p
= tolower(*p
)))) {
222 if (strcmp(k
,"allow") == 0) {
227 if (strcmp(k
,"deny") == 0) {
232 if (state
== ACLS_INIT
) {
236 /* State 2: allow row */
237 /* State 3: deny row */
240 ((state
== ACLS_ALLOW
) || (state
== ACLS_DENY
))) {
242 k
= p
; i
= 0; /* save start of verb */
243 while ((*p
!= '\0') &&
244 (!isspace(*p
= tolower(*p
)))) {
252 if (strcmp(k
,"all") == 0) {
253 state
= state
+ ACLD_ALL
;
256 if (strcmp(k
,"host") == 0) {
257 state
= state
+ ACLD_HOST
;
260 if (strcmp(k
,"net") == 0) {
261 state
= state
+ ACLD_NET
;
264 if ((state
== ACLS_ALLOW
) || (state
== ACLS_DENY
)) {
270 if ((state
== ACLS_ALLOW
) || (state
== ACLS_DENY
)) {
274 /* State 4 & 5: all state, remove any comment */
277 ((state
== ACLS_ALLOW_ALL
) || (state
== ACLS_DENY_ALL
))) {
278 acl_add_net(allow
,&addr
,&mask
);
282 /* State 6 & 7: host line */
283 /* State 8 & 9: net line */
286 (state
>= ACLS_ALLOW_HOST
) && (state
<= ACLS_DENY_NET
)) {
288 k
= p
; i
= 0; /* save start of verb */
289 while ((*p
!= '\0') &&
290 (!isspace(*p
= tolower(*p
)))) {
298 if ((state
== ACLS_ALLOW_HOST
) || (state
== ACLS_DENY_HOST
)) {
299 if ((*k
>= '0') && (*k
<= '9')) {
300 (void)inet_aton(k
,&addr
);
301 acl_add_host(allow
,&addr
);
302 state
= state
+ ACLD_HOST_DONE
;
304 host
= gethostbyname(k
);
308 if (host
->h_addrtype
== AF_INET
) {
309 while ((host_addr
= (struct in_addr
*) *host
->h_addr_list
++)
311 acl_add_host(allow
,host_addr
);
313 state
= state
+ ACLD_HOST_DONE
;
318 if ((state
== ACLS_ALLOW_NET
) || (state
== ACLS_DENY_NET
)) {
319 if ((*k
>= '0') && (*k
<= '9')) {
320 (void)inet_aton(k
,&addr
);
321 state
= state
+ ACLD_NET_DONE
;
323 net
= getnetbyname(k
);
327 addr
.s_addr
= ntohl(net
->n_net
);
328 state
= state
+ ACLD_NET_DONE
;
335 if ((state
>= ACLS_ALLOW_HOST
) && (state
<= ACLS_DENY_NET
)) {
339 /* State 10 & 11: allow/deny host line */
342 ((state
== ACLS_ALLOW_HOST_DONE
) || (state
== ACLS_DENY_HOST_DONE
))) {
346 /* State 12 & 13: allow/deny net line */
349 ((state
== ACLS_ALLOW_NET_DONE
) || (state
== ACLS_DENY_NET_DONE
))) {
350 mask
.s_addr
= htonl(0xffffff00);
351 if (ntohl(addr
.s_addr
) < 0xc0000000) {
352 mask
.s_addr
= htonl(0xffff0000);
354 if (ntohl(addr
.s_addr
) < 0x80000000) {
355 mask
.s_addr
= htonl(0xff000000);
357 acl_add_net(allow
,&addr
,&mask
);
362 ((state
== ACLS_ALLOW_NET_DONE
) || (state
== ACLS_DENY_NET_DONE
))) {
364 k
= p
; i
= 0; /* save start of verb */
365 while ((*p
!= '\0') &&
366 (!isspace(*p
= tolower(*p
)))) {
374 if (strcmp(k
,"netmask") == 0) {
375 state
= state
+ ACLD_NET_MASK
;
378 if ((state
== ACLS_ALLOW_NET_DONE
) || (state
== ACLS_DENY_NET_DONE
)) {
379 state
= ACLE_NONETMASK
;
384 /* State 14 & 15: allow/deny net netmask line */
387 ((state
== ACLS_ALLOW_NET_MASK
) || (state
== ACLS_DENY_NET_MASK
))) {
389 k
= p
; i
= 0; /* save start of verb */
390 while ((*p
!= '\0') &&
391 (!isspace(*p
= tolower(*p
)))) {
399 if ((state
== ACLS_ALLOW_NET_MASK
) || (state
== ACLS_DENY_NET_MASK
)) {
400 if ((*k
>= '0') && (*k
<= '9')) {
401 (void)inet_aton(k
,&mask
);
402 state
= state
+ ACLD_NET_EOL
;
404 net
= getnetbyname(k
);
408 mask
.s_addr
= ntohl(net
->n_net
);
409 state
= state
+ ACLD_NET_EOL
;
416 if ((state
== ACLS_ALLOW_NET_MASK
) || (state
== ACLS_DENY_NET_MASK
)) {
420 /* State 16 & 17: allow/deny host line */
423 ((state
== ACLS_ALLOW_NET_EOL
) || (state
== ACLS_DENY_NET_EOL
))) {
424 acl_add_net(allow
,&addr
,&mask
);
430 fprintf(stderr
,"acl: excpected \"netmask\" missing at line %d\n",line_no
);
434 fprintf(stderr
,"acl: unknown network at line %d\n",line_no
);
438 fprintf(stderr
,"acl: unknown host at line %d\n",line_no
);
442 fprintf(stderr
,"acl: unknown verb at line %d\n",line_no
);
446 fprintf(stderr
,"acl: unknown secondary verb at line %d\n",line_no
);
450 fprintf(stderr
,"acl: unexpected end of line at line %d\n",line_no
);
456 fprintf(stderr
,"acl: unexpected state %d %s\n",state
,k
);
461 if (data_file
!= NULL
) {
462 (void)fflush(stderr
);
463 (void)fclose(data_file
);
466 /* Always add a last allow all if file don't exists or */
467 /* the file doesn't cover all cases. */
469 addr
.s_addr
= mask
.s_addr
= 0;
471 acl_add_net(allow
,&addr
,&mask
);
478 yp_acl_securenet(file
)
481 char data_line
[1024];
488 struct in_addr addr
,mask
;
490 FILE *data_file
= NULL
;
493 data_file
= fopen(file
,"r");
496 /* Always add a localhost allow first, to be compatable with sun */
498 addr
.s_addr
= htonl(0x7f000001);
499 mask
.s_addr
= htonl(0xffffffff);
501 acl_add_net(allow
,&addr
,&mask
);
503 while ((data_file
!= NULL
) &&
504 (acl_read_line(data_file
,data_line
,sizeof(data_line
)))) {
508 len
= strlen(data_line
);
513 p
= (char *) &data_line
;
515 /* State 1: Initial State */
518 addr
.s_addr
= mask
.s_addr
= 0;
520 k
= p
; i
= 0; /* save start of verb */
521 while ((*p
!= '\0') &&
522 (!isspace(*p
= tolower(*p
)))) {
528 state
= ACLS_ALLOW_NET_MASK
;
531 if (state
== ACLS_INIT
) {
535 if (state
== ACLS_ALLOW_NET_MASK
) {
537 if ((*k
>= '0') && (*k
<= '9')) {
538 (void)inet_aton(k
,&mask
);
539 state
= ACLS_ALLOW_NET
;
541 net
= getnetbyname(k
);
545 mask
.s_addr
= ntohl(net
->n_net
);
546 state
= ACLS_ALLOW_NET
;
550 k
= p
; i
= 0; /* save start of verb */
551 while ((*p
!= '\0') &&
552 (!isspace(*p
= tolower(*p
)))) {
561 if ((state
== ACLS_ALLOW_NET_MASK
)) {
565 if (state
== ACLS_ALLOW_NET
) {
567 if ((*k
>= '0') && (*k
<= '9')) {
568 (void)inet_aton(k
,&addr
);
569 state
= ACLS_ALLOW_NET_EOL
;
571 net
= getnetbyname(k
);
575 addr
.s_addr
= ntohl(net
->n_net
);
576 state
= ACLS_ALLOW_NET_EOL
;
581 if ((state
== ACLS_ALLOW_NET
)) {
585 if ((*p
== '\0') && (state
== ACLS_ALLOW_NET_EOL
)) {
586 acl_add_net(allow
,&addr
,&mask
);
593 fprintf(stderr
,"securenet: unknown network at line %d\n",line_no
);
597 fprintf(stderr
,"securenet: unexpected end of line at line %d\n",line_no
);
603 fprintf(stderr
,"securenet: unexpected state %d %s\n",state
,k
);
608 if (data_file
!= NULL
) {
609 (void)fflush(stderr
);
610 (void)fclose(data_file
);
612 /* Always add a last deny all if file exists */
614 addr
.s_addr
= mask
.s_addr
= 0;
616 acl_add_net(allow
,&addr
,&mask
);
620 /* Always add a last allow all if file don't exists */
622 addr
.s_addr
= mask
.s_addr
= 0;
624 acl_add_net(allow
,&addr
,&mask
);
635 while (acl_root
!= NULL
) {