1 /* Copyright (C) 2004-2024 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/>. */
24 #include <not-cancel.h>
26 #include "nscd-client.h"
27 #include "nscd_proto.h"
30 /* Define in nscd_gethst_r.c. */
31 extern int __nss_not_use_nscd_hosts
;
34 /* We use the mapping from nscd_gethst. */
35 libc_locked_map_ptr (extern, __hst_map_handle
) attribute_hidden
;
37 /* Defined in nscd_gethst_r.c. */
38 extern int __nss_have_localdomain attribute_hidden
;
42 __nscd_getai (const char *key
, struct nscd_ai_result
**result
, int *h_errnop
)
44 if (__glibc_unlikely (__nss_have_localdomain
>= 0))
46 if (__nss_have_localdomain
== 0)
47 __nss_have_localdomain
= getenv ("LOCALDOMAIN") != NULL
? 1 : -1;
48 if (__nss_have_localdomain
> 0)
50 __nss_not_use_nscd_hosts
= 1;
55 size_t keylen
= strlen (key
) + 1;
59 /* If the mapping is available, try to search there instead of
60 communicating with the nscd. */
61 struct mapped_database
*mapped
;
62 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
66 struct nscd_ai_result
*resultbuf
= NULL
;
67 const char *recend
= (const char *) ~UINTMAX_C (0);
68 char *respdata
= NULL
;
71 ai_response_header ai_resp
;
73 if (mapped
!= NO_MAPPING
)
75 struct datahead
*found
= __nscd_cache_search (GETAI
, key
, keylen
,
76 mapped
, sizeof ai_resp
);
79 respdata
= (char *) (&found
->data
[0].aidata
+ 1);
80 ai_resp
= found
->data
[0].aidata
;
81 recend
= (const char *) found
->data
+ found
->recsize
;
82 /* Now check if we can trust ai_resp fields. If GC is
83 in progress, it can contain anything. */
84 if (mapped
->head
->gc_cycle
!= gc_cycle
)
92 /* If we do not have the cache mapped, try to get the data over the
96 sock
= __nscd_open_socket (key
, keylen
, GETAI
, &ai_resp
,
100 /* nscd not running or wrong version. */
101 __nss_not_use_nscd_hosts
= 1;
106 if (ai_resp
.found
== 1)
108 size_t datalen
= ai_resp
.naddrs
+ ai_resp
.addrslen
+ ai_resp
.canonlen
;
110 /* This check really only affects the case where the data
111 comes from the mapped cache. */
112 if (respdata
+ datalen
> recend
)
119 resultbuf
= (struct nscd_ai_result
*) malloc (sizeof (*resultbuf
)
121 if (resultbuf
== NULL
)
123 *h_errnop
= NETDB_INTERNAL
;
127 /* Set up the data structure, including pointers. */
128 resultbuf
->naddrs
= ai_resp
.naddrs
;
129 resultbuf
->addrs
= (char *) (resultbuf
+ 1);
130 resultbuf
->family
= (uint8_t *) (resultbuf
->addrs
+ ai_resp
.addrslen
);
131 if (ai_resp
.canonlen
!= 0)
132 resultbuf
->canon
= (char *) (resultbuf
->family
+ resultbuf
->naddrs
);
134 resultbuf
->canon
= NULL
;
136 if (respdata
== NULL
)
138 /* Read the data from the socket. */
139 if ((size_t) __readall (sock
, resultbuf
+ 1, datalen
) == datalen
)
147 *h_errnop
= NETDB_INTERNAL
;
152 /* Copy the data in the block. */
153 memcpy (resultbuf
+ 1, respdata
, datalen
);
155 /* Try to detect corrupt databases. */
156 if (resultbuf
->canon
!= NULL
157 && resultbuf
->canon
[ai_resp
.canonlen
- 1] != '\0')
158 /* We cannot use the database. */
160 if (mapped
->head
->gc_cycle
!= gc_cycle
)
173 if (__glibc_unlikely (ai_resp
.found
== -1))
175 /* The daemon does not cache this database. */
176 __nss_not_use_nscd_hosts
= 1;
180 /* Store the error number. */
181 *h_errnop
= ai_resp
.error
;
183 /* Set errno to 0 to indicate no error, just no found record. */
185 /* Even though we have not found anything, the result is zero. */
191 __close_nocancel_nostatus (sock
);
193 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
195 /* When we come here this means there has been a GC cycle while we
196 were looking for the data. This means the data might have been
197 inconsistent. Retry if possible. */
198 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
200 /* nscd is just running gc now. Disable using the mapping. */
201 if (atomic_fetch_add_relaxed (&mapped
->counter
, -1) == 1)
202 __nscd_unmap (mapped
);