1 /* Cache handling for host lookup.
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
27 #include <resolv/resolv-internal.h>
28 #include <resolv/resolv_context.h>
29 #include <scratch_buffer.h>
35 static const ai_response_header notfound
=
37 .version
= NSCD_VERSION
,
47 addhstaiX (struct database_dyn
*db
, int fd
, request_header
*req
,
48 void *key
, uid_t uid
, struct hashentry
*const he
,
51 /* Search for the entry matching the key. Please note that we don't
52 look again in the table whether the dataset is now available. We
53 simply insert it. It does not matter if it is in there twice. The
54 pruning function only will look at the timestamp. */
56 /* We allocate all data in one memory block: the iov vector,
57 the response header and the dataset itself. */
61 ai_response_header resp
;
65 if (__glibc_unlikely (debug_level
> 0))
68 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key
);
70 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key
);
79 no_more
= !__nss_database_get (nss_database_hosts
, &nip
);
81 /* Initialize configurations. */
82 struct resolv_context
*ctx
= __resolv_context_get ();
86 struct scratch_buffer tmpbuf6
;
87 scratch_buffer_init (&tmpbuf6
);
88 struct scratch_buffer tmpbuf4
;
89 scratch_buffer_init (&tmpbuf4
);
90 struct scratch_buffer canonbuf
;
91 scratch_buffer_init (&canonbuf
);
93 int32_t ttl
= INT32_MAX
;
95 char *key_copy
= NULL
;
96 bool alloca_used
= false;
97 time_t timeout
= MAX_TIMEOUT_VALUE
;
102 int status
[2] = { NSS_STATUS_UNAVAIL
, NSS_STATUS_UNAVAIL
};
109 nss_gethostbyname4_r
*fct4
= __nss_lookup_function (nip
,
113 struct gaih_addrtuple
*at
;
119 status
[1] = DL_CALL_FCT (fct4
, (key
, &at
,
120 tmpbuf6
.data
, tmpbuf6
.length
,
121 &rc6
, &herrno
, &ttl
));
122 if (rc6
!= ERANGE
|| (herrno
!= NETDB_INTERNAL
123 && herrno
!= TRY_AGAIN
))
125 if (!scratch_buffer_grow (&tmpbuf6
))
132 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
135 if (status
[1] != NSS_STATUS_SUCCESS
)
138 /* We found the data. Count the addresses and the size. */
139 for (const struct gaih_addrtuple
*at2
= at
; at2
!= NULL
;
143 /* We do not handle anything other than IPv4 and IPv6
144 addresses. The getaddrinfo implementation does not
145 either so it is not worth trying to do more. */
146 if (at2
->family
== AF_INET
)
147 addrslen
+= INADDRSZ
;
148 else if (at2
->family
== AF_INET6
)
149 addrslen
+= IN6ADDRSZ
;
152 canonlen
= strlen (canon
) + 1;
154 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
156 /* Now we can allocate the data structure. If the TTL of the
157 entry is reported as zero do not cache the entry at all. */
158 if (ttl
!= 0 && he
== NULL
)
159 dataset
= (struct dataset
*) mempool_alloc (db
, total
164 /* We cannot permanently add the result in the moment. But
165 we can provide the result as is. Store the data in some
167 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
169 /* We cannot add this record to the permanent database. */
173 /* Fill in the address and address families. */
174 char *addrs
= dataset
->strdata
;
175 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
177 for (const struct gaih_addrtuple
*at2
= at
; at2
!= NULL
;
180 *family
++ = at2
->family
;
181 if (at2
->family
== AF_INET
)
182 addrs
= mempcpy (addrs
, at2
->addr
, INADDRSZ
);
183 else if (at2
->family
== AF_INET6
)
184 addrs
= mempcpy (addrs
, at2
->addr
, IN6ADDRSZ
);
191 /* Prefer the function which also returns the TTL and
193 nss_gethostbyname3_r
*fct
194 = __nss_lookup_function (nip
, "gethostbyname3_r");
196 fct
= __nss_lookup_function (nip
, "gethostbyname2_r");
201 struct hostent th
[2];
203 /* Collect IPv6 information first. */
207 status
[0] = DL_CALL_FCT (fct
, (key
, AF_INET6
, &th
[0],
208 tmpbuf6
.data
, tmpbuf6
.length
,
211 if (rc6
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
213 if (!scratch_buffer_grow (&tmpbuf6
))
220 if (rc6
!= 0 && herrno
== NETDB_INTERNAL
)
223 /* Next collect IPv4 information. */
227 status
[1] = DL_CALL_FCT (fct
, (key
, AF_INET
, &th
[1],
228 tmpbuf4
.data
, tmpbuf4
.length
,
230 ttl
== INT32_MAX
? &ttl
: NULL
,
231 canon
== NULL
? &canon
: NULL
));
232 if (rc4
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
234 if (!scratch_buffer_grow (&tmpbuf4
))
241 if (rc4
!= 0 && herrno
== NETDB_INTERNAL
)
244 if (status
[0] != NSS_STATUS_SUCCESS
245 && status
[1] != NSS_STATUS_SUCCESS
)
248 /* We found the data. Count the addresses and the size. */
249 for (int j
= 0; j
< 2; ++j
)
250 if (status
[j
] == NSS_STATUS_SUCCESS
)
251 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
254 addrslen
+= th
[j
].h_length
;
259 /* Determine the canonical name. */
260 nss_getcanonname_r
*cfct
;
261 cfct
= __nss_lookup_function (nip
, "getcanonname_r");
267 if (DL_CALL_FCT (cfct
, (key
, canonbuf
.data
, canonbuf
.length
,
269 == NSS_STATUS_SUCCESS
)
272 /* Set to name now to avoid using gethostbyaddr. */
277 struct hostent
*hstent
= NULL
;
279 struct hostent hstent_mem
;
284 if (status
[1] == NSS_STATUS_SUCCESS
)
286 addr
= th
[1].h_addr_list
[0];
287 addrlen
= sizeof (struct in_addr
);
288 addrfamily
= AF_INET
;
292 addr
= th
[0].h_addr_list
[0];
293 addrlen
= sizeof (struct in6_addr
);
294 addrfamily
= AF_INET6
;
300 rc
= __gethostbyaddr2_r (addr
, addrlen
, addrfamily
,
302 canonbuf
.data
, canonbuf
.length
,
303 &hstent
, &herrno
, NULL
);
304 if (rc
!= ERANGE
|| herrno
!= NETDB_INTERNAL
)
306 if (!scratch_buffer_grow (&canonbuf
))
316 canon
= hstent
->h_name
;
323 canonlen
= canon
== NULL
? 0 : (strlen (canon
) + 1);
325 total
= sizeof (*dataset
) + naddrs
+ addrslen
+ canonlen
;
328 /* Now we can allocate the data structure. If the TTL of the
329 entry is reported as zero do not cache the entry at all. */
330 if (ttl
!= 0 && he
== NULL
)
331 dataset
= (struct dataset
*) mempool_alloc (db
, total
336 /* We cannot permanently add the result in the moment. But
337 we can provide the result as is. Store the data in some
339 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
341 /* We cannot add this record to the permanent database. */
345 /* Fill in the address and address families. */
346 char *addrs
= dataset
->strdata
;
347 uint8_t *family
= (uint8_t *) (addrs
+ addrslen
);
349 for (int j
= 0; j
< 2; ++j
)
350 if (status
[j
] == NSS_STATUS_SUCCESS
)
351 for (int i
= 0; th
[j
].h_addr_list
[i
] != NULL
; ++i
)
353 addrs
= mempcpy (addrs
, th
[j
].h_addr_list
[i
],
355 *family
++ = th
[j
].h_addrtype
;
361 timeout
= datahead_init_pos (&dataset
->head
, total
+ req
->key_len
,
362 total
- offsetof (struct dataset
, resp
),
363 he
== NULL
? 0 : dh
->nreloads
+ 1,
364 ttl
== INT32_MAX
? db
->postimeout
: ttl
);
366 /* Fill in the rest of the dataset. */
367 dataset
->resp
.version
= NSCD_VERSION
;
368 dataset
->resp
.found
= 1;
369 dataset
->resp
.naddrs
= naddrs
;
370 dataset
->resp
.addrslen
= addrslen
;
371 dataset
->resp
.canonlen
= canonlen
;
372 dataset
->resp
.error
= NETDB_SUCCESS
;
375 cp
= mempcpy (cp
, canon
, canonlen
);
377 key_copy
= memcpy (cp
, key
, req
->key_len
);
379 assert (cp
== (char *) dataset
+ total
);
381 /* Now we can determine whether on refill we have to create a
382 new record or not. */
387 if (total
+ req
->key_len
== dh
->allocsize
388 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
389 && memcmp (&dataset
->resp
, dh
->data
,
390 dh
->allocsize
- offsetof (struct dataset
,
393 /* The data has not changed. We will just bump the
394 timeout value. Note that the new record has been
395 allocated on the stack and need not be freed. */
396 dh
->timeout
= dataset
->head
.timeout
;
397 dh
->ttl
= dataset
->head
.ttl
;
402 /* We have to create a new record. Just allocate
403 appropriate memory and copy it. */
405 = (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
407 if (__glibc_likely (newp
!= NULL
))
409 /* Adjust pointer into the memory block. */
410 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
412 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
416 /* Mark the old record as obsolete. */
422 /* We write the dataset before inserting it to the database
423 since while inserting this thread might block and so
424 would unnecessarily let the receiver wait. */
427 writeall (fd
, &dataset
->resp
, dataset
->head
.recsize
);
433 if (nss_next_action (nip
, status
[1]) == NSS_ACTION_RETURN
)
436 if (nip
[1].module
== NULL
)
442 /* No result found. Create a negative result record. */
443 if (he
!= NULL
&& rc4
== EAGAIN
)
445 /* If we have an old record available but cannot find one now
446 because the service is not available we keep the old record
447 and make sure it does not get removed. */
448 if (reload_count
!= UINT_MAX
&& dh
->nreloads
== reload_count
)
449 /* Do not reset the value if we never not reload the record. */
450 dh
->nreloads
= reload_count
- 1;
452 /* Reload with the same time-to-live value. */
453 timeout
= dh
->timeout
= time (NULL
) + dh
->ttl
;
457 /* We have no data. This means we send the standard reply for
459 total
= sizeof (notfound
);
462 TEMP_FAILURE_RETRY (send (fd
, ¬found
, total
, MSG_NOSIGNAL
));
464 /* If we have a transient error or cannot permanently store the
466 if (rc4
== EAGAIN
|| __builtin_expect (db
->negtimeout
== 0, 0))
468 /* Mark the old entry as obsolete. */
473 else if ((dataset
= mempool_alloc (db
, (sizeof (struct dataset
)
474 + req
->key_len
), 1)) != NULL
)
476 timeout
= datahead_init_neg (&dataset
->head
,
477 sizeof (struct dataset
) + req
->key_len
,
478 total
, db
->negtimeout
);
480 /* This is the reply. */
481 memcpy (&dataset
->resp
, ¬found
, total
);
483 /* Copy the key data. */
484 key_copy
= memcpy (dataset
->strdata
, key
, req
->key_len
);
489 __resolv_context_put (ctx
);
491 if (dataset
!= NULL
&& !alloca_used
)
493 /* If necessary, we also propagate the data to disk. */
497 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
498 msync ((void *) pval
,
499 ((uintptr_t) dataset
& pagesize_m1
) + total
+ req
->key_len
,
503 (void) cache_add (req
->type
, key_copy
, req
->key_len
, &dataset
->head
,
504 true, db
, uid
, he
== NULL
);
506 pthread_rwlock_unlock (&db
->lock
);
508 /* Mark the old entry as obsolete. */
513 scratch_buffer_free (&tmpbuf6
);
514 scratch_buffer_free (&tmpbuf4
);
515 scratch_buffer_free (&canonbuf
);
522 addhstai (struct database_dyn
*db
, int fd
, request_header
*req
, void *key
,
525 addhstaiX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
530 readdhstai (struct database_dyn
*db
, struct hashentry
*he
, struct datahead
*dh
)
538 return addhstaiX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);