2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1994, Garrett Wollman
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "namespace.h"
32 #include "reentrant.h"
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
45 #include "un-namespace.h"
46 #include "netdb_private.h"
51 /* Network lookup order if nsswitch.conf is broken or nonexistent */
52 static const ns_src default_src[] = {
53 { NSSRC_FILES, NS_SUCCESS },
54 { NSSRC_DNS, NS_SUCCESS },
58 NETDB_THREAD_ALLOC(netent_data)
59 NETDB_THREAD_ALLOC(netdata)
63 net_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
69 size_t desired_size, size;
70 enum nss_lookup_type lookup_type;
73 lookup_type = (enum nss_lookup_type)(uintptr_t)cache_mdata;
74 switch (lookup_type) {
76 name = va_arg(ap, char *);
79 desired_size = sizeof(enum nss_lookup_type) + size + 1;
80 if (desired_size > *buffer_size) {
85 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
86 memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
91 net = va_arg(ap, uint32_t);
92 type = va_arg(ap, int);
94 desired_size = sizeof(enum nss_lookup_type) +
95 sizeof(uint32_t) + sizeof(int);
96 if (desired_size > *buffer_size) {
101 memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
102 memcpy(buffer + sizeof(enum nss_lookup_type), &net,
104 memcpy(buffer + sizeof(enum nss_lookup_type) + sizeof(uint32_t),
110 /* should be unreachable */
115 *buffer_size = desired_size;
121 net_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
129 size_t orig_buf_size;
131 struct netent new_ne;
132 size_t desired_size, size, aliases_size;
136 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) {
138 name = va_arg(ap, char *);
141 net = va_arg(ap, uint32_t);
142 type = va_arg(ap, int);
147 /* should be unreachable */
151 ne = va_arg(ap, struct netent *);
152 orig_buf = va_arg(ap, char *);
153 orig_buf_size = va_arg(ap, size_t);
155 desired_size = _ALIGNBYTES + sizeof(struct netent) + sizeof(char *);
156 if (ne->n_name != NULL)
157 desired_size += strlen(ne->n_name) + 1;
159 if (ne->n_aliases != NULL) {
161 for (alias = ne->n_aliases; *alias; ++alias) {
162 desired_size += strlen(*alias) + 1;
166 desired_size += _ALIGNBYTES +
167 (aliases_size + 1) * sizeof(char *);
170 if (*buffer_size < desired_size) {
171 /* this assignment is here for future use */
172 *buffer_size = desired_size;
176 memcpy(&new_ne, ne, sizeof(struct netent));
178 *buffer_size = desired_size;
179 memset(buffer, 0, desired_size);
180 p = buffer + sizeof(struct netent) + sizeof(char *);
181 memcpy(buffer + sizeof(struct netent), &p, sizeof(char *));
182 p = (char *)_ALIGN(p);
184 if (new_ne.n_name != NULL) {
185 size = strlen(new_ne.n_name);
186 memcpy(p, new_ne.n_name, size);
191 if (new_ne.n_aliases != NULL) {
192 p = (char *)_ALIGN(p);
193 memcpy(p, new_ne.n_aliases, sizeof(char *) * aliases_size);
194 new_ne.n_aliases = (char **)p;
195 p += sizeof(char *) * (aliases_size + 1);
197 for (alias = new_ne.n_aliases; *alias; ++alias) {
198 size = strlen(*alias);
199 memcpy(p, *alias, size);
205 memcpy(buffer, &new_ne, sizeof(struct netent));
210 net_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
218 size_t orig_buf_size;
224 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) {
226 name = va_arg(ap, char *);
229 net = va_arg(ap, uint32_t);
230 type = va_arg(ap, int);
235 /* should be unreachable */
239 ne = va_arg(ap, struct netent *);
240 orig_buf = va_arg(ap, char *);
241 orig_buf_size = va_arg(ap, size_t);
242 ret_errno = va_arg(ap, int *);
245 buffer_size - sizeof(struct netent) - sizeof(char *)) {
250 memcpy(ne, buffer, sizeof(struct netent));
251 memcpy(&p, buffer + sizeof(struct netent), sizeof(char *));
253 orig_buf = (char *)_ALIGN(orig_buf);
254 memcpy(orig_buf, buffer + sizeof(struct netent) + sizeof(char *) +
255 _ALIGN(p) - (size_t)p,
256 buffer_size - sizeof(struct netent) - sizeof(char *) -
257 _ALIGN(p) + (size_t)p);
258 p = (char *)_ALIGN(p);
260 NS_APPLY_OFFSET(ne->n_name, orig_buf, p, char *);
261 if (ne->n_aliases != NULL) {
262 NS_APPLY_OFFSET(ne->n_aliases, orig_buf, p, char **);
264 for (alias = ne->n_aliases; *alias; ++alias)
265 NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
269 *((struct netent **)retval) = ne;
273 #endif /* NS_CACHING */
276 netent_data_free(void *ptr)
278 struct netent_data *ned = ptr;
288 netdata_free(void *ptr)
294 __copy_netent(struct netent *ne, struct netent *nptr, char *buf, size_t buflen)
300 /* Find out the amount of space required to store the answer. */
301 numptr = 1; /* NULL ptr */
302 len = (char *)ALIGN(buf) - buf;
303 for (i = 0; ne->n_aliases[i]; i++, numptr++) {
304 len += strlen(ne->n_aliases[i]) + 1;
306 len += strlen(ne->n_name) + 1;
307 len += numptr * sizeof(char*);
309 if (len > (int)buflen) {
314 /* copy net value and type */
315 nptr->n_addrtype = ne->n_addrtype;
316 nptr->n_net = ne->n_net;
318 cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
320 /* copy official name */
321 n = strlen(ne->n_name) + 1;
322 strcpy(cp, ne->n_name);
327 nptr->n_aliases = (char **)ALIGN(buf);
328 for (i = 0 ; ne->n_aliases[i]; i++) {
329 n = strlen(ne->n_aliases[i]) + 1;
330 strcpy(cp, ne->n_aliases[i]);
331 nptr->n_aliases[i] = cp;
334 nptr->n_aliases[i] = NULL;
340 getnetbyname_r(const char *name, struct netent *ne, char *buffer,
341 size_t buflen, struct netent **result, int *h_errorp)
344 static const nss_cache_info cache_info =
345 NS_COMMON_CACHE_INFO_INITIALIZER(
346 networks, (void *)nss_lt_name,
347 net_id_func, net_marshal_func, net_unmarshal_func);
349 static const ns_dtab dtab[] = {
350 NS_FILES_CB(_ht_getnetbyname, NULL)
351 { NSSRC_DNS, _dns_getnetbyname, NULL },
352 NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */
354 NS_CACHE_CB(&cache_info)
358 int rval, ret_errno = 0;
360 rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS,
361 "getnetbyname_r", default_src, name, ne, buffer, buflen,
362 &ret_errno, h_errorp);
364 if (rval != NS_SUCCESS) {
366 return ((ret_errno != 0) ? ret_errno : -1);
372 getnetbyaddr_r(uint32_t addr, int af, struct netent *ne, char *buffer,
373 size_t buflen, struct netent **result, int *h_errorp)
376 static const nss_cache_info cache_info =
377 NS_COMMON_CACHE_INFO_INITIALIZER(
378 networks, (void *)nss_lt_id,
379 net_id_func, net_marshal_func, net_unmarshal_func);
381 static const ns_dtab dtab[] = {
382 NS_FILES_CB(_ht_getnetbyaddr, NULL)
383 { NSSRC_DNS, _dns_getnetbyaddr, NULL },
384 NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */
386 NS_CACHE_CB(&cache_info)
390 int rval, ret_errno = 0;
392 rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS,
393 "getnetbyaddr_r", default_src, addr, af, ne, buffer, buflen,
394 &ret_errno, h_errorp);
396 if (rval != NS_SUCCESS) {
398 return ((ret_errno != 0) ? ret_errno : -1);
404 getnetbyname(const char *name)
410 if ((nd = __netdata_init()) == NULL)
412 if (getnetbyname_r(name, &nd->net, nd->data, sizeof(nd->data), &rval,
419 getnetbyaddr(uint32_t addr, int af)
425 if ((nd = __netdata_init()) == NULL)
427 if (getnetbyaddr_r(addr, af, &nd->net, nd->data, sizeof(nd->data),
428 &rval, &ret_h_errno) != 0)
434 setnetent(int stayopen)
436 struct netent_data *ned;
438 if ((ned = __netent_data_init()) == NULL)
440 _setnethtent(stayopen, ned);
441 _setnetdnsent(stayopen);
447 struct netent_data *ned;
449 if ((ned = __netent_data_init()) == NULL)