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: fchmod04
22  *
23  * Test Description:
24  *  Verify that, fchmod(2) will succeed to change the mode of a directory
25  *  and set the sticky bit on it if invoked by non-root (uid != 0) process
26  *  with the following constraints,
27  *	- the process is the owner of the directory.
28  *	- the effective group ID or one of the supplementary group ID's of the
29  *	  process is equal to the group ID of the directory.
30  *
31  * Expected Result:
32  *  fchmod() should return value 0 on success and succeeds to set sticky bit
33  *  on the specified directory.
34  *
35  * Algorithm:
36  *  Setup:
37  *   Setup signal handling.
38  *   Create temporary directory.
39  *   Pause for SIGUSR1 if option specified.
40  *
41  *  Test:
42  *   Loop if the proper options are given.
43  *   Execute system call
44  *   Check return code, if system call failed (return=-1)
45  *   	Log the errno and Issue a FAIL message.
46  *   Otherwise,
47  *   	Verify the Functionality of system call
48  *      if successful,
49  *      	Issue Functionality-Pass message.
50  *      Otherwise,
51  *		Issue Functionality-Fail message.
52  *  Cleanup:
53  *   Print errno log and/or timing stats if options given
54  *   Delete the temporary directory created.
55  *
56  * Usage:  <for command-line>
57  *  fchmod04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
58  *     where,  -c n : Run n copies concurrently.
59  *             -f   : Turn off functionality Testing.
60  *	       -i n : Execute test n times.
61  *	       -I x : Execute test for x seconds.
62  *	       -P x : Pause for x seconds between iterations.
63  *	       -t   : Turn on syscall timing.
64  *
65  * HISTORY
66  *	07/2001 Ported by Wayne Boyer
67  *
68  * RESTRICTIONS:
69  *  This test should be run by 'non-super-user' only.
70  *
71  */
72 
73 #include <stdio.h>
74 #include <sys/types.h>
75 #include <sys/stat.h>
76 #include <sys/fcntl.h>
77 #include <errno.h>
78 #include <string.h>
79 #include <signal.h>
80 #include <pwd.h>
81 
82 #include "test.h"
83 
84 #define DIR_MODE 	S_IRWXU | S_IRWXG | S_IRWXO
85 #define PERMS		01777	/*
86 				 * Mode permissions of test directory with
87 				 * sticky bit set.
88 				 */
89 #define TESTDIR		"testdir_4"
90 
91 int fd;				/* file descriptor for test directory */
92 char *TCID = "fchmod04";
93 int TST_TOTAL = 1;
94 
95 char nobody_uid[] = "nobody";
96 struct passwd *ltpuser;
97 
98 void setup();
99 void cleanup();
100 
main(int ac,char ** av)101 int main(int ac, char **av)
102 {
103 	struct stat stat_buf;	/* stat struct. */
104 	int lc;
105 	mode_t dir_mode;	/* mode permissions set on testdirectory */
106 
107 	tst_parse_opts(ac, av, NULL, NULL);
108 
109 	setup();
110 
111 	for (lc = 0; TEST_LOOPING(lc); lc++) {
112 
113 		tst_count = 0;
114 
115 		/*
116 		 * Call fchmod(2) with mode argument to
117 		 * set sticky bit on TESTDIR
118 		 */
119 		TEST(fchmod(fd, PERMS));
120 
121 		if (TEST_RETURN == -1) {
122 			tst_resm(TFAIL | TTERRNO, "fchmod failed");
123 			continue;
124 		}
125 		if (fstat(fd, &stat_buf) == -1)
126 			tst_brkm(TFAIL | TERRNO, cleanup,
127 				 "fstat failed");
128 		dir_mode = stat_buf.st_mode;
129 
130 		if ((dir_mode & PERMS) == PERMS)
131 			tst_resm(TPASS, "Functionality of fchmod(%d, "
132 				 "%#o) successful", fd, PERMS);
133 		else
134 			tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
135 				 "Expected 0%03o",
136 				 TESTDIR, dir_mode, PERMS);
137 	}
138 
139 	cleanup();
140 	tst_exit();
141 }
142 
143 /*
144  * void
145  * setup() - performs all ONE TIME setup for this test.
146  *  Create a temporary directory and cd to it.
147  *  Create another test directory under temporary directory.
148  *  Open the test directory for reading.
149  */
setup(void)150 void setup(void)
151 {
152 	tst_require_root();
153 
154 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
155 
156 	/* Switch to nobody user for correct error code collection */
157 	ltpuser = getpwnam(nobody_uid);
158 	if (seteuid(ltpuser->pw_uid) == -1) {
159 		tst_resm(TINFO, "seteuid failed to "
160 			 "to set the effective uid to %d", ltpuser->pw_uid);
161 		perror("seteuid");
162 	}
163 
164 	TEST_PAUSE;
165 
166 	tst_tmpdir();
167 
168 	/*
169 	 * Create a test directory under temporary directory with specified
170 	 * mode permissios and open it for reading/writing.
171 	 */
172 	if (mkdir(TESTDIR, DIR_MODE) < 0) {
173 		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
174 	}
175 	if ((fd = open(TESTDIR, O_RDONLY)) == -1) {
176 		tst_brkm(TBROK, cleanup,
177 			 "open(%s, O_RDONLY) failed, errno=%d : %s",
178 			 TESTDIR, errno, strerror(errno));
179 	}
180 }
181 
182 /*
183  * void
184  * cleanup() - performs all ONE TIME cleanup for this test at
185  *		completion or premature exit.
186  *  Close the test directory opened during setup().
187  *  Remove the test directory and temporary directory created in setup().
188  */
cleanup(void)189 void cleanup(void)
190 {
191 
192 	/* Close the test directory opened during setup() */
193 	if (close(fd) == -1) {
194 		tst_brkm(TBROK, NULL,
195 			 "close(%s) Failed, errno=%d : %s",
196 			 TESTDIR, errno, strerror(errno));
197 	}
198 
199 	tst_rmdir();
200 
201 }
202