1 /* Copyright (C) 1998-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
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.
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/>. */
24 #include <arpa/nameser.h>
25 #include <not-cancel.h>
27 #include "nscd-client.h"
28 #include "nscd_proto.h"
30 int __nss_not_use_nscd_hosts
;
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
) internal_function
;
39 __nscd_gethostbyname_r (const char *name
, struct hostent
*resultbuf
,
40 char *buffer
, size_t buflen
, struct hostent
**result
,
45 reqtype
= (_res
.options
& RES_USE_INET6
) ? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
47 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
48 buffer
, buflen
, result
, h_errnop
);
53 __nscd_gethostbyname2_r (const char *name
, int af
, struct hostent
*resultbuf
,
54 char *buffer
, size_t buflen
, struct hostent
**result
,
59 reqtype
= af
== AF_INET6
? GETHOSTBYNAMEv6
: GETHOSTBYNAME
;
61 return nscd_gethst_r (name
, strlen (name
) + 1, reqtype
, resultbuf
,
62 buffer
, buflen
, result
, h_errnop
);
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
)
73 if (!((len
== INADDRSZ
&& type
== AF_INET
)
74 || (len
== IN6ADDRSZ
&& type
== AF_INET6
)))
75 /* LEN and TYPE do not match. */
78 reqtype
= type
== AF_INET6
? GETHOSTBYADDRv6
: GETHOSTBYADDR
;
80 return nscd_gethst_r (addr
, len
, reqtype
, resultbuf
, buffer
, buflen
, result
,
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
89 libc_freeres_fn (hst_map_free
)
91 if (__hst_map_handle
.mapped
!= NO_MAPPING
)
93 void *p
= __hst_map_handle
.mapped
;
94 __hst_map_handle
.mapped
= NO_MAPPING
;
101 __nscd_get_nl_timestamp (void)
104 if (__nss_not_use_nscd_hosts
!= 0)
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
))
115 struct mapped_database
*map
= __hst_map_handle
.mapped
;
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
);
123 if (map
== NO_MAPPING
)
126 retval
= map
->head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
];
128 /* Release the lock. */
129 __hst_map_handle
.lock
= 0;
135 int __nss_have_localdomain attribute_hidden
;
139 nscd_gethst_r (const char *key
, size_t keylen
, request_type type
,
140 struct hostent
*resultbuf
, char *buffer
, size_t buflen
,
141 struct hostent
**result
, int *h_errnop
)
143 if (__builtin_expect (__nss_have_localdomain
>= 0, 0))
145 if (__nss_have_localdomain
== 0)
146 __nss_have_localdomain
= getenv ("LOCALDOMAIN") != NULL
? 1 : -1;
147 if (__nss_have_localdomain
> 0)
149 __nss_not_use_nscd_hosts
= 1;
157 /* If the mapping is available, try to search there instead of
158 communicating with the nscd. */
159 struct mapped_database
*mapped
;
160 mapped
= __nscd_get_map_ref (GETFDHST
, "hosts", &__hst_map_handle
,
164 const char *h_name
= NULL
;
165 const uint32_t *aliases_len
= NULL
;
166 const char *addr_list
= NULL
;
167 size_t addr_list_len
= 0;
169 const char *recend
= (const char *) ~UINTMAX_C (0);
171 hst_response_header hst_resp
;
172 if (mapped
!= NO_MAPPING
)
174 /* No const qualifier, as it can change during garbage collection. */
175 struct datahead
*found
= __nscd_cache_search (type
, key
, keylen
, mapped
,
179 h_name
= (char *) (&found
->data
[0].hstdata
+ 1);
180 hst_resp
= found
->data
[0].hstdata
;
181 aliases_len
= (uint32_t *) (h_name
+ hst_resp
.h_name_len
);
182 addr_list
= ((char *) aliases_len
183 + hst_resp
.h_aliases_cnt
* sizeof (uint32_t));
184 addr_list_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
185 recend
= (const char *) found
->data
+ found
->recsize
;
186 /* Now check if we can trust hst_resp fields. If GC is
187 in progress, it can contain anything. */
188 if (mapped
->head
->gc_cycle
!= gc_cycle
)
194 #ifndef _STRING_ARCH_unaligned
195 /* The aliases_len array in the mapped database might very
196 well be unaligned. We will access it word-wise so on
197 platforms which do not tolerate unaligned accesses we
198 need to make an aligned copy. */
199 if (((uintptr_t) aliases_len
& (__alignof__ (*aliases_len
) - 1))
202 uint32_t *tmp
= alloca (hst_resp
.h_aliases_cnt
203 * sizeof (uint32_t));
204 aliases_len
= memcpy (tmp
, aliases_len
,
205 hst_resp
.h_aliases_cnt
206 * sizeof (uint32_t));
209 if (type
!= GETHOSTBYADDR
&& type
!= GETHOSTBYNAME
)
211 if (hst_resp
.h_length
== INADDRSZ
)
212 addr_list
+= addr_list_len
;
213 addr_list_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
215 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
223 sock
= __nscd_open_socket (key
, keylen
, type
, &hst_resp
,
227 __nss_not_use_nscd_hosts
= 1;
232 /* No value found so far. */
235 if (__builtin_expect (hst_resp
.found
== -1, 0))
237 /* The daemon does not cache this database. */
238 __nss_not_use_nscd_hosts
= 1;
242 if (hst_resp
.found
== 1)
252 /* A first check whether the buffer is sufficiently large is possible. */
253 /* Now allocate the buffer the array for the group members. We must
254 align the pointer and the base of the h_addr_list pointers. */
255 align1
= ((__alignof__ (char *) - (cp
- ((char *) 0)))
256 & (__alignof__ (char *) - 1));
257 align2
= ((__alignof__ (char *) - ((cp
+ align1
+ hst_resp
.h_name_len
)
259 & (__alignof__ (char *) - 1));
260 if (buflen
< (align1
+ hst_resp
.h_name_len
+ align2
261 + ((hst_resp
.h_aliases_cnt
+ hst_resp
.h_addr_list_cnt
264 + hst_resp
.h_addr_list_cnt
* (type
== AF_INET
265 ? INADDRSZ
: IN6ADDRSZ
)))
268 *h_errnop
= NETDB_INTERNAL
;
269 __set_errno (ERANGE
);
275 /* Prepare the result as far as we can. */
276 resultbuf
->h_aliases
= (char **) cp
;
277 cp
+= (hst_resp
.h_aliases_cnt
+ 1) * sizeof (char *);
278 resultbuf
->h_addr_list
= (char **) cp
;
279 cp
+= (hst_resp
.h_addr_list_cnt
+ 1) * sizeof (char *);
281 resultbuf
->h_name
= cp
;
282 cp
+= hst_resp
.h_name_len
+ align2
;
284 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
286 resultbuf
->h_addrtype
= AF_INET
;
287 resultbuf
->h_length
= INADDRSZ
;
291 resultbuf
->h_addrtype
= AF_INET6
;
292 resultbuf
->h_length
= IN6ADDRSZ
;
294 for (cnt
= 0; cnt
< hst_resp
.h_addr_list_cnt
; ++cnt
)
296 resultbuf
->h_addr_list
[cnt
] = cp
;
297 cp
+= resultbuf
->h_length
;
299 resultbuf
->h_addr_list
[cnt
] = NULL
;
305 vec
[0].iov_base
= resultbuf
->h_name
;
306 vec
[0].iov_len
= hst_resp
.h_name_len
;
307 total_len
= hst_resp
.h_name_len
;
310 if (hst_resp
.h_aliases_cnt
> 0)
312 aliases_len
= alloca (hst_resp
.h_aliases_cnt
313 * sizeof (uint32_t));
314 vec
[n
].iov_base
= (void *) aliases_len
;
315 vec
[n
].iov_len
= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
317 total_len
+= hst_resp
.h_aliases_cnt
* sizeof (uint32_t);
321 if (type
== GETHOSTBYADDR
|| type
== GETHOSTBYNAME
)
323 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
324 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
326 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
332 if (hst_resp
.h_length
== INADDRSZ
)
334 ignore
= alloca (hst_resp
.h_addr_list_cnt
* INADDRSZ
);
335 vec
[n
].iov_base
= ignore
;
336 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
338 total_len
+= hst_resp
.h_addr_list_cnt
* INADDRSZ
;
343 vec
[n
].iov_base
= resultbuf
->h_addr_list
[0];
344 vec
[n
].iov_len
= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
346 total_len
+= hst_resp
.h_addr_list_cnt
* IN6ADDRSZ
;
351 if ((size_t) __readvall (sock
, vec
, n
) != total_len
)
356 memcpy (resultbuf
->h_name
, h_name
, hst_resp
.h_name_len
);
357 memcpy (resultbuf
->h_addr_list
[0], addr_list
, addr_list_len
);
360 /* Now we also can read the aliases. */
362 for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
364 resultbuf
->h_aliases
[cnt
] = cp
;
365 cp
+= aliases_len
[cnt
];
366 total_len
+= aliases_len
[cnt
];
368 resultbuf
->h_aliases
[cnt
] = NULL
;
370 if (__builtin_expect ((const char *) addr_list
+ addr_list_len
371 + total_len
> recend
, 0))
373 /* aliases_len array might contain garbage during nscd GC cycle,
374 retry rather than fail in that case. */
375 if (addr_list
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
379 /* See whether this would exceed the buffer capacity. */
380 if (__builtin_expect (cp
> buffer
+ buflen
, 0))
382 /* aliases_len array might contain garbage during nscd GC cycle,
383 retry rather than fail in that case. */
384 if (addr_list
!= NULL
&& mapped
->head
->gc_cycle
!= gc_cycle
)
392 /* And finally read the aliases. */
393 if (addr_list
== NULL
)
396 || ((size_t) __readall (sock
, resultbuf
->h_aliases
[0], total_len
)
405 memcpy (resultbuf
->h_aliases
[0],
406 (const char *) addr_list
+ addr_list_len
, total_len
);
408 /* Try to detect corrupt databases. */
409 if (resultbuf
->h_name
[hst_resp
.h_name_len
- 1] != '\0'
410 || ({for (cnt
= 0; cnt
< hst_resp
.h_aliases_cnt
; ++cnt
)
411 if (resultbuf
->h_aliases
[cnt
][aliases_len
[cnt
] - 1]
414 cnt
< hst_resp
.h_aliases_cnt
; }))
416 /* We cannot use the database. */
417 if (mapped
->head
->gc_cycle
!= gc_cycle
)
428 /* Store the error number. */
429 *h_errnop
= hst_resp
.error
;
431 /* Set errno to 0 to indicate no error, just no found record. */
433 /* Even though we have not found anything, the result is zero. */
439 close_not_cancel_no_status (sock
);
441 if (__nscd_drop_map_ref (mapped
, &gc_cycle
) != 0)
443 /* When we come here this means there has been a GC cycle while we
444 were looking for the data. This means the data might have been
445 inconsistent. Retry if possible. */
446 if ((gc_cycle
& 1) != 0 || ++nretries
== 5 || retval
== -1)
448 /* nscd is just running gc now. Disable using the mapping. */
449 if (atomic_decrement_val (&mapped
->counter
) == 0)
450 __nscd_unmap (mapped
);