hurd: fix build of tst-system.c
[glibc.git] / nscd / nscd_gethst_r.c
blobab9c9d311f29a9116f47ee3222e9d90efe041e1a
1 /* Copyright (C) 1998-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <resolv/resolv-internal.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <arpa/nameser.h>
24 #include <not-cancel.h>
26 #include "nscd-client.h"
27 #include "nscd_proto.h"
29 int __nss_not_use_nscd_hosts;
31 static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
32 struct hostent *resultbuf, char *buffer,
33 size_t buflen, struct hostent **result,
34 int *h_errnop);
37 int
38 __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
39 char *buffer, size_t buflen, struct hostent **result,
40 int *h_errnop)
42 return nscd_gethst_r (name, strlen (name) + 1, GETHOSTBYNAME, resultbuf,
43 buffer, buflen, result, h_errnop);
47 int
48 __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
49 char *buffer, size_t buflen, struct hostent **result,
50 int *h_errnop)
52 request_type reqtype;
54 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
56 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
57 buffer, buflen, result, h_errnop);
61 int
62 __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
63 struct hostent *resultbuf, char *buffer, size_t buflen,
64 struct hostent **result, int *h_errnop)
66 request_type reqtype;
68 if (!((len == INADDRSZ && type == AF_INET)
69 || (len == IN6ADDRSZ && type == AF_INET6)))
70 /* LEN and TYPE do not match. */
71 return -1;
73 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
75 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
76 h_errnop);
80 libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
81 /* Note that we only free the structure if necessary. The memory
82 mapping is not removed since it is not visible to the malloc
83 handling. */
84 libc_freeres_fn (hst_map_free)
86 if (__hst_map_handle.mapped != NO_MAPPING)
88 void *p = __hst_map_handle.mapped;
89 __hst_map_handle.mapped = NO_MAPPING;
90 free (p);
95 uint32_t
96 __nscd_get_nl_timestamp (void)
98 uint32_t retval;
99 if (__nss_not_use_nscd_hosts != 0)
100 return 0;
102 /* __nscd_get_mapping can change hst_map_handle.mapped to NO_MAPPING.
103 However, __nscd_get_mapping assumes the prior value was not NO_MAPPING.
104 Thus we have to acquire the lock to prevent this thread from changing
105 hst_map_handle.mapped to NO_MAPPING while another thread is inside
106 __nscd_get_mapping. */
107 if (!__nscd_acquire_maplock (&__hst_map_handle))
108 return 0;
110 struct mapped_database *map = __hst_map_handle.mapped;
112 if (map == NULL
113 || (map != NO_MAPPING
114 && map->head->nscd_certainly_running == 0
115 && map->head->timestamp + MAPPING_TIMEOUT < time64_now ()))
116 map = __nscd_get_mapping (GETFDHST, "hosts", &__hst_map_handle.mapped);
118 if (map == NO_MAPPING)
119 retval = 0;
120 else
121 retval = map->head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP];
123 /* Release the lock. */
124 __hst_map_handle.lock = 0;
126 return retval;
130 int __nss_have_localdomain attribute_hidden;
132 static int
133 nscd_gethst_r (const char *key, size_t keylen, request_type type,
134 struct hostent *resultbuf, char *buffer, size_t buflen,
135 struct hostent **result, int *h_errnop)
137 if (__glibc_unlikely (__nss_have_localdomain >= 0))
139 if (__nss_have_localdomain == 0)
140 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
141 if (__nss_have_localdomain > 0)
143 __nss_not_use_nscd_hosts = 1;
144 return -1;
148 int gc_cycle;
149 int nretries = 0;
151 /* If the mapping is available, try to search there instead of
152 communicating with the nscd. */
153 struct mapped_database *mapped;
154 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
155 &gc_cycle);
157 retry:;
158 const char *h_name = NULL;
159 const uint32_t *aliases_len = NULL;
160 const char *addr_list = NULL;
161 size_t addr_list_len = 0;
162 int retval = -1;
163 const char *recend = (const char *) ~UINTMAX_C (0);
164 int sock = -1;
165 hst_response_header hst_resp;
166 if (mapped != NO_MAPPING)
168 /* No const qualifier, as it can change during garbage collection. */
169 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
170 sizeof hst_resp);
171 if (found != NULL)
173 h_name = (char *) (&found->data[0].hstdata + 1);
174 hst_resp = found->data[0].hstdata;
175 aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len);
176 addr_list = ((char *) aliases_len
177 + hst_resp.h_aliases_cnt * sizeof (uint32_t));
178 addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ;
179 recend = (const char *) found->data + found->recsize;
180 /* Now check if we can trust hst_resp fields. If GC is
181 in progress, it can contain anything. */
182 if (mapped->head->gc_cycle != gc_cycle)
184 retval = -2;
185 goto out;
188 /* The aliases_len array in the mapped database might very
189 well be unaligned. We will access it word-wise so on
190 platforms which do not tolerate unaligned accesses we
191 need to make an aligned copy. */
192 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
193 != 0)
195 uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
196 * sizeof (uint32_t));
197 aliases_len = memcpy (tmp, aliases_len,
198 hst_resp.h_aliases_cnt
199 * sizeof (uint32_t));
201 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
203 if (hst_resp.h_length == INADDRSZ)
204 addr_list += addr_list_len;
205 addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
207 if (__builtin_expect ((const char *) addr_list + addr_list_len
208 > recend, 0))
209 goto out;
213 if (h_name == NULL)
215 sock = __nscd_open_socket (key, keylen, type, &hst_resp,
216 sizeof (hst_resp));
217 if (sock == -1)
219 __nss_not_use_nscd_hosts = 1;
220 goto out;
224 /* No value found so far. */
225 *result = NULL;
227 if (__glibc_unlikely (hst_resp.found == -1))
229 /* The daemon does not cache this database. */
230 __nss_not_use_nscd_hosts = 1;
231 goto out_close;
234 if (hst_resp.found == 1)
236 char *cp = buffer;
237 uintptr_t align1;
238 uintptr_t align2;
239 size_t total_len;
240 ssize_t cnt;
241 char *ignore;
242 int n;
244 /* A first check whether the buffer is sufficiently large is possible. */
245 /* Now allocate the buffer the array for the group members. We must
246 align the pointer and the base of the h_addr_list pointers. */
247 align1 = ((__alignof__ (char *) - ((uintptr_t) cp))
248 & (__alignof__ (char *) - 1));
249 align2 = ((__alignof__ (char *) - ((uintptr_t) (cp + align1 + hst_resp.h_name_len)))
250 & (__alignof__ (char *) - 1));
251 if (buflen < (align1 + hst_resp.h_name_len + align2
252 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
253 + 2)
254 * sizeof (char *))
255 + hst_resp.h_addr_list_cnt * (type == AF_INET
256 ? INADDRSZ : IN6ADDRSZ)))
258 no_room:
259 *h_errnop = NETDB_INTERNAL;
260 __set_errno (ERANGE);
261 retval = ERANGE;
262 goto out_close;
264 cp += align1;
266 /* Prepare the result as far as we can. */
267 resultbuf->h_aliases = (char **) cp;
268 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
269 resultbuf->h_addr_list = (char **) cp;
270 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
272 resultbuf->h_name = cp;
273 cp += hst_resp.h_name_len + align2;
275 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
277 resultbuf->h_addrtype = AF_INET;
278 resultbuf->h_length = INADDRSZ;
280 else
282 resultbuf->h_addrtype = AF_INET6;
283 resultbuf->h_length = IN6ADDRSZ;
285 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
287 resultbuf->h_addr_list[cnt] = cp;
288 cp += resultbuf->h_length;
290 resultbuf->h_addr_list[cnt] = NULL;
292 if (h_name == NULL)
294 struct iovec vec[4];
296 vec[0].iov_base = resultbuf->h_name;
297 vec[0].iov_len = hst_resp.h_name_len;
298 total_len = hst_resp.h_name_len;
299 n = 1;
301 if (hst_resp.h_aliases_cnt > 0)
303 aliases_len = alloca (hst_resp.h_aliases_cnt
304 * sizeof (uint32_t));
305 vec[n].iov_base = (void *) aliases_len;
306 vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
308 total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
309 ++n;
312 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
314 vec[n].iov_base = resultbuf->h_addr_list[0];
315 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
317 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
319 ++n;
321 else
323 if (hst_resp.h_length == INADDRSZ)
325 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
326 vec[n].iov_base = ignore;
327 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
329 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
331 ++n;
334 vec[n].iov_base = resultbuf->h_addr_list[0];
335 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
337 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
339 ++n;
342 if ((size_t) __readvall (sock, vec, n) != total_len)
343 goto out_close;
345 else
347 memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
348 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
351 /* Now we also can read the aliases. */
352 total_len = 0;
353 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
355 resultbuf->h_aliases[cnt] = cp;
356 cp += aliases_len[cnt];
357 total_len += aliases_len[cnt];
359 resultbuf->h_aliases[cnt] = NULL;
361 if (__builtin_expect ((const char *) addr_list + addr_list_len
362 + total_len > recend, 0))
364 /* aliases_len array might contain garbage during nscd GC cycle,
365 retry rather than fail in that case. */
366 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
367 retval = -2;
368 goto out_close;
370 /* See whether this would exceed the buffer capacity. */
371 if (__glibc_unlikely (cp > buffer + buflen))
373 /* aliases_len array might contain garbage during nscd GC cycle,
374 retry rather than fail in that case. */
375 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
377 retval = -2;
378 goto out_close;
380 goto no_room;
383 /* And finally read the aliases. */
384 if (addr_list == NULL)
386 if (total_len == 0
387 || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
388 == total_len))
390 retval = 0;
391 *result = resultbuf;
394 else
396 memcpy (resultbuf->h_aliases[0],
397 (const char *) addr_list + addr_list_len, total_len);
399 /* Try to detect corrupt databases. */
400 if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
401 || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
402 if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
403 != '\0')
404 break;
405 cnt < hst_resp.h_aliases_cnt; }))
407 /* We cannot use the database. */
408 if (mapped->head->gc_cycle != gc_cycle)
409 retval = -2;
410 goto out_close;
413 retval = 0;
414 *result = resultbuf;
417 else
419 /* Store the error number. */
420 *h_errnop = hst_resp.error;
422 /* Set errno to 0 to indicate no error, just no found record. */
423 __set_errno (0);
424 /* Even though we have not found anything, the result is zero. */
425 retval = 0;
428 out_close:
429 if (sock != -1)
430 __close_nocancel_nostatus (sock);
431 out:
432 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
434 /* When we come here this means there has been a GC cycle while we
435 were looking for the data. This means the data might have been
436 inconsistent. Retry if possible. */
437 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
439 /* nscd is just running gc now. Disable using the mapping. */
440 if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
441 __nscd_unmap (mapped);
442 mapped = NO_MAPPING;
445 if (retval != -1)
446 goto retry;
449 return retval;