1/* Hosts file parser in nss_files module.
2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <assert.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <arpa/nameser.h>
23#include <netdb.h>
24#include <resolv/resolv-internal.h>
25#include <scratch_buffer.h>
26#include <alloc_buffer.h>
27
28
29/* Get implementation for some internal functions. */
30#include "../resolv/res_hconf.h"
31
32
33#define ENTNAME hostent
34#define DATABASE "hosts"
35#define NEED_H_ERRNO
36
37#define EXTRA_ARGS , af
38#define EXTRA_ARGS_DECL , int af
39
40#define ENTDATA hostent_data
41struct hostent_data
42 {
43 unsigned char host_addr[16]; /* IPv4 or IPv6 address. */
44 char *h_addr_ptrs[2]; /* Points to that and null terminator. */
45 };
46
47#define TRAILING_LIST_MEMBER h_aliases
48#define TRAILING_LIST_SEPARATOR_P isspace
49#include "files-parse.c"
50LINE_PARSER
51("#",
52 {
53 char *addr;
54
55 STRING_FIELD (addr, isspace, 1);
56
57 /* Parse address. */
58 if (inet_pton (af == AF_UNSPEC ? AF_INET : af, addr, entdata->host_addr)
59 > 0)
60 af = af == AF_UNSPEC ? AF_INET : af;
61 else
62 {
63 if (af == AF_INET && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
64 {
65 if (IN6_IS_ADDR_V4MAPPED (entdata->host_addr))
66 memcpy (entdata->host_addr, entdata->host_addr + 12, INADDRSZ);
67 else if (IN6_IS_ADDR_LOOPBACK (entdata->host_addr))
68 {
69 in_addr_t localhost = htonl (INADDR_LOOPBACK);
70 memcpy (entdata->host_addr, &localhost, sizeof (localhost));
71 }
72 else
73 /* Illegal address: ignore line. */
74 return 0;
75 }
76 else if (af == AF_UNSPEC
77 && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
78 af = AF_INET6;
79 else
80 /* Illegal address: ignore line. */
81 return 0;
82 }
83
84 /* We always return entries of the requested form. */
85 result->h_addrtype = af;
86 result->h_length = af == AF_INET ? INADDRSZ : IN6ADDRSZ;
87
88 /* Store a pointer to the address in the expected form. */
89 entdata->h_addr_ptrs[0] = (char *) entdata->host_addr;
90 entdata->h_addr_ptrs[1] = NULL;
91 result->h_addr_list = entdata->h_addr_ptrs;
92
93 STRING_FIELD (result->h_name, isspace, 1);
94 })
95
96#define EXTRA_ARGS_VALUE , AF_INET
97#include "files-XXX.c"
98#undef EXTRA_ARGS_VALUE
99
100/* We only need to consider IPv4 mapped addresses if the input to the
101 gethostbyaddr() function is an IPv6 address. */
102#define EXTRA_ARGS_VALUE , af
103DB_LOOKUP (hostbyaddr, ,,,
104 {
105 if (result->h_length == (int) len
106 && ! memcmp (addr, result->h_addr_list[0], len))
107 break;
108 }, const void *addr, socklen_t len, int af)
109#undef EXTRA_ARGS_VALUE
110
111/* Type of the address and alias arrays. */
112#define DYNARRAY_STRUCT array
113#define DYNARRAY_ELEMENT char *
114#define DYNARRAY_PREFIX array_
115#include <malloc/dynarray-skeleton.c>
116
117static enum nss_status
118gethostbyname3_multi (FILE * stream, const char *name, int af,
119 struct hostent *result, char *buffer, size_t buflen,
120 int *errnop, int *herrnop)
121{
122 assert (af == AF_INET || af == AF_INET6);
123
124 /* We have to get all host entries from the file. */
125 struct scratch_buffer tmp_buffer;
126 scratch_buffer_init (&tmp_buffer);
127 struct hostent tmp_result_buf;
128 struct array addresses;
129 array_init (&addresses);
130 struct array aliases;
131 array_init (&aliases);
132 enum nss_status status;
133
134 /* Preserve the addresses and aliases encountered so far. */
135 for (size_t i = 0; result->h_addr_list[i] != NULL; ++i)
136 array_add (&addresses, result->h_addr_list[i]);
137 for (size_t i = 0; result->h_aliases[i] != NULL; ++i)
138 array_add (&aliases, result->h_aliases[i]);
139
140 /* The output buffer re-uses now-unused space at the end of the
141 buffer, starting with the aliases array. It comes last in the
142 data produced by internal_getent. (The alias names themselves
143 are still located in the line read in internal_getent, which is
144 stored at the beginning of the buffer.) */
145 struct alloc_buffer outbuf;
146 {
147 char *bufferend = (char *) result->h_aliases;
148 outbuf = alloc_buffer_create (bufferend, buffer + buflen - bufferend);
149 }
150
151 while (true)
152 {
153 status = internal_getent (stream, &tmp_result_buf, tmp_buffer.data,
154 tmp_buffer.length, errnop, herrnop, af);
155 /* Enlarge the buffer if necessary. */
156 if (status == NSS_STATUS_TRYAGAIN && *herrnop == NETDB_INTERNAL
157 && *errnop == ERANGE)
158 {
159 if (!scratch_buffer_grow (&tmp_buffer))
160 {
161 *errnop = ENOMEM;
162 /* *herrnop and status already have the right value. */
163 break;
164 }
165 /* Loop around and retry with a larger buffer. */
166 }
167 else if (status == NSS_STATUS_SUCCESS)
168 {
169 /* A line was read. Check that it matches the search
170 criteria. */
171
172 int matches = 1;
173 struct hostent *old_result = result;
174 result = &tmp_result_buf;
175 /* The following piece is a bit clumsy but we want to use
176 the `LOOKUP_NAME_CASE' value. The optimizer should do
177 its job. */
178 do
179 {
180 LOOKUP_NAME_CASE (h_name, h_aliases)
181 result = old_result;
182 }
183 while ((matches = 0));
184
185 /* If the line matches, we need to copy the addresses and
186 aliases, so that we can reuse tmp_buffer for the next
187 line. */
188 if (matches)
189 {
190 /* Record the addresses. */
191 for (size_t i = 0; tmp_result_buf.h_addr_list[i] != NULL; ++i)
192 {
193 /* Allocate the target space in the output buffer,
194 depending on the address family. */
195 void *target;
196 if (af == AF_INET)
197 {
198 assert (tmp_result_buf.h_length == 4);
199 target = alloc_buffer_alloc (&outbuf, struct in_addr);
200 }
201 else if (af == AF_INET6)
202 {
203 assert (tmp_result_buf.h_length == 16);
204 target = alloc_buffer_alloc (&outbuf, struct in6_addr);
205 }
206 else
207 __builtin_unreachable ();
208
209 if (target == NULL)
210 {
211 /* Request a larger output buffer. */
212 *errnop = ERANGE;
213 *herrnop = NETDB_INTERNAL;
214 status = NSS_STATUS_TRYAGAIN;
215 break;
216 }
217 memcpy (target, tmp_result_buf.h_addr_list[i],
218 tmp_result_buf.h_length);
219 array_add (&addresses, target);
220 }
221
222 /* Record the aliases. */
223 for (size_t i = 0; tmp_result_buf.h_aliases[i] != NULL; ++i)
224 {
225 char *alias = tmp_result_buf.h_aliases[i];
226 array_add (&aliases,
227 alloc_buffer_copy_string (&outbuf, alias));
228 }
229
230 /* If the real name is different add, it also to the
231 aliases. This means that there is a duplication in
232 the alias list but this is really the user's
233 problem. */
234 {
235 char *new_name = tmp_result_buf.h_name;
236 if (strcmp (old_result->h_name, new_name) != 0)
237 array_add (&aliases,
238 alloc_buffer_copy_string (&outbuf, new_name));
239 }
240
241 /* Report memory allocation failures during the
242 expansion of the temporary arrays. */
243 if (array_has_failed (&addresses) || array_has_failed (&aliases))
244 {
245 *errnop = ENOMEM;
246 *herrnop = NETDB_INTERNAL;
247 status = NSS_STATUS_UNAVAIL;
248 break;
249 }
250
251 /* Request a larger output buffer if we ran out of room. */
252 if (alloc_buffer_has_failed (&outbuf))
253 {
254 *errnop = ERANGE;
255 *herrnop = NETDB_INTERNAL;
256 status = NSS_STATUS_TRYAGAIN;
257 break;
258 }
259
260 result = old_result;
261 } /* If match was found. */
262
263 /* If no match is found, loop around and fetch another
264 line. */
265
266 } /* status == NSS_STATUS_SUCCESS. */
267 else
268 /* internal_getent returned an error. */
269 break;
270 } /* while (true) */
271
272 /* Propagate the NSS_STATUS_TRYAGAIN error to the caller. It means
273 that we may not have loaded the complete result.
274 NSS_STATUS_NOTFOUND, however, means that we reached the end of
275 the file successfully. */
276 if (status != NSS_STATUS_TRYAGAIN)
277 status = NSS_STATUS_SUCCESS;
278
279 if (status == NSS_STATUS_SUCCESS)
280 {
281 /* Copy the address and alias arrays into the output buffer and
282 add NULL terminators. The pointed-to elements were directly
283 written into the output buffer above and do not need to be
284 copied again. */
285 size_t addresses_count = array_size (&addresses);
286 size_t aliases_count = array_size (&aliases);
287 char **out_addresses = alloc_buffer_alloc_array
288 (&outbuf, char *, addresses_count + 1);
289 char **out_aliases = alloc_buffer_alloc_array
290 (&outbuf, char *, aliases_count + 1);
291 if (out_addresses == NULL || out_aliases == NULL)
292 {
293 /* The output buffer is not large enough. */
294 *errnop = ERANGE;
295 *herrnop = NETDB_INTERNAL;
296 status = NSS_STATUS_TRYAGAIN;
297 /* Fall through to function exit. */
298 }
299 else
300 {
301 /* Everything is allocated in place. Make the copies and
302 adjust the array pointers. */
303 memcpy (out_addresses, array_begin (&addresses),
304 addresses_count * sizeof (char *));
305 out_addresses[addresses_count] = NULL;
306 memcpy (out_aliases, array_begin (&aliases),
307 aliases_count * sizeof (char *));
308 out_aliases[aliases_count] = NULL;
309
310 result->h_addr_list = out_addresses;
311 result->h_aliases = out_aliases;
312
313 status = NSS_STATUS_SUCCESS;
314 }
315 }
316
317 scratch_buffer_free (&tmp_buffer);
318 array_free (&addresses);
319 array_free (&aliases);
320 return status;
321}
322
323enum nss_status
324_nss_files_gethostbyname3_r (const char *name, int af, struct hostent *result,
325 char *buffer, size_t buflen, int *errnop,
326 int *herrnop, int32_t *ttlp, char **canonp)
327{
328 FILE *stream = NULL;
329 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct hostent_data);
330 buffer += pad;
331 buflen = buflen > pad ? buflen - pad : 0;
332
333 /* Open file. */
334 enum nss_status status = internal_setent (&stream);
335
336 if (status == NSS_STATUS_SUCCESS)
337 {
338 while ((status = internal_getent (stream, result, buffer, buflen, errnop,
339 herrnop, af))
340 == NSS_STATUS_SUCCESS)
341 {
342 LOOKUP_NAME_CASE (h_name, h_aliases)
343 }
344
345 if (status == NSS_STATUS_SUCCESS
346 && _res_hconf.flags & HCONF_FLAG_MULTI)
347 status = gethostbyname3_multi
348 (stream, name, af, result, buffer, buflen, errnop, herrnop);
349
350 internal_endent (&stream);
351 }
352
353 if (canonp && status == NSS_STATUS_SUCCESS)
354 *canonp = result->h_name;
355
356 return status;
357}
358
359enum nss_status
360_nss_files_gethostbyname_r (const char *name, struct hostent *result,
361 char *buffer, size_t buflen, int *errnop,
362 int *herrnop)
363{
364 return _nss_files_gethostbyname3_r (name, AF_INET, result, buffer, buflen,
365 errnop, herrnop, NULL, NULL);
366}
367
368enum nss_status
369_nss_files_gethostbyname2_r (const char *name, int af, struct hostent *result,
370 char *buffer, size_t buflen, int *errnop,
371 int *herrnop)
372{
373 return _nss_files_gethostbyname3_r (name, af, result, buffer, buflen,
374 errnop, herrnop, NULL, NULL);
375}
376
377enum nss_status
378_nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
379 char *buffer, size_t buflen, int *errnop,
380 int *herrnop, int32_t *ttlp)
381{
382 FILE *stream = NULL;
383
384 /* Open file. */
385 enum nss_status status = internal_setent (&stream);
386
387 if (status == NSS_STATUS_SUCCESS)
388 {
389 bool any = false;
390 bool got_canon = false;
391 while (1)
392 {
393 /* Align the buffer for the next record. */
394 uintptr_t pad = (-(uintptr_t) buffer
395 % __alignof__ (struct hostent_data));
396 buffer += pad;
397 buflen = buflen > pad ? buflen - pad : 0;
398
399 struct hostent result;
400 status = internal_getent (stream, &result, buffer, buflen, errnop,
401 herrnop, AF_UNSPEC);
402 if (status != NSS_STATUS_SUCCESS)
403 break;
404
405 int naliases = 0;
406 if (__strcasecmp (name, result.h_name) != 0)
407 {
408 for (; result.h_aliases[naliases] != NULL; ++naliases)
409 if (! __strcasecmp (name, result.h_aliases[naliases]))
410 break;
411 if (result.h_aliases[naliases] == NULL)
412 continue;
413
414 /* We know this alias exist. Count it. */
415 ++naliases;
416 }
417
418 /* Determine how much memory has been used so far. */
419 // XXX It is not necessary to preserve the aliases array
420 while (result.h_aliases[naliases] != NULL)
421 ++naliases;
422 char *bufferend = (char *) &result.h_aliases[naliases + 1];
423 assert (buflen >= bufferend - buffer);
424 buflen -= bufferend - buffer;
425 buffer = bufferend;
426
427 /* We found something. */
428 any = true;
429
430 /* Create the record the caller expects. There is only one
431 address. */
432 assert (result.h_addr_list[1] == NULL);
433 if (*pat == NULL)
434 {
435 uintptr_t pad = (-(uintptr_t) buffer
436 % __alignof__ (struct gaih_addrtuple));
437 buffer += pad;
438 buflen = buflen > pad ? buflen - pad : 0;
439
440 if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple),
441 0))
442 {
443 *errnop = ERANGE;
444 *herrnop = NETDB_INTERNAL;
445 status = NSS_STATUS_TRYAGAIN;
446 break;
447 }
448
449 *pat = (struct gaih_addrtuple *) buffer;
450 buffer += sizeof (struct gaih_addrtuple);
451 buflen -= sizeof (struct gaih_addrtuple);
452 }
453
454 (*pat)->next = NULL;
455 (*pat)->name = got_canon ? NULL : result.h_name;
456 got_canon = true;
457 (*pat)->family = result.h_addrtype;
458 memcpy ((*pat)->addr, result.h_addr_list[0], result.h_length);
459 (*pat)->scopeid = 0;
460
461 pat = &((*pat)->next);
462
463 /* If we only look for the first matching entry we are done. */
464 if ((_res_hconf.flags & HCONF_FLAG_MULTI) == 0)
465 break;
466 }
467
468 /* If we have to look for multiple records and found one, this
469 is a success. */
470 if (status == NSS_STATUS_NOTFOUND && any)
471 {
472 assert ((_res_hconf.flags & HCONF_FLAG_MULTI) != 0);
473 status = NSS_STATUS_SUCCESS;
474 }
475
476 internal_endent (&stream);
477 }
478 else if (status == NSS_STATUS_TRYAGAIN)
479 {
480 *errnop = errno;
481 *herrnop = TRY_AGAIN;
482 }
483 else
484 {
485 *errnop = errno;
486 *herrnop = NO_DATA;
487 }
488
489 return status;
490}
491