tst-reload2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Test that reloading is disabled after a chroot.
  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 <nss.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <limits.h>
  20. #include <sys/types.h>
  21. #include <errno.h>
  22. #include <pwd.h>
  23. #include <grp.h>
  24. #include <unistd.h>
  25. #include <netdb.h>
  26. #include <support/support.h>
  27. #include <support/check.h>
  28. #include <support/xunistd.h>
  29. #include "nss_test.h"
  30. #ifndef PATH_MAX
  31. # define PATH_MAX 1024
  32. #endif
  33. static struct passwd pwd_table1[] =
  34. {
  35. PWD_N (1234, "test1"),
  36. PWD_N (4321, "test2"),
  37. PWD_LAST ()
  38. };
  39. static const char *group_4[] = {
  40. "alpha", "beta", "gamma", "fred", NULL
  41. };
  42. static struct group group_table_data1[] =
  43. {
  44. GRP (4),
  45. GRP_LAST ()
  46. };
  47. void
  48. _nss_test1_init_hook (test_tables *t)
  49. {
  50. t->pwd_table = pwd_table1;
  51. t->grp_table = group_table_data1;
  52. }
  53. static struct passwd pwd_table2[] =
  54. {
  55. PWD_N (5, "test1"),
  56. PWD_N (2468, "test2"),
  57. PWD_LAST ()
  58. };
  59. static const char *group_5[] = {
  60. "fred", NULL
  61. };
  62. static struct group group_table_data2[] =
  63. {
  64. GRP (5),
  65. GRP_LAST ()
  66. };
  67. void
  68. _nss_test2_init_hook (test_tables *t)
  69. {
  70. t->pwd_table = pwd_table2;
  71. t->grp_table = group_table_data2;
  72. }
  73. static int
  74. do_test (void)
  75. {
  76. struct passwd *pw;
  77. struct group *gr;
  78. struct hostent *he;
  79. char buf1[PATH_MAX];
  80. char buf2[PATH_MAX];
  81. support_need_proc ("Our xmkdirp fails if we can't map our uid, which requires /proc.");
  82. sprintf (buf1, "/subdir%s", support_slibdir_prefix);
  83. xmkdirp (buf1, 0777);
  84. /* Copy this DSO into the chroot so it *could* be loaded. */
  85. sprintf (buf1, "%s/libnss_files.so.2", support_slibdir_prefix);
  86. sprintf (buf2, "/subdir%s/libnss_files.so.2", support_slibdir_prefix);
  87. support_copy_file (buf1, buf2);
  88. /* Check we're using the "outer" nsswitch.conf. */
  89. /* This uses the test1 DSO. */
  90. pw = getpwnam ("test1");
  91. TEST_VERIFY (pw != NULL);
  92. if (pw)
  93. TEST_COMPARE (pw->pw_uid, 1234);
  94. /* This just loads the test2 DSO. */
  95. gr = getgrgid (5);
  96. TEST_VERIFY (gr != NULL);
  97. /* Change the root dir. */
  98. TEST_VERIFY (chroot ("/subdir") == 0);
  99. xchdir ("/");
  100. /* Check we're NOT using the "inner" nsswitch.conf. */
  101. /* Both DSOs are loaded, which is used? */
  102. pw = getpwnam ("test2");
  103. TEST_VERIFY (pw != NULL);
  104. if (pw)
  105. TEST_VERIFY (pw->pw_uid != 2468);
  106. /* We should still be using the old configuration. */
  107. pw = getpwnam ("test1");
  108. TEST_VERIFY (pw != NULL);
  109. if (pw)
  110. TEST_COMPARE (pw->pw_uid, 1234);
  111. gr = getgrgid (5);
  112. TEST_VERIFY (gr != NULL);
  113. gr = getgrnam ("name4");
  114. TEST_VERIFY (gr == NULL);
  115. /* hosts in the outer nsswitch is files; the inner one is test1.
  116. Verify that we're still using the outer nsswitch *and* that we
  117. can load the files DSO. */
  118. he = gethostbyname ("test2");
  119. TEST_VERIFY (he != NULL);
  120. return 0;
  121. }
  122. #include <support/test-driver.c>