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 Description:
22  *  Verify that access() succeeds to check the read/write/execute permissions
23  *  on a file if the mode argument passed was R_OK/W_OK/X_OK.
24  *
25  *  Also verify that, access() succeeds to test the accessibility of the file
26  *  referred to by symbolic link if the pathname points to a symbolic link.
27  *
28  *	07/2001 Ported by Wayne Boyer
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <signal.h>
39 #include <pwd.h>
40 
41 #include "test.h"
42 
43 #define TEMP_FILE	"temp_file"
44 #define SYM_FILE	"sym_file"
45 #define TEST_FILE1	"test_file1"
46 #define TEST_FILE2	"test_file2"
47 #define TEST_FILE3	"test_file3"
48 #define FILE_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
49 #define EXE_MODE	0777
50 
51 char *TCID = "access02";
52 int TST_TOTAL = 4;
53 
54 static int fd1, fd2, fd4;
55 static const char nobody_uid[] = "nobody";
56 static struct passwd *ltpuser;
57 
58 static int setup1(void);
59 static int setup2(void);
60 static int setup3(void);
61 static int setup4(void);
62 
63 static struct test_case_t {
64 	char *pathname;
65 	mode_t a_mode;
66 	int (*setupfunc) (void);
67 } test_cases[] = {
68 	{TEST_FILE1, R_OK, setup1},
69 	{TEST_FILE2, W_OK, setup2},
70 	{TEST_FILE3, X_OK, setup3},
71 	{SYM_FILE, W_OK, setup4},
72 };
73 
74 static void setup(void);
75 static void cleanup(void);
76 static int access_verify(int);
77 
main(int ac,char ** av)78 int main(int ac, char **av)
79 {
80 	int lc;
81 	int i;
82 	mode_t access_mode;
83 	char *file_name;
84 
85 	tst_parse_opts(ac, av, NULL, NULL);
86 
87 	setup();
88 
89 	for (lc = 0; TEST_LOOPING(lc); lc++) {
90 		tst_count = 0;
91 		for (i = 0; i < TST_TOTAL; i++) {
92 			file_name = test_cases[i].pathname;
93 			access_mode = test_cases[i].a_mode;
94 
95 			/*
96 			 * Call access(2) to check the test file
97 			 * for specified access mode permissions.
98 			 */
99 			TEST(access(file_name, access_mode));
100 
101 			if (TEST_RETURN == -1) {
102 				tst_resm(TFAIL | TTERRNO,
103 					 "access(%s, %#o) failed",
104 					 file_name, access_mode);
105 				continue;
106 			}
107 
108 			access_verify(i);
109 		}
110 	}
111 
112 	cleanup();
113 	tst_exit();
114 }
115 
setup(void)116 static void setup(void)
117 {
118 	int i;
119 
120 	tst_require_root();
121 	tst_sig(FORK, DEF_HANDLER, cleanup);
122 
123 	ltpuser = getpwnam(nobody_uid);
124 	if (ltpuser == NULL)
125 		tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
126 	if (setuid(ltpuser->pw_uid) == -1)
127 		tst_brkm(TINFO | TERRNO, NULL, "setuid failed");
128 
129 	TEST_PAUSE;
130 	tst_tmpdir();
131 
132 	for (i = 0; i < TST_TOTAL; i++)
133 		test_cases[i].setupfunc();
134 }
135 
136 /*
137  * setup1() - Setup function to test the functionality of access() for
138  *	      the access mode argument R_OK.
139  *
140  *   Creat/open a testfile and write some data into it.
141  *   This function returns 0.
142  */
setup1(void)143 static int setup1(void)
144 {
145 	char write_buf[] = "abc";
146 
147 	fd1 = open(TEST_FILE1, O_RDWR | O_CREAT, FILE_MODE);
148 	if (fd1 == -1) {
149 		tst_brkm(TBROK | TERRNO, cleanup,
150 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
151 			 TEST_FILE1, FILE_MODE);
152 	}
153 
154 	/* write some data into testfile */
155 	if (write(fd1, write_buf, strlen(write_buf)) != strlen(write_buf)) {
156 		tst_brkm(TBROK | TERRNO, cleanup,
157 			 "write(%s) failed in setup1", TEST_FILE1);
158 	}
159 
160 	return 0;
161 }
162 
163 /*
164  * setup2() - Setup function to test the functionality of access() for
165  *	      the access mode argument W_OK.
166  *
167  *   Creat/open a testfile for writing under temporary directory.
168  *   This function returns 0.
169  */
setup2(void)170 static int setup2(void)
171 {
172 	fd2 = open(TEST_FILE2, O_RDWR | O_CREAT, FILE_MODE);
173 	if (fd1 == -1) {
174 		tst_brkm(TBROK | TERRNO, cleanup,
175 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
176 			 TEST_FILE2, FILE_MODE);
177 	}
178 
179 	return 0;
180 }
181 
182 /*
183  * setup3() - Setup function to test the functionality of access() for
184  *	      the access mode argument X_OK.
185  *
186  *   Creat/open a testfile and provide execute permissions to it.
187  *   This function returns 0.
188  */
setup3(void)189 static int setup3(void)
190 {
191 	int fd3;
192 #ifdef UCLINUX
193 	char exechead[] = "#!/bin/sh\n";
194 #endif
195 
196 	fd3 = open(TEST_FILE3, O_RDWR | O_CREAT, FILE_MODE);
197 	if (fd3 == -1) {
198 		tst_brkm(TBROK | TERRNO, cleanup,
199 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
200 			 TEST_FILE3, FILE_MODE);
201 	}
202 #ifdef UCLINUX
203 	if (write(fd3, exechead, sizeof(exechead)) < 0) {
204 		tst_brkm(TBROK | TERRNO, cleanup, "write(%s) failed",
205 			 TEST_FILE3);
206 	}
207 #endif
208 
209 	/* Close the test file created above */
210 	if (close(fd3) == -1) {
211 		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
212 			 TEST_FILE3);
213 	}
214 
215 	/* Set execute permission bits on the test file. */
216 	if (chmod(TEST_FILE3, EXE_MODE) < 0) {
217 		tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, %#o) failed",
218 			 TEST_FILE3, EXE_MODE);
219 	}
220 
221 	return 0;
222 }
223 
224 /*
225  * setup4() - Setup function to test the functionality of access() for
226  *	      symbolic link file.
227  *
228  *   Creat/open a temporary file and close it.
229  *   Creat a symbolic link of temporary file.
230  *   This function returns 0.
231  */
setup4(void)232 static int setup4(void)
233 {
234 	fd4 = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE);
235 	if (fd4 == -1) {
236 		tst_brkm(TBROK | TERRNO, cleanup,
237 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
238 			 TEMP_FILE, FILE_MODE);
239 	}
240 
241 	/* Creat a symbolic link for temporary file */
242 	if (symlink(TEMP_FILE, SYM_FILE) < 0) {
243 		tst_brkm(TBROK | TERRNO, cleanup,
244 			 "symlink(%s, %s) failed", TEMP_FILE, SYM_FILE);
245 	}
246 
247 	return 0;
248 }
249 
250 /*
251  * access_verify(i) -
252  *
253  *	This function verify the accessibility of the
254  *	the testfile with the one verified by access().
255  */
access_verify(int i)256 static int access_verify(int i)
257 {
258 	char write_buf[] = "abc";
259 	char read_buf[BUFSIZ];
260 	int rval;
261 
262 	rval = 0;
263 
264 	switch (i) {
265 	case 0:
266 		/*
267 		 * The specified file has read access.
268 		 * Attempt to read some data from the testfile
269 		 * and if successful, access() behaviour is
270 		 * correct.
271 		 */
272 		rval = read(fd1, &read_buf, sizeof(read_buf));
273 		if (rval == -1)
274 			tst_resm(TFAIL | TERRNO, "read(%s) failed", TEST_FILE1);
275 		break;
276 	case 1:
277 		/*
278 		 * The specified file has write access.
279 		 * Attempt to write some data to the testfile
280 		 * and if successful, access() behaviour is correct.
281 		 */
282 		rval = write(fd2, write_buf, strlen(write_buf));
283 		if (rval == -1)
284 			tst_resm(TFAIL | TERRNO, "write(%s) failed",
285 				 TEST_FILE2);
286 		break;
287 	case 2:
288 		/*
289 		 * The specified file has execute access.
290 		 * Attempt to execute the specified executable
291 		 * file, if successful, access() behaviour is correct.
292 		 */
293 		rval = system("./" TEST_FILE3);
294 		if (rval != 0)
295 			tst_resm(TFAIL, "Fail to execute the %s", TEST_FILE3);
296 		break;
297 	case 3:
298 		/*
299 		 * The file pointed to by symbolic link has
300 		 * write access.
301 		 * Attempt to write some data to this temporary file
302 		 * pointed to by symlink. if successful, access() bahaviour
303 		 * is correct.
304 		 */
305 		rval = write(fd4, write_buf, strlen(write_buf));
306 		if (rval == -1)
307 			tst_resm(TFAIL | TERRNO, "write(%s) failed", TEMP_FILE);
308 		break;
309 	default:
310 		break;
311 	}
312 
313 	return rval;
314 }
315 
cleanup(void)316 static void cleanup(void)
317 {
318 	if (close(fd1) == -1)
319 		tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TEST_FILE1);
320 	if (close(fd2) == -1)
321 		tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TEST_FILE2);
322 	if (close(fd4) == -1)
323 		tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TEMP_FILE);
324 
325 	tst_rmdir();
326 }
327