blob: cbf0911679be2f78d7f1036dde4ff6473464ce31 [file] [log] [blame]
Yabin Cui58d33a52014-12-16 17:03:44 -08001/* $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
35static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93";
36static 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
Tom Marshalled92a042016-06-17 16:38:12 -070058#include "hosts_cache.h"
59
Yabin Cui58d33a52014-12-16 17:03:44 -080060#define ALIGNBYTES (sizeof(uintptr_t) - 1)
61#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
62
63#ifndef _REENTRANT
64void res_close(void);
65#endif
66
67static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
68
Elliott Hughes50339182017-10-13 17:52:01 -070069void
70/*ARGSUSED*/
71sethostent(int stayopen)
72{
73 res_static rs = __res_get_static();
74 if (rs) sethostent_r(&rs->hostf);
75}
76
77void
78endhostent(void)
79{
80 res_static rs = __res_get_static();
81 if (rs) endhostent_r(&rs->hostf);
82}
Yabin Cui58d33a52014-12-16 17:03:44 -080083
84void
85sethostent_r(FILE **hf)
86{
87 if (!*hf)
Elliott Hughes50339182017-10-13 17:52:01 -070088 *hf = fopen(_PATH_HOSTS, "re");
Yabin Cui58d33a52014-12-16 17:03:44 -080089 else
90 rewind(*hf);
91}
92
93void
94endhostent_r(FILE **hf)
95{
96 if (*hf) {
97 (void)fclose(*hf);
98 *hf = NULL;
99 }
100}
101
102/*ARGSUSED*/
103int
104_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
105{
106 struct hostent *hp;
107 const char *name;
108 int af;
109 struct getnamaddr *info = rv;
110
111 _DIAGASSERT(rv != NULL);
112
113 name = va_arg(ap, char *);
114 /* NOSTRICT skip string len */(void)va_arg(ap, int);
115 af = va_arg(ap, int);
116
Tom Marshalled92a042016-06-17 16:38:12 -0700117 int rc = hc_gethtbyname(name, af, info);
118 if (rc != NETDB_INTERNAL) {
119 return (rc == NETDB_SUCCESS ? NS_SUCCESS : NS_NOTFOUND);
120 }
121
Yabin Cui58d33a52014-12-16 17:03:44 -0800122#if 0
123 {
124 res_state res = __res_get_state();
125 if (res == NULL)
126 return NS_NOTFOUND;
127 if (res->options & RES_USE_INET6)
128 hp = _hf_gethtbyname2(name, AF_INET6, info);
129 else
130 hp = NULL;
131 if (hp == NULL)
132 hp = _hf_gethtbyname2(name, AF_INET, info);
133 __res_put_state(res);
134 }
135#else
136 hp = _hf_gethtbyname2(name, af, info);
137#endif
138 if (hp == NULL) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700139 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
140 return NS_UNAVAIL; // glibc compatibility.
141 }
Elliott Hughes50339182017-10-13 17:52:01 -0700142 *info->he = HOST_NOT_FOUND;
Yabin Cui58d33a52014-12-16 17:03:44 -0800143 return NS_NOTFOUND;
144 }
145 return NS_SUCCESS;
146}
147
Elliott Hughes50339182017-10-13 17:52:01 -0700148struct hostent *
Yabin Cui58d33a52014-12-16 17:03:44 -0800149_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
150{
151 struct hostent *hp, hent;
152 char *buf, *ptr;
153 size_t len, anum, num, i;
154 FILE *hf;
155 char *aliases[MAXALIASES];
156 char *addr_ptrs[MAXADDRS];
157
158 _DIAGASSERT(name != NULL);
159
160 hf = NULL;
161 sethostent_r(&hf);
162 if (hf == NULL) {
163 errno = EINVAL;
164 *info->he = NETDB_INTERNAL;
165 return NULL;
166 }
167
168 if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800169 *info->he = NETDB_INTERNAL;
170 return NULL;
171 }
172
173 anum = 0; /* XXX: gcc */
174 hent.h_name = NULL; /* XXX: gcc */
175 hent.h_addrtype = 0; /* XXX: gcc */
176 hent.h_length = 0; /* XXX: gcc */
177
178 for (num = 0; num < MAXADDRS;) {
179 info->hp->h_addrtype = af;
180 info->hp->h_length = 0;
181
182 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
183 info->he);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700184 if (hp == NULL) {
185 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
186 goto nospc; // glibc compatibility.
187 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800188 break;
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700189 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800190
191 if (strcasecmp(hp->h_name, name) != 0) {
192 char **cp;
193 for (cp = hp->h_aliases; *cp != NULL; cp++)
194 if (strcasecmp(*cp, name) == 0)
195 break;
196 if (*cp == NULL) continue;
197 }
198
199 if (num == 0) {
200 hent.h_addrtype = af = hp->h_addrtype;
201 hent.h_length = hp->h_length;
202
203 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
204 for (anum = 0; hp->h_aliases[anum]; anum++) {
205 if (anum >= MAXALIASES)
206 goto nospc;
207 HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
208 ptr, len);
209 }
210 ptr = (void *)ALIGN(ptr);
211 if ((size_t)(ptr - buf) >= info->buflen)
212 goto nospc;
213 }
214
215 if (num >= MAXADDRS)
216 goto nospc;
217 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
218 len);
219 num++;
220 }
221 endhostent_r(&hf);
222
223 if (num == 0) {
224 *info->he = HOST_NOT_FOUND;
225 free(buf);
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 free(buf);
251 return hp;
252nospc:
Yabin Cui58d33a52014-12-16 17:03:44 -0800253 *info->he = NETDB_INTERNAL;
254 free(buf);
255 errno = ENOSPC;
256 return NULL;
257}
258
259/*ARGSUSED*/
260int
261_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
262{
263 struct hostent *hp;
264 const unsigned char *addr;
265 struct getnamaddr *info = rv;
266 FILE *hf;
267
268 _DIAGASSERT(rv != NULL);
269
270 addr = va_arg(ap, unsigned char *);
271 info->hp->h_length = va_arg(ap, int);
272 info->hp->h_addrtype = va_arg(ap, int);
273
274 hf = NULL;
275 sethostent_r(&hf);
276 if (hf == NULL) {
277 *info->he = NETDB_INTERNAL;
278 return NS_UNAVAIL;
279 }
280 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
281 info->he)) != NULL)
282 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
283 break;
284 endhostent_r(&hf);
285
286 if (hp == NULL) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700287 if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
Yabin Cui58d33a52014-12-16 17:03:44 -0800288 *info->he = HOST_NOT_FOUND;
289 return NS_NOTFOUND;
290 }
291 return NS_SUCCESS;
292}