blob: 483105a95fba8dc2af5b62618245c83128987596 [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
Yabin Cui58d33a52014-12-16 17:03:44 -080058#ifndef _REENTRANT
59void res_close(void);
60#endif
61
62static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
63
Elliott Hughes50339182017-10-13 17:52:01 -070064void
65/*ARGSUSED*/
66sethostent(int stayopen)
67{
Elliott Hughesd0bbfa82021-04-08 11:58:51 -070068 struct res_static* rs = __res_get_static();
Elliott Hughes50339182017-10-13 17:52:01 -070069 if (rs) sethostent_r(&rs->hostf);
70}
71
72void
73endhostent(void)
74{
Elliott Hughesd0bbfa82021-04-08 11:58:51 -070075 struct res_static* rs = __res_get_static();
Elliott Hughes50339182017-10-13 17:52:01 -070076 if (rs) endhostent_r(&rs->hostf);
77}
Yabin Cui58d33a52014-12-16 17:03:44 -080078
79void
80sethostent_r(FILE **hf)
81{
82 if (!*hf)
Elliott Hughes50339182017-10-13 17:52:01 -070083 *hf = fopen(_PATH_HOSTS, "re");
Yabin Cui58d33a52014-12-16 17:03:44 -080084 else
85 rewind(*hf);
86}
87
88void
89endhostent_r(FILE **hf)
90{
91 if (*hf) {
92 (void)fclose(*hf);
93 *hf = NULL;
94 }
95}
96
97/*ARGSUSED*/
98int
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) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700129 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
130 return NS_UNAVAIL; // glibc compatibility.
131 }
Elliott Hughes50339182017-10-13 17:52:01 -0700132 *info->he = HOST_NOT_FOUND;
Yabin Cui58d33a52014-12-16 17:03:44 -0800133 return NS_NOTFOUND;
134 }
135 return NS_SUCCESS;
136}
137
Elliott Hughes50339182017-10-13 17:52:01 -0700138struct hostent *
Yabin Cui58d33a52014-12-16 17:03:44 -0800139_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) {
Yabin Cui58d33a52014-12-16 17:03:44 -0800159 *info->he = NETDB_INTERNAL;
160 return NULL;
161 }
162
163 anum = 0; /* XXX: gcc */
164 hent.h_name = NULL; /* XXX: gcc */
165 hent.h_addrtype = 0; /* XXX: gcc */
166 hent.h_length = 0; /* XXX: gcc */
167
168 for (num = 0; num < MAXADDRS;) {
169 info->hp->h_addrtype = af;
170 info->hp->h_length = 0;
171
172 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
173 info->he);
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700174 if (hp == NULL) {
175 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
176 goto nospc; // glibc compatibility.
177 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800178 break;
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700179 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800180
181 if (strcasecmp(hp->h_name, name) != 0) {
182 char **cp;
183 for (cp = hp->h_aliases; *cp != NULL; cp++)
184 if (strcasecmp(*cp, name) == 0)
185 break;
186 if (*cp == NULL) continue;
187 }
188
189 if (num == 0) {
190 hent.h_addrtype = af = hp->h_addrtype;
191 hent.h_length = hp->h_length;
192
193 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
194 for (anum = 0; hp->h_aliases[anum]; anum++) {
195 if (anum >= MAXALIASES)
196 goto nospc;
197 HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
198 ptr, len);
199 }
200 ptr = (void *)ALIGN(ptr);
201 if ((size_t)(ptr - buf) >= info->buflen)
202 goto nospc;
203 }
204
205 if (num >= MAXADDRS)
206 goto nospc;
207 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
208 len);
209 num++;
210 }
211 endhostent_r(&hf);
212
213 if (num == 0) {
214 *info->he = HOST_NOT_FOUND;
215 free(buf);
216 return NULL;
217 }
218
219 hp = info->hp;
220 ptr = info->buf;
221 len = info->buflen;
222
223 hp->h_addrtype = hent.h_addrtype;
224 hp->h_length = hent.h_length;
225
226 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
227 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
228
229 for (i = 0; i < num; i++)
230 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
231 len);
232 hp->h_addr_list[num] = NULL;
233
234 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
235
236 for (i = 0; i < anum; i++)
237 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
238 hp->h_aliases[anum] = NULL;
239
240 free(buf);
241 return hp;
242nospc:
Yabin Cui58d33a52014-12-16 17:03:44 -0800243 *info->he = NETDB_INTERNAL;
244 free(buf);
245 errno = ENOSPC;
246 return NULL;
247}
248
249/*ARGSUSED*/
250int
251_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
252{
253 struct hostent *hp;
254 const unsigned char *addr;
255 struct getnamaddr *info = rv;
256 FILE *hf;
257
258 _DIAGASSERT(rv != NULL);
259
260 addr = va_arg(ap, unsigned char *);
261 info->hp->h_length = va_arg(ap, int);
262 info->hp->h_addrtype = va_arg(ap, int);
263
264 hf = NULL;
265 sethostent_r(&hf);
266 if (hf == NULL) {
267 *info->he = NETDB_INTERNAL;
268 return NS_UNAVAIL;
269 }
270 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
271 info->he)) != NULL)
272 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
273 break;
274 endhostent_r(&hf);
275
276 if (hp == NULL) {
Elliott Hughesbb7d9fb2017-10-23 17:38:35 -0700277 if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
Yabin Cui58d33a52014-12-16 17:03:44 -0800278 *info->he = HOST_NOT_FOUND;
279 return NS_NOTFOUND;
280 }
281 return NS_SUCCESS;
282}