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