1/*	$NetBSD: sethostent.c,v 1.21 2023/12/07 22:33:49 kre Exp $	*/
2
3/*
4 * Copyright (c) 1985, 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
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34#if 0
35static char sccsid[] = "@(#)sethostent.c	8.1 (Berkeley) 6/4/93";
36static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
37#else
38__RCSID("$NetBSD: sethostent.c,v 1.21 2023/12/07 22:33:49 kre Exp $");
39#endif
40#endif /* LIBC_SCCS and not lint */
41
42#include "namespace.h"
43#include <sys/param.h>
44#include <netinet/in.h>
45#include <arpa/nameser.h>
46#include <arpa/inet.h>
47#include <assert.h>
48#include <string.h>
49#include <nsswitch.h>
50#include <netdb.h>
51#include <resolv.h>
52#include <errno.h>
53#include <stdlib.h>
54
55#include "hostent.h"
56
57#ifdef __weak_alias
58__weak_alias(sethostent,_sethostent)
59__weak_alias(endhostent,_endhostent)
60#endif
61
62#ifndef _REENTRANT
63void	res_close(void);
64#endif
65
66static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
67
68void
69/*ARGSUSED*/
70sethostent(int stayopen)
71{
72#ifndef _REENTRANT
73	if ((_res.options & RES_INIT) == 0 && res_init() == -1)
74		return;
75	if (stayopen)
76		_res.options |= RES_STAYOPEN | RES_USEVC;
77#endif
78	sethostent_r(&_h_file);
79}
80
81void
82endhostent(void)
83{
84#ifndef _REENTRANT
85	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
86	res_close();
87#endif
88	endhostent_r(&_h_file);
89}
90static const char *_h_hosts = _PATH_HOSTS;
91
92void
93_hf_sethostsfile(const char *f) {
94	_h_hosts = f;
95}
96
97void
98sethostent_r(FILE **hf)
99{
100	if (!*hf)
101		*hf = fopen(_h_hosts, "re");
102	else
103		rewind(*hf);
104}
105
106void
107endhostent_r(FILE **hf)
108{
109	if (*hf) {
110		(void)fclose(*hf);
111		*hf = NULL;
112	}
113}
114
115/*ARGSUSED*/
116int
117_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
118{
119	struct hostent *hp;
120	const char *name;
121	int af;
122	struct getnamaddr *info = rv;
123
124	_DIAGASSERT(rv != NULL);
125
126	name = va_arg(ap, char *);
127	/* NOSTRICT skip string len */(void)va_arg(ap, int);
128	af = va_arg(ap, int);
129
130#if 0
131	{
132		res_state res = __res_get_state();
133		if (res == NULL)
134			return NS_NOTFOUND;
135		if (res->options & RES_USE_INET6)
136			hp = _hf_gethtbyname2(name, AF_INET6, info);
137		else
138			hp = NULL;
139		if (hp == NULL)
140			hp = _hf_gethtbyname2(name, AF_INET, info);
141		__res_put_state(res);
142	}
143#else
144	hp = _hf_gethtbyname2(name, af, info);
145#endif
146	if (hp == NULL) {
147		*info->he = HOST_NOT_FOUND;
148		return NS_NOTFOUND;
149	}
150	return NS_SUCCESS;
151}
152
153struct hostent *
154_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
155{
156	struct hostent *hp, hent;
157	char *buf, *ptr;
158	size_t len, anum, num, i;
159	FILE *hf;
160	char *aliases[MAXALIASES];
161	char *addr_ptrs[MAXADDRS];
162
163	_DIAGASSERT(name != NULL);
164
165	hf = NULL;
166	sethostent_r(&hf);
167	if (hf == NULL) {
168		errno = EINVAL;
169		*info->he = NETDB_INTERNAL;
170		return NULL;
171	}
172
173	if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
174		endhostent_r(&hf);
175		*info->he = NETDB_INTERNAL;
176		return NULL;
177	}
178
179	anum = 0;		/* XXX: gcc */
180	hent.h_name = NULL;	/* XXX: gcc */
181	hent.h_addrtype = 0;	/* XXX: gcc */
182	hent.h_length = 0;	/* XXX: gcc */
183
184	for (num = 0; num < MAXADDRS;) {
185		info->hp->h_addrtype = af;
186		info->hp->h_length = 0;
187
188		hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
189		    info->he);
190		if (hp == NULL)
191			break;
192
193		if (strcasecmp(hp->h_name, name) != 0) {
194			char **cp;
195			for (cp = hp->h_aliases; *cp != NULL; cp++)
196				if (strcasecmp(*cp, name) == 0)
197					break;
198			if (*cp == NULL) continue;
199		}
200
201		if (num == 0) {
202			hent.h_addrtype = af = hp->h_addrtype;
203			hent.h_length = hp->h_length;
204
205			HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
206			for (anum = 0; hp->h_aliases[anum]; anum++) {
207				if (anum >= __arraycount(aliases))
208					goto nospc;
209				HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
210				    ptr, len);
211			}
212			ptr = (void *)ALIGN(ptr);
213			if ((size_t)(ptr - buf) >= info->buflen)
214				goto nospc;
215		}
216
217		if (num >= __arraycount(addr_ptrs))
218			goto nospc;
219		HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
220		    len);
221		num++;
222	}
223	endhostent_r(&hf);
224
225	if (num == 0) {
226		*info->he = HOST_NOT_FOUND;
227		free(buf);
228		return NULL;
229	}
230
231	hp = info->hp;
232	ptr = info->buf;
233	len = info->buflen;
234
235	hp->h_addrtype = hent.h_addrtype;
236	hp->h_length = hent.h_length;
237
238	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
239	HENT_ARRAY(hp->h_addr_list, num, ptr, len);
240
241	for (i = 0; i < num; i++)
242		HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
243		    len);
244	hp->h_addr_list[num] = NULL;
245
246	HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
247
248	for (i = 0; i < anum; i++)
249		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
250	hp->h_aliases[anum] = NULL;
251
252	free(buf);
253	return hp;
254nospc:
255	endhostent_r(&hf);
256	*info->he = NETDB_INTERNAL;
257	free(buf);
258	errno = ENOSPC;
259	return NULL;
260}
261
262/*ARGSUSED*/
263int
264_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
265{
266	struct hostent *hp;
267	const unsigned char *addr;
268	struct getnamaddr *info = rv;
269	FILE *hf;
270
271	_DIAGASSERT(rv != NULL);
272
273	addr = va_arg(ap, unsigned char *);
274	info->hp->h_length = va_arg(ap, int);
275	info->hp->h_addrtype = va_arg(ap, int);
276
277	hf = NULL;
278	sethostent_r(&hf);
279	if (hf == NULL) {
280		*info->he = NETDB_INTERNAL;
281		return NS_UNAVAIL;
282	}
283	while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
284	    info->he)) != NULL)
285		if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
286			break;
287	endhostent_r(&hf);
288
289	if (hp == NULL) {
290		*info->he = HOST_NOT_FOUND;
291		return NS_NOTFOUND;
292	}
293	return NS_SUCCESS;
294}
295