getXXbyYY_r.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <atomic.h>
  17. #include <errno.h>
  18. #include <stdbool.h>
  19. #include "nsswitch.h"
  20. #include "sysdep.h"
  21. #ifdef USE_NSCD
  22. # include <nscd/nscd_proto.h>
  23. #endif
  24. #ifdef NEED__RES
  25. # include <resolv/resolv_context.h>
  26. #endif
  27. /*******************************************************************\
  28. |* Here we assume several symbols to be defined: *|
  29. |* *|
  30. |* LOOKUP_TYPE - the return type of the function *|
  31. |* *|
  32. |* FUNCTION_NAME - name of the non-reentrant function *|
  33. |* *|
  34. |* DATABASE_NAME - name of the database the function accesses *|
  35. |* (e.g., host, services, ...) *|
  36. |* *|
  37. |* ADD_PARAMS - additional parameters, can vary in number *|
  38. |* *|
  39. |* ADD_VARIABLES - names of additional parameters *|
  40. |* *|
  41. |* Optionally the following vars can be defined: *|
  42. |* *|
  43. |* EXTRA_PARAMS - optional parameters, can vary in number *|
  44. |* *|
  45. |* EXTRA_VARIABLES - names of optional parameter *|
  46. |* *|
  47. |* FUNCTION2_NAME - alternative name of the non-reentrant function *|
  48. |* *|
  49. |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
  50. |* the global `h_errno' variable. *|
  51. |* *|
  52. |* NEED__RES - obtain a struct resolv_context resolver context *|
  53. |* *|
  54. |* PREPROCESS - code run before anything else *|
  55. |* *|
  56. |* POSTPROCESS - code run after the lookup *|
  57. |* *|
  58. \*******************************************************************/
  59. /* To make the real sources a bit prettier. */
  60. #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
  61. #ifdef FUNCTION2_NAME
  62. # define REENTRANT2_NAME APPEND_R (FUNCTION2_NAME)
  63. #else
  64. # define REENTRANT2_NAME NULL
  65. #endif
  66. #define APPEND_R(name) APPEND_R1 (name)
  67. #define APPEND_R1(name) name##_r
  68. #define INTERNAL(name) INTERNAL1 (name)
  69. #define INTERNAL1(name) __##name
  70. #define NEW(name) NEW1 (name)
  71. #define NEW1(name) __new_##name
  72. #ifdef USE_NSCD
  73. # define NSCD_NAME ADD_NSCD (REENTRANT_NAME)
  74. # define ADD_NSCD(name) ADD_NSCD1 (name)
  75. # define ADD_NSCD1(name) __nscd_##name
  76. # define NOT_USENSCD_NAME ADD_NOT_NSCDUSE (DATABASE_NAME)
  77. # define ADD_NOT_NSCDUSE(name) ADD_NOT_NSCDUSE1 (name)
  78. # define ADD_NOT_NSCDUSE1(name) __nss_not_use_nscd_##name
  79. # define CONCAT2(arg1, arg2) CONCAT2_2 (arg1, arg2)
  80. # define CONCAT2_2(arg1, arg2) arg1##arg2
  81. #endif
  82. #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
  83. #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
  84. #ifdef FUNCTION2_NAME
  85. # define REENTRANT2_NAME_STRING STRINGIZE (REENTRANT2_NAME)
  86. #else
  87. # define REENTRANT2_NAME_STRING NULL
  88. #endif
  89. #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
  90. #define STRINGIZE(name) STRINGIZE1 (name)
  91. #define STRINGIZE1(name) #name
  92. #ifndef DB_LOOKUP_FCT
  93. # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2)
  94. # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
  95. # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
  96. #endif
  97. /* Sometimes we need to store error codes in the `h_errno' variable. */
  98. #ifdef NEED_H_ERRNO
  99. # define H_ERRNO_PARM , int *h_errnop
  100. # define H_ERRNO_VAR , h_errnop
  101. # define H_ERRNO_VAR_P h_errnop
  102. #else
  103. # define H_ERRNO_PARM
  104. # define H_ERRNO_VAR
  105. # define H_ERRNO_VAR_P NULL
  106. #endif
  107. #ifndef EXTRA_PARAMS
  108. # define EXTRA_PARAMS
  109. #endif
  110. #ifndef EXTRA_VARIABLES
  111. # define EXTRA_VARIABLES
  112. #endif
  113. #ifdef HAVE_AF
  114. # define AF_VAL af
  115. #else
  116. # define AF_VAL AF_INET
  117. #endif
  118. /* Set defaults for merge functions that haven't been defined. */
  119. #ifndef DEEPCOPY_FN
  120. static inline int
  121. __copy_einval (LOOKUP_TYPE a,
  122. const size_t b,
  123. LOOKUP_TYPE *c,
  124. char *d,
  125. char **e)
  126. {
  127. return EINVAL;
  128. }
  129. # define DEEPCOPY_FN __copy_einval
  130. #endif
  131. #ifndef MERGE_FN
  132. static inline int
  133. __merge_einval (LOOKUP_TYPE *a,
  134. char *b,
  135. char *c,
  136. size_t d,
  137. LOOKUP_TYPE *e,
  138. char *f)
  139. {
  140. return EINVAL;
  141. }
  142. # define MERGE_FN __merge_einval
  143. #endif
  144. #define CHECK_MERGE(err, status) \
  145. ({ \
  146. do \
  147. { \
  148. if (err) \
  149. { \
  150. __set_errno (err); \
  151. if (err == ERANGE) \
  152. status = NSS_STATUS_TRYAGAIN; \
  153. else \
  154. status = NSS_STATUS_UNAVAIL; \
  155. break; \
  156. } \
  157. } \
  158. while (0); \
  159. })
  160. /* Type of the lookup function we need here. */
  161. typedef enum nss_status (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *,
  162. size_t, int * H_ERRNO_PARM
  163. EXTRA_PARAMS);
  164. /* The lookup function for the first entry of this service. */
  165. extern int DB_LOOKUP_FCT (service_user **nip, const char *name,
  166. const char *name2, void **fctp);
  167. libc_hidden_proto (DB_LOOKUP_FCT)
  168. int
  169. INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
  170. size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM
  171. EXTRA_PARAMS)
  172. {
  173. static bool startp_initialized;
  174. static service_user *startp;
  175. static lookup_function start_fct;
  176. service_user *nip;
  177. int do_merge = 0;
  178. LOOKUP_TYPE mergegrp;
  179. char *mergebuf = NULL;
  180. char *endptr = NULL;
  181. union
  182. {
  183. lookup_function l;
  184. void *ptr;
  185. } fct;
  186. int no_more, err;
  187. enum nss_status status = NSS_STATUS_UNAVAIL;
  188. #ifdef USE_NSCD
  189. int nscd_status;
  190. #endif
  191. #ifdef NEED_H_ERRNO
  192. bool any_service = false;
  193. #endif
  194. #ifdef NEED__RES
  195. /* The HANDLE_DIGITS_DOTS case below already needs the resolver
  196. configuration, so this has to happen early. */
  197. struct resolv_context *res_ctx = __resolv_context_get ();
  198. if (res_ctx == NULL)
  199. {
  200. *h_errnop = NETDB_INTERNAL;
  201. *result = NULL;
  202. return errno;
  203. }
  204. #endif /* NEED__RES */
  205. #ifdef PREPROCESS
  206. PREPROCESS;
  207. #endif
  208. #ifdef HANDLE_DIGITS_DOTS
  209. switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,
  210. buflen, result, &status, AF_VAL,
  211. H_ERRNO_VAR_P))
  212. {
  213. case -1:
  214. # ifdef NEED__RES
  215. __resolv_context_put (res_ctx);
  216. # endif
  217. return errno;
  218. case 1:
  219. #ifdef NEED_H_ERRNO
  220. any_service = true;
  221. #endif
  222. goto done;
  223. }
  224. #endif
  225. #ifdef USE_NSCD
  226. if (NOT_USENSCD_NAME > 0 && ++NOT_USENSCD_NAME > NSS_NSCD_RETRY)
  227. NOT_USENSCD_NAME = 0;
  228. if (!NOT_USENSCD_NAME
  229. && !__nss_database_custom[CONCAT2 (NSS_DBSIDX_, DATABASE_NAME)])
  230. {
  231. nscd_status = NSCD_NAME (ADD_VARIABLES, resbuf, buffer, buflen, result
  232. H_ERRNO_VAR);
  233. if (nscd_status >= 0)
  234. {
  235. # ifdef NEED__RES
  236. __resolv_context_put (res_ctx);
  237. # endif
  238. return nscd_status;
  239. }
  240. }
  241. #endif
  242. if (! startp_initialized)
  243. {
  244. no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING,
  245. REENTRANT2_NAME_STRING, &fct.ptr);
  246. if (no_more)
  247. {
  248. void *tmp_ptr = (service_user *) -1l;
  249. #ifdef PTR_MANGLE
  250. PTR_MANGLE (tmp_ptr);
  251. #endif
  252. startp = tmp_ptr;
  253. }
  254. else
  255. {
  256. void *tmp_ptr = fct.l;
  257. #ifdef PTR_MANGLE
  258. PTR_MANGLE (tmp_ptr);
  259. #endif
  260. start_fct = tmp_ptr;
  261. tmp_ptr = nip;
  262. #ifdef PTR_MANGLE
  263. PTR_MANGLE (tmp_ptr);
  264. #endif
  265. startp = tmp_ptr;
  266. }
  267. /* Make sure start_fct and startp are written before
  268. startp_initialized. */
  269. atomic_write_barrier ();
  270. startp_initialized = true;
  271. }
  272. else
  273. {
  274. fct.l = start_fct;
  275. nip = startp;
  276. #ifdef PTR_DEMANGLE
  277. PTR_DEMANGLE (fct.l);
  278. PTR_DEMANGLE (nip);
  279. #endif
  280. no_more = nip == (service_user *) -1l;
  281. }
  282. while (no_more == 0)
  283. {
  284. #ifdef NEED_H_ERRNO
  285. any_service = true;
  286. #endif
  287. status = DL_CALL_FCT (fct.l, (ADD_VARIABLES, resbuf, buffer, buflen,
  288. &errno H_ERRNO_VAR EXTRA_VARIABLES));
  289. /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
  290. provided buffer is too small. In this case we should give
  291. the user the possibility to enlarge the buffer and we should
  292. not simply go on with the next service (even if the TRYAGAIN
  293. action tells us so). */
  294. if (status == NSS_STATUS_TRYAGAIN
  295. #ifdef NEED_H_ERRNO
  296. && *h_errnop == NETDB_INTERNAL
  297. #endif
  298. && errno == ERANGE)
  299. break;
  300. if (do_merge)
  301. {
  302. if (status == NSS_STATUS_SUCCESS)
  303. {
  304. /* The previous loop saved a buffer for merging.
  305. Perform the merge now. */
  306. err = MERGE_FN (&mergegrp, mergebuf, endptr, buflen, resbuf,
  307. buffer);
  308. CHECK_MERGE (err,status);
  309. do_merge = 0;
  310. }
  311. else
  312. {
  313. /* If the result wasn't SUCCESS, copy the saved buffer back
  314. into the result buffer and set the status back to
  315. NSS_STATUS_SUCCESS to match the previous pass through the
  316. loop.
  317. * If the next action is CONTINUE, it will overwrite the value
  318. currently in the buffer and return the new value.
  319. * If the next action is RETURN, we'll return the previously-
  320. acquired values.
  321. * If the next action is MERGE, then it will be added to the
  322. buffer saved from the previous source. */
  323. err = DEEPCOPY_FN (mergegrp, buflen, resbuf, buffer, NULL);
  324. CHECK_MERGE (err, status);
  325. status = NSS_STATUS_SUCCESS;
  326. }
  327. }
  328. /* If we were are configured to merge this value with the next one,
  329. save the current value of the group struct. */
  330. if (nss_next_action (nip, status) == NSS_ACTION_MERGE
  331. && status == NSS_STATUS_SUCCESS)
  332. {
  333. /* Copy the current values into a buffer to be merged with the next
  334. set of retrieved values. */
  335. if (mergebuf == NULL)
  336. {
  337. /* Only allocate once and reuse it for as many merges as we need
  338. to perform. */
  339. mergebuf = malloc (buflen);
  340. if (mergebuf == NULL)
  341. {
  342. __set_errno (ENOMEM);
  343. status = NSS_STATUS_UNAVAIL;
  344. break;
  345. }
  346. }
  347. err = DEEPCOPY_FN (*resbuf, buflen, &mergegrp, mergebuf, &endptr);
  348. CHECK_MERGE (err, status);
  349. do_merge = 1;
  350. }
  351. no_more = __nss_next2 (&nip, REENTRANT_NAME_STRING,
  352. REENTRANT2_NAME_STRING, &fct.ptr, status, 0);
  353. }
  354. free (mergebuf);
  355. mergebuf = NULL;
  356. #ifdef HANDLE_DIGITS_DOTS
  357. done:
  358. #endif
  359. *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
  360. #ifdef NEED_H_ERRNO
  361. if (status == NSS_STATUS_UNAVAIL && !any_service && errno != ENOENT)
  362. /* This happens when we weren't able to use a service for reasons other
  363. than the module not being found. In such a case, we'd want to tell the
  364. caller that errno has the real reason for failure. */
  365. *h_errnop = NETDB_INTERNAL;
  366. else if (status != NSS_STATUS_SUCCESS && !any_service)
  367. /* We were not able to use any service. */
  368. *h_errnop = NO_RECOVERY;
  369. #endif
  370. #ifdef POSTPROCESS
  371. POSTPROCESS;
  372. #endif
  373. #ifdef NEED__RES
  374. /* This has to happen late because the POSTPROCESS stage above might
  375. need the resolver context. */
  376. __resolv_context_put (res_ctx);
  377. #endif /* NEED__RES */
  378. int res;
  379. if (status == NSS_STATUS_SUCCESS || status == NSS_STATUS_NOTFOUND)
  380. res = 0;
  381. /* Don't pass back ERANGE if this is not for a too-small buffer. */
  382. else if (errno == ERANGE && status != NSS_STATUS_TRYAGAIN)
  383. res = EINVAL;
  384. #ifdef NEED_H_ERRNO
  385. /* These functions only set errno if h_errno is NETDB_INTERNAL. */
  386. else if (status == NSS_STATUS_TRYAGAIN && *h_errnop != NETDB_INTERNAL)
  387. res = EAGAIN;
  388. #endif
  389. else
  390. return errno;
  391. __set_errno (res);
  392. return res;
  393. }
  394. #ifdef NO_COMPAT_NEEDED
  395. strong_alias (INTERNAL (REENTRANT_NAME), REENTRANT_NAME);
  396. #elif !defined FUNCTION2_NAME
  397. # include <shlib-compat.h>
  398. # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2)
  399. # define OLD(name) OLD1 (name)
  400. # define OLD1(name) __old_##name
  401. int
  402. attribute_compat_text_section
  403. OLD (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, char *buffer,
  404. size_t buflen, LOOKUP_TYPE **result H_ERRNO_PARM)
  405. {
  406. int ret = INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, resbuf, buffer,
  407. buflen, result H_ERRNO_VAR);
  408. if (ret != 0 || result == NULL)
  409. ret = -1;
  410. return ret;
  411. }
  412. # define do_symbol_version(real, name, version) \
  413. compat_symbol (libc, real, name, version)
  414. do_symbol_version (OLD (REENTRANT_NAME), REENTRANT_NAME, GLIBC_2_0);
  415. # endif
  416. /* As INTERNAL (REENTRANT_NAME) may be hidden, we need an alias
  417. in between so that the REENTRANT_NAME@@GLIBC_2.1.2 is not
  418. hidden too. */
  419. strong_alias (INTERNAL (REENTRANT_NAME), NEW (REENTRANT_NAME));
  420. # define do_default_symbol_version(real, name, version) \
  421. versioned_symbol (libc, real, name, version)
  422. do_default_symbol_version (NEW (REENTRANT_NAME),
  423. REENTRANT_NAME, GLIBC_2_1_2);
  424. #endif
  425. nss_interface_function (REENTRANT_NAME)