1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * Test Name: setgroups02
22 *
23 * Test Description:
24 * Verify that, only root process can invoke setgroups() system call to
25 * set the supplementary group IDs of the process.
26 *
27 * Expected Result:
28 * The call succeeds in setting all the supplementary group IDs of the
29 * calling process. The new group should be set in the process supplemental
30 * group list.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
41 * Log the errno and Issue a FAIL message.
42 * Otherwise,
43 * Verify the Functionality of system call
44 * if successful,
45 * Issue Functionality-Pass message.
46 * Otherwise,
47 * Issue Functionality-Fail message.
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 *
51 * Usage: <for command-line>
52 * setgroups02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
53 * where, -c n : Run n copies concurrently.
54 * -f : Turn off functionality Testing.
55 * -i n : Execute test n times.
56 * -I x : Execute test for x seconds.
57 * -P x : Pause for x seconds between iterations.
58 * -t : Turn on syscall timing.
59 *
60 * HISTORY
61 * 07/2001 Ported by Wayne Boyer
62 *
63 * RESTRICTIONS:
64 * This test should be run by 'super-user' (root) only.
65 *
66 */
67 #include <sys/types.h>
68 #include <unistd.h>
69 #include <errno.h>
70 #include <pwd.h>
71 #include <grp.h>
72
73 #include "test.h"
74
75 #include "compat_16.h"
76
77 #define TESTUSER "nobody"
78
79 TCID_DEFINE(setgroups02);
80 int TST_TOTAL = 1; /* Total number of test conditions */
81 GID_T groups_list[NGROUPS]; /* Array to hold gids for getgroups() */
82
83 struct passwd *user_info; /* struct. to hold test user info */
84 void setup(); /* setup function for the test */
85 void cleanup(); /* cleanup function for the test */
86
main(int ac,char ** av)87 int main(int ac, char **av)
88 {
89 int lc, i;
90 int gidsetsize = 1; /* only one GID, the GID of TESTUSER */
91 int PASS_FLAG = 0; /* used for checking group array */
92
93 tst_parse_opts(ac, av, NULL, NULL);
94
95 setup();
96
97 for (lc = 0; TEST_LOOPING(lc); lc++) {
98
99 tst_count = 0;
100
101 /*
102 * Call setgroups() to set supplimentary group IDs of
103 * the calling super-user process to gid of TESTUSER.
104 */
105 TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
106
107 if (TEST_RETURN == -1) {
108 tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
109 "errno=%d : %s", gidsetsize, TEST_ERRNO,
110 strerror(TEST_ERRNO));
111 continue;
112 }
113
114 /*
115 * Call getgroups(2) to verify that
116 * setgroups(2) successfully set the
117 * supp. gids of TESTUSER.
118 */
119 groups_list[0] = '\0';
120 if (GETGROUPS(cleanup, gidsetsize, groups_list) < 0) {
121 tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
122 "error=%d", errno);
123 }
124 for (i = 0; i < NGROUPS; i++) {
125 if (groups_list[i] == user_info->pw_gid) {
126 tst_resm(TPASS,
127 "Functionality of setgroups"
128 "(%d, groups_list) successful",
129 gidsetsize);
130 PASS_FLAG = 1;
131 }
132 }
133 if (PASS_FLAG == 0) {
134 tst_resm(TFAIL, "Supplimentary gid %d not set "
135 "for the process", user_info->pw_gid);
136 }
137 }
138
139 cleanup();
140 tst_exit();
141 }
142
143 /*
144 * setup() - performs all ONE TIME setup for this test.
145 *
146 * Make sure the test process uid is root.
147 * Get the supplimentrary group id of test user from /etc/passwd file.
148 */
setup(void)149 void setup(void)
150 {
151
152 tst_require_root();
153
154 tst_sig(NOFORK, DEF_HANDLER, cleanup);
155
156 TEST_PAUSE;
157
158 /* Get the group id info. of TESTUSER from /etc/passwd */
159 if ((user_info = getpwnam(TESTUSER)) == NULL) {
160 tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
161 }
162
163 if (!GID_SIZE_CHECK(user_info->pw_gid)) {
164 tst_brkm(TBROK,
165 cleanup,
166 "gid returned from getpwnam is too large for testing setgroups16");
167 }
168
169 groups_list[0] = user_info->pw_gid;
170 }
171
172 /*
173 * cleanup() - performs all ONE TIME cleanup for this test at
174 * completion or premature exit.
175 */
cleanup(void)176 void cleanup(void)
177 {
178
179 }
180