1/* Test that reloading is disabled after a chroot.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <nss.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <limits.h>
24#include <sys/types.h>
25#include <errno.h>
26#include <pwd.h>
27#include <grp.h>
28#include <unistd.h>
29#include <netdb.h>
30
31#include <support/support.h>
32#include <support/check.h>
33#include <support/xunistd.h>
34
35#include "nss_test.h"
36
37#ifndef PATH_MAX
38# define PATH_MAX 1024
39#endif
40
41static struct passwd pwd_table1[] =
42 {
43 PWD_N (1234, "test1"),
44 PWD_N (4321, "test2"),
45 PWD_LAST ()
46 };
47
48static const char *group_4[] = {
49 "alpha", "beta", "gamma", "fred", NULL
50};
51
52static struct group group_table_data1[] =
53 {
54 GRP (4),
55 GRP_LAST ()
56 };
57
58void
59_nss_test1_init_hook (test_tables *t)
60{
61 t->pwd_table = pwd_table1;
62 t->grp_table = group_table_data1;
63}
64
65static struct passwd pwd_table2[] =
66 {
67 PWD_N (5, "test1"),
68 PWD_N (2468, "test2"),
69 PWD_LAST ()
70 };
71
72static const char *group_5[] = {
73 "fred", NULL
74};
75
76static struct group group_table_data2[] =
77 {
78 GRP (5),
79 GRP_LAST ()
80 };
81
82void
83_nss_test2_init_hook (test_tables *t)
84{
85 t->pwd_table = pwd_table2;
86 t->grp_table = group_table_data2;
87}
88
89static int
90do_test (void)
91{
92 struct passwd *pw;
93 struct group *gr;
94 struct hostent *he;
95 char buf1[PATH_MAX];
96 char buf2[PATH_MAX];
97
98 support_need_proc (why_msg: "Our xmkdirp fails if we can't map our uid, which requires /proc.");
99
100 sprintf (buf1, "/subdir%s", support_slibdir_prefix);
101 xmkdirp (buf1, 0777);
102
103 /* Copy this DSO into the chroot so it *could* be loaded. */
104 sprintf (buf1, "%s/libnss_files.so.2", support_slibdir_prefix);
105 sprintf (buf2, "/subdir%s/libnss_files.so.2", support_slibdir_prefix);
106 support_copy_file (from: buf1, to: buf2);
107
108 /* Check we're using the "outer" nsswitch.conf. */
109
110 /* This uses the test1 DSO. */
111 pw = getpwnam (name: "test1");
112 TEST_VERIFY (pw != NULL);
113 if (pw)
114 TEST_COMPARE (pw->pw_uid, 1234);
115
116 /* This just loads the test2 DSO. */
117 gr = getgrgid (gid: 5);
118 TEST_VERIFY (gr != NULL);
119
120
121 /* Change the root dir. */
122
123 TEST_VERIFY (chroot ("/subdir") == 0);
124 chdir (path: "/");
125
126 /* Check we're NOT using the "inner" nsswitch.conf. */
127
128 /* Both DSOs are loaded, which is used? */
129 pw = getpwnam (name: "test2");
130 TEST_VERIFY (pw != NULL);
131 if (pw)
132 TEST_VERIFY (pw->pw_uid != 2468);
133
134 /* We should still be using the old configuration. */
135 pw = getpwnam (name: "test1");
136 TEST_VERIFY (pw != NULL);
137 if (pw)
138 TEST_COMPARE (pw->pw_uid, 1234);
139 gr = getgrgid (gid: 5);
140 TEST_VERIFY (gr != NULL);
141 gr = getgrnam (name: "name4");
142 TEST_VERIFY (gr == NULL);
143
144 /* hosts in the outer nsswitch is files; the inner one is test1.
145 Verify that we're still using the outer nsswitch *and* that we
146 can load the files DSO. */
147 he = gethostbyname (name: "test2");
148 TEST_VERIFY (he != NULL);
149
150 return 0;
151}
152
153#include <support/test-driver.c>
154

source code of glibc/nss/tst-reload2.c