1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* From "tsol_tndb_parser.c 7.24 01/09/05 SMI; TSOL 2.x"
*
* These functions parse entries in the "thrhdb" (remote host database) file.
* Each entry in the file has two fields, separated by a colon. The first
* field is the IP host or network address. The second is the name of the
* template to use (from tnrhtp).
*
* In order to help preserve sanity, we do not allow more than one unescaped
* colon in a line.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>
#include <libtsnet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <inet/ip.h>
#include <arpa/inet.h>
#include <nss.h>
#include <errno.h>
/*
* This routine deals with old pre-CIDR subnet address specifications. In the
* bad old days, a subnet was represented as:
*
* Expression Implied Prefix
* 10.1.1.0 /24
* 10.1.0.0 /16
* 10.0.0.0 /8
* 0.0.0.0 /0
*/
static int
get_classful_prefix(in_addr_t addr)
{
int bits;
if (addr == 0)
return (0);
addr = ntohl(addr);
for (bits = IP_ABITS; bits > 0 && (addr & 0xFF) == 0; bits -= 8)
addr >>= 8;
return (bits);
}
/*
* This routine deals with old pre-CIDR network address specifications. In the
* bad old days, a network was represented as:
*
* Expression Implied Prefix
* 10.1.1 /24
* 10.1 /16
* 10 /8
*
* This routine must compute the mask and left-align the address.
*/
static int
get_network_prefix(in_addr_t *addrp)
{
int bits;
in_addr_t addr;
addr = ntohl(*addrp);
for (bits = IP_ABITS; bits > 0 && addr < 0x01000000; bits -= 8)
addr <<= 8;
*addrp = htonl(addr);
return (bits);
}
static boolean_t
parse_address(tsol_rhent_t *rh, const char *addrbuf)
{
int upper_lim;
int len;
const uchar_t *aptr;
if (strchr(addrbuf, ':') == NULL) {
/* IPv4 address */
rh->rh_address.ta_family = AF_INET;
if (inet_pton(AF_INET, addrbuf,
&rh->rh_address.ta_addr_v4) > 0) {
if (rh->rh_prefix == -1)
rh->rh_prefix = get_classful_prefix(rh->
rh_address.ta_addr_v4.s_addr);
} else if ((rh->rh_address.ta_addr_v4.s_addr =
inet_network(addrbuf)) != (in_addr_t)-1) {
len = get_network_prefix(&rh->rh_address.ta_addr_v4.
s_addr);
if (rh->rh_prefix == -1)
rh->rh_prefix = len;
} else {
return (B_FALSE);
}
upper_lim = IP_ABITS;
aptr = (const uchar_t *)&rh->rh_address.ta_addr_v4;
} else {
/* IPv6 address */
rh->rh_address.ta_family = AF_INET6;
if (inet_pton(AF_INET6, addrbuf,
&rh->rh_address.ta_addr_v6) <= 0)
return (B_FALSE);
if (rh->rh_prefix == -1)
rh->rh_prefix = IPV6_ABITS;
upper_lim = IPV6_ABITS;
aptr = (const uchar_t *)&rh->rh_address.ta_addr_v6;
}
if (rh->rh_prefix < 0 || rh->rh_prefix > upper_lim)
return (B_FALSE);
/*
* Verify that there are no bits set in the "host" portion of the
* IP address.
*/
len = rh->rh_prefix;
aptr += len / 8;
if ((len & 7) != 0) {
if ((*aptr++ & (0xff >> (len & 7))) != 0)
return (B_FALSE);
len = (len + 7) & ~7;
}
while (len < upper_lim) {
if (*aptr++ != 0)
return (B_FALSE);
len += 8;
}
return (B_TRUE);
}
tsol_rhent_t *
rhstr_to_ent(tsol_rhstr_t *rhstrp, int *errp, char **errstrp)
{
int len;
int err = 0;
char *cp, *cp2, *errstr;
char *address = rhstrp->address;
char *template = rhstrp->template;
char addrbuf[1024];
tsol_rhent_t *rhentp = NULL;
/*
* The user can specify NULL pointers for these. Make sure that we
* don't have to deal with checking for NULL everywhere by just
* pointing to our own variables if the user gives NULL.
*/
if (errp == NULL)
errp = &err;
if (errstrp == NULL)
errstrp = &errstr;
/* The default, unless we find a more specific error locus. */
*errstrp = address;
if (address == NULL || *address == '#' || *address == '\n') {
*errp = LTSNET_EMPTY;
if (template && *template != '\0' && *template != '#' &&
*template != '\n')
*errstrp = template;
else if (address == NULL)
*errstrp = " ";
goto err_ret;
}
if (*address == '\0') {
*errp = LTSNET_NO_ADDR;
if (template && *template != '\0' && *template != '#' &&
*template != '\n')
*errstrp = template;
goto err_ret;
}
if (template == NULL || *template == '#' || *template == '\n' ||
*template == '\0') {
*errp = LTSNET_NO_HOSTTYPE;
goto err_ret;
}
if ((rhentp = calloc(1, sizeof (*rhentp))) == NULL) {
*errp = LTSNET_SYSERR;
return (NULL);
}
if ((cp = strrchr(address, '/')) != NULL) {
len = cp - address;
if (len >= sizeof (addrbuf)) {
*errp = LTSNET_ILL_ADDR;
goto err_ret;
}
(void) memset(addrbuf, '\0', sizeof (addrbuf));
(void) memcpy(addrbuf, address, len);
cp++;
errno = 0;
rhentp->rh_prefix = strtol(cp, &cp2, 0);
if (errno != 0) {
*errp = LTSNET_SYSERR;
*errstrp = cp2;
goto err_ret;
}
if ((isdigit(*cp) == 0)) {
*errp = LTSNET_ILL_ADDR;
*errstrp = address;
goto err_ret;
}
} else {
rhentp->rh_prefix = -1;
(void) strlcpy(addrbuf, address, sizeof (addrbuf));
}
if (strlcpy(rhentp->rh_template, template,
sizeof (rhentp->rh_template)) >= sizeof (rhentp->rh_template)) {
*errstrp = template;
*errp = LTSNET_ILL_NAME;
goto err_ret;
}
if (!parse_address(rhentp, addrbuf)) {
*errp = LTSNET_ILL_ADDR;
*errstrp = address;
goto err_ret;
}
#ifdef DEBUG
(void) fprintf(stdout, "rhstr_to_ent: %s:%s\n",
address, rhentp->rh_template);
#endif /* DEBUG */
return (rhentp);
err_ret:
err = errno;
tsol_freerhent(rhentp);
errno = err;
#ifdef DEBUG
(void) fprintf(stderr, "\nrhstr_to_ent: %s: %s\n",
*errstrp, (char *)tsol_strerror(*errp, errno));
#endif /* DEBUG */
return (NULL);
}
void
tsol_freerhent(tsol_rhent_t *rh)
{
if (rh != NULL)
free(rh);
}
|