sethostent.c revision 1.16.36.2 1 1.16.36.2 yamt /* $NetBSD: sethostent.c,v 1.16.36.2 2014/05/22 11:36:53 yamt Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.4 cgd * Copyright (c) 1985, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.12 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.7 christos #include <sys/cdefs.h>
33 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
34 1.4 cgd #if 0
35 1.4 cgd static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93";
36 1.9 perry static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp ";
37 1.4 cgd #else
38 1.16.36.2 yamt __RCSID("$NetBSD: sethostent.c,v 1.16.36.2 2014/05/22 11:36:53 yamt Exp $");
39 1.4 cgd #endif
40 1.1 cgd #endif /* LIBC_SCCS and not lint */
41 1.1 cgd
42 1.10 kleink #include "namespace.h"
43 1.1 cgd #include <sys/param.h>
44 1.1 cgd #include <netinet/in.h>
45 1.1 cgd #include <arpa/nameser.h>
46 1.16.36.2 yamt #include <arpa/inet.h>
47 1.16.36.2 yamt #include <assert.h>
48 1.16.36.2 yamt #include <string.h>
49 1.16.36.2 yamt #include <nsswitch.h>
50 1.1 cgd #include <netdb.h>
51 1.1 cgd #include <resolv.h>
52 1.16.36.2 yamt #include <errno.h>
53 1.16.36.2 yamt #include <stdlib.h>
54 1.16.36.2 yamt
55 1.16.36.2 yamt #include "hostent.h"
56 1.10 kleink
57 1.10 kleink #ifdef __weak_alias
58 1.11 mycroft __weak_alias(sethostent,_sethostent)
59 1.11 mycroft __weak_alias(endhostent,_endhostent)
60 1.10 kleink #endif
61 1.1 cgd
62 1.13 christos #ifndef _REENTRANT
63 1.16.36.1 yamt void res_close(void);
64 1.13 christos #endif
65 1.16.36.2 yamt
66 1.16.36.2 yamt static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
67 1.6 mrg
68 1.1 cgd void
69 1.13 christos /*ARGSUSED*/
70 1.16.36.1 yamt sethostent(int stayopen)
71 1.1 cgd {
72 1.13 christos #ifndef _REENTRANT
73 1.5 mrg if ((_res.options & RES_INIT) == 0 && res_init() == -1)
74 1.5 mrg return;
75 1.1 cgd if (stayopen)
76 1.1 cgd _res.options |= RES_STAYOPEN | RES_USEVC;
77 1.13 christos #endif
78 1.16.36.2 yamt sethostent_r(&_h_file);
79 1.1 cgd }
80 1.1 cgd
81 1.1 cgd void
82 1.16.36.1 yamt endhostent(void)
83 1.1 cgd {
84 1.13 christos #ifndef _REENTRANT
85 1.1 cgd _res.options &= ~(RES_STAYOPEN | RES_USEVC);
86 1.6 mrg res_close();
87 1.13 christos #endif
88 1.16.36.2 yamt endhostent_r(&_h_file);
89 1.16.36.2 yamt }
90 1.16.36.2 yamt static const char *_h_hosts = _PATH_HOSTS;
91 1.16.36.2 yamt
92 1.16.36.2 yamt void
93 1.16.36.2 yamt _hf_sethostsfile(const char *f) {
94 1.16.36.2 yamt _h_hosts = f;
95 1.16.36.2 yamt }
96 1.16.36.2 yamt
97 1.16.36.2 yamt void
98 1.16.36.2 yamt sethostent_r(FILE **hf)
99 1.16.36.2 yamt {
100 1.16.36.2 yamt if (!*hf)
101 1.16.36.2 yamt *hf = fopen(_h_hosts, "re");
102 1.16.36.2 yamt else
103 1.16.36.2 yamt rewind(*hf);
104 1.16.36.2 yamt }
105 1.16.36.2 yamt
106 1.16.36.2 yamt void
107 1.16.36.2 yamt endhostent_r(FILE **hf)
108 1.16.36.2 yamt {
109 1.16.36.2 yamt if (*hf) {
110 1.16.36.2 yamt (void)fclose(*hf);
111 1.16.36.2 yamt *hf = NULL;
112 1.16.36.2 yamt }
113 1.16.36.2 yamt }
114 1.16.36.2 yamt
115 1.16.36.2 yamt /*ARGSUSED*/
116 1.16.36.2 yamt int
117 1.16.36.2 yamt _hf_gethtbyname(void *rv, void *cb_data, va_list ap)
118 1.16.36.2 yamt {
119 1.16.36.2 yamt struct hostent *hp;
120 1.16.36.2 yamt const char *name;
121 1.16.36.2 yamt int af;
122 1.16.36.2 yamt struct getnamaddr *info = rv;
123 1.16.36.2 yamt
124 1.16.36.2 yamt _DIAGASSERT(rv != NULL);
125 1.16.36.2 yamt
126 1.16.36.2 yamt name = va_arg(ap, char *);
127 1.16.36.2 yamt /* NOSTRICT skip string len */(void)va_arg(ap, int);
128 1.16.36.2 yamt af = va_arg(ap, int);
129 1.16.36.2 yamt
130 1.16.36.2 yamt #if 0
131 1.16.36.2 yamt {
132 1.16.36.2 yamt res_state res = __res_get_state();
133 1.16.36.2 yamt if (res == NULL)
134 1.16.36.2 yamt return NS_NOTFOUND;
135 1.16.36.2 yamt if (res->options & RES_USE_INET6)
136 1.16.36.2 yamt hp = _hf_gethtbyname2(name, AF_INET6, info);
137 1.16.36.2 yamt else
138 1.16.36.2 yamt hp = NULL;
139 1.16.36.2 yamt if (hp == NULL)
140 1.16.36.2 yamt hp = _hf_gethtbyname2(name, AF_INET, info);
141 1.16.36.2 yamt __res_put_state(res);
142 1.16.36.2 yamt }
143 1.16.36.2 yamt #else
144 1.16.36.2 yamt hp = _hf_gethtbyname2(name, af, info);
145 1.16.36.2 yamt #endif
146 1.16.36.2 yamt if (hp == NULL) {
147 1.16.36.2 yamt *info->he = HOST_NOT_FOUND;
148 1.16.36.2 yamt return NS_NOTFOUND;
149 1.16.36.2 yamt }
150 1.16.36.2 yamt return NS_SUCCESS;
151 1.16.36.2 yamt }
152 1.16.36.2 yamt
153 1.16.36.2 yamt struct hostent *
154 1.16.36.2 yamt _hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
155 1.16.36.2 yamt {
156 1.16.36.2 yamt struct hostent *hp, hent;
157 1.16.36.2 yamt char *buf, *ptr;
158 1.16.36.2 yamt size_t len, anum, num, i;
159 1.16.36.2 yamt FILE *hf;
160 1.16.36.2 yamt char *aliases[MAXALIASES];
161 1.16.36.2 yamt char *addr_ptrs[MAXADDRS];
162 1.16.36.2 yamt
163 1.16.36.2 yamt _DIAGASSERT(name != NULL);
164 1.16.36.2 yamt
165 1.16.36.2 yamt hf = NULL;
166 1.16.36.2 yamt sethostent_r(&hf);
167 1.16.36.2 yamt if (hf == NULL) {
168 1.16.36.2 yamt errno = EINVAL;
169 1.16.36.2 yamt *info->he = NETDB_INTERNAL;
170 1.16.36.2 yamt return NULL;
171 1.16.36.2 yamt }
172 1.16.36.2 yamt
173 1.16.36.2 yamt if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
174 1.16.36.2 yamt *info->he = NETDB_INTERNAL;
175 1.16.36.2 yamt return NULL;
176 1.16.36.2 yamt }
177 1.16.36.2 yamt
178 1.16.36.2 yamt anum = 0; /* XXX: gcc */
179 1.16.36.2 yamt hent.h_name = NULL; /* XXX: gcc */
180 1.16.36.2 yamt hent.h_addrtype = 0; /* XXX: gcc */
181 1.16.36.2 yamt hent.h_length = 0; /* XXX: gcc */
182 1.16.36.2 yamt
183 1.16.36.2 yamt for (num = 0; num < MAXADDRS;) {
184 1.16.36.2 yamt info->hp->h_addrtype = af;
185 1.16.36.2 yamt info->hp->h_length = 0;
186 1.16.36.2 yamt
187 1.16.36.2 yamt hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
188 1.16.36.2 yamt info->he);
189 1.16.36.2 yamt if (hp == NULL)
190 1.16.36.2 yamt break;
191 1.16.36.2 yamt
192 1.16.36.2 yamt if (strcasecmp(hp->h_name, name) != 0) {
193 1.16.36.2 yamt char **cp;
194 1.16.36.2 yamt for (cp = hp->h_aliases; *cp != NULL; cp++)
195 1.16.36.2 yamt if (strcasecmp(*cp, name) == 0)
196 1.16.36.2 yamt break;
197 1.16.36.2 yamt if (*cp == NULL) continue;
198 1.16.36.2 yamt }
199 1.16.36.2 yamt
200 1.16.36.2 yamt if (num == 0) {
201 1.16.36.2 yamt hent.h_addrtype = af = hp->h_addrtype;
202 1.16.36.2 yamt hent.h_length = hp->h_length;
203 1.16.36.2 yamt
204 1.16.36.2 yamt HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
205 1.16.36.2 yamt for (anum = 0; hp->h_aliases[anum]; anum++) {
206 1.16.36.2 yamt if (anum >= __arraycount(aliases))
207 1.16.36.2 yamt goto nospc;
208 1.16.36.2 yamt HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
209 1.16.36.2 yamt ptr, len);
210 1.16.36.2 yamt }
211 1.16.36.2 yamt ptr = (void *)ALIGN(ptr);
212 1.16.36.2 yamt if ((size_t)(ptr - buf) >= info->buflen)
213 1.16.36.2 yamt goto nospc;
214 1.16.36.2 yamt }
215 1.16.36.2 yamt
216 1.16.36.2 yamt if (num >= __arraycount(addr_ptrs))
217 1.16.36.2 yamt goto nospc;
218 1.16.36.2 yamt HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
219 1.16.36.2 yamt len);
220 1.16.36.2 yamt num++;
221 1.16.36.2 yamt }
222 1.16.36.2 yamt endhostent_r(&hf);
223 1.16.36.2 yamt
224 1.16.36.2 yamt if (num == 0) {
225 1.16.36.2 yamt *info->he = HOST_NOT_FOUND;
226 1.16.36.2 yamt free(buf);
227 1.16.36.2 yamt return NULL;
228 1.16.36.2 yamt }
229 1.16.36.2 yamt
230 1.16.36.2 yamt hp = info->hp;
231 1.16.36.2 yamt ptr = info->buf;
232 1.16.36.2 yamt len = info->buflen;
233 1.16.36.2 yamt
234 1.16.36.2 yamt hp->h_addrtype = hent.h_addrtype;
235 1.16.36.2 yamt hp->h_length = hent.h_length;
236 1.16.36.2 yamt
237 1.16.36.2 yamt HENT_ARRAY(hp->h_aliases, anum, ptr, len);
238 1.16.36.2 yamt HENT_ARRAY(hp->h_addr_list, num, ptr, len);
239 1.16.36.2 yamt
240 1.16.36.2 yamt for (i = 0; i < num; i++)
241 1.16.36.2 yamt HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
242 1.16.36.2 yamt len);
243 1.16.36.2 yamt hp->h_addr_list[num] = NULL;
244 1.16.36.2 yamt
245 1.16.36.2 yamt HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
246 1.16.36.2 yamt
247 1.16.36.2 yamt for (i = 0; i < anum; i++)
248 1.16.36.2 yamt HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
249 1.16.36.2 yamt hp->h_aliases[anum] = NULL;
250 1.16.36.2 yamt
251 1.16.36.2 yamt free(buf);
252 1.16.36.2 yamt return hp;
253 1.16.36.2 yamt nospc:
254 1.16.36.2 yamt *info->he = NETDB_INTERNAL;
255 1.16.36.2 yamt free(buf);
256 1.16.36.2 yamt errno = ENOSPC;
257 1.16.36.2 yamt return NULL;
258 1.16.36.2 yamt }
259 1.16.36.2 yamt
260 1.16.36.2 yamt /*ARGSUSED*/
261 1.16.36.2 yamt int
262 1.16.36.2 yamt _hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
263 1.16.36.2 yamt {
264 1.16.36.2 yamt struct hostent *hp;
265 1.16.36.2 yamt const unsigned char *addr;
266 1.16.36.2 yamt struct getnamaddr *info = rv;
267 1.16.36.2 yamt FILE *hf;
268 1.16.36.2 yamt
269 1.16.36.2 yamt _DIAGASSERT(rv != NULL);
270 1.16.36.2 yamt
271 1.16.36.2 yamt addr = va_arg(ap, unsigned char *);
272 1.16.36.2 yamt info->hp->h_length = va_arg(ap, int);
273 1.16.36.2 yamt info->hp->h_addrtype = va_arg(ap, int);
274 1.16.36.2 yamt
275 1.16.36.2 yamt hf = NULL;
276 1.16.36.2 yamt sethostent_r(&hf);
277 1.16.36.2 yamt if (hf == NULL) {
278 1.16.36.2 yamt *info->he = NETDB_INTERNAL;
279 1.16.36.2 yamt return NS_UNAVAIL;
280 1.16.36.2 yamt }
281 1.16.36.2 yamt while ((hp = gethostent_r(hf, info->hp, info->buf, info->buflen,
282 1.16.36.2 yamt info->he)) != NULL)
283 1.16.36.2 yamt if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
284 1.16.36.2 yamt break;
285 1.16.36.2 yamt endhostent_r(&hf);
286 1.16.36.2 yamt
287 1.16.36.2 yamt if (hp == NULL) {
288 1.16.36.2 yamt *info->he = HOST_NOT_FOUND;
289 1.16.36.2 yamt return NS_NOTFOUND;
290 1.16.36.2 yamt }
291 1.16.36.2 yamt return NS_SUCCESS;
292 1.1 cgd }
293