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)
53static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
54#endif /* LIBC_SCCS and not lint */
55#include <sys/cdefs.h>
56__FBSDID("$FreeBSD$");
57
58#include <sys/param.h>
59#include <sys/socket.h>
60#include <netinet/in.h>
61#include <arpa/inet.h>
62#include <netdb.h>
63#include <stdio.h>
64#include <ctype.h>
65#include <errno.h>
66#include <string.h>
67#include <stdarg.h>
68#include <nsswitch.h>
69#include <arpa/nameser.h>	/* XXX */
70#include <resolv.h>		/* XXX */
71#include "netdb_private.h"
72
73void
74_sethosthtent(int f, struct hostent_data *hed)
75{
76	if (!hed->hostf)
77		hed->hostf = fopen(_PATH_HOSTS, "re");
78	else
79		rewind(hed->hostf);
80	hed->stayopen = f;
81}
82
83void
84_endhosthtent(struct hostent_data *hed)
85{
86	if (hed->hostf && !hed->stayopen) {
87		(void) fclose(hed->hostf);
88		hed->hostf = NULL;
89	}
90}
91
92static int
93gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
94    res_state statp)
95{
96	char *p, *bp, *ep;
97	char *cp, **q;
98	int af, len;
99	char hostbuf[BUFSIZ + 1];
100
101	if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "re"))) {
102		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
103		return (-1);
104	}
105 again:
106	if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
107		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
108		return (-1);
109	}
110	if (*p == '#')
111		goto again;
112	cp = strpbrk(p, "#\n");
113	if (cp != NULL)
114		*cp = '\0';
115	if (!(cp = strpbrk(p, " \t")))
116		goto again;
117	*cp++ = '\0';
118	if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
119		af = AF_INET6;
120		len = IN6ADDRSZ;
121	} else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
122		if (mapped) {
123			_map_v4v6_address((char *)hed->host_addr,
124			    (char *)hed->host_addr);
125			af = AF_INET6;
126			len = IN6ADDRSZ;
127		} else {
128			af = AF_INET;
129			len = INADDRSZ;
130		}
131	} else {
132		goto again;
133	}
134	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
135	hed->h_addr_ptrs[1] = NULL;
136	he->h_addr_list = hed->h_addr_ptrs;
137	he->h_length = len;
138	he->h_addrtype = af;
139	while (*cp == ' ' || *cp == '\t')
140		cp++;
141	bp = hed->hostbuf;
142	ep = hed->hostbuf + sizeof hed->hostbuf;
143	he->h_name = bp;
144	q = he->h_aliases = hed->host_aliases;
145	if ((p = strpbrk(cp, " \t")) != NULL)
146		*p++ = '\0';
147	len = strlen(cp) + 1;
148	if (ep - bp < len) {
149		RES_SET_H_ERRNO(statp, NO_RECOVERY);
150		return (-1);
151	}
152	strlcpy(bp, cp, ep - bp);
153	bp += len;
154	cp = p;
155	while (cp && *cp) {
156		if (*cp == ' ' || *cp == '\t') {
157			cp++;
158			continue;
159		}
160		if (q >= &hed->host_aliases[_MAXALIASES - 1])
161			break;
162		if ((p = strpbrk(cp, " \t")) != NULL)
163			*p++ = '\0';
164		len = strlen(cp) + 1;
165		if (ep - bp < len)
166			break;
167		strlcpy(bp, cp, ep - bp);
168		*q++ = bp;
169		bp += len;
170		cp = p;
171	}
172	*q = NULL;
173	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
174	return (0);
175}
176
177int
178gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
179    struct hostent **result, int *h_errnop)
180{
181	struct hostent_data *hed;
182	struct hostent he;
183	res_state statp;
184
185	statp = __res_state();
186	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
187		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
188		*h_errnop = statp->res_h_errno;
189		return (-1);
190	}
191	if ((hed = __hostent_data_init()) == NULL) {
192		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
193		*h_errnop = statp->res_h_errno;
194		return (-1);
195	}
196	if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
197		return (-1);
198	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
199		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
200		*h_errnop = statp->res_h_errno;
201		return ((errno != 0) ? errno : -1);
202	}
203	*result = hptr;
204	return (0);
205}
206
207struct hostent *
208gethostent(void)
209{
210	struct hostdata *hd;
211	struct hostent *rval;
212	int ret_h_errno;
213
214	if ((hd = __hostdata_init()) == NULL)
215		return (NULL);
216	if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
217	    &ret_h_errno) != 0)
218		return (NULL);
219	return (rval);
220}
221
222int
223_ht_gethostbyname(void *rval, void *cb_data, va_list ap)
224{
225	const char *name;
226	int af;
227	char *buffer;
228	size_t buflen;
229	int *errnop, *h_errnop;
230	struct hostent *hptr, he;
231	struct hostent_data *hed;
232	char **cp;
233	res_state statp;
234	int error;
235
236	name = va_arg(ap, const char *);
237	af = va_arg(ap, int);
238	hptr = va_arg(ap, struct hostent *);
239	buffer = va_arg(ap, char *);
240	buflen = va_arg(ap, size_t);
241	errnop = va_arg(ap, int *);
242	h_errnop = va_arg(ap, int *);
243
244	*((struct hostent **)rval) = NULL;
245
246	statp = __res_state();
247	if ((hed = __hostent_data_init()) == NULL) {
248		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
249		*h_errnop = statp->res_h_errno;
250		return (NS_NOTFOUND);
251	}
252
253	_sethosthtent(0, hed);
254	while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
255		if (he.h_addrtype != af)
256			continue;
257		if (he.h_addrtype == AF_INET &&
258		    statp->options & RES_USE_INET6) {
259			_map_v4v6_address(he.h_addr, he.h_addr);
260			he.h_length = IN6ADDRSZ;
261			he.h_addrtype = AF_INET6;
262		}
263		if (strcasecmp(he.h_name, name) == 0)
264			break;
265		for (cp = he.h_aliases; *cp != 0; cp++)
266			if (strcasecmp(*cp, name) == 0)
267				goto found;
268	}
269found:
270	_endhosthtent(hed);
271
272	if (error != 0) {
273		*h_errnop = statp->res_h_errno;
274		return (NS_NOTFOUND);
275	}
276	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
277		*errnop = errno;
278		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
279		*h_errnop = statp->res_h_errno;
280		return (NS_RETURN);
281	}
282	*((struct hostent **)rval) = hptr;
283	return (NS_SUCCESS);
284}
285
286int
287_ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
288{
289	const void *addr;
290	socklen_t len;
291	int af;
292	char *buffer;
293	size_t buflen;
294	int *errnop, *h_errnop;
295	struct hostent *hptr, he;
296	struct hostent_data *hed;
297	res_state statp;
298	int error;
299
300	addr = va_arg(ap, const void *);
301	len = va_arg(ap, socklen_t);
302	af = va_arg(ap, int);
303	hptr = va_arg(ap, struct hostent *);
304	buffer = va_arg(ap, char *);
305	buflen = va_arg(ap, size_t);
306	errnop = va_arg(ap, int *);
307	h_errnop = va_arg(ap, int *);
308
309	*((struct hostent **)rval) = NULL;
310
311	statp = __res_state();
312	if ((hed = __hostent_data_init()) == NULL) {
313		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
314		*h_errnop = statp->res_h_errno;
315		return (NS_NOTFOUND);
316	}
317
318	_sethosthtent(0, hed);
319	while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
320		if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
321			if (he.h_addrtype == AF_INET &&
322			    statp->options & RES_USE_INET6) {
323				_map_v4v6_address(he.h_addr, he.h_addr);
324				he.h_length = IN6ADDRSZ;
325				he.h_addrtype = AF_INET6;
326			}
327			break;
328		}
329	_endhosthtent(hed);
330
331	if (error != 0)
332		return (NS_NOTFOUND);
333	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
334		*errnop = errno;
335		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
336		*h_errnop = statp->res_h_errno;
337		return (NS_RETURN);
338	}
339	*((struct hostent **)rval) = hptr;
340	return (NS_SUCCESS);
341}
342