Home | History | Annotate | Download | only in setgroups
      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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * Test Name: setgroups02
     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.
     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  *  setgroups02 [-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 #include <sys/types.h>
     68 #include <unistd.h>
     69 #include <errno.h>
     70 #include <pwd.h>
     71 #include <grp.h>
     72 #include <sys/param.h>
     73 
     74 #include "test.h"
     75 
     76 #include "compat_16.h"
     77 
     78 #define TESTUSER	"nobody"
     79 
     80 TCID_DEFINE(setgroups02);
     81 int TST_TOTAL = 1;		/* Total number of test conditions */
     82 GID_T groups_list[NGROUPS];	/* Array to hold gids for getgroups() */
     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 
     88 int main(int ac, char **av)
     89 {
     90 	int lc, i;
     91 	int gidsetsize = 1;	/* only one GID, the GID of TESTUSER */
     92 	int PASS_FLAG = 0;	/* used for checking group array */
     93 
     94 	tst_parse_opts(ac, av, NULL, NULL);
     95 
     96 	setup();
     97 
     98 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     99 
    100 		tst_count = 0;
    101 
    102 		/*
    103 		 * Call setgroups() to set supplimentary group IDs of
    104 		 * the calling super-user process to gid of TESTUSER.
    105 		 */
    106 		TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
    107 
    108 		if (TEST_RETURN == -1) {
    109 			tst_resm(TFAIL, "setgroups(%d, groups_list) Failed, "
    110 				 "errno=%d : %s", gidsetsize, TEST_ERRNO,
    111 				 strerror(TEST_ERRNO));
    112 			continue;
    113 		}
    114 
    115 		/*
    116 		 * Call getgroups(2) to verify that
    117 		 * setgroups(2) successfully set the
    118 		 * supp. gids of TESTUSER.
    119 		 */
    120 		groups_list[0] = '\0';
    121 		if (GETGROUPS(cleanup, gidsetsize, groups_list) < 0) {
    122 			tst_brkm(TFAIL, cleanup, "getgroups() Fails, "
    123 				 "error=%d", errno);
    124 		}
    125 		for (i = 0; i < NGROUPS; i++) {
    126 			if (groups_list[i] == user_info->pw_gid) {
    127 				tst_resm(TPASS,
    128 					 "Functionality of setgroups"
    129 					 "(%d, groups_list) successful",
    130 					 gidsetsize);
    131 				PASS_FLAG = 1;
    132 			}
    133 		}
    134 		if (PASS_FLAG == 0) {
    135 			tst_resm(TFAIL, "Supplimentary gid %d not set "
    136 				 "for the process", user_info->pw_gid);
    137 		}
    138 	}
    139 
    140 	cleanup();
    141 	tst_exit();
    142 }
    143 
    144 /*
    145  * setup() - performs all ONE TIME setup for this test.
    146  *
    147  *  Make sure the test process uid is root.
    148  *  Get the supplimentrary group id of test user from /etc/passwd file.
    149  */
    150 void setup(void)
    151 {
    152 
    153 	tst_require_root();
    154 
    155 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    156 
    157 	TEST_PAUSE;
    158 
    159 	/* Get the group id info. of TESTUSER from /etc/passwd */
    160 	if ((user_info = getpwnam(TESTUSER)) == NULL) {
    161 		tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
    162 	}
    163 
    164 	if (!GID_SIZE_CHECK(user_info->pw_gid)) {
    165 		tst_brkm(TBROK,
    166 			 cleanup,
    167 			 "gid returned from getpwnam is too large for testing setgroups16");
    168 	}
    169 
    170 	groups_list[0] = user_info->pw_gid;
    171 }
    172 
    173 /*
    174  * cleanup() - performs all ONE TIME cleanup for this test at
    175  *	       completion or premature exit.
    176  */
    177 void cleanup(void)
    178 {
    179 
    180 }
    181