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