blob: 0f346a26d4aa272e7d4ca462e2ff903789050159 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000074
subrata_modak34a572a2008-08-27 12:02:42 +000075#include "compat_16.h"
76
plars865695b2001-08-27 22:15:12 +000077#define TESTUSER "nobody"
78
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020079TCID_DEFINE(setgroups02);
subrata_modak56207ce2009-03-23 13:35:39 +000080int TST_TOTAL = 1; /* Total number of test conditions */
subrata_modak34a572a2008-08-27 12:02:42 +000081GID_T groups_list[NGROUPS]; /* Array to hold gids for getgroups() */
plars865695b2001-08-27 22:15:12 +000082
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
subrata_modak56207ce2009-03-23 13:35:39 +000087int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000088{
subrata_modak56207ce2009-03-23 13:35:39 +000089 int lc, i; /* loop counters */
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020090 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000091 int gidsetsize = 1; /* only one GID, the GID of TESTUSER */
92 int PASS_FLAG = 0; /* used for checking group array */
subrata_modakbdbaec52009-02-26 12:14:51 +000093
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080094 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080095 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000096
plars865695b2001-08-27 22:15:12 +000097 setup();
98
plars865695b2001-08-27 22:15:12 +000099 for (lc = 0; TEST_LOOPING(lc); lc++) {
100
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000102
103 /*
104 * Call setgroups() to set supplimentary group IDs of
105 * the calling super-user process to gid of TESTUSER.
106 */
Stanislav Kholmanskikhce0dffe2013-09-05 11:01:48 +0400107 TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
subrata_modakbdbaec52009-02-26 12:14:51 +0000108
plars865695b2001-08-27 22:15:12 +0000109 if (TEST_RETURN == -1) {
110 tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
111 "errno=%d : %s", gidsetsize, TEST_ERRNO,
112 strerror(TEST_ERRNO));
113 continue;
114 }
115
116 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200117 * Call getgroups(2) to verify that
118 * setgroups(2) successfully set the
119 * supp. gids of TESTUSER.
plars865695b2001-08-27 22:15:12 +0000120 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 groups_list[0] = '\0';
122 if (GETGROUPS(cleanup, gidsetsize, groups_list) < 0) {
123 tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
124 "error=%d", errno);
125 }
126 for (i = 0; i < NGROUPS; i++) {
127 if (groups_list[i] == user_info->pw_gid) {
128 tst_resm(TPASS,
129 "Functionality of setgroups"
130 "(%d, groups_list) successful",
131 gidsetsize);
132 PASS_FLAG = 1;
plars865695b2001-08-27 22:15:12 +0000133 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200134 }
135 if (PASS_FLAG == 0) {
136 tst_resm(TFAIL, "Supplimentary gid %d not set "
137 "for the process", user_info->pw_gid);
plars865695b2001-08-27 22:15:12 +0000138 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800139 }
plars865695b2001-08-27 22:15:12 +0000140
plars865695b2001-08-27 22:15:12 +0000141 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800142 tst_exit();
plars865695b2001-08-27 22:15:12 +0000143}
144
145/*
146 * setup() - performs all ONE TIME setup for this test.
147 *
148 * Make sure the test process uid is root.
149 * Get the supplimentrary group id of test user from /etc/passwd file.
150 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400151void setup(void)
plars865695b2001-08-27 22:15:12 +0000152{
Garrett Cooper2c282152010-12-16 00:55:50 -0800153
plars865695b2001-08-27 22:15:12 +0000154 tst_sig(NOFORK, DEF_HANDLER, cleanup);
155
156 /* Make sure the calling process is super-user only */
157 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800158 tst_brkm(TBROK, NULL, "Must be ROOT to run this test.");
plars865695b2001-08-27 22:15:12 +0000159 }
160
plars865695b2001-08-27 22:15:12 +0000161 TEST_PAUSE;
162
163 /* Get the group id info. of TESTUSER from /etc/passwd */
164 if ((user_info = getpwnam(TESTUSER)) == NULL) {
165 tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
166 }
167
subrata_modak07af28f2008-09-10 08:59:40 +0000168 if (!GID_SIZE_CHECK(user_info->pw_gid)) {
subrata_modak34a572a2008-08-27 12:02:42 +0000169 tst_brkm(TBROK,
170 cleanup,
171 "gid returned from getpwnam is too large for testing setgroups16");
172 }
subrata_modak56207ce2009-03-23 13:35:39 +0000173
plars865695b2001-08-27 22:15:12 +0000174 groups_list[0] = user_info->pw_gid;
175}
176
177/*
178 * cleanup() - performs all ONE TIME cleanup for this test at
179 * completion or premature exit.
180 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400181void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000182{
plars865695b2001-08-27 22:15:12 +0000183
Chris Dearmanec6edca2012-10-17 19:54:01 -0700184}