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