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 #include "compat_16.h"
81
82 char *TCID = "getresgid03";
83 int TST_TOTAL = 1;
84 GID_T pr_gid, pe_gid, ps_gid; /* calling process real/effective/saved gid */
85
86 void setup(); /* Main setup function of test */
87 void cleanup(); /* cleanup function for the test */
88
main(int ac,char ** av)89 int main(int ac, char **av)
90 {
91 int lc;
92 GID_T real_gid, /* real/eff./saved user id from getresgid() */
93 eff_gid, sav_gid;
94
95 tst_parse_opts(ac, av, NULL, NULL);
96
97 setup();
98
99 for (lc = 0; TEST_LOOPING(lc); lc++) {
100
101 tst_count = 0;
102
103 /*
104 * Call getresgid() to get the real/effective/saved
105 * user id's of the calling process after
106 * setregid() in setup.
107 */
108 TEST(GETRESGID(cleanup, &real_gid, &eff_gid, &sav_gid));
109
110 if (TEST_RETURN == -1) {
111 tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s",
112 TEST_ERRNO, strerror(TEST_ERRNO));
113 continue;
114 }
115 /*
116 * Verify the real/effective/saved gid
117 * values returned by getresgid with the
118 * expected values.
119 */
120 if ((real_gid != pr_gid) || (eff_gid != pe_gid) ||
121 (sav_gid != ps_gid)) {
122 tst_resm(TFAIL, "real:%d, effective:%d, "
123 "saved-user:%d ids differ",
124 real_gid, eff_gid, sav_gid);
125 } else {
126 tst_resm(TPASS, "Functionality of getresgid() "
127 "successful");
128 }
129 }
130
131 cleanup();
132 tst_exit();
133 }
134
135 /*
136 * setup() - performs all ONE TIME setup for this test.
137 * Make sure test process gid is root.
138 * Get the real/effective/saved user id of the calling process.
139 * Get the user info. of test user "ltpuser1" from /etc/passwd file.
140 * Set the eff. user id of test process to that of "ltpuser1" user.
141 */
setup(void)142 void setup(void)
143 {
144 struct passwd *user_id; /* passwd struct for test user */
145
146 tst_require_root();
147
148 tst_sig(NOFORK, DEF_HANDLER, cleanup);
149
150 TEST_PAUSE;
151
152 /* Real user-id of the calling process */
153 pr_gid = getgid();
154
155 /* Saved user-id of the calling process. */
156 ps_gid = getegid();
157
158 /* Get effective gid of "ltpuser1" user from passwd file */
159 if ((user_id = getpwnam("nobody")) == NULL) {
160 tst_brkm(TBROK, cleanup,
161 "getpwnam(nobody) Failed, errno=%d", errno);
162 }
163
164 /* Effective user-id of the test-user "ltpuser1" */
165 pe_gid = user_id->pw_gid;
166
167 /*
168 * Set the effective user-id of the process to that of
169 * test user "ltpuser1".
170 * The real/saved user id remains same as of caller.
171 */
172 if (setresgid(-1, pe_gid, -1) < 0) {
173 tst_brkm(TBROK, cleanup,
174 "setresgid(-1, %d, -1) Fails, errno:%d : %s",
175 ps_gid, errno, strerror(errno));
176 }
177 }
178
179 /*
180 * cleanup() - performs all ONE TIME cleanup for this test at
181 * completion or premature exit.
182 */
cleanup(void)183 void cleanup(void)
184 {
185
186 /* Reset the effective/saved gid of the calling process */
187 if (setregid(-1, pr_gid) < 0) {
188 tst_brkm(TBROK, NULL, "resetting process effective gid failed");
189 }
190
191 }
192