1 /* Cache handling for host lookup.
2 Copyright (C) 1998-2022 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/>. */
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
35 #include <stackinfo.h>
36 #include <scratch_buffer.h>
42 /* This is the standard reply in case the service is disabled. */
43 static const hst_response_header disabled
=
45 .version
= NSCD_VERSION
,
52 .error
= NETDB_INTERNAL
55 /* This is the struct describing how to write this record. */
56 const struct iovec hst_iov_disabled
=
58 .iov_base
= (void *) &disabled
,
59 .iov_len
= sizeof (disabled
)
63 /* This is the standard reply in case we haven't found the dataset. */
64 static const hst_response_header notfound
=
66 .version
= NSCD_VERSION
,
73 .error
= HOST_NOT_FOUND
77 /* This is the standard reply in case there are temporary problems. */
78 static const hst_response_header tryagain
=
80 .version
= NSCD_VERSION
,
92 cache_addhst (struct database_dyn
*db
, int fd
, request_header
*req
,
93 const void *key
, struct hostent
*hst
, uid_t owner
,
94 struct hashentry
*const he
, struct datahead
*dh
, int errval
,
97 bool all_written
= true;
98 time_t t
= time (NULL
);
100 /* We allocate all data in one memory block: the iov vector,
101 the response header and the dataset itself. */
104 struct datahead head
;
105 hst_response_header resp
;
109 assert (offsetof (struct dataset
, resp
) == offsetof (struct datahead
, data
));
111 time_t timeout
= MAX_TIMEOUT_VALUE
;
114 if (he
!= NULL
&& errval
== EAGAIN
)
116 /* If we have an old record available but cannot find one
117 now because the service is not available we keep the old
118 record and make sure it does not get removed. */
119 if (reload_count
!= UINT_MAX
)
120 /* Do not reset the value if we never not reload the record. */
121 dh
->nreloads
= reload_count
- 1;
123 /* Reload with the same time-to-live value. */
124 timeout
= dh
->timeout
= t
+ dh
->ttl
;
128 /* We have no data. This means we send the standard reply for this
129 case. Possibly this is only temporary. */
130 ssize_t total
= sizeof (notfound
);
131 assert (sizeof (notfound
) == sizeof (tryagain
));
133 const hst_response_header
*resp
= (errval
== EAGAIN
134 ? &tryagain
: ¬found
);
137 && TEMP_FAILURE_RETRY (send (fd
, resp
, total
,
138 MSG_NOSIGNAL
)) != total
)
141 /* If we have a transient error or cannot permanently store
142 the result, so be it. */
143 if (errval
== EAGAIN
|| __builtin_expect (db
->negtimeout
== 0, 0))
145 /* Mark the old entry as obsolete. */
149 else if ((dataset
= mempool_alloc (db
, (sizeof (struct dataset
)
150 + req
->key_len
), 1)) != NULL
)
152 timeout
= datahead_init_neg (&dataset
->head
,
153 (sizeof (struct dataset
)
154 + req
->key_len
), total
,
156 ? db
->negtimeout
: ttl
));
158 /* This is the reply. */
159 memcpy (&dataset
->resp
, resp
, total
);
161 /* Copy the key data. */
162 memcpy (dataset
->strdata
, key
, req
->key_len
);
164 /* If necessary, we also propagate the data to disk. */
168 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
169 msync ((void *) pval
,
170 ((uintptr_t) dataset
& pagesize_m1
)
171 + sizeof (struct dataset
) + req
->key_len
, MS_ASYNC
);
174 (void) cache_add (req
->type
, &dataset
->strdata
, req
->key_len
,
175 &dataset
->head
, true, db
, owner
, he
== NULL
);
177 pthread_rwlock_unlock (&db
->lock
);
179 /* Mark the old entry as obsolete. */
187 /* Determine the I/O structure. */
188 size_t h_name_len
= strlen (hst
->h_name
) + 1;
189 size_t h_aliases_cnt
;
190 uint32_t *h_aliases_len
;
191 size_t h_addr_list_cnt
;
194 char *key_copy
= NULL
;
199 /* Determine the number of aliases. */
201 for (cnt
= 0; hst
->h_aliases
[cnt
] != NULL
; ++cnt
)
203 /* Determine the length of all aliases. */
204 h_aliases_len
= (uint32_t *) alloca (h_aliases_cnt
* sizeof (uint32_t));
206 for (cnt
= 0; cnt
< h_aliases_cnt
; ++cnt
)
208 h_aliases_len
[cnt
] = strlen (hst
->h_aliases
[cnt
]) + 1;
209 total
+= h_aliases_len
[cnt
];
212 /* Determine the number of addresses. */
214 while (hst
->h_addr_list
[h_addr_list_cnt
] != NULL
)
217 if (h_addr_list_cnt
== 0)
219 return MAX_TIMEOUT_VALUE
;
221 total
+= (sizeof (struct dataset
)
223 + h_aliases_cnt
* sizeof (uint32_t)
224 + h_addr_list_cnt
* hst
->h_length
);
226 /* If we refill the cache, first assume the reconrd did not
227 change. Allocate memory on the cache since it is likely
228 discarded anyway. If it turns out to be necessary to have a
229 new record we can still allocate real memory. */
230 bool alloca_used
= false;
233 /* If the record contains more than one IP address (used for
234 load balancing etc) don't cache the entry. This is something
235 the current cache handling cannot handle and it is more than
236 questionable whether it is worthwhile complicating the cache
237 handling just for handling such a special case. */
238 if (he
== NULL
&& h_addr_list_cnt
== 1)
239 dataset
= (struct dataset
*) mempool_alloc (db
, total
+ req
->key_len
,
244 /* We cannot permanently add the result in the moment. But
245 we can provide the result as is. Store the data in some
247 dataset
= (struct dataset
*) alloca (total
+ req
->key_len
);
249 /* We cannot add this record to the permanent database. */
253 timeout
= datahead_init_pos (&dataset
->head
, total
+ req
->key_len
,
254 total
- offsetof (struct dataset
, resp
),
255 he
== NULL
? 0 : dh
->nreloads
+ 1,
256 ttl
== INT32_MAX
? db
->postimeout
: ttl
);
258 dataset
->resp
.version
= NSCD_VERSION
;
259 dataset
->resp
.found
= 1;
260 dataset
->resp
.h_name_len
= h_name_len
;
261 dataset
->resp
.h_aliases_cnt
= h_aliases_cnt
;
262 dataset
->resp
.h_addrtype
= hst
->h_addrtype
;
263 dataset
->resp
.h_length
= hst
->h_length
;
264 dataset
->resp
.h_addr_list_cnt
= h_addr_list_cnt
;
265 dataset
->resp
.error
= NETDB_SUCCESS
;
267 /* Make sure there is no gap. */
268 assert ((char *) (&dataset
->resp
.error
+ 1) == dataset
->strdata
);
270 cp
= dataset
->strdata
;
272 cp
= mempcpy (cp
, hst
->h_name
, h_name_len
);
273 cp
= mempcpy (cp
, h_aliases_len
, h_aliases_cnt
* sizeof (uint32_t));
275 /* The normal addresses first. */
277 for (cnt
= 0; cnt
< h_addr_list_cnt
; ++cnt
)
278 cp
= mempcpy (cp
, hst
->h_addr_list
[cnt
], hst
->h_length
);
280 /* Then the aliases. */
282 for (cnt
= 0; cnt
< h_aliases_cnt
; ++cnt
)
283 cp
= mempcpy (cp
, hst
->h_aliases
[cnt
], h_aliases_len
[cnt
]);
286 == dataset
->strdata
+ total
- offsetof (struct dataset
,
289 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
290 that the answer we get from the NSS does not contain the key
291 itself. This is the case if the resolver is used and the name
292 is extended by the domainnames from /etc/resolv.conf. Therefore
293 we explicitly add the name here. */
294 key_copy
= memcpy (cp
, key
, req
->key_len
);
296 assert ((char *) &dataset
->resp
+ dataset
->head
.recsize
== cp
);
298 /* Now we can determine whether on refill we have to create a new
304 if (total
+ req
->key_len
== dh
->allocsize
305 && total
- offsetof (struct dataset
, resp
) == dh
->recsize
306 && memcmp (&dataset
->resp
, dh
->data
,
307 dh
->allocsize
- offsetof (struct dataset
, resp
)) == 0)
309 /* The data has not changed. We will just bump the
310 timeout value. Note that the new record has been
311 allocated on the stack and need not be freed. */
312 assert (h_addr_list_cnt
== 1);
313 dh
->ttl
= dataset
->head
.ttl
;
314 dh
->timeout
= dataset
->head
.timeout
;
319 if (h_addr_list_cnt
== 1)
321 /* We have to create a new record. Just allocate
322 appropriate memory and copy it. */
324 = (struct dataset
*) mempool_alloc (db
,
325 total
+ req
->key_len
,
329 /* Adjust pointers into the memory block. */
330 addresses
= (char *) newp
+ (addresses
332 aliases
= (char *) newp
+ (aliases
- (char *) dataset
);
333 assert (key_copy
!= NULL
);
334 key_copy
= (char *) newp
+ (key_copy
- (char *) dataset
);
336 dataset
= memcpy (newp
, dataset
, total
+ req
->key_len
);
341 /* Mark the old record as obsolete. */
347 /* We write the dataset before inserting it to the database
348 since while inserting this thread might block and so would
349 unnecessarily keep the receiver waiting. */
352 if (writeall (fd
, &dataset
->resp
, dataset
->head
.recsize
)
353 != dataset
->head
.recsize
)
357 /* Add the record to the database. But only if it has not been
360 If the record contains more than one IP address (used for
361 load balancing etc) don't cache the entry. This is something
362 the current cache handling cannot handle and it is more than
363 questionable whether it is worthwhile complicating the cache
364 handling just for handling such a special case. */
367 /* If necessary, we also propagate the data to disk. */
371 uintptr_t pval
= (uintptr_t) dataset
& ~pagesize_m1
;
372 msync ((void *) pval
,
373 ((uintptr_t) dataset
& pagesize_m1
)
374 + total
+ req
->key_len
, MS_ASYNC
);
377 /* NB: the following code is really complicated. It has
378 seemlingly duplicated code paths which do the same. The
379 problem is that we always must add the hash table entry
380 with the FIRST flag set first. Otherwise we get dangling
381 pointers in case memory allocation fails. */
382 assert (hst
->h_addr_list
[1] == NULL
);
384 /* Avoid adding names if more than one address is available. See
385 above for more info. */
386 assert (req
->type
== GETHOSTBYNAME
387 || req
->type
== GETHOSTBYNAMEv6
388 || req
->type
== GETHOSTBYADDR
389 || req
->type
== GETHOSTBYADDRv6
);
391 (void) cache_add (req
->type
, key_copy
, req
->key_len
,
392 &dataset
->head
, true, db
, owner
, he
== NULL
);
394 pthread_rwlock_unlock (&db
->lock
);
398 if (__builtin_expect (!all_written
, 0) && debug_level
> 0)
401 dbg_log (_("short write in %s: %s"), __FUNCTION__
,
402 strerror_r (errno
, buf
, sizeof (buf
)));
410 lookup (int type
, void *key
, struct hostent
*resultbufp
, char *buffer
,
411 size_t buflen
, struct hostent
**hst
, int32_t *ttlp
)
413 if (type
== GETHOSTBYNAME
)
414 return __gethostbyname3_r (key
, AF_INET
, resultbufp
, buffer
, buflen
, hst
,
415 &h_errno
, ttlp
, NULL
);
416 if (type
== GETHOSTBYNAMEv6
)
417 return __gethostbyname3_r (key
, AF_INET6
, resultbufp
, buffer
, buflen
, hst
,
418 &h_errno
, ttlp
, NULL
);
419 if (type
== GETHOSTBYADDR
)
420 return __gethostbyaddr2_r (key
, NS_INADDRSZ
, AF_INET
, resultbufp
, buffer
,
421 buflen
, hst
, &h_errno
, ttlp
);
422 return __gethostbyaddr2_r (key
, NS_IN6ADDRSZ
, AF_INET6
, resultbufp
, buffer
,
423 buflen
, hst
, &h_errno
, ttlp
);
428 addhstbyX (struct database_dyn
*db
, int fd
, request_header
*req
,
429 void *key
, uid_t uid
, struct hashentry
*he
, struct datahead
*dh
)
431 /* Search for the entry matching the key. Please note that we don't
432 look again in the table whether the dataset is now available. We
433 simply insert it. It does not matter if it is in there twice. The
434 pruning function only will look at the timestamp. */
435 struct hostent resultbuf
;
438 int32_t ttl
= INT32_MAX
;
440 if (__glibc_unlikely (debug_level
> 0))
443 char buf
[INET6_ADDRSTRLEN
+ 1];
444 if (req
->type
== GETHOSTBYNAME
|| req
->type
== GETHOSTBYNAMEv6
)
447 str
= inet_ntop (req
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
448 key
, buf
, sizeof (buf
));
451 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str
);
453 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str
);
456 struct scratch_buffer tmpbuf
;
457 scratch_buffer_init (&tmpbuf
);
459 while (lookup (req
->type
, key
, &resultbuf
,
460 tmpbuf
.data
, tmpbuf
.length
, &hst
, &ttl
) != 0
461 && h_errno
== NETDB_INTERNAL
462 && (errval
= errno
) == ERANGE
)
463 if (!scratch_buffer_grow (&tmpbuf
))
465 /* We ran out of memory. We cannot do anything but sending a
466 negative response. In reality this should never
469 /* We set the error to indicate this is (possibly) a temporary
470 error and that it does not mean the entry is not
477 time_t timeout
= cache_addhst (db
, fd
, req
, key
, hst
, uid
, he
, dh
,
478 h_errno
== TRY_AGAIN
? errval
: 0, ttl
);
479 scratch_buffer_free (&tmpbuf
);
485 addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
486 void *key
, uid_t uid
)
488 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
493 readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
498 .type
= GETHOSTBYNAME
,
502 return addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
507 addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
508 void *key
, uid_t uid
)
510 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
515 readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
520 .type
= GETHOSTBYADDR
,
524 return addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
529 addhstbynamev6 (struct database_dyn
*db
, int fd
, request_header
*req
,
530 void *key
, uid_t uid
)
532 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
537 readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
542 .type
= GETHOSTBYNAMEv6
,
546 return addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);
551 addhstbyaddrv6 (struct database_dyn
*db
, int fd
, request_header
*req
,
552 void *key
, uid_t uid
)
554 addhstbyX (db
, fd
, req
, key
, uid
, NULL
, NULL
);
559 readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
564 .type
= GETHOSTBYADDRv6
,
568 return addhstbyX (db
, -1, &req
, db
->data
+ he
->key
, he
->owner
, he
, dh
);