setregid04.c revision 605fa3362fd7cef0baa2131be32cf44661783d3e
1/* 2 * Copyright (c) International Business Machines Corp., 2001 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12 * the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 * Ported by John George 19 */ 20 21/* 22 * Test setregid() when executed by root. 23 */ 24 25#include <errno.h> 26#include <pwd.h> 27#include <grp.h> 28#include <stdlib.h> 29#include <string.h> 30 31#include "test.h" 32#include "usctest.h" 33#include "compat_16.h" 34 35TCID_DEFINE(setregid04); 36 37static gid_t neg_one = -1; 38 39static struct group users_gr, daemon_gr, root_gr, bin_gr; 40 41/* 42 * The following structure contains all test data. Each structure in the array 43 * is used for a separate test. The tests are executed in the for loop below. 44 */ 45 46struct test_data_t { 47 gid_t *real_gid; 48 gid_t *eff_gid; 49 struct group *exp_real_usr; 50 struct group *exp_eff_usr; 51 const char *test_msg; 52} test_data[] = { 53 { 54 &root_gr.gr_gid, &root_gr.gr_gid, &root_gr, &root_gr, 55 "After setregid(root, root),"}, { 56 &users_gr.gr_gid, &neg_one, &users_gr, &root_gr, 57 "After setregid(users, -1)"}, { 58 &root_gr.gr_gid, &neg_one, &root_gr, &root_gr, 59 "After setregid(root,-1),"}, { 60 &neg_one, &neg_one, &root_gr, &root_gr, 61 "After setregid(-1, -1),"}, { 62 &neg_one, &root_gr.gr_gid, &root_gr, &root_gr, 63 "After setregid(-1, root)"}, { 64 &root_gr.gr_gid, &neg_one, &root_gr, &root_gr, 65 "After setregid(root, -1),"}, { 66 &daemon_gr.gr_gid, &users_gr.gr_gid, &daemon_gr, &users_gr, 67 "After setregid(daemon, users)"}, { 68 &neg_one, &neg_one, &daemon_gr, &users_gr, 69 "After setregid(-1, -1)"}, { 70 &neg_one, &users_gr.gr_gid, &daemon_gr, &users_gr, 71 "After setregid(-1, users)"} 72}; 73 74int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]); 75 76static void setup(void); 77static void cleanup(void); 78static void gid_verify(struct group *ru, struct group *eu, const char *when); 79 80int main(int ac, char **av) 81{ 82 int lc; 83 const char *msg; 84 85 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 86 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 87 88 setup(); 89 90 for (lc = 0; TEST_LOOPING(lc); lc++) { 91 int i; 92 93 tst_count = 0; 94 95 for (i = 0; i < TST_TOTAL; i++) { 96 /* Set the real or effective group id */ 97 TEST(SETREGID(cleanup, *test_data[i].real_gid, 98 *test_data[i].eff_gid)); 99 100 if (TEST_RETURN == -1) { 101 tst_resm(TBROK, "setregid(%d, %d) failed", 102 *test_data[i].real_gid, 103 *test_data[i].eff_gid); 104 } else { 105 gid_verify(test_data[i].exp_real_usr, 106 test_data[i].exp_eff_usr, 107 test_data[i].test_msg); 108 } 109 } 110 } 111 112 cleanup(); 113 tst_exit(); 114} 115 116#define SAFE_GETGROUP(GROUPNAME) \ 117 if (getgrnam(#GROUPNAME) == NULL) { \ 118 tst_brkm(TBROK, NULL, "Couldn't find the `" #GROUPNAME "' group"); \ 119 } \ 120 GROUPNAME ## _gr = *(getgrnam(#GROUPNAME)); 121 122static void setup(void) 123{ 124 tst_require_root(NULL); 125 126 tst_sig(FORK, DEF_HANDLER, cleanup); 127 128 SAFE_GETGROUP(root); 129 SAFE_GETGROUP(users); 130 SAFE_GETGROUP(daemon); 131 SAFE_GETGROUP(bin); 132 133 TEST_PAUSE; 134} 135 136static void cleanup(void) 137{ 138} 139 140static void gid_verify(struct group *rg, struct group *eg, const char *when) 141{ 142 if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) { 143 tst_resm(TFAIL, "ERROR: %s real gid = %d; effective gid = %d", 144 when, getgid(), getegid()); 145 tst_resm(TINFO, "Expected: real gid = %d; effective gid = %d", 146 rg->gr_gid, eg->gr_gid); 147 } else { 148 tst_resm(TPASS, "real or effective gid was modified as " 149 "expected"); 150 } 151} 152