getnssent_r.c 7.1 KB

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