Fix ifunc-impl-list.c build for s390
[glibc.git] / nscd / nscd_gethst_r.c
blob7950ed695c4553e95bb210ae79871a68bec74174
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 #if !_STRING_ARCH_unaligned
189 /* The aliases_len array in the mapped database might very
190 well be unaligned. We will access it word-wise so on
191 platforms which do not tolerate unaligned accesses we
192 need to make an aligned copy. */
193 if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1))
194 != 0)
196 uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
197 * sizeof (uint32_t));
198 aliases_len = memcpy (tmp, aliases_len,
199 hst_resp.h_aliases_cnt
200 * sizeof (uint32_t));
202 #endif
203 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
205 if (hst_resp.h_length == INADDRSZ)
206 addr_list += addr_list_len;
207 addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
209 if (__builtin_expect ((const char *) addr_list + addr_list_len
210 > recend, 0))
211 goto out;
215 if (h_name == NULL)
217 sock = __nscd_open_socket (key, keylen, type, &hst_resp,
218 sizeof (hst_resp));
219 if (sock == -1)
221 __nss_not_use_nscd_hosts = 1;
222 goto out;
226 /* No value found so far. */
227 *result = NULL;
229 if (__glibc_unlikely (hst_resp.found == -1))
231 /* The daemon does not cache this database. */
232 __nss_not_use_nscd_hosts = 1;
233 goto out_close;
236 if (hst_resp.found == 1)
238 char *cp = buffer;
239 uintptr_t align1;
240 uintptr_t align2;
241 size_t total_len;
242 ssize_t cnt;
243 char *ignore;
244 int n;
246 /* A first check whether the buffer is sufficiently large is possible. */
247 /* Now allocate the buffer the array for the group members. We must
248 align the pointer and the base of the h_addr_list pointers. */
249 align1 = ((__alignof__ (char *) - (cp - ((char *) 0)))
250 & (__alignof__ (char *) - 1));
251 align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len)
252 - ((char *) 0)))
253 & (__alignof__ (char *) - 1));
254 if (buflen < (align1 + hst_resp.h_name_len + align2
255 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
256 + 2)
257 * sizeof (char *))
258 + hst_resp.h_addr_list_cnt * (type == AF_INET
259 ? INADDRSZ : IN6ADDRSZ)))
261 no_room:
262 *h_errnop = NETDB_INTERNAL;
263 __set_errno (ERANGE);
264 retval = ERANGE;
265 goto out_close;
267 cp += align1;
269 /* Prepare the result as far as we can. */
270 resultbuf->h_aliases = (char **) cp;
271 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
272 resultbuf->h_addr_list = (char **) cp;
273 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
275 resultbuf->h_name = cp;
276 cp += hst_resp.h_name_len + align2;
278 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
280 resultbuf->h_addrtype = AF_INET;
281 resultbuf->h_length = INADDRSZ;
283 else
285 resultbuf->h_addrtype = AF_INET6;
286 resultbuf->h_length = IN6ADDRSZ;
288 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
290 resultbuf->h_addr_list[cnt] = cp;
291 cp += resultbuf->h_length;
293 resultbuf->h_addr_list[cnt] = NULL;
295 if (h_name == NULL)
297 struct iovec vec[4];
299 vec[0].iov_base = resultbuf->h_name;
300 vec[0].iov_len = hst_resp.h_name_len;
301 total_len = hst_resp.h_name_len;
302 n = 1;
304 if (hst_resp.h_aliases_cnt > 0)
306 aliases_len = alloca (hst_resp.h_aliases_cnt
307 * sizeof (uint32_t));
308 vec[n].iov_base = (void *) aliases_len;
309 vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
311 total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
312 ++n;
315 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
317 vec[n].iov_base = resultbuf->h_addr_list[0];
318 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
320 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
322 ++n;
324 else
326 if (hst_resp.h_length == INADDRSZ)
328 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
329 vec[n].iov_base = ignore;
330 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
332 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
334 ++n;
337 vec[n].iov_base = resultbuf->h_addr_list[0];
338 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
340 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
342 ++n;
345 if ((size_t) __readvall (sock, vec, n) != total_len)
346 goto out_close;
348 else
350 memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
351 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
354 /* Now we also can read the aliases. */
355 total_len = 0;
356 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
358 resultbuf->h_aliases[cnt] = cp;
359 cp += aliases_len[cnt];
360 total_len += aliases_len[cnt];
362 resultbuf->h_aliases[cnt] = NULL;
364 if (__builtin_expect ((const char *) addr_list + addr_list_len
365 + total_len > recend, 0))
367 /* aliases_len array might contain garbage during nscd GC cycle,
368 retry rather than fail in that case. */
369 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
370 retval = -2;
371 goto out_close;
373 /* See whether this would exceed the buffer capacity. */
374 if (__glibc_unlikely (cp > buffer + buflen))
376 /* aliases_len array might contain garbage during nscd GC cycle,
377 retry rather than fail in that case. */
378 if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle)
380 retval = -2;
381 goto out_close;
383 goto no_room;
386 /* And finally read the aliases. */
387 if (addr_list == NULL)
389 if (total_len == 0
390 || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
391 == total_len))
393 retval = 0;
394 *result = resultbuf;
397 else
399 memcpy (resultbuf->h_aliases[0],
400 (const char *) addr_list + addr_list_len, total_len);
402 /* Try to detect corrupt databases. */
403 if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
404 || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
405 if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
406 != '\0')
407 break;
408 cnt < hst_resp.h_aliases_cnt; }))
410 /* We cannot use the database. */
411 if (mapped->head->gc_cycle != gc_cycle)
412 retval = -2;
413 goto out_close;
416 retval = 0;
417 *result = resultbuf;
420 else
422 /* Store the error number. */
423 *h_errnop = hst_resp.error;
425 /* Set errno to 0 to indicate no error, just no found record. */
426 __set_errno (0);
427 /* Even though we have not found anything, the result is zero. */
428 retval = 0;
431 out_close:
432 if (sock != -1)
433 __close_nocancel_nostatus (sock);
434 out:
435 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
437 /* When we come here this means there has been a GC cycle while we
438 were looking for the data. This means the data might have been
439 inconsistent. Retry if possible. */
440 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
442 /* nscd is just running gc now. Disable using the mapping. */
443 if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
444 __nscd_unmap (mapped);
445 mapped = NO_MAPPING;
448 if (retval != -1)
449 goto retry;
452 return retval;