2 * Copyright (c) 1994, Garrett Wollman
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include "namespace.h"
30 #include "reentrant.h"
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
43 #include <arpa/nameser.h> /* XXX hack for _res */
44 #include <resolv.h> /* XXX hack for _res */
45 #include "un-namespace.h"
46 #include "netdb_private.h"
51 static int gethostbyname_internal(const char *, int, struct hostent *, char *,
52 size_t, struct hostent **, int *, res_state);
54 /* Host lookup order if nsswitch.conf is broken or nonexistent */
55 static const ns_src default_src[] = {
56 { NSSRC_FILES, NS_SUCCESS },
57 { NSSRC_DNS, NS_SUCCESS },
61 static int host_id_func(char *, size_t *, va_list, void *);
62 static int host_marshal_func(char *, size_t *, void *, va_list, void *);
63 static int host_unmarshal_func(char *, size_t, void *, va_list, void *);
66 NETDB_THREAD_ALLOC(hostent)
67 NETDB_THREAD_ALLOC(hostent_data)
68 NETDB_THREAD_ALLOC(hostdata)
71 hostent_free(void *ptr)
77 hostent_data_free(void *ptr)
79 struct hostent_data *hed = ptr;
89 hostdata_free(void *ptr)
95 __copy_hostent(struct hostent *he, struct hostent *hptr, char *buf,
103 /* Find out the amount of space required to store the answer. */
104 nptr = 2; /* NULL ptrs */
105 len = (char *)ALIGN(buf) - buf;
106 for (i = 0; he->h_addr_list[i]; i++, nptr++) {
109 for (i = 0; he->h_aliases[i]; i++, nptr++) {
110 len += strlen(he->h_aliases[i]) + 1;
112 len += strlen(he->h_name) + 1;
113 len += nptr * sizeof(char*);
120 /* copy address size and type */
121 hptr->h_addrtype = he->h_addrtype;
122 n = hptr->h_length = he->h_length;
124 ptr = (char **)ALIGN(buf);
125 cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
127 /* copy address list */
128 hptr->h_addr_list = ptr;
129 for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
130 memcpy(cp, he->h_addr_list[i], n);
131 hptr->h_addr_list[i] = cp;
134 hptr->h_addr_list[i] = NULL;
137 /* copy official name */
138 n = strlen(he->h_name) + 1;
139 strcpy(cp, he->h_name);
144 hptr->h_aliases = ptr;
145 for (i = 0 ; he->h_aliases[i]; i++) {
146 n = strlen(he->h_aliases[i]) + 1;
147 strcpy(cp, he->h_aliases[i]);
148 hptr->h_aliases[i] = cp;
151 hptr->h_aliases[i] = NULL;
158 host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
169 size_t desired_size, size;
170 enum nss_lookup_type lookup_type;
172 int res = NS_UNAVAIL;
174 statp = __res_state();
175 res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
176 RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
178 lookup_type = (enum nss_lookup_type)cache_mdata;
179 switch (lookup_type) {
181 str = va_arg(ap, char *);
182 type = va_arg(ap, int);
185 desired_size = sizeof(res_options) + sizeof(int) +
186 sizeof(enum nss_lookup_type) + sizeof(int) + size + 1;
188 if (desired_size > *buffer_size) {
195 memcpy(p, &res_options, sizeof(res_options));
196 p += sizeof(res_options);
198 memcpy(p, &op_id, sizeof(int));
201 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
204 memcpy(p, &type, sizeof(int));
207 memcpy(p, str, size + 1);
212 addr = va_arg(ap, void *);
213 len = va_arg(ap, socklen_t);
214 type = va_arg(ap, int);
216 desired_size = sizeof(res_options) + sizeof(int) +
217 sizeof(enum nss_lookup_type) + sizeof(int) +
218 sizeof(socklen_t) + len;
220 if (desired_size > *buffer_size) {
226 memcpy(p, &res_options, sizeof(res_options));
227 p += sizeof(res_options);
229 memcpy(p, &op_id, sizeof(int));
232 memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
235 memcpy(p, &type, sizeof(int));
238 memcpy(p, &len, sizeof(socklen_t));
239 p += sizeof(socklen_t);
241 memcpy(p, addr, len);
246 /* should be unreachable */
251 *buffer_size = desired_size;
256 host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
265 struct hostent new_ht;
266 size_t desired_size, aliases_size, addr_size, size;
269 switch ((enum nss_lookup_type)cache_mdata) {
271 str = va_arg(ap, char *);
272 type = va_arg(ap, int);
275 addr = va_arg(ap, void *);
276 len = va_arg(ap, socklen_t);
277 type = va_arg(ap, int);
280 /* should be unreachable */
283 ht = va_arg(ap, struct hostent *);
285 desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *);
286 if (ht->h_name != NULL)
287 desired_size += strlen(ht->h_name) + 1;
289 if (ht->h_aliases != NULL) {
291 for (iter = ht->h_aliases; *iter; ++iter) {
292 desired_size += strlen(*iter) + 1;
296 desired_size += _ALIGNBYTES +
297 (aliases_size + 1) * sizeof(char *);
300 if (ht->h_addr_list != NULL) {
302 for (iter = ht->h_addr_list; *iter; ++iter)
305 desired_size += addr_size * _ALIGN(ht->h_length);
306 desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *);
309 if (desired_size > *buffer_size) {
310 /* this assignment is here for future use */
311 *buffer_size = desired_size;
315 memcpy(&new_ht, ht, sizeof(struct hostent));
316 memset(buffer, 0, desired_size);
318 *buffer_size = desired_size;
319 p = buffer + sizeof(struct hostent) + sizeof(char *);
320 memcpy(buffer + sizeof(struct hostent), &p, sizeof(char *));
321 p = (char *)_ALIGN(p);
323 if (new_ht.h_name != NULL) {
324 size = strlen(new_ht.h_name);
325 memcpy(p, new_ht.h_name, size);
330 if (new_ht.h_aliases != NULL) {
331 p = (char *)_ALIGN(p);
332 memcpy(p, new_ht.h_aliases, sizeof(char *) * aliases_size);
333 new_ht.h_aliases = (char **)p;
334 p += sizeof(char *) * (aliases_size + 1);
336 for (iter = new_ht.h_aliases; *iter; ++iter) {
337 size = strlen(*iter);
338 memcpy(p, *iter, size);
344 if (new_ht.h_addr_list != NULL) {
345 p = (char *)_ALIGN(p);
346 memcpy(p, new_ht.h_addr_list, sizeof(char *) * addr_size);
347 new_ht.h_addr_list = (char **)p;
348 p += sizeof(char *) * (addr_size + 1);
350 size = _ALIGN(new_ht.h_length);
351 for (iter = new_ht.h_addr_list; *iter; ++iter) {
352 memcpy(p, *iter, size);
357 memcpy(buffer, &new_ht, sizeof(struct hostent));
362 host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
374 size_t orig_buf_size;
376 switch ((enum nss_lookup_type)cache_mdata) {
378 str = va_arg(ap, char *);
379 type = va_arg(ap, int);
382 addr = va_arg(ap, void *);
383 len = va_arg(ap, socklen_t);
384 type = va_arg(ap, int);
387 /* should be unreachable */
391 ht = va_arg(ap, struct hostent *);
392 orig_buf = va_arg(ap, char *);
393 orig_buf_size = va_arg(ap, size_t);
396 buffer_size - sizeof(struct hostent) - sizeof(char *)) {
401 memcpy(ht, buffer, sizeof(struct hostent));
402 memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *));
404 orig_buf = (char *)_ALIGN(orig_buf);
405 memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) +
406 _ALIGN(p) - (size_t)p,
407 buffer_size - sizeof(struct hostent) - sizeof(char *) -
408 _ALIGN(p) + (size_t)p);
409 p = (char *)_ALIGN(p);
411 NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *);
412 if (ht->h_aliases != NULL) {
413 NS_APPLY_OFFSET(ht->h_aliases, orig_buf, p, char **);
415 for (iter = ht->h_aliases; *iter; ++iter)
416 NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
419 if (ht->h_addr_list != NULL) {
420 NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **);
422 for (iter = ht->h_addr_list; *iter; ++iter)
423 NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
426 *((struct hostent **)retval) = ht;
429 #endif /* NS_CACHING */
432 fakeaddr(const char *name, int af, struct hostent *hp, char *buf,
433 size_t buflen, res_state statp)
435 struct hostent_data *hed;
438 if ((hed = __hostent_data_init()) == NULL) {
440 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
444 if ((af != AF_INET ||
445 inet_aton(name, (struct in_addr *)hed->host_addr) != 1) &&
446 inet_pton(af, name, hed->host_addr) != 1) {
447 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
450 strncpy(hed->hostbuf, name, MAXDNAME);
451 hed->hostbuf[MAXDNAME] = '\0';
452 if (af == AF_INET && (statp->options & RES_USE_INET6) != 0U) {
453 _map_v4v6_address((char *)hed->host_addr,
454 (char *)hed->host_addr);
460 he.h_length = NS_INADDRSZ;
463 he.h_length = NS_IN6ADDRSZ;
466 errno = EAFNOSUPPORT;
467 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
470 he.h_name = hed->hostbuf;
471 he.h_aliases = hed->host_aliases;
472 hed->host_aliases[0] = NULL;
473 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
474 hed->h_addr_ptrs[1] = NULL;
475 he.h_addr_list = hed->h_addr_ptrs;
476 if (__copy_hostent(&he, hp, buf, buflen) != 0) {
477 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
480 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
485 gethostbyname_r(const char *name, struct hostent *he, char *buffer,
486 size_t buflen, struct hostent **result, int *h_errnop)
490 statp = __res_state();
491 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
492 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
495 if (statp->options & RES_USE_INET6) {
496 if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) {
500 if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen,
501 result, h_errnop, statp) == 0)
504 return (gethostbyname_internal(name, AF_INET, he, buffer, buflen,
505 result, h_errnop, statp));
509 gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer,
510 size_t buflen, struct hostent **result, int *h_errnop)
514 statp = __res_state();
515 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
516 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
519 return (gethostbyname_internal(name, af, he, buffer, buflen, result,
524 gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf,
525 size_t buflen, struct hostent **result, int *h_errnop, res_state statp)
528 int rval, ret_errno = 0;
532 static const nss_cache_info cache_info =
533 NS_COMMON_CACHE_INFO_INITIALIZER(
534 hosts, (void *)nss_lt_name,
535 host_id_func, host_marshal_func, host_unmarshal_func);
537 static const ns_dtab dtab[] = {
538 NS_FILES_CB(_ht_gethostbyname, NULL)
539 { NSSRC_DNS, _dns_gethostbyname, NULL },
540 NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
542 NS_CACHE_CB(&cache_info)
552 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
553 *h_errnop = statp->res_h_errno;
554 errno = EAFNOSUPPORT;
559 * if there aren't any dots, it could be a user-level alias.
560 * this is also done in res_query() since we are not the only
561 * function that looks up host names.
563 if (!strchr(name, '.') &&
564 (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
567 if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
572 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
573 "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
574 &ret_errno, h_errnop);
576 if (rval != NS_SUCCESS) {
578 return ((ret_errno != 0) ? ret_errno : -1);
584 gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
585 char *buf, size_t buflen, struct hostent **result, int *h_errnop)
587 const u_char *uaddr = (const u_char *)addr;
588 const struct in6_addr *addr6;
590 int rval, ret_errno = 0;
594 static const nss_cache_info cache_info =
595 NS_COMMON_CACHE_INFO_INITIALIZER(
596 hosts, (void *)nss_lt_id,
597 host_id_func, host_marshal_func, host_unmarshal_func);
599 static const ns_dtab dtab[] = {
600 NS_FILES_CB(_ht_gethostbyaddr, NULL)
601 { NSSRC_DNS, _dns_gethostbyaddr, NULL },
602 NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
604 NS_CACHE_CB(&cache_info)
609 statp = __res_state();
610 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
611 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
612 *h_errnop = statp->res_h_errno;
616 if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
617 addr6 = (const struct in6_addr *)addr;
618 if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
619 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
620 *h_errnop = statp->res_h_errno;
623 if (IN6_IS_ADDR_V4MAPPED(addr6) ||
624 IN6_IS_ADDR_V4COMPAT(addr6)) {
626 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
639 errno = EAFNOSUPPORT;
640 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
641 *h_errnop = statp->res_h_errno;
646 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
647 *h_errnop = statp->res_h_errno;
651 rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
652 "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
653 &ret_errno, h_errnop);
655 if (rval != NS_SUCCESS) {
657 return ((ret_errno != 0) ? ret_errno : -1);
663 gethostbyname(const char *name)
666 struct hostent *rval;
669 if ((hd = __hostdata_init()) == NULL)
671 if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
678 gethostbyname2(const char *name, int af)
681 struct hostent *rval;
684 if ((hd = __hostdata_init()) == NULL)
686 if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
687 &rval, &ret_h_errno) != 0)
693 gethostbyaddr(const void *addr, socklen_t len, int af)
696 struct hostent *rval;
699 if ((hd = __hostdata_init()) == NULL)
701 if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
702 sizeof(hd->data), &rval, &ret_h_errno) != 0)
708 sethostent(int stayopen)
710 struct hostent_data *hed;
712 if ((hed = __hostent_data_init()) == NULL)
714 _sethosthtent(stayopen, hed);
715 _sethostdnsent(stayopen);
721 struct hostent_data *hed;
723 if ((hed = __hostent_data_init()) == NULL)