2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
42 #include <resolv.h> /* XXX */
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
48 #include "netdb_private.h"
52 _gethostbynis(const char *name, char *map, int af, struct hostent *he,
53 struct hostent_data *hed)
58 int resultlen, size, addrok = 0;
62 statp = __res_state();
72 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
76 if (hed->yp_domain == (char *)NULL)
77 if (yp_get_default_domain (&hed->yp_domain)) {
78 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
82 if (yp_match(hed->yp_domain, map, name, strlen(name), &result,
84 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
88 /* avoid potential memory leak */
89 ypbuf = alloca(resultlen + 2);
90 bcopy(result, ypbuf, resultlen);
91 ypbuf[resultlen] = '\0';
95 if ((cp = strchr(result, '\n')))
98 cp = strpbrk(result, " \t");
100 he->h_addr_list = hed->h_addr_ptrs;
101 he->h_addr = (char *)hed->host_addr;
104 addrok = inet_aton(result, (struct in_addr *)hed->host_addr);
107 if (statp->options & RES_USE_INET6) {
108 _map_v4v6_address((char *)hed->host_addr,
109 (char *)hed->host_addr);
115 addrok = inet_pton(af, result, hed->host_addr);
119 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
122 he->h_addr_list[1] = NULL;
125 while (*cp == ' ' || *cp == '\t')
128 ep = hed->hostbuf + sizeof hed->hostbuf;
130 q = he->h_aliases = hed->host_aliases;
131 p = strpbrk(cp, " \t");
134 size = strlen(cp) + 1;
135 if (ep - bp < size) {
136 RES_SET_H_ERRNO(statp, NO_RECOVERY);
139 strlcpy(bp, cp, ep - bp);
143 if (*cp == ' ' || *cp == '\t') {
147 if (q >= &hed->host_aliases[_MAXALIASES - 1])
149 p = strpbrk(cp, " \t");
152 size = strlen(cp) + 1;
155 strlcpy(bp, cp, ep - bp);
165 _gethostbynisname_r(const char *name, int af, struct hostent *he,
166 struct hostent_data *hed)
172 map = "hosts.byname";
175 map = "ipnodes.byname";
178 return (_gethostbynis(name, map, af, he, hed));
182 _gethostbynisaddr_r(const void *addr, socklen_t len, int af,
183 struct hostent *he, struct hostent_data *hed)
190 map = "hosts.byaddr";
193 map = "ipnodes.byaddr";
196 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
198 return (_gethostbynis(numaddr, map, af, he, hed));
203 _nis_gethostbyname(void *rval, void *cb_data, va_list ap)
210 int *errnop, *h_errnop;
211 struct hostent *hptr, he;
212 struct hostent_data *hed;
215 name = va_arg(ap, const char *);
216 af = va_arg(ap, int);
217 hptr = va_arg(ap, struct hostent *);
218 buffer = va_arg(ap, char *);
219 buflen = va_arg(ap, size_t);
220 errnop = va_arg(ap, int *);
221 h_errnop = va_arg(ap, int *);
223 *((struct hostent **)rval) = NULL;
225 statp = __res_state();
226 if ((hed = __hostent_data_init()) == NULL) {
227 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
228 *h_errnop = statp->res_h_errno;
229 return (NS_NOTFOUND);
232 if (_gethostbynisname_r(name, af, &he, hed) != 0) {
233 *h_errnop = statp->res_h_errno;
234 return (NS_NOTFOUND);
236 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
238 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
239 *h_errnop = statp->res_h_errno;
242 *((struct hostent **)rval) = hptr;
245 *((struct hostent **)rval) = NULL;
251 _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
259 int *errnop, *h_errnop;
260 struct hostent *hptr, he;
261 struct hostent_data *hed;
264 addr = va_arg(ap, const void *);
265 len = va_arg(ap, socklen_t);
266 af = va_arg(ap, int);
267 hptr = va_arg(ap, struct hostent *);
268 buffer = va_arg(ap, char *);
269 buflen = va_arg(ap, size_t);
270 errnop = va_arg(ap, int *);
271 h_errnop = va_arg(ap, int *);
273 *((struct hostent **)rval) = NULL;
275 statp = __res_state();
276 if ((hed = __hostent_data_init()) == NULL) {
277 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
278 *h_errnop = statp->res_h_errno;
279 return (NS_NOTFOUND);
282 if (_gethostbynisaddr_r(addr, len, af, &he, hed) != 0) {
283 *h_errnop = statp->res_h_errno;
284 return (NS_NOTFOUND);
286 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
288 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
289 *h_errnop = statp->res_h_errno;
292 *((struct hostent **)rval) = hptr;
295 *((struct hostent **)rval) = NULL;