setregid04.c revision 43088e16aa60d69e3ec5a69cdd8bdd45b8891127
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#include <pwd.h> 65#include <grp.h> 66#include <malloc.h> 67#include <string.h> 68#include <test.h> 69#include <usctest.h> 70#include <errno.h> 71 72extern int Tst_count; 73 74char *TCID = "setregid04"; 75gid_t users_gr_gid, root_gr_gid, daemon_gr_gid, bin_gr_gid; 76gid_t neg_one = -1; 77int exp_enos[] = { 0 }; 78 79/* Avoid clashing with daemon in unistd.h. */ 80struct group users_gr, daemon_gr, root_gr, bin_gr; 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 const char *test_msg; 93} test_data[] = { 94 { 95 &root_gr_gid, &root_gr_gid, &root_gr, &root_gr, 96 "After setregid(root, root)," 97 }, { 98 &users_gr_gid, &neg_one, &users_gr, &root_gr, 99 "After setregid(users, -1)" 100 }, { 101 &root_gr_gid, &neg_one, &root_gr, &root_gr, 102 "After setregid(root,-1)," 103 }, { 104 &neg_one, &neg_one, &root_gr, &root_gr, 105 "After setregid(-1, -1)," 106 }, { 107 &neg_one, &root_gr_gid, &root_gr, &root_gr, 108 "After setregid(-1, root)" 109 }, { 110 &root_gr_gid, &neg_one, &root_gr, &root_gr, 111 "After setregid(root, -1)," 112 }, { 113 &daemon_gr_gid, &users_gr_gid, &daemon_gr, &users_gr, 114 "After setregid(daemon, users)" 115 }, { 116 &neg_one, &neg_one, &daemon_gr, &users_gr, 117 "After setregid(-1, -1)" 118 }, { 119 &neg_one, &users_gr_gid, &daemon_gr, &users_gr, 120 "After setregid(-1, users)" 121 } 122}; 123 124int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); 125 126void setup(void); /* Setup function for the test */ 127void cleanup(void); /* Cleanup function for the test */ 128void gid_verify(struct group *ru, struct group *eu, const char *when); 129 130int main(int ac, char **av) 131{ 132 int lc; /* loop counter */ 133 char *msg; /* message returned from parse_opts */ 134 135 /* parse standard options */ 136 if ((msg = parse_opts(ac, av, NULL) { 137 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 138 tst_exit(); 139 /*NOTREACHED*/ 140 } 141 142 /* Perform global setup for test */ 143 setup(); 144 145 /* check looping state if -i option is given */ 146 for (lc = 0; TEST_LOOPING(lc); lc++) { 147 int i; 148 149 /* reset Tst_count in case we are looping */ 150 Tst_count = 0; 151 152 for (i = 0; i < TST_TOTAL; i++) { 153 /* Set the real or effective group id */ 154 TEST(setregid(*test_data[i].real_gid, 155 *test_data[i].eff_gid)); 156 157 if (TEST_RETURN == -1) { 158 TEST_ERROR_LOG(TEST_ERRNO); 159 tst_resm(TBROK, "setregid(%d, %d) failed", 160 *test_data[i].real_gid, 161 *test_data[i].eff_gid); 162 } else { 163 /* 164 * Perform functional verification if test 165 * executed without (-f) option. 166 */ 167 if (STD_FUNCTIONAL_TEST) { 168 gid_verify(test_data[i].exp_real_usr, 169 test_data[i].exp_eff_usr, 170 test_data[i].test_msg); 171 } else { 172 tst_resm(TPASS, "Call succeeded."); 173 } 174 } 175 } 176 } 177 cleanup(); 178 /*NOTREACHED*/ 179 return 0; 180} 181 182#define SAFE_GETGROUP(GROUPNAME) \ 183 if ((junk = getgrnam(#GROUPNAME)) == NULL) { \ 184 tst_brkm(TBROK, NULL, "Couldn't find the `" #GROUPNAME "' group"); \ 185 tst_exit(); \ 186 } \ 187 memcpy((void*) &GROUPNAME ## _gr, (const void*) junk, sizeof(struct group)); \ 188 GROUPNAME ## _gr_gid = GROUPNAME ## _gr.gr_gid 189 190/* 191 * setup() 192 * performs all ONE TIME setup for this test 193 */ 194void setup(void) 195{ 196 struct group *junk; 197 198 /* capture signals */ 199 tst_sig(FORK, DEF_HANDLER, cleanup); 200 201 /* Check that the test process id is super/root */ 202 if (geteuid() != 0) { 203 tst_brkm(TBROK, NULL, "Must be root for this test!"); 204 tst_exit(); 205 } 206 207 /* set the expected errnos... */ 208 TEST_EXP_ENOS(exp_enos); 209 210 SAFE_GETGROUP(root); 211 SAFE_GETGROUP(users); 212 SAFE_GETGROUP(daemon); 213 SAFE_GETGROUP(bin); 214 215 /* Pause if that option was specified 216 * TEST_PAUSE contains the code to fork the test with the -c option. 217 */ 218 TEST_PAUSE; 219} 220 221/* 222 * cleanup() 223 * performs all ONE TIME cleanup for this test at 224 * completion or premature exit 225 */ 226void cleanup(void) 227{ 228 /* 229 * print timing stats if that option was specified. 230 * print errno log if that option was specified. 231 */ 232 TEST_CLEANUP; 233 234 /* exit with return code appropriate for results */ 235 tst_exit(); 236 /*NOTREACHED*/ 237} 238 239void gid_verify(struct group *rg, struct group *eg, const char *when) 240{ 241 if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) { 242 tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d", 243 when, getgid(), getegid()); 244 tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d", 245 rg->gr_gid, eg->gr_gid); 246 } else { 247 tst_resm(TPASS, "real or effective gid was modified as " 248 "expected"); 249 } 250} 251