getnssent_r.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2000-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 <errno.h>
  15. #include <netdb.h>
  16. #include "nsswitch.h"
  17. #include <resolv/resolv_context.h>
  18. /* Set up NIP to run through the services. If ALL is zero, use NIP's
  19. current location if it's not nil. Return nonzero if there are no
  20. services (left). */
  21. static int
  22. setup (const char *func_name, db_lookup_function lookup_fct,
  23. void **fctp, nss_action_list *nip, nss_action_list *startp, int all)
  24. {
  25. int no_more;
  26. if (*startp == NULL || all)
  27. {
  28. no_more = lookup_fct (nip, func_name, NULL, fctp);
  29. *startp = no_more ? (nss_action_list) -1l : *nip;
  30. }
  31. else if (*startp == (nss_action_list) -1l)
  32. /* No services at all. */
  33. return 1;
  34. else
  35. {
  36. if (!*nip)
  37. /* Reset to the beginning of the service list. */
  38. *nip = *startp;
  39. /* Look up the first function. */
  40. no_more = __nss_lookup (nip, func_name, NULL, fctp);
  41. }
  42. return no_more;
  43. }
  44. void
  45. __nss_setent (const char *func_name, db_lookup_function lookup_fct,
  46. nss_action_list *nip, nss_action_list *startp,
  47. nss_action_list *last_nip, int stayopen, int *stayopen_tmp,
  48. int res)
  49. {
  50. void *fct;
  51. int no_more;
  52. struct resolv_context *res_ctx = NULL;
  53. if (res)
  54. {
  55. res_ctx = __resolv_context_get ();
  56. if (res_ctx == NULL)
  57. {
  58. __set_h_errno (NETDB_INTERNAL);
  59. return;
  60. }
  61. }
  62. /* Cycle through the services and run their `setXXent' functions until
  63. we find an available service. */
  64. no_more = setup (func_name, lookup_fct, &fct, nip,
  65. startp, 1);
  66. while (! no_more)
  67. {
  68. int is_last_nip = *nip == *last_nip;
  69. enum nss_status status;
  70. if (stayopen_tmp)
  71. status = DL_CALL_FCT (((setent_function) fct), (*stayopen_tmp));
  72. else
  73. status = DL_CALL_FCT (((setent_function) fct), (0));
  74. /* This is a special-case. When [SUCCESS=merge] is in play,
  75. _nss_next2() will skip to the next database. Due to the
  76. implementation of that function, we can't know whether we're
  77. in an enumeration or an individual lookup, which behaves
  78. differently with regards to merging. We'll treat SUCCESS as
  79. an indication to start the enumeration at this database. */
  80. if (nss_next_action (*nip, status) == NSS_ACTION_MERGE)
  81. no_more = 1;
  82. else
  83. no_more = __nss_next2 (nip, func_name, NULL, &fct, status, 0);
  84. if (is_last_nip)
  85. *last_nip = *nip;
  86. }
  87. __resolv_context_put (res_ctx);
  88. if (stayopen_tmp)
  89. *stayopen_tmp = stayopen;
  90. }
  91. void
  92. __nss_endent (const char *func_name, db_lookup_function lookup_fct,
  93. nss_action_list *nip, nss_action_list *startp,
  94. nss_action_list *last_nip, int res)
  95. {
  96. void *fct;
  97. int no_more;
  98. struct resolv_context *res_ctx = NULL;
  99. if (res)
  100. {
  101. res_ctx = __resolv_context_get ();
  102. if (res_ctx == NULL)
  103. {
  104. __set_h_errno (NETDB_INTERNAL);
  105. return;
  106. }
  107. }
  108. /* Cycle through all the services and run their endXXent functions. */
  109. no_more = setup (func_name, lookup_fct, &fct, nip, startp, 1);
  110. while (! no_more)
  111. {
  112. /* Ignore status, we force check in __NSS_NEXT. */
  113. DL_CALL_FCT (((endent_function) fct), ());
  114. if (*nip == *last_nip)
  115. /* We have processed all services which were used. */
  116. break;
  117. no_more = __nss_next2 (nip, func_name, NULL, &fct, 0, 1);
  118. }
  119. *last_nip = *nip = NULL;
  120. __resolv_context_put (res_ctx);
  121. }
  122. int
  123. __nss_getent_r (const char *getent_func_name,
  124. const char *setent_func_name,
  125. db_lookup_function lookup_fct,
  126. nss_action_list *nip, nss_action_list *startp,
  127. nss_action_list *last_nip, int *stayopen_tmp, int res,
  128. void *resbuf, char *buffer, size_t buflen,
  129. void **result, int *h_errnop)
  130. {
  131. void *fct;
  132. int no_more;
  133. enum nss_status status;
  134. struct resolv_context *res_ctx = NULL;
  135. if (res)
  136. {
  137. res_ctx = __resolv_context_get ();
  138. if (res_ctx == NULL)
  139. {
  140. *h_errnop = NETDB_INTERNAL;
  141. *result = NULL;
  142. return errno;
  143. }
  144. }
  145. /* Initialize status to return if no more functions are found. */
  146. status = NSS_STATUS_NOTFOUND;
  147. /* Run through available functions, starting with the same function last
  148. run. We will repeat each function as long as it succeeds, and then go
  149. on to the next service action. */
  150. no_more = setup (getent_func_name, lookup_fct, &fct, nip,
  151. startp, 0);
  152. while (! no_more)
  153. {
  154. int is_last_nip = *nip == *last_nip;
  155. status = DL_CALL_FCT (((getent_function) fct),
  156. (resbuf, buffer, buflen, &errno, &h_errno));
  157. /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
  158. provided buffer is too small. In this case we should give
  159. the user the possibility to enlarge the buffer and we should
  160. not simply go on with the next service (even if the TRYAGAIN
  161. action tells us so). */
  162. if (status == NSS_STATUS_TRYAGAIN
  163. && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
  164. && errno == ERANGE)
  165. break;
  166. do
  167. {
  168. /* This is a special-case. When [SUCCESS=merge] is in play,
  169. _nss_next2() will skip to the next database. Due to the
  170. implementation of that function, we can't know whether we're
  171. in an enumeration or an individual lookup, which behaves
  172. differently with regards to merging. We'll treat SUCCESS as
  173. an indication to return the results here. */
  174. if (status == NSS_STATUS_SUCCESS
  175. && nss_next_action (*nip, status) == NSS_ACTION_MERGE)
  176. no_more = 1;
  177. else
  178. no_more = __nss_next2 (nip, getent_func_name, NULL, &fct,
  179. status, 0);
  180. if (is_last_nip)
  181. *last_nip = *nip;
  182. if (! no_more)
  183. {
  184. /* Call the `setXXent' function. This wasn't done before. */
  185. void *sfct;
  186. no_more = __nss_lookup (nip, setent_func_name, NULL, &sfct);
  187. if (! no_more)
  188. {
  189. if (stayopen_tmp)
  190. status = DL_CALL_FCT (((setent_function) sfct), (*stayopen_tmp));
  191. else
  192. status = DL_CALL_FCT (((setent_function) sfct), (0));
  193. }
  194. else
  195. status = NSS_STATUS_NOTFOUND;
  196. }
  197. }
  198. while (! no_more && status != NSS_STATUS_SUCCESS);
  199. }
  200. __resolv_context_put (res_ctx);
  201. *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
  202. return (status == NSS_STATUS_SUCCESS ? 0
  203. : status != NSS_STATUS_TRYAGAIN ? ENOENT
  204. /* h_errno functions only set errno if h_errno is NETDB_INTERNAL. */
  205. : (h_errnop == NULL || *h_errnop == NETDB_INTERNAL) ? errno
  206. : EAGAIN);
  207. }