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: chmod07
22  *
23  * Test Description:
24  *  Verify that, chmod(2) will succeed to change the mode of a file/directory
25  *  and sets the sticky bit on it if invoked by root (uid = 0) process with
26  *  the following constraints,
27  *	- the process is not the owner of the file/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 file/directory.
30  *
31  * Expected Result:
32  *  chmod() should return value 0 on success and succeeds to set sticky bit
33  *  on the specified file.
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  *  chmod07 [-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 'super-user' (root) 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 <grp.h>
81 #include <pwd.h>
82 
83 #include "test.h"
84 
85 #define LTPUSER		"nobody"
86 #ifdef ANDROID
87 #define LTPGRP		"sdcard_r"
88 #else
89 #define LTPGRP		"users"
90 #endif
91 #define FILE_MODE 	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
92 #define PERMS		01777	/*
93 				 * Mode permissions of test file with sticky
94 				 * bit set.
95 				 */
96 #define TESTFILE	"testfile"
97 
98 char *TCID = "chmod07";
99 int TST_TOTAL = 1;
100 
101 void setup();			/* Main setup function for the test */
102 void cleanup();			/* Main cleanup function for the test */
103 
main(int ac,char ** av)104 int main(int ac, char **av)
105 {
106 	struct stat stat_buf;	/* stat(2) struct contents */
107 	int lc;
108 
109 	tst_parse_opts(ac, av, NULL, NULL);
110 
111 	setup();
112 
113 	for (lc = 0; TEST_LOOPING(lc); lc++) {
114 
115 		tst_count = 0;
116 
117 		/*
118 		 * Call chmod(2) with specified mode argument
119 		 * (sticky-bit set) on testfile.
120 		 */
121 		TEST(chmod(TESTFILE, PERMS));
122 
123 		if (TEST_RETURN == -1) {
124 			tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
125 				 TESTFILE, PERMS);
126 			continue;
127 		}
128 		/*
129 		 * Get the testfile information using
130 		 * stat(2).
131 		 */
132 		if (stat(TESTFILE, &stat_buf) == -1)
133 			tst_brkm(TFAIL | TTERRNO, cleanup,
134 				 "stat failed");
135 
136 		/* Check for expected mode permissions */
137 		if ((stat_buf.st_mode & PERMS) == PERMS)
138 			tst_resm(TPASS, "Functionality of "
139 				 "chmod(%s, %#o) successful",
140 				 TESTFILE, PERMS);
141 		else
142 			tst_resm(TFAIL, "%s: Incorrect modes 0%03o; "
143 				 "expected 0%03o", TESTFILE,
144 				 stat_buf.st_mode, PERMS);
145 	}
146 
147 	cleanup();
148 	tst_exit();
149 }
150 
151 /*
152  * void
153  * setup() - performs all ONE TIME setup for this test.
154  *  Create a temporary directory and change directory to it.
155  *  Create a test file under temporary directory and close it
156  *  Change the ownership of test file to that of "ltpuser1" user.
157  */
setup(void)158 void setup(void)
159 {
160 	struct passwd *ltpuser;	/* password struct for ltpuser1 */
161 	struct group *ltpgroup;	/* group struct for ltpuser1 */
162 	int fd;			/* file descriptor variable */
163 	gid_t group1_gid;	/* user and process group id's */
164 	uid_t user1_uid;
165 
166 	tst_sig(FORK, DEF_HANDLER, cleanup);
167 
168 	TEST_PAUSE;
169 
170 	tst_require_root();
171 
172 	tst_tmpdir();
173 
174 	/* Get the uid of guest user - ltpuser1 */
175 	if ((ltpuser = getpwnam(LTPUSER)) == NULL)
176 		tst_brkm(TBROK, cleanup, "getpwnam failed");
177 	user1_uid = ltpuser->pw_uid;
178 
179 	/* Get the group id of guest user - ltpuser1 */
180 	if ((ltpgroup = getgrnam(LTPGRP)) == NULL)
181 		tst_brkm(TBROK, cleanup, "getgrnam failed");
182 	group1_gid = ltpgroup->gr_gid;
183 
184 	fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
185 	if (fd == -1)
186 		tst_brkm(TBROK | TERRNO, cleanup,
187 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
188 			 TESTFILE, FILE_MODE);
189 	if (close(fd) == -1)
190 		tst_brkm(TBROK, cleanup, "close(%s) failed", TESTFILE);
191 	if (chown(TESTFILE, user1_uid, group1_gid) == -1)
192 		tst_brkm(TBROK | TERRNO, cleanup, "chown(%s) failed", TESTFILE);
193 
194 	if (setgid(group1_gid) == -1)
195 		tst_brkm(TBROK | TERRNO, cleanup, "setgid(%d) failed",
196 			 group1_gid);
197 }
198 
cleanup(void)199 void cleanup(void)
200 {
201 	tst_rmdir();
202 }
203