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