]> git.ipfire.org Git - thirdparty/glibc.git/blame - nscd/aicache.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nscd / aicache.c
CommitLineData
d19687d6 1/* Cache handling for host lookup.
bfff8b1b 2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
d19687d6
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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.
d19687d6 10
43bc8ac6 11 This program is distributed in the hope that it will be useful,
d19687d6 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.
d19687d6 15
43bc8ac6 16 You should have received a copy of the GNU General Public License
59ba27a6 17 along with this program; if not, see <http://www.gnu.org/licenses/>. */
d19687d6
UD
18
19#include <assert.h>
20#include <errno.h>
21#include <libintl.h>
22#include <netdb.h>
1eb946b9 23#include <nss.h>
d19687d6
UD
24#include <string.h>
25#include <time.h>
26#include <unistd.h>
27#include <sys/mman.h>
b76e0659 28#include <resolv/resolv-internal.h>
595aba70 29#include <resolv/res_hconf.h>
eac10791
UD
30
31#include "dbg_log.h"
32#include "nscd.h"
33#ifdef HAVE_SENDFILE
34# include <kernel-features.h>
35#endif
d19687d6
UD
36
37
1eb946b9
UD
38typedef enum nss_status (*nss_gethostbyname4_r)
39 (const char *name, struct gaih_addrtuple **pat,
40 char *buffer, size_t buflen, int *errnop,
41 int *h_errnop, int32_t *ttlp);
d1fe1f22 42typedef enum nss_status (*nss_gethostbyname3_r)
d19687d6
UD
43 (const char *name, int af, struct hostent *host,
44 char *buffer, size_t buflen, int *errnop,
d1fe1f22 45 int *h_errnop, int32_t *, char **);
d19687d6
UD
46typedef enum nss_status (*nss_getcanonname_r)
47 (const char *name, char *buffer, size_t buflen, char **result,
48 int *errnop, int *h_errnop);
49
50
51static const ai_response_header notfound =
52{
53 .version = NSCD_VERSION,
54 .found = 0,
55 .naddrs = 0,
56 .addrslen = 0,
57 .canonlen = 0,
58 .error = 0
59};
60
61
a4c7ea7b 62static time_t
d19687d6 63addhstaiX (struct database_dyn *db, int fd, request_header *req,
20e498bd
UD
64 void *key, uid_t uid, struct hashentry *const he,
65 struct datahead *dh)
d19687d6
UD
66{
67 /* Search for the entry matching the key. Please note that we don't
68 look again in the table whether the dataset is now available. We
69 simply insert it. It does not matter if it is in there twice. The
70 pruning function only will look at the timestamp. */
d19687d6
UD
71
72 /* We allocate all data in one memory block: the iov vector,
73 the response header and the dataset itself. */
74 struct dataset
75 {
76 struct datahead head;
77 ai_response_header resp;
78 char strdata[0];
79 } *dataset = NULL;
80
a1ffb40e 81 if (__glibc_unlikely (debug_level > 0))
d19687d6
UD
82 {
83 if (he == NULL)
84 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
85 else
86 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key);
87 }
88
d19687d6 89 static service_user *hosts_database;
b2179107 90 service_user *nip;
d19687d6
UD
91 int no_more;
92 int rc6 = 0;
93 int rc4 = 0;
94 int herrno = 0;
95
b2179107 96 if (hosts_database == NULL)
d19687d6 97 no_more = __nss_database_lookup ("hosts", NULL,
b2179107
AS
98 "dns [!UNAVAIL=return] files",
99 &hosts_database);
100 else
101 no_more = 0;
102 nip = hosts_database;
d19687d6 103
595aba70 104 /* Initialize configurations. */
6f9d4f59 105 _res_hconf_init ();
d19687d6 106 if (__res_maybe_init (&_res, 0) == -1)
e0a69f5c 107 no_more = 1;
d19687d6
UD
108
109 /* If we are looking for both IPv4 and IPv6 address we don't want
110 the lookup functions to automatically promote IPv4 addresses to
111 IPv6 addresses. Currently this is decided by setting the
112 RES_USE_INET6 bit in _res.options. */
113 int old_res_options = _res.options;
b76e0659 114 _res.options &= ~DEPRECATED_RES_USE_INET6;
d19687d6 115
ea42a20c 116 size_t tmpbuf6len = 1024;
d19687d6
UD
117 char *tmpbuf6 = alloca (tmpbuf6len);
118 size_t tmpbuf4len = 0;
119 char *tmpbuf4 = NULL;
384ca551 120 int32_t ttl = INT32_MAX;
d19687d6
UD
121 ssize_t total = 0;
122 char *key_copy = NULL;
123 bool alloca_used = false;
a4c7ea7b 124 time_t timeout = MAX_TIMEOUT_VALUE;
d19687d6
UD
125
126 while (!no_more)
127 {
1eb946b9 128 void *cp;
d19687d6 129 int status[2] = { NSS_STATUS_UNAVAIL, NSS_STATUS_UNAVAIL };
1eb946b9
UD
130 int naddrs = 0;
131 size_t addrslen = 0;
5c9629d2 132 char *canon = NULL;
1eb946b9
UD
133 size_t canonlen;
134
135 nss_gethostbyname4_r fct4 = __nss_lookup_function (nip,
136 "gethostbyname4_r");
137 if (fct4 != NULL)
138 {
ea42a20c
UD
139 struct gaih_addrtuple atmem;
140 struct gaih_addrtuple *at;
1eb946b9
UD
141 while (1)
142 {
ea42a20c 143 at = &atmem;
1eb946b9 144 rc6 = 0;
5c9629d2
UD
145 herrno = 0;
146 status[1] = DL_CALL_FCT (fct4, (key, &at, tmpbuf6, tmpbuf6len,
1eb946b9 147 &rc6, &herrno, &ttl));
5c9629d2
UD
148 if (rc6 != ERANGE || (herrno != NETDB_INTERNAL
149 && herrno != TRY_AGAIN))
1eb946b9
UD
150 break;
151 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
152 }
153
154 if (rc6 != 0 && herrno == NETDB_INTERNAL)
155 goto out;
d19687d6 156
5c9629d2 157 if (status[1] != NSS_STATUS_SUCCESS)
1eb946b9
UD
158 goto next_nip;
159
160 /* We found the data. Count the addresses and the size. */
ea42a20c 161 for (const struct gaih_addrtuple *at2 = at = &atmem; at2 != NULL;
5c9629d2 162 at2 = at2->next)
1eb946b9
UD
163 {
164 ++naddrs;
5c9629d2
UD
165 /* We do not handle anything other than IPv4 and IPv6
166 addresses. The getaddrinfo implementation does not
167 either so it is not worth trying to do more. */
1eb946b9
UD
168 if (at2->family == AF_INET)
169 addrslen += INADDRSZ;
5c9629d2 170 else if (at2->family == AF_INET6)
1eb946b9 171 addrslen += IN6ADDRSZ;
1eb946b9
UD
172 }
173 canon = at->name;
174 canonlen = strlen (canon) + 1;
175
176 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
177
178 /* Now we can allocate the data structure. If the TTL of the
179 entry is reported as zero do not cache the entry at all. */
180 if (ttl != 0 && he == NULL)
20e498bd
UD
181 dataset = (struct dataset *) mempool_alloc (db, total
182 + req->key_len, 1);
1eb946b9
UD
183
184 if (dataset == NULL)
185 {
186 /* We cannot permanently add the result in the moment. But
187 we can provide the result as is. Store the data in some
188 temporary memory. */
189 dataset = (struct dataset *) alloca (total + req->key_len);
190
191 /* We cannot add this record to the permanent database. */
192 alloca_used = true;
193 }
d1fe1f22 194
1eb946b9 195 /* Fill in the address and address families. */
5c9629d2 196 char *addrs = dataset->strdata;
1eb946b9
UD
197 uint8_t *family = (uint8_t *) (addrs + addrslen);
198
5c9629d2
UD
199 for (const struct gaih_addrtuple *at2 = at; at2 != NULL;
200 at2 = at2->next)
1eb946b9
UD
201 {
202 *family++ = at2->family;
203 if (at2->family == AF_INET)
204 addrs = mempcpy (addrs, at2->addr, INADDRSZ);
5c9629d2 205 else if (at2->family == AF_INET6)
1eb946b9 206 addrs = mempcpy (addrs, at2->addr, IN6ADDRSZ);
1eb946b9
UD
207 }
208
209 cp = family;
210 }
211 else
d19687d6 212 {
1eb946b9
UD
213 /* Prefer the function which also returns the TTL and
214 canonical name. */
215 nss_gethostbyname3_r fct = __nss_lookup_function (nip,
216 "gethostbyname3_r");
217 if (fct == NULL)
218 fct = __nss_lookup_function (nip, "gethostbyname2_r");
219
220 if (fct == NULL)
221 goto next_nip;
222
d19687d6
UD
223 struct hostent th[2];
224
225 /* Collect IPv6 information first. */
226 while (1)
227 {
228 rc6 = 0;
229 status[0] = DL_CALL_FCT (fct, (key, AF_INET6, &th[0], tmpbuf6,
1eb946b9
UD
230 tmpbuf6len, &rc6, &herrno, &ttl,
231 &canon));
d19687d6
UD
232 if (rc6 != ERANGE || herrno != NETDB_INTERNAL)
233 break;
234 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
235 }
236
237 if (rc6 != 0 && herrno == NETDB_INTERNAL)
238 goto out;
239
240 /* If the IPv6 lookup has been successful do not use the
241 buffer used in that lookup, use a new one. */
242 if (status[0] == NSS_STATUS_SUCCESS && rc6 == 0)
243 {
244 tmpbuf4len = 512;
245 tmpbuf4 = alloca (tmpbuf4len);
246 }
247 else
248 {
249 tmpbuf4len = tmpbuf6len;
250 tmpbuf4 = tmpbuf6;
251 }
252
ffb1b882 253 /* Next collect IPv4 information. */
d19687d6
UD
254 while (1)
255 {
256 rc4 = 0;
257 status[1] = DL_CALL_FCT (fct, (key, AF_INET, &th[1], tmpbuf4,
d1fe1f22 258 tmpbuf4len, &rc4, &herrno,
384ca551 259 ttl == INT32_MAX ? &ttl : NULL,
d1fe1f22 260 canon == NULL ? &canon : NULL));
d19687d6
UD
261 if (rc4 != ERANGE || herrno != NETDB_INTERNAL)
262 break;
d1fe1f22 263 tmpbuf4 = extend_alloca (tmpbuf4, tmpbuf4len, 2 * tmpbuf4len);
d19687d6
UD
264 }
265
ffb1b882 266 if (rc4 != 0 && herrno == NETDB_INTERNAL)
d19687d6
UD
267 goto out;
268
1eb946b9
UD
269 if (status[0] != NSS_STATUS_SUCCESS
270 && status[1] != NSS_STATUS_SUCCESS)
271 goto next_nip;
272
273 /* We found the data. Count the addresses and the size. */
274 for (int j = 0; j < 2; ++j)
275 if (status[j] == NSS_STATUS_SUCCESS)
276 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
277 {
278 ++naddrs;
279 addrslen += th[j].h_length;
280 }
281
282 if (canon == NULL)
d19687d6 283 {
1eb946b9
UD
284 /* Determine the canonical name. */
285 nss_getcanonname_r cfct;
286 cfct = __nss_lookup_function (nip, "getcanonname_r");
287 if (cfct != NULL)
288 {
289 const size_t max_fqdn_len = 256;
290 char *buf = alloca (max_fqdn_len);
291 char *s;
292 int rc;
293
294 if (DL_CALL_FCT (cfct, (key, buf, max_fqdn_len, &s,
295 &rc, &herrno))
296 == NSS_STATUS_SUCCESS)
297 canon = s;
298 else
299 /* Set to name now to avoid using gethostbyaddr. */
300 canon = key;
301 }
302 else
303 {
20e498bd 304 struct hostent *hstent = NULL;
1eb946b9 305 int herrno;
20e498bd 306 struct hostent hstent_mem;
1eb946b9
UD
307 void *addr;
308 size_t addrlen;
309 int addrfamily;
310
311 if (status[1] == NSS_STATUS_SUCCESS)
312 {
313 addr = th[1].h_addr_list[0];
314 addrlen = sizeof (struct in_addr);
315 addrfamily = AF_INET;
316 }
317 else
d19687d6 318 {
1eb946b9
UD
319 addr = th[0].h_addr_list[0];
320 addrlen = sizeof (struct in6_addr);
321 addrfamily = AF_INET6;
d19687d6
UD
322 }
323
1eb946b9
UD
324 size_t tmpbuflen = 512;
325 char *tmpbuf = alloca (tmpbuflen);
326 int rc;
327 while (1)
d1fe1f22 328 {
1eb946b9 329 rc = __gethostbyaddr2_r (addr, addrlen, addrfamily,
20e498bd
UD
330 &hstent_mem, tmpbuf, tmpbuflen,
331 &hstent, &herrno, NULL);
1eb946b9
UD
332 if (rc != ERANGE || herrno != NETDB_INTERNAL)
333 break;
334 tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
335 tmpbuflen * 2);
d1fe1f22 336 }
1eb946b9
UD
337
338 if (rc == 0)
d1fe1f22 339 {
20e498bd
UD
340 if (hstent != NULL)
341 canon = hstent->h_name;
d1fe1f22 342 else
1eb946b9 343 canon = key;
d1fe1f22 344 }
d19687d6 345 }
1eb946b9 346 }
d19687d6 347
1eb946b9
UD
348 canonlen = canon == NULL ? 0 : (strlen (canon) + 1);
349
350 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
d19687d6 351
d19687d6 352
1eb946b9
UD
353 /* Now we can allocate the data structure. If the TTL of the
354 entry is reported as zero do not cache the entry at all. */
355 if (ttl != 0 && he == NULL)
20e498bd
UD
356 dataset = (struct dataset *) mempool_alloc (db, total
357 + req->key_len, 1);
1eb946b9
UD
358
359 if (dataset == NULL)
360 {
361 /* We cannot permanently add the result in the moment. But
362 we can provide the result as is. Store the data in some
363 temporary memory. */
364 dataset = (struct dataset *) alloca (total + req->key_len);
365
366 /* We cannot add this record to the permanent database. */
367 alloca_used = true;
368 }
d19687d6 369
1eb946b9 370 /* Fill in the address and address families. */
5c9629d2 371 char *addrs = dataset->strdata;
1eb946b9
UD
372 uint8_t *family = (uint8_t *) (addrs + addrslen);
373
374 for (int j = 0; j < 2; ++j)
375 if (status[j] == NSS_STATUS_SUCCESS)
376 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
377 {
378 addrs = mempcpy (addrs, th[j].h_addr_list[i],
379 th[j].h_length);
380 *family++ = th[j].h_addrtype;
d19687d6
UD
381 }
382
1eb946b9
UD
383 cp = family;
384 }
d19687d6 385
1cdeb237
SP
386 timeout = datahead_init_pos (&dataset->head, total + req->key_len,
387 total - offsetof (struct dataset, resp),
388 he == NULL ? 0 : dh->nreloads + 1,
389 ttl == INT32_MAX ? db->postimeout : ttl);
d19687d6 390
1cdeb237 391 /* Fill in the rest of the dataset. */
1eb946b9
UD
392 dataset->resp.version = NSCD_VERSION;
393 dataset->resp.found = 1;
394 dataset->resp.naddrs = naddrs;
395 dataset->resp.addrslen = addrslen;
396 dataset->resp.canonlen = canonlen;
397 dataset->resp.error = NETDB_SUCCESS;
d19687d6 398
1eb946b9
UD
399 if (canon != NULL)
400 cp = mempcpy (cp, canon, canonlen);
d19687d6 401
1eb946b9
UD
402 key_copy = memcpy (cp, key, req->key_len);
403
5c9629d2
UD
404 assert (cp == (char *) dataset + total);
405
1eb946b9
UD
406 /* Now we can determine whether on refill we have to create a
407 new record or not. */
408 if (he != NULL)
409 {
410 assert (fd == -1);
411
412 if (total + req->key_len == dh->allocsize
413 && total - offsetof (struct dataset, resp) == dh->recsize
414 && memcmp (&dataset->resp, dh->data,
415 dh->allocsize - offsetof (struct dataset,
416 resp)) == 0)
417 {
418 /* The data has not changed. We will just bump the
419 timeout value. Note that the new record has been
420 allocated on the stack and need not be freed. */
421 dh->timeout = dataset->head.timeout;
a4c7ea7b 422 dh->ttl = dataset->head.ttl;
1eb946b9
UD
423 ++dh->nreloads;
424 }
425 else
426 {
427 /* We have to create a new record. Just allocate
428 appropriate memory and copy it. */
429 struct dataset *newp
430 = (struct dataset *) mempool_alloc (db, total + req->key_len,
20e498bd 431 1);
a1ffb40e 432 if (__glibc_likely (newp != NULL))
1eb946b9
UD
433 {
434 /* Adjust pointer into the memory block. */
435 key_copy = (char *) newp + (key_copy - (char *) dataset);
436
437 dataset = memcpy (newp, dataset, total + req->key_len);
438 alloca_used = false;
d19687d6 439 }
1eb946b9
UD
440
441 /* Mark the old record as obsolete. */
442 dh->usable = false;
443 }
444 }
445 else
446 {
447 /* We write the dataset before inserting it to the database
448 since while inserting this thread might block and so
449 would unnecessarily let the receiver wait. */
450 assert (fd != -1);
d19687d6 451
eac10791 452#ifdef HAVE_SENDFILE
1eb946b9
UD
453 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
454 {
455 assert (db->wr_fd != -1);
456 assert ((char *) &dataset->resp > (char *) db->data);
ea547a1a 457 assert ((char *) dataset - (char *) db->head + total
1eb946b9
UD
458 <= (sizeof (struct database_pers_head)
459 + db->head->module * sizeof (ref_t)
460 + db->head->data_size));
9bea3473 461# ifndef __ASSUME_SENDFILE
1eb946b9 462 ssize_t written;
9bea3473
UD
463 written =
464# endif
465 sendfileall (fd, db->wr_fd, (char *) &dataset->resp
466 - (char *) db->head, dataset->head.recsize);
eac10791 467# ifndef __ASSUME_SENDFILE
1eb946b9
UD
468 if (written == -1 && errno == ENOSYS)
469 goto use_write;
eac10791 470# endif
1eb946b9
UD
471 }
472 else
eac10791 473# ifndef __ASSUME_SENDFILE
1eb946b9 474 use_write:
eac10791
UD
475# endif
476#endif
ea547a1a 477 writeall (fd, &dataset->resp, dataset->head.recsize);
d19687d6
UD
478 }
479
1eb946b9
UD
480 goto out;
481
482next_nip:
d19687d6
UD
483 if (nss_next_action (nip, status[1]) == NSS_ACTION_RETURN)
484 break;
485
486 if (nip->next == NULL)
487 no_more = -1;
488 else
489 nip = nip->next;
490 }
491
492 /* No result found. Create a negative result record. */
493 if (he != NULL && rc4 == EAGAIN)
494 {
495 /* If we have an old record available but cannot find one now
496 because the service is not available we keep the old record
497 and make sure it does not get removed. */
498 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
499 /* Do not reset the value if we never not reload the record. */
500 dh->nreloads = reload_count - 1;
a4c7ea7b
UD
501
502 /* Reload with the same time-to-live value. */
503 timeout = dh->timeout = time (NULL) + dh->ttl;
d19687d6
UD
504 }
505 else
506 {
507 /* We have no data. This means we send the standard reply for
508 this case. */
509 total = sizeof (notfound);
510
511 if (fd != -1)
2c210d1e 512 TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
d19687d6 513
3e1aa84e
UD
514 /* If we have a transient error or cannot permanently store the
515 result, so be it. */
516 if (rc4 == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
445b4a53
TK
517 {
518 /* Mark the old entry as obsolete. */
519 if (dh != NULL)
520 dh->usable = false;
521 dataset = NULL;
522 }
99231d9a
UD
523 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
524 + req->key_len), 1)) != NULL)
d19687d6 525 {
1cdeb237
SP
526 timeout = datahead_init_neg (&dataset->head,
527 sizeof (struct dataset) + req->key_len,
528 total, db->negtimeout);
d19687d6
UD
529
530 /* This is the reply. */
531 memcpy (&dataset->resp, &notfound, total);
532
533 /* Copy the key data. */
534 key_copy = memcpy (dataset->strdata, key, req->key_len);
535 }
d19687d6
UD
536 }
537
538 out:
b76e0659 539 _res.options |= old_res_options & DEPRECATED_RES_USE_INET6;
d19687d6 540
d19687d6
UD
541 if (dataset != NULL && !alloca_used)
542 {
543 /* If necessary, we also propagate the data to disk. */
544 if (db->persistent)
545 {
546 // XXX async OK?
547 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
548 msync ((void *) pval,
549 ((uintptr_t) dataset & pagesize_m1) + total + req->key_len,
550 MS_ASYNC);
551 }
552
7e71e55f 553 (void) cache_add (req->type, key_copy, req->key_len, &dataset->head,
528741cb 554 true, db, uid, he == NULL);
d19687d6 555
00ebd7ed
UD
556 pthread_rwlock_unlock (&db->lock);
557
d19687d6
UD
558 /* Mark the old entry as obsolete. */
559 if (dh != NULL)
560 dh->usable = false;
561 }
a4c7ea7b
UD
562
563 return timeout;
d19687d6
UD
564}
565
566
567void
568addhstai (struct database_dyn *db, int fd, request_header *req, void *key,
569 uid_t uid)
570{
571 addhstaiX (db, fd, req, key, uid, NULL, NULL);
572}
573
574
a4c7ea7b 575time_t
d19687d6
UD
576readdhstai (struct database_dyn *db, struct hashentry *he, struct datahead *dh)
577{
578 request_header req =
579 {
580 .type = GETAI,
581 .key_len = he->len
582 };
583
a4c7ea7b 584 return addhstaiX (db, -1, &req, db->data + he->key, he->owner, he, dh);
d19687d6 585}