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 * NAME
22 *	mkdir04
23 *
24 * DESCRIPTION
25 *
26 * ALGORITHM
27 *	Setup:
28 *		Setup signal handling.
29 *		Pause for SIGUSR1 if option specified.
30 *		Create temporary directory.
31 *
32 *	Test:
33 *		Loop if the proper options are given.
34 *              fork the first child
35 *                      set to be ltpuser1
36 *                      create a dirctory tstdir1 with 0700 permission
37 *              fork the second child
38 *                      set to ltpuser2
39 *                      try to create a subdirectory tstdir2 under tstdir1
40 *                      check the returnvalue, if succeeded (return=0)
41 *			       Log the errno and Issue a FAIL message.
42 *		        Otherwise,
43 *			       Verify the errno
44 *			       if equals to EACCES,
45 *				       Issue Pass message.
46 *			       Otherwise,
47 *				       Issue Fail message.
48 *	Cleanup:
49 *		Print errno log and/or timing stats if options given
50 *		Delete the temporary directory created.
51 * USAGE
52 *	mkdir04 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
53 *	where,  -c n : Run n copies concurrently.
54 *		-e   : Turn on errno logging.
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 *	None.
66 *
67 */
68
69#include <errno.h>
70#include <string.h>
71#include <signal.h>
72#include <sys/stat.h>
73#include <sys/types.h>
74#include <fcntl.h>
75#include <pwd.h>
76#include <sys/wait.h>
77#include <unistd.h>
78
79#include "test.h"
80#include "safe_macros.h"
81
82void setup();
83void cleanup();
84int fail;
85
86#define PERMS		0700
87
88static uid_t nobody_uid, bin_uid;
89
90char *TCID = "mkdir04";
91int TST_TOTAL = 1;
92int fail;
93
94char tstdir1[100];
95char tstdir2[100];
96
97int main(int ac, char **av)
98{
99	int lc;
100	int rval;
101	pid_t pid, pid1;
102	int status;
103
104	/*
105	 * parse standard options
106	 */
107	tst_parse_opts(ac, av, NULL, NULL);
108
109	/*
110	 * perform global setup for test
111	 */
112	setup();
113
114	/*
115	 * check looping state if -i option given
116	 */
117	for (lc = 0; TEST_LOOPING(lc); lc++) {
118
119		tst_count = 0;
120
121		/* Initialize the test directories name */
122		sprintf(tstdir1, "tstdir1.%d", getpid());
123		if ((pid = FORK_OR_VFORK()) < 0) {
124			tst_brkm(TBROK, cleanup, "fork #1 failed");
125		}
126
127		if (pid == 0) {	/* first child */
128			rval = setreuid(nobody_uid, nobody_uid);
129			if (rval < 0) {
130				tst_resm(TFAIL | TERRNO, "setreuid failed to "
131					 "to set the real uid to %d and "
132					 "effective uid to %d",
133					 nobody_uid, nobody_uid);
134				exit(1);
135			}
136			/* create the parent directory with 0700 permits */
137			if (mkdir(tstdir1, PERMS) == -1) {
138				tst_resm(TFAIL | TERRNO,
139					 "mkdir(%s, %#o) Failed",
140					 tstdir1, PERMS);
141				exit(1);
142			}
143			/* create tstdir1 succeeded */
144			exit(0);
145		}
146		wait(&status);
147		if (WEXITSTATUS(status) != 0) {
148			tst_brkm(TFAIL, cleanup,
149				 "Test to check mkdir EACCES failed"
150				 "in create parent directory");
151		}
152
153		sprintf(tstdir2, "%s/tst", tstdir1);
154
155		if ((pid1 = FORK_OR_VFORK()) < 0) {
156			tst_brkm(TBROK, cleanup, "fork #2 failed");
157		}
158
159		if (pid1 == 0) {	/* second child */
160			rval = setreuid(bin_uid, bin_uid);
161			if (rval < 0) {
162				tst_resm(TFAIL | TERRNO, "setreuid failed to "
163					 "to set the real uid to %d and "
164					 "effective uid to %d",
165					 bin_uid, bin_uid);
166				exit(1);
167			}
168			if (mkdir(tstdir2, PERMS) != -1) {
169				tst_resm(TFAIL, "mkdir(%s, %#o) unexpected "
170					 "succeeded", tstdir2, PERMS);
171				exit(1);
172			}
173			if (errno != EACCES) {
174				tst_resm(TFAIL, "Expected EACCES got %d",
175					 errno);
176				exit(1);
177			}
178			/* PASS */
179			exit(0);
180		}
181		waitpid(pid1, &status, 0);
182		if (WEXITSTATUS(status) == 0) {
183			tst_resm(TPASS, "Test to attempt to creat a directory "
184				 "in a directory having no permissions "
185				 "SUCCEEDED in setting errno to EACCES");
186		} else {
187			tst_resm(TFAIL, "Test to attempt to creat a directory "
188				 "in a directory having no permissions FAILED");
189			cleanup();
190		}
191	}
192
193	/*
194	 * cleanup and exit
195	 */
196	cleanup();
197	tst_exit();
198
199}
200
201/*
202 * setup() - performs all ONE TIME setup for this test.
203 */
204void setup(void)
205{
206	struct passwd *pw;
207
208	tst_require_root();
209
210	pw = SAFE_GETPWNAM(NULL, "nobody");
211	nobody_uid = pw->pw_uid;
212	pw = SAFE_GETPWNAM(NULL, "bin");
213	bin_uid = pw->pw_uid;
214
215	tst_sig(FORK, DEF_HANDLER, cleanup);
216
217	TEST_PAUSE;
218
219	/* Create a temporary directory and make it current. */
220	tst_tmpdir();
221
222	umask(0);
223}
224
225/*
226 * cleanup() - performs all ONE TIME cleanup for this test at
227 *             completion or premature exit.
228 */
229void cleanup(void)
230{
231
232	/*
233	 * Remove the temporary directory.
234	 */
235	tst_rmdir();
236
237	/*
238	 * Exit with return code appropriate for results.
239	 */
240
241}
242