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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* All routines necessary to deal the "ethers" database. The sources
* contain mappings between 48 bit ethernet addresses and corresponding
* hosts names. The addresses have an ascii representation of the form
* "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the
* bytes are always in network order.
*/
#include "c_synonyms.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <thread.h>
#include <pthread.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <nss_dbdefs.h>
int str2ether(const char *, int, void *, char *, int);
static DEFINE_NSS_DB_ROOT(db_root);
void
_nss_initf_ethers(nss_db_params_t *p)
{
p->name = NSS_DBNAM_ETHERS;
p->default_config = NSS_DEFCONF_ETHERS;
}
/*
* Given a host's name, this routine finds the corresponding 48 bit
* ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
* Returns zero if successful, non-zero otherwise.
*/
int
ether_hostton(
const char *host, /* function input */
struct ether_addr *e /* function output */
)
{
nss_XbyY_args_t arg;
nss_status_t res;
/*
* let the backend do the allocation to store stuff for parsing.
*/
NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether);
arg.key.name = host;
res = nss_search(&db_root, _nss_initf_ethers,
NSS_DBOP_ETHERS_HOSTTON, &arg);
(void) NSS_XbyY_FINI(&arg);
return (arg.status = res);
}
/*
* Given a 48 bit ethernet address, it finds the corresponding hostname
* ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
* Returns zero if successful, non-zero otherwise.
*/
int
ether_ntohost(
char *host, /* function output */
const struct ether_addr *e /* function input */
)
{
nss_XbyY_args_t arg;
nss_status_t res;
/*
* let the backend do the allocation to store stuff for parsing.
*/
NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether);
arg.key.ether = (void *)e;
res = nss_search(&db_root, _nss_initf_ethers,
NSS_DBOP_ETHERS_NTOHOST, &arg);
/* memcpy(host, ether_res.host, strlen(ether_res.host)); */
(void) NSS_XbyY_FINI(&arg);
return (arg.status = res);
}
/*
* Parses a line from "ethers" database into its components. The line has
* the form 8:0:20:1:17:c8 krypton
* where the first part is a 48 bit ethernet address and the second is
* the corresponding hosts name.
* Returns zero if successful, non-zero otherwise.
*/
int
ether_line(
const char *s, /* the string to be parsed */
struct ether_addr *e, /* ethernet address struct to be filled in */
char *hostname /* hosts name to be set */
)
{
int i;
uint_t t[6];
i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
&t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
if (i != 7) {
return (7 - i);
}
for (i = 0; i < 6; i++)
e->ether_addr_octet[i] = (uchar_t)t[i];
return (0);
}
/*
* Parses a line from "ethers" database into its components.
* Useful for the vile purposes of the backends that
* expect a str2ether() format.
*
* This function, after parsing the instr line, will
* place the resulting struct ether_addr in b->buf.result only if
* b->buf.result is initialized (not NULL). I.e. it always happens
* for "files" backend (that needs to parse input line and
* then do a match for the ether key) and happens for "nis"
* backend only if the call was ether_hostton.
*
* Also, it will place the resulting hostname into b->buf.buffer
* only if b->buf.buffer is initialized. I.e. it always happens
* for "files" backend (that needs to parse input line and
* then do a match for the host key) and happens for "nis"
* backend only if the call was ether_ntohost.
*
* Cannot use the sscanf() technique for parsing because instr
* is a read-only, not necessarily null-terminated, buffer.
*
* Return values: 0 = success, 1 = parse error, 2 = erange ...
* The structure pointer passed in is a structure in the caller's space
* wherein the field pointers would be set to areas in the buffer if
* need be. instring and buffer should be separate areas.
*/
#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
#define lisalnum(x) (isdigit(x) || \
((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
/* ARGSUSED */
int
str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
{
uchar_t *ether = (uchar_t *)ent;
char *host = buffer;
const char *p, *limit, *start;
ptrdiff_t i;
p = instr;
limit = p + lenstr;
/* skip beginning whitespace, if any */
while (p < limit && isspace(*p))
p++;
if (ether) { /* parse ether */
for (i = 0; i < 6; i++) {
int j = 0, n = 0;
start = p;
while (p < limit && lisalnum(start[j])) {
/* don't worry about overflow here */
n = 16 * n + DIGIT(start[j]);
j++;
p++;
}
if (*p != ':' && i < 5) {
return (NSS_STR_PARSE_PARSE);
} else {
p++;
*(ether + i) = (uchar_t)n;
}
}
} else { /* skip ether */
while (p < limit && !isspace(*p))
p++;
}
if (host) { /* parse host */
while (p < limit && isspace(*p)) /* skip whitespace */
p++;
start = p;
while (p < limit && !isspace(*p)) /* skip hostname */
p++;
if ((i = (p - start)) < MAXHOSTNAMELEN) {
(void) memcpy(host, start, i);
host[i] = '\0';
} else
return (NSS_STR_PARSE_ERANGE); /* failure */
}
return (NSS_STR_PARSE_SUCCESS);
}
typedef struct {
char ea_string[18];
struct ether_addr ea_addr;
} eabuf_t;
static eabuf_t *
ea_buf(void)
{
static thread_key_t key = THR_ONCE_KEY;
static eabuf_t ea_main;
eabuf_t *eabuf;
if (thr_main())
return (&ea_main);
if (thr_keycreate_once(&key, free) != 0)
return (NULL);
eabuf = pthread_getspecific(key);
if (eabuf == NULL) {
eabuf = malloc(sizeof (eabuf_t));
(void) thr_setspecific(key, eabuf);
}
return (eabuf);
}
/*
* Converts a 48 bit ethernet number to its string representation.
*/
char *
ether_ntoa(const struct ether_addr *e)
{
eabuf_t *eabuf;
char *s;
if ((eabuf = ea_buf()) == NULL)
return (NULL);
s = eabuf->ea_string;
(void) sprintf(s, "%x:%x:%x:%x:%x:%x",
e->ether_addr_octet[0], e->ether_addr_octet[1],
e->ether_addr_octet[2], e->ether_addr_octet[3],
e->ether_addr_octet[4], e->ether_addr_octet[5]);
return (s);
}
/*
* Converts an ethernet address representation back into its 48 bits.
*/
struct ether_addr *
ether_aton(const char *s)
{
eabuf_t *eabuf;
struct ether_addr *e;
int i;
uint_t t[6];
if ((eabuf = ea_buf()) == NULL)
return (NULL);
e = &eabuf->ea_addr;
i = sscanf(s, " %x:%x:%x:%x:%x:%x",
&t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
if (i != 6)
return (NULL);
for (i = 0; i < 6; i++)
e->ether_addr_octet[i] = (uchar_t)t[i];
return (e);
}
|