nsswitch.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (C) 1996-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 <ctype.h>
  15. #include <dlfcn.h>
  16. #include <errno.h>
  17. #include <netdb.h>
  18. #include <libc-lock.h>
  19. #include <search.h>
  20. #include <stdio.h>
  21. #include <stdio_ext.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <aliases.h>
  25. #include <grp.h>
  26. #include <netinet/ether.h>
  27. #include <pwd.h>
  28. #include <shadow.h>
  29. #include <unistd.h>
  30. #if !defined DO_STATIC_NSS || defined SHARED
  31. # include <gnu/lib-names.h>
  32. #endif
  33. #include "nsswitch.h"
  34. #include "../nscd/nscd_proto.h"
  35. #include <sysdep.h>
  36. #include <config.h>
  37. #ifdef USE_NSCD
  38. /* Flags whether custom rules for database is set. */
  39. bool __nss_database_custom[NSS_DBSIDX_max];
  40. #endif
  41. /*__libc_lock_define_initialized (static, lock)*/
  42. /* -1 == not found
  43. 0 == function found
  44. 1 == finished */
  45. int
  46. __nss_lookup (nss_action_list *ni, const char *fct_name, const char *fct2_name,
  47. void **fctp)
  48. {
  49. *fctp = __nss_lookup_function (*ni, fct_name);
  50. if (*fctp == NULL && fct2_name != NULL)
  51. *fctp = __nss_lookup_function (*ni, fct2_name);
  52. while (*fctp == NULL
  53. && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
  54. && (*ni)[1].module != NULL)
  55. {
  56. ++(*ni);
  57. *fctp = __nss_lookup_function (*ni, fct_name);
  58. if (*fctp == NULL && fct2_name != NULL)
  59. *fctp = __nss_lookup_function (*ni, fct2_name);
  60. }
  61. return *fctp != NULL ? 0 : (*ni)[1].module == NULL ? 1 : -1;
  62. }
  63. libc_hidden_def (__nss_lookup)
  64. /* -1 == not found
  65. 0 == adjusted for next function
  66. 1 == finished */
  67. int
  68. __nss_next2 (nss_action_list *ni, const char *fct_name, const char *fct2_name,
  69. void **fctp, int status, int all_values)
  70. {
  71. if (all_values)
  72. {
  73. if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
  74. && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
  75. && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
  76. && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
  77. return 1;
  78. }
  79. else
  80. {
  81. /* This is really only for debugging. */
  82. if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
  83. || status > NSS_STATUS_RETURN, 0))
  84. __libc_fatal ("Illegal status in __nss_next.\n");
  85. if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
  86. return 1;
  87. }
  88. if ((*ni)[1].module == NULL)
  89. return -1;
  90. do
  91. {
  92. ++(*ni);
  93. *fctp = __nss_lookup_function (*ni, fct_name);
  94. if (*fctp == NULL && fct2_name != NULL)
  95. *fctp = __nss_lookup_function (*ni, fct2_name);
  96. }
  97. while (*fctp == NULL
  98. && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
  99. && (*ni)[1].module != NULL);
  100. return *fctp != NULL ? 0 : -1;
  101. }
  102. libc_hidden_def (__nss_next2)
  103. void *
  104. __nss_lookup_function (nss_action_list ni, const char *fct_name)
  105. {
  106. if (ni->module == NULL)
  107. return NULL;
  108. return __nss_module_get_function (ni->module, fct_name);
  109. }
  110. libc_hidden_def (__nss_lookup_function)