blob: 9738140752fec5dd89af0fae7f0b53b9f60817f8 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: chmod07
plars865695b2001-08-27 22:15:12 +000022 *
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.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * 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>
nstrazfa31d552002-05-14 16:50:06 +000057 * chmod07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000058 * 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#include "usctest.h"
85
86#define LTPUSER "nobody"
robbiew48b0c352001-08-31 16:03:43 +000087#define LTPGRP "users"
plars865695b2001-08-27 22:15:12 +000088#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
89#define PERMS 01777 /*
90 * Mode permissions of test file with sticky
91 * bit set.
92 */
93#define TESTFILE "testfile"
94
subrata_modak56207ce2009-03-23 13:35:39 +000095char *TCID = "chmod07"; /* Test program identifier. */
96int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +000097extern int Tst_count; /* Test Case counter for tst_* routines */
98
99void setup(); /* Main setup function for the test */
100void cleanup(); /* Main cleanup function for the test */
101
subrata_modak56207ce2009-03-23 13:35:39 +0000102int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000103{
104 struct stat stat_buf; /* stat(2) struct contents */
105 int lc; /* loop counter */
106 char *msg; /* message returned from parse_opts */
subrata_modak56207ce2009-03-23 13:35:39 +0000107
plars865695b2001-08-27 22:15:12 +0000108 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800109 msg = parse_opts(ac, av, NULL, NULL);
110 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
112 tst_exit();
113 }
114
115 /* Perform global setup for test */
116 setup();
117
118 /* Check looping state if -i option given */
119 for (lc = 0; TEST_LOOPING(lc); lc++) {
120 /* Reset Tst_count in case we are looping. */
121 Tst_count = 0;
122
subrata_modak4bb656a2009-02-26 12:02:09 +0000123 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000124 * Call chmod(2) with specified mode argument
plars865695b2001-08-27 22:15:12 +0000125 * (sticky-bit set) on testfile.
subrata_modak56207ce2009-03-23 13:35:39 +0000126 */
plars865695b2001-08-27 22:15:12 +0000127 TEST(chmod(TESTFILE, PERMS));
subrata_modakbdbaec52009-02-26 12:14:51 +0000128
plars865695b2001-08-27 22:15:12 +0000129 /* check return code of chmod(2) */
130 if (TEST_RETURN == -1) {
vapier45996092009-08-28 11:49:07 +0000131 tst_resm(TFAIL|TTERRNO, "chmod(%s, %#o) failed",
132 TESTFILE, PERMS);
plars865695b2001-08-27 22:15:12 +0000133 continue;
134 }
135 /*
136 * Perform functional verification if test
137 * executed without (-f) option.
138 */
139 if (STD_FUNCTIONAL_TEST) {
140 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000141 * Get the testfile information using
plars865695b2001-08-27 22:15:12 +0000142 * stat(2).
143 */
144 if (stat(TESTFILE, &stat_buf) < 0) {
145 tst_brkm(TFAIL, cleanup, "stat(2) of %s failed,"
subrata_modak56207ce2009-03-23 13:35:39 +0000146 " errno:%d", TESTFILE, TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000147 }
148
149 /* Check for expected mode permissions */
150 if ((stat_buf.st_mode & PERMS) == PERMS) {
151 tst_resm(TPASS, "Functionality of "
152 "chmod(%s, %#o) successful",
153 TESTFILE, PERMS);
154 } else {
155 tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
156 "Expected 0%03o", TESTFILE,
157 stat_buf.st_mode, PERMS);
158 }
159 } else {
160 tst_resm(TPASS, "call succeeded");
161 }
subrata_modak56207ce2009-03-23 13:35:39 +0000162 } /* End for TEST_LOOPING */
plars865695b2001-08-27 22:15:12 +0000163
164 /* Call cleanup() to undo setup done for the test. */
165 cleanup();
166
robbiew2c945242002-11-11 19:01:25 +0000167 return 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000168 /*NOTREACHED*/} /* End main */
plars865695b2001-08-27 22:15:12 +0000169
170/*
171 * void
172 * setup() - performs all ONE TIME setup for this test.
173 * Create a temporary directory and change directory to it.
174 * Create a test file under temporary directory and close it
175 * Change the ownership of test file to that of "ltpuser1" user.
176 */
subrata_modak56207ce2009-03-23 13:35:39 +0000177void setup()
plars865695b2001-08-27 22:15:12 +0000178{
subrata_modak56207ce2009-03-23 13:35:39 +0000179 struct passwd *ltpuser; /* password struct for ltpuser1 */
180 struct group *ltpgroup; /* group struct for ltpuser1 */
181 int fd; /* file descriptor variable */
182 gid_t group1_gid; /* user and process group id's */
plars865695b2001-08-27 22:15:12 +0000183 uid_t user1_uid;
184
185 /* capture signals */
186 tst_sig(FORK, DEF_HANDLER, cleanup);
187
subrata_modak56207ce2009-03-23 13:35:39 +0000188 /* Pause if that option was specified */
189 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000190
191 /* Check that the test process id is super/root */
192 if (geteuid() != 0) {
193 tst_brkm(TBROK, NULL, "Must be super/root for this test!");
194 tst_exit();
195 }
196
197 /* make a temp directory and cd to it */
198 tst_tmpdir();
199
200 /* Get the uid of guest user - ltpuser1 */
201 if ((ltpuser = getpwnam(LTPUSER)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000202 tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER);
plars865695b2001-08-27 22:15:12 +0000203 }
204 user1_uid = ltpuser->pw_uid;
205
206 /* Get the group id of guest user - ltpuser1 */
207 if ((ltpgroup = getgrnam(LTPGRP)) == NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000208 tst_brkm(TBROK, cleanup, "%s not in /etc/group", LTPGRP);
plars865695b2001-08-27 22:15:12 +0000209 }
210 group1_gid = ltpgroup->gr_gid;
211
212 /*
213 * Create a test file under temporary directory with specified
214 * mode permissios and set the ownership of the test file to the
215 * uid/gid of guest user.
216 */
vapier45996092009-08-28 11:49:07 +0000217 fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
218 if (fd == -1)
219 tst_brkm(TBROK|TERRNO, cleanup,
220 "open(%s, O_RDWR|O_CREAT, %#o) failed",
221 TESTFILE, FILE_MODE);
222 if (close(fd) == -1)
223 tst_brkm(TBROK, cleanup, "close(%s) failed",
224 TESTFILE);
225 if (chown(TESTFILE, user1_uid, group1_gid) < 0)
226 tst_brkm(TBROK|TERRNO, cleanup, "chown(%s) failed", TESTFILE);
subrata_modakbdbaec52009-02-26 12:14:51 +0000227
plars865695b2001-08-27 22:15:12 +0000228 /* Set the effective gid of the process to that of user */
vapier45996092009-08-28 11:49:07 +0000229 if (setgid(group1_gid) < 0)
230 tst_brkm(TBROK|TERRNO, cleanup, "setgid(%d) failed", group1_gid);
subrata_modak56207ce2009-03-23 13:35:39 +0000231} /* End setup() */
plars865695b2001-08-27 22:15:12 +0000232
233/*
234 * void
235 * cleanup() - performs all ONE TIME cleanup for this test at
236 * completion or premature exit.
237 * Remove the test directory and testfile created in the setup.
238 */
subrata_modak56207ce2009-03-23 13:35:39 +0000239void cleanup()
plars865695b2001-08-27 22:15:12 +0000240{
241 /*
242 * print timing stats if that option was specified.
243 */
244 TEST_CLEANUP;
245
246 /* Remove temporary directory and all files in it */
247 tst_rmdir();
248
249 /* exit with return code appropriate for results */
250 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000251} /* End cleanup() */