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: getresuid03
22 *
23 * Test Description:
24 *  Verify that getresuid() will be successful to get the real, effective
25 *  and saved user ids after calling process invokes setresuid() to change
26 *  the effective uid to that of specified user.
27 * $
28 * Expected Result:
29 *  getresuid() should return with 0 value and the effective user id
30 *  should match the euid 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 *  getresuid03 [-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
69#include <stdio.h>
70#include <unistd.h>
71#include <sys/types.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <string.h>
75#include <signal.h>
76#include <pwd.h>
77
78#include "test.h"
79
80extern int getresuid(uid_t *, uid_t *, uid_t *);
81extern int setresuid(uid_t, uid_t, uid_t);
82
83char *TCID = "getresuid03";
84int TST_TOTAL = 1;
85uid_t pr_uid, pe_uid, ps_uid;	/* calling process real/effective/saved uid */
86
87void setup();			/* Main setup function of test */
88void cleanup();			/* cleanup function for the test */
89
90int main(int ac, char **av)
91{
92	int lc;
93	uid_t real_uid,		/* real/eff./saved user id from getresuid() */
94	 eff_uid, sav_uid;
95
96	tst_parse_opts(ac, av, NULL, NULL);
97
98	setup();
99
100	for (lc = 0; TEST_LOOPING(lc); lc++) {
101
102		tst_count = 0;
103
104		/*
105		 * Call getresuid() to get the real/effective/saved
106		 * user id's of the calling process after
107		 * setreuid() in setup.
108		 */
109		TEST(getresuid(&real_uid, &eff_uid, &sav_uid));
110
111		if (TEST_RETURN == -1) {
112			tst_resm(TFAIL, "getresuid() Failed, errno=%d : %s",
113				 TEST_ERRNO, strerror(TEST_ERRNO));
114			continue;
115		}
116
117		if ((real_uid != pr_uid) || (eff_uid != pe_uid) ||
118		    (sav_uid != ps_uid)) {
119			tst_resm(TFAIL, "real:%d, effective:%d, "
120				 "saved-user:%d ids differ",
121				 real_uid, eff_uid, sav_uid);
122		} else {
123			tst_resm(TPASS, "Functionality of getresuid() "
124				 "successful");
125		}
126	}
127
128	cleanup();
129	tst_exit();
130}
131
132/*
133 * setup() - performs all ONE TIME setup for this test.
134 *	     Make sure test process uid is root.
135 *	     Get the real/effective/saved user id of the calling process.
136 *	     Get the user info. of test user "ltpuser1" from /etc/passwd file.
137 *	     Set the eff. user id of test process to that of "ltpuser1" user.
138 */
139void setup(void)
140{
141	struct passwd *user_id;	/* passwd struct for test user */
142
143	tst_require_root();
144
145	tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
147	TEST_PAUSE;
148
149	/* Real user-id of the calling process */
150	pr_uid = getuid();
151
152	/* Saved user-id of the calling process. */
153	ps_uid = geteuid();
154
155	/* Get effective uid of "ltpuser1" user from passwd file */
156	if ((user_id = getpwnam("nobody")) == NULL) {
157		tst_brkm(TBROK, cleanup,
158			 "getpwnam(nobody) Failed, errno=%d", errno);
159	}
160
161	/* Effective user-id of the test-user "ltpuser1" */
162	pe_uid = user_id->pw_uid;
163
164	/*
165	 * Set the effective user-id of the process to that of
166	 * test user "ltpuser1".
167	 * The real/saved  user id remains same as of caller.
168	 */
169	if (setresuid(-1, pe_uid, -1) < 0) {
170		tst_brkm(TBROK, cleanup,
171			 "setresuid(-1, %d, -1) Fails, errno:%d : %s",
172			 ps_uid, errno, strerror(errno));
173	}
174}
175
176/*
177 * cleanup() - performs all ONE TIME cleanup for this test at
178 *             completion or premature exit.
179 */
180void cleanup(void)
181{
182
183	/* Reset the effective/saved uid of the calling process */
184	if (setreuid(-1, pr_uid) < 0) {
185		tst_brkm(TBROK, NULL, "resetting process effective uid failed");
186	}
187
188}
189