blob: fd08074b7cdb91d2fe337865bd57b289fe504e9f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: setgroups02
plars865695b2001-08-27 22:15:12 +000022 *
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.
subrata_modakbdbaec52009-02-26 12:14:51 +000031 *
plars865695b2001-08-27 22:15:12 +000032 * 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * 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>
nstrazfa31d552002-05-14 16:50:06 +000052 * setgroups02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000053 * 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>
robbiew8541eeb2003-03-27 19:59:32 +000071#include <grp.h>
plars865695b2001-08-27 22:15:12 +000072
73#include "test.h"
74#include "usctest.h"
75
subrata_modak34a572a2008-08-27 12:02:42 +000076#include "compat_16.h"
77
plars865695b2001-08-27 22:15:12 +000078#define TESTUSER "nobody"
79
subrata_modak34a572a2008-08-27 12:02:42 +000080TCID_DEFINE(setgroups02); /* Test program identifier. */
subrata_modak56207ce2009-03-23 13:35:39 +000081int TST_TOTAL = 1; /* Total number of test conditions */
subrata_modak34a572a2008-08-27 12:02:42 +000082GID_T groups_list[NGROUPS]; /* Array to hold gids for getgroups() */
plars865695b2001-08-27 22:15:12 +000083
84struct passwd *user_info; /* struct. to hold test user info */
85void setup(); /* setup function for the test */
86void cleanup(); /* cleanup function for the test */
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000089{
subrata_modak56207ce2009-03-23 13:35:39 +000090 int lc, i; /* loop counters */
91 char *msg; /* message returned from parse_opts */
92 int gidsetsize = 1; /* only one GID, the GID of TESTUSER */
93 int PASS_FLAG = 0; /* used for checking group array */
subrata_modakbdbaec52009-02-26 12:14:51 +000094
plars865695b2001-08-27 22:15:12 +000095 /* Parse standard options given to run the test. */
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080096 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080097 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000098
plars865695b2001-08-27 22:15:12 +000099 setup();
100
plars865695b2001-08-27 22:15:12 +0000101 for (lc = 0; TEST_LOOPING(lc); lc++) {
102
plars865695b2001-08-27 22:15:12 +0000103 Tst_count = 0;
104
105 /*
106 * Call setgroups() to set supplimentary group IDs of
107 * the calling super-user process to gid of TESTUSER.
108 */
subrata_modak34a572a2008-08-27 12:02:42 +0000109 TEST(SETGROUPS(gidsetsize, groups_list));
subrata_modakbdbaec52009-02-26 12:14:51 +0000110
plars865695b2001-08-27 22:15:12 +0000111 if (TEST_RETURN == -1) {
112 tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
113 "errno=%d : %s", gidsetsize, TEST_ERRNO,
114 strerror(TEST_ERRNO));
115 continue;
116 }
117
118 /*
119 * Perform functional verification if test
120 * executed without (-f) option.
121 */
122 if (STD_FUNCTIONAL_TEST) {
123 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000124 * Call getgroups(2) to verify that
plars865695b2001-08-27 22:15:12 +0000125 * setgroups(2) successfully set the
126 * supp. gids of TESTUSER.
127 */
128 groups_list[0] = '\0';
subrata_modak34a572a2008-08-27 12:02:42 +0000129 if (GETGROUPS(gidsetsize, groups_list) < 0) {
plars865695b2001-08-27 22:15:12 +0000130 tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
131 "error=%d", errno);
132 }
subrata_modak56207ce2009-03-23 13:35:39 +0000133 for (i = 0; i < NGROUPS; i++) {
134 if (groups_list[i] == user_info->pw_gid) {
135 tst_resm(TPASS,
136 "Functionality of setgroups"
137 "(%d, groups_list) successful",
138 gidsetsize);
139 PASS_FLAG = 1;
140 }
robbiew9006bce2004-03-02 19:22:08 +0000141 }
142 if (PASS_FLAG == 0) {
143 tst_resm(TFAIL, "Supplimentary gid %d not set "
144 "for the process", user_info->pw_gid);
plars865695b2001-08-27 22:15:12 +0000145 }
146 } else {
147 tst_resm(TPASS, "call succeeded");
148 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800149 }
plars865695b2001-08-27 22:15:12 +0000150
plars865695b2001-08-27 22:15:12 +0000151 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800152 tst_exit();
153 tst_exit();
plars865695b2001-08-27 22:15:12 +0000154
plars865695b2001-08-27 22:15:12 +0000155}
156
157/*
158 * setup() - performs all ONE TIME setup for this test.
159 *
160 * Make sure the test process uid is root.
161 * Get the supplimentrary group id of test user from /etc/passwd file.
162 */
subrata_modak56207ce2009-03-23 13:35:39 +0000163void setup()
plars865695b2001-08-27 22:15:12 +0000164{
Garrett Cooper2c282152010-12-16 00:55:50 -0800165
plars865695b2001-08-27 22:15:12 +0000166 tst_sig(NOFORK, DEF_HANDLER, cleanup);
167
168 /* Make sure the calling process is super-user only */
169 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800170 tst_brkm(TBROK, NULL, "Must be ROOT to run this test.");
plars865695b2001-08-27 22:15:12 +0000171 }
172
plars865695b2001-08-27 22:15:12 +0000173 TEST_PAUSE;
174
175 /* Get the group id info. of TESTUSER from /etc/passwd */
176 if ((user_info = getpwnam(TESTUSER)) == NULL) {
177 tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
178 }
179
subrata_modak07af28f2008-09-10 08:59:40 +0000180 if (!GID_SIZE_CHECK(user_info->pw_gid)) {
subrata_modak34a572a2008-08-27 12:02:42 +0000181 tst_brkm(TBROK,
182 cleanup,
183 "gid returned from getpwnam is too large for testing setgroups16");
184 }
subrata_modak56207ce2009-03-23 13:35:39 +0000185
plars865695b2001-08-27 22:15:12 +0000186 groups_list[0] = user_info->pw_gid;
187}
188
189/*
190 * cleanup() - performs all ONE TIME cleanup for this test at
191 * completion or premature exit.
192 */
subrata_modak56207ce2009-03-23 13:35:39 +0000193void cleanup()
plars865695b2001-08-27 22:15:12 +0000194{
195 /*
196 * print timing stats if that option was specified.
197 */
198 TEST_CLEANUP;
199
Chris Dearmanec6edca2012-10-17 19:54:01 -0700200}