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 <arpa/nameser.h> /* XXX hack for _res */
46 #include <resolv.h> /* XXX hack for _res */
47 #include "un-namespace.h"
48 #include "netdb_private.h"
53 static int gethostbyname_internal(const char *, int, struct hostent *, char *,
54 size_t, struct hostent **, int *, res_state);
56 /* Host lookup order if nsswitch.conf is broken or nonexistent */
57 static const ns_src default_src[] = {
58 { NSSRC_FILES, NS_SUCCESS },
59 { NSSRC_DNS, NS_SUCCESS },
63 static int host_id_func(char *, size_t *, va_list, void *);
64 static int host_marshal_func(char *, size_t *, void *, va_list, void *);
65 static int host_unmarshal_func(char *, size_t, void *, va_list, void *);
68 NETDB_THREAD_ALLOC(hostent)
69 NETDB_THREAD_ALLOC(hostent_data)
70 NETDB_THREAD_ALLOC(hostdata)
73 hostent_free(void *ptr)
79 hostent_data_free(void *ptr)
81 struct hostent_data *hed = ptr;
91 hostdata_free(void *ptr)
97 __copy_hostent(struct hostent *he, struct hostent *hptr, char *buf,
105 /* Find out the amount of space required to store the answer. */
106 nptr = 2; /* NULL ptrs */
107 len = (char *)ALIGN(buf) - buf;
108 for (i = 0; he->h_addr_list[i]; i++, nptr++) {
111 for (i = 0; he->h_aliases[i]; i++, nptr++) {
112 len += strlen(he->h_aliases[i]) + 1;
114 len += strlen(he->h_name) + 1;
115 len += nptr * sizeof(char*);
122 /* copy address size and type */
123 hptr->h_addrtype = he->h_addrtype;
124 n = hptr->h_length = he->h_length;
126 ptr = (char **)ALIGN(buf);
127 cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
129 /* copy address list */
130 hptr->h_addr_list = ptr;
131 for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
132 memcpy(cp, he->h_addr_list[i], n);
133 hptr->h_addr_list[i] = cp;
136 hptr->h_addr_list[i] = NULL;
139 /* copy official name */
140 n = strlen(he->h_name) + 1;
141 strcpy(cp, he->h_name);
146 hptr->h_aliases = ptr;
147 for (i = 0 ; he->h_aliases[i]; i++) {
148 n = strlen(he->h_aliases[i]) + 1;
149 strcpy(cp, he->h_aliases[i]);
150 hptr->h_aliases[i] = cp;
153 hptr->h_aliases[i] = NULL;
160 host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
171 size_t desired_size, size;
172 enum nss_lookup_type lookup_type;
174 int res = NS_UNAVAIL;
176 statp = __res_state();
177 res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
178 RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
180 lookup_type = (enum nss_lookup_type)(uintptr_t)cache_mdata;
181 switch (lookup_type) {
183 str = va_arg(ap, char *);
184 type = va_arg(ap, int);
187 desired_size = sizeof(res_options) + sizeof(int) +
188 sizeof(enum nss_lookup_type) + sizeof(int) + size + 1;
190 if (desired_size > *buffer_size) {
197 memcpy(p, &res_options, sizeof(res_options));
198 p += sizeof(res_options);
200 memcpy(p, &op_id, sizeof(int));
203 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
206 memcpy(p, &type, sizeof(int));
209 memcpy(p, str, size + 1);
214 addr = va_arg(ap, void *);
215 len = va_arg(ap, socklen_t);
216 type = va_arg(ap, int);
218 desired_size = sizeof(res_options) + sizeof(int) +
219 sizeof(enum nss_lookup_type) + sizeof(int) +
220 sizeof(socklen_t) + len;
222 if (desired_size > *buffer_size) {
228 memcpy(p, &res_options, sizeof(res_options));
229 p += sizeof(res_options);
231 memcpy(p, &op_id, sizeof(int));
234 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
237 memcpy(p, &type, sizeof(int));
240 memcpy(p, &len, sizeof(socklen_t));
241 p += sizeof(socklen_t);
243 memcpy(p, addr, len);
248 /* should be unreachable */
253 *buffer_size = desired_size;
258 host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
267 struct hostent new_ht;
268 size_t desired_size, aliases_size, addr_size, size;
271 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) {
273 str = va_arg(ap, char *);
274 type = va_arg(ap, int);
277 addr = va_arg(ap, void *);
278 len = va_arg(ap, socklen_t);
279 type = va_arg(ap, int);
282 /* should be unreachable */
285 ht = va_arg(ap, struct hostent *);
287 desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *);
288 if (ht->h_name != NULL)
289 desired_size += strlen(ht->h_name) + 1;
291 if (ht->h_aliases != NULL) {
293 for (iter = ht->h_aliases; *iter; ++iter) {
294 desired_size += strlen(*iter) + 1;
298 desired_size += _ALIGNBYTES +
299 (aliases_size + 1) * sizeof(char *);
302 if (ht->h_addr_list != NULL) {
304 for (iter = ht->h_addr_list; *iter; ++iter)
307 desired_size += addr_size * _ALIGN(ht->h_length);
308 desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *);
311 if (desired_size > *buffer_size) {
312 /* this assignment is here for future use */
313 *buffer_size = desired_size;
317 memcpy(&new_ht, ht, sizeof(struct hostent));
318 memset(buffer, 0, desired_size);
320 *buffer_size = desired_size;
321 p = buffer + sizeof(struct hostent) + sizeof(char *);
322 memcpy(buffer + sizeof(struct hostent), &p, sizeof(char *));
323 p = (char *)_ALIGN(p);
325 if (new_ht.h_name != NULL) {
326 size = strlen(new_ht.h_name);
327 memcpy(p, new_ht.h_name, size);
332 if (new_ht.h_aliases != NULL) {
333 p = (char *)_ALIGN(p);
334 memcpy(p, new_ht.h_aliases, sizeof(char *) * aliases_size);
335 new_ht.h_aliases = (char **)p;
336 p += sizeof(char *) * (aliases_size + 1);
338 for (iter = new_ht.h_aliases; *iter; ++iter) {
339 size = strlen(*iter);
340 memcpy(p, *iter, size);
346 if (new_ht.h_addr_list != NULL) {
347 p = (char *)_ALIGN(p);
348 memcpy(p, new_ht.h_addr_list, sizeof(char *) * addr_size);
349 new_ht.h_addr_list = (char **)p;
350 p += sizeof(char *) * (addr_size + 1);
352 size = _ALIGN(new_ht.h_length);
353 for (iter = new_ht.h_addr_list; *iter; ++iter) {
354 memcpy(p, *iter, size);
359 memcpy(buffer, &new_ht, sizeof(struct hostent));
364 host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
376 size_t orig_buf_size;
378 switch ((enum nss_lookup_type)(uintptr_t)cache_mdata) {
380 str = va_arg(ap, char *);
381 type = va_arg(ap, int);
384 addr = va_arg(ap, void *);
385 len = va_arg(ap, socklen_t);
386 type = va_arg(ap, int);
389 /* should be unreachable */
393 ht = va_arg(ap, struct hostent *);
394 orig_buf = va_arg(ap, char *);
395 orig_buf_size = va_arg(ap, size_t);
398 buffer_size - sizeof(struct hostent) - sizeof(char *)) {
403 memcpy(ht, buffer, sizeof(struct hostent));
404 memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *));
406 orig_buf = (char *)_ALIGN(orig_buf);
407 memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) +
408 _ALIGN(p) - (size_t)p,
409 buffer_size - sizeof(struct hostent) - sizeof(char *) -
410 _ALIGN(p) + (size_t)p);
411 p = (char *)_ALIGN(p);
413 NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *);
414 if (ht->h_aliases != NULL) {
415 NS_APPLY_OFFSET(ht->h_aliases, orig_buf, p, char **);
417 for (iter = ht->h_aliases; *iter; ++iter)
418 NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
421 if (ht->h_addr_list != NULL) {
422 NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **);
424 for (iter = ht->h_addr_list; *iter; ++iter)
425 NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
428 *((struct hostent **)retval) = ht;
431 #endif /* NS_CACHING */
434 fakeaddr(const char *name, int af, struct hostent *hp, char *buf,
435 size_t buflen, res_state statp)
437 struct hostent_data *hed;
440 if ((hed = __hostent_data_init()) == NULL) {
442 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
446 if ((af != AF_INET ||
447 inet_aton(name, (struct in_addr *)hed->host_addr) != 1) &&
448 inet_pton(af, name, hed->host_addr) != 1) {
449 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
452 strncpy(hed->hostbuf, name, MAXDNAME);
453 hed->hostbuf[MAXDNAME] = '\0';
454 if (af == AF_INET && (statp->options & RES_USE_INET6) != 0U) {
455 _map_v4v6_address((char *)hed->host_addr,
456 (char *)hed->host_addr);
462 he.h_length = NS_INADDRSZ;
465 he.h_length = NS_IN6ADDRSZ;
468 errno = EAFNOSUPPORT;
469 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
472 he.h_name = hed->hostbuf;
473 he.h_aliases = hed->host_aliases;
474 hed->host_aliases[0] = NULL;
475 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
476 hed->h_addr_ptrs[1] = NULL;
477 he.h_addr_list = hed->h_addr_ptrs;
478 if (__copy_hostent(&he, hp, buf, buflen) != 0) {
479 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
482 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
487 gethostbyname_r(const char *name, struct hostent *he, char *buffer,
488 size_t buflen, struct hostent **result, int *h_errnop)
492 statp = __res_state();
493 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
494 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
497 if (statp->options & RES_USE_INET6) {
498 if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) {
502 if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen,
503 result, h_errnop, statp) == 0)
506 return (gethostbyname_internal(name, AF_INET, he, buffer, buflen,
507 result, h_errnop, statp));
511 gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer,
512 size_t buflen, struct hostent **result, int *h_errnop)
516 statp = __res_state();
517 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
518 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
521 return (gethostbyname_internal(name, af, he, buffer, buflen, result,
526 gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf,
527 size_t buflen, struct hostent **result, int *h_errnop, res_state statp)
530 int rval, ret_errno = 0;
534 static const nss_cache_info cache_info =
535 NS_COMMON_CACHE_INFO_INITIALIZER(
536 hosts, (void *)nss_lt_name,
537 host_id_func, host_marshal_func, host_unmarshal_func);
539 static const ns_dtab dtab[] = {
540 NS_FILES_CB(_ht_gethostbyname, NULL)
541 { NSSRC_DNS, _dns_gethostbyname, NULL },
542 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
544 NS_CACHE_CB(&cache_info)
554 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
555 *h_errnop = statp->res_h_errno;
556 errno = EAFNOSUPPORT;
561 * if there aren't any dots, it could be a user-level alias.
562 * this is also done in res_query() since we are not the only
563 * function that looks up host names.
565 if (!strchr(name, '.') &&
566 (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
569 if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
574 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
575 "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
576 &ret_errno, h_errnop);
578 if (rval != NS_SUCCESS) {
580 return ((ret_errno != 0) ? ret_errno : -1);
586 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
587 char *buf, size_t buflen, struct hostent **result, int *h_errnop)
589 const u_char *uaddr = (const u_char *)addr;
590 const struct in6_addr *addr6;
592 int rval, ret_errno = 0;
596 static const nss_cache_info cache_info =
597 NS_COMMON_CACHE_INFO_INITIALIZER(
598 hosts, (void *)nss_lt_id,
599 host_id_func, host_marshal_func, host_unmarshal_func);
601 static const ns_dtab dtab[] = {
602 NS_FILES_CB(_ht_gethostbyaddr, NULL)
603 { NSSRC_DNS, _dns_gethostbyaddr, NULL },
604 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
606 NS_CACHE_CB(&cache_info)
611 statp = __res_state();
612 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
613 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
614 *h_errnop = statp->res_h_errno;
618 if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
619 addr6 = (const struct in6_addr *)addr;
620 if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
621 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
622 *h_errnop = statp->res_h_errno;
625 if (IN6_IS_ADDR_V4MAPPED(addr6) ||
626 IN6_IS_ADDR_V4COMPAT(addr6)) {
628 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
641 errno = EAFNOSUPPORT;
642 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
643 *h_errnop = statp->res_h_errno;
648 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
649 *h_errnop = statp->res_h_errno;
653 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
654 "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
655 &ret_errno, h_errnop);
657 if (rval != NS_SUCCESS) {
659 return ((ret_errno != 0) ? ret_errno : -1);
665 gethostbyname(const char *name)
668 struct hostent *rval;
671 if ((hd = __hostdata_init()) == NULL)
673 if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
680 gethostbyname2(const char *name, int af)
683 struct hostent *rval;
686 if ((hd = __hostdata_init()) == NULL)
688 if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
689 &rval, &ret_h_errno) != 0)
695 gethostbyaddr(const void *addr, socklen_t len, int af)
698 struct hostent *rval;
701 if ((hd = __hostdata_init()) == NULL)
703 if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
704 sizeof(hd->data), &rval, &ret_h_errno) != 0)
710 sethostent(int stayopen)
712 struct hostent_data *hed;
714 if ((hed = __hostent_data_init()) == NULL)
716 _sethosthtent(stayopen, hed);
717 _sethostdnsent(stayopen);
723 struct hostent_data *hed;
725 if ((hed = __hostent_data_init()) == NULL)