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 | |
| 20 | /* |
| 21 | * Test Name: getresgid02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that getresgid() will be successful to get the real, effective |
| 25 | * and saved user ids after calling process invokes setregid() to change |
| 26 | * the effective/saved gids to that of specified user. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * getresgid() should return with 0 value and the real/effective/saved |
| 30 | * user ids should match the expected values. |
| 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> |
| 52 | * getresgid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 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 | |
| 68 | #include <stdio.h> |
| 69 | #include <unistd.h> |
| 70 | #include <sys/types.h> |
| 71 | #include <errno.h> |
| 72 | #include <fcntl.h> |
| 73 | #include <string.h> |
| 74 | #include <signal.h> |
| 75 | #include <pwd.h> |
| 76 | |
| 77 | #include "test.h" |
| 78 | #include "usctest.h" |
| 79 | |
| 80 | #define LTPUSER "nobody" |
| 81 | |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 82 | extern int getresgid(gid_t*, gid_t*, gid_t*); |
| 83 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | char *TCID="getresgid02"; /* Test program identifier. */ |
| 85 | int TST_TOTAL=1; /* Total number of test cases. */ |
| 86 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 87 | gid_t pr_gid, pe_gid, ps_gid; /* calling process real/effective/saved gid */ |
| 88 | |
| 89 | void setup(); /* Main setup function of test */ |
| 90 | void cleanup(); /* cleanup function for the test */ |
| 91 | |
| 92 | int |
| 93 | main(int ac, char **av) |
| 94 | { |
| 95 | int lc; /* loop counter */ |
| 96 | char *msg; /* message returned from parse_opts */ |
| 97 | gid_t real_gid, /* real/eff./saved user id from getresgid() */ |
| 98 | eff_gid, sav_gid; |
| 99 | |
| 100 | /* Parse standard options given to run the test. */ |
| 101 | msg = parse_opts(ac, av, (option_t *) NULL, NULL); |
| 102 | if (msg != (char *) NULL) { |
| 103 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 104 | tst_exit(); |
| 105 | } |
| 106 | |
| 107 | /* Perform global setup for test */ |
| 108 | setup(); |
| 109 | |
| 110 | /* Check looping state if -i option given */ |
| 111 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 112 | /* Reset Tst_count in case we are looping. */ |
| 113 | Tst_count=0; |
| 114 | |
| 115 | /* |
| 116 | * Call getresgid() to get the real/effective/saved |
| 117 | * user id's of the calling process after |
| 118 | * setregid() in setup. |
| 119 | */ |
| 120 | TEST(getresgid(&real_gid, &eff_gid, &sav_gid)); |
| 121 | |
| 122 | /* check return code of getresgid(2) */ |
| 123 | if (TEST_RETURN == -1) { |
| 124 | tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s", |
| 125 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 126 | continue; |
| 127 | } |
| 128 | /* |
| 129 | * Perform functional verification if test |
| 130 | * executed without (-f) option. |
| 131 | */ |
| 132 | if (STD_FUNCTIONAL_TEST) { |
| 133 | /* |
| 134 | * Verify the real/effective/saved gid |
| 135 | * values returned by getresgid with the |
| 136 | * expected values. |
| 137 | */ |
| 138 | if ((real_gid != pr_gid) || (eff_gid != pe_gid) || |
| 139 | (sav_gid != ps_gid)) { |
| 140 | tst_resm(TFAIL, "real:%d, effective:%d, " |
| 141 | "saved-user:%d ids differ", |
| 142 | real_gid, eff_gid, sav_gid); |
| 143 | } else { |
| 144 | tst_resm(TPASS, "Functionality of getresgid() " |
| 145 | "successful"); |
| 146 | } |
| 147 | } else { |
| 148 | tst_resm(TPASS, "call succeeded"); |
| 149 | } |
| 150 | } /* End for TEST_LOOPING */ |
| 151 | |
| 152 | /* Call cleanup() to undo setup done for the test. */ |
| 153 | cleanup(); |
| 154 | |
| 155 | /*NOTREACHED*/ |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame^] | 156 | return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } /* End main */ |
| 158 | |
| 159 | /* |
| 160 | * setup() - performs all ONE TIME setup for this test. |
| 161 | * Get the real/effective/saved user id of the calling process. |
| 162 | */ |
| 163 | void |
| 164 | setup() |
| 165 | { |
| 166 | struct passwd *user_id; /* passwd struct for test user*/ |
| 167 | |
| 168 | /* capture signals */ |
| 169 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 170 | |
| 171 | /* Pause if that option was specified */ |
| 172 | TEST_PAUSE; |
| 173 | |
| 174 | /* Check that the test process id is super/root */ |
| 175 | if (geteuid() != 0) { |
| 176 | tst_brkm(TBROK, cleanup, "Must be super/root for this test!"); |
| 177 | } |
| 178 | |
| 179 | /* Real user-id of the calling process */ |
| 180 | pr_gid = getgid(); |
| 181 | |
| 182 | /* Get effective gid of LTPUSER user from passwd file */ |
| 183 | if ((user_id = getpwnam(LTPUSER)) == NULL) { |
| 184 | tst_brkm(TBROK, cleanup, "getpwnam(%s) Failed", LTPUSER); |
| 185 | } |
| 186 | |
| 187 | /* Effective user-id of the test-user LTPUSER */ |
| 188 | pe_gid = user_id->pw_gid; |
| 189 | |
| 190 | /* Saved user-id of the test-user LTPUSER */ |
| 191 | ps_gid = user_id->pw_gid; |
| 192 | |
| 193 | /* |
| 194 | * Set the effective user-id of the process to that of |
| 195 | * test user LTPUSER |
| 196 | * The real user id remains same as of caller. |
| 197 | */ |
| 198 | if (setregid(-1, ps_gid) < 0) { |
| 199 | tst_brkm(TBROK, cleanup, |
| 200 | "setregid(-1, %d) Fails, errno:%d : %s", |
| 201 | ps_gid, errno, strerror(errno)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 207 | * completion or premature exit. |
| 208 | * Restore the test process gid to root. |
| 209 | */ |
| 210 | void |
| 211 | cleanup() |
| 212 | { |
| 213 | /* |
| 214 | * print timing stats if that option was specified. |
| 215 | * print errno log if that option was specified. |
| 216 | */ |
| 217 | TEST_CLEANUP; |
| 218 | |
| 219 | /* Reset the effective/saved gid of the calling process */ |
| 220 | if (setregid(-1, pr_gid) < 0) { |
| 221 | tst_brkm(TBROK, NULL, |
| 222 | "resetting process effective gid failed"); |
| 223 | } |
| 224 | |
| 225 | /* exit with return code appropriate for results */ |
| 226 | tst_exit(); |
| 227 | } |