xref: /netbsd-src/lib/libc/net/sethostent.c (revision 4d12bfcd155352508213ace5ccc59ce930ea2974)
1 /*	$NetBSD: sethostent.c,v 1.19 2013/08/27 09:56:12 christos 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
35 static char sccsid[] = "@(#)sethostent.c	8.1 (Berkeley) 6/4/93";
36 static 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.19 2013/08/27 09:56:12 christos 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
63 void	res_close(void);
64 #endif
65 
66 static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
67 
68 void
69 /*ARGSUSED*/
70 sethostent(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 
81 void
82 endhostent(void)
83 {
84 #ifndef _REENTRANT
85 	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
86 	res_close();
87 #endif
88 	endhostent_r(&_h_file);
89 }
90 static const char *_h_hosts = _PATH_HOSTS;
91 
92 void
93 _hf_sethostsfile(const char *f) {
94 	_h_hosts = f;
95 }
96 
97 void
98 sethostent_r(FILE **hf)
99 {
100 	if (!*hf)
101 		*hf = fopen(_h_hosts, "re");
102 	else
103 		rewind(*hf);
104 }
105 
106 void
107 endhostent_r(FILE **hf)
108 {
109 	if (*hf) {
110 		(void)fclose(*hf);
111 		*hf = NULL;
112 	}
113 }
114 
115 /*ARGSUSED*/
116 int
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 
153 struct 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 		*info->he = NETDB_INTERNAL;
175 		return NULL;
176 	}
177 
178 	anum = 0;		/* XXX: gcc */
179 	hent.h_name = NULL;	/* XXX: gcc */
180 	hent.h_addrtype = 0;	/* XXX: gcc */
181 	hent.h_length = 0;	/* XXX: gcc */
182 
183 	for (num = 0; num < MAXADDRS;) {
184 		info->hp->h_addrtype = af;
185 		info->hp->h_length = 0;
186 
187 		hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
188 		    info->he);
189 		if (hp == NULL)
190 			break;
191 
192 		if (strcasecmp(hp->h_name, name) != 0) {
193 			char **cp;
194 			for (cp = hp->h_aliases; *cp != NULL; cp++)
195 				if (strcasecmp(*cp, name) == 0)
196 					break;
197 			if (*cp == NULL) continue;
198 		}
199 
200 		if (num == 0) {
201 			hent.h_addrtype = af = hp->h_addrtype;
202 			hent.h_length = hp->h_length;
203 
204 			HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
205 			for (anum = 0; hp->h_aliases[anum]; anum++) {
206 				if (anum >= __arraycount(aliases))
207 					goto nospc;
208 				HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
209 				    ptr, len);
210 			}
211 			ptr = (void *)ALIGN(ptr);
212 			if ((size_t)(ptr - buf) >= info->buflen)
213 				goto nospc;
214 		}
215 
216 		if (num >= __arraycount(addr_ptrs))
217 			goto nospc;
218 		HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
219 		    len);
220 		num++;
221 	}
222 	endhostent_r(&hf);
223 
224 	if (num == 0) {
225 		*info->he = HOST_NOT_FOUND;
226 		return NULL;
227 	}
228 
229 	hp = info->hp;
230 	ptr = info->buf;
231 	len = info->buflen;
232 
233 	hp->h_addrtype = hent.h_addrtype;
234 	hp->h_length = hent.h_length;
235 
236 	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
237 	HENT_ARRAY(hp->h_addr_list, num, ptr, len);
238 
239 	for (i = 0; i < num; i++)
240 		HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
241 		    len);
242 	hp->h_addr_list[num] = NULL;
243 
244 	HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
245 
246 	for (i = 0; i < anum; i++)
247 		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
248 	hp->h_aliases[anum] = NULL;
249 
250 	return hp;
251 nospc:
252 	*info->he = NETDB_INTERNAL;
253 	errno = ENOSPC;
254 	return NULL;
255 }
256 
257 /*ARGSUSED*/
258 int
259 _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
260 {
261 	struct hostent *hp;
262 	const unsigned char *addr;
263 	struct getnamaddr *info = rv;
264 	FILE *hf;
265 
266 	_DIAGASSERT(rv != NULL);
267 
268 	addr = va_arg(ap, unsigned char *);
269 	info->hp->h_length = va_arg(ap, int);
270 	info->hp->h_addrtype = va_arg(ap, int);
271 
272 	hf = NULL;
273 	sethostent_r(&hf);
274 	if (hf == NULL) {
275 		*info->he = NETDB_INTERNAL;
276 		return NS_UNAVAIL;
277 	}
278 	while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
279 	    info->he)) != NULL)
280 		if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
281 			break;
282 	endhostent_r(&hf);
283 
284 	if (hp == NULL) {
285 		*info->he = HOST_NOT_FOUND;
286 		return NS_NOTFOUND;
287 	}
288 	return NS_SUCCESS;
289 }
290