.
[glibc/history.git] / nscd / aicache.c
blob524c0a63af0080fc50a3b56830ef13b1d7a808be
1 /* Cache handling for host lookup.
2 Copyright (C) 2004-2008, 2009 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 *const he,
64 struct datahead *dh)
66 /* Search for the entry matching the key. Please note that we don't
67 look again in the table whether the dataset is now available. We
68 simply insert it. It does not matter if it is in there twice. The
69 pruning function only will look at the timestamp. */
71 /* We allocate all data in one memory block: the iov vector,
72 the response header and the dataset itself. */
73 struct dataset
75 struct datahead head;
76 ai_response_header resp;
77 char strdata[0];
78 } *dataset = NULL;
80 if (__builtin_expect (debug_level > 0, 0))
82 if (he == NULL)
83 dbg_log (_("Haven't found \"%s\" in hosts cache!"), (char *) key);
84 else
85 dbg_log (_("Reloading \"%s\" in hosts cache!"), (char *) key);
88 static service_user *hosts_database;
89 service_user *nip = NULL;
90 int no_more;
91 int rc6 = 0;
92 int rc4 = 0;
93 int herrno = 0;
95 if (hosts_database != NULL)
97 nip = hosts_database;
98 no_more = 0;
100 else
101 no_more = __nss_database_lookup ("hosts", NULL,
102 "dns [!UNAVAIL=return] files", &nip);
104 if (__res_maybe_init (&_res, 0) == -1)
105 no_more = 1;
107 /* If we are looking for both IPv4 and IPv6 address we don't want
108 the lookup functions to automatically promote IPv4 addresses to
109 IPv6 addresses. Currently this is decided by setting the
110 RES_USE_INET6 bit in _res.options. */
111 int old_res_options = _res.options;
112 _res.options &= ~RES_USE_INET6;
114 size_t tmpbuf6len = 512;
115 char *tmpbuf6 = alloca (tmpbuf6len);
116 size_t tmpbuf4len = 0;
117 char *tmpbuf4 = NULL;
118 int32_t ttl = INT32_MAX;
119 ssize_t total = 0;
120 char *key_copy = NULL;
121 bool alloca_used = false;
123 while (!no_more)
125 void *cp;
126 int status[2] = { NSS_STATUS_UNAVAIL, NSS_STATUS_UNAVAIL };
127 int naddrs = 0;
128 size_t addrslen = 0;
129 char *canon = NULL;
130 size_t canonlen;
132 nss_gethostbyname4_r fct4 = __nss_lookup_function (nip,
133 "gethostbyname4_r");
134 if (fct4 != NULL)
136 struct gaih_addrtuple *at = NULL;
137 while (1)
139 rc6 = 0;
140 herrno = 0;
141 status[1] = DL_CALL_FCT (fct4, (key, &at, tmpbuf6, tmpbuf6len,
142 &rc6, &herrno, &ttl));
143 if (rc6 != ERANGE || (herrno != NETDB_INTERNAL
144 && herrno != TRY_AGAIN))
145 break;
146 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
149 if (rc6 != 0 && herrno == NETDB_INTERNAL)
150 goto out;
152 if (status[1] != NSS_STATUS_SUCCESS)
153 goto next_nip;
155 /* We found the data. Count the addresses and the size. */
156 for (const struct gaih_addrtuple *at2 = at; at2 != NULL;
157 at2 = at2->next)
159 ++naddrs;
160 /* We do not handle anything other than IPv4 and IPv6
161 addresses. The getaddrinfo implementation does not
162 either so it is not worth trying to do more. */
163 if (at2->family == AF_INET)
164 addrslen += INADDRSZ;
165 else if (at2->family == AF_INET6)
166 addrslen += IN6ADDRSZ;
168 canon = at->name;
169 canonlen = strlen (canon) + 1;
171 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
173 /* Now we can allocate the data structure. If the TTL of the
174 entry is reported as zero do not cache the entry at all. */
175 if (ttl != 0 && he == NULL)
176 dataset = (struct dataset *) mempool_alloc (db, total
177 + req->key_len, 1);
179 if (dataset == NULL)
181 /* We cannot permanently add the result in the moment. But
182 we can provide the result as is. Store the data in some
183 temporary memory. */
184 dataset = (struct dataset *) alloca (total + req->key_len);
186 /* We cannot add this record to the permanent database. */
187 alloca_used = true;
190 /* Fill in the address and address families. */
191 char *addrs = dataset->strdata;
192 uint8_t *family = (uint8_t *) (addrs + addrslen);
194 for (const struct gaih_addrtuple *at2 = at; at2 != NULL;
195 at2 = at2->next)
197 *family++ = at2->family;
198 if (at2->family == AF_INET)
199 addrs = mempcpy (addrs, at2->addr, INADDRSZ);
200 else if (at2->family == AF_INET6)
201 addrs = mempcpy (addrs, at2->addr, IN6ADDRSZ);
204 cp = family;
206 else
208 /* Prefer the function which also returns the TTL and
209 canonical name. */
210 nss_gethostbyname3_r fct = __nss_lookup_function (nip,
211 "gethostbyname3_r");
212 if (fct == NULL)
213 fct = __nss_lookup_function (nip, "gethostbyname2_r");
215 if (fct == NULL)
216 goto next_nip;
218 struct hostent th[2];
220 /* Collect IPv6 information first. */
221 while (1)
223 rc6 = 0;
224 status[0] = DL_CALL_FCT (fct, (key, AF_INET6, &th[0], tmpbuf6,
225 tmpbuf6len, &rc6, &herrno, &ttl,
226 &canon));
227 if (rc6 != ERANGE || herrno != NETDB_INTERNAL)
228 break;
229 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
232 if (rc6 != 0 && herrno == NETDB_INTERNAL)
233 goto out;
235 /* If the IPv6 lookup has been successful do not use the
236 buffer used in that lookup, use a new one. */
237 if (status[0] == NSS_STATUS_SUCCESS && rc6 == 0)
239 tmpbuf4len = 512;
240 tmpbuf4 = alloca (tmpbuf4len);
242 else
244 tmpbuf4len = tmpbuf6len;
245 tmpbuf4 = tmpbuf6;
248 /* Next collect IPv4 information. */
249 while (1)
251 rc4 = 0;
252 status[1] = DL_CALL_FCT (fct, (key, AF_INET, &th[1], tmpbuf4,
253 tmpbuf4len, &rc4, &herrno,
254 ttl == INT32_MAX ? &ttl : NULL,
255 canon == NULL ? &canon : NULL));
256 if (rc4 != ERANGE || herrno != NETDB_INTERNAL)
257 break;
258 tmpbuf4 = extend_alloca (tmpbuf4, tmpbuf4len, 2 * tmpbuf4len);
261 if (rc4 != 0 && herrno == NETDB_INTERNAL)
262 goto out;
264 if (status[0] != NSS_STATUS_SUCCESS
265 && status[1] != NSS_STATUS_SUCCESS)
266 goto next_nip;
268 /* We found the data. Count the addresses and the size. */
269 for (int j = 0; j < 2; ++j)
270 if (status[j] == NSS_STATUS_SUCCESS)
271 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
273 ++naddrs;
274 addrslen += th[j].h_length;
277 if (canon == NULL)
279 /* Determine the canonical name. */
280 nss_getcanonname_r cfct;
281 cfct = __nss_lookup_function (nip, "getcanonname_r");
282 if (cfct != NULL)
284 const size_t max_fqdn_len = 256;
285 char *buf = alloca (max_fqdn_len);
286 char *s;
287 int rc;
289 if (DL_CALL_FCT (cfct, (key, buf, max_fqdn_len, &s,
290 &rc, &herrno))
291 == NSS_STATUS_SUCCESS)
292 canon = s;
293 else
294 /* Set to name now to avoid using gethostbyaddr. */
295 canon = key;
297 else
299 struct hostent *hstent = NULL;
300 int herrno;
301 struct hostent hstent_mem;
302 void *addr;
303 size_t addrlen;
304 int addrfamily;
306 if (status[1] == NSS_STATUS_SUCCESS)
308 addr = th[1].h_addr_list[0];
309 addrlen = sizeof (struct in_addr);
310 addrfamily = AF_INET;
312 else
314 addr = th[0].h_addr_list[0];
315 addrlen = sizeof (struct in6_addr);
316 addrfamily = AF_INET6;
319 size_t tmpbuflen = 512;
320 char *tmpbuf = alloca (tmpbuflen);
321 int rc;
322 while (1)
324 rc = __gethostbyaddr2_r (addr, addrlen, addrfamily,
325 &hstent_mem, tmpbuf, tmpbuflen,
326 &hstent, &herrno, NULL);
327 if (rc != ERANGE || herrno != NETDB_INTERNAL)
328 break;
329 tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
330 tmpbuflen * 2);
333 if (rc == 0)
335 if (hstent != NULL)
336 canon = hstent->h_name;
337 else
338 canon = key;
343 canonlen = canon == NULL ? 0 : (strlen (canon) + 1);
345 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
348 /* Now we can allocate the data structure. If the TTL of the
349 entry is reported as zero do not cache the entry at all. */
350 if (ttl != 0 && he == NULL)
351 dataset = (struct dataset *) mempool_alloc (db, total
352 + req->key_len, 1);
354 if (dataset == NULL)
356 /* We cannot permanently add the result in the moment. But
357 we can provide the result as is. Store the data in some
358 temporary memory. */
359 dataset = (struct dataset *) alloca (total + req->key_len);
361 /* We cannot add this record to the permanent database. */
362 alloca_used = true;
365 /* Fill in the address and address families. */
366 char *addrs = dataset->strdata;
367 uint8_t *family = (uint8_t *) (addrs + addrslen);
369 for (int j = 0; j < 2; ++j)
370 if (status[j] == NSS_STATUS_SUCCESS)
371 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
373 addrs = mempcpy (addrs, th[j].h_addr_list[i],
374 th[j].h_length);
375 *family++ = th[j].h_addrtype;
378 cp = family;
381 /* Fill in the rest of the dataset. */
382 dataset->head.allocsize = total + req->key_len;
383 dataset->head.recsize = total - offsetof (struct dataset, resp);
384 dataset->head.notfound = false;
385 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
386 dataset->head.usable = true;
388 /* Compute the timeout time. */
389 dataset->head.timeout = time (NULL) + (ttl == INT32_MAX
390 ? db->postimeout : ttl);
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;
399 if (canon != NULL)
400 cp = mempcpy (cp, canon, canonlen);
402 key_copy = memcpy (cp, key, req->key_len);
404 assert (cp == (char *) dataset + total);
406 /* Now we can determine whether on refill we have to create a
407 new record or not. */
408 if (he != NULL)
410 assert (fd == -1);
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)
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;
422 ++dh->nreloads;
424 else
426 /* We have to create a new record. Just allocate
427 appropriate memory and copy it. */
428 struct dataset *newp
429 = (struct dataset *) mempool_alloc (db, total + req->key_len,
431 if (__builtin_expect (newp != NULL, 1))
433 /* Adjust pointer into the memory block. */
434 key_copy = (char *) newp + (key_copy - (char *) dataset);
436 dataset = memcpy (newp, dataset, total + req->key_len);
437 alloca_used = false;
440 /* Mark the old record as obsolete. */
441 dh->usable = false;
444 else
446 /* We write the dataset before inserting it to the database
447 since while inserting this thread might block and so
448 would unnecessarily let the receiver wait. */
449 assert (fd != -1);
451 #ifdef HAVE_SENDFILE
452 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
454 assert (db->wr_fd != -1);
455 assert ((char *) &dataset->resp > (char *) db->data);
456 assert ((char *) &dataset->resp - (char *) db->head + total
457 <= (sizeof (struct database_pers_head)
458 + db->head->module * sizeof (ref_t)
459 + db->head->data_size));
460 ssize_t written;
461 written = sendfileall (fd, db->wr_fd, (char *) &dataset->resp
462 - (char *) db->head, total);
463 # ifndef __ASSUME_SENDFILE
464 if (written == -1 && errno == ENOSYS)
465 goto use_write;
466 # endif
468 else
469 # ifndef __ASSUME_SENDFILE
470 use_write:
471 # endif
472 #endif
473 writeall (fd, &dataset->resp, total);
476 goto out;
478 next_nip:
479 if (nss_next_action (nip, status[1]) == NSS_ACTION_RETURN)
480 break;
482 if (nip->next == NULL)
483 no_more = -1;
484 else
485 nip = nip->next;
488 /* No result found. Create a negative result record. */
489 if (he != NULL && rc4 == EAGAIN)
491 /* If we have an old record available but cannot find one now
492 because the service is not available we keep the old record
493 and make sure it does not get removed. */
494 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
495 /* Do not reset the value if we never not reload the record. */
496 dh->nreloads = reload_count - 1;
498 else
500 /* We have no data. This means we send the standard reply for
501 this case. */
502 total = sizeof (notfound);
504 if (fd != -1)
505 TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
507 dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1);
508 /* If we cannot permanently store the result, so be it. */
509 if (dataset != NULL)
511 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
512 dataset->head.recsize = total;
513 dataset->head.notfound = true;
514 dataset->head.nreloads = 0;
515 dataset->head.usable = true;
517 /* Compute the timeout time. */
518 dataset->head.timeout = time (NULL) + db->negtimeout;
520 /* This is the reply. */
521 memcpy (&dataset->resp, &notfound, total);
523 /* Copy the key data. */
524 key_copy = memcpy (dataset->strdata, key, req->key_len);
528 out:
529 _res.options = old_res_options;
531 if (dataset != NULL && !alloca_used)
533 /* If necessary, we also propagate the data to disk. */
534 if (db->persistent)
536 // XXX async OK?
537 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
538 msync ((void *) pval,
539 ((uintptr_t) dataset & pagesize_m1) + total + req->key_len,
540 MS_ASYNC);
543 (void) cache_add (req->type, key_copy, req->key_len, &dataset->head,
544 true, db, uid, he == NULL);
546 pthread_rwlock_unlock (&db->lock);
548 /* Mark the old entry as obsolete. */
549 if (dh != NULL)
550 dh->usable = false;
555 void
556 addhstai (struct database_dyn *db, int fd, request_header *req, void *key,
557 uid_t uid)
559 addhstaiX (db, fd, req, key, uid, NULL, NULL);
563 void
564 readdhstai (struct database_dyn *db, struct hashentry *he, struct datahead *dh)
566 request_header req =
568 .type = GETAI,
569 .key_len = he->len
572 addhstaiX (db, -1, &req, db->data + he->key, he->owner, he, dh);