nss_action.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* NSS actions, elements in a nsswitch.conf configuration line.
  2. Copyright (c) 2020-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <nsswitch.h>
  16. #include <string.h>
  17. #include <libc-lock.h>
  18. /* Maintain a global list of NSS action lists. Since most databases
  19. use the same list of actions, this list is usually short.
  20. Deduplication in __nss_action_allocate ensures that the list does
  21. not grow without bounds. */
  22. struct nss_action_list_wrapper
  23. {
  24. /* The next element of the list. */
  25. struct nss_action_list_wrapper *next;
  26. /* Number of elements in the list (excluding the terminator). */
  27. size_t count;
  28. /* NULL-terminated list of actions. */
  29. struct nss_action actions[];
  30. };
  31. /* Toplevel list of allocated NSS action lists. */
  32. static struct nss_action_list_wrapper *nss_actions;
  33. /* Lock covers the nss_actions list. */
  34. __libc_lock_define (static, nss_actions_lock);
  35. /* Returns true if the actions are equal (same module, same actions
  36. array). */
  37. static bool
  38. actions_equal (const struct nss_action *a, const struct nss_action *b)
  39. {
  40. return a->module == b->module && a->action_bits == b->action_bits;
  41. }
  42. /* Returns true if COUNT actions at A and B are equal (according to
  43. actions_equal above). Caller must ensure that either A or B have at
  44. least COUNT actions. */
  45. static bool
  46. action_lists_equal (const struct nss_action *a, const struct nss_action *b,
  47. size_t count)
  48. {
  49. for (size_t i = 0; i < count; ++i)
  50. if (!actions_equal (a + i, b + i))
  51. return false;
  52. return true;
  53. }
  54. /* Returns a pre-allocated action list for COUNT actions at ACTIONS,
  55. or NULL if no such list exists. */
  56. static nss_action_list
  57. find_allocated (struct nss_action *actions, size_t count)
  58. {
  59. for (struct nss_action_list_wrapper *p = nss_actions; p != NULL; p = p->next)
  60. if (p->count == count && action_lists_equal (p->actions, actions, count))
  61. return p->actions;
  62. return NULL;
  63. }
  64. nss_action_list
  65. __nss_action_allocate (struct nss_action *actions, size_t count)
  66. {
  67. nss_action_list result = NULL;
  68. __libc_lock_lock (nss_actions_lock);
  69. result = find_allocated (actions, count);
  70. if (result == NULL)
  71. {
  72. struct nss_action_list_wrapper *wrapper
  73. = malloc (sizeof (*wrapper) + sizeof (*actions) * count);
  74. if (wrapper != NULL)
  75. {
  76. wrapper->next = nss_actions;
  77. wrapper->count = count;
  78. memcpy (wrapper->actions, actions, sizeof (*actions) * count);
  79. nss_actions = wrapper;
  80. result = wrapper->actions;
  81. }
  82. }
  83. __libc_lock_unlock (nss_actions_lock);
  84. return result;
  85. }
  86. void
  87. __nss_action_freeres (void)
  88. {
  89. struct nss_action_list_wrapper *current = nss_actions;
  90. while (current != NULL)
  91. {
  92. struct nss_action_list_wrapper *next = current->next;
  93. free (current);
  94. current = next;
  95. }
  96. nss_actions = NULL;
  97. }