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 <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <arpa/nameser.h>
44 #include <resolv.h> /* XXX */
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
50 #include "netdb_private.h"
54 _gethostbynis(const char *name, char *map, int af, struct hostent *he,
55 struct hostent_data *hed)
60 int resultlen, size, addrok = 0;
61 char ypbuf[YPMAXRECORD + 2];
64 statp = __res_state();
74 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
78 if (hed->yp_domain == (char *)NULL)
79 if (yp_get_default_domain (&hed->yp_domain)) {
80 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
84 if (yp_match(hed->yp_domain, map, name, strlen(name), &result,
86 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
90 /* avoid potential memory leak */
91 bcopy((char *)result, (char *)&ypbuf, resultlen);
92 ypbuf[resultlen] = '\0';
94 result = (char *)&ypbuf;
96 if ((cp = strchr(result, '\n')))
99 cp = strpbrk(result, " \t");
101 he->h_addr_list = hed->h_addr_ptrs;
102 he->h_addr = (char *)hed->host_addr;
105 addrok = inet_aton(result, (struct in_addr *)hed->host_addr);
108 if (statp->options & RES_USE_INET6) {
109 _map_v4v6_address((char *)hed->host_addr,
110 (char *)hed->host_addr);
116 addrok = inet_pton(af, result, hed->host_addr);
120 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
123 he->h_addr_list[1] = NULL;
126 while (*cp == ' ' || *cp == '\t')
129 ep = hed->hostbuf + sizeof hed->hostbuf;
131 q = he->h_aliases = hed->host_aliases;
132 p = strpbrk(cp, " \t");
135 size = strlen(cp) + 1;
136 if (ep - bp < size) {
137 RES_SET_H_ERRNO(statp, NO_RECOVERY);
140 strlcpy(bp, cp, ep - bp);
144 if (*cp == ' ' || *cp == '\t') {
148 if (q >= &hed->host_aliases[_MAXALIASES - 1])
150 p = strpbrk(cp, " \t");
153 size = strlen(cp) + 1;
156 strlcpy(bp, cp, ep - bp);
166 _gethostbynisname_r(const char *name, int af, struct hostent *he,
167 struct hostent_data *hed)
173 map = "hosts.byname";
176 map = "ipnodes.byname";
179 return (_gethostbynis(name, map, af, he, hed));
183 _gethostbynisaddr_r(const void *addr, socklen_t len, int af,
184 struct hostent *he, struct hostent_data *hed)
191 map = "hosts.byaddr";
194 map = "ipnodes.byaddr";
197 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
199 return (_gethostbynis(numaddr, map, af, he, hed));
204 _nis_gethostbyname(void *rval, void *cb_data, va_list ap)
211 int *errnop, *h_errnop;
212 struct hostent *hptr, he;
213 struct hostent_data *hed;
216 name = va_arg(ap, const char *);
217 af = va_arg(ap, int);
218 hptr = va_arg(ap, struct hostent *);
219 buffer = va_arg(ap, char *);
220 buflen = va_arg(ap, size_t);
221 errnop = va_arg(ap, int *);
222 h_errnop = va_arg(ap, int *);
224 *((struct hostent **)rval) = NULL;
226 statp = __res_state();
227 if ((hed = __hostent_data_init()) == NULL) {
228 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
229 *h_errnop = statp->res_h_errno;
230 return (NS_NOTFOUND);
233 if (_gethostbynisname_r(name, af, &he, hed) != 0) {
234 *h_errnop = statp->res_h_errno;
235 return (NS_NOTFOUND);
237 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
239 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
240 *h_errnop = statp->res_h_errno;
243 *((struct hostent **)rval) = hptr;
246 *((struct hostent **)rval) = NULL;
252 _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
260 int *errnop, *h_errnop;
261 struct hostent *hptr, he;
262 struct hostent_data *hed;
265 addr = va_arg(ap, const void *);
266 len = va_arg(ap, socklen_t);
267 af = va_arg(ap, int);
268 hptr = va_arg(ap, struct hostent *);
269 buffer = va_arg(ap, char *);
270 buflen = va_arg(ap, size_t);
271 errnop = va_arg(ap, int *);
272 h_errnop = va_arg(ap, int *);
274 *((struct hostent **)rval) = NULL;
276 statp = __res_state();
277 if ((hed = __hostent_data_init()) == NULL) {
278 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
279 *h_errnop = statp->res_h_errno;
280 return (NS_NOTFOUND);
283 if (_gethostbynisaddr_r(addr, len, af, &he, hed) != 0) {
284 *h_errnop = statp->res_h_errno;
285 return (NS_NOTFOUND);
287 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
289 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
290 *h_errnop = statp->res_h_errno;
293 *((struct hostent **)rval) = hptr;
296 *((struct hostent **)rval) = NULL;