]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/hstcache.c
* resolv/nss_dns/dns-host.c (gaih_getanswer_slice): Also log and
[thirdparty/glibc.git] / nscd / hstcache.c
CommitLineData
67479a70 1/* Cache handling for host lookup.
c52137d3 2 Copyright (C) 1998-2005, 2006, 2007, 2008 Free Software Foundation, Inc.
67479a70
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
43bc8ac6 6 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
67479a70 10
43bc8ac6 11 This program is distributed in the hope that it will be useful,
67479a70 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
67479a70 15
43bc8ac6
UD
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. */
67479a70 19
9caf4f1c 20#include <alloca.h>
67479a70
UD
21#include <assert.h>
22#include <errno.h>
23#include <error.h>
a95a08b4 24#include <libintl.h>
67479a70 25#include <netdb.h>
c7a9b6e2 26#include <stdbool.h>
67479a70
UD
27#include <stddef.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <time.h>
32#include <unistd.h>
33#include <arpa/inet.h>
9e56c380 34#include <arpa/nameser.h>
a95a08b4 35#include <sys/mman.h>
c7a9b6e2 36#include <stackinfo.h>
67479a70
UD
37
38#include "nscd.h"
39#include "dbg_log.h"
eac10791
UD
40#ifdef HAVE_SENDFILE
41# include <kernel-features.h>
42#endif
67479a70 43
67479a70
UD
44
45/* This is the standard reply in case the service is disabled. */
46static const hst_response_header disabled =
47{
c2e13112
RM
48 .version = NSCD_VERSION,
49 .found = -1,
50 .h_name_len = 0,
51 .h_aliases_cnt = 0,
52 .h_addrtype = -1,
53 .h_length = -1,
54 .h_addr_list_cnt = 0,
55 .error = NETDB_INTERNAL
67479a70
UD
56};
57
58/* This is the struct describing how to write this record. */
59const struct iovec hst_iov_disabled =
60{
c2e13112
RM
61 .iov_base = (void *) &disabled,
62 .iov_len = sizeof (disabled)
67479a70
UD
63};
64
65
66/* This is the standard reply in case we haven't found the dataset. */
67static const hst_response_header notfound =
68{
c2e13112
RM
69 .version = NSCD_VERSION,
70 .found = 0,
71 .h_name_len = 0,
72 .h_aliases_cnt = 0,
73 .h_addrtype = -1,
74 .h_length = -1,
75 .h_addr_list_cnt = 0,
76 .error = HOST_NOT_FOUND
67479a70
UD
77};
78
67479a70 79
67479a70 80static void
a95a08b4 81cache_addhst (struct database_dyn *db, int fd, request_header *req,
c207f23b 82 const void *key, struct hostent *hst, uid_t owner,
384ca551
UD
83 struct hashentry *he, struct datahead *dh, int errval,
84 int32_t ttl)
67479a70
UD
85{
86 ssize_t total;
87 ssize_t written;
88 time_t t = time (NULL);
89
a95a08b4
UD
90 /* We allocate all data in one memory block: the iov vector,
91 the response header and the dataset itself. */
92 struct dataset
93 {
94 struct datahead head;
95 hst_response_header resp;
96 char strdata[0];
97 } *dataset;
98
99 assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
100
67479a70
UD
101 if (hst == NULL)
102 {
a95a08b4
UD
103 if (he != NULL && errval == EAGAIN)
104 {
105 /* If we have an old record available but cannot find one
106 now because the service is not available we keep the old
107 record and make sure it does not get removed. */
108 if (reload_count != UINT_MAX)
109 /* Do not reset the value if we never not reload the record. */
110 dh->nreloads = reload_count - 1;
111
112 written = total = 0;
113 }
114 else
115 {
116 /* We have no data. This means we send the standard reply for this
117 case. */
118 written = total = sizeof (notfound);
67479a70 119
a95a08b4 120 if (fd != -1)
2c210d1e
UD
121 written = TEMP_FAILURE_RETRY (send (fd, &notfound, total,
122 MSG_NOSIGNAL));
67479a70 123
c52137d3
UD
124 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len,
125 IDX_result_data);
a95a08b4
UD
126 /* If we cannot permanently store the result, so be it. */
127 if (dataset != NULL)
128 {
129 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
130 dataset->head.recsize = total;
131 dataset->head.notfound = true;
132 dataset->head.nreloads = 0;
133 dataset->head.usable = true;
67479a70 134
a95a08b4 135 /* Compute the timeout time. */
384ca551
UD
136 dataset->head.timeout = t + (ttl == INT32_MAX
137 ? db->negtimeout : ttl);
67479a70 138
a95a08b4
UD
139 /* This is the reply. */
140 memcpy (&dataset->resp, &notfound, total);
67479a70 141
a95a08b4
UD
142 /* Copy the key data. */
143 memcpy (dataset->strdata, key, req->key_len);
67479a70 144
8c89236f
UD
145 /* If necessary, we also propagate the data to disk. */
146 if (db->persistent)
147 {
148 // XXX async OK?
149 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
150 msync ((void *) pval,
151 ((uintptr_t) dataset & pagesize_m1)
152 + sizeof (struct dataset) + req->key_len, MS_ASYNC);
153 }
154
a95a08b4
UD
155 /* Now get the lock to safely insert the records. */
156 pthread_rwlock_rdlock (&db->lock);
157
7e71e55f 158 (void) cache_add (req->type, &dataset->strdata, req->key_len,
528741cb 159 &dataset->head, true, db, owner, he == NULL);
a95a08b4
UD
160
161 pthread_rwlock_unlock (&db->lock);
162
163 /* Mark the old entry as obsolete. */
164 if (dh != NULL)
165 dh->usable = false;
166 }
167 else
168 ++db->head->addfailed;
99bb9f42 169 }
67479a70
UD
170 }
171 else
172 {
173 /* Determine the I/O structure. */
67479a70
UD
174 size_t h_name_len = strlen (hst->h_name) + 1;
175 size_t h_aliases_cnt;
3107c0c5 176 uint32_t *h_aliases_len;
67479a70
UD
177 size_t h_addr_list_cnt;
178 int addr_list_type;
179 char *addresses;
180 char *aliases;
181 char *key_copy = NULL;
182 char *cp;
183 size_t cnt;
184
185 /* Determine the number of aliases. */
186 h_aliases_cnt = 0;
187 for (cnt = 0; hst->h_aliases[cnt] != NULL; ++cnt)
188 ++h_aliases_cnt;
189 /* Determine the length of all aliases. */
3107c0c5 190 h_aliases_len = (uint32_t *) alloca (h_aliases_cnt * sizeof (uint32_t));
67479a70
UD
191 total = 0;
192 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
193 {
194 h_aliases_len[cnt] = strlen (hst->h_aliases[cnt]) + 1;
195 total += h_aliases_len[cnt];
196 }
197
198 /* Determine the number of addresses. */
199 h_addr_list_cnt = 0;
1ce7d80d 200 while (hst->h_addr_list[h_addr_list_cnt] != NULL)
67479a70
UD
201 ++h_addr_list_cnt;
202
a95a08b4
UD
203 if (h_addr_list_cnt == 0)
204 /* Invalid entry. */
205 return;
206
207 total += (sizeof (struct dataset)
67479a70 208 + h_name_len
3107c0c5 209 + h_aliases_cnt * sizeof (uint32_t)
488fb3c7 210 + h_addr_list_cnt * hst->h_length);
a95a08b4 211 written = total;
67479a70 212
a95a08b4
UD
213 /* If we refill the cache, first assume the reconrd did not
214 change. Allocate memory on the cache since it is likely
215 discarded anyway. If it turns out to be necessary to have a
216 new record we can still allocate real memory. */
217 bool alloca_used = false;
218 dataset = NULL;
67479a70 219
a95a08b4
UD
220 /* If the record contains more than one IP address (used for
221 load balancing etc) don't cache the entry. This is something
222 the current cache handling cannot handle and it is more than
223 questionable whether it is worthwhile complicating the cache
224 handling just for handling such a special case. */
a30d41c1 225 if (he == NULL && h_addr_list_cnt == 1)
a95a08b4
UD
226 {
227 dataset = (struct dataset *) mempool_alloc (db,
c52137d3
UD
228 total + req->key_len,
229 IDX_result_data);
a95a08b4
UD
230 if (dataset == NULL)
231 ++db->head->addfailed;
232 }
67479a70 233
a95a08b4
UD
234 if (dataset == NULL)
235 {
236 /* We cannot permanently add the result in the moment. But
237 we can provide the result as is. Store the data in some
238 temporary memory. */
239 dataset = (struct dataset *) alloca (total + req->key_len);
240
241 /* We cannot add this record to the permanent database. */
242 alloca_used = true;
243 }
244
245 dataset->head.allocsize = total + req->key_len;
246 dataset->head.recsize = total - offsetof (struct dataset, resp);
247 dataset->head.notfound = false;
248 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
249 dataset->head.usable = true;
250
251 /* Compute the timeout time. */
384ca551 252 dataset->head.timeout = t + (ttl == INT32_MAX ? db->postimeout : ttl);
a95a08b4
UD
253
254 dataset->resp.version = NSCD_VERSION;
255 dataset->resp.found = 1;
256 dataset->resp.h_name_len = h_name_len;
257 dataset->resp.h_aliases_cnt = h_aliases_cnt;
258 dataset->resp.h_addrtype = hst->h_addrtype;
259 dataset->resp.h_length = hst->h_length;
260 dataset->resp.h_addr_list_cnt = h_addr_list_cnt;
261 dataset->resp.error = NETDB_SUCCESS;
262
263 cp = dataset->strdata;
67479a70
UD
264
265 cp = mempcpy (cp, hst->h_name, h_name_len);
3107c0c5 266 cp = mempcpy (cp, h_aliases_len, h_aliases_cnt * sizeof (uint32_t));
67479a70
UD
267
268 /* The normal addresses first. */
269 addresses = cp;
270 for (cnt = 0; cnt < h_addr_list_cnt; ++cnt)
271 cp = mempcpy (cp, hst->h_addr_list[cnt], hst->h_length);
272
67479a70
UD
273 /* Then the aliases. */
274 aliases = cp;
275 for (cnt = 0; cnt < h_aliases_cnt; ++cnt)
276 cp = mempcpy (cp, hst->h_aliases[cnt], h_aliases_len[cnt]);
277
a95a08b4
UD
278 assert (cp
279 == dataset->strdata + total - offsetof (struct dataset,
280 strdata));
67479a70
UD
281
282 /* If we are adding a GETHOSTBYNAME{,v6} entry we must be prepared
283 that the answer we get from the NSS does not contain the key
284 itself. This is the case if the resolver is used and the name
285 is extended by the domainnames from /etc/resolv.conf. Therefore
286 we explicitly add the name here. */
c207f23b 287 key_copy = memcpy (cp, key, req->key_len);
67479a70 288
a95a08b4
UD
289 /* Now we can determine whether on refill we have to create a new
290 record or not. */
291 if (he != NULL)
292 {
293 assert (fd == -1);
67479a70 294
a95a08b4
UD
295 if (total + req->key_len == dh->allocsize
296 && total - offsetof (struct dataset, resp) == dh->recsize
297 && memcmp (&dataset->resp, dh->data,
298 dh->allocsize - offsetof (struct dataset, resp)) == 0)
299 {
8c89236f 300 /* The data has not changed. We will just bump the
a95a08b4
UD
301 timeout value. Note that the new record has been
302 allocated on the stack and need not be freed. */
c44d3bdf 303 assert (h_addr_list_cnt == 1);
a95a08b4
UD
304 dh->timeout = dataset->head.timeout;
305 ++dh->nreloads;
306 }
307 else
308 {
c44d3bdf 309 if (h_addr_list_cnt == 1)
a95a08b4 310 {
c44d3bdf
UD
311 /* We have to create a new record. Just allocate
312 appropriate memory and copy it. */
313 struct dataset *newp
314 = (struct dataset *) mempool_alloc (db,
c52137d3
UD
315 total + req->key_len,
316 IDX_result_data);
c44d3bdf
UD
317 if (newp != NULL)
318 {
319 /* Adjust pointers into the memory block. */
320 addresses = (char *) newp + (addresses
321 - (char *) dataset);
322 aliases = (char *) newp + (aliases - (char *) dataset);
323 assert (key_copy != NULL);
324 key_copy = (char *) newp + (key_copy - (char *) dataset);
325
326 dataset = memcpy (newp, dataset, total + req->key_len);
327 alloca_used = false;
328 }
e8667ddc
UD
329 else
330 ++db->head->addfailed;
a95a08b4
UD
331 }
332
333 /* Mark the old record as obsolete. */
334 dh->usable = false;
335 }
336 }
337 else
490998a5 338 {
a95a08b4
UD
339 /* We write the dataset before inserting it to the database
340 since while inserting this thread might block and so would
341 unnecessarily keep the receiver waiting. */
342 assert (fd != -1);
343
eac10791 344#ifdef HAVE_SENDFILE
74158740 345 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
eac10791
UD
346 {
347 assert (db->wr_fd != -1);
348 assert ((char *) &dataset->resp > (char *) db->data);
349 assert ((char *) &dataset->resp - (char *) db->head
350 + total
351 <= (sizeof (struct database_pers_head)
352 + db->head->module * sizeof (ref_t)
353 + db->head->data_size));
bd547139
UD
354 written = sendfileall (fd, db->wr_fd,
355 (char *) &dataset->resp
356 - (char *) db->head, total);
eac10791
UD
357# ifndef __ASSUME_SENDFILE
358 if (written == -1 && errno == ENOSYS)
359 goto use_write;
360# endif
361 }
362 else
363# ifndef __ASSUME_SENDFILE
364 use_write:
365# endif
366#endif
367 written = writeall (fd, &dataset->resp, total);
490998a5
UD
368 }
369
a95a08b4
UD
370 /* Add the record to the database. But only if it has not been
371 stored on the stack.
67479a70 372
a95a08b4
UD
373 If the record contains more than one IP address (used for
374 load balancing etc) don't cache the entry. This is something
375 the current cache handling cannot handle and it is more than
376 questionable whether it is worthwhile complicating the cache
377 handling just for handling such a special case. */
378 if (! alloca_used)
67479a70 379 {
a95a08b4
UD
380 /* If necessary, we also propagate the data to disk. */
381 if (db->persistent)
3418007e
UD
382 {
383 // XXX async OK?
384 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
385 msync ((void *) pval,
386 ((uintptr_t) dataset & pagesize_m1)
387 + total + req->key_len, MS_ASYNC);
388 }
a95a08b4
UD
389
390 addr_list_type = (hst->h_length == NS_INADDRSZ
391 ? GETHOSTBYADDR : GETHOSTBYADDRv6);
392
393 /* Now get the lock to safely insert the records. */
394 pthread_rwlock_rdlock (&db->lock);
395
396 /* NB: the following code is really complicated. It has
397 seemlingly duplicated code paths which do the same. The
398 problem is that we always must add the hash table entry
399 with the FIRST flag set first. Otherwise we get dangling
400 pointers in case memory allocation fails. */
c207f23b 401 assert (hst->h_addr_list[1] == NULL);
a95a08b4 402
a95a08b4
UD
403 /* Avoid adding names if more than one address is available. See
404 above for more info. */
c207f23b
UD
405 assert (req->type == GETHOSTBYNAME
406 || req->type == GETHOSTBYNAMEv6
407 || req->type == GETHOSTBYADDR
408 || req->type == GETHOSTBYADDRv6);
a95a08b4 409
7e71e55f 410 (void) cache_add (req->type, key_copy, req->key_len,
528741cb 411 &dataset->head, true, db, owner, he == NULL);
a95a08b4 412
a95a08b4 413 pthread_rwlock_unlock (&db->lock);
67479a70 414 }
67479a70
UD
415 }
416
23700036 417 if (__builtin_expect (written != total, 0) && debug_level > 0)
67479a70
UD
418 {
419 char buf[256];
420 dbg_log (_("short write in %s: %s"), __FUNCTION__,
421 strerror_r (errno, buf, sizeof (buf)));
422 }
423}
424
425
a95a08b4
UD
426static int
427lookup (int type, void *key, struct hostent *resultbufp, char *buffer,
384ca551 428 size_t buflen, struct hostent **hst, int32_t *ttlp)
a95a08b4
UD
429{
430 if (type == GETHOSTBYNAME)
384ca551
UD
431 return __gethostbyname3_r (key, AF_INET, resultbufp, buffer, buflen, hst,
432 &h_errno, ttlp, NULL);
8c89236f 433 if (type == GETHOSTBYNAMEv6)
384ca551
UD
434 return __gethostbyname3_r (key, AF_INET6, resultbufp, buffer, buflen, hst,
435 &h_errno, ttlp, NULL);
8c89236f 436 if (type == GETHOSTBYADDR)
384ca551
UD
437 return __gethostbyaddr2_r (key, NS_INADDRSZ, AF_INET, resultbufp, buffer,
438 buflen, hst, &h_errno, ttlp);
439 return __gethostbyaddr2_r (key, NS_IN6ADDRSZ, AF_INET6, resultbufp, buffer,
440 buflen, hst, &h_errno, ttlp);
a95a08b4
UD
441}
442
443
444static void
445addhstbyX (struct database_dyn *db, int fd, request_header *req,
446 void *key, uid_t uid, struct hashentry *he, struct datahead *dh)
67479a70
UD
447{
448 /* Search for the entry matching the key. Please note that we don't
449 look again in the table whether the dataset is now available. We
450 simply insert it. It does not matter if it is in there twice. The
451 pruning function only will look at the timestamp. */
c7a9b6e2
UD
452 int buflen = 1024;
453 char *buffer = (char *) alloca (buflen);
67479a70
UD
454 struct hostent resultbuf;
455 struct hostent *hst;
c7a9b6e2 456 bool use_malloc = false;
a95a08b4 457 int errval = 0;
384ca551 458 int32_t ttl = INT32_MAX;
67479a70 459
c7a9b6e2 460 if (__builtin_expect (debug_level > 0, 0))
a95a08b4 461 {
c207f23b
UD
462 const char *str;
463 char buf[INET6_ADDRSTRLEN + 1];
464 if (req->type == GETHOSTBYNAME || req->type == GETHOSTBYNAMEv6)
465 str = key;
466 else
467 str = inet_ntop (req->type == GETHOSTBYADDR ? AF_INET : AF_INET6,
468 key, buf, sizeof (buf));
469
a95a08b4 470 if (he == NULL)
c207f23b 471 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) str);
a95a08b4 472 else
c207f23b 473 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) str);
a95a08b4 474 }
67479a70 475
384ca551 476 while (lookup (req->type, key, &resultbuf, buffer, buflen, &hst, &ttl) != 0
67479a70 477 && h_errno == NETDB_INTERNAL
a95a08b4 478 && (errval = errno) == ERANGE)
67479a70
UD
479 {
480 errno = 0;
c7a9b6e2
UD
481
482 if (__builtin_expect (buflen > 32768, 0))
483 {
b21fa963 484 char *old_buffer = buffer;
4379b403 485 buflen *= 2;
c7a9b6e2
UD
486 buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
487 if (buffer == NULL)
488 {
489 /* We ran out of memory. We cannot do anything but
490 sending a negative response. In reality this should
491 never happen. */
492 hst = NULL;
493 buffer = old_buffer;
a95a08b4
UD
494
495 /* We set the error to indicate this is (possibly) a
496 temporary error and that it does not mean the entry
497 is not available at all. */
498 errval = EAGAIN;
c7a9b6e2
UD
499 break;
500 }
501 use_malloc = true;
502 }
503 else
9caf4f1c
UD
504 /* Allocate a new buffer on the stack. If possible combine it
505 with the previously allocated buffer. */
4379b403 506 buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
67479a70
UD
507 }
508
c207f23b 509 cache_addhst (db, fd, req, key, hst, uid, he, dh,
384ca551 510 h_errno == TRY_AGAIN ? errval : 0, ttl);
c7a9b6e2
UD
511
512 if (use_malloc)
513 free (buffer);
67479a70
UD
514}
515
516
517void
a95a08b4 518addhstbyname (struct database_dyn *db, int fd, request_header *req,
a1c542bf 519 void *key, uid_t uid)
67479a70 520{
a95a08b4
UD
521 addhstbyX (db, fd, req, key, uid, NULL, NULL);
522}
67479a70 523
67479a70 524
a95a08b4
UD
525void
526readdhstbyname (struct database_dyn *db, struct hashentry *he,
527 struct datahead *dh)
528{
529 request_header req =
a1c542bf 530 {
a95a08b4
UD
531 .type = GETHOSTBYNAME,
532 .key_len = he->len
533 };
a1c542bf 534
a95a08b4
UD
535 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
536}
c7a9b6e2 537
67479a70 538
a95a08b4
UD
539void
540addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
541 void *key, uid_t uid)
542{
543 addhstbyX (db, fd, req, key, uid, NULL, NULL);
544}
a1c542bf 545
c7a9b6e2 546
a95a08b4
UD
547void
548readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
549 struct datahead *dh)
550{
551 request_header req =
552 {
553 .type = GETHOSTBYADDR,
554 .key_len = he->len
555 };
556
557 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
67479a70
UD
558}
559
560
561void
a95a08b4 562addhstbynamev6 (struct database_dyn *db, int fd, request_header *req,
a1c542bf 563 void *key, uid_t uid)
67479a70 564{
a95a08b4
UD
565 addhstbyX (db, fd, req, key, uid, NULL, NULL);
566}
67479a70 567
a1c542bf 568
a95a08b4
UD
569void
570readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
571 struct datahead *dh)
572{
573 request_header req =
67479a70 574 {
a95a08b4
UD
575 .type = GETHOSTBYNAMEv6,
576 .key_len = he->len
577 };
c7a9b6e2 578
a95a08b4 579 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
67479a70
UD
580}
581
582
583void
a95a08b4 584addhstbyaddrv6 (struct database_dyn *db, int fd, request_header *req,
a1c542bf 585 void *key, uid_t uid)
67479a70 586{
a95a08b4
UD
587 addhstbyX (db, fd, req, key, uid, NULL, NULL);
588}
67479a70 589
67479a70 590
a95a08b4
UD
591void
592readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
593 struct datahead *dh)
594{
595 request_header req =
67479a70 596 {
a95a08b4
UD
597 .type = GETHOSTBYADDRv6,
598 .key_len = he->len
599 };
c7a9b6e2 600
a95a08b4 601 addhstbyX (db, -1, &req, db->data + he->key, he->owner, he, dh);
67479a70 602}