don't use Bash-specific ${parameter/pattern/string} expansion
[glibc.git] / nscd / aicache.c
blobfd55852ed85d7f7542a43047748bf8f4b421f09f
1 /* Cache handling for host lookup.
2 Copyright (C) 2004-2013 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, see <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <libintl.h>
22 #include <netdb.h>
23 #include <nss.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <resolv/res_hconf.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 time_t
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 /* Initialize configurations. */
105 if (__glibc_unlikely (!_res_hconf.initialized))
106 _res_hconf_init ();
107 if (__res_maybe_init (&_res, 0) == -1)
108 no_more = 1;
110 /* If we are looking for both IPv4 and IPv6 address we don't want
111 the lookup functions to automatically promote IPv4 addresses to
112 IPv6 addresses. Currently this is decided by setting the
113 RES_USE_INET6 bit in _res.options. */
114 int old_res_options = _res.options;
115 _res.options &= ~RES_USE_INET6;
117 size_t tmpbuf6len = 1024;
118 char *tmpbuf6 = alloca (tmpbuf6len);
119 size_t tmpbuf4len = 0;
120 char *tmpbuf4 = NULL;
121 int32_t ttl = INT32_MAX;
122 ssize_t total = 0;
123 char *key_copy = NULL;
124 bool alloca_used = false;
125 time_t timeout = MAX_TIMEOUT_VALUE;
127 while (!no_more)
129 void *cp;
130 int status[2] = { NSS_STATUS_UNAVAIL, NSS_STATUS_UNAVAIL };
131 int naddrs = 0;
132 size_t addrslen = 0;
133 char *canon = NULL;
134 size_t canonlen;
136 nss_gethostbyname4_r fct4 = __nss_lookup_function (nip,
137 "gethostbyname4_r");
138 if (fct4 != NULL)
140 struct gaih_addrtuple atmem;
141 struct gaih_addrtuple *at;
142 while (1)
144 at = &atmem;
145 rc6 = 0;
146 herrno = 0;
147 status[1] = DL_CALL_FCT (fct4, (key, &at, tmpbuf6, tmpbuf6len,
148 &rc6, &herrno, &ttl));
149 if (rc6 != ERANGE || (herrno != NETDB_INTERNAL
150 && herrno != TRY_AGAIN))
151 break;
152 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
155 if (rc6 != 0 && herrno == NETDB_INTERNAL)
156 goto out;
158 if (status[1] != NSS_STATUS_SUCCESS)
159 goto next_nip;
161 /* We found the data. Count the addresses and the size. */
162 for (const struct gaih_addrtuple *at2 = at = &atmem; at2 != NULL;
163 at2 = at2->next)
165 ++naddrs;
166 /* We do not handle anything other than IPv4 and IPv6
167 addresses. The getaddrinfo implementation does not
168 either so it is not worth trying to do more. */
169 if (at2->family == AF_INET)
170 addrslen += INADDRSZ;
171 else if (at2->family == AF_INET6)
172 addrslen += IN6ADDRSZ;
174 canon = at->name;
175 canonlen = strlen (canon) + 1;
177 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
179 /* Now we can allocate the data structure. If the TTL of the
180 entry is reported as zero do not cache the entry at all. */
181 if (ttl != 0 && he == NULL)
182 dataset = (struct dataset *) mempool_alloc (db, total
183 + req->key_len, 1);
185 if (dataset == NULL)
187 /* We cannot permanently add the result in the moment. But
188 we can provide the result as is. Store the data in some
189 temporary memory. */
190 dataset = (struct dataset *) alloca (total + req->key_len);
192 /* We cannot add this record to the permanent database. */
193 alloca_used = true;
196 /* Fill in the address and address families. */
197 char *addrs = dataset->strdata;
198 uint8_t *family = (uint8_t *) (addrs + addrslen);
200 for (const struct gaih_addrtuple *at2 = at; at2 != NULL;
201 at2 = at2->next)
203 *family++ = at2->family;
204 if (at2->family == AF_INET)
205 addrs = mempcpy (addrs, at2->addr, INADDRSZ);
206 else if (at2->family == AF_INET6)
207 addrs = mempcpy (addrs, at2->addr, IN6ADDRSZ);
210 cp = family;
212 else
214 /* Prefer the function which also returns the TTL and
215 canonical name. */
216 nss_gethostbyname3_r fct = __nss_lookup_function (nip,
217 "gethostbyname3_r");
218 if (fct == NULL)
219 fct = __nss_lookup_function (nip, "gethostbyname2_r");
221 if (fct == NULL)
222 goto next_nip;
224 struct hostent th[2];
226 /* Collect IPv6 information first. */
227 while (1)
229 rc6 = 0;
230 status[0] = DL_CALL_FCT (fct, (key, AF_INET6, &th[0], tmpbuf6,
231 tmpbuf6len, &rc6, &herrno, &ttl,
232 &canon));
233 if (rc6 != ERANGE || herrno != NETDB_INTERNAL)
234 break;
235 tmpbuf6 = extend_alloca (tmpbuf6, tmpbuf6len, 2 * tmpbuf6len);
238 if (rc6 != 0 && herrno == NETDB_INTERNAL)
239 goto out;
241 /* If the IPv6 lookup has been successful do not use the
242 buffer used in that lookup, use a new one. */
243 if (status[0] == NSS_STATUS_SUCCESS && rc6 == 0)
245 tmpbuf4len = 512;
246 tmpbuf4 = alloca (tmpbuf4len);
248 else
250 tmpbuf4len = tmpbuf6len;
251 tmpbuf4 = tmpbuf6;
254 /* Next collect IPv4 information. */
255 while (1)
257 rc4 = 0;
258 status[1] = DL_CALL_FCT (fct, (key, AF_INET, &th[1], tmpbuf4,
259 tmpbuf4len, &rc4, &herrno,
260 ttl == INT32_MAX ? &ttl : NULL,
261 canon == NULL ? &canon : NULL));
262 if (rc4 != ERANGE || herrno != NETDB_INTERNAL)
263 break;
264 tmpbuf4 = extend_alloca (tmpbuf4, tmpbuf4len, 2 * tmpbuf4len);
267 if (rc4 != 0 && herrno == NETDB_INTERNAL)
268 goto out;
270 if (status[0] != NSS_STATUS_SUCCESS
271 && status[1] != NSS_STATUS_SUCCESS)
272 goto next_nip;
274 /* We found the data. Count the addresses and the size. */
275 for (int j = 0; j < 2; ++j)
276 if (status[j] == NSS_STATUS_SUCCESS)
277 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
279 ++naddrs;
280 addrslen += th[j].h_length;
283 if (canon == NULL)
285 /* Determine the canonical name. */
286 nss_getcanonname_r cfct;
287 cfct = __nss_lookup_function (nip, "getcanonname_r");
288 if (cfct != NULL)
290 const size_t max_fqdn_len = 256;
291 char *buf = alloca (max_fqdn_len);
292 char *s;
293 int rc;
295 if (DL_CALL_FCT (cfct, (key, buf, max_fqdn_len, &s,
296 &rc, &herrno))
297 == NSS_STATUS_SUCCESS)
298 canon = s;
299 else
300 /* Set to name now to avoid using gethostbyaddr. */
301 canon = key;
303 else
305 struct hostent *hstent = NULL;
306 int herrno;
307 struct hostent hstent_mem;
308 void *addr;
309 size_t addrlen;
310 int addrfamily;
312 if (status[1] == NSS_STATUS_SUCCESS)
314 addr = th[1].h_addr_list[0];
315 addrlen = sizeof (struct in_addr);
316 addrfamily = AF_INET;
318 else
320 addr = th[0].h_addr_list[0];
321 addrlen = sizeof (struct in6_addr);
322 addrfamily = AF_INET6;
325 size_t tmpbuflen = 512;
326 char *tmpbuf = alloca (tmpbuflen);
327 int rc;
328 while (1)
330 rc = __gethostbyaddr2_r (addr, addrlen, addrfamily,
331 &hstent_mem, tmpbuf, tmpbuflen,
332 &hstent, &herrno, NULL);
333 if (rc != ERANGE || herrno != NETDB_INTERNAL)
334 break;
335 tmpbuf = extend_alloca (tmpbuf, tmpbuflen,
336 tmpbuflen * 2);
339 if (rc == 0)
341 if (hstent != NULL)
342 canon = hstent->h_name;
343 else
344 canon = key;
349 canonlen = canon == NULL ? 0 : (strlen (canon) + 1);
351 total = sizeof (*dataset) + naddrs + addrslen + canonlen;
354 /* Now we can allocate the data structure. If the TTL of the
355 entry is reported as zero do not cache the entry at all. */
356 if (ttl != 0 && he == NULL)
357 dataset = (struct dataset *) mempool_alloc (db, total
358 + req->key_len, 1);
360 if (dataset == NULL)
362 /* We cannot permanently add the result in the moment. But
363 we can provide the result as is. Store the data in some
364 temporary memory. */
365 dataset = (struct dataset *) alloca (total + req->key_len);
367 /* We cannot add this record to the permanent database. */
368 alloca_used = true;
371 /* Fill in the address and address families. */
372 char *addrs = dataset->strdata;
373 uint8_t *family = (uint8_t *) (addrs + addrslen);
375 for (int j = 0; j < 2; ++j)
376 if (status[j] == NSS_STATUS_SUCCESS)
377 for (int i = 0; th[j].h_addr_list[i] != NULL; ++i)
379 addrs = mempcpy (addrs, th[j].h_addr_list[i],
380 th[j].h_length);
381 *family++ = th[j].h_addrtype;
384 cp = family;
387 /* Fill in the rest of the dataset. */
388 dataset->head.allocsize = total + req->key_len;
389 dataset->head.recsize = total - offsetof (struct dataset, resp);
390 dataset->head.notfound = false;
391 dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1);
392 dataset->head.usable = true;
394 /* Compute the timeout time. */
395 dataset->head.ttl = ttl == INT32_MAX ? db->postimeout : ttl;
396 timeout = dataset->head.timeout = time (NULL) + dataset->head.ttl;
398 dataset->resp.version = NSCD_VERSION;
399 dataset->resp.found = 1;
400 dataset->resp.naddrs = naddrs;
401 dataset->resp.addrslen = addrslen;
402 dataset->resp.canonlen = canonlen;
403 dataset->resp.error = NETDB_SUCCESS;
405 if (canon != NULL)
406 cp = mempcpy (cp, canon, canonlen);
408 key_copy = memcpy (cp, key, req->key_len);
410 assert (cp == (char *) dataset + total);
412 /* Now we can determine whether on refill we have to create a
413 new record or not. */
414 if (he != NULL)
416 assert (fd == -1);
418 if (total + req->key_len == dh->allocsize
419 && total - offsetof (struct dataset, resp) == dh->recsize
420 && memcmp (&dataset->resp, dh->data,
421 dh->allocsize - offsetof (struct dataset,
422 resp)) == 0)
424 /* The data has not changed. We will just bump the
425 timeout value. Note that the new record has been
426 allocated on the stack and need not be freed. */
427 dh->timeout = dataset->head.timeout;
428 dh->ttl = dataset->head.ttl;
429 ++dh->nreloads;
431 else
433 /* We have to create a new record. Just allocate
434 appropriate memory and copy it. */
435 struct dataset *newp
436 = (struct dataset *) mempool_alloc (db, total + req->key_len,
438 if (__builtin_expect (newp != NULL, 1))
440 /* Adjust pointer into the memory block. */
441 key_copy = (char *) newp + (key_copy - (char *) dataset);
443 dataset = memcpy (newp, dataset, total + req->key_len);
444 alloca_used = false;
447 /* Mark the old record as obsolete. */
448 dh->usable = false;
451 else
453 /* We write the dataset before inserting it to the database
454 since while inserting this thread might block and so
455 would unnecessarily let the receiver wait. */
456 assert (fd != -1);
458 #ifdef HAVE_SENDFILE
459 if (__builtin_expect (db->mmap_used, 1) && !alloca_used)
461 assert (db->wr_fd != -1);
462 assert ((char *) &dataset->resp > (char *) db->data);
463 assert ((char *) dataset - (char *) db->head + total
464 <= (sizeof (struct database_pers_head)
465 + db->head->module * sizeof (ref_t)
466 + db->head->data_size));
467 # ifndef __ASSUME_SENDFILE
468 ssize_t written;
469 written =
470 # endif
471 sendfileall (fd, db->wr_fd, (char *) &dataset->resp
472 - (char *) db->head, dataset->head.recsize);
473 # ifndef __ASSUME_SENDFILE
474 if (written == -1 && errno == ENOSYS)
475 goto use_write;
476 # endif
478 else
479 # ifndef __ASSUME_SENDFILE
480 use_write:
481 # endif
482 #endif
483 writeall (fd, &dataset->resp, dataset->head.recsize);
486 goto out;
488 next_nip:
489 if (nss_next_action (nip, status[1]) == NSS_ACTION_RETURN)
490 break;
492 if (nip->next == NULL)
493 no_more = -1;
494 else
495 nip = nip->next;
498 /* No result found. Create a negative result record. */
499 if (he != NULL && rc4 == EAGAIN)
501 /* If we have an old record available but cannot find one now
502 because the service is not available we keep the old record
503 and make sure it does not get removed. */
504 if (reload_count != UINT_MAX && dh->nreloads == reload_count)
505 /* Do not reset the value if we never not reload the record. */
506 dh->nreloads = reload_count - 1;
508 /* Reload with the same time-to-live value. */
509 timeout = dh->timeout = time (NULL) + dh->ttl;
511 else
513 /* We have no data. This means we send the standard reply for
514 this case. */
515 total = sizeof (notfound);
517 if (fd != -1)
518 TEMP_FAILURE_RETRY (send (fd, &notfound, total, MSG_NOSIGNAL));
520 /* If we have a transient error or cannot permanently store the
521 result, so be it. */
522 if (rc4 == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
524 /* Mark the old entry as obsolete. */
525 if (dh != NULL)
526 dh->usable = false;
527 dataset = NULL;
529 else if ((dataset = mempool_alloc (db, (sizeof (struct dataset)
530 + req->key_len), 1)) != NULL)
532 dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
533 dataset->head.recsize = total;
534 dataset->head.notfound = true;
535 dataset->head.nreloads = 0;
536 dataset->head.usable = true;
538 /* Compute the timeout time. */
539 timeout = dataset->head.timeout = time (NULL) + db->negtimeout;
540 dataset->head.ttl = db->negtimeout;
542 /* This is the reply. */
543 memcpy (&dataset->resp, &notfound, total);
545 /* Copy the key data. */
546 key_copy = memcpy (dataset->strdata, key, req->key_len);
550 out:
551 _res.options |= old_res_options & RES_USE_INET6;
553 if (dataset != NULL && !alloca_used)
555 /* If necessary, we also propagate the data to disk. */
556 if (db->persistent)
558 // XXX async OK?
559 uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
560 msync ((void *) pval,
561 ((uintptr_t) dataset & pagesize_m1) + total + req->key_len,
562 MS_ASYNC);
565 (void) cache_add (req->type, key_copy, req->key_len, &dataset->head,
566 true, db, uid, he == NULL);
568 pthread_rwlock_unlock (&db->lock);
570 /* Mark the old entry as obsolete. */
571 if (dh != NULL)
572 dh->usable = false;
575 return timeout;
579 void
580 addhstai (struct database_dyn *db, int fd, request_header *req, void *key,
581 uid_t uid)
583 addhstaiX (db, fd, req, key, uid, NULL, NULL);
587 time_t
588 readdhstai (struct database_dyn *db, struct hashentry *he, struct datahead *dh)
590 request_header req =
592 .type = GETAI,
593 .key_len = he->len
596 return addhstaiX (db, -1, &req, db->data + he->key, he->owner, he, dh);