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