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