setregid04.c revision c6d09b660be500183f6970f2554693f93245e266
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 20/* 21 * NAME 22 * setregid04.c 23 * 24 * DESCRIPTION 25 * Test setregid() when executed by root. 26 * 27 * ALGORITHM 28 * 29 * Setup: 30 * Setup signal handling 31 * Get user information. 32 * Pause for SIGUSER1 if option specified. 33 * Setup test values. 34 * Loop if the proper options are given. 35 * For each test set execute the system call 36 * Check return code, if system call failed (return=-1) 37 * Log the errno and Issue a FAIL message. 38 * Otherwise, 39 * Verify the Functionality of system call 40 * if successful, 41 * Issue Functionality-Pass message. 42 * Otherwise, 43 * Issue Functionality-Fail message. 44 * Cleanup: 45 * Print errno log and/or timing stats if options given. 46 * 47 * USAGE: <for command-line> 48 * setregid04 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] 49 * where, -c n : Run n copies concurrently. 50 * -e : Turn on errno logging. 51 * -f : Turn off functionality Testing. 52 * -i n : Execute test n times. 53 * -I x : Execute test for x seconds. 54 * -P x : Pause for x seconds between iterations. 55 * -t : Turn on syscall timing. 56 * History 57 * 07/2001 John George 58 * -Ported 59 * 60 * Restrictions 61 * This test must be ran as root. 62 */ 63 64 65#include <pwd.h> 66#include <grp.h> 67#include <malloc.h> 68#include <string.h> 69#include <test.h> 70#include <usctest.h> 71#include <errno.h> 72 73extern int Tst_count; 74 75char *TCID = "setregid04"; 76gid_t users_gr_gid, root_gr_gid, daemon_gr_gid, bin_gr_gid; 77int neg_one = -1; 78int exp_enos[]={0}; 79 80struct group users, daemongr, root, bin; 81 82/* 83 * The following structure contains all test data. Each structure in the array 84 * is used for a separate test. The tests are executed in the for loop below. 85 */ 86 87struct test_data_t { 88 gid_t* real_gid; 89 gid_t* eff_gid; 90 struct group* exp_real_usr; 91 struct group* exp_eff_usr; 92 char * test_msg; 93} test_data[] = { 94 { &root_gr_gid, &root_gr_gid, &root, &root, "After setregid(root, root)," }, 95 { &users_gr_gid, &neg_one, &users, &root, "After setregid(users, -1)" }, 96 { &root_gr_gid, &neg_one, &root, &root, "After setregid(root,-1)," }, 97 { &neg_one, &neg_one, &root, &root, "After setregid(-1, -1)," }, 98 { &neg_one, &root_gr_gid, &root, &root, "After setregid(-1, root)" }, 99 { &root_gr_gid, &neg_one, &root, &root, "After setregid(root, -1)," }, 100 { &daemon_gr_gid, &users_gr_gid, &daemongr, &users, "After setregid(daemongr, users)" }, 101 { &neg_one, &neg_one, &daemongr, &users, "After setregid(-1, -1)" }, 102 { &neg_one, &users_gr_gid, &daemongr, &users, "After setregid(-1, users)" }, 103}; 104 105int TST_TOTAL = sizeof(test_data)/sizeof(test_data[0]); 106 107void setup(void); /* Setup function for the test */ 108void cleanup(void); /* Cleanup function for the test */ 109void gid_verify(struct group *ru, struct group *eu, char *when); 110 111main(int ac, char **av) 112{ 113 int lc; /* loop counter */ 114 char *msg; /* message returned from parse_opts */ 115 116 /* parse standard options */ 117 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != 118 (char *)NULL) { 119 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 120 tst_exit(); 121 /*NOTREACHED*/ 122 } 123 124 /* Perform global setup for test */ 125 setup(); 126 127 /* check looping state if -i option is given */ 128 for (lc = 0; TEST_LOOPING(lc); lc++) { 129 int i; 130 131 /* reset Tst_count in case we are looping */ 132 Tst_count = 0; 133 134 for (i = 0; i < TST_TOTAL; i++) { 135 /* Set the real or effective group id */ 136 TEST(setregid(*test_data[i].real_gid, 137 *test_data[i].eff_gid)); 138 139 if (TEST_RETURN == -1) { 140 TEST_ERROR_LOG(TEST_ERRNO); 141 tst_resm(TBROK, "setregid(%d, %d) failed", 142 test_data[i].real_gid, 143 test_data[i].eff_gid); 144 } else { 145 /* 146 * Perform functional verification if test 147 * executed without (-f) option. 148 */ 149 if (STD_FUNCTIONAL_TEST) { 150 gid_verify(test_data[i].exp_real_usr, 151 test_data[i].exp_eff_usr, 152 test_data[i].test_msg); 153 } else { 154 tst_resm(TPASS, "Call succeeded."); 155 } 156 } 157 } 158 } 159 cleanup(); 160 /*NOTREACHED*/ 161} 162 163/* 164 * setup() 165 * performs all ONE TIME setup for this test 166 */ 167void 168setup(void) 169{ 170 /* capture signals */ 171 tst_sig(FORK, DEF_HANDLER, cleanup); 172 173 /* Check that the test process id is super/root */ 174 if (geteuid() != 0) { 175 tst_brkm(TBROK, NULL, "Must be root for this test!"); 176 tst_exit(); 177 } 178 179 /* set the expected errnos... */ 180 TEST_EXP_ENOS(exp_enos); 181 182 root = *(getgrnam("root")); 183 root_gr_gid = root.gr_gid; 184 185 users = *( getgrnam("users")); 186 users_gr_gid = users.gr_gid; 187 188 daemongr = *(getgrnam("daemon")); 189 daemon_gr_gid = daemongr.gr_gid; 190 191 bin = *(getgrnam("bin")); 192 bin_gr_gid = bin.gr_gid; 193 194 /* Pause if that option was specified 195 * TEST_PAUSE contains the code to fork the test with the -c option. 196 */ 197 TEST_PAUSE; 198} 199 200/* 201 * cleanup() 202 * performs all ONE TIME cleanup for this test at 203 * completion or premature exit 204 */ 205void 206cleanup(void) 207{ 208 /* 209 * print timing stats if that option was specified. 210 * print errno log if that option was specified. 211 */ 212 TEST_CLEANUP; 213 214 /* exit with return code appropriate for results */ 215 tst_exit(); 216 /*NOTREACHED*/ 217} 218 219void 220gid_verify(struct group *rg, struct group *eg, char *when) 221{ 222 if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) { 223 tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d", 224 when, getgid(), getegid()); 225 tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d", 226 rg->gr_gid, eg->gr_gid); 227 } else { 228 tst_resm(TPASS, "real or effective gid was modified as " 229 "expected"); 230 } 231} 232 233