]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/nscd_gethst_r.c
Move libc_freeres_ptrs and libc_subfreeres to hidden/weak functions
[thirdparty/glibc.git] / nscd / nscd_gethst_r.c
CommitLineData
6d7e8eda 1/* Copyright (C) 1998-2023 Free Software Foundation, Inc.
67479a70 2 This file is part of the GNU C Library.
67479a70
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
67479a70
UD
8
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
41bdb6e2 12 Lesser General Public License for more details.
67479a70 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
67479a70
UD
17
18#include <errno.h>
b76e0659 19#include <resolv/resolv-internal.h>
67479a70 20#include <stdio.h>
67479a70 21#include <string.h>
e054f494 22#include <stdint.h>
67479a70 23#include <arpa/nameser.h>
40c38b6c 24#include <not-cancel.h>
67479a70 25
3f804c95 26#include "nscd-client.h"
67479a70
UD
27#include "nscd_proto.h"
28
29int __nss_not_use_nscd_hosts;
30
31static int nscd_gethst_r (const char *key, size_t keylen, request_type type,
32 struct hostent *resultbuf, char *buffer,
261eada2 33 size_t buflen, struct hostent **result,
75b3047e 34 int *h_errnop);
67479a70
UD
35
36
37int
38__nscd_gethostbyname_r (const char *name, struct hostent *resultbuf,
261eada2
UD
39 char *buffer, size_t buflen, struct hostent **result,
40 int *h_errnop)
67479a70 41{
3f8b44be 42 return nscd_gethst_r (name, strlen (name) + 1, GETHOSTBYNAME, resultbuf,
261eada2 43 buffer, buflen, result, h_errnop);
67479a70
UD
44}
45
46
47int
48__nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf,
261eada2
UD
49 char *buffer, size_t buflen, struct hostent **result,
50 int *h_errnop)
67479a70
UD
51{
52 request_type reqtype;
53
54 reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME;
55
56 return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf,
261eada2 57 buffer, buflen, result, h_errnop);
67479a70
UD
58}
59
60
61int
9d4d69b8 62__nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type,
67479a70 63 struct hostent *resultbuf, char *buffer, size_t buflen,
261eada2 64 struct hostent **result, int *h_errnop)
67479a70
UD
65{
66 request_type reqtype;
67
68 if (!((len == INADDRSZ && type == AF_INET)
69 || (len == IN6ADDRSZ && type == AF_INET6)))
70 /* LEN and TYPE do not match. */
1670698f 71 return -1;
67479a70
UD
72
73 reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR;
74
261eada2 75 return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result,
67479a70
UD
76 h_errnop);
77}
78
79
6f8a7dff 80libc_locked_map_ptr (, __hst_map_handle) attribute_hidden;
c207f23b
UD
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. */
88677348
AZN
84void
85__nscd_hst_map_freemem (void)
67479a70 86{
ed2ced8a
UD
87 if (__hst_map_handle.mapped != NO_MAPPING)
88 {
89 void *p = __hst_map_handle.mapped;
90 __hst_map_handle.mapped = NO_MAPPING;
91 free (p);
92 }
67479a70
UD
93}
94
95
3a2c0242
UD
96uint32_t
97__nscd_get_nl_timestamp (void)
98{
509072a0 99 uint32_t retval;
3a2c0242
UD
100 if (__nss_not_use_nscd_hosts != 0)
101 return 0;
102
509072a0
AJ
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))
109 return 0;
110
3a2c0242
UD
111 struct mapped_database *map = __hst_map_handle.mapped;
112
113 if (map == NULL
114 || (map != NO_MAPPING
115 && map->head->nscd_certainly_running == 0
fa4a1927 116 && map->head->timestamp + MAPPING_TIMEOUT < time64_now ()))
3a2c0242
UD
117 map = __nscd_get_mapping (GETFDHST, "hosts", &__hst_map_handle.mapped);
118
119 if (map == NO_MAPPING)
509072a0
AJ
120 retval = 0;
121 else
122 retval = map->head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP];
123
124 /* Release the lock. */
125 __hst_map_handle.lock = 0;
3a2c0242 126
509072a0 127 return retval;
3a2c0242
UD
128}
129
130
50e481ce
UD
131int __nss_have_localdomain attribute_hidden;
132
67479a70
UD
133static int
134nscd_gethst_r (const char *key, size_t keylen, request_type type,
135 struct hostent *resultbuf, char *buffer, size_t buflen,
261eada2 136 struct hostent **result, int *h_errnop)
67479a70 137{
a1ffb40e 138 if (__glibc_unlikely (__nss_have_localdomain >= 0))
50e481ce
UD
139 {
140 if (__nss_have_localdomain == 0)
141 __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
142 if (__nss_have_localdomain > 0)
143 {
144 __nss_not_use_nscd_hosts = 1;
145 return -1;
146 }
147 }
148
0891f970
UD
149 int gc_cycle;
150 int nretries = 0;
151
152 /* If the mapping is available, try to search there instead of
153 communicating with the nscd. */
154 struct mapped_database *mapped;
ed2ced8a
UD
155 mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
156 &gc_cycle);
0891f970
UD
157
158 retry:;
c207f23b
UD
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;
163 int retval = -1;
c207f23b
UD
164 const char *recend = (const char *) ~UINTMAX_C (0);
165 int sock = -1;
1a77d37f 166 hst_response_header hst_resp;
af38d7ce 167 if (mapped != NO_MAPPING)
67479a70 168 {
1a77d37f 169 /* No const qualifier, as it can change during garbage collection. */
cfe1fc10
JJ
170 struct datahead *found = __nscd_cache_search (type, key, keylen, mapped,
171 sizeof hst_resp);
c207f23b
UD
172 if (found != NULL)
173 {
1a77d37f
JJ
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);
c207f23b 177 addr_list = ((char *) aliases_len
1a77d37f
JJ
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)
184 {
185 retval = -2;
186 goto out;
187 }
c207f23b 188
c207f23b
UD
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)
195 {
1a77d37f 196 uint32_t *tmp = alloca (hst_resp.h_aliases_cnt
c207f23b
UD
197 * sizeof (uint32_t));
198 aliases_len = memcpy (tmp, aliases_len,
1a77d37f 199 hst_resp.h_aliases_cnt
c207f23b
UD
200 * sizeof (uint32_t));
201 }
c207f23b
UD
202 if (type != GETHOSTBYADDR && type != GETHOSTBYNAME)
203 {
1a77d37f 204 if (hst_resp.h_length == INADDRSZ)
c207f23b 205 addr_list += addr_list_len;
1a77d37f 206 addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
c207f23b 207 }
c207f23b
UD
208 if (__builtin_expect ((const char *) addr_list + addr_list_len
209 > recend, 0))
1a77d37f 210 goto out;
c207f23b
UD
211 }
212 }
213
1a77d37f 214 if (h_name == NULL)
c207f23b 215 {
1a77d37f
JJ
216 sock = __nscd_open_socket (key, keylen, type, &hst_resp,
217 sizeof (hst_resp));
c207f23b
UD
218 if (sock == -1)
219 {
220 __nss_not_use_nscd_hosts = 1;
4a4a65f2 221 goto out;
c207f23b 222 }
67479a70
UD
223 }
224
16aac663
UD
225 /* No value found so far. */
226 *result = NULL;
227
a1ffb40e 228 if (__glibc_unlikely (hst_resp.found == -1))
67479a70
UD
229 {
230 /* The daemon does not cache this database. */
67479a70 231 __nss_not_use_nscd_hosts = 1;
c207f23b 232 goto out_close;
67479a70
UD
233 }
234
1a77d37f 235 if (hst_resp.found == 1)
67479a70 236 {
67479a70 237 char *cp = buffer;
3810076f
UD
238 uintptr_t align1;
239 uintptr_t align2;
67479a70
UD
240 size_t total_len;
241 ssize_t cnt;
242 char *ignore;
243 int n;
244
49c091e5 245 /* A first check whether the buffer is sufficiently large is possible. */
67479a70 246 /* Now allocate the buffer the array for the group members. We must
3810076f 247 align the pointer and the base of the h_addr_list pointers. */
cc4d6614 248 align1 = ((__alignof__ (char *) - ((uintptr_t) cp))
3810076f 249 & (__alignof__ (char *) - 1));
cc4d6614 250 align2 = ((__alignof__ (char *) - ((uintptr_t) (cp + align1 + hst_resp.h_name_len)))
3810076f 251 & (__alignof__ (char *) - 1));
1a77d37f
JJ
252 if (buflen < (align1 + hst_resp.h_name_len + align2
253 + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt
c207f23b 254 + 2)
67479a70 255 * sizeof (char *))
1a77d37f
JJ
256 + hst_resp.h_addr_list_cnt * (type == AF_INET
257 ? INADDRSZ : IN6ADDRSZ)))
67479a70
UD
258 {
259 no_room:
4379b403 260 *h_errnop = NETDB_INTERNAL;
67479a70 261 __set_errno (ERANGE);
261eada2 262 retval = ERANGE;
c207f23b 263 goto out_close;
67479a70 264 }
3810076f 265 cp += align1;
67479a70
UD
266
267 /* Prepare the result as far as we can. */
268 resultbuf->h_aliases = (char **) cp;
1a77d37f 269 cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *);
67479a70 270 resultbuf->h_addr_list = (char **) cp;
1a77d37f 271 cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *);
67479a70
UD
272
273 resultbuf->h_name = cp;
1a77d37f 274 cp += hst_resp.h_name_len + align2;
67479a70 275
67479a70
UD
276 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
277 {
67479a70
UD
278 resultbuf->h_addrtype = AF_INET;
279 resultbuf->h_length = INADDRSZ;
67479a70
UD
280 }
281 else
282 {
c207f23b
UD
283 resultbuf->h_addrtype = AF_INET6;
284 resultbuf->h_length = IN6ADDRSZ;
285 }
1a77d37f 286 for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt)
c207f23b
UD
287 {
288 resultbuf->h_addr_list[cnt] = cp;
289 cp += resultbuf->h_length;
290 }
291 resultbuf->h_addr_list[cnt] = NULL;
67479a70 292
c207f23b
UD
293 if (h_name == NULL)
294 {
4a4a65f2
UD
295 struct iovec vec[4];
296
c207f23b 297 vec[0].iov_base = resultbuf->h_name;
1a77d37f
JJ
298 vec[0].iov_len = hst_resp.h_name_len;
299 total_len = hst_resp.h_name_len;
c207f23b 300 n = 1;
67479a70 301
1a77d37f 302 if (hst_resp.h_aliases_cnt > 0)
c207f23b 303 {
1a77d37f 304 aliases_len = alloca (hst_resp.h_aliases_cnt
c207f23b
UD
305 * sizeof (uint32_t));
306 vec[n].iov_base = (void *) aliases_len;
1a77d37f 307 vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t);
67479a70 308
1a77d37f 309 total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t);
c207f23b
UD
310 ++n;
311 }
67479a70 312
c207f23b 313 if (type == GETHOSTBYADDR || type == GETHOSTBYNAME)
67479a70 314 {
c207f23b 315 vec[n].iov_base = resultbuf->h_addr_list[0];
1a77d37f 316 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
c207f23b 317
1a77d37f 318 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
c207f23b
UD
319
320 ++n;
67479a70 321 }
c207f23b
UD
322 else
323 {
1a77d37f 324 if (hst_resp.h_length == INADDRSZ)
c207f23b 325 {
1a77d37f 326 ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ);
c207f23b 327 vec[n].iov_base = ignore;
1a77d37f 328 vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ;
67479a70 329
1a77d37f 330 total_len += hst_resp.h_addr_list_cnt * INADDRSZ;
67479a70 331
c207f23b
UD
332 ++n;
333 }
67479a70 334
c207f23b 335 vec[n].iov_base = resultbuf->h_addr_list[0];
1a77d37f 336 vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ;
67479a70 337
1a77d37f 338 total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ;
c207f23b
UD
339
340 ++n;
341 }
342
d2dc7d84 343 if ((size_t) __readvall (sock, vec, n) != total_len)
c207f23b
UD
344 goto out_close;
345 }
346 else
347 {
1a77d37f 348 memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len);
c207f23b
UD
349 memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len);
350 }
67479a70
UD
351
352 /* Now we also can read the aliases. */
353 total_len = 0;
1a77d37f 354 for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
67479a70
UD
355 {
356 resultbuf->h_aliases[cnt] = cp;
357 cp += aliases_len[cnt];
358 total_len += aliases_len[cnt];
359 }
360 resultbuf->h_aliases[cnt] = NULL;
361
c207f23b
UD
362 if (__builtin_expect ((const char *) addr_list + addr_list_len
363 + total_len > recend, 0))
1a77d37f
JJ
364 {
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)
368 retval = -2;
369 goto out_close;
370 }
67479a70 371 /* See whether this would exceed the buffer capacity. */
a1ffb40e 372 if (__glibc_unlikely (cp > buffer + buflen))
1a77d37f
JJ
373 {
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)
377 {
378 retval = -2;
379 goto out_close;
380 }
381 goto no_room;
382 }
67479a70
UD
383
384 /* And finally read the aliases. */
c207f23b
UD
385 if (addr_list == NULL)
386 {
74ac0a89
UD
387 if (total_len == 0
388 || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len)
389 == total_len))
c207f23b
UD
390 {
391 retval = 0;
392 *result = resultbuf;
393 }
394 }
395 else
261eada2 396 {
c207f23b
UD
397 memcpy (resultbuf->h_aliases[0],
398 (const char *) addr_list + addr_list_len, total_len);
399
5429ff76 400 /* Try to detect corrupt databases. */
1a77d37f
JJ
401 if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0'
402 || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt)
5429ff76
UD
403 if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1]
404 != '\0')
405 break;
1a77d37f
JJ
406 cnt < hst_resp.h_aliases_cnt; }))
407 {
408 /* We cannot use the database. */
409 if (mapped->head->gc_cycle != gc_cycle)
410 retval = -2;
411 goto out_close;
412 }
5429ff76 413
261eada2
UD
414 retval = 0;
415 *result = resultbuf;
416 }
67479a70
UD
417 }
418 else
419 {
420 /* Store the error number. */
1a77d37f 421 *h_errnop = hst_resp.error;
67479a70 422
cfca0aa3
UD
423 /* Set errno to 0 to indicate no error, just no found record. */
424 __set_errno (0);
261eada2
UD
425 /* Even though we have not found anything, the result is zero. */
426 retval = 0;
67479a70 427 }
12c80513 428
c207f23b
UD
429 out_close:
430 if (sock != -1)
c181840c 431 __close_nocancel_nostatus (sock);
12c80513 432 out:
1a77d37f 433 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
0891f970
UD
434 {
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. */
1a77d37f 438 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
0891f970
UD
439 {
440 /* nscd is just running gc now. Disable using the mapping. */
a364a3a7 441 if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
1a77d37f 442 __nscd_unmap (mapped);
0891f970
UD
443 mapped = NO_MAPPING;
444 }
445
1a77d37f
JJ
446 if (retval != -1)
447 goto retry;
0891f970 448 }
12c80513 449
261eada2 450 return retval;
67479a70 451}