4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * From "tsol_tndb_parser.c 7.24 01/09/05 SMI; TSOL 2.x"
27 * These functions parse entries in the "thrhdb" (remote host database) file.
28 * Each entry in the file has two fields, separated by a colon. The first
29 * field is the IP host or network address. The second is the name of the
30 * template to use (from tnrhtp).
32 * In order to help preserve sanity, we do not allow more than one unescaped
36 #pragma ident "%Z%%M% %I% %E% SMI"
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #include <arpa/inet.h>
54 * This routine deals with old pre-CIDR subnet address specifications. In the
55 * bad old days, a subnet was represented as:
57 * Expression Implied Prefix
64 get_classful_prefix(in_addr_t addr
)
71 for (bits
= IP_ABITS
; bits
> 0 && (addr
& 0xFF) == 0; bits
-= 8)
78 * This routine deals with old pre-CIDR network address specifications. In the
79 * bad old days, a network was represented as:
81 * Expression Implied Prefix
86 * This routine must compute the mask and left-align the address.
89 get_network_prefix(in_addr_t
*addrp
)
95 for (bits
= IP_ABITS
; bits
> 0 && addr
< 0x01000000; bits
-= 8)
103 parse_address(tsol_rhent_t
*rh
, const char *addrbuf
)
109 if (strchr(addrbuf
, ':') == NULL
) {
111 rh
->rh_address
.ta_family
= AF_INET
;
112 if (inet_pton(AF_INET
, addrbuf
,
113 &rh
->rh_address
.ta_addr_v4
) > 0) {
114 if (rh
->rh_prefix
== -1)
115 rh
->rh_prefix
= get_classful_prefix(rh
->
116 rh_address
.ta_addr_v4
.s_addr
);
117 } else if ((rh
->rh_address
.ta_addr_v4
.s_addr
=
118 inet_network(addrbuf
)) != (in_addr_t
)-1) {
119 len
= get_network_prefix(&rh
->rh_address
.ta_addr_v4
.
121 if (rh
->rh_prefix
== -1)
126 upper_lim
= IP_ABITS
;
127 aptr
= (const uchar_t
*)&rh
->rh_address
.ta_addr_v4
;
130 rh
->rh_address
.ta_family
= AF_INET6
;
131 if (inet_pton(AF_INET6
, addrbuf
,
132 &rh
->rh_address
.ta_addr_v6
) <= 0)
134 if (rh
->rh_prefix
== -1)
135 rh
->rh_prefix
= IPV6_ABITS
;
136 upper_lim
= IPV6_ABITS
;
137 aptr
= (const uchar_t
*)&rh
->rh_address
.ta_addr_v6
;
140 if (rh
->rh_prefix
< 0 || rh
->rh_prefix
> upper_lim
)
144 * Verify that there are no bits set in the "host" portion of the
149 if ((len
& 7) != 0) {
150 if ((*aptr
++ & (0xff >> (len
& 7))) != 0)
152 len
= (len
+ 7) & ~7;
154 while (len
< upper_lim
) {
164 rhstr_to_ent(tsol_rhstr_t
*rhstrp
, int *errp
, char **errstrp
)
168 char *cp
, *cp2
, *errstr
;
169 char *address
= rhstrp
->address
;
170 char *template = rhstrp
->template;
172 tsol_rhent_t
*rhentp
= NULL
;
175 * The user can specify NULL pointers for these. Make sure that we
176 * don't have to deal with checking for NULL everywhere by just
177 * pointing to our own variables if the user gives NULL.
183 /* The default, unless we find a more specific error locus. */
186 if (address
== NULL
|| *address
== '#' || *address
== '\n') {
187 *errp
= LTSNET_EMPTY
;
188 if (template && *template != '\0' && *template != '#' &&
191 else if (address
== NULL
)
195 if (*address
== '\0') {
196 *errp
= LTSNET_NO_ADDR
;
197 if (template && *template != '\0' && *template != '#' &&
202 if (template == NULL
|| *template == '#' || *template == '\n' ||
204 *errp
= LTSNET_NO_HOSTTYPE
;
207 if ((rhentp
= calloc(1, sizeof (*rhentp
))) == NULL
) {
208 *errp
= LTSNET_SYSERR
;
211 if ((cp
= strrchr(address
, '/')) != NULL
) {
213 if (len
>= sizeof (addrbuf
)) {
214 *errp
= LTSNET_ILL_ADDR
;
217 (void) memset(addrbuf
, '\0', sizeof (addrbuf
));
218 (void) memcpy(addrbuf
, address
, len
);
221 rhentp
->rh_prefix
= strtol(cp
, &cp2
, 0);
223 *errp
= LTSNET_SYSERR
;
227 if ((isdigit(*cp
) == 0)) {
228 *errp
= LTSNET_ILL_ADDR
;
233 rhentp
->rh_prefix
= -1;
234 (void) strlcpy(addrbuf
, address
, sizeof (addrbuf
));
236 if (strlcpy(rhentp
->rh_template
, template,
237 sizeof (rhentp
->rh_template
)) >= sizeof (rhentp
->rh_template
)) {
239 *errp
= LTSNET_ILL_NAME
;
242 if (!parse_address(rhentp
, addrbuf
)) {
243 *errp
= LTSNET_ILL_ADDR
;
249 (void) fprintf(stdout
, "rhstr_to_ent: %s:%s\n",
250 address
, rhentp
->rh_template
);
257 tsol_freerhent(rhentp
);
260 (void) fprintf(stderr
, "\nrhstr_to_ent: %s: %s\n",
261 *errstrp
, (char *)tsol_strerror(*errp
, errno
));
268 tsol_freerhent(tsol_rhent_t
*rh
)