blob: 1675f6be7f363ab438605f12c444d7e505efc977 [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{
90 int lc; /* loop counter */
91 char *msg; /* message returned from parse_opts */
92 int gidsetsize = NGROUPS; /* total groups */
93
94 /* Parse standard options given to run the test. */
95 msg = parse_opts(ac, av, (option_t *)NULL, NULL);
96 if (msg != (char *)NULL) {
97 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
98 }
99
100 /* Perform global setup for test */
101 setup();
102
103 /* Check looping state if -i option given */
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105
106 /* Reset Tst_count in case we are looping. */
107 Tst_count = 0;
108
109 /*
110 * Call setgroups() to set supplimentary group IDs of
111 * the calling super-user process to gid of TESTUSER.
112 */
113 TEST(setgroups(gidsetsize, groups_list));
114
115 /* check return code of setgroups(2) */
116 if (TEST_RETURN == -1) {
117 tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
118 "errno=%d : %s", gidsetsize, TEST_ERRNO,
119 strerror(TEST_ERRNO));
120 continue;
121 }
122
123 /*
124 * Perform functional verification if test
125 * executed without (-f) option.
126 */
127 if (STD_FUNCTIONAL_TEST) {
128 /*
129 * Call getgroups(2) to verify that
130 * setgroups(2) successfully set the
131 * supp. gids of TESTUSER.
132 */
133 groups_list[0] = '\0';
134 if (getgroups(gidsetsize, groups_list) < 0) {
135 tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
136 "error=%d", errno);
137 }
138 if (groups_list[0] != user_info->pw_gid) {
139 tst_resm(TFAIL, "Supplimentary gid %d not set "
140 "for the process", user_info->pw_gid);
141 } else {
142 tst_resm(TPASS, "Functionality of setgroups"
143 "(%d, groups_list) successful",
144 gidsetsize);
145 }
146 } else {
147 tst_resm(TPASS, "call succeeded");
148 }
149 } /* End for TEST_LOOPING */
150
151 /* Call cleanup() to undo setup done for the test. */
152 cleanup();
153
154 /*NOTREACHED*/
robbiew8541eeb2003-03-27 19:59:32 +0000155 return(0);
plars865695b2001-08-27 22:15:12 +0000156}
157
158/*
159 * setup() - performs all ONE TIME setup for this test.
160 *
161 * Make sure the test process uid is root.
162 * Get the supplimentrary group id of test user from /etc/passwd file.
163 */
164void
165setup()
166{
167 /* capture signals */
168 tst_sig(NOFORK, DEF_HANDLER, cleanup);
169
170 /* Make sure the calling process is super-user only */
171 if (geteuid() != 0) {
172 tst_brkm(TBROK, tst_exit, "Must be ROOT to run this test.");
173 }
174
175 /* Pause if that option was specified */
176 TEST_PAUSE;
177
178 /* Get the group id info. of TESTUSER from /etc/passwd */
179 if ((user_info = getpwnam(TESTUSER)) == NULL) {
180 tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
181 }
182
183 groups_list[0] = user_info->pw_gid;
184}
185
186/*
187 * cleanup() - performs all ONE TIME cleanup for this test at
188 * completion or premature exit.
189 */
190void
191cleanup()
192{
193 /*
194 * print timing stats if that option was specified.
195 */
196 TEST_CLEANUP;
197
198 /* exit with return code appropriate for results */
199 tst_exit();
200}