1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (C) International Business Machines  Corp., 2001
3f08c3bdfSopenharmony_ci * Ported by Wayne Boyer
4f08c3bdfSopenharmony_ci * Adapted by Dustin Kirkland (k1rkland@us.ibm.com)
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * This program is free software;  you can redistribute it and/or modify
7f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
8f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
9f08c3bdfSopenharmony_ci * (at your option) any later version.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
12f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
17f08c3bdfSopenharmony_ci * along with this program;  if not, write to the Free Software
18f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci/*
22f08c3bdfSopenharmony_ci * Testcase to check the basic functionality of setfsgid(2) system
23f08c3bdfSopenharmony_ci * call fails when called by a non-root user.
24f08c3bdfSopenharmony_ci */
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include <stdio.h>
27f08c3bdfSopenharmony_ci#include <unistd.h>
28f08c3bdfSopenharmony_ci#include <sys/types.h>
29f08c3bdfSopenharmony_ci#include <errno.h>
30f08c3bdfSopenharmony_ci#include <pwd.h>
31f08c3bdfSopenharmony_ci#include <grp.h>
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#include "test.h"
34f08c3bdfSopenharmony_ci#include "compat_16.h"
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ciTCID_DEFINE(setfsgid03);
37f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic char nobody_uid[] = "nobody";
40f08c3bdfSopenharmony_cistatic struct passwd *ltpuser;
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void setup(void);
43f08c3bdfSopenharmony_cistatic void cleanup(void);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ciint main(int ac, char **av)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	int lc;
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	gid_t gid;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	setup();
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
56f08c3bdfSopenharmony_ci		tst_count = 0;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci		gid = 1;
59f08c3bdfSopenharmony_ci		while (!getgrgid(gid))
60f08c3bdfSopenharmony_ci			gid++;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci		GID16_CHECK(gid, setfsgid, cleanup);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci		TEST(SETFSGID(cleanup, gid));
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
67f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
68f08c3bdfSopenharmony_ci				"setfsgid() failed unexpectedly");
69f08c3bdfSopenharmony_ci			continue;
70f08c3bdfSopenharmony_ci		}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		if (TEST_RETURN == gid) {
73f08c3bdfSopenharmony_ci			tst_resm(TFAIL,
74f08c3bdfSopenharmony_ci				 "setfsgid() returned %ld, expected anything but %d",
75f08c3bdfSopenharmony_ci				 TEST_RETURN, gid);
76f08c3bdfSopenharmony_ci		} else {
77f08c3bdfSopenharmony_ci			tst_resm(TPASS, "setfsgid() returned expected value : "
78f08c3bdfSopenharmony_ci				 "%ld", TEST_RETURN);
79f08c3bdfSopenharmony_ci		}
80f08c3bdfSopenharmony_ci	}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	cleanup();
83f08c3bdfSopenharmony_ci	tst_exit();
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic void setup(void)
87f08c3bdfSopenharmony_ci{
88f08c3bdfSopenharmony_ci	tst_require_root();
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	ltpuser = getpwnam(nobody_uid);
91f08c3bdfSopenharmony_ci	if (ltpuser == NULL)
92f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
93f08c3bdfSopenharmony_ci			nobody_uid);
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	if (setuid(ltpuser->pw_uid) == -1)
96f08c3bdfSopenharmony_ci		tst_resm(TINFO | TERRNO,
97f08c3bdfSopenharmony_ci			"setuid failed to set the effective uid to %d",
98f08c3bdfSopenharmony_ci			ltpuser->pw_uid);
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	TEST_PAUSE;
103f08c3bdfSopenharmony_ci}
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_cistatic void cleanup(void)
106f08c3bdfSopenharmony_ci{
107f08c3bdfSopenharmony_ci}
108