Enable support for GCC 11 -Wmismatched-dealloc.
[glibc.git] / nss / nss_files / files-hosts.c
blob2b47ec3e53d593475c6da8f4029f0dd0dd0bbd39
1 /* Hosts file parser in nss_files module.
2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <https://www.gnu.org/licenses/>. */
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 #include <nss.h>
29 NSS_DECLARE_MODULE_FUNCTIONS (files)
31 /* Get implementation for some internal functions. */
32 #include "../resolv/res_hconf.h"
35 #define ENTNAME hostent
36 #define DATABASE "hosts"
37 #define NEED_H_ERRNO
39 #define EXTRA_ARGS , af
40 #define EXTRA_ARGS_DECL , int af
42 #define ENTDATA hostent_data
43 struct hostent_data
45 unsigned char host_addr[16]; /* IPv4 or IPv6 address. */
46 char *h_addr_ptrs[2]; /* Points to that and null terminator. */
49 #define TRAILING_LIST_MEMBER h_aliases
50 #define TRAILING_LIST_SEPARATOR_P isspace
51 #include "files-parse.c"
52 LINE_PARSER
53 ("#",
55 char *addr;
57 STRING_FIELD (addr, isspace, 1);
59 /* Parse address. */
60 if (inet_pton (af == AF_UNSPEC ? AF_INET : af, addr, entdata->host_addr)
61 > 0)
62 af = af == AF_UNSPEC ? AF_INET : af;
63 else
65 if (af == AF_INET && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
67 if (IN6_IS_ADDR_V4MAPPED (entdata->host_addr))
68 memcpy (entdata->host_addr, entdata->host_addr + 12, INADDRSZ);
69 else if (IN6_IS_ADDR_LOOPBACK (entdata->host_addr))
71 in_addr_t localhost = htonl (INADDR_LOOPBACK);
72 memcpy (entdata->host_addr, &localhost, sizeof (localhost));
74 else
75 /* Illegal address: ignore line. */
76 return 0;
78 else if (af == AF_UNSPEC
79 && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
80 af = AF_INET6;
81 else
82 /* Illegal address: ignore line. */
83 return 0;
86 /* We always return entries of the requested form. */
87 result->h_addrtype = af;
88 result->h_length = af == AF_INET ? INADDRSZ : IN6ADDRSZ;
90 /* Store a pointer to the address in the expected form. */
91 entdata->h_addr_ptrs[0] = (char *) entdata->host_addr;
92 entdata->h_addr_ptrs[1] = NULL;
93 result->h_addr_list = entdata->h_addr_ptrs;
95 STRING_FIELD (result->h_name, isspace, 1);
98 #define EXTRA_ARGS_VALUE , AF_INET
99 #include "files-XXX.c"
100 #undef EXTRA_ARGS_VALUE
102 /* We only need to consider IPv4 mapped addresses if the input to the
103 gethostbyaddr() function is an IPv6 address. */
104 #define EXTRA_ARGS_VALUE , af
105 DB_LOOKUP (hostbyaddr, ,,,
107 if (result->h_length == (int) len
108 && ! memcmp (addr, result->h_addr_list[0], len))
109 break;
110 }, const void *addr, socklen_t len, int af)
111 #undef EXTRA_ARGS_VALUE
113 /* Type of the address and alias arrays. */
114 #define DYNARRAY_STRUCT array
115 #define DYNARRAY_ELEMENT char *
116 #define DYNARRAY_PREFIX array_
117 #include <malloc/dynarray-skeleton.c>
119 static enum nss_status
120 gethostbyname3_multi (FILE * stream, const char *name, int af,
121 struct hostent *result, char *buffer, size_t buflen,
122 int *errnop, int *herrnop)
124 assert (af == AF_INET || af == AF_INET6);
126 /* We have to get all host entries from the file. */
127 struct scratch_buffer tmp_buffer;
128 scratch_buffer_init (&tmp_buffer);
129 struct hostent tmp_result_buf;
130 struct array addresses;
131 array_init (&addresses);
132 struct array aliases;
133 array_init (&aliases);
134 enum nss_status status;
136 /* Preserve the addresses and aliases encountered so far. */
137 for (size_t i = 0; result->h_addr_list[i] != NULL; ++i)
138 array_add (&addresses, result->h_addr_list[i]);
139 for (size_t i = 0; result->h_aliases[i] != NULL; ++i)
140 array_add (&aliases, result->h_aliases[i]);
142 /* The output buffer re-uses now-unused space at the end of the
143 buffer, starting with the aliases array. It comes last in the
144 data produced by internal_getent. (The alias names themselves
145 are still located in the line read in internal_getent, which is
146 stored at the beginning of the buffer.) */
147 struct alloc_buffer outbuf;
149 char *bufferend = (char *) result->h_aliases;
150 outbuf = alloc_buffer_create (bufferend, buffer + buflen - bufferend);
153 while (true)
155 status = internal_getent (stream, &tmp_result_buf, tmp_buffer.data,
156 tmp_buffer.length, errnop, herrnop, af);
157 /* Enlarge the buffer if necessary. */
158 if (status == NSS_STATUS_TRYAGAIN && *herrnop == NETDB_INTERNAL
159 && *errnop == ERANGE)
161 if (!scratch_buffer_grow (&tmp_buffer))
163 *errnop = ENOMEM;
164 /* *herrnop and status already have the right value. */
165 break;
167 /* Loop around and retry with a larger buffer. */
169 else if (status == NSS_STATUS_SUCCESS)
171 /* A line was read. Check that it matches the search
172 criteria. */
174 int matches = 1;
175 struct hostent *old_result = result;
176 result = &tmp_result_buf;
177 /* The following piece is a bit clumsy but we want to use
178 the `LOOKUP_NAME_CASE' value. The optimizer should do
179 its job. */
182 LOOKUP_NAME_CASE (h_name, h_aliases)
183 result = old_result;
185 while ((matches = 0));
187 /* If the line matches, we need to copy the addresses and
188 aliases, so that we can reuse tmp_buffer for the next
189 line. */
190 if (matches)
192 /* Record the addresses. */
193 for (size_t i = 0; tmp_result_buf.h_addr_list[i] != NULL; ++i)
195 /* Allocate the target space in the output buffer,
196 depending on the address family. */
197 void *target;
198 if (af == AF_INET)
200 assert (tmp_result_buf.h_length == 4);
201 target = alloc_buffer_alloc (&outbuf, struct in_addr);
203 else if (af == AF_INET6)
205 assert (tmp_result_buf.h_length == 16);
206 target = alloc_buffer_alloc (&outbuf, struct in6_addr);
208 else
209 __builtin_unreachable ();
211 if (target == NULL)
213 /* Request a larger output buffer. */
214 *errnop = ERANGE;
215 *herrnop = NETDB_INTERNAL;
216 status = NSS_STATUS_TRYAGAIN;
217 break;
219 memcpy (target, tmp_result_buf.h_addr_list[i],
220 tmp_result_buf.h_length);
221 array_add (&addresses, target);
224 /* Record the aliases. */
225 for (size_t i = 0; tmp_result_buf.h_aliases[i] != NULL; ++i)
227 char *alias = tmp_result_buf.h_aliases[i];
228 array_add (&aliases,
229 alloc_buffer_copy_string (&outbuf, alias));
232 /* If the real name is different add, it also to the
233 aliases. This means that there is a duplication in
234 the alias list but this is really the user's
235 problem. */
237 char *new_name = tmp_result_buf.h_name;
238 if (strcmp (old_result->h_name, new_name) != 0)
239 array_add (&aliases,
240 alloc_buffer_copy_string (&outbuf, new_name));
243 /* Report memory allocation failures during the
244 expansion of the temporary arrays. */
245 if (array_has_failed (&addresses) || array_has_failed (&aliases))
247 *errnop = ENOMEM;
248 *herrnop = NETDB_INTERNAL;
249 status = NSS_STATUS_UNAVAIL;
250 break;
253 /* Request a larger output buffer if we ran out of room. */
254 if (alloc_buffer_has_failed (&outbuf))
256 *errnop = ERANGE;
257 *herrnop = NETDB_INTERNAL;
258 status = NSS_STATUS_TRYAGAIN;
259 break;
262 result = old_result;
263 } /* If match was found. */
265 /* If no match is found, loop around and fetch another
266 line. */
268 } /* status == NSS_STATUS_SUCCESS. */
269 else
270 /* internal_getent returned an error. */
271 break;
272 } /* while (true) */
274 /* Propagate the NSS_STATUS_TRYAGAIN error to the caller. It means
275 that we may not have loaded the complete result.
276 NSS_STATUS_NOTFOUND, however, means that we reached the end of
277 the file successfully. */
278 if (status != NSS_STATUS_TRYAGAIN)
279 status = NSS_STATUS_SUCCESS;
281 if (status == NSS_STATUS_SUCCESS)
283 /* Copy the address and alias arrays into the output buffer and
284 add NULL terminators. The pointed-to elements were directly
285 written into the output buffer above and do not need to be
286 copied again. */
287 size_t addresses_count = array_size (&addresses);
288 size_t aliases_count = array_size (&aliases);
289 char **out_addresses = alloc_buffer_alloc_array
290 (&outbuf, char *, addresses_count + 1);
291 char **out_aliases = alloc_buffer_alloc_array
292 (&outbuf, char *, aliases_count + 1);
293 if (out_addresses == NULL || out_aliases == NULL)
295 /* The output buffer is not large enough. */
296 *errnop = ERANGE;
297 *herrnop = NETDB_INTERNAL;
298 status = NSS_STATUS_TRYAGAIN;
299 /* Fall through to function exit. */
301 else
303 /* Everything is allocated in place. Make the copies and
304 adjust the array pointers. */
305 memcpy (out_addresses, array_begin (&addresses),
306 addresses_count * sizeof (char *));
307 out_addresses[addresses_count] = NULL;
308 memcpy (out_aliases, array_begin (&aliases),
309 aliases_count * sizeof (char *));
310 out_aliases[aliases_count] = NULL;
312 result->h_addr_list = out_addresses;
313 result->h_aliases = out_aliases;
315 status = NSS_STATUS_SUCCESS;
319 scratch_buffer_free (&tmp_buffer);
320 array_free (&addresses);
321 array_free (&aliases);
322 return status;
325 enum nss_status
326 _nss_files_gethostbyname3_r (const char *name, int af, struct hostent *result,
327 char *buffer, size_t buflen, int *errnop,
328 int *herrnop, int32_t *ttlp, char **canonp)
330 FILE *stream = NULL;
331 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct hostent_data);
332 buffer += pad;
333 buflen = buflen > pad ? buflen - pad : 0;
335 /* Open file. */
336 enum nss_status status = internal_setent (&stream);
338 if (status == NSS_STATUS_SUCCESS)
340 while ((status = internal_getent (stream, result, buffer, buflen, errnop,
341 herrnop, af))
342 == NSS_STATUS_SUCCESS)
344 LOOKUP_NAME_CASE (h_name, h_aliases)
347 if (status == NSS_STATUS_SUCCESS
348 && _res_hconf.flags & HCONF_FLAG_MULTI)
349 status = gethostbyname3_multi
350 (stream, name, af, result, buffer, buflen, errnop, herrnop);
352 internal_endent (&stream);
355 if (canonp && status == NSS_STATUS_SUCCESS)
356 *canonp = result->h_name;
358 return status;
361 enum nss_status
362 _nss_files_gethostbyname_r (const char *name, struct hostent *result,
363 char *buffer, size_t buflen, int *errnop,
364 int *herrnop)
366 return _nss_files_gethostbyname3_r (name, AF_INET, result, buffer, buflen,
367 errnop, herrnop, NULL, NULL);
370 enum nss_status
371 _nss_files_gethostbyname2_r (const char *name, int af, struct hostent *result,
372 char *buffer, size_t buflen, int *errnop,
373 int *herrnop)
375 return _nss_files_gethostbyname3_r (name, af, result, buffer, buflen,
376 errnop, herrnop, NULL, NULL);
379 enum nss_status
380 _nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
381 char *buffer, size_t buflen, int *errnop,
382 int *herrnop, int32_t *ttlp)
384 FILE *stream = NULL;
386 /* Open file. */
387 enum nss_status status = internal_setent (&stream);
389 if (status == NSS_STATUS_SUCCESS)
391 bool any = false;
392 bool got_canon = false;
393 while (1)
395 /* Align the buffer for the next record. */
396 uintptr_t pad = (-(uintptr_t) buffer
397 % __alignof__ (struct hostent_data));
398 buffer += pad;
399 buflen = buflen > pad ? buflen - pad : 0;
401 struct hostent result;
402 status = internal_getent (stream, &result, buffer, buflen, errnop,
403 herrnop, AF_UNSPEC);
404 if (status != NSS_STATUS_SUCCESS)
405 break;
407 int naliases = 0;
408 if (__strcasecmp (name, result.h_name) != 0)
410 for (; result.h_aliases[naliases] != NULL; ++naliases)
411 if (! __strcasecmp (name, result.h_aliases[naliases]))
412 break;
413 if (result.h_aliases[naliases] == NULL)
414 continue;
416 /* We know this alias exist. Count it. */
417 ++naliases;
420 /* Determine how much memory has been used so far. */
421 // XXX It is not necessary to preserve the aliases array
422 while (result.h_aliases[naliases] != NULL)
423 ++naliases;
424 char *bufferend = (char *) &result.h_aliases[naliases + 1];
425 assert (buflen >= bufferend - buffer);
426 buflen -= bufferend - buffer;
427 buffer = bufferend;
429 /* We found something. */
430 any = true;
432 /* Create the record the caller expects. There is only one
433 address. */
434 assert (result.h_addr_list[1] == NULL);
435 if (*pat == NULL)
437 uintptr_t pad = (-(uintptr_t) buffer
438 % __alignof__ (struct gaih_addrtuple));
439 buffer += pad;
440 buflen = buflen > pad ? buflen - pad : 0;
442 if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple),
445 *errnop = ERANGE;
446 *herrnop = NETDB_INTERNAL;
447 status = NSS_STATUS_TRYAGAIN;
448 break;
451 *pat = (struct gaih_addrtuple *) buffer;
452 buffer += sizeof (struct gaih_addrtuple);
453 buflen -= sizeof (struct gaih_addrtuple);
456 (*pat)->next = NULL;
457 (*pat)->name = got_canon ? NULL : result.h_name;
458 got_canon = true;
459 (*pat)->family = result.h_addrtype;
460 memcpy ((*pat)->addr, result.h_addr_list[0], result.h_length);
461 (*pat)->scopeid = 0;
463 pat = &((*pat)->next);
465 /* If we only look for the first matching entry we are done. */
466 if ((_res_hconf.flags & HCONF_FLAG_MULTI) == 0)
467 break;
470 /* If we have to look for multiple records and found one, this
471 is a success. */
472 if (status == NSS_STATUS_NOTFOUND && any)
474 assert ((_res_hconf.flags & HCONF_FLAG_MULTI) != 0);
475 status = NSS_STATUS_SUCCESS;
478 internal_endent (&stream);
480 else if (status == NSS_STATUS_TRYAGAIN)
482 *errnop = errno;
483 *herrnop = TRY_AGAIN;
485 else
487 *errnop = errno;
488 *herrnop = NO_DATA;
491 return status;