1 /*	$NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 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.20 2014/03/17 13:24:23 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 #include "resolv_private.h"
57 
58 #ifndef _REENTRANT
59 void	res_close(void);
60 #endif
61 
62 static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
63 
64 void
65 /*ARGSUSED*/
sethostent(int stayopen)66 sethostent(int stayopen)
67 {
68 	struct res_static* rs = __res_get_static();
69 	if (rs) sethostent_r(&rs->hostf);
70 }
71 
72 void
endhostent(void)73 endhostent(void)
74 {
75 	struct res_static* rs = __res_get_static();
76 	if (rs) endhostent_r(&rs->hostf);
77 }
78 
79 void
sethostent_r(FILE ** hf)80 sethostent_r(FILE **hf)
81 {
82 	if (!*hf)
83 		*hf = fopen(_PATH_HOSTS, "re");
84 	else
85 		rewind(*hf);
86 }
87 
88 void
endhostent_r(FILE ** hf)89 endhostent_r(FILE **hf)
90 {
91 	if (*hf) {
92 		(void)fclose(*hf);
93 		*hf = NULL;
94 	}
95 }
96 
97 /*ARGSUSED*/
98 int
_hf_gethtbyname(void * rv,void * cb_data,va_list ap)99 _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
100 {
101 	struct hostent *hp;
102 	const char *name;
103 	int af;
104 	struct getnamaddr *info = rv;
105 
106 	_DIAGASSERT(rv != NULL);
107 
108 	name = va_arg(ap, char *);
109 	/* NOSTRICT skip string len */(void)va_arg(ap, int);
110 	af = va_arg(ap, int);
111 
112 #if 0
113 	{
114 		res_state res = __res_get_state();
115 		if (res == NULL)
116 			return NS_NOTFOUND;
117 		if (res->options & RES_USE_INET6)
118 			hp = _hf_gethtbyname2(name, AF_INET6, info);
119 		else
120 			hp = NULL;
121 		if (hp == NULL)
122 			hp = _hf_gethtbyname2(name, AF_INET, info);
123 		__res_put_state(res);
124 	}
125 #else
126 	hp = _hf_gethtbyname2(name, af, info);
127 #endif
128 	if (hp == NULL) {
129 		if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
130 			return NS_UNAVAIL; // glibc compatibility.
131 		}
132 		*info->he = HOST_NOT_FOUND;
133 		return NS_NOTFOUND;
134 	}
135 	return NS_SUCCESS;
136 }
137 
138 struct hostent *
_hf_gethtbyname2(const char * name,int af,struct getnamaddr * info)139 _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
140 {
141 	struct hostent *hp, hent;
142 	char *buf, *ptr;
143 	size_t len, anum, num, i;
144 	FILE *hf;
145 	char *aliases[MAXALIASES];
146 	char *addr_ptrs[MAXADDRS];
147 
148 	_DIAGASSERT(name != NULL);
149 
150 	hf = NULL;
151 	sethostent_r(&hf);
152 	if (hf == NULL) {
153 		errno = EINVAL;
154 		*info->he = NETDB_INTERNAL;
155 		return NULL;
156 	}
157 
158 	if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
159 		*info->he = NETDB_INTERNAL;
160 		endhostent_r(&hf);
161 		return NULL;
162 	}
163 
164 	anum = 0;		/* XXX: gcc */
165 	hent.h_name = NULL;	/* XXX: gcc */
166 	hent.h_addrtype = 0;	/* XXX: gcc */
167 	hent.h_length = 0;	/* XXX: gcc */
168 
169 	for (num = 0; num < MAXADDRS;) {
170 		info->hp->h_addrtype = af;
171 		info->hp->h_length = 0;
172 
173 		hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
174 		    info->he);
175 		if (hp == NULL) {
176 			if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
177 				goto nospc; // glibc compatibility.
178 			}
179 			break;
180 		}
181 
182 		if (strcasecmp(hp->h_name, name) != 0) {
183 			char **cp;
184 			for (cp = hp->h_aliases; *cp != NULL; cp++)
185 				if (strcasecmp(*cp, name) == 0)
186 					break;
187 			if (*cp == NULL) continue;
188 		}
189 
190 		if (num == 0) {
191 			hent.h_addrtype = af = hp->h_addrtype;
192 			hent.h_length = hp->h_length;
193 
194 			HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
195 			for (anum = 0; hp->h_aliases[anum]; anum++) {
196 				if (anum >= MAXALIASES)
197 					goto nospc;
198 				HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
199 				    ptr, len);
200 			}
201 			ptr = (void *)ALIGN(ptr);
202 			if ((size_t)(ptr - buf) >= info->buflen)
203 				goto nospc;
204 		}
205 
206 		if (num >= MAXADDRS)
207 			goto nospc;
208 		HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
209 		    len);
210 		num++;
211 	}
212 	endhostent_r(&hf);
213 
214 	if (num == 0) {
215 		*info->he = HOST_NOT_FOUND;
216 		free(buf);
217 		return NULL;
218 	}
219 
220 	hp = info->hp;
221 	ptr = info->buf;
222 	len = info->buflen;
223 
224 	hp->h_addrtype = hent.h_addrtype;
225 	hp->h_length = hent.h_length;
226 
227 	HENT_ARRAY(hp->h_aliases, anum, ptr, len);
228 	HENT_ARRAY(hp->h_addr_list, num, ptr, len);
229 
230 	for (i = 0; i < num; i++)
231 		HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
232 		    len);
233 	hp->h_addr_list[num] = NULL;
234 
235 	HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
236 
237 	for (i = 0; i < anum; i++)
238 		HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
239 	hp->h_aliases[anum] = NULL;
240 
241 	free(buf);
242 	return hp;
243 nospc:
244 	*info->he = NETDB_INTERNAL;
245 	endhostent_r(&hf);
246 	free(buf);
247 	errno = ENOSPC;
248 	return NULL;
249 }
250 
251 /*ARGSUSED*/
252 int
_hf_gethtbyaddr(void * rv,void * cb_data,va_list ap)253 _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
254 {
255 	struct hostent *hp;
256 	const unsigned char *addr;
257 	struct getnamaddr *info = rv;
258 	FILE *hf;
259 
260 	_DIAGASSERT(rv != NULL);
261 
262 	addr = va_arg(ap, unsigned char *);
263 	info->hp->h_length = va_arg(ap, int);
264 	info->hp->h_addrtype = va_arg(ap, int);
265 
266 	hf = NULL;
267 	sethostent_r(&hf);
268 	if (hf == NULL) {
269 		*info->he = NETDB_INTERNAL;
270 		return NS_UNAVAIL;
271 	}
272 	while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
273 	    info->he)) != NULL)
274 		if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
275 			break;
276 	endhostent_r(&hf);
277 
278 	if (hp == NULL) {
279 		if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
280 		*info->he = HOST_NOT_FOUND;
281 		return NS_NOTFOUND;
282 	}
283 	return NS_SUCCESS;
284 }
285