blob: 9b4b6b8ac781efa9f003091fbc1a6c671a9d621b [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
20/*
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.
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>
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
76#define TESTUSER "nobody"
77
nstrazfa31d552002-05-14 16:50:06 +000078char *TCID="setgroups02"; /* Test program identifier. */
plars865695b2001-08-27 22:15:12 +000079int TST_TOTAL=1; /* Total number of test conditions */
80extern int Tst_count; /* Test Case counter for tst_* routines */
81gid_t groups_list[NGROUPS]; /* Array to hold gids for getgroups() */
82
83struct passwd *user_info; /* struct. to hold test user info */
84void setup(); /* setup function for the test */
85void cleanup(); /* cleanup function for the test */
86
87int
88main(int ac, char **av)
89{
robbiew9006bce2004-03-02 19:22:08 +000090 int lc,i; /* loop counters */
plars865695b2001-08-27 22:15:12 +000091 char *msg; /* message returned from parse_opts */
92 int gidsetsize = NGROUPS; /* total groups */
robbiew9006bce2004-03-02 19:22:08 +000093 int PASS_FLAG = 0; /* used for checking group array */
plars865695b2001-08-27 22:15:12 +000094
95 /* Parse standard options given to run the test. */
96 msg = parse_opts(ac, av, (option_t *)NULL, NULL);
97 if (msg != (char *)NULL) {
98 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
99 }
100
101 /* Perform global setup for test */
102 setup();
103
104 /* Check looping state if -i option given */
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106
107 /* Reset Tst_count in case we are looping. */
108 Tst_count = 0;
109
110 /*
111 * Call setgroups() to set supplimentary group IDs of
112 * the calling super-user process to gid of TESTUSER.
113 */
114 TEST(setgroups(gidsetsize, groups_list));
115
116 /* check return code of setgroups(2) */
117 if (TEST_RETURN == -1) {
118 tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
119 "errno=%d : %s", gidsetsize, TEST_ERRNO,
120 strerror(TEST_ERRNO));
121 continue;
122 }
123
124 /*
125 * Perform functional verification if test
126 * executed without (-f) option.
127 */
128 if (STD_FUNCTIONAL_TEST) {
129 /*
130 * Call getgroups(2) to verify that
131 * setgroups(2) successfully set the
132 * supp. gids of TESTUSER.
133 */
134 groups_list[0] = '\0';
135 if (getgroups(gidsetsize, groups_list) < 0) {
136 tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
137 "error=%d", errno);
138 }
robbiew9006bce2004-03-02 19:22:08 +0000139 for (i=0;i<NGROUPS;i++) {
140 if (groups_list[i] == user_info->pw_gid) {
plars865695b2001-08-27 22:15:12 +0000141 tst_resm(TPASS, "Functionality of setgroups"
142 "(%d, groups_list) successful",
143 gidsetsize);
robbiew9006bce2004-03-02 19:22:08 +0000144 PASS_FLAG=1;
145 }
146 }
147 if (PASS_FLAG == 0) {
148 tst_resm(TFAIL, "Supplimentary gid %d not set "
149 "for the process", user_info->pw_gid);
plars865695b2001-08-27 22:15:12 +0000150 }
151 } else {
152 tst_resm(TPASS, "call succeeded");
153 }
154 } /* End for TEST_LOOPING */
155
156 /* Call cleanup() to undo setup done for the test. */
157 cleanup();
158
159 /*NOTREACHED*/
robbiew8541eeb2003-03-27 19:59:32 +0000160 return(0);
plars865695b2001-08-27 22:15:12 +0000161}
162
163/*
164 * setup() - performs all ONE TIME setup for this test.
165 *
166 * Make sure the test process uid is root.
167 * Get the supplimentrary group id of test user from /etc/passwd file.
168 */
169void
170setup()
171{
172 /* capture signals */
173 tst_sig(NOFORK, DEF_HANDLER, cleanup);
174
175 /* Make sure the calling process is super-user only */
176 if (geteuid() != 0) {
177 tst_brkm(TBROK, tst_exit, "Must be ROOT to run this test.");
178 }
179
180 /* Pause if that option was specified */
181 TEST_PAUSE;
182
183 /* Get the group id info. of TESTUSER from /etc/passwd */
184 if ((user_info = getpwnam(TESTUSER)) == NULL) {
185 tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
186 }
187
188 groups_list[0] = user_info->pw_gid;
189}
190
191/*
192 * cleanup() - performs all ONE TIME cleanup for this test at
193 * completion or premature exit.
194 */
195void
196cleanup()
197{
198 /*
199 * print timing stats if that option was specified.
200 */
201 TEST_CLEANUP;
202
203 /* exit with return code appropriate for results */
204 tst_exit();
205}