1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Test Name: getresgid03
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Test Description:
24f08c3bdfSopenharmony_ci *  Verify that getresgid() will be successful to get the real, effective
25f08c3bdfSopenharmony_ci *  and saved user ids after calling process invokes setresgid() to change
26f08c3bdfSopenharmony_ci *  the effective gid to that of specified user.
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * Expected Result:
29f08c3bdfSopenharmony_ci *  getresgid() should return with 0 value and the effective user id
30f08c3bdfSopenharmony_ci *  should match the egid of specified user, real/saved user ids should
31f08c3bdfSopenharmony_ci *  remain unchanged.
32f08c3bdfSopenharmony_ci *
33f08c3bdfSopenharmony_ci * Algorithm:
34f08c3bdfSopenharmony_ci *  Setup:
35f08c3bdfSopenharmony_ci *   Setup signal handling.
36f08c3bdfSopenharmony_ci *   Pause for SIGUSR1 if option specified.
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci *  Test:
39f08c3bdfSopenharmony_ci *   Loop if the proper options are given.
40f08c3bdfSopenharmony_ci *   Execute system call
41f08c3bdfSopenharmony_ci *   Check return code, if system call failed (return=-1)
42f08c3bdfSopenharmony_ci *	Log the errno and Issue a FAIL message.
43f08c3bdfSopenharmony_ci *   Otherwise,
44f08c3bdfSopenharmony_ci *	Verify the Functionality of system call
45f08c3bdfSopenharmony_ci *      if successful,
46f08c3bdfSopenharmony_ci *		Issue Functionality-Pass message.
47f08c3bdfSopenharmony_ci *      Otherwise,
48f08c3bdfSopenharmony_ci *		Issue Functionality-Fail message.
49f08c3bdfSopenharmony_ci *  Cleanup:
50f08c3bdfSopenharmony_ci *   Print errno log and/or timing stats if options given
51f08c3bdfSopenharmony_ci *
52f08c3bdfSopenharmony_ci * Usage:  <for command-line>
53f08c3bdfSopenharmony_ci *  getresgid03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
55f08c3bdfSopenharmony_ci *             -f   : Turn off functionality Testing.
56f08c3bdfSopenharmony_ci *	       -i n : Execute test n times.
57f08c3bdfSopenharmony_ci *	       -I x : Execute test for x seconds.
58f08c3bdfSopenharmony_ci *	       -P x : Pause for x seconds between iterations.
59f08c3bdfSopenharmony_ci *	       -t   : Turn on syscall timing.
60f08c3bdfSopenharmony_ci *
61f08c3bdfSopenharmony_ci * HISTORY
62f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
63f08c3bdfSopenharmony_ci *
64f08c3bdfSopenharmony_ci * RESTRICTIONS:
65f08c3bdfSopenharmony_ci *  This test should be run by 'super-user' (root) only.
66f08c3bdfSopenharmony_ci *
67f08c3bdfSopenharmony_ci */
68f08c3bdfSopenharmony_ci#define _GNU_SOURCE 1
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci#include <stdio.h>
71f08c3bdfSopenharmony_ci#include <unistd.h>
72f08c3bdfSopenharmony_ci#include <sys/types.h>
73f08c3bdfSopenharmony_ci#include <errno.h>
74f08c3bdfSopenharmony_ci#include <fcntl.h>
75f08c3bdfSopenharmony_ci#include <string.h>
76f08c3bdfSopenharmony_ci#include <signal.h>
77f08c3bdfSopenharmony_ci#include <pwd.h>
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci#include "test.h"
80f08c3bdfSopenharmony_ci#include "compat_16.h"
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cichar *TCID = "getresgid03";
83f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
84f08c3bdfSopenharmony_ciGID_T pr_gid, pe_gid, ps_gid;	/* calling process real/effective/saved gid */
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_civoid setup();			/* Main setup function of test */
87f08c3bdfSopenharmony_civoid cleanup();			/* cleanup function for the test */
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ciint main(int ac, char **av)
90f08c3bdfSopenharmony_ci{
91f08c3bdfSopenharmony_ci	int lc;
92f08c3bdfSopenharmony_ci	GID_T real_gid,		/* real/eff./saved user id from getresgid() */
93f08c3bdfSopenharmony_ci	 eff_gid, sav_gid;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	setup();
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci		tst_count = 0;
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci		/*
104f08c3bdfSopenharmony_ci		 * Call getresgid() to get the real/effective/saved
105f08c3bdfSopenharmony_ci		 * user id's of the calling process after
106f08c3bdfSopenharmony_ci		 * setregid() in setup.
107f08c3bdfSopenharmony_ci		 */
108f08c3bdfSopenharmony_ci		TEST(GETRESGID(cleanup, &real_gid, &eff_gid, &sav_gid));
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
111f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s",
112f08c3bdfSopenharmony_ci				 TEST_ERRNO, strerror(TEST_ERRNO));
113f08c3bdfSopenharmony_ci			continue;
114f08c3bdfSopenharmony_ci		}
115f08c3bdfSopenharmony_ci		/*
116f08c3bdfSopenharmony_ci		 * Verify the real/effective/saved gid
117f08c3bdfSopenharmony_ci		 * values returned by getresgid with the
118f08c3bdfSopenharmony_ci		 * expected values.
119f08c3bdfSopenharmony_ci		 */
120f08c3bdfSopenharmony_ci		if ((real_gid != pr_gid) || (eff_gid != pe_gid) ||
121f08c3bdfSopenharmony_ci		    (sav_gid != ps_gid)) {
122f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "real:%d, effective:%d, "
123f08c3bdfSopenharmony_ci				 "saved-user:%d ids differ",
124f08c3bdfSopenharmony_ci				 real_gid, eff_gid, sav_gid);
125f08c3bdfSopenharmony_ci		} else {
126f08c3bdfSopenharmony_ci			tst_resm(TPASS, "Functionality of getresgid() "
127f08c3bdfSopenharmony_ci				 "successful");
128f08c3bdfSopenharmony_ci		}
129f08c3bdfSopenharmony_ci	}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	cleanup();
132f08c3bdfSopenharmony_ci	tst_exit();
133f08c3bdfSopenharmony_ci}
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci/*
136f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test.
137f08c3bdfSopenharmony_ci *	     Make sure test process gid is root.
138f08c3bdfSopenharmony_ci *	     Get the real/effective/saved user id of the calling process.
139f08c3bdfSopenharmony_ci *	     Get the user info. of test user "ltpuser1" from /etc/passwd file.
140f08c3bdfSopenharmony_ci *	     Set the eff. user id of test process to that of "ltpuser1" user.
141f08c3bdfSopenharmony_ci */
142f08c3bdfSopenharmony_civoid setup(void)
143f08c3bdfSopenharmony_ci{
144f08c3bdfSopenharmony_ci	struct passwd *user_id;	/* passwd struct for test user */
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_ci	tst_require_root();
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_ci	TEST_PAUSE;
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_ci	/* Real user-id of the calling process */
153f08c3bdfSopenharmony_ci	pr_gid = getgid();
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	/* Saved user-id of the calling process. */
156f08c3bdfSopenharmony_ci	ps_gid = getegid();
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	/* Get effective gid of "ltpuser1" user from passwd file */
159f08c3bdfSopenharmony_ci	if ((user_id = getpwnam("nobody")) == NULL) {
160f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup,
161f08c3bdfSopenharmony_ci			 "getpwnam(nobody) Failed, errno=%d", errno);
162f08c3bdfSopenharmony_ci	}
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ci	/* Effective user-id of the test-user "ltpuser1" */
165f08c3bdfSopenharmony_ci	pe_gid = user_id->pw_gid;
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci	/*
168f08c3bdfSopenharmony_ci	 * Set the effective user-id of the process to that of
169f08c3bdfSopenharmony_ci	 * test user "ltpuser1".
170f08c3bdfSopenharmony_ci	 * The real/saved  user id remains same as of caller.
171f08c3bdfSopenharmony_ci	 */
172f08c3bdfSopenharmony_ci	if (setresgid(-1, pe_gid, -1) < 0) {
173f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup,
174f08c3bdfSopenharmony_ci			 "setresgid(-1, %d, -1) Fails, errno:%d : %s",
175f08c3bdfSopenharmony_ci			 ps_gid, errno, strerror(errno));
176f08c3bdfSopenharmony_ci	}
177f08c3bdfSopenharmony_ci}
178f08c3bdfSopenharmony_ci
179f08c3bdfSopenharmony_ci/*
180f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at
181f08c3bdfSopenharmony_ci *             completion or premature exit.
182f08c3bdfSopenharmony_ci */
183f08c3bdfSopenharmony_civoid cleanup(void)
184f08c3bdfSopenharmony_ci{
185f08c3bdfSopenharmony_ci
186f08c3bdfSopenharmony_ci	/* Reset the effective/saved gid of the calling process */
187f08c3bdfSopenharmony_ci	if (setregid(-1, pr_gid) < 0) {
188f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "resetting process effective gid failed");
189f08c3bdfSopenharmony_ci	}
190f08c3bdfSopenharmony_ci
191f08c3bdfSopenharmony_ci}
192