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
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*/
sethostent(int stayopen)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
endhostent(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
_hf_sethostsfile(const char * f)93 _hf_sethostsfile(const char *f) {
94 _h_hosts = f;
95 }
96
97 void
sethostent_r(FILE ** hf)98 sethostent_r(FILE **hf)
99 {
100 if (!*hf)
101 *hf = fopen(_h_hosts, "re");
102 else
103 rewind(*hf);
104 }
105
106 void
endhostent_r(FILE ** hf)107 endhostent_r(FILE **hf)
108 {
109 if (*hf) {
110 (void)fclose(*hf);
111 *hf = NULL;
112 }
113 }
114
115 /*ARGSUSED*/
116 int
_hf_gethtbyname(void * rv,void * cb_data,va_list ap)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 *
_hf_gethtbyname2(const char * name,int af,struct getnamaddr * info)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 free(buf);
227 return NULL;
228 }
229
230 hp = info->hp;
231 ptr = info->buf;
232 len = info->buflen;
233
234 hp->h_addrtype = hent.h_addrtype;
235 hp->h_length = hent.h_length;
236
237 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
238 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
239
240 for (i = 0; i < num; i++)
241 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
242 len);
243 hp->h_addr_list[num] = NULL;
244
245 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
246
247 for (i = 0; i < anum; i++)
248 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
249 hp->h_aliases[anum] = NULL;
250
251 free(buf);
252 return hp;
253 nospc:
254 *info->he = NETDB_INTERNAL;
255 free(buf);
256 errno = ENOSPC;
257 return NULL;
258 }
259
260 /*ARGSUSED*/
261 int
_hf_gethtbyaddr(void * rv,void * cb_data,va_list ap)262 _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
263 {
264 struct hostent *hp;
265 const unsigned char *addr;
266 struct getnamaddr *info = rv;
267 FILE *hf;
268
269 _DIAGASSERT(rv != NULL);
270
271 addr = va_arg(ap, unsigned char *);
272 info->hp->h_length = va_arg(ap, int);
273 info->hp->h_addrtype = va_arg(ap, int);
274
275 hf = NULL;
276 sethostent_r(&hf);
277 if (hf == NULL) {
278 *info->he = NETDB_INTERNAL;
279 return NS_UNAVAIL;
280 }
281 while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
282 info->he)) != NULL)
283 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
284 break;
285 endhostent_r(&hf);
286
287 if (hp == NULL) {
288 *info->he = HOST_NOT_FOUND;
289 return NS_NOTFOUND;
290 }
291 return NS_SUCCESS;
292 }
293