plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: setgroups02 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 31 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 52 | * setgroups02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 71 | #include <grp.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | #include "test.h" |
| 74 | #include "usctest.h" |
| 75 | |
subrata_modak | 34a572a | 2008-08-27 12:02:42 +0000 | [diff] [blame] | 76 | #include "compat_16.h" |
| 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | #define TESTUSER "nobody" |
| 79 | |
subrata_modak | 34a572a | 2008-08-27 12:02:42 +0000 | [diff] [blame] | 80 | TCID_DEFINE(setgroups02); /* Test program identifier. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | int TST_TOTAL = 1; /* Total number of test conditions */ |
subrata_modak | 34a572a | 2008-08-27 12:02:42 +0000 | [diff] [blame] | 82 | GID_T groups_list[NGROUPS]; /* Array to hold gids for getgroups() */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | struct passwd *user_info; /* struct. to hold test user info */ |
| 85 | void setup(); /* setup function for the test */ |
| 86 | void cleanup(); /* cleanup function for the test */ |
| 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 96 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 97 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | setup(); |
| 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | 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_modak | 34a572a | 2008-08-27 12:02:42 +0000 | [diff] [blame] | 109 | TEST(SETGROUPS(gidsetsize, groups_list)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 124 | * Call getgroups(2) to verify that |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | * setgroups(2) successfully set the |
| 126 | * supp. gids of TESTUSER. |
| 127 | */ |
| 128 | groups_list[0] = '\0'; |
subrata_modak | 34a572a | 2008-08-27 12:02:42 +0000 | [diff] [blame] | 129 | if (GETGROUPS(gidsetsize, groups_list) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_brkm(TFAIL, cleanup, "getgroups() Fails, " |
| 131 | "error=%d", errno); |
| 132 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | 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 | } |
robbiew | 9006bce | 2004-03-02 19:22:08 +0000 | [diff] [blame] | 141 | } |
| 142 | if (PASS_FLAG == 0) { |
| 143 | tst_resm(TFAIL, "Supplimentary gid %d not set " |
| 144 | "for the process", user_info->pw_gid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
| 146 | } else { |
| 147 | tst_resm(TPASS, "call succeeded"); |
| 148 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 152 | tst_exit(); |
| 153 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | } |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 165 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 167 | |
| 168 | /* Make sure the calling process is super-user only */ |
| 169 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 170 | tst_brkm(TBROK, NULL, "Must be ROOT to run this test."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | 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_modak | 07af28f | 2008-09-10 08:59:40 +0000 | [diff] [blame] | 180 | if (!GID_SIZE_CHECK(user_info->pw_gid)) { |
subrata_modak | 34a572a | 2008-08-27 12:02:42 +0000 | [diff] [blame] | 181 | tst_brkm(TBROK, |
| 182 | cleanup, |
| 183 | "gid returned from getpwnam is too large for testing setgroups16"); |
| 184 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | { |
| 195 | /* |
| 196 | * print timing stats if that option was specified. |
| 197 | */ |
| 198 | TEST_CLEANUP; |
| 199 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame^] | 200 | } |