xref: /freebsd/lib/libc/net/gethostbyht.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1985, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  * -
31  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32  *
33  * Permission to use, copy, modify, and distribute this software for any
34  * purpose with or without fee is hereby granted, provided that the above
35  * copyright notice and this permission notice appear in all copies, and that
36  * the name of Digital Equipment Corporation not be used in advertising or
37  * publicity pertaining to distribution of the document or software without
38  * specific, written prior permission.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47  * SOFTWARE.
48  * -
49  * --Copyright--
50  */
51 
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
54 #endif /* LIBC_SCCS and not lint */
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59 #include <netdb.h>
60 #include <stdio.h>
61 #include <ctype.h>
62 #include <errno.h>
63 #include <string.h>
64 #include <stdarg.h>
65 #include <nsswitch.h>
66 #include <arpa/nameser.h>	/* XXX */
67 #include <resolv.h>		/* XXX */
68 #include "netdb_private.h"
69 
70 void
71 _sethosthtent(int f, struct hostent_data *hed)
72 {
73 	if (!hed->hostf)
74 		hed->hostf = fopen(_PATH_HOSTS, "re");
75 	else
76 		rewind(hed->hostf);
77 	hed->stayopen = f;
78 }
79 
80 void
81 _endhosthtent(struct hostent_data *hed)
82 {
83 	if (hed->hostf && !hed->stayopen) {
84 		(void) fclose(hed->hostf);
85 		hed->hostf = NULL;
86 	}
87 }
88 
89 static int
90 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
91     res_state statp)
92 {
93 	char *p, *bp, *ep;
94 	char *cp, **q;
95 	int af, len;
96 	char hostbuf[BUFSIZ + 1];
97 
98 	if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "re"))) {
99 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
100 		return (-1);
101 	}
102  again:
103 	if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
104 		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
105 		return (-1);
106 	}
107 	if (*p == '#')
108 		goto again;
109 	cp = strpbrk(p, "#\n");
110 	if (cp != NULL)
111 		*cp = '\0';
112 	if (!(cp = strpbrk(p, " \t")))
113 		goto again;
114 	*cp++ = '\0';
115 	if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
116 		af = AF_INET6;
117 		len = IN6ADDRSZ;
118 	} else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
119 		if (mapped) {
120 			_map_v4v6_address((char *)hed->host_addr,
121 			    (char *)hed->host_addr);
122 			af = AF_INET6;
123 			len = IN6ADDRSZ;
124 		} else {
125 			af = AF_INET;
126 			len = INADDRSZ;
127 		}
128 	} else {
129 		goto again;
130 	}
131 	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
132 	hed->h_addr_ptrs[1] = NULL;
133 	he->h_addr_list = hed->h_addr_ptrs;
134 	he->h_length = len;
135 	he->h_addrtype = af;
136 	while (*cp == ' ' || *cp == '\t')
137 		cp++;
138 	bp = hed->hostbuf;
139 	ep = hed->hostbuf + sizeof hed->hostbuf;
140 	he->h_name = bp;
141 	q = he->h_aliases = hed->host_aliases;
142 	if ((p = strpbrk(cp, " \t")) != NULL)
143 		*p++ = '\0';
144 	len = strlen(cp) + 1;
145 	if (ep - bp < len) {
146 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
147 		return (-1);
148 	}
149 	strlcpy(bp, cp, ep - bp);
150 	bp += len;
151 	cp = p;
152 	while (cp && *cp) {
153 		if (*cp == ' ' || *cp == '\t') {
154 			cp++;
155 			continue;
156 		}
157 		if (q >= &hed->host_aliases[_MAXALIASES - 1])
158 			break;
159 		if ((p = strpbrk(cp, " \t")) != NULL)
160 			*p++ = '\0';
161 		len = strlen(cp) + 1;
162 		if (ep - bp < len)
163 			break;
164 		strlcpy(bp, cp, ep - bp);
165 		*q++ = bp;
166 		bp += len;
167 		cp = p;
168 	}
169 	*q = NULL;
170 	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
171 	return (0);
172 }
173 
174 int
175 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
176     struct hostent **result, int *h_errnop)
177 {
178 	struct hostent_data *hed;
179 	struct hostent he;
180 	res_state statp;
181 
182 	statp = __res_state();
183 	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
184 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
185 		*h_errnop = statp->res_h_errno;
186 		return (-1);
187 	}
188 	if ((hed = __hostent_data_init()) == NULL) {
189 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
190 		*h_errnop = statp->res_h_errno;
191 		return (-1);
192 	}
193 	if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
194 		return (-1);
195 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
196 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
197 		*h_errnop = statp->res_h_errno;
198 		return ((errno != 0) ? errno : -1);
199 	}
200 	*result = hptr;
201 	return (0);
202 }
203 
204 struct hostent *
205 gethostent(void)
206 {
207 	struct hostdata *hd;
208 	struct hostent *rval;
209 	int ret_h_errno;
210 
211 	if ((hd = __hostdata_init()) == NULL)
212 		return (NULL);
213 	if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
214 	    &ret_h_errno) != 0)
215 		return (NULL);
216 	return (rval);
217 }
218 
219 int
220 _ht_gethostbyname(void *rval, void *cb_data, va_list ap)
221 {
222 	const char *name;
223 	int af;
224 	char *buffer;
225 	size_t buflen;
226 	int *errnop, *h_errnop;
227 	struct hostent *hptr, he;
228 	struct hostent_data *hed;
229 	char **cp;
230 	res_state statp;
231 	int error;
232 
233 	name = va_arg(ap, const char *);
234 	af = va_arg(ap, int);
235 	hptr = va_arg(ap, struct hostent *);
236 	buffer = va_arg(ap, char *);
237 	buflen = va_arg(ap, size_t);
238 	errnop = va_arg(ap, int *);
239 	h_errnop = va_arg(ap, int *);
240 
241 	*((struct hostent **)rval) = NULL;
242 
243 	statp = __res_state();
244 	if ((hed = __hostent_data_init()) == NULL) {
245 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
246 		*h_errnop = statp->res_h_errno;
247 		return (NS_NOTFOUND);
248 	}
249 
250 	_sethosthtent(0, hed);
251 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
252 		if (he.h_addrtype != af)
253 			continue;
254 		if (he.h_addrtype == AF_INET &&
255 		    statp->options & RES_USE_INET6) {
256 			_map_v4v6_address(he.h_addr, he.h_addr);
257 			he.h_length = IN6ADDRSZ;
258 			he.h_addrtype = AF_INET6;
259 		}
260 		if (strcasecmp(he.h_name, name) == 0)
261 			break;
262 		for (cp = he.h_aliases; *cp != 0; cp++)
263 			if (strcasecmp(*cp, name) == 0)
264 				goto found;
265 	}
266 found:
267 	_endhosthtent(hed);
268 
269 	if (error != 0) {
270 		*h_errnop = statp->res_h_errno;
271 		return (NS_NOTFOUND);
272 	}
273 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
274 		*errnop = errno;
275 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
276 		*h_errnop = statp->res_h_errno;
277 		return (NS_RETURN);
278 	}
279 	*((struct hostent **)rval) = hptr;
280 	return (NS_SUCCESS);
281 }
282 
283 int
284 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
285 {
286 	const void *addr;
287 	socklen_t len;
288 	int af;
289 	char *buffer;
290 	size_t buflen;
291 	int *errnop, *h_errnop;
292 	struct hostent *hptr, he;
293 	struct hostent_data *hed;
294 	res_state statp;
295 	int error;
296 
297 	addr = va_arg(ap, const void *);
298 	len = va_arg(ap, socklen_t);
299 	af = va_arg(ap, int);
300 	hptr = va_arg(ap, struct hostent *);
301 	buffer = va_arg(ap, char *);
302 	buflen = va_arg(ap, size_t);
303 	errnop = va_arg(ap, int *);
304 	h_errnop = va_arg(ap, int *);
305 
306 	*((struct hostent **)rval) = NULL;
307 
308 	statp = __res_state();
309 	if ((hed = __hostent_data_init()) == NULL) {
310 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
311 		*h_errnop = statp->res_h_errno;
312 		return (NS_NOTFOUND);
313 	}
314 
315 	_sethosthtent(0, hed);
316 	while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
317 		if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
318 			if (he.h_addrtype == AF_INET &&
319 			    statp->options & RES_USE_INET6) {
320 				_map_v4v6_address(he.h_addr, he.h_addr);
321 				he.h_length = IN6ADDRSZ;
322 				he.h_addrtype = AF_INET6;
323 			}
324 			break;
325 		}
326 	_endhosthtent(hed);
327 
328 	if (error != 0)
329 		return (NS_NOTFOUND);
330 	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
331 		*errnop = errno;
332 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
333 		*h_errnop = statp->res_h_errno;
334 		return (NS_RETURN);
335 	}
336 	*((struct hostent **)rval) = hptr;
337 	return (NS_SUCCESS);
338 }
339