blob: be2962156b1efcec2b7fb3bc140d6f798d1a8e57 [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 Marshall91664dd2016-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
69static const char *_h_hosts = _PATH_HOSTS;
70
71void
72sethostent_r(FILE **hf)
73{
74 if (!*hf)
75 *hf = fopen(_h_hosts, "re");
76 else
77 rewind(*hf);
78}
79
80void
81endhostent_r(FILE **hf)
82{
83 if (*hf) {
84 (void)fclose(*hf);
85 *hf = NULL;
86 }
87}
88
89/*ARGSUSED*/
90int
91_hf_gethtbyname(void *rv, void *cb_data, va_list ap)
92{
93 struct hostent *hp;
94 const char *name;
95 int af;
96 struct getnamaddr *info = rv;
97
98 _DIAGASSERT(rv != NULL);
99
100 name = va_arg(ap, char *);
101 /* NOSTRICT skip string len */(void)va_arg(ap, int);
102 af = va_arg(ap, int);
103
Tom Marshall91664dd2016-06-17 16:38:12 -0700104 int rc = hc_gethtbyname(name, af, info);
105 if (rc != NETDB_INTERNAL) {
106 return (rc == NETDB_SUCCESS ? NS_SUCCESS : NS_NOTFOUND);
107 }
108
Yabin Cui58d33a52014-12-16 17:03:44 -0800109#if 0
110 {
111 res_state res = __res_get_state();
112 if (res == NULL)
113 return NS_NOTFOUND;
114 if (res->options & RES_USE_INET6)
115 hp = _hf_gethtbyname2(name, AF_INET6, info);
116 else
117 hp = NULL;
118 if (hp == NULL)
119 hp = _hf_gethtbyname2(name, AF_INET, info);
120 __res_put_state(res);
121 }
122#else
123 hp = _hf_gethtbyname2(name, af, info);
124#endif
125 if (hp == NULL) {
Yabin Cui70692562014-12-19 10:10:04 -0800126 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
127 return NS_UNAVAIL;
128 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800129 return NS_NOTFOUND;
130 }
131 return NS_SUCCESS;
132}
133
134static struct hostent *
135_hf_gethtbyname2(const char *name, int af, struct getnamaddr *info)
136{
137 struct hostent *hp, hent;
138 char *buf, *ptr;
139 size_t len, anum, num, i;
140 FILE *hf;
141 char *aliases[MAXALIASES];
142 char *addr_ptrs[MAXADDRS];
143
144 _DIAGASSERT(name != NULL);
145
146 hf = NULL;
147 sethostent_r(&hf);
148 if (hf == NULL) {
149 errno = EINVAL;
150 *info->he = NETDB_INTERNAL;
151 return NULL;
152 }
153
154 if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
155 endhostent_r(&hf);
156 *info->he = NETDB_INTERNAL;
157 return NULL;
158 }
159
160 anum = 0; /* XXX: gcc */
161 hent.h_name = NULL; /* XXX: gcc */
162 hent.h_addrtype = 0; /* XXX: gcc */
163 hent.h_length = 0; /* XXX: gcc */
164
165 for (num = 0; num < MAXADDRS;) {
166 info->hp->h_addrtype = af;
167 info->hp->h_length = 0;
168
169 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
170 info->he);
Yabin Cui70692562014-12-19 10:10:04 -0800171 if (hp == NULL) {
172 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
173 goto nospc;
174 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800175 break;
Yabin Cui70692562014-12-19 10:10:04 -0800176 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800177
178 if (strcasecmp(hp->h_name, name) != 0) {
179 char **cp;
180 for (cp = hp->h_aliases; *cp != NULL; cp++)
181 if (strcasecmp(*cp, name) == 0)
182 break;
183 if (*cp == NULL) continue;
184 }
185
186 if (num == 0) {
187 hent.h_addrtype = af = hp->h_addrtype;
188 hent.h_length = hp->h_length;
189
190 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
191 for (anum = 0; hp->h_aliases[anum]; anum++) {
192 if (anum >= MAXALIASES)
193 goto nospc;
194 HENT_SCOPY(aliases[anum], hp->h_aliases[anum],
195 ptr, len);
196 }
197 ptr = (void *)ALIGN(ptr);
198 if ((size_t)(ptr - buf) >= info->buflen)
199 goto nospc;
200 }
201
202 if (num >= MAXADDRS)
203 goto nospc;
204 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr,
205 len);
206 num++;
207 }
208 endhostent_r(&hf);
209
210 if (num == 0) {
211 *info->he = HOST_NOT_FOUND;
212 free(buf);
213 return NULL;
214 }
215
216 hp = info->hp;
217 ptr = info->buf;
218 len = info->buflen;
219
220 hp->h_addrtype = hent.h_addrtype;
221 hp->h_length = hent.h_length;
222
223 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
224 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
225
226 for (i = 0; i < num; i++)
227 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr,
228 len);
229 hp->h_addr_list[num] = NULL;
230
231 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
232
233 for (i = 0; i < anum; i++)
234 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
235 hp->h_aliases[anum] = NULL;
236
237 free(buf);
238 return hp;
239nospc:
240 endhostent_r(&hf);
241 *info->he = NETDB_INTERNAL;
242 free(buf);
243 errno = ENOSPC;
244 return NULL;
245}
246
247/*ARGSUSED*/
248int
249_hf_gethtbyaddr(void *rv, void *cb_data, va_list ap)
250{
251 struct hostent *hp;
252 const unsigned char *addr;
253 struct getnamaddr *info = rv;
254 FILE *hf;
255
256 _DIAGASSERT(rv != NULL);
257
258 addr = va_arg(ap, unsigned char *);
259 info->hp->h_length = va_arg(ap, int);
260 info->hp->h_addrtype = va_arg(ap, int);
261
262 hf = NULL;
263 sethostent_r(&hf);
264 if (hf == NULL) {
265 *info->he = NETDB_INTERNAL;
266 return NS_UNAVAIL;
267 }
268 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
269 info->he)) != NULL)
270 if (!memcmp(hp->h_addr_list[0], addr, (size_t)hp->h_length))
271 break;
272 endhostent_r(&hf);
273
274 if (hp == NULL) {
Yabin Cui70692562014-12-19 10:10:04 -0800275 if (errno == ENOSPC) {
276 return NS_UNAVAIL;
277 }
Yabin Cui58d33a52014-12-16 17:03:44 -0800278 *info->he = HOST_NOT_FOUND;
279 return NS_NOTFOUND;
280 }
281 return NS_SUCCESS;
282}