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