2.9
[glibc/nacl-glibc.git] / nscd / aicache.c
blob5ffab76a04401b680c5f104bef288af99825a6e3
1 /* Cache handling for host lookup.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 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, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <libintl.h>
23 #include <netdb.h>
24 #include <nss.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
30 #include "dbg_log.h"
31 #include "nscd.h"
32 #ifdef HAVE_SENDFILE
33 # include <kernel-features.h>
34 #endif
37 typedef enum nss_status (*nss_gethostbyname4_r)
38 (const char *name, struct gaih_addrtuple **pat,
39 char *buffer, size_t buflen, int *errnop,
40 int *h_errnop, int32_t *ttlp);
41 typedef enum nss_status (*nss_gethostbyname3_r)
42 (const char *name, int af, struct hostent *host,
43 char *buffer, size_t buflen, int *errnop,
44 int *h_errnop, int32_t *, char **);
45 typedef enum nss_status (*nss_getcanonname_r)
46 (const char *name, char *buffer, size_t buflen, char **result,
47 int *errnop, int *h_errnop);
50 static const ai_response_header notfound =
52 .version = NSCD_VERSION,
53 .found = 0,
54 .naddrs = 0,
55 .addrslen = 0,
56 .canonlen = 0,
57 .error = 0
61 static void
62 addhstaiX (struct database_dyn *db, int fd, request_header *req,
63 void *key, uid_t uid, struct hashentry *he, struct datahead *dh)
65 /* Search for the entry matching the key. Please note that we don't
66 look again in the table whether the dataset is now available. We
67 simply insert it. It does not matter if it is in there twice. The
68 pruning function only will look at the timestamp. */
70 /* We allocate all data in one memory block: the iov vector,
71 the response header and the dataset itself. */
72 struct dataset
74 struct datahead head;
75 ai_response_header resp;
76 char strdata[0];
77 } *dataset = NULL;
79 if (__builtin_expect (debug_level > 0, 0))
81 if (he == NULL)
82 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
83 else
84 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key);
87 static service_user *hosts_database;
88 service_user *nip = NULL;
89 int no_more;
90 int rc6 = 0;
91 int rc4 = 0;
92 int herrno = 0;
94 if (hosts_database != NULL)
96 nip = hosts_database;
97 no_more = 0;
99 else
100 no_more = __nss_database_lookup ("hosts", NULL,
101 "dns [!UNAVAIL=return] files", &nip);
103 if (__res_maybe_init (&_res, 0) == -1)
104 no_more = 1;
106 /* If we are looking for both IPv4 and IPv6 address we don't want
107 the lookup functions to automatically promote IPv4 addresses to
108 IPv6 addresses. Currently this is decided by setting the
109 RES_USE_INET6 bit in _res.options. */
110 int old_res_options = _res.options;
111 _res.options &= ~RES_USE_INET6;
113 size_t tmpbuf6len = 512;
114 char *tmpbuf6 = alloca (tmpbuf6len);
115 size_t tmpbuf4len = 0;
116 char *tmpbuf4 = NULL;
117 int32_t ttl = INT32_MAX;
118 ssize_t total = 0;
119 char *key_copy = NULL;
120 bool alloca_used = false;
122 while (!no_more)
124 void *cp;
125 int status[2] = { NSS_STATUS_UNAVAIL, NSS_STATUS_UNAVAIL };
126 int naddrs = 0;
127 size_t addrslen = 0;
128 char *canon = NULL;
129 size_t canonlen;
131 nss_gethostbyname4_r fct4 = __nss_lookup_function (nip,
132 "gethostbyname4_r");
133 if (fct4 != NULL)
135 struct gaih_addrtuple *at = NULL;
136 while (1)
138 rc6 = 0;
139 herrno = 0;
140 status[1] = DL_CALL_FCT (fct4, (key, &at, tmpbuf6, tmpbuf6len,
141 &rc6, &herrno, &ttl));
142 if (rc6 != ERANGE || (herrno != NETDB_INTERNAL
143 && herrno != TRY_AGAIN))
144 break;
145 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
148 if (rc6 != 0 && herrno == NETDB_INTERNAL)
149 goto out;
151 if (status[1] != NSS_STATUS_SUCCESS)
152 goto next_nip;
154 /* We found the data. Count the addresses and the size. */
155 for (const struct gaih_addrtuple *at2 = at; at2 != NULL;
156 at2 = at2->next)
158 ++naddrs;
159 /* We do not handle anything other than IPv4 and IPv6
160 addresses. The getaddrinfo implementation does not
161 either so it is not worth trying to do more. */
162 if (at2->family == AF_INET)
163 addrslen += INADDRSZ;
164 else if (at2->family == AF_INET6)
165 addrslen += IN6ADDRSZ;
167 canon = at->name;
168 canonlen = strlen (canon) + 1;
170 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
172 /* Now we can allocate the data structure. If the TTL of the
173 entry is reported as zero do not cache the entry at all. */
174 if (ttl != 0 && he == NULL)
176 dataset = (struct dataset *) mempool_alloc (db, total
177 + req->key_len,
178 IDX_result_data);
179 if (dataset == NULL)
180 ++db->head->addfailed;
183 if (dataset == NULL)
185 /* We cannot permanently add the result in the moment. But
186 we can provide the result as is. Store the data in some
187 temporary memory. */
188 dataset = (struct dataset *) alloca (total + req->key_len);
190 /* We cannot add this record to the permanent database. */
191 alloca_used = true;
194 /* Fill in the address and address families. */
195 char *addrs = dataset->strdata;
196 uint8_t *family = (uint8_t *) (addrs + addrslen);
198 for (const struct gaih_addrtuple *at2 = at; at2 != NULL;
199 at2 = at2->next)
201 *family++ = at2->family;
202 if (at2->family == AF_INET)
203 addrs = mempcpy (addrs, at2->addr, INADDRSZ);
204 else if (at2->family == AF_INET6)
205 addrs = mempcpy (addrs, at2->addr, IN6ADDRSZ);
208 cp = family;
210 else
212 /* Prefer the function which also returns the TTL and
213 canonical name. */
214 nss_gethostbyname3_r fct = __nss_lookup_function (nip,
215 "gethostbyname3_r");
216 if (fct == NULL)
217 fct = __nss_lookup_function (nip, "gethostbyname2_r");
219 if (fct == NULL)
220 goto next_nip;
222 struct hostent th[2];
224 /* Collect IPv6 information first. */
225 while (1)
227 rc6 = 0;
228 status[0] = DL_CALL_FCT (fct, (key, AF_INET6, &th[0], tmpbuf6,
229 tmpbuf6len, &rc6, &herrno, &ttl,
230 &canon));
231 if (rc6 != ERANGE || herrno != NETDB_INTERNAL)
232 break;
233 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
236 if (rc6 != 0 && herrno == NETDB_INTERNAL)
237 goto out;
239 /* If the IPv6 lookup has been successful do not use the
240 buffer used in that lookup, use a new one. */
241 if (status[0] == NSS_STATUS_SUCCESS && rc6 == 0)
243 tmpbuf4len = 512;
244 tmpbuf4 = alloca (tmpbuf4len);
246 else
248 tmpbuf4len = tmpbuf6len;
249 tmpbuf4 = tmpbuf6;
252 /* Next collect IPv4 information. */
253 while (1)
255 rc4 = 0;
256 status[1] = DL_CALL_FCT (fct, (key, AF_INET, &th[1], tmpbuf4,
257 tmpbuf4len, &rc4, &herrno,
258 ttl == INT32_MAX ? &ttl : NULL,
259 canon == NULL ? &canon : NULL));
260 if (rc4 != ERANGE || herrno != NETDB_INTERNAL)
261 break;
262 tmpbuf4 = extend_alloca (tmpbuf4, tmpbuf4len, 2 * tmpbuf4len);
265 if (rc4 != 0 && herrno == NETDB_INTERNAL)
266 goto out;
268 if (status[0] != NSS_STATUS_SUCCESS
269 && status[1] != NSS_STATUS_SUCCESS)
270 goto next_nip;
272 /* We found the data. Count the addresses and the size. */
273 for (int j = 0; j < 2; ++j)
274 if (status[j] == NSS_STATUS_SUCCESS)
275 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
277 ++naddrs;
278 addrslen += th[j].h_length;
281 if (canon == NULL)
283 /* Determine the canonical name. */
284 nss_getcanonname_r cfct;
285 cfct = __nss_lookup_function (nip, "getcanonname_r");
286 if (cfct != NULL)
288 const size_t max_fqdn_len = 256;
289 char *buf = alloca (max_fqdn_len);
290 char *s;
291 int rc;
293 if (DL_CALL_FCT (cfct, (key, buf, max_fqdn_len, &s,
294 &rc, &herrno))
295 == NSS_STATUS_SUCCESS)
296 canon = s;
297 else
298 /* Set to name now to avoid using gethostbyaddr. */
299 canon = key;
301 else
303 struct hostent *he = NULL;
304 int herrno;
305 struct hostent he_mem;
306 void *addr;
307 size_t addrlen;
308 int addrfamily;
310 if (status[1] == NSS_STATUS_SUCCESS)
312 addr = th[1].h_addr_list[0];
313 addrlen = sizeof (struct in_addr);
314 addrfamily = AF_INET;
316 else
318 addr = th[0].h_addr_list[0];
319 addrlen = sizeof (struct in6_addr);
320 addrfamily = AF_INET6;
323 size_t tmpbuflen = 512;
324 char *tmpbuf = alloca (tmpbuflen);
325 int rc;
326 while (1)
328 rc = __gethostbyaddr2_r (addr, addrlen, addrfamily,
329 &he_mem, tmpbuf, tmpbuflen,
330 &he, &herrno, NULL);
331 if (rc != ERANGE || herrno != NETDB_INTERNAL)
332 break;
333 tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
334 tmpbuflen * 2);
337 if (rc == 0)
339 if (he != NULL)
340 canon = he->h_name;
341 else
342 canon = key;
347 canonlen = canon == NULL ? 0 : (strlen (canon) + 1);
349 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
352 /* Now we can allocate the data structure. If the TTL of the
353 entry is reported as zero do not cache the entry at all. */
354 if (ttl != 0 && he == NULL)
356 dataset = (struct dataset *) mempool_alloc (db, total
357 + req->key_len,
358 IDX_result_data);
359 if (dataset == NULL)
360 ++db->head->addfailed;
363 if (dataset == NULL)
365 /* We cannot permanently add the result in the moment. But
366 we can provide the result as is. Store the data in some
367 temporary memory. */
368 dataset = (struct dataset *) alloca (total + req->key_len);
370 /* We cannot add this record to the permanent database. */
371 alloca_used = true;
374 /* Fill in the address and address families. */
375 char *addrs = dataset->strdata;
376 uint8_t *family = (uint8_t *) (addrs + addrslen);
378 for (int j = 0; j < 2; ++j)
379 if (status[j] == NSS_STATUS_SUCCESS)
380 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
382 addrs = mempcpy (addrs, th[j].h_addr_list[i],
383 th[j].h_length);
384 *family++ = th[j].h_addrtype;
387 cp = family;
390 /* Fill in the rest of the dataset. */
391 dataset->head.allocsize = total + req->key_len;
392 dataset->head.recsize = total - offsetof (struct dataset, resp);
393 dataset->head.notfound = false;
394 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
395 dataset->head.usable = true;
397 /* Compute the timeout time. */
398 dataset->head.timeout = time (NULL) + (ttl == INT32_MAX
399 ? db->postimeout : ttl);
401 dataset->resp.version = NSCD_VERSION;
402 dataset->resp.found = 1;
403 dataset->resp.naddrs = naddrs;
404 dataset->resp.addrslen = addrslen;
405 dataset->resp.canonlen = canonlen;
406 dataset->resp.error = NETDB_SUCCESS;
408 if (canon != NULL)
409 cp = mempcpy (cp, canon, canonlen);
411 key_copy = memcpy (cp, key, req->key_len);
413 assert (cp == (char *) dataset + total);
415 /* Now we can determine whether on refill we have to create a
416 new record or not. */
417 if (he != NULL)
419 assert (fd == -1);
421 if (total + req->key_len == dh->allocsize
422 && total - offsetof (struct dataset, resp) == dh->recsize
423 && memcmp (&dataset->resp, dh->data,
424 dh->allocsize - offsetof (struct dataset,
425 resp)) == 0)
427 /* The data has not changed. We will just bump the
428 timeout value. Note that the new record has been
429 allocated on the stack and need not be freed. */
430 dh->timeout = dataset->head.timeout;
431 ++dh->nreloads;
433 else
435 /* We have to create a new record. Just allocate
436 appropriate memory and copy it. */
437 struct dataset *newp
438 = (struct dataset *) mempool_alloc (db, total + req->key_len,
439 IDX_result_data);
440 if (__builtin_expect (newp != NULL, 1))
442 /* Adjust pointer into the memory block. */
443 key_copy = (char *) newp + (key_copy - (char *) dataset);
445 dataset = memcpy (newp, dataset, total + req->key_len);
446 alloca_used = false;
448 else
449 ++db->head->addfailed;
451 /* Mark the old record as obsolete. */
452 dh->usable = false;
455 else
457 /* We write the dataset before inserting it to the database
458 since while inserting this thread might block and so
459 would unnecessarily let the receiver wait. */
460 assert (fd != -1);
462 #ifdef HAVE_SENDFILE
463 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
465 assert (db->wr_fd != -1);
466 assert ((char *) &dataset->resp > (char *) db->data);
467 assert ((char *) &dataset->resp - (char *) db->head + total
468 <= (sizeof (struct database_pers_head)
469 + db->head->module * sizeof (ref_t)
470 + db->head->data_size));
471 ssize_t written;
472 written = sendfileall (fd, db->wr_fd, (char *) &dataset->resp
473 - (char *) db->head, total);
474 # ifndef __ASSUME_SENDFILE
475 if (written == -1 && errno == ENOSYS)
476 goto use_write;
477 # endif
479 else
480 # ifndef __ASSUME_SENDFILE
481 use_write:
482 # endif
483 #endif
484 writeall (fd, &dataset->resp, total);
487 goto out;
489 next_nip:
490 if (nss_next_action (nip, status[1]) == NSS_ACTION_RETURN)
491 break;
493 if (nip->next == NULL)
494 no_more = -1;
495 else
496 nip = nip->next;
499 /* No result found. Create a negative result record. */
500 if (he != NULL && rc4 == EAGAIN)
502 /* If we have an old record available but cannot find one now
503 because the service is not available we keep the old record
504 and make sure it does not get removed. */
505 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
506 /* Do not reset the value if we never not reload the record. */
507 dh->nreloads = reload_count - 1;
509 else
511 /* We have no data. This means we send the standard reply for
512 this case. */
513 total = sizeof (notfound);
515 if (fd != -1)
516 TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
518 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len,
519 IDX_result_data);
520 /* If we cannot permanently store the result, so be it. */
521 if (dataset != NULL)
523 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
524 dataset->head.recsize = total;
525 dataset->head.notfound = true;
526 dataset->head.nreloads = 0;
527 dataset->head.usable = true;
529 /* Compute the timeout time. */
530 dataset->head.timeout = time (NULL) + db->negtimeout;
532 /* This is the reply. */
533 memcpy (&dataset->resp, &notfound, total);
535 /* Copy the key data. */
536 key_copy = memcpy (dataset->strdata, key, req->key_len);
538 else
539 ++db->head->addfailed;
542 out:
543 _res.options = old_res_options;
545 if (dataset != NULL && !alloca_used)
547 /* If necessary, we also propagate the data to disk. */
548 if (db->persistent)
550 // XXX async OK?
551 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
552 msync ((void *) pval,
553 ((uintptr_t) dataset & pagesize_m1) + total + req->key_len,
554 MS_ASYNC);
557 /* Now get the lock to safely insert the records. */
558 pthread_rwlock_rdlock (&db->lock);
560 (void) cache_add (req->type, key_copy, req->key_len, &dataset->head,
561 true, db, uid, he == NULL);
563 pthread_rwlock_unlock (&db->lock);
565 /* Mark the old entry as obsolete. */
566 if (dh != NULL)
567 dh->usable = false;
572 void
573 addhstai (struct database_dyn *db, int fd, request_header *req, void *key,
574 uid_t uid)
576 addhstaiX (db, fd, req, key, uid, NULL, NULL);
580 void
581 readdhstai (struct database_dyn *db, struct hashentry *he, struct datahead *dh)
583 request_header req =
585 .type = GETAI,
586 .key_len = he->len
589 addhstaiX (db, -1, &req, db->data + he->key, he->owner, he, dh);