blob: 5a7ab7820d6d6b434f046d447d1ce4d0e0cecce4 [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
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090032#include <arpa/inet.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090033#include <arpa/nameser.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090034#include <assert.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090035#include <errno.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090036#include <netdb.h>
37#include <netinet/in.h>
38#include <nsswitch.h>
39#include <resolv.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090040#include <stdlib.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090041#include <string.h>
42#include <sys/param.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090043
44#include "hostent.h"
45#include "resolv_private.h"
46
Bernie Innocentiafaacf72018-08-30 07:34:37 +090047// NetBSD uses _DIAGASSERT to null-check arguments and the like,
48// but it's clear from the number of mistakes in their assertions
49// that they don't actually test or ship with this.
50#define _DIAGASSERT(e) /* nothing */
51
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090052#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090053#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090054
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090055static struct hostent* _hf_gethtbyname2(const char*, int, struct getnamaddr*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090056
57void
Bernie Innocenti9c575932018-09-07 21:10:25 +090058sethostent(int /*stayopen*/) {
59 struct res_static* rs = __res_get_static();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090060 if (rs) sethostent_r(&rs->hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090061}
62
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090063void endhostent(void) {
Bernie Innocenti9c575932018-09-07 21:10:25 +090064 struct res_static* rs = __res_get_static();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090065 if (rs) endhostent_r(&rs->hostf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090066}
67
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090068void sethostent_r(FILE** hf) {
69 if (!*hf)
70 *hf = fopen(_PATH_HOSTS, "re");
71 else
72 rewind(*hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090073}
74
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090075void endhostent_r(FILE** hf) {
76 if (*hf) {
77 (void) fclose(*hf);
78 *hf = NULL;
79 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090080}
81
Bernie Innocenti9c575932018-09-07 21:10:25 +090082int _hf_gethtbyname(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090083 struct hostent* hp;
84 const char* name;
85 int af;
Bernie Innocenti9c575932018-09-07 21:10:25 +090086 struct getnamaddr* info = (struct getnamaddr*) rv;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090087
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090088 _DIAGASSERT(rv != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090089
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090090 name = va_arg(ap, char*);
91 /* NOSTRICT skip string len */ (void) va_arg(ap, int);
92 af = va_arg(ap, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090093
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090094 hp = _hf_gethtbyname2(name, af, info);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090095 if (hp == NULL) {
96 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
97 return NS_UNAVAIL; // glibc compatibility.
98 }
99 *info->he = HOST_NOT_FOUND;
100 return NS_NOTFOUND;
101 }
102 return NS_SUCCESS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900103}
104
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900105struct hostent* _hf_gethtbyname2(const char* name, int af, struct getnamaddr* info) {
106 struct hostent *hp, hent;
107 char *buf, *ptr;
108 size_t len, anum, num, i;
109 FILE* hf;
110 char* aliases[MAXALIASES];
111 char* addr_ptrs[MAXADDRS];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900112
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900113 _DIAGASSERT(name != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900114
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900115 hf = NULL;
116 sethostent_r(&hf);
117 if (hf == NULL) {
118 errno = EINVAL;
119 *info->he = NETDB_INTERNAL;
120 return NULL;
121 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900122
Bernie Innocenti9c575932018-09-07 21:10:25 +0900123 if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900124 *info->he = NETDB_INTERNAL;
125 return NULL;
126 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900127
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900128 anum = 0; /* XXX: gcc */
129 hent.h_name = NULL; /* XXX: gcc */
130 hent.h_addrtype = 0; /* XXX: gcc */
131 hent.h_length = 0; /* XXX: gcc */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900132
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900133 for (num = 0; num < MAXADDRS;) {
134 info->hp->h_addrtype = af;
135 info->hp->h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900136
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900137 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he);
138 if (hp == NULL) {
139 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
140 goto nospc; // glibc compatibility.
141 }
142 break;
143 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900144
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900145 if (strcasecmp(hp->h_name, name) != 0) {
146 char** cp;
147 for (cp = hp->h_aliases; *cp != NULL; cp++)
148 if (strcasecmp(*cp, name) == 0) break;
149 if (*cp == NULL) continue;
150 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900151
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900152 if (num == 0) {
153 hent.h_addrtype = af = hp->h_addrtype;
154 hent.h_length = hp->h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900155
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900156 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
157 for (anum = 0; hp->h_aliases[anum]; anum++) {
158 if (anum >= MAXALIASES) goto nospc;
159 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
160 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900161 ptr = (char*) ALIGN(ptr);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900162 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
163 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900164
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900165 if (num >= MAXADDRS) goto nospc;
166 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
167 num++;
168 }
169 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900170
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900171 if (num == 0) {
172 *info->he = HOST_NOT_FOUND;
173 free(buf);
174 return NULL;
175 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900176
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900177 hp = info->hp;
178 ptr = info->buf;
179 len = info->buflen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900180
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900181 hp->h_addrtype = hent.h_addrtype;
182 hp->h_length = hent.h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900183
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900184 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
185 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900186
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900187 for (i = 0; i < num; i++) HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
188 hp->h_addr_list[num] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900189
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900190 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900191
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900192 for (i = 0; i < anum; i++) HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
193 hp->h_aliases[anum] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900194
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900195 free(buf);
196 return hp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900197nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900198 *info->he = NETDB_INTERNAL;
199 free(buf);
200 errno = ENOSPC;
201 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900202}
203
Bernie Innocenti9c575932018-09-07 21:10:25 +0900204int _hf_gethtbyaddr(void* rv, void* /*cb_data*/, va_list ap) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900205 struct hostent* hp;
206 const unsigned char* addr;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900207 struct getnamaddr* info = (struct getnamaddr*) rv;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 FILE* hf;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900209
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900210 _DIAGASSERT(rv != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900211
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900212 addr = va_arg(ap, unsigned char*);
213 info->hp->h_length = va_arg(ap, int);
214 info->hp->h_addrtype = va_arg(ap, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900215
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900216 hf = NULL;
217 sethostent_r(&hf);
218 if (hf == NULL) {
219 *info->he = NETDB_INTERNAL;
220 return NS_UNAVAIL;
221 }
222 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL)
223 if (!memcmp(hp->h_addr_list[0], addr, (size_t) hp->h_length)) break;
224 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900225
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900226 if (hp == NULL) {
227 if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
228 *info->he = HOST_NOT_FOUND;
229 return NS_NOTFOUND;
230 }
231 return NS_SUCCESS;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900232}