4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1999 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
39 #include <sys/tiuser.h>
41 #define ACCFILE "/var/yp/securenets"
56 static int string2inaddr(char *, sa_family_t
*, inaddr_t
*);
57 static int addrequal(sa_family_t af
, inaddr_t
*laddr
, inaddr_t
*mask
,
60 static struct seclist
*slist
;
61 static int nofile
= 0;
64 get_secure_nets(char *daemon_name
)
67 char strung
[MAXLINE
], nmask
[MAXLINE
], net
[MAXLINE
];
68 inaddr_t maskin
, netin
;
69 sa_family_t maskaf
, netaf
;
70 struct seclist
*tmp1
, *tmp2
;
71 int items
= 0, line
= 0;
72 if (fp
= fopen(ACCFILE
, "r")) {
73 tmp1
= (struct seclist
*) malloc(sizeof (struct seclist
));
75 while (fgets(strung
, MAXLINE
, fp
)) {
77 if (strung
[strlen(strung
) - 1] != '\n') {
78 syslog(LOG_ERR
|LOG_DAEMON
,
79 "%s: %s line %d: too long\n",
80 daemon_name
, ACCFILE
, line
);
83 if (strung
[0] != '#') {
86 "%46s%46s", nmask
, net
) < 2) {
88 syslog(LOG_ERR
|LOG_DAEMON
,
89 "%s: %s line %d: missing fields\n",
90 daemon_name
, ACCFILE
, line
);
94 if (! string2inaddr(net
, &netaf
, &netin
)) {
95 syslog(LOG_ERR
|LOG_DAEMON
,
96 "%s: %s line %d: error in address\n",
97 daemon_name
, ACCFILE
, line
);
101 if (! string2inaddr(nmask
, &maskaf
, &maskin
) ||
103 syslog(LOG_ERR
|LOG_DAEMON
,
104 "%s: %s line %d: error in netmask\n",
105 daemon_name
, ACCFILE
, line
);
108 if (! addrequal(netaf
, &netin
, &maskin
,
110 syslog(LOG_ERR
|LOG_DAEMON
,
111 "%s: %s line %d: netmask does not match network\n",
112 daemon_name
, ACCFILE
, line
);
119 tmp1
->next
= (struct seclist
*)
120 malloc(sizeof (struct seclist
));
126 /* if nothing to process, set nofile flag and free up memory */
132 syslog(LOG_WARNING
|LOG_DAEMON
, "%s: no %s file\n",
133 daemon_name
, ACCFILE
);
139 check_secure_net_ti(struct netbuf
*caller
, char *ypname
) {
143 char buf
[INET6_ADDRSTRLEN
];
148 af
= ((struct sockaddr_storage
*)caller
->buf
)->ss_family
;
150 addr
.in4
= ((struct sockaddr_in
*)caller
->buf
)->sin_addr
;
151 } else if (af
== AF_INET6
) {
152 addr
.in6
= ((struct sockaddr_in6
*)caller
->buf
)->sin6_addr
;
158 while (tmp
!= NULL
) {
160 addrequal(af
, &tmp
->net
, &tmp
->mask
, &addr
)) {
165 syslog(LOG_ERR
|LOG_DAEMON
, "%s: access denied for %s\n",
166 ypname
, inet_ntop(af
,
167 (af
== AF_INET6
) ? (void *)&addr
.in6
:
168 (void *)&addr
.in4
, buf
, sizeof (buf
)));
175 string2inaddr(char *string
, sa_family_t
*af
, inaddr_t
*addr
) {
177 sa_family_t stringaf
= AF_UNSPEC
;
179 stringaf
= (strchr(string
, ':') != 0) ? AF_INET6
: AF_INET
;
181 if (*af
!= AF_UNSPEC
&& strcmp(string
, "host") == 0) {
182 if (*af
== AF_INET
) {
183 string
= "255.255.255.255";
185 } else if (*af
== AF_INET6
) {
186 string
= "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
192 if (inet_pton(*af
, string
, (*af
== AF_INET6
) ? (void *)&addr
->in6
:
193 (void *)&addr
->in4
) != 1) {
202 addrequal(sa_family_t af
, inaddr_t
*laddr
, inaddr_t
*mask
, inaddr_t
*caddr
) {
204 if (af
== AF_INET6
) {
206 for (i
= 0; i
< sizeof (laddr
->in6
.s6_addr
); i
++) {
207 if ((caddr
->in6
.s6_addr
[i
] & mask
->in6
.s6_addr
[i
]) !=
208 laddr
->in6
.s6_addr
[i
])
212 } else if (af
== AF_INET
) {
213 return ((caddr
->in4
.s_addr
& mask
->in4
.s_addr
) ==
222 print_inaddr(char *string
, sa_family_t af
, inaddr_t
*addr
) {
224 char buf
[INET6_ADDRSTRLEN
];
227 string
, (af
== AF_INET6
)?"AF_INET6":"AF_INET",
228 inet_ntop(af
, (af
== AF_INET6
) ? (void *)&addr
->in6
:
229 (void *)&addr
->in4
, buf
, sizeof (buf
)));