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
273
274
275
276
277
278
279
280
281
|
/*
* 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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* All routines necessary to deal the "netmasks" database. The sources
* contain mappings between 32 bit Internet addresses and corresponding
* 32 bit Internet address masks. The addresses are in dotted internet
* address notation.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <nss_dbdefs.h>
int str2addr(const char *, int, void *, char *, int);
static DEFINE_NSS_DB_ROOT(db_root);
void
_nss_initf_netmasks(nss_db_params_t *p)
{
p->name = NSS_DBNAM_NETMASKS;
p->default_config = NSS_DEFCONF_NETMASKS;
}
/*
* Print a network number such as 129.144 as well as an IP address.
* Assumes network byte order for both IP addresses and network numbers
* (Network numbers are normally passed around in host byte order).
* to be MT safe, use a passed in buffer like otherget*_r APIs.
*/
static char *
inet_nettoa(struct in_addr in, char *result, int len)
{
uint32_t addr = in.s_addr;
uchar_t *up = (uchar_t *)&addr;
if (result == NULL)
return (NULL);
/* Omit leading zeros */
if (up[0]) {
(void) snprintf(result, len, "%d.%d.%d.%d",
up[0], up[1], up[2], up[3]);
} else if (up[1]) {
(void) snprintf(result, len, "%d.%d.%d", up[1], up[2], up[3]);
} else if (up[2]) {
(void) snprintf(result, len, "%d.%d", up[2], up[3]);
} else {
(void) snprintf(result, len, "%d", up[3]);
}
return (result);
}
/*
* Given a 32 bit key look it up in the netmasks database
* based on the "netmasks" policy in /etc/nsswitch.conf.
* If the key is a network number with the trailing zero's removed
* (e.g. "192.9.200") this routine can't use inet_ntoa to convert
* the address to the string key.
* Returns zero if successful, non-zero otherwise.
*/
static int
getnetmaskbykey(const struct in_addr addr, struct in_addr *mask)
{
nss_XbyY_args_t arg;
nss_status_t res;
char tmp[NSS_LINELEN_NETMASKS];
/*
* let the backend do the allocation to store stuff for parsing.
* To simplify things, we put the dotted internet address form of
* the network address in the 'name' field as a filter to speed
* up the lookup.
*/
if (inet_nettoa(addr, tmp, NSS_LINELEN_NETMASKS) == NULL)
return (NSS_NOTFOUND);
NSS_XbyY_INIT(&arg, mask, NULL, 0, str2addr);
arg.key.name = tmp;
res = nss_search(&db_root, _nss_initf_netmasks,
NSS_DBOP_NETMASKS_BYNET, &arg);
(void) NSS_XbyY_FINI(&arg);
return (arg.status = res);
}
/*
* Given a 32 bit internet network number, it finds the corresponding netmask
* address based on the "netmasks" policy in /etc/nsswitch.conf.
* Returns zero if successful, non-zero otherwise.
* Check both for the (masked) network number and the shifted network
* number (e.g., both "10.0.0.0" and "10").
* Assumes that the caller passes in an unshifted number (or an IP address).
*/
int
getnetmaskbynet(const struct in_addr net, struct in_addr *mask)
{
struct in_addr net1, net2;
uint32_t i;
i = ntohl(net.s_addr);
/*
* Try looking for the network number both with and without
* the trailing zeros.
*/
if ((i & IN_CLASSA_NET) == 0) {
/* Assume already a right-shifted network number */
net2.s_addr = htonl(i);
if ((i & IN_CLASSB_NET) != 0) {
net1.s_addr = htonl(i << IN_CLASSC_NSHIFT);
} else if ((i & IN_CLASSC_NET) != 0) {
net1.s_addr = htonl(i << IN_CLASSB_NSHIFT);
} else {
net1.s_addr = htonl(i << IN_CLASSA_NSHIFT);
}
} else if (IN_CLASSA(i)) {
net1.s_addr = htonl(i & IN_CLASSA_NET);
net2.s_addr = htonl(i >> IN_CLASSA_NSHIFT);
} else if (IN_CLASSB(i)) {
net1.s_addr = htonl(i & IN_CLASSB_NET);
net2.s_addr = htonl(i >> IN_CLASSB_NSHIFT);
} else {
net1.s_addr = htonl(i & IN_CLASSC_NET);
net2.s_addr = htonl(i >> IN_CLASSC_NSHIFT);
}
if (getnetmaskbykey(net1, mask) == 0) {
return (0);
}
if (getnetmaskbykey(net2, mask) == 0) {
return (0);
}
return (-1);
}
/*
* Find the netmask used for an IP address.
* Returns zero if successful, non-zero otherwise.
*
* Support Variable Length Subnetmasks by looking for the longest
* matching subnetmask in the database.
* Start by looking for a match for the full IP address and
* mask off one rightmost bit after another until we find a match.
* Note that for a match the found netmask must match what was used
* for the lookup masking.
* As a fallback for compatibility finally lookup the network
* number with and without the trailing zeros.
* In order to suppress redundant lookups in the name service
* we keep the previous lookup key and compare against it before
* doing the lookup.
*/
int
getnetmaskbyaddr(const struct in_addr addr, struct in_addr *mask)
{
struct in_addr prevnet, net;
uint32_t i, maskoff;
i = ntohl(addr.s_addr);
prevnet.s_addr = 0;
mask->s_addr = 0;
for (maskoff = 0xFFFFFFFF; maskoff != 0; maskoff = maskoff << 1) {
net.s_addr = htonl(i & maskoff);
if (net.s_addr != prevnet.s_addr) {
if (getnetmaskbykey(net, mask) != 0) {
mask->s_addr = 0;
}
}
if (htonl(maskoff) == mask->s_addr)
return (0);
prevnet.s_addr = net.s_addr;
}
/*
* Non-VLSM fallback.
* Try looking for the network number with and without the trailing
* zeros.
*/
return (getnetmaskbynet(addr, mask));
}
/*
* Parse netmasks entry into its components. The network address is placed
* in buffer for use by check_addr for 'files' backend, to match the network
* address. The network address is placed in the buffer as a network order
* internet address, if buffer is non null. The network order form of the mask
* itself is placed in 'ent'.
*/
int
str2addr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
{
int retval;
struct in_addr *mask = (struct in_addr *)ent;
const char *p, *limit, *start;
struct in_addr addr;
int i;
char tmp[NSS_LINELEN_NETMASKS];
p = instr;
limit = p + lenstr;
retval = NSS_STR_PARSE_PARSE;
while (p < limit && isspace(*p)) /* skip leading whitespace */
p++;
if (buffer) { /* for 'files' backend verification */
for (start = p, i = 0; p < limit && !isspace(*p); p++)
i++;
if (p < limit && i < buflen) {
(void) memcpy(tmp, start, i);
tmp[i] = '\0';
addr.s_addr = inet_addr(tmp);
/* Addr will always be an ipv4 address (32bits) */
if (addr.s_addr == 0xffffffffUL)
return (NSS_STR_PARSE_PARSE);
else {
(void) memcpy(buffer, (char *)&addr,
sizeof (struct in_addr));
}
} else
return (NSS_STR_PARSE_ERANGE);
}
while (p < limit && isspace(*p)) /* skip intermediate */
p++;
if (mask) {
for (start = p, i = 0; p < limit && !isspace(*p); p++)
i++;
if (p <= limit) {
if ((i + 1) > NSS_LINELEN_NETMASKS)
return (NSS_STR_PARSE_ERANGE);
(void) memcpy(tmp, start, i);
tmp[i] = '\0';
addr.s_addr = inet_addr(tmp);
/* Addr will always be an ipv4 address (32bits) */
if (addr.s_addr == 0xffffffffUL)
retval = NSS_STR_PARSE_PARSE;
else {
mask->s_addr = addr.s_addr;
retval = NSS_STR_PARSE_SUCCESS;
}
}
}
return (retval);
}
|