1 | /* Copyright (C) 2004-2024 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
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. |
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 |
12 | Lesser General Public License for more details. |
13 | |
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/>. */ |
17 | |
18 | #include <assert.h> |
19 | #include <errno.h> |
20 | #include <netdb.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <unistd.h> |
24 | #include <not-cancel.h> |
25 | |
26 | #include "nscd-client.h" |
27 | #include "nscd_proto.h" |
28 | |
29 | |
30 | /* Define in nscd_gethst_r.c. */ |
31 | extern int __nss_not_use_nscd_hosts; |
32 | |
33 | |
34 | /* We use the mapping from nscd_gethst. */ |
35 | libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden; |
36 | |
37 | /* Defined in nscd_gethst_r.c. */ |
38 | extern int __nss_have_localdomain attribute_hidden; |
39 | |
40 | |
41 | int |
42 | __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop) |
43 | { |
44 | if (__glibc_unlikely (__nss_have_localdomain >= 0)) |
45 | { |
46 | if (__nss_have_localdomain == 0) |
47 | __nss_have_localdomain = getenv ("LOCALDOMAIN" ) != NULL ? 1 : -1; |
48 | if (__nss_have_localdomain > 0) |
49 | { |
50 | __nss_not_use_nscd_hosts = 1; |
51 | return -1; |
52 | } |
53 | } |
54 | |
55 | size_t keylen = strlen (key) + 1; |
56 | int gc_cycle; |
57 | int nretries = 0; |
58 | |
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 (type: GETFDHST, name: "hosts" , mapptr: &__hst_map_handle, |
63 | gc_cyclep: &gc_cycle); |
64 | |
65 | retry:; |
66 | struct nscd_ai_result *resultbuf = NULL; |
67 | const char *recend = (const char *) ~UINTMAX_C (0); |
68 | char *respdata = NULL; |
69 | int retval = -1; |
70 | int sock = -1; |
71 | ai_response_header ai_resp; |
72 | |
73 | if (mapped != NO_MAPPING) |
74 | { |
75 | struct datahead *found = __nscd_cache_search (type: GETAI, key, keylen, |
76 | mapped, datalen: sizeof ai_resp); |
77 | if (found != NULL) |
78 | { |
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) |
85 | { |
86 | retval = -2; |
87 | goto out; |
88 | } |
89 | } |
90 | } |
91 | |
92 | /* If we do not have the cache mapped, try to get the data over the |
93 | socket. */ |
94 | if (respdata == NULL) |
95 | { |
96 | sock = __nscd_open_socket (key, keylen, type: GETAI, response: &ai_resp, |
97 | responselen: sizeof (ai_resp)); |
98 | if (sock == -1) |
99 | { |
100 | /* nscd not running or wrong version. */ |
101 | __nss_not_use_nscd_hosts = 1; |
102 | goto out; |
103 | } |
104 | } |
105 | |
106 | if (ai_resp.found == 1) |
107 | { |
108 | size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen; |
109 | |
110 | /* This check really only affects the case where the data |
111 | comes from the mapped cache. */ |
112 | if (respdata + datalen > recend) |
113 | { |
114 | assert (sock == -1); |
115 | goto out; |
116 | } |
117 | |
118 | /* Create result. */ |
119 | resultbuf = (struct nscd_ai_result *) malloc (size: sizeof (*resultbuf) |
120 | + datalen); |
121 | if (resultbuf == NULL) |
122 | { |
123 | *h_errnop = NETDB_INTERNAL; |
124 | goto out_close; |
125 | } |
126 | |
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); |
133 | else |
134 | resultbuf->canon = NULL; |
135 | |
136 | if (respdata == NULL) |
137 | { |
138 | /* Read the data from the socket. */ |
139 | if ((size_t) __readall (fd: sock, buf: resultbuf + 1, len: datalen) == datalen) |
140 | { |
141 | retval = 0; |
142 | *result = resultbuf; |
143 | } |
144 | else |
145 | { |
146 | free (ptr: resultbuf); |
147 | *h_errnop = NETDB_INTERNAL; |
148 | } |
149 | } |
150 | else |
151 | { |
152 | /* Copy the data in the block. */ |
153 | memcpy (resultbuf + 1, respdata, datalen); |
154 | |
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. */ |
159 | { |
160 | if (mapped->head->gc_cycle != gc_cycle) |
161 | retval = -2; |
162 | else |
163 | free (ptr: resultbuf); |
164 | goto out_close; |
165 | } |
166 | |
167 | retval = 0; |
168 | *result = resultbuf; |
169 | } |
170 | } |
171 | else |
172 | { |
173 | if (__glibc_unlikely (ai_resp.found == -1)) |
174 | { |
175 | /* The daemon does not cache this database. */ |
176 | __nss_not_use_nscd_hosts = 1; |
177 | goto out_close; |
178 | } |
179 | |
180 | /* Store the error number. */ |
181 | *h_errnop = ai_resp.error; |
182 | |
183 | /* Set errno to 0 to indicate no error, just no found record. */ |
184 | __set_errno (0); |
185 | /* Even though we have not found anything, the result is zero. */ |
186 | retval = 0; |
187 | } |
188 | |
189 | out_close: |
190 | if (sock != -1) |
191 | __close_nocancel_nostatus (fd: sock); |
192 | out: |
193 | if (__nscd_drop_map_ref (map: mapped, gc_cycle: &gc_cycle) != 0) |
194 | { |
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) |
199 | { |
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); |
203 | mapped = NO_MAPPING; |
204 | } |
205 | |
206 | if (retval != -1) |
207 | { |
208 | *result = NULL; |
209 | free (ptr: resultbuf); |
210 | goto retry; |
211 | } |
212 | } |
213 | |
214 | return retval; |
215 | } |
216 | |