1 | /* Copyright (C) 1998-2019 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <resolv/resolv-internal.h> |
21 | #include <stdio.h> |
22 | #include <string.h> |
23 | #include <stdint.h> |
24 | #include <arpa/nameser.h> |
25 | #include <not-cancel.h> |
26 | |
27 | #include "nscd-client.h" |
28 | #include "nscd_proto.h" |
29 | |
30 | int __nss_not_use_nscd_hosts; |
31 | |
32 | static int nscd_gethst_r (const char *key, size_t keylen, request_type type, |
33 | struct hostent *resultbuf, char *buffer, |
34 | size_t buflen, struct hostent **result, |
35 | int *h_errnop); |
36 | |
37 | |
38 | int |
39 | __nscd_gethostbyname_r (const char *name, struct hostent *resultbuf, |
40 | char *buffer, size_t buflen, struct hostent **result, |
41 | int *h_errnop) |
42 | { |
43 | request_type reqtype; |
44 | |
45 | reqtype = res_use_inet6 () ? GETHOSTBYNAMEv6 : GETHOSTBYNAME; |
46 | |
47 | return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf, |
48 | buffer, buflen, result, h_errnop); |
49 | } |
50 | |
51 | |
52 | int |
53 | __nscd_gethostbyname2_r (const char *name, int af, struct hostent *resultbuf, |
54 | char *buffer, size_t buflen, struct hostent **result, |
55 | int *h_errnop) |
56 | { |
57 | request_type reqtype; |
58 | |
59 | reqtype = af == AF_INET6 ? GETHOSTBYNAMEv6 : GETHOSTBYNAME; |
60 | |
61 | return nscd_gethst_r (name, strlen (name) + 1, reqtype, resultbuf, |
62 | buffer, buflen, result, h_errnop); |
63 | } |
64 | |
65 | |
66 | int |
67 | __nscd_gethostbyaddr_r (const void *addr, socklen_t len, int type, |
68 | struct hostent *resultbuf, char *buffer, size_t buflen, |
69 | struct hostent **result, int *h_errnop) |
70 | { |
71 | request_type reqtype; |
72 | |
73 | if (!((len == INADDRSZ && type == AF_INET) |
74 | || (len == IN6ADDRSZ && type == AF_INET6))) |
75 | /* LEN and TYPE do not match. */ |
76 | return -1; |
77 | |
78 | reqtype = type == AF_INET6 ? GETHOSTBYADDRv6 : GETHOSTBYADDR; |
79 | |
80 | return nscd_gethst_r (addr, len, reqtype, resultbuf, buffer, buflen, result, |
81 | h_errnop); |
82 | } |
83 | |
84 | |
85 | libc_locked_map_ptr (, __hst_map_handle) attribute_hidden; |
86 | /* Note that we only free the structure if necessary. The memory |
87 | mapping is not removed since it is not visible to the malloc |
88 | handling. */ |
89 | libc_freeres_fn (hst_map_free) |
90 | { |
91 | if (__hst_map_handle.mapped != NO_MAPPING) |
92 | { |
93 | void *p = __hst_map_handle.mapped; |
94 | __hst_map_handle.mapped = NO_MAPPING; |
95 | free (p); |
96 | } |
97 | } |
98 | |
99 | |
100 | uint32_t |
101 | __nscd_get_nl_timestamp (void) |
102 | { |
103 | uint32_t retval; |
104 | if (__nss_not_use_nscd_hosts != 0) |
105 | return 0; |
106 | |
107 | /* __nscd_get_mapping can change hst_map_handle.mapped to NO_MAPPING. |
108 | However, __nscd_get_mapping assumes the prior value was not NO_MAPPING. |
109 | Thus we have to acquire the lock to prevent this thread from changing |
110 | hst_map_handle.mapped to NO_MAPPING while another thread is inside |
111 | __nscd_get_mapping. */ |
112 | if (!__nscd_acquire_maplock (&__hst_map_handle)) |
113 | return 0; |
114 | |
115 | struct mapped_database *map = __hst_map_handle.mapped; |
116 | |
117 | if (map == NULL |
118 | || (map != NO_MAPPING |
119 | && map->head->nscd_certainly_running == 0 |
120 | && map->head->timestamp + MAPPING_TIMEOUT < time (NULL))) |
121 | map = __nscd_get_mapping (GETFDHST, "hosts" , &__hst_map_handle.mapped); |
122 | |
123 | if (map == NO_MAPPING) |
124 | retval = 0; |
125 | else |
126 | retval = map->head->extra_data[NSCD_HST_IDX_CONF_TIMESTAMP]; |
127 | |
128 | /* Release the lock. */ |
129 | __hst_map_handle.lock = 0; |
130 | |
131 | return retval; |
132 | } |
133 | |
134 | |
135 | int __nss_have_localdomain attribute_hidden; |
136 | |
137 | static int |
138 | nscd_gethst_r (const char *key, size_t keylen, request_type type, |
139 | struct hostent *resultbuf, char *buffer, size_t buflen, |
140 | struct hostent **result, int *h_errnop) |
141 | { |
142 | if (__glibc_unlikely (__nss_have_localdomain >= 0)) |
143 | { |
144 | if (__nss_have_localdomain == 0) |
145 | __nss_have_localdomain = getenv ("LOCALDOMAIN" ) != NULL ? 1 : -1; |
146 | if (__nss_have_localdomain > 0) |
147 | { |
148 | __nss_not_use_nscd_hosts = 1; |
149 | return -1; |
150 | } |
151 | } |
152 | |
153 | int gc_cycle; |
154 | int nretries = 0; |
155 | |
156 | /* If the mapping is available, try to search there instead of |
157 | communicating with the nscd. */ |
158 | struct mapped_database *mapped; |
159 | mapped = __nscd_get_map_ref (GETFDHST, "hosts" , &__hst_map_handle, |
160 | &gc_cycle); |
161 | |
162 | retry:; |
163 | const char *h_name = NULL; |
164 | const uint32_t *aliases_len = NULL; |
165 | const char *addr_list = NULL; |
166 | size_t addr_list_len = 0; |
167 | int retval = -1; |
168 | const char *recend = (const char *) ~UINTMAX_C (0); |
169 | int sock = -1; |
170 | hst_response_header hst_resp; |
171 | if (mapped != NO_MAPPING) |
172 | { |
173 | /* No const qualifier, as it can change during garbage collection. */ |
174 | struct datahead *found = __nscd_cache_search (type, key, keylen, mapped, |
175 | sizeof hst_resp); |
176 | if (found != NULL) |
177 | { |
178 | h_name = (char *) (&found->data[0].hstdata + 1); |
179 | hst_resp = found->data[0].hstdata; |
180 | aliases_len = (uint32_t *) (h_name + hst_resp.h_name_len); |
181 | addr_list = ((char *) aliases_len |
182 | + hst_resp.h_aliases_cnt * sizeof (uint32_t)); |
183 | addr_list_len = hst_resp.h_addr_list_cnt * INADDRSZ; |
184 | recend = (const char *) found->data + found->recsize; |
185 | /* Now check if we can trust hst_resp fields. If GC is |
186 | in progress, it can contain anything. */ |
187 | if (mapped->head->gc_cycle != gc_cycle) |
188 | { |
189 | retval = -2; |
190 | goto out; |
191 | } |
192 | |
193 | #if !_STRING_ARCH_unaligned |
194 | /* The aliases_len array in the mapped database might very |
195 | well be unaligned. We will access it word-wise so on |
196 | platforms which do not tolerate unaligned accesses we |
197 | need to make an aligned copy. */ |
198 | if (((uintptr_t) aliases_len & (__alignof__ (*aliases_len) - 1)) |
199 | != 0) |
200 | { |
201 | uint32_t *tmp = alloca (hst_resp.h_aliases_cnt |
202 | * sizeof (uint32_t)); |
203 | aliases_len = memcpy (tmp, aliases_len, |
204 | hst_resp.h_aliases_cnt |
205 | * sizeof (uint32_t)); |
206 | } |
207 | #endif |
208 | if (type != GETHOSTBYADDR && type != GETHOSTBYNAME) |
209 | { |
210 | if (hst_resp.h_length == INADDRSZ) |
211 | addr_list += addr_list_len; |
212 | addr_list_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ; |
213 | } |
214 | if (__builtin_expect ((const char *) addr_list + addr_list_len |
215 | > recend, 0)) |
216 | goto out; |
217 | } |
218 | } |
219 | |
220 | if (h_name == NULL) |
221 | { |
222 | sock = __nscd_open_socket (key, keylen, type, &hst_resp, |
223 | sizeof (hst_resp)); |
224 | if (sock == -1) |
225 | { |
226 | __nss_not_use_nscd_hosts = 1; |
227 | goto out; |
228 | } |
229 | } |
230 | |
231 | /* No value found so far. */ |
232 | *result = NULL; |
233 | |
234 | if (__glibc_unlikely (hst_resp.found == -1)) |
235 | { |
236 | /* The daemon does not cache this database. */ |
237 | __nss_not_use_nscd_hosts = 1; |
238 | goto out_close; |
239 | } |
240 | |
241 | if (hst_resp.found == 1) |
242 | { |
243 | char *cp = buffer; |
244 | uintptr_t align1; |
245 | uintptr_t align2; |
246 | size_t total_len; |
247 | ssize_t cnt; |
248 | char *ignore; |
249 | int n; |
250 | |
251 | /* A first check whether the buffer is sufficiently large is possible. */ |
252 | /* Now allocate the buffer the array for the group members. We must |
253 | align the pointer and the base of the h_addr_list pointers. */ |
254 | align1 = ((__alignof__ (char *) - (cp - ((char *) 0))) |
255 | & (__alignof__ (char *) - 1)); |
256 | align2 = ((__alignof__ (char *) - ((cp + align1 + hst_resp.h_name_len) |
257 | - ((char *) 0))) |
258 | & (__alignof__ (char *) - 1)); |
259 | if (buflen < (align1 + hst_resp.h_name_len + align2 |
260 | + ((hst_resp.h_aliases_cnt + hst_resp.h_addr_list_cnt |
261 | + 2) |
262 | * sizeof (char *)) |
263 | + hst_resp.h_addr_list_cnt * (type == AF_INET |
264 | ? INADDRSZ : IN6ADDRSZ))) |
265 | { |
266 | no_room: |
267 | *h_errnop = NETDB_INTERNAL; |
268 | __set_errno (ERANGE); |
269 | retval = ERANGE; |
270 | goto out_close; |
271 | } |
272 | cp += align1; |
273 | |
274 | /* Prepare the result as far as we can. */ |
275 | resultbuf->h_aliases = (char **) cp; |
276 | cp += (hst_resp.h_aliases_cnt + 1) * sizeof (char *); |
277 | resultbuf->h_addr_list = (char **) cp; |
278 | cp += (hst_resp.h_addr_list_cnt + 1) * sizeof (char *); |
279 | |
280 | resultbuf->h_name = cp; |
281 | cp += hst_resp.h_name_len + align2; |
282 | |
283 | if (type == GETHOSTBYADDR || type == GETHOSTBYNAME) |
284 | { |
285 | resultbuf->h_addrtype = AF_INET; |
286 | resultbuf->h_length = INADDRSZ; |
287 | } |
288 | else |
289 | { |
290 | resultbuf->h_addrtype = AF_INET6; |
291 | resultbuf->h_length = IN6ADDRSZ; |
292 | } |
293 | for (cnt = 0; cnt < hst_resp.h_addr_list_cnt; ++cnt) |
294 | { |
295 | resultbuf->h_addr_list[cnt] = cp; |
296 | cp += resultbuf->h_length; |
297 | } |
298 | resultbuf->h_addr_list[cnt] = NULL; |
299 | |
300 | if (h_name == NULL) |
301 | { |
302 | struct iovec vec[4]; |
303 | |
304 | vec[0].iov_base = resultbuf->h_name; |
305 | vec[0].iov_len = hst_resp.h_name_len; |
306 | total_len = hst_resp.h_name_len; |
307 | n = 1; |
308 | |
309 | if (hst_resp.h_aliases_cnt > 0) |
310 | { |
311 | aliases_len = alloca (hst_resp.h_aliases_cnt |
312 | * sizeof (uint32_t)); |
313 | vec[n].iov_base = (void *) aliases_len; |
314 | vec[n].iov_len = hst_resp.h_aliases_cnt * sizeof (uint32_t); |
315 | |
316 | total_len += hst_resp.h_aliases_cnt * sizeof (uint32_t); |
317 | ++n; |
318 | } |
319 | |
320 | if (type == GETHOSTBYADDR || type == GETHOSTBYNAME) |
321 | { |
322 | vec[n].iov_base = resultbuf->h_addr_list[0]; |
323 | vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ; |
324 | |
325 | total_len += hst_resp.h_addr_list_cnt * INADDRSZ; |
326 | |
327 | ++n; |
328 | } |
329 | else |
330 | { |
331 | if (hst_resp.h_length == INADDRSZ) |
332 | { |
333 | ignore = alloca (hst_resp.h_addr_list_cnt * INADDRSZ); |
334 | vec[n].iov_base = ignore; |
335 | vec[n].iov_len = hst_resp.h_addr_list_cnt * INADDRSZ; |
336 | |
337 | total_len += hst_resp.h_addr_list_cnt * INADDRSZ; |
338 | |
339 | ++n; |
340 | } |
341 | |
342 | vec[n].iov_base = resultbuf->h_addr_list[0]; |
343 | vec[n].iov_len = hst_resp.h_addr_list_cnt * IN6ADDRSZ; |
344 | |
345 | total_len += hst_resp.h_addr_list_cnt * IN6ADDRSZ; |
346 | |
347 | ++n; |
348 | } |
349 | |
350 | if ((size_t) __readvall (sock, vec, n) != total_len) |
351 | goto out_close; |
352 | } |
353 | else |
354 | { |
355 | memcpy (resultbuf->h_name, h_name, hst_resp.h_name_len); |
356 | memcpy (resultbuf->h_addr_list[0], addr_list, addr_list_len); |
357 | } |
358 | |
359 | /* Now we also can read the aliases. */ |
360 | total_len = 0; |
361 | for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt) |
362 | { |
363 | resultbuf->h_aliases[cnt] = cp; |
364 | cp += aliases_len[cnt]; |
365 | total_len += aliases_len[cnt]; |
366 | } |
367 | resultbuf->h_aliases[cnt] = NULL; |
368 | |
369 | if (__builtin_expect ((const char *) addr_list + addr_list_len |
370 | + total_len > recend, 0)) |
371 | { |
372 | /* aliases_len array might contain garbage during nscd GC cycle, |
373 | retry rather than fail in that case. */ |
374 | if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle) |
375 | retval = -2; |
376 | goto out_close; |
377 | } |
378 | /* See whether this would exceed the buffer capacity. */ |
379 | if (__glibc_unlikely (cp > buffer + buflen)) |
380 | { |
381 | /* aliases_len array might contain garbage during nscd GC cycle, |
382 | retry rather than fail in that case. */ |
383 | if (addr_list != NULL && mapped->head->gc_cycle != gc_cycle) |
384 | { |
385 | retval = -2; |
386 | goto out_close; |
387 | } |
388 | goto no_room; |
389 | } |
390 | |
391 | /* And finally read the aliases. */ |
392 | if (addr_list == NULL) |
393 | { |
394 | if (total_len == 0 |
395 | || ((size_t) __readall (sock, resultbuf->h_aliases[0], total_len) |
396 | == total_len)) |
397 | { |
398 | retval = 0; |
399 | *result = resultbuf; |
400 | } |
401 | } |
402 | else |
403 | { |
404 | memcpy (resultbuf->h_aliases[0], |
405 | (const char *) addr_list + addr_list_len, total_len); |
406 | |
407 | /* Try to detect corrupt databases. */ |
408 | if (resultbuf->h_name[hst_resp.h_name_len - 1] != '\0' |
409 | || ({for (cnt = 0; cnt < hst_resp.h_aliases_cnt; ++cnt) |
410 | if (resultbuf->h_aliases[cnt][aliases_len[cnt] - 1] |
411 | != '\0') |
412 | break; |
413 | cnt < hst_resp.h_aliases_cnt; })) |
414 | { |
415 | /* We cannot use the database. */ |
416 | if (mapped->head->gc_cycle != gc_cycle) |
417 | retval = -2; |
418 | goto out_close; |
419 | } |
420 | |
421 | retval = 0; |
422 | *result = resultbuf; |
423 | } |
424 | } |
425 | else |
426 | { |
427 | /* Store the error number. */ |
428 | *h_errnop = hst_resp.error; |
429 | |
430 | /* Set errno to 0 to indicate no error, just no found record. */ |
431 | __set_errno (0); |
432 | /* Even though we have not found anything, the result is zero. */ |
433 | retval = 0; |
434 | } |
435 | |
436 | out_close: |
437 | if (sock != -1) |
438 | __close_nocancel_nostatus (sock); |
439 | out: |
440 | if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0) |
441 | { |
442 | /* When we come here this means there has been a GC cycle while we |
443 | were looking for the data. This means the data might have been |
444 | inconsistent. Retry if possible. */ |
445 | if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1) |
446 | { |
447 | /* nscd is just running gc now. Disable using the mapping. */ |
448 | if (atomic_decrement_val (&mapped->counter) == 0) |
449 | __nscd_unmap (mapped); |
450 | mapped = NO_MAPPING; |
451 | } |
452 | |
453 | if (retval != -1) |
454 | goto retry; |
455 | } |
456 | |
457 | return retval; |
458 | } |
459 | |