1 /* $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 <arpa/inet.h>
33 #include <arpa/nameser.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <netinet/in.h>
38 #include <resolv.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/param.h>
42
43 #include "hostent.h"
44 #include "resolv_private.h"
45
46 constexpr int MAXALIASES = 35;
47 constexpr int MAXADDRS = 35;
48
sethostent_r(FILE ** hf)49 static void sethostent_r(FILE** hf) {
50 if (!*hf)
51 *hf = fopen(_PATH_HOSTS, "re");
52 else
53 rewind(*hf);
54 }
55
endhostent_r(FILE ** hf)56 static void endhostent_r(FILE** hf) {
57 if (*hf) {
58 (void) fclose(*hf);
59 *hf = NULL;
60 }
61 }
62
63 // TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
64 // does not currently return to netd.
_hf_gethtbyname2(const char * name,int af,getnamaddr * info)65 int _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
66 struct hostent *hp, hent;
67 char *buf, *ptr;
68 size_t len, num, i;
69 char* aliases[MAXALIASES];
70 char* addr_ptrs[MAXADDRS];
71
72 // TODO: Wrap the 'hf' into a RAII class or std::shared_ptr and modify the
73 // sethostent_r()/endhostent_r() to get rid of manually endhostent_r(&hf) everywhere.
74 FILE* hf = NULL;
75 sethostent_r(&hf);
76 if (hf == NULL) {
77 // TODO: Consider converting to a private extended EAI_* error code.
78 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
79 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
80 // and EINVAL, to EAI_FAIL.
81 return EAI_FAIL;
82 }
83
84 if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
85 endhostent_r(&hf);
86 return EAI_MEMORY;
87 }
88
89 hent.h_name = NULL;
90 hent.h_addrtype = 0;
91 hent.h_length = 0;
92
93 size_t anum = 0;
94 for (num = 0; num < MAXADDRS; /**/) {
95 info->hp->h_addrtype = af;
96 info->hp->h_length = 0;
97
98 int he;
99 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he);
100 if (hp == NULL) {
101 if (he == NETDB_INTERNAL && errno == ENOSPC) {
102 goto nospc; // glibc compatibility.
103 }
104 break;
105 }
106
107 if (hp->h_name == nullptr) {
108 free(buf);
109 endhostent_r(&hf);
110 return EAI_FAIL;
111 }
112 const char* h_name = hp->h_name;
113 if (strcasecmp(h_name, name) != 0) {
114 char** cp;
115 for (cp = hp->h_aliases; *cp != NULL; cp++)
116 if (strcasecmp(*cp, name) == 0) break;
117 // NOTE: does not increment num
118 if (*cp == NULL) continue;
119 }
120
121 if (num == 0) {
122 hent.h_addrtype = hp->h_addrtype;
123 hent.h_length = hp->h_length;
124
125 HENT_SCOPY(hent.h_name, h_name, ptr, len);
126 for (anum = 0; hp->h_aliases[anum]; anum++) {
127 if (anum >= MAXALIASES) goto nospc;
128 const char* h_alias = hp->h_aliases[anum];
129 HENT_SCOPY(aliases[anum], h_alias, ptr, len);
130 }
131 ptr = align_ptr(ptr);
132 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
133 }
134
135 if (hp->h_addr_list[0] == nullptr) {
136 free(buf);
137 endhostent_r(&hf);
138 return EAI_FAIL;
139 }
140 const char* addr = hp->h_addr_list[0];
141 HENT_COPY(addr_ptrs[num], addr, hp->h_length, ptr, len);
142
143 num++;
144 }
145 endhostent_r(&hf);
146
147 if (num == 0) {
148 free(buf);
149 // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead.
150 // The original return error number is h_errno HOST_NOT_FOUND which was converted to
151 // EAI_NODATA.
152 return EAI_NODATA;
153 }
154
155 hp = info->hp;
156 ptr = info->buf;
157 len = info->buflen;
158
159 hp->h_addrtype = hent.h_addrtype;
160 hp->h_length = hent.h_length;
161
162 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
163 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
164
165 for (i = 0; i < num; i++) {
166 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
167
168 // reserve space for mapping IPv4 address to IPv6 address in place
169 if (hp->h_addrtype == AF_INET) {
170 HENT_COPY(ptr, NAT64_PAD, sizeof(NAT64_PAD), ptr, len);
171 }
172 }
173 hp->h_addr_list[num] = NULL;
174
175 if (hent.h_name == nullptr) {
176 free(buf);
177 return EAI_FAIL;
178 }
179 // Curly brackets are required to avoid the "bypasses variable initialization" compile error.
180 {
181 const char* h_name = hent.h_name;
182 HENT_SCOPY(hp->h_name, h_name, ptr, len);
183 }
184 for (i = 0; i < anum; i++) {
185 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
186 }
187 hp->h_aliases[anum] = NULL;
188
189 free(buf);
190 return 0;
191 nospc:
192 endhostent_r(&hf);
193 free(buf);
194 return EAI_MEMORY;
195 }
196
197 // TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
198 // does not currently return to netd.
_hf_gethtbyaddr(const unsigned char * uaddr,int len,int af,getnamaddr * info)199 int _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) {
200 info->hp->h_length = len;
201 info->hp->h_addrtype = af;
202
203 FILE* hf = NULL;
204 sethostent_r(&hf);
205 if (hf == NULL) {
206 // TODO: Consider converting to a private extended EAI_* error code.
207 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
208 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
209 // and EINVAL, to EAI_FAIL.
210 return EAI_FAIL;
211 }
212 struct hostent* hp;
213 int he;
214 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he)) != NULL) {
215 if (hp->h_addr_list[0] == nullptr) continue;
216 // Reassign it to a local variable to avoid -Wnullable-to-nonnull-conversion on calling
217 // memcmp.
218 const char* addr = hp->h_addr_list[0];
219 if (!memcmp(addr, uaddr, (size_t)hp->h_length)) break;
220 }
221 endhostent_r(&hf);
222
223 if (hp == NULL) {
224 if (errno == ENOSPC) return EAI_MEMORY; // glibc compatibility.
225 // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead.
226 // The original return error number is h_errno HOST_NOT_FOUND which was converted to
227 // EAI_NODATA.
228 return EAI_NODATA;
229 }
230 return 0;
231 }
232