blob: 8b1c5ab6ff1e2cd589b7b62d30a988dc047aac55 [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $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
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090042#include <arpa/inet.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090043#include <arpa/nameser.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090044#include <assert.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090045#include <errno.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090046#include <netdb.h>
47#include <netinet/in.h>
48#include <nsswitch.h>
49#include <resolv.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090050#include <stdlib.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090051#include <string.h>
52#include <sys/param.h>
53#include "namespace.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090054
55#include "hostent.h"
56#include "resolv_private.h"
57
58#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090059#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090060
61#ifndef _REENTRANT
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090062void res_close(void);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090063#endif
64
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090065static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090066
67void
68/*ARGSUSED*/
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090069sethostent(int stayopen) {
70 res_static rs = __res_get_static();
71 if (rs) sethostent_r(&rs->hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090072}
73
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090074void endhostent(void) {
75 res_static rs = __res_get_static();
76 if (rs) endhostent_r(&rs->hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090077}
78
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090079void sethostent_r(FILE** hf) {
80 if (!*hf)
81 *hf = fopen(_PATH_HOSTS, "re");
82 else
83 rewind(*hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090084}
85
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090086void endhostent_r(FILE** hf) {
87 if (*hf) {
88 (void) fclose(*hf);
89 *hf = NULL;
90 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090091}
92
93/*ARGSUSED*/
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090094int _hf_gethtbyname(void* rv, void* cb_data, va_list ap) {
95 struct hostent* hp;
96 const char* name;
97 int af;
98 struct getnamaddr* info = rv;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090099
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900100 _DIAGASSERT(rv != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900101
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900102 name = va_arg(ap, char*);
103 /* NOSTRICT skip string len */ (void) va_arg(ap, int);
104 af = va_arg(ap, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900105
106#if 0
107 {
108 res_state res = __res_get_state();
109 if (res == NULL)
110 return NS_NOTFOUND;
111 if (res->options & RES_USE_INET6)
112 hp = _hf_gethtbyname2(name, AF_INET6, info);
113 else
114 hp = NULL;
115 if (hp == NULL)
116 hp = _hf_gethtbyname2(name, AF_INET, info);
117 __res_put_state(res);
118 }
119#else
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900120 hp = _hf_gethtbyname2(name, af, info);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900121#endif
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900122 if (hp == NULL) {
123 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
124 return NS_UNAVAIL; // glibc compatibility.
125 }
126 *info->he = HOST_NOT_FOUND;
127 return NS_NOTFOUND;
128 }
129 return NS_SUCCESS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900130}
131
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900132struct hostent* _hf_gethtbyname2(const char* name, int af, struct getnamaddr* info) {
133 struct hostent *hp, hent;
134 char *buf, *ptr;
135 size_t len, anum, num, i;
136 FILE* hf;
137 char* aliases[MAXALIASES];
138 char* addr_ptrs[MAXADDRS];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900139
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900140 _DIAGASSERT(name != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900141
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900142 hf = NULL;
143 sethostent_r(&hf);
144 if (hf == NULL) {
145 errno = EINVAL;
146 *info->he = NETDB_INTERNAL;
147 return NULL;
148 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900149
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900150 if ((ptr = buf = malloc(len = info->buflen)) == NULL) {
151 *info->he = NETDB_INTERNAL;
152 return NULL;
153 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900154
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900155 anum = 0; /* XXX: gcc */
156 hent.h_name = NULL; /* XXX: gcc */
157 hent.h_addrtype = 0; /* XXX: gcc */
158 hent.h_length = 0; /* XXX: gcc */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900159
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900160 for (num = 0; num < MAXADDRS;) {
161 info->hp->h_addrtype = af;
162 info->hp->h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900163
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900164 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he);
165 if (hp == NULL) {
166 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
167 goto nospc; // glibc compatibility.
168 }
169 break;
170 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900171
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900172 if (strcasecmp(hp->h_name, name) != 0) {
173 char** cp;
174 for (cp = hp->h_aliases; *cp != NULL; cp++)
175 if (strcasecmp(*cp, name) == 0) break;
176 if (*cp == NULL) continue;
177 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900178
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900179 if (num == 0) {
180 hent.h_addrtype = af = hp->h_addrtype;
181 hent.h_length = hp->h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900182
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900183 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
184 for (anum = 0; hp->h_aliases[anum]; anum++) {
185 if (anum >= MAXALIASES) goto nospc;
186 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
187 }
188 ptr = (void*) ALIGN(ptr);
189 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
190 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900191
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900192 if (num >= MAXADDRS) goto nospc;
193 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
194 num++;
195 }
196 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900197
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900198 if (num == 0) {
199 *info->he = HOST_NOT_FOUND;
200 free(buf);
201 return NULL;
202 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900203
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900204 hp = info->hp;
205 ptr = info->buf;
206 len = info->buflen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900207
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 hp->h_addrtype = hent.h_addrtype;
209 hp->h_length = hent.h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900210
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900211 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
212 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900213
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900214 for (i = 0; i < num; i++) HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
215 hp->h_addr_list[num] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900216
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900217 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900218
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900219 for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
220 hp->h_aliases[anum] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900221
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900222 free(buf);
223 return hp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900224nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900225 *info->he = NETDB_INTERNAL;
226 free(buf);
227 errno = ENOSPC;
228 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900229}
230
231/*ARGSUSED*/
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900232int _hf_gethtbyaddr(void* rv, void* cb_data, va_list ap) {
233 struct hostent* hp;
234 const unsigned char* addr;
235 struct getnamaddr* info = rv;
236 FILE* hf;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900237
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900238 _DIAGASSERT(rv != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900239
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900240 addr = va_arg(ap, unsigned char*);
241 info->hp->h_length = va_arg(ap, int);
242 info->hp->h_addrtype = va_arg(ap, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900243
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900244 hf = NULL;
245 sethostent_r(&hf);
246 if (hf == NULL) {
247 *info->he = NETDB_INTERNAL;
248 return NS_UNAVAIL;
249 }
250 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL)
251 if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break;
252 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900253
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900254 if (hp == NULL) {
255 if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
256 *info->he = HOST_NOT_FOUND;
257 return NS_NOTFOUND;
258 }
259 return NS_SUCCESS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900260}