1 /* Cache handling for host lookup.
2 Copyright (C) 2004-2008, 2009, 2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 # include <kernel-features.h>
37 typedef enum nss_status (*nss_gethostbyname4_r
)
38 (const char *name
, struct gaih_addrtuple
**pat
,
39 char *buffer
, size_t buflen
, int *errnop
,
40 int *h_errnop
, int32_t *ttlp
);
41 typedef enum nss_status (*nss_gethostbyname3_r
)
42 (const char *name
, int af
, struct hostent
*host
,
43 char *buffer
, size_t buflen
, int *errnop
,
44 int *h_errnop
, int32_t *, char **);
45 typedef enum nss_status (*nss_getcanonname_r
)
46 (const char *name
, char *buffer
, size_t buflen
, char **result
,
47 int *errnop
, int *h_errnop
);
50 static const ai_response_header notfound
=
52 .version
= NSCD_VERSION
,
62 addhstaiX (struct database_dyn
*db
, int fd
, request_header
*req
,
63 void *key
, uid_t uid
, struct hashentry
*const he
,
66 /* Search for the entry matching the key. Please note that we don't
67 look again in the table whether the dataset is now available. We
68 simply insert it. It does not matter if it is in there twice. The
69 pruning function only will look at the timestamp. */
71 /* We allocate all data in one memory block: the iov vector,
72 the response header and the dataset itself. */
76 ai_response_header resp
;
80 if (__builtin_expect (debug_level
> 0, 0))
83 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key
);
85 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key
);
88 static service_user
*hosts_database
;
89 service_user
*nip
= NULL
;
95 if (hosts_database
!= NULL
)
101 no_more
= __nss_database_lookup ("hosts", NULL
,
102 "dns [!UNAVAIL=return] files", &nip
);
104 if (__res_maybe_init (&_res
, 0) == -1)
107 /* If we are looking for both IPv4 and IPv6 address we don't want
108 the lookup functions to automatically promote IPv4 addresses to
109 IPv6 addresses. Currently this is decided by setting the
110 RES_USE_INET6 bit in _res.options. */
111 int old_res_options
= _res
.options
;
112 _res
.options
&= ~RES_USE_INET6
;
114 size_t tmpbuf6len
= 1024;
115 char *tmpbuf6
= alloca (tmpbuf6len
);
116 size_t tmpbuf4len
= 0;
117 char *tmpbuf4
= NULL
;
118 int32_t ttl
= INT32_MAX
;
120 char *key_copy
= NULL
;
121 bool alloca_used
= false;
126 int status
[2] = { NSS_STATUS_UNAVAIL
, NSS_STATUS_UNAVAIL
};
132 nss_gethostbyname4_r fct4
= __nss_lookup_function (nip
,
136 struct gaih_addrtuple atmem
;
137 struct gaih_addrtuple
*at
;
143 status
[1] = DL_CALL_FCT (fct4
, (key
, &at
, tmpbuf6
, tmpbuf6len
,
144 &rc6
, &herrno
, &ttl
));
145 if (rc6
!= ERANGE
|| (herrno
!= NETDB_INTERNAL
146 && herrno
!= TRY_AGAIN
))
148 tmpbuf6
= extend_alloca (tmpbuf6
, tmpbuf6len
, 2 * tmpbuf6len
);
151 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
154 if (status
[1] != NSS_STATUS_SUCCESS
)
157 /* We found the data. Count the addresses and the size. */
158 for (const struct gaih_addrtuple
*at2
= at
= &atmem
; at2
!= NULL
;
162 /* We do not handle anything other than IPv4 and IPv6
163 addresses. The getaddrinfo implementation does not
164 either so it is not worth trying to do more. */
165 if (at2
->family
== AF_INET
)
166 addrslen
+= INADDRSZ
;
167 else if (at2
->family
== AF_INET6
)
168 addrslen
+= IN6ADDRSZ
;
171 canonlen
= strlen (canon
) + 1;
173 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
175 /* Now we can allocate the data structure. If the TTL of the
176 entry is reported as zero do not cache the entry at all. */
177 if (ttl
!= 0 && he
== NULL
)
178 dataset
= (struct dataset
*) mempool_alloc (db
, total
183 /* We cannot permanently add the result in the moment. But
184 we can provide the result as is. Store the data in some
186 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
188 /* We cannot add this record to the permanent database. */
192 /* Fill in the address and address families. */
193 char *addrs
= dataset
->strdata
;
194 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
196 for (const struct gaih_addrtuple
*at2
= at
; at2
!= NULL
;
199 *family
++ = at2
->family
;
200 if (at2
->family
== AF_INET
)
201 addrs
= mempcpy (addrs
, at2
->addr
, INADDRSZ
);
202 else if (at2
->family
== AF_INET6
)
203 addrs
= mempcpy (addrs
, at2
->addr
, IN6ADDRSZ
);
210 /* Prefer the function which also returns the TTL and
212 nss_gethostbyname3_r fct
= __nss_lookup_function (nip
,
215 fct
= __nss_lookup_function (nip
, "gethostbyname2_r");
220 struct hostent th
[2];
222 /* Collect IPv6 information first. */
226 status
[0] = DL_CALL_FCT (fct
, (key
, AF_INET6
, &th
[0], tmpbuf6
,
227 tmpbuf6len
, &rc6
, &herrno
, &ttl
,
229 if (rc6
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
231 tmpbuf6
= extend_alloca (tmpbuf6
, tmpbuf6len
, 2 * tmpbuf6len
);
234 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
237 /* If the IPv6 lookup has been successful do not use the
238 buffer used in that lookup, use a new one. */
239 if (status
[0] == NSS_STATUS_SUCCESS
&& rc6
== 0)
242 tmpbuf4
= alloca (tmpbuf4len
);
246 tmpbuf4len
= tmpbuf6len
;
250 /* Next collect IPv4 information. */
254 status
[1] = DL_CALL_FCT (fct
, (key
, AF_INET
, &th
[1], tmpbuf4
,
255 tmpbuf4len
, &rc4
, &herrno
,
256 ttl
== INT32_MAX
? &ttl
: NULL
,
257 canon
== NULL
? &canon
: NULL
));
258 if (rc4
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
260 tmpbuf4
= extend_alloca (tmpbuf4
, tmpbuf4len
, 2 * tmpbuf4len
);
263 if (rc4
!= 0 && herrno
== NETDB_INTERNAL
)
266 if (status
[0] != NSS_STATUS_SUCCESS
267 && status
[1] != NSS_STATUS_SUCCESS
)
270 /* We found the data. Count the addresses and the size. */
271 for (int j
= 0; j
< 2; ++j
)
272 if (status
[j
] == NSS_STATUS_SUCCESS
)
273 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
276 addrslen
+= th
[j
].h_length
;
281 /* Determine the canonical name. */
282 nss_getcanonname_r cfct
;
283 cfct
= __nss_lookup_function (nip
, "getcanonname_r");
286 const size_t max_fqdn_len
= 256;
287 char *buf
= alloca (max_fqdn_len
);
291 if (DL_CALL_FCT (cfct
, (key
, buf
, max_fqdn_len
, &s
,
293 == NSS_STATUS_SUCCESS
)
296 /* Set to name now to avoid using gethostbyaddr. */
301 struct hostent
*hstent
= NULL
;
303 struct hostent hstent_mem
;
308 if (status
[1] == NSS_STATUS_SUCCESS
)
310 addr
= th
[1].h_addr_list
[0];
311 addrlen
= sizeof (struct in_addr
);
312 addrfamily
= AF_INET
;
316 addr
= th
[0].h_addr_list
[0];
317 addrlen
= sizeof (struct in6_addr
);
318 addrfamily
= AF_INET6
;
321 size_t tmpbuflen
= 512;
322 char *tmpbuf
= alloca (tmpbuflen
);
326 rc
= __gethostbyaddr2_r (addr
, addrlen
, addrfamily
,
327 &hstent_mem
, tmpbuf
, tmpbuflen
,
328 &hstent
, &herrno
, NULL
);
329 if (rc
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
331 tmpbuf
= extend_alloca (tmpbuf
, tmpbuflen
,
338 canon
= hstent
->h_name
;
345 canonlen
= canon
== NULL
? 0 : (strlen (canon
) + 1);
347 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
350 /* Now we can allocate the data structure. If the TTL of the
351 entry is reported as zero do not cache the entry at all. */
352 if (ttl
!= 0 && he
== NULL
)
353 dataset
= (struct dataset
*) mempool_alloc (db
, total
358 /* We cannot permanently add the result in the moment. But
359 we can provide the result as is. Store the data in some
361 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
363 /* We cannot add this record to the permanent database. */
367 /* Fill in the address and address families. */
368 char *addrs
= dataset
->strdata
;
369 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
371 for (int j
= 0; j
< 2; ++j
)
372 if (status
[j
] == NSS_STATUS_SUCCESS
)
373 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
375 addrs
= mempcpy (addrs
, th
[j
].h_addr_list
[i
],
377 *family
++ = th
[j
].h_addrtype
;
383 /* Fill in the rest of the dataset. */
384 dataset
->head
.allocsize
= total
+ req
->key_len
;
385 dataset
->head
.recsize
= total
- offsetof (struct dataset
, resp
);
386 dataset
->head
.notfound
= false;
387 dataset
->head
.nreloads
= he
== NULL
? 0 : (dh
->nreloads
+ 1);
388 dataset
->head
.usable
= true;
390 /* Compute the timeout time. */
391 dataset
->head
.timeout
= time (NULL
) + (ttl
== INT32_MAX
392 ? db
->postimeout
: ttl
);
394 dataset
->resp
.version
= NSCD_VERSION
;
395 dataset
->resp
.found
= 1;
396 dataset
->resp
.naddrs
= naddrs
;
397 dataset
->resp
.addrslen
= addrslen
;
398 dataset
->resp
.canonlen
= canonlen
;
399 dataset
->resp
.error
= NETDB_SUCCESS
;
402 cp
= mempcpy (cp
, canon
, canonlen
);
404 key_copy
= memcpy (cp
, key
, req
->key_len
);
406 assert (cp
== (char *) dataset
+ total
);
408 /* Now we can determine whether on refill we have to create a
409 new record or not. */
414 if (total
+ req
->key_len
== dh
->allocsize
415 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
416 && memcmp (&dataset
->resp
, dh
->data
,
417 dh
->allocsize
- offsetof (struct dataset
,
420 /* The data has not changed. We will just bump the
421 timeout value. Note that the new record has been
422 allocated on the stack and need not be freed. */
423 dh
->timeout
= dataset
->head
.timeout
;
428 /* We have to create a new record. Just allocate
429 appropriate memory and copy it. */
431 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
433 if (__builtin_expect (newp
!= NULL
, 1))
435 /* Adjust pointer into the memory block. */
436 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
438 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
442 /* Mark the old record as obsolete. */
448 /* We write the dataset before inserting it to the database
449 since while inserting this thread might block and so
450 would unnecessarily let the receiver wait. */
454 if (__builtin_expect (db
->mmap_used
, 1) && !alloca_used
)
456 assert (db
->wr_fd
!= -1);
457 assert ((char *) &dataset
->resp
> (char *) db
->data
);
458 assert ((char *) dataset
- (char *) db
->head
+ total
459 <= (sizeof (struct database_pers_head
)
460 + db
->head
->module
* sizeof (ref_t
)
461 + db
->head
->data_size
));
463 written
= sendfileall (fd
, db
->wr_fd
, (char *) &dataset
->resp
464 - (char *) db
->head
, dataset
->head
.recsize
);
465 # ifndef __ASSUME_SENDFILE
466 if (written
== -1 && errno
== ENOSYS
)
471 # ifndef __ASSUME_SENDFILE
475 writeall (fd
, &dataset
->resp
, dataset
->head
.recsize
);
481 if (nss_next_action (nip
, status
[1]) == NSS_ACTION_RETURN
)
484 if (nip
->next
== NULL
)
490 /* No result found. Create a negative result record. */
491 if (he
!= NULL
&& rc4
== EAGAIN
)
493 /* If we have an old record available but cannot find one now
494 because the service is not available we keep the old record
495 and make sure it does not get removed. */
496 if (reload_count
!= UINT_MAX
&& dh
->nreloads
== reload_count
)
497 /* Do not reset the value if we never not reload the record. */
498 dh
->nreloads
= reload_count
- 1;
502 /* We have no data. This means we send the standard reply for
504 total
= sizeof (notfound
);
507 TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
, MSG_NOSIGNAL
));
509 dataset
= mempool_alloc (db
, sizeof (struct dataset
) + req
->key_len
, 1);
510 /* If we cannot permanently store the result, so be it. */
513 dataset
->head
.allocsize
= sizeof (struct dataset
) + req
->key_len
;
514 dataset
->head
.recsize
= total
;
515 dataset
->head
.notfound
= true;
516 dataset
->head
.nreloads
= 0;
517 dataset
->head
.usable
= true;
519 /* Compute the timeout time. */
520 dataset
->head
.timeout
= time (NULL
) + db
->negtimeout
;
522 /* This is the reply. */
523 memcpy (&dataset
->resp
, ¬found
, total
);
525 /* Copy the key data. */
526 key_copy
= memcpy (dataset
->strdata
, key
, req
->key_len
);
531 _res
.options
= old_res_options
;
533 if (dataset
!= NULL
&& !alloca_used
)
535 /* If necessary, we also propagate the data to disk. */
539 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
540 msync ((void *) pval
,
541 ((uintptr_t) dataset
& pagesize_m1
) + total
+ req
->key_len
,
545 (void) cache_add (req
->type
, key_copy
, req
->key_len
, &dataset
->head
,
546 true, db
, uid
, he
== NULL
);
548 pthread_rwlock_unlock (&db
->lock
);
550 /* Mark the old entry as obsolete. */
558 addhstai (struct database_dyn
*db
, int fd
, request_header
*req
, void *key
,
561 addhstaiX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
566 readdhstai (struct database_dyn
*db
, struct hashentry
*he
, struct datahead
*dh
)
574 addhstaiX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);