1 /* Copyright (C) 1998-2025 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/>. */
19 #include <resolv/resolv-internal.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
,
38 __nscd_gethostbyname_r (const char *name
, struct hostent
*resultbuf
,
39 char *buffer
, size_t buflen
, struct hostent
**result
,
42 return nscd_gethst_r (name
, strlen (name
) + 1, GETHOSTBYNAME
, resultbuf
,
43 buffer
, buflen
, result
, h_errnop
);
48 __nscd_gethostbyname2_r (const char *name
, int af
, struct hostent
*resultbuf
,
49 char *buffer
, size_t buflen
, struct hostent
**result
,
54 reqtype
= af
== AF_INET6
? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
56 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
57 buffer
, buflen
, result
, h_errnop
);
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
)
68 if (!((len
== INADDRSZ
&& type
== AF_INET
)
69 || (len
== IN6ADDRSZ
&& type
== AF_INET6
)))
70 /* LEN and TYPE do not match. */
73 reqtype
= type
== AF_INET6
? GETHOSTBYADDRv6
: GETHOSTBYADDR
;
75 return nscd_gethst_r (addr
, len
, reqtype
, resultbuf
, buffer
, buflen
, result
,
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
85 __nscd_hst_map_freemem (void)
87 if (__hst_map_handle
.mapped
!= NO_MAPPING
)
89 void *p
= __hst_map_handle
.mapped
;
90 __hst_map_handle
.mapped
= NO_MAPPING
;
97 __nscd_get_nl_timestamp (void)
100 if (__nss_not_use_nscd_hosts
!= 0)
103 /* __nscd_get_mapping can change hst_map_handle.mapped to NO_MAPPING.
104 However, __nscd_get_mapping assumes the prior value was not NO_MAPPING.
105 Thus we have to acquire the lock to prevent this thread from changing
106 hst_map_handle.mapped to NO_MAPPING while another thread is inside
107 __nscd_get_mapping. */
108 if (!__nscd_acquire_maplock (&__hst_map_handle
))
111 struct mapped_database
*map
= __hst_map_handle
.mapped
;
114 || (map
!= NO_MAPPING
115 && map
->head
->nscd_certainly_running
== 0
116 && map
->head
->timestamp
+ MAPPING_TIMEOUT
< time64_now ()))
117 map
= __nscd_get_mapping (GETFDHST
, "hosts", &__hst_map_handle
.mapped
);
119 if (map
== NO_MAPPING
)
122 retval
= map
->head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
];
124 /* Release the lock. */
125 __hst_map_handle
.lock
= 0;
131 int __nss_have_localdomain attribute_hidden
;
134 nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
135 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
136 struct hostent
**result
, int *h_errnop
)
138 if (__glibc_unlikely (__nss_have_localdomain
>= 0))
140 if (__nss_have_localdomain
== 0)
141 __nss_have_localdomain
= getenv ("LOCALDOMAIN") != NULL
? 1 : -1;
142 if (__nss_have_localdomain
> 0)
144 __nss_not_use_nscd_hosts
= 1;
152 /* If the mapping is available, try to search there instead of
153 communicating with the nscd. */
154 struct mapped_database
*mapped
;
155 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
159 const char *h_name
= NULL
;
160 const uint32_t *aliases_len
= NULL
;
161 const char *addr_list
= NULL
;
162 size_t addr_list_len
= 0;
164 const char *recend
= (const char *) ~UINTMAX_C (0);
166 hst_response_header hst_resp
;
167 if (mapped
!= NO_MAPPING
)
169 /* No const qualifier, as it can change during garbage collection. */
170 struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
, mapped
,
174 h_name
= (char *) (&found
->data
[0].hstdata
+ 1);
175 hst_resp
= found
->data
[0].hstdata
;
176 aliases_len
= (uint32_t *) (h_name
+ hst_resp
.h_name_len
);
177 addr_list
= ((char *) aliases_len
178 + hst_resp
.h_aliases_cnt
* sizeof (uint32_t));
179 addr_list_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
180 recend
= (const char *) found
->data
+ found
->recsize
;
181 /* Now check if we can trust hst_resp fields. If GC is
182 in progress, it can contain anything. */
183 if (mapped
->head
->gc_cycle
!= gc_cycle
)
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))
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 if (type
!= GETHOSTBYADDR
&& type
!= GETHOSTBYNAME
)
204 if (hst_resp
.h_length
== INADDRSZ
)
205 addr_list
+= addr_list_len
;
206 addr_list_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
208 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
216 sock
= __nscd_open_socket (key
, keylen
, type
, &hst_resp
,
220 __nss_not_use_nscd_hosts
= 1;
225 /* No value found so far. */
228 if (__glibc_unlikely (hst_resp
.found
== -1))
230 /* The daemon does not cache this database. */
231 __nss_not_use_nscd_hosts
= 1;
235 if (hst_resp
.found
== 1)
245 /* A first check whether the buffer is sufficiently large is possible. */
246 /* Now allocate the buffer the array for the group members. We must
247 align the pointer and the base of the h_addr_list pointers. */
248 align1
= ((__alignof__ (char *) - ((uintptr_t) cp
))
249 & (__alignof__ (char *) - 1));
250 align2
= ((__alignof__ (char *) - ((uintptr_t) (cp
+ align1
+ hst_resp
.h_name_len
)))
251 & (__alignof__ (char *) - 1));
252 if (buflen
< (align1
+ hst_resp
.h_name_len
+ align2
253 + ((hst_resp
.h_aliases_cnt
+ hst_resp
.h_addr_list_cnt
256 + hst_resp
.h_addr_list_cnt
* (type
== AF_INET
257 ? INADDRSZ
: IN6ADDRSZ
)))
260 *h_errnop
= NETDB_INTERNAL
;
261 __set_errno (ERANGE
);
267 /* Prepare the result as far as we can. */
268 resultbuf
->h_aliases
= (char **) cp
;
269 cp
+= (hst_resp
.h_aliases_cnt
+ 1) * sizeof (char *);
270 resultbuf
->h_addr_list
= (char **) cp
;
271 cp
+= (hst_resp
.h_addr_list_cnt
+ 1) * sizeof (char *);
273 resultbuf
->h_name
= cp
;
274 cp
+= hst_resp
.h_name_len
+ align2
;
276 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
278 resultbuf
->h_addrtype
= AF_INET
;
279 resultbuf
->h_length
= INADDRSZ
;
283 resultbuf
->h_addrtype
= AF_INET6
;
284 resultbuf
->h_length
= IN6ADDRSZ
;
286 for (cnt
= 0; cnt
< hst_resp
.h_addr_list_cnt
; ++cnt
)
288 resultbuf
->h_addr_list
[cnt
] = cp
;
289 cp
+= resultbuf
->h_length
;
291 resultbuf
->h_addr_list
[cnt
] = NULL
;
297 vec
[0].iov_base
= resultbuf
->h_name
;
298 vec
[0].iov_len
= hst_resp
.h_name_len
;
299 total_len
= hst_resp
.h_name_len
;
302 if (hst_resp
.h_aliases_cnt
> 0)
304 aliases_len
= alloca (hst_resp
.h_aliases_cnt
305 * sizeof (uint32_t));
306 vec
[n
].iov_base
= (void *) aliases_len
;
307 vec
[n
].iov_len
= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
309 total_len
+= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
313 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
315 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
316 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
318 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
324 if (hst_resp
.h_length
== INADDRSZ
)
326 ignore
= alloca (hst_resp
.h_addr_list_cnt
* INADDRSZ
);
327 vec
[n
].iov_base
= ignore
;
328 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
330 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
335 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
336 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
338 total_len
+= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
343 if ((size_t) __readvall (sock
, vec
, n
) != total_len
)
348 memcpy (resultbuf
->h_name
, h_name
, hst_resp
.h_name_len
);
349 memcpy (resultbuf
->h_addr_list
[0], addr_list
, addr_list_len
);
352 /* Now we also can read the aliases. */
354 for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
356 resultbuf
->h_aliases
[cnt
] = cp
;
357 cp
+= aliases_len
[cnt
];
358 total_len
+= aliases_len
[cnt
];
360 resultbuf
->h_aliases
[cnt
] = NULL
;
362 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
363 + total_len
> recend
, 0))
365 /* aliases_len array might contain garbage during nscd GC cycle,
366 retry rather than fail in that case. */
367 if (addr_list
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
371 /* See whether this would exceed the buffer capacity. */
372 if (__glibc_unlikely (cp
> buffer
+ buflen
))
374 /* aliases_len array might contain garbage during nscd GC cycle,
375 retry rather than fail in that case. */
376 if (addr_list
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
384 /* And finally read the aliases. */
385 if (addr_list
== NULL
)
388 || ((size_t) __readall (sock
, resultbuf
->h_aliases
[0], total_len
)
397 memcpy (resultbuf
->h_aliases
[0],
398 (const char *) addr_list
+ addr_list_len
, total_len
);
400 /* Try to detect corrupt databases. */
401 if (resultbuf
->h_name
[hst_resp
.h_name_len
- 1] != '\0'
402 || ({for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
403 if (resultbuf
->h_aliases
[cnt
][aliases_len
[cnt
] - 1]
406 cnt
< hst_resp
.h_aliases_cnt
; }))
408 /* We cannot use the database. */
409 if (mapped
->head
->gc_cycle
!= gc_cycle
)
420 /* Store the error number. */
421 *h_errnop
= hst_resp
.error
;
423 /* Set errno to 0 to indicate no error, just no found record. */
425 /* Even though we have not found anything, the result is zero. */
431 __close_nocancel_nostatus (sock
);
433 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
435 /* When we come here this means there has been a GC cycle while we
436 were looking for the data. This means the data might have been
437 inconsistent. Retry if possible. */
438 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
440 /* nscd is just running gc now. Disable using the mapping. */
441 if (atomic_fetch_add_relaxed (&mapped
->counter
, -1) == 1)
442 __nscd_unmap (mapped
);