1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
13f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
14f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci/**********************************************************
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci *    TEST IDENTIFIER   : setresgid01
20f08c3bdfSopenharmony_ci *
21f08c3bdfSopenharmony_ci *    EXECUTED BY       : root / superuser
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci *    TEST TITLE        : Checking functionality of setresgid(2)
24f08c3bdfSopenharmony_ci *
25f08c3bdfSopenharmony_ci *    TEST CASE TOTAL   : 5
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci *    AUTHOR            : Madhu T L <madhu.tarikere@wipro.com>
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci *    SIGNALS
30f08c3bdfSopenharmony_ci *      Uses SIGUSR1 to pause before test if option set.
31f08c3bdfSopenharmony_ci *      (See the parse_opts(3) man page).
32f08c3bdfSopenharmony_ci *
33f08c3bdfSopenharmony_ci *    DESCRIPTION
34f08c3bdfSopenharmony_ci *      Verify that,
35f08c3bdfSopenharmony_ci *	1. setresgid(2) is successful for setresgid(-1, -1, -1)
36f08c3bdfSopenharmony_ci *	2. setresgid(2) is successful for setresgid(-1, -1, nobody)
37f08c3bdfSopenharmony_ci *	3. setresgid(2) is successful for setresgid(-1, nobody, -1)
38f08c3bdfSopenharmony_ci *	4. setresgid(2) is successful for setresgid(nobody, -1, -1)
39f08c3bdfSopenharmony_ci *	5. setresgid(2) is successful for setresgid(root, root, root)
40f08c3bdfSopenharmony_ci *
41f08c3bdfSopenharmony_ci *      Setup:
42f08c3bdfSopenharmony_ci *	  Setup signal handling.
43f08c3bdfSopenharmony_ci *	  Test caller is superuser
44f08c3bdfSopenharmony_ci *	  Check existence of root and nobody user id's
45f08c3bdfSopenharmony_ci *	  Pause for SIGUSR1 if option specified.
46f08c3bdfSopenharmony_ci *
47f08c3bdfSopenharmony_ci *	Test:
48f08c3bdfSopenharmony_ci *	 Loop if the proper options are given.
49f08c3bdfSopenharmony_ci *	  Execute system call
50f08c3bdfSopenharmony_ci *	  Check return value and functionality, if success,
51f08c3bdfSopenharmony_ci *		 Issue PASS message
52f08c3bdfSopenharmony_ci *	Otherwise,
53f08c3bdfSopenharmony_ci *		Issue FAIL message
54f08c3bdfSopenharmony_ci *
55f08c3bdfSopenharmony_ci *	Cleanup:
56f08c3bdfSopenharmony_ci *	  Print errno log and/or timing stats if options given
57f08c3bdfSopenharmony_ci *
58f08c3bdfSopenharmony_ci * USAGE:  <for command-line>
59f08c3bdfSopenharmony_ci *  setresgid01 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
60f08c3bdfSopenharmony_ci *		where,  -c n : Run n copies concurrently.
61f08c3bdfSopenharmony_ci *			-e   : Turn on errno logging.
62f08c3bdfSopenharmony_ci *			-f   : Turn off functional testing
63f08c3bdfSopenharmony_ci *			-h   : Show help screen
64f08c3bdfSopenharmony_ci *			-i n : Execute test n times.
65f08c3bdfSopenharmony_ci *			-I x : Execute test for x seconds.
66f08c3bdfSopenharmony_ci *			-p   : Pause for SIGUSR1 before starting
67f08c3bdfSopenharmony_ci *			-P x : Pause for x seconds between iterations.
68f08c3bdfSopenharmony_ci *			-t   : Turn on syscall timing.
69f08c3bdfSopenharmony_ci *
70f08c3bdfSopenharmony_ci ****************************************************************/
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci#define _GNU_SOURCE 1
73f08c3bdfSopenharmony_ci#include <errno.h>
74f08c3bdfSopenharmony_ci#include <pwd.h>
75f08c3bdfSopenharmony_ci#include <sys/types.h>
76f08c3bdfSopenharmony_ci#include <unistd.h>
77f08c3bdfSopenharmony_ci#include "test.h"
78f08c3bdfSopenharmony_ci#include "safe_macros.h"
79f08c3bdfSopenharmony_ci#include "compat_16.h"
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci#define EXP_RET_VAL	0
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistruct test_case_t {		/* test case structure */
84f08c3bdfSopenharmony_ci	uid_t *rgid;		/* real GID */
85f08c3bdfSopenharmony_ci	uid_t *egid;		/* effective GID */
86f08c3bdfSopenharmony_ci	uid_t *sgid;		/* saved GID */
87f08c3bdfSopenharmony_ci	struct passwd *exp_rgid;	/* Expected real GID */
88f08c3bdfSopenharmony_ci	struct passwd *exp_egid;	/* Expected effective GID */
89f08c3bdfSopenharmony_ci	struct passwd *exp_sgid;	/* Expected saved GID */
90f08c3bdfSopenharmony_ci	char *desc;		/* Test description */
91f08c3bdfSopenharmony_ci};
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ciTCID_DEFINE(setresgid01);
94f08c3bdfSopenharmony_cistatic int testno;
95f08c3bdfSopenharmony_cistatic struct passwd nobody, root;
96f08c3bdfSopenharmony_cistatic uid_t nobody_gid, root_gid, neg = -1;
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_cistatic int test_functionality(uid_t, uid_t, uid_t);
99f08c3bdfSopenharmony_cistatic void setup(void);
100f08c3bdfSopenharmony_cistatic void cleanup(void);
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci/* Don't change order of these test cases */
103f08c3bdfSopenharmony_cistatic struct test_case_t tdat[] = {
104f08c3bdfSopenharmony_ci	{&neg, &neg, &neg, &root, &root, &root,
105f08c3bdfSopenharmony_ci	 "setresgid(-1, -1, -1)"},
106f08c3bdfSopenharmony_ci	{&neg, &neg, &nobody.pw_gid, &root, &root, &nobody,
107f08c3bdfSopenharmony_ci	 "setresgid(-1, -1, nobody)"},
108f08c3bdfSopenharmony_ci	{&neg, &nobody.pw_gid, &neg, &root, &nobody, &nobody,
109f08c3bdfSopenharmony_ci	 "setresgid(-1, nobody, -1)"},
110f08c3bdfSopenharmony_ci	{&nobody.pw_gid, &neg, &neg, &nobody, &nobody, &nobody,
111f08c3bdfSopenharmony_ci	 "setresgid(nobody, -1, -1)"},
112f08c3bdfSopenharmony_ci	{&root.pw_gid, &root.pw_gid, &root.pw_gid, &root, &root, &root,
113f08c3bdfSopenharmony_ci	 "setresgid(root, root, root)"},
114f08c3bdfSopenharmony_ci};
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ciint TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ciint main(int argc, char **argv)
119f08c3bdfSopenharmony_ci{
120f08c3bdfSopenharmony_ci	int lc;
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
123f08c3bdfSopenharmony_ci
124f08c3bdfSopenharmony_ci	setup();
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
127f08c3bdfSopenharmony_ci		/* reset tst_count in case we are looping */
128f08c3bdfSopenharmony_ci		tst_count = 0;
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci		for (testno = 0; testno < TST_TOTAL; ++testno) {
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci			TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid,
133f08c3bdfSopenharmony_ci				       *tdat[testno].sgid));
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci			if (TEST_RETURN == EXP_RET_VAL) {
136f08c3bdfSopenharmony_ci				if (!test_functionality
137f08c3bdfSopenharmony_ci				    (tdat[testno].exp_rgid->pw_gid,
138f08c3bdfSopenharmony_ci				     tdat[testno].exp_egid->pw_gid,
139f08c3bdfSopenharmony_ci				     tdat[testno].exp_sgid->pw_gid)) {
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci					tst_resm(TPASS, "Test for %s "
142f08c3bdfSopenharmony_ci						 "successful",
143f08c3bdfSopenharmony_ci						 tdat[testno].desc);
144f08c3bdfSopenharmony_ci				} else {
145f08c3bdfSopenharmony_ci					tst_resm(TFAIL, "Functionality test "
146f08c3bdfSopenharmony_ci						 "for %s failed",
147f08c3bdfSopenharmony_ci						 tdat[testno].desc);
148f08c3bdfSopenharmony_ci				}
149f08c3bdfSopenharmony_ci			} else {
150f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Test for %s failed; returned"
151f08c3bdfSopenharmony_ci					 " %ld (expected %d), errno %d (expected"
152f08c3bdfSopenharmony_ci					 " 0)", tdat[testno].desc,
153f08c3bdfSopenharmony_ci					 TEST_RETURN, EXP_RET_VAL, TEST_ERRNO);
154f08c3bdfSopenharmony_ci			}
155f08c3bdfSopenharmony_ci		}
156f08c3bdfSopenharmony_ci	}
157f08c3bdfSopenharmony_ci	cleanup();
158f08c3bdfSopenharmony_ci
159f08c3bdfSopenharmony_ci	tst_exit();
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_cistatic int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
163f08c3bdfSopenharmony_ci{
164f08c3bdfSopenharmony_ci	uid_t cur_rgid, cur_egid, cur_sgid;
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci	/* Get current real, effective and saved group id's */
167f08c3bdfSopenharmony_ci	SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &cur_sgid);
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_ci	if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
170f08c3bdfSopenharmony_ci	    && (cur_sgid == exp_sgid)) {
171f08c3bdfSopenharmony_ci		return 0;
172f08c3bdfSopenharmony_ci	}
173f08c3bdfSopenharmony_ci	return 1;
174f08c3bdfSopenharmony_ci}
175f08c3bdfSopenharmony_ci
176f08c3bdfSopenharmony_ci/*
177f08c3bdfSopenharmony_ci * setup()
178f08c3bdfSopenharmony_ci *	performs all ONE TIME setup for this test
179f08c3bdfSopenharmony_ci */
180f08c3bdfSopenharmony_civoid setup(void)
181f08c3bdfSopenharmony_ci{
182f08c3bdfSopenharmony_ci	struct passwd *passwd_p;
183f08c3bdfSopenharmony_ci
184f08c3bdfSopenharmony_ci	tst_require_root();
185f08c3bdfSopenharmony_ci
186f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
187f08c3bdfSopenharmony_ci
188f08c3bdfSopenharmony_ci	if ((passwd_p = getpwnam("root")) == NULL) {
189f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "getpwnam() failed for root");
190f08c3bdfSopenharmony_ci
191f08c3bdfSopenharmony_ci	}
192f08c3bdfSopenharmony_ci	root = *passwd_p;
193f08c3bdfSopenharmony_ci	GID16_CHECK((root_gid = root.pw_gid), "setresgid", cleanup)
194f08c3bdfSopenharmony_ci
195f08c3bdfSopenharmony_ci	if ((passwd_p = getpwnam("nobody")) == NULL) {
196f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "nobody user id doesn't exist");
197f08c3bdfSopenharmony_ci
198f08c3bdfSopenharmony_ci	}
199f08c3bdfSopenharmony_ci	nobody = *passwd_p;
200f08c3bdfSopenharmony_ci	GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup)
201f08c3bdfSopenharmony_ci
202f08c3bdfSopenharmony_ci	/* Pause if that option was specified
203f08c3bdfSopenharmony_ci	 * TEST_PAUSE contains the code to fork the test with the -c option.
204f08c3bdfSopenharmony_ci	 */
205f08c3bdfSopenharmony_ci	TEST_PAUSE;
206f08c3bdfSopenharmony_ci}
207f08c3bdfSopenharmony_ci
208f08c3bdfSopenharmony_ci/*
209f08c3bdfSopenharmony_ci * cleanup()
210f08c3bdfSopenharmony_ci *	performs all ONE TIME cleanup for this test at
211f08c3bdfSopenharmony_ci *	completion or premature exit
212f08c3bdfSopenharmony_ci */
213f08c3bdfSopenharmony_civoid cleanup(void)
214f08c3bdfSopenharmony_ci{
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci}
217