1 /* Cache handling for host lookup.
2 Copyright (C) 2004-2017 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, see <http://www.gnu.org/licenses/>. */
28 #include <resolv/resolv-internal.h>
29 #include <resolv/resolv_context.h>
30 #include <resolv/res_use_inet6.h>
35 # include <kernel-features.h>
39 typedef enum nss_status (*nss_gethostbyname4_r
)
40 (const char *name
, struct gaih_addrtuple
**pat
,
41 char *buffer
, size_t buflen
, int *errnop
,
42 int *h_errnop
, int32_t *ttlp
);
43 typedef enum nss_status (*nss_gethostbyname3_r
)
44 (const char *name
, int af
, struct hostent
*host
,
45 char *buffer
, size_t buflen
, int *errnop
,
46 int *h_errnop
, int32_t *, char **);
47 typedef enum nss_status (*nss_getcanonname_r
)
48 (const char *name
, char *buffer
, size_t buflen
, char **result
,
49 int *errnop
, int *h_errnop
);
52 static const ai_response_header notfound
=
54 .version
= NSCD_VERSION
,
64 addhstaiX (struct database_dyn
*db
, int fd
, request_header
*req
,
65 void *key
, uid_t uid
, struct hashentry
*const he
,
68 /* Search for the entry matching the key. Please note that we don't
69 look again in the table whether the dataset is now available. We
70 simply insert it. It does not matter if it is in there twice. The
71 pruning function only will look at the timestamp. */
73 /* We allocate all data in one memory block: the iov vector,
74 the response header and the dataset itself. */
78 ai_response_header resp
;
82 if (__glibc_unlikely (debug_level
> 0))
85 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key
);
87 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key
);
90 static service_user
*hosts_database
;
97 if (hosts_database
== NULL
)
98 no_more
= __nss_database_lookup ("hosts", NULL
,
99 "dns [!UNAVAIL=return] files",
103 nip
= hosts_database
;
105 /* Initialize configurations. If we are looking for both IPv4 and
106 IPv6 address we don't want the lookup functions to automatically
107 promote IPv4 addresses to IPv6 addresses. Therefore, use the
108 _no_inet6 variant. */
109 struct resolv_context
*ctx
= __resolv_context_get ();
110 bool enable_inet6
= __resolv_context_disable_inet6 (ctx
);
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;
122 time_t timeout
= MAX_TIMEOUT_VALUE
;
127 int status
[2] = { NSS_STATUS_UNAVAIL
, NSS_STATUS_UNAVAIL
};
133 nss_gethostbyname4_r fct4
= __nss_lookup_function (nip
,
137 struct gaih_addrtuple atmem
;
138 struct gaih_addrtuple
*at
;
144 status
[1] = DL_CALL_FCT (fct4
, (key
, &at
, tmpbuf6
, tmpbuf6len
,
145 &rc6
, &herrno
, &ttl
));
146 if (rc6
!= ERANGE
|| (herrno
!= NETDB_INTERNAL
147 && herrno
!= TRY_AGAIN
))
149 tmpbuf6
= extend_alloca (tmpbuf6
, tmpbuf6len
, 2 * tmpbuf6len
);
152 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
155 if (status
[1] != NSS_STATUS_SUCCESS
)
158 /* We found the data. Count the addresses and the size. */
159 for (const struct gaih_addrtuple
*at2
= at
= &atmem
; at2
!= NULL
;
163 /* We do not handle anything other than IPv4 and IPv6
164 addresses. The getaddrinfo implementation does not
165 either so it is not worth trying to do more. */
166 if (at2
->family
== AF_INET
)
167 addrslen
+= INADDRSZ
;
168 else if (at2
->family
== AF_INET6
)
169 addrslen
+= IN6ADDRSZ
;
172 canonlen
= strlen (canon
) + 1;
174 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
176 /* Now we can allocate the data structure. If the TTL of the
177 entry is reported as zero do not cache the entry at all. */
178 if (ttl
!= 0 && he
== NULL
)
179 dataset
= (struct dataset
*) mempool_alloc (db
, total
184 /* We cannot permanently add the result in the moment. But
185 we can provide the result as is. Store the data in some
187 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
189 /* We cannot add this record to the permanent database. */
193 /* Fill in the address and address families. */
194 char *addrs
= dataset
->strdata
;
195 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
197 for (const struct gaih_addrtuple
*at2
= at
; at2
!= NULL
;
200 *family
++ = at2
->family
;
201 if (at2
->family
== AF_INET
)
202 addrs
= mempcpy (addrs
, at2
->addr
, INADDRSZ
);
203 else if (at2
->family
== AF_INET6
)
204 addrs
= mempcpy (addrs
, at2
->addr
, IN6ADDRSZ
);
211 /* Prefer the function which also returns the TTL and
213 nss_gethostbyname3_r fct
= __nss_lookup_function (nip
,
216 fct
= __nss_lookup_function (nip
, "gethostbyname2_r");
221 struct hostent th
[2];
223 /* Collect IPv6 information first. */
227 status
[0] = DL_CALL_FCT (fct
, (key
, AF_INET6
, &th
[0], tmpbuf6
,
228 tmpbuf6len
, &rc6
, &herrno
, &ttl
,
230 if (rc6
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
232 tmpbuf6
= extend_alloca (tmpbuf6
, tmpbuf6len
, 2 * tmpbuf6len
);
235 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
238 /* If the IPv6 lookup has been successful do not use the
239 buffer used in that lookup, use a new one. */
240 if (status
[0] == NSS_STATUS_SUCCESS
&& rc6
== 0)
243 tmpbuf4
= alloca (tmpbuf4len
);
247 tmpbuf4len
= tmpbuf6len
;
251 /* Next collect IPv4 information. */
255 status
[1] = DL_CALL_FCT (fct
, (key
, AF_INET
, &th
[1], tmpbuf4
,
256 tmpbuf4len
, &rc4
, &herrno
,
257 ttl
== INT32_MAX
? &ttl
: NULL
,
258 canon
== NULL
? &canon
: NULL
));
259 if (rc4
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
261 tmpbuf4
= extend_alloca (tmpbuf4
, tmpbuf4len
, 2 * tmpbuf4len
);
264 if (rc4
!= 0 && herrno
== NETDB_INTERNAL
)
267 if (status
[0] != NSS_STATUS_SUCCESS
268 && status
[1] != NSS_STATUS_SUCCESS
)
271 /* We found the data. Count the addresses and the size. */
272 for (int j
= 0; j
< 2; ++j
)
273 if (status
[j
] == NSS_STATUS_SUCCESS
)
274 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
277 addrslen
+= th
[j
].h_length
;
282 /* Determine the canonical name. */
283 nss_getcanonname_r cfct
;
284 cfct
= __nss_lookup_function (nip
, "getcanonname_r");
287 const size_t max_fqdn_len
= 256;
288 char *buf
= alloca (max_fqdn_len
);
292 if (DL_CALL_FCT (cfct
, (key
, buf
, max_fqdn_len
, &s
,
294 == NSS_STATUS_SUCCESS
)
297 /* Set to name now to avoid using gethostbyaddr. */
302 struct hostent
*hstent
= NULL
;
304 struct hostent hstent_mem
;
309 if (status
[1] == NSS_STATUS_SUCCESS
)
311 addr
= th
[1].h_addr_list
[0];
312 addrlen
= sizeof (struct in_addr
);
313 addrfamily
= AF_INET
;
317 addr
= th
[0].h_addr_list
[0];
318 addrlen
= sizeof (struct in6_addr
);
319 addrfamily
= AF_INET6
;
322 size_t tmpbuflen
= 512;
323 char *tmpbuf
= alloca (tmpbuflen
);
327 rc
= __gethostbyaddr2_r (addr
, addrlen
, addrfamily
,
328 &hstent_mem
, tmpbuf
, tmpbuflen
,
329 &hstent
, &herrno
, NULL
);
330 if (rc
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
332 tmpbuf
= extend_alloca (tmpbuf
, tmpbuflen
,
339 canon
= hstent
->h_name
;
346 canonlen
= canon
== NULL
? 0 : (strlen (canon
) + 1);
348 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
351 /* Now we can allocate the data structure. If the TTL of the
352 entry is reported as zero do not cache the entry at all. */
353 if (ttl
!= 0 && he
== NULL
)
354 dataset
= (struct dataset
*) mempool_alloc (db
, total
359 /* We cannot permanently add the result in the moment. But
360 we can provide the result as is. Store the data in some
362 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
364 /* We cannot add this record to the permanent database. */
368 /* Fill in the address and address families. */
369 char *addrs
= dataset
->strdata
;
370 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
372 for (int j
= 0; j
< 2; ++j
)
373 if (status
[j
] == NSS_STATUS_SUCCESS
)
374 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
376 addrs
= mempcpy (addrs
, th
[j
].h_addr_list
[i
],
378 *family
++ = th
[j
].h_addrtype
;
384 timeout
= datahead_init_pos (&dataset
->head
, total
+ req
->key_len
,
385 total
- offsetof (struct dataset
, resp
),
386 he
== NULL
? 0 : dh
->nreloads
+ 1,
387 ttl
== INT32_MAX
? db
->postimeout
: ttl
);
389 /* Fill in the rest of the dataset. */
390 dataset
->resp
.version
= NSCD_VERSION
;
391 dataset
->resp
.found
= 1;
392 dataset
->resp
.naddrs
= naddrs
;
393 dataset
->resp
.addrslen
= addrslen
;
394 dataset
->resp
.canonlen
= canonlen
;
395 dataset
->resp
.error
= NETDB_SUCCESS
;
398 cp
= mempcpy (cp
, canon
, canonlen
);
400 key_copy
= memcpy (cp
, key
, req
->key_len
);
402 assert (cp
== (char *) dataset
+ total
);
404 /* Now we can determine whether on refill we have to create a
405 new record or not. */
410 if (total
+ req
->key_len
== dh
->allocsize
411 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
412 && memcmp (&dataset
->resp
, dh
->data
,
413 dh
->allocsize
- offsetof (struct dataset
,
416 /* The data has not changed. We will just bump the
417 timeout value. Note that the new record has been
418 allocated on the stack and need not be freed. */
419 dh
->timeout
= dataset
->head
.timeout
;
420 dh
->ttl
= dataset
->head
.ttl
;
425 /* We have to create a new record. Just allocate
426 appropriate memory and copy it. */
428 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
430 if (__glibc_likely (newp
!= NULL
))
432 /* Adjust pointer into the memory block. */
433 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
435 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
439 /* Mark the old record as obsolete. */
445 /* We write the dataset before inserting it to the database
446 since while inserting this thread might block and so
447 would unnecessarily let the receiver wait. */
451 if (__builtin_expect (db
->mmap_used
, 1) && !alloca_used
)
453 assert (db
->wr_fd
!= -1);
454 assert ((char *) &dataset
->resp
> (char *) db
->data
);
455 assert ((char *) dataset
- (char *) db
->head
+ total
456 <= (sizeof (struct database_pers_head
)
457 + db
->head
->module
* sizeof (ref_t
)
458 + db
->head
->data_size
));
459 # ifndef __ASSUME_SENDFILE
463 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;
500 /* Reload with the same time-to-live value. */
501 timeout
= dh
->timeout
= time (NULL
) + dh
->ttl
;
505 /* We have no data. This means we send the standard reply for
507 total
= sizeof (notfound
);
510 TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
, MSG_NOSIGNAL
));
512 /* If we have a transient error or cannot permanently store the
514 if (rc4
== EAGAIN
|| __builtin_expect (db
->negtimeout
== 0, 0))
516 /* Mark the old entry as obsolete. */
521 else if ((dataset
= mempool_alloc (db
, (sizeof (struct dataset
)
522 + req
->key_len
), 1)) != NULL
)
524 timeout
= datahead_init_neg (&dataset
->head
,
525 sizeof (struct dataset
) + req
->key_len
,
526 total
, db
->negtimeout
);
528 /* This is the reply. */
529 memcpy (&dataset
->resp
, ¬found
, total
);
531 /* Copy the key data. */
532 key_copy
= memcpy (dataset
->strdata
, key
, req
->key_len
);
537 __resolv_context_enable_inet6 (ctx
, enable_inet6
);
538 __resolv_context_put (ctx
);
540 if (dataset
!= NULL
&& !alloca_used
)
542 /* If necessary, we also propagate the data to disk. */
546 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
547 msync ((void *) pval
,
548 ((uintptr_t) dataset
& pagesize_m1
) + total
+ req
->key_len
,
552 (void) cache_add (req
->type
, key_copy
, req
->key_len
, &dataset
->head
,
553 true, db
, uid
, he
== NULL
);
555 pthread_rwlock_unlock (&db
->lock
);
557 /* Mark the old entry as obsolete. */
567 addhstai (struct database_dyn
*db
, int fd
, request_header
*req
, void *key
,
570 addhstaiX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
575 readdhstai (struct database_dyn
*db
, struct hashentry
*he
, struct datahead
*dh
)
583 return addhstaiX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);