nscd_getai.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 2004-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <netdb.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <not-cancel.h>
  21. #include "nscd-client.h"
  22. #include "nscd_proto.h"
  23. /* Define in nscd_gethst_r.c. */
  24. extern int __nss_not_use_nscd_hosts;
  25. /* We use the mapping from nscd_gethst. */
  26. libc_locked_map_ptr (extern, __hst_map_handle) attribute_hidden;
  27. /* Defined in nscd_gethst_r.c. */
  28. extern int __nss_have_localdomain attribute_hidden;
  29. int
  30. __nscd_getai (const char *key, struct nscd_ai_result **result, int *h_errnop)
  31. {
  32. if (__glibc_unlikely (__nss_have_localdomain >= 0))
  33. {
  34. if (__nss_have_localdomain == 0)
  35. __nss_have_localdomain = getenv ("LOCALDOMAIN") != NULL ? 1 : -1;
  36. if (__nss_have_localdomain > 0)
  37. {
  38. __nss_not_use_nscd_hosts = 1;
  39. return -1;
  40. }
  41. }
  42. size_t keylen = strlen (key) + 1;
  43. int gc_cycle;
  44. int nretries = 0;
  45. /* If the mapping is available, try to search there instead of
  46. communicating with the nscd. */
  47. struct mapped_database *mapped;
  48. mapped = __nscd_get_map_ref (GETFDHST, "hosts", &__hst_map_handle,
  49. &gc_cycle);
  50. retry:;
  51. struct nscd_ai_result *resultbuf = NULL;
  52. const char *recend = (const char *) ~UINTMAX_C (0);
  53. char *respdata = NULL;
  54. int retval = -1;
  55. int sock = -1;
  56. ai_response_header ai_resp;
  57. if (mapped != NO_MAPPING)
  58. {
  59. struct datahead *found = __nscd_cache_search (GETAI, key, keylen,
  60. mapped, sizeof ai_resp);
  61. if (found != NULL)
  62. {
  63. respdata = (char *) (&found->data[0].aidata + 1);
  64. ai_resp = found->data[0].aidata;
  65. recend = (const char *) found->data + found->recsize;
  66. /* Now check if we can trust ai_resp fields. If GC is
  67. in progress, it can contain anything. */
  68. if (mapped->head->gc_cycle != gc_cycle)
  69. {
  70. retval = -2;
  71. goto out;
  72. }
  73. }
  74. }
  75. /* If we do not have the cache mapped, try to get the data over the
  76. socket. */
  77. if (respdata == NULL)
  78. {
  79. sock = __nscd_open_socket (key, keylen, GETAI, &ai_resp,
  80. sizeof (ai_resp));
  81. if (sock == -1)
  82. {
  83. /* nscd not running or wrong version. */
  84. __nss_not_use_nscd_hosts = 1;
  85. goto out;
  86. }
  87. }
  88. if (ai_resp.found == 1)
  89. {
  90. size_t datalen = ai_resp.naddrs + ai_resp.addrslen + ai_resp.canonlen;
  91. /* This check really only affects the case where the data
  92. comes from the mapped cache. */
  93. if (respdata + datalen > recend)
  94. {
  95. assert (sock == -1);
  96. goto out;
  97. }
  98. /* Create result. */
  99. resultbuf = (struct nscd_ai_result *) malloc (sizeof (*resultbuf)
  100. + datalen);
  101. if (resultbuf == NULL)
  102. {
  103. *h_errnop = NETDB_INTERNAL;
  104. goto out_close;
  105. }
  106. /* Set up the data structure, including pointers. */
  107. resultbuf->naddrs = ai_resp.naddrs;
  108. resultbuf->addrs = (char *) (resultbuf + 1);
  109. resultbuf->family = (uint8_t *) (resultbuf->addrs + ai_resp.addrslen);
  110. if (ai_resp.canonlen != 0)
  111. resultbuf->canon = (char *) (resultbuf->family + resultbuf->naddrs);
  112. else
  113. resultbuf->canon = NULL;
  114. if (respdata == NULL)
  115. {
  116. /* Read the data from the socket. */
  117. if ((size_t) __readall (sock, resultbuf + 1, datalen) == datalen)
  118. {
  119. retval = 0;
  120. *result = resultbuf;
  121. }
  122. else
  123. {
  124. free (resultbuf);
  125. *h_errnop = NETDB_INTERNAL;
  126. }
  127. }
  128. else
  129. {
  130. /* Copy the data in the block. */
  131. memcpy (resultbuf + 1, respdata, datalen);
  132. /* Try to detect corrupt databases. */
  133. if (resultbuf->canon != NULL
  134. && resultbuf->canon[ai_resp.canonlen - 1] != '\0')
  135. /* We cannot use the database. */
  136. {
  137. if (mapped->head->gc_cycle != gc_cycle)
  138. retval = -2;
  139. else
  140. free (resultbuf);
  141. goto out_close;
  142. }
  143. retval = 0;
  144. *result = resultbuf;
  145. }
  146. }
  147. else
  148. {
  149. if (__glibc_unlikely (ai_resp.found == -1))
  150. {
  151. /* The daemon does not cache this database. */
  152. __nss_not_use_nscd_hosts = 1;
  153. goto out_close;
  154. }
  155. /* Store the error number. */
  156. *h_errnop = ai_resp.error;
  157. /* Set errno to 0 to indicate no error, just no found record. */
  158. __set_errno (0);
  159. /* Even though we have not found anything, the result is zero. */
  160. retval = 0;
  161. }
  162. out_close:
  163. if (sock != -1)
  164. __close_nocancel_nostatus (sock);
  165. out:
  166. if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
  167. {
  168. /* When we come here this means there has been a GC cycle while we
  169. were looking for the data. This means the data might have been
  170. inconsistent. Retry if possible. */
  171. if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
  172. {
  173. /* nscd is just running gc now. Disable using the mapping. */
  174. if (atomic_fetch_add_relaxed (&mapped->counter, -1) == 1)
  175. __nscd_unmap (mapped);
  176. mapped = NO_MAPPING;
  177. }
  178. if (retval != -1)
  179. {
  180. *result = NULL;
  181. free (resultbuf);
  182. goto retry;
  183. }
  184. }
  185. return retval;
  186. }