• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: getresgid03
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 setresgid() to change
26  *  the effective gid to that of specified user.
27  *
28  * Expected Result:
29  *  getresgid() should return with 0 value and the effective user id
30  *  should match the egid of specified user, real/saved user ids should
31  *  remain unchanged.
32  *
33  * Algorithm:
34  *  Setup:
35  *   Setup signal handling.
36  *   Pause for SIGUSR1 if option specified.
37  *
38  *  Test:
39  *   Loop if the proper options are given.
40  *   Execute system call
41  *   Check return code, if system call failed (return=-1)
42  *	Log the errno and Issue a FAIL message.
43  *   Otherwise,
44  *	Verify the Functionality of system call
45  *      if successful,
46  *		Issue Functionality-Pass message.
47  *      Otherwise,
48  *		Issue Functionality-Fail message.
49  *  Cleanup:
50  *   Print errno log and/or timing stats if options given
51  *
52  * Usage:  <for command-line>
53  *  getresgid03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54  *     where,  -c n : Run n copies concurrently.
55  *             -f   : Turn off functionality Testing.
56  *	       -i n : Execute test n times.
57  *	       -I x : Execute test for x seconds.
58  *	       -P x : Pause for x seconds between iterations.
59  *	       -t   : Turn on syscall timing.
60  *
61  * HISTORY
62  *	07/2001 Ported by Wayne Boyer
63  *
64  * RESTRICTIONS:
65  *  This test should be run by 'super-user' (root) only.
66  *
67  */
68 #define _GNU_SOURCE 1
69 
70 #include <stdio.h>
71 #include <unistd.h>
72 #include <sys/types.h>
73 #include <errno.h>
74 #include <fcntl.h>
75 #include <string.h>
76 #include <signal.h>
77 #include <pwd.h>
78 
79 #include "test.h"
80 
81 extern int getresgid(gid_t *, gid_t *, gid_t *);
82 extern int setresgid(gid_t, gid_t, gid_t);
83 
84 char *TCID = "getresgid03";
85 int TST_TOTAL = 1;
86 gid_t pr_gid, pe_gid, ps_gid;	/* calling process real/effective/saved gid */
87 
88 void setup();			/* Main setup function of test */
89 void cleanup();			/* cleanup function for the test */
90 
main(int ac,char ** av)91 int main(int ac, char **av)
92 {
93 	int lc;
94 	gid_t real_gid,		/* real/eff./saved user id from getresgid() */
95 	 eff_gid, sav_gid;
96 
97 	tst_parse_opts(ac, av, NULL, NULL);
98 
99 	setup();
100 
101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
102 
103 		tst_count = 0;
104 
105 		/*
106 		 * Call getresgid() to get the real/effective/saved
107 		 * user id's of the calling process after
108 		 * setregid() in setup.
109 		 */
110 		TEST(getresgid(&real_gid, &eff_gid, &sav_gid));
111 
112 		if (TEST_RETURN == -1) {
113 			tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s",
114 				 TEST_ERRNO, strerror(TEST_ERRNO));
115 			continue;
116 		}
117 		/*
118 		 * Verify the real/effective/saved gid
119 		 * values returned by getresgid with the
120 		 * expected values.
121 		 */
122 		if ((real_gid != pr_gid) || (eff_gid != pe_gid) ||
123 		    (sav_gid != ps_gid)) {
124 			tst_resm(TFAIL, "real:%d, effective:%d, "
125 				 "saved-user:%d ids differ",
126 				 real_gid, eff_gid, sav_gid);
127 		} else {
128 			tst_resm(TPASS, "Functionality of getresgid() "
129 				 "successful");
130 		}
131 	}
132 
133 	cleanup();
134 	tst_exit();
135 }
136 
137 /*
138  * setup() - performs all ONE TIME setup for this test.
139  *	     Make sure test process gid is root.
140  *	     Get the real/effective/saved user id of the calling process.
141  *	     Get the user info. of test user "ltpuser1" from /etc/passwd file.
142  *	     Set the eff. user id of test process to that of "ltpuser1" user.
143  */
setup(void)144 void setup(void)
145 {
146 	struct passwd *user_id;	/* passwd struct for test user */
147 
148 	tst_require_root();
149 
150 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
151 
152 	TEST_PAUSE;
153 
154 	/* Real user-id of the calling process */
155 	pr_gid = getgid();
156 
157 	/* Saved user-id of the calling process. */
158 	ps_gid = getegid();
159 
160 	/* Get effective gid of "ltpuser1" user from passwd file */
161 	if ((user_id = getpwnam("nobody")) == NULL) {
162 		tst_brkm(TBROK, cleanup,
163 			 "getpwnam(nobody) Failed, errno=%d", errno);
164 	}
165 
166 	/* Effective user-id of the test-user "ltpuser1" */
167 	pe_gid = user_id->pw_gid;
168 
169 	/*
170 	 * Set the effective user-id of the process to that of
171 	 * test user "ltpuser1".
172 	 * The real/saved  user id remains same as of caller.
173 	 */
174 	if (setresgid(-1, pe_gid, -1) < 0) {
175 		tst_brkm(TBROK, cleanup,
176 			 "setresgid(-1, %d, -1) Fails, errno:%d : %s",
177 			 ps_gid, errno, strerror(errno));
178 	}
179 }
180 
181 /*
182  * cleanup() - performs all ONE TIME cleanup for this test at
183  *             completion or premature exit.
184  */
cleanup(void)185 void cleanup(void)
186 {
187 
188 	/* Reset the effective/saved gid of the calling process */
189 	if (setregid(-1, pr_gid) < 0) {
190 		tst_brkm(TBROK, NULL, "resetting process effective gid failed");
191 	}
192 
193 }
194