1433d6423SLionel Sambuc /* test33: access() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2433d6423SLionel Sambuc
3433d6423SLionel Sambuc #include <sys/types.h>
4433d6423SLionel Sambuc #include <sys/stat.h>
5433d6423SLionel Sambuc #include <sys/wait.h>
6433d6423SLionel Sambuc #include <stdlib.h>
7433d6423SLionel Sambuc #include <unistd.h>
8433d6423SLionel Sambuc #include <string.h>
9433d6423SLionel Sambuc #include <fcntl.h>
10433d6423SLionel Sambuc #include <limits.h>
11433d6423SLionel Sambuc #include <errno.h>
12433d6423SLionel Sambuc #include <time.h>
13433d6423SLionel Sambuc #include <stdio.h>
14433d6423SLionel Sambuc
15433d6423SLionel Sambuc int max_error = 1;
16433d6423SLionel Sambuc #include "common.h"
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc #define ITERATIONS 2
19433d6423SLionel Sambuc
200a6a1f1dSLionel Sambuc #define System(cmd) if (system_p(cmd) != 0) printf("``%s'' failed\n", cmd)
21433d6423SLionel Sambuc #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
22433d6423SLionel Sambuc #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
23433d6423SLionel Sambuc #define Chmod(a,b) if (chmod(a,b) != 0) printf("Can't chmod %s\n", a)
24433d6423SLionel Sambuc #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
25433d6423SLionel Sambuc
26433d6423SLionel Sambuc int superuser; /* nonzero if uid == euid (euid == 0 always) */
27433d6423SLionel Sambuc char *MaxName; /* Name of maximum length */
28433d6423SLionel Sambuc char MaxPath[PATH_MAX]; /* Same for path */
29433d6423SLionel Sambuc char *ToLongName; /* Name of maximum +1 length */
30433d6423SLionel Sambuc char ToLongPath[PATH_MAX + 1]; /* Same for path, both too long */
31433d6423SLionel Sambuc
32433d6423SLionel Sambuc void test33a(void);
33433d6423SLionel Sambuc void test33b(void);
34433d6423SLionel Sambuc void test33c(void);
35433d6423SLionel Sambuc void test33d(void);
36433d6423SLionel Sambuc void test_access(void);
37433d6423SLionel Sambuc void makelongnames(void);
38433d6423SLionel Sambuc
main(int argc,char * argv[])39433d6423SLionel Sambuc int main(int argc, char *argv[])
40433d6423SLionel Sambuc {
41433d6423SLionel Sambuc int i, m = 0xFFFF;
42433d6423SLionel Sambuc
43433d6423SLionel Sambuc sync();
44433d6423SLionel Sambuc if (argc == 2) m = atoi(argv[1]);
45433d6423SLionel Sambuc umask(0000);
46433d6423SLionel Sambuc start(33);
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc if (geteuid() != 0) {
49433d6423SLionel Sambuc printf("must be setuid root; test aborted\n");
50433d6423SLionel Sambuc cleanup();
51433d6423SLionel Sambuc exit(1);
52433d6423SLionel Sambuc }
53433d6423SLionel Sambuc if (getuid() == 0) {
54433d6423SLionel Sambuc printf("must be setuid root logged in as someone else; test aborted\n");
55433d6423SLionel Sambuc cleanup();
56433d6423SLionel Sambuc exit(1);
57433d6423SLionel Sambuc }
58433d6423SLionel Sambuc
59433d6423SLionel Sambuc makelongnames();
60433d6423SLionel Sambuc superuser = (getuid() == 0);
61433d6423SLionel Sambuc
62433d6423SLionel Sambuc for (i = 0; i < ITERATIONS; i++) {
63433d6423SLionel Sambuc if (m & 0001) test33a();
64433d6423SLionel Sambuc if (m & 0002) test33b();
65433d6423SLionel Sambuc if (m & 0004) test33c();
66433d6423SLionel Sambuc if (m & 0010) test33d();
67433d6423SLionel Sambuc }
68433d6423SLionel Sambuc quit();
69433d6423SLionel Sambuc return 1;
70433d6423SLionel Sambuc }
71433d6423SLionel Sambuc
test33a()72433d6423SLionel Sambuc void test33a()
73433d6423SLionel Sambuc { /* Test normal operation. */
74433d6423SLionel Sambuc int stat_loc; /* For the wait(&stat_loc) call. */
75433d6423SLionel Sambuc
76433d6423SLionel Sambuc subtest = 1;
77433d6423SLionel Sambuc System("rm -rf ../DIR_33/*");
78433d6423SLionel Sambuc
79433d6423SLionel Sambuc /* To test normal access first make some files for real uid. */
80433d6423SLionel Sambuc switch (fork()) {
81433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
82433d6423SLionel Sambuc case 0:
83433d6423SLionel Sambuc alarm(20);
84433d6423SLionel Sambuc setuid(getuid()); /* (Re)set the effective ids to the
85433d6423SLionel Sambuc * real ids. */
86433d6423SLionel Sambuc setgid(getgid());
87433d6423SLionel Sambuc System("> rwx; chmod 700 rwx");
88433d6423SLionel Sambuc System("> rw_; chmod 600 rw_");
89433d6423SLionel Sambuc System("> r_x; chmod 500 r_x");
90433d6423SLionel Sambuc System("> r__; chmod 400 r__");
91433d6423SLionel Sambuc System("> _wx; chmod 300 _wx");
92433d6423SLionel Sambuc System("> _w_; chmod 200 _w_");
93433d6423SLionel Sambuc System("> __x; chmod 100 __x");
94433d6423SLionel Sambuc System("> ___; chmod 000 ___");
95433d6423SLionel Sambuc exit(0);
96433d6423SLionel Sambuc
97433d6423SLionel Sambuc default:
98433d6423SLionel Sambuc wait(&stat_loc);
99433d6423SLionel Sambuc if (stat_loc != 0) e(1);/* Alarm? */
100433d6423SLionel Sambuc }
101433d6423SLionel Sambuc test_access();
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc /* Let's test access() on directorys. */
104433d6423SLionel Sambuc switch (fork()) {
105433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
106433d6423SLionel Sambuc case 0:
107433d6423SLionel Sambuc alarm(20);
108433d6423SLionel Sambuc setuid(getuid()); /* (Re)set the effective ids to the
109433d6423SLionel Sambuc * real ids. */
110433d6423SLionel Sambuc setgid(getgid());
111433d6423SLionel Sambuc System("rm -rf [_r][_w][_x]");
112433d6423SLionel Sambuc System("mkdir rwx; chmod 700 rwx");
113433d6423SLionel Sambuc System("mkdir rw_; chmod 600 rw_");
114433d6423SLionel Sambuc System("mkdir r_x; chmod 500 r_x");
115433d6423SLionel Sambuc System("mkdir r__; chmod 400 r__");
116433d6423SLionel Sambuc System("mkdir _wx; chmod 300 _wx");
117433d6423SLionel Sambuc System("mkdir _w_; chmod 200 _w_");
118433d6423SLionel Sambuc System("mkdir __x; chmod 100 __x");
119433d6423SLionel Sambuc System("mkdir ___; chmod 000 ___");
120433d6423SLionel Sambuc exit(0);
121433d6423SLionel Sambuc
122433d6423SLionel Sambuc default:
123433d6423SLionel Sambuc wait(&stat_loc);
124433d6423SLionel Sambuc if (stat_loc != 0) e(2);/* Alarm? */
125433d6423SLionel Sambuc }
126433d6423SLionel Sambuc test_access();
127433d6423SLionel Sambuc
128433d6423SLionel Sambuc switch (fork()) {
129433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
130433d6423SLionel Sambuc case 0:
131433d6423SLionel Sambuc alarm(20);
132433d6423SLionel Sambuc setuid(getuid()); /* (Re)set the effective ids to the
133433d6423SLionel Sambuc * real ids. */
134433d6423SLionel Sambuc setgid(getgid());
135433d6423SLionel Sambuc System("rmdir [_r][_w][_x]");
136433d6423SLionel Sambuc Mkfifo("rwx");
137433d6423SLionel Sambuc System("chmod 700 rwx");
138433d6423SLionel Sambuc Mkfifo("rw_");
139433d6423SLionel Sambuc System("chmod 600 rw_");
140433d6423SLionel Sambuc Mkfifo("r_x");
141433d6423SLionel Sambuc System("chmod 500 r_x");
142433d6423SLionel Sambuc Mkfifo("r__");
143433d6423SLionel Sambuc System("chmod 400 r__");
144433d6423SLionel Sambuc Mkfifo("_wx");
145433d6423SLionel Sambuc System("chmod 300 _wx");
146433d6423SLionel Sambuc Mkfifo("_w_");
147433d6423SLionel Sambuc System("chmod 200 _w_");
148433d6423SLionel Sambuc Mkfifo("__x");
149433d6423SLionel Sambuc System("chmod 100 __x");
150433d6423SLionel Sambuc Mkfifo("___");
151433d6423SLionel Sambuc System("chmod 000 ___");
152433d6423SLionel Sambuc exit(0);
153433d6423SLionel Sambuc
154433d6423SLionel Sambuc default:
155433d6423SLionel Sambuc wait(&stat_loc);
156433d6423SLionel Sambuc if (stat_loc != 0) e(3);/* Alarm? */
157433d6423SLionel Sambuc }
158433d6423SLionel Sambuc test_access();
159433d6423SLionel Sambuc
160433d6423SLionel Sambuc /* Remove all the fifos. */
161433d6423SLionel Sambuc switch (fork()) {
162433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
163433d6423SLionel Sambuc case 0:
164433d6423SLionel Sambuc alarm(20);
165433d6423SLionel Sambuc setuid(getuid());
166433d6423SLionel Sambuc setgid(getgid());
167433d6423SLionel Sambuc System("rm -rf [_r][_w][_x]");
168433d6423SLionel Sambuc exit(0);
169433d6423SLionel Sambuc
170433d6423SLionel Sambuc default:
171433d6423SLionel Sambuc wait(&stat_loc);
172433d6423SLionel Sambuc if (stat_loc != 0) e(4);/* Alarm? */
173433d6423SLionel Sambuc }
174433d6423SLionel Sambuc }
175433d6423SLionel Sambuc
test33b()176433d6423SLionel Sambuc void test33b()
177433d6423SLionel Sambuc {
178433d6423SLionel Sambuc int stat_loc; /* For the wait(&stat_loc) call. */
179433d6423SLionel Sambuc
180433d6423SLionel Sambuc subtest = 2;
181433d6423SLionel Sambuc System("rm -rf ../DIR_33/*");
182433d6423SLionel Sambuc
183433d6423SLionel Sambuc switch (fork()) {
184433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
185433d6423SLionel Sambuc case 0:
186433d6423SLionel Sambuc alarm(20);
187433d6423SLionel Sambuc
188433d6423SLionel Sambuc /* (Re)set the effective ids to the real ids. */
189433d6423SLionel Sambuc setuid(getuid());
190433d6423SLionel Sambuc setgid(getgid());
191433d6423SLionel Sambuc System("> ______rwx; chmod 007 ______rwx");
192433d6423SLionel Sambuc System("> ________x; chmod 001 ________x");
193433d6423SLionel Sambuc System("> _________; chmod 000 _________");
194433d6423SLionel Sambuc exit(0);
195433d6423SLionel Sambuc
196433d6423SLionel Sambuc default:
197433d6423SLionel Sambuc wait(&stat_loc);
198433d6423SLionel Sambuc if (stat_loc != 0) e(1);/* Alarm? */
199433d6423SLionel Sambuc }
200433d6423SLionel Sambuc
201433d6423SLionel Sambuc /* If we are superuser, we have access to all. */
202433d6423SLionel Sambuc /* Well, almost, execution access might need at least one X bit. */
203433d6423SLionel Sambuc if (superuser) {
204433d6423SLionel Sambuc if (access("_________", R_OK) != 0) e(2);
205433d6423SLionel Sambuc if (access("_________", W_OK) != 0) e(3);
206433d6423SLionel Sambuc if (access("________x", R_OK) != 0) e(4);
207433d6423SLionel Sambuc if (access("________x", W_OK) != 0) e(5);
208433d6423SLionel Sambuc if (access("________x", X_OK) != 0) e(6);
209433d6423SLionel Sambuc if (access("______rwx", R_OK) != 0) e(7);
210433d6423SLionel Sambuc if (access("______rwx", W_OK) != 0) e(8);
211433d6423SLionel Sambuc if (access("______rwx", X_OK) != 0) e(9);
212433d6423SLionel Sambuc }
213433d6423SLionel Sambuc if (!superuser) {
214433d6423SLionel Sambuc if (access("_________", R_OK) != -1) e(10);
215433d6423SLionel Sambuc if (errno != EACCES) e(11);
216433d6423SLionel Sambuc if (access("_________", W_OK) != -1) e(12);
217433d6423SLionel Sambuc if (errno != EACCES) e(13);
218433d6423SLionel Sambuc if (access("_________", X_OK) != -1) e(14);
219433d6423SLionel Sambuc if (errno != EACCES) e(15);
220433d6423SLionel Sambuc if (access("________x", R_OK) != -1) e(16);
221433d6423SLionel Sambuc if (errno != EACCES) e(17);
222433d6423SLionel Sambuc if (access("________x", W_OK) != -1) e(18);
223433d6423SLionel Sambuc if (errno != EACCES) e(19);
224433d6423SLionel Sambuc if (access("________x", X_OK) != -1) e(20);
225433d6423SLionel Sambuc if (errno != EACCES) e(21);
226433d6423SLionel Sambuc if (access("______rwx", R_OK) != -1) e(22);
227433d6423SLionel Sambuc if (errno != EACCES) e(23);
228433d6423SLionel Sambuc if (access("______rwx", W_OK) != -1) e(24);
229433d6423SLionel Sambuc if (errno != EACCES) e(25);
230433d6423SLionel Sambuc if (access("______rwx", X_OK) != -1) e(26);
231433d6423SLionel Sambuc if (errno != EACCES) e(27);
232433d6423SLionel Sambuc }
233433d6423SLionel Sambuc
234433d6423SLionel Sambuc /* If the real uid != effective uid. */
235433d6423SLionel Sambuc if (!superuser) {
236433d6423SLionel Sambuc System("rm -rf [_r][_w][_x]");
237433d6423SLionel Sambuc System("> rwx");
238433d6423SLionel Sambuc Chmod("rwx", 0700);
239433d6423SLionel Sambuc System("> rw_");
240433d6423SLionel Sambuc Chmod("rw_", 0600);
241433d6423SLionel Sambuc System("> r_x");
242433d6423SLionel Sambuc Chmod("r_x", 0500);
243433d6423SLionel Sambuc System("> r__");
244433d6423SLionel Sambuc Chmod("r__", 0400);
245433d6423SLionel Sambuc System("> _wx");
246433d6423SLionel Sambuc Chmod("_wx", 0300);
247433d6423SLionel Sambuc System("> _w_");
248433d6423SLionel Sambuc Chmod("_w_", 0200);
249433d6423SLionel Sambuc System("> __x");
250433d6423SLionel Sambuc Chmod("__x", 0100);
251433d6423SLionel Sambuc System("> ___");
252433d6423SLionel Sambuc Chmod("___", 0000);
253433d6423SLionel Sambuc
254433d6423SLionel Sambuc if (access("rwx", F_OK) != 0) e(28);
255433d6423SLionel Sambuc if (access("rwx", R_OK) != -1) e(29);
256433d6423SLionel Sambuc if (errno != EACCES) e(30);
257433d6423SLionel Sambuc if (access("rwx", W_OK) != -1) e(31);
258433d6423SLionel Sambuc if (errno != EACCES) e(32);
259433d6423SLionel Sambuc if (access("rwx", X_OK) != -1) e(33);
260433d6423SLionel Sambuc if (errno != EACCES) e(34);
261433d6423SLionel Sambuc
262433d6423SLionel Sambuc if (access("rw_", F_OK) != 0) e(35);
263433d6423SLionel Sambuc if (access("rw_", R_OK) != -1) e(36);
264433d6423SLionel Sambuc if (errno != EACCES) e(37);
265433d6423SLionel Sambuc if (access("rw_", W_OK) != -1) e(38);
266433d6423SLionel Sambuc if (errno != EACCES) e(39);
267433d6423SLionel Sambuc if (access("rw_", X_OK) != -1) e(40);
268433d6423SLionel Sambuc if (errno != EACCES) e(41);
269433d6423SLionel Sambuc
270433d6423SLionel Sambuc if (access("r_x", F_OK) != 0) e(42);
271433d6423SLionel Sambuc if (access("r_x", R_OK) != -1) e(43);
272433d6423SLionel Sambuc if (errno != EACCES) e(44);
273433d6423SLionel Sambuc if (access("r_x", W_OK) != -1) e(45);
274433d6423SLionel Sambuc if (errno != EACCES) e(46);
275433d6423SLionel Sambuc if (access("r_x", X_OK) != -1) e(47);
276433d6423SLionel Sambuc if (errno != EACCES) e(48);
277433d6423SLionel Sambuc
278433d6423SLionel Sambuc if (access("r__", F_OK) != 0) e(49);
279433d6423SLionel Sambuc if (access("r__", R_OK) != -1) e(50);
280433d6423SLionel Sambuc if (errno != EACCES) e(51);
281433d6423SLionel Sambuc if (access("r__", W_OK) != -1) e(52);
282433d6423SLionel Sambuc if (errno != EACCES) e(53);
283433d6423SLionel Sambuc if (access("r__", X_OK) != -1) e(54);
284433d6423SLionel Sambuc if (errno != EACCES) e(55);
285433d6423SLionel Sambuc
286433d6423SLionel Sambuc if (access("_wx", F_OK) != 0) e(56);
287433d6423SLionel Sambuc if (access("_wx", R_OK) != -1) e(57);
288433d6423SLionel Sambuc if (errno != EACCES) e(58);
289433d6423SLionel Sambuc if (access("_wx", W_OK) != -1) e(59);
290433d6423SLionel Sambuc if (errno != EACCES) e(60);
291433d6423SLionel Sambuc if (access("_wx", X_OK) != -1) e(61);
292433d6423SLionel Sambuc if (errno != EACCES) e(62);
293433d6423SLionel Sambuc
294433d6423SLionel Sambuc if (access("_w_", F_OK) != 0) e(63);
295433d6423SLionel Sambuc if (access("_w_", R_OK) != -1) e(64);
296433d6423SLionel Sambuc if (errno != EACCES) e(65);
297433d6423SLionel Sambuc if (access("_w_", W_OK) != -1) e(66);
298433d6423SLionel Sambuc if (errno != EACCES) e(67);
299433d6423SLionel Sambuc if (access("_w_", X_OK) != -1) e(68);
300433d6423SLionel Sambuc if (errno != EACCES) e(69);
301433d6423SLionel Sambuc
302433d6423SLionel Sambuc if (access("__x", F_OK) != 0) e(70);
303433d6423SLionel Sambuc if (access("__x", R_OK) != -1) e(71);
304433d6423SLionel Sambuc if (errno != EACCES) e(72);
305433d6423SLionel Sambuc if (access("__x", W_OK) != -1) e(73);
306433d6423SLionel Sambuc if (errno != EACCES) e(74);
307433d6423SLionel Sambuc if (access("__x", X_OK) != -1) e(75);
308433d6423SLionel Sambuc if (errno != EACCES) e(76);
309433d6423SLionel Sambuc
310433d6423SLionel Sambuc if (access("___", F_OK) != 0) e(77);
311433d6423SLionel Sambuc if (access("___", R_OK) != -1) e(78);
312433d6423SLionel Sambuc if (errno != EACCES) e(79);
313433d6423SLionel Sambuc if (access("___", W_OK) != -1) e(80);
314433d6423SLionel Sambuc if (errno != EACCES) e(81);
315433d6423SLionel Sambuc if (access("___", X_OK) != -1) e(82);
316433d6423SLionel Sambuc if (errno != EACCES) e(83);
317433d6423SLionel Sambuc
318433d6423SLionel Sambuc System("rm -rf [_r][_w][_x]");
319433d6423SLionel Sambuc }
320433d6423SLionel Sambuc }
321433d6423SLionel Sambuc
test33c()322433d6423SLionel Sambuc void test33c()
323433d6423SLionel Sambuc { /* Test errors returned. */
324433d6423SLionel Sambuc int i, fd, does_truncate;
325433d6423SLionel Sambuc
326433d6423SLionel Sambuc subtest = 3;
327433d6423SLionel Sambuc System("rm -rf ../DIR_33/*");
328433d6423SLionel Sambuc
329433d6423SLionel Sambuc /* Test what access() does with non existing files. */
330433d6423SLionel Sambuc System("rm -rf nonexist");
331433d6423SLionel Sambuc if (access("noexist", F_OK) != -1) e(1);
332433d6423SLionel Sambuc if (errno != ENOENT) e(2);
333433d6423SLionel Sambuc if (access("noexist", R_OK) != -1) e(3);
334433d6423SLionel Sambuc if (errno != ENOENT) e(4);
335433d6423SLionel Sambuc if (access("noexist", W_OK) != -1) e(5);
336433d6423SLionel Sambuc if (errno != ENOENT) e(6);
337433d6423SLionel Sambuc if (access("noexist", X_OK) != -1) e(7);
338433d6423SLionel Sambuc if (errno != ENOENT) e(8);
339433d6423SLionel Sambuc if (access("noexist", R_OK | W_OK) != -1) e(9);
340433d6423SLionel Sambuc if (errno != ENOENT) e(10);
341433d6423SLionel Sambuc if (access("noexist", R_OK | X_OK) != -1) e(11);
342433d6423SLionel Sambuc if (errno != ENOENT) e(12);
343433d6423SLionel Sambuc if (access("noexist", W_OK | X_OK) != -1) e(13);
344433d6423SLionel Sambuc if (errno != ENOENT) e(14);
345433d6423SLionel Sambuc if (access("noexist", R_OK | W_OK | X_OK) != -1) e(15);
346433d6423SLionel Sambuc if (errno != ENOENT) e(16);
347433d6423SLionel Sambuc
348433d6423SLionel Sambuc /* Test access on a nonsearchable path. */
349433d6423SLionel Sambuc if (mkdir("nosearch", 0777) != 0) e(1000);
350433d6423SLionel Sambuc if ( (i = creat("nosearch/file", 0666)) < 0) e(1001);
351433d6423SLionel Sambuc if (close(i) < 0) e(1002);
352*dc2c582fSDavid van Moolenbroek if ( (i = creat("file", 0666)) < 0) e(1003);
353433d6423SLionel Sambuc if (close(i) < 0) e(1004);
354433d6423SLionel Sambuc if (chmod("nosearch/file", 05777) < 0) e(1005);
355433d6423SLionel Sambuc if (chmod("file", 05777) < 0) e(1006);
356433d6423SLionel Sambuc if (chmod("nosearch", 0677) != 0) e(1007);
357433d6423SLionel Sambuc if (access("nosearch/file", F_OK) != 0) e(17);
358433d6423SLionel Sambuc
359433d6423SLionel Sambuc /* Test ToLongName and ToLongPath */
360433d6423SLionel Sambuc does_truncate = does_fs_truncate();
361433d6423SLionel Sambuc if (does_truncate) {
362*dc2c582fSDavid van Moolenbroek if ((fd = creat(ToLongName, 0777)) == -1) e(18);
363433d6423SLionel Sambuc if (close(fd) != 0) e(19);
364433d6423SLionel Sambuc if (access(ToLongName, F_OK) != 0) e(20);
365433d6423SLionel Sambuc } else {
366433d6423SLionel Sambuc if ((fd = creat(ToLongName, 0777)) != -1) e(21);
367433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(22);
368433d6423SLionel Sambuc (void) close(fd); /* Just in case */
369433d6423SLionel Sambuc if (access(ToLongName, F_OK) != -1) e(23);
370433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(24);
371433d6423SLionel Sambuc }
372433d6423SLionel Sambuc
373433d6423SLionel Sambuc ToLongPath[PATH_MAX - 2] = '/';
374433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = 'a';
375433d6423SLionel Sambuc if (access(ToLongPath, F_OK) != -1) e(27);
376433d6423SLionel Sambuc if (errno != ENAMETOOLONG) e(28);
377433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = '/';
378433d6423SLionel Sambuc
379433d6423SLionel Sambuc /* Test empty strings. */
380433d6423SLionel Sambuc if (access("", F_OK) != -1) e(29);
381433d6423SLionel Sambuc if (errno != ENOENT) e(30);
382433d6423SLionel Sambuc System("rm -rf idonotexist");
383433d6423SLionel Sambuc if (access("idonotexist", F_OK) != -1) e(31);
384433d6423SLionel Sambuc if (errno != ENOENT) e(32);
385433d6423SLionel Sambuc
386433d6423SLionel Sambuc /* Test non directorys in prefix of path. */
387433d6423SLionel Sambuc if (access("/etc/passwd/dir/foo", F_OK) != -1) e(33);
388433d6423SLionel Sambuc if (errno != ENOTDIR) e(34);
389433d6423SLionel Sambuc System("rm -rf nodir; > nodir");
390433d6423SLionel Sambuc if (access("nodir/foo", F_OK) != -1) e(35);
391433d6423SLionel Sambuc if (errno != ENOTDIR) e(36);
392433d6423SLionel Sambuc
393433d6423SLionel Sambuc /* Test if invalid amode arguments are signaled. */
394433d6423SLionel Sambuc System("> allmod");
395433d6423SLionel Sambuc Chmod("allmod", 05777);
396433d6423SLionel Sambuc for (i = -1025; i < 1025; i++) {
397433d6423SLionel Sambuc if ((mode_t) i != F_OK && ((mode_t) i & ~(R_OK | W_OK | X_OK)) != 0) {
398433d6423SLionel Sambuc if (access("allmod", (mode_t) i) != -1) e(37);
399433d6423SLionel Sambuc if (errno != EINVAL) e(38);
400433d6423SLionel Sambuc } else
401433d6423SLionel Sambuc if (access("allmod", (mode_t) i) != 0) e(39);
402433d6423SLionel Sambuc }
403433d6423SLionel Sambuc }
404433d6423SLionel Sambuc
test33d()405433d6423SLionel Sambuc void test33d()
406433d6423SLionel Sambuc { /* Test access() flags. */
407433d6423SLionel Sambuc #define EXCLUDE(a,b) (((a)^(b)) == ((a)|(b)))
408433d6423SLionel Sambuc subtest = 4;
409433d6423SLionel Sambuc System("rm -rf ../DIR_33/*");
410433d6423SLionel Sambuc
411433d6423SLionel Sambuc /* The test are rather strong, stronger that POSIX specifies. */
412433d6423SLionel Sambuc /* The should be OR able, this test tests if all the 1 bits */
413433d6423SLionel Sambuc /* Are in diferent places. This should be what one wants. */
414433d6423SLionel Sambuc if (!EXCLUDE(R_OK, W_OK | X_OK)) e(1);
415433d6423SLionel Sambuc if (!EXCLUDE(W_OK, R_OK | X_OK)) e(2);
416433d6423SLionel Sambuc if (!EXCLUDE(X_OK, R_OK | W_OK)) e(3);
417433d6423SLionel Sambuc if (F_OK == R_OK) e(4);
418433d6423SLionel Sambuc if (F_OK == W_OK) e(5);
419433d6423SLionel Sambuc if (F_OK == X_OK) e(6);
420433d6423SLionel Sambuc if (F_OK == (R_OK | W_OK)) e(7);
421433d6423SLionel Sambuc if (F_OK == (W_OK | X_OK)) e(8);
422433d6423SLionel Sambuc if (F_OK == (R_OK | X_OK)) e(9);
423433d6423SLionel Sambuc if (F_OK == (R_OK | W_OK | X_OK)) e(10);
424433d6423SLionel Sambuc }
425433d6423SLionel Sambuc
test_access()426433d6423SLionel Sambuc void test_access()
427433d6423SLionel Sambuc { /* Test all [_r][_w][_x] files. */
428433d6423SLionel Sambuc if (!superuser) {
429433d6423SLionel Sambuc /* Test normal access. */
430433d6423SLionel Sambuc if (access("rwx", F_OK) != 0) e(11);
431433d6423SLionel Sambuc if (access("rwx", R_OK) != 0) e(12);
432433d6423SLionel Sambuc if (access("rwx", W_OK) != 0) e(13);
433433d6423SLionel Sambuc if (access("rwx", X_OK) != 0) e(14);
434433d6423SLionel Sambuc if (access("rwx", R_OK | W_OK) != 0) e(15);
435433d6423SLionel Sambuc if (access("rwx", R_OK | X_OK) != 0) e(16);
436433d6423SLionel Sambuc if (access("rwx", W_OK | X_OK) != 0) e(17);
437433d6423SLionel Sambuc if (access("rwx", R_OK | W_OK | X_OK) != 0) e(18);
438433d6423SLionel Sambuc
439433d6423SLionel Sambuc if (access("rw_", F_OK) != 0) e(19);
440433d6423SLionel Sambuc if (access("rw_", R_OK) != 0) e(20);
441433d6423SLionel Sambuc if (access("rw_", W_OK) != 0) e(21);
442433d6423SLionel Sambuc if (access("rw_", X_OK) != -1) e(22);
443433d6423SLionel Sambuc if (errno != EACCES) e(23);
444433d6423SLionel Sambuc if (access("rw_", R_OK | W_OK) != 0) e(24);
445433d6423SLionel Sambuc if (access("rw_", R_OK | X_OK) != -1) e(25);
446433d6423SLionel Sambuc if (errno != EACCES) e(26);
447433d6423SLionel Sambuc if (access("rw_", W_OK | X_OK) != -1) e(27);
448433d6423SLionel Sambuc if (errno != EACCES) e(28);
449433d6423SLionel Sambuc if (access("rw_", R_OK | W_OK | X_OK) != -1) e(29);
450433d6423SLionel Sambuc if (errno != EACCES) e(30);
451433d6423SLionel Sambuc
452433d6423SLionel Sambuc if (access("r_x", F_OK) != 0) e(31);
453433d6423SLionel Sambuc if (access("r_x", R_OK) != 0) e(32);
454433d6423SLionel Sambuc if (access("r_x", W_OK) != -1) e(33);
455433d6423SLionel Sambuc if (errno != EACCES) e(34);
456433d6423SLionel Sambuc if (access("r_x", X_OK) != 0) e(35);
457433d6423SLionel Sambuc if (access("r_x", R_OK | W_OK) != -1) e(36);
458433d6423SLionel Sambuc if (errno != EACCES) e(37);
459433d6423SLionel Sambuc if (access("r_x", R_OK | X_OK) != 0) e(38);
460433d6423SLionel Sambuc if (access("r_x", W_OK | X_OK) != -1) e(39);
461433d6423SLionel Sambuc if (errno != EACCES) e(40);
462433d6423SLionel Sambuc if (access("r_x", R_OK | W_OK | X_OK) != -1) e(41);
463433d6423SLionel Sambuc if (errno != EACCES) e(42);
464433d6423SLionel Sambuc
465433d6423SLionel Sambuc if (access("r__", F_OK) != 0) e(43);
466433d6423SLionel Sambuc if (access("r__", R_OK) != 0) e(44);
467433d6423SLionel Sambuc if (access("r__", W_OK) != -1) e(45);
468433d6423SLionel Sambuc if (errno != EACCES) e(46);
469433d6423SLionel Sambuc if (access("r__", X_OK) != -1) e(47);
470433d6423SLionel Sambuc if (errno != EACCES) e(48);
471433d6423SLionel Sambuc if (access("r__", R_OK | W_OK) != -1) e(49);
472433d6423SLionel Sambuc if (errno != EACCES) e(50);
473433d6423SLionel Sambuc if (access("r__", R_OK | X_OK) != -1) e(51);
474433d6423SLionel Sambuc if (errno != EACCES) e(52);
475433d6423SLionel Sambuc if (access("r__", W_OK | X_OK) != -1) e(53);
476433d6423SLionel Sambuc if (errno != EACCES) e(54);
477433d6423SLionel Sambuc if (access("r__", R_OK | W_OK | X_OK) != -1) e(55);
478433d6423SLionel Sambuc if (errno != EACCES) e(56);
479433d6423SLionel Sambuc
480433d6423SLionel Sambuc if (access("_wx", F_OK) != 0) e(57);
481433d6423SLionel Sambuc if (access("_wx", R_OK) != -1) e(58);
482433d6423SLionel Sambuc if (errno != EACCES) e(59);
483433d6423SLionel Sambuc if (access("_wx", W_OK) != 0) e(60);
484433d6423SLionel Sambuc if (access("_wx", X_OK) != 0) e(61);
485433d6423SLionel Sambuc if (access("_wx", R_OK | W_OK) != -1) e(62);
486433d6423SLionel Sambuc if (errno != EACCES) e(63);
487433d6423SLionel Sambuc if (access("_wx", R_OK | X_OK) != -1) e(64);
488433d6423SLionel Sambuc if (errno != EACCES) e(65);
489433d6423SLionel Sambuc if (access("_wx", W_OK | X_OK) != 0) e(66);
490433d6423SLionel Sambuc if (access("_wx", R_OK | W_OK | X_OK) != -1) e(67);
491433d6423SLionel Sambuc if (errno != EACCES) e(68);
492433d6423SLionel Sambuc
493433d6423SLionel Sambuc if (access("_w_", F_OK) != 0) e(69);
494433d6423SLionel Sambuc if (access("_w_", R_OK) != -1) e(70);
495433d6423SLionel Sambuc if (errno != EACCES) e(71);
496433d6423SLionel Sambuc if (access("_w_", W_OK) != 0) e(72);
497433d6423SLionel Sambuc if (access("_w_", X_OK) != -1) e(73);
498433d6423SLionel Sambuc if (errno != EACCES) e(74);
499433d6423SLionel Sambuc if (access("_w_", R_OK | W_OK) != -1) e(75);
500433d6423SLionel Sambuc if (errno != EACCES) e(76);
501433d6423SLionel Sambuc if (access("_w_", R_OK | X_OK) != -1) e(77);
502433d6423SLionel Sambuc if (errno != EACCES) e(78);
503433d6423SLionel Sambuc if (access("_w_", W_OK | X_OK) != -1) e(79);
504433d6423SLionel Sambuc if (errno != EACCES) e(80);
505433d6423SLionel Sambuc if (access("_w_", R_OK | W_OK | X_OK) != -1) e(81);
506433d6423SLionel Sambuc if (errno != EACCES) e(82);
507433d6423SLionel Sambuc
508433d6423SLionel Sambuc if (access("__x", F_OK) != 0) e(83);
509433d6423SLionel Sambuc if (access("__x", R_OK) != -1) e(84);
510433d6423SLionel Sambuc if (errno != EACCES) e(85);
511433d6423SLionel Sambuc if (access("__x", W_OK) != -1) e(86);
512433d6423SLionel Sambuc if (errno != EACCES) e(87);
513433d6423SLionel Sambuc if (access("__x", X_OK) != 0) e(88);
514433d6423SLionel Sambuc if (access("__x", R_OK | W_OK) != -1) e(89);
515433d6423SLionel Sambuc if (errno != EACCES) e(90);
516433d6423SLionel Sambuc if (access("__x", R_OK | X_OK) != -1) e(91);
517433d6423SLionel Sambuc if (errno != EACCES) e(92);
518433d6423SLionel Sambuc if (access("__x", W_OK | X_OK) != -1) e(93);
519433d6423SLionel Sambuc if (errno != EACCES) e(94);
520433d6423SLionel Sambuc if (access("__x", R_OK | W_OK | X_OK) != -1) e(95);
521433d6423SLionel Sambuc if (errno != EACCES) e(96);
522433d6423SLionel Sambuc
523433d6423SLionel Sambuc if (access("___", F_OK) != 0) e(97);
524433d6423SLionel Sambuc if (access("___", R_OK) != -1) e(98);
525433d6423SLionel Sambuc if (errno != EACCES) e(99);
526433d6423SLionel Sambuc if (access("___", W_OK) != -1) e(100);
527433d6423SLionel Sambuc if (errno != EACCES) e(101);
528433d6423SLionel Sambuc if (access("___", X_OK) != -1) e(102);
529433d6423SLionel Sambuc if (errno != EACCES) e(103);
530433d6423SLionel Sambuc if (access("___", R_OK | W_OK) != -1) e(104);
531433d6423SLionel Sambuc if (errno != EACCES) e(105);
532433d6423SLionel Sambuc if (access("___", R_OK | X_OK) != -1) e(106);
533433d6423SLionel Sambuc if (errno != EACCES) e(107);
534433d6423SLionel Sambuc if (access("___", W_OK | X_OK) != -1) e(108);
535433d6423SLionel Sambuc if (errno != EACCES) e(109);
536433d6423SLionel Sambuc if (access("___", R_OK | W_OK | X_OK) != -1) e(110);
537433d6423SLionel Sambuc if (errno != EACCES) e(111);
538433d6423SLionel Sambuc }
539433d6423SLionel Sambuc if (superuser) {
540433d6423SLionel Sambuc /* Test root access don't test X_OK on [_r][_w]_ files. */
541433d6423SLionel Sambuc if (access("rwx", F_OK) != 0) e(112);
542433d6423SLionel Sambuc if (access("rwx", R_OK) != 0) e(113);
543433d6423SLionel Sambuc if (access("rwx", W_OK) != 0) e(114);
544433d6423SLionel Sambuc if (access("rwx", X_OK) != 0) e(115);
545433d6423SLionel Sambuc if (access("rwx", R_OK | W_OK) != 0) e(116);
546433d6423SLionel Sambuc if (access("rwx", R_OK | X_OK) != 0) e(117);
547433d6423SLionel Sambuc if (access("rwx", W_OK | X_OK) != 0) e(118);
548433d6423SLionel Sambuc if (access("rwx", R_OK | W_OK | X_OK) != 0) e(119);
549433d6423SLionel Sambuc
550433d6423SLionel Sambuc if (access("rw_", F_OK) != 0) e(120);
551433d6423SLionel Sambuc if (access("rw_", R_OK) != 0) e(121);
552433d6423SLionel Sambuc if (access("rw_", W_OK) != 0) e(122);
553433d6423SLionel Sambuc if (access("rw_", R_OK | W_OK) != 0) e(123);
554433d6423SLionel Sambuc
555433d6423SLionel Sambuc if (access("r_x", F_OK) != 0) e(124);
556433d6423SLionel Sambuc if (access("r_x", R_OK) != 0) e(125);
557433d6423SLionel Sambuc if (access("r_x", W_OK) != 0) e(126);
558433d6423SLionel Sambuc if (access("r_x", X_OK) != 0) e(127);
559433d6423SLionel Sambuc if (access("r_x", R_OK | W_OK) != 0) e(128);
560433d6423SLionel Sambuc if (access("r_x", R_OK | X_OK) != 0) e(129);
561433d6423SLionel Sambuc if (access("r_x", W_OK | X_OK) != 0) e(130);
562433d6423SLionel Sambuc if (access("r_x", R_OK | W_OK | X_OK) != 0) e(131);
563433d6423SLionel Sambuc
564433d6423SLionel Sambuc if (access("r__", F_OK) != 0) e(132);
565433d6423SLionel Sambuc if (access("r__", R_OK) != 0) e(133);
566433d6423SLionel Sambuc if (access("r__", W_OK) != 0) e(134);
567433d6423SLionel Sambuc if (access("r__", R_OK | W_OK) != 0) e(135);
568433d6423SLionel Sambuc
569433d6423SLionel Sambuc if (access("_wx", F_OK) != 0) e(136);
570433d6423SLionel Sambuc if (access("_wx", R_OK) != 0) e(137);
571433d6423SLionel Sambuc if (access("_wx", W_OK) != 0) e(138);
572433d6423SLionel Sambuc if (access("_wx", X_OK) != 0) e(139);
573433d6423SLionel Sambuc if (access("_wx", R_OK | W_OK) != 0) e(140);
574433d6423SLionel Sambuc if (access("_wx", R_OK | X_OK) != 0) e(141);
575433d6423SLionel Sambuc if (access("_wx", W_OK | X_OK) != 0) e(142);
576433d6423SLionel Sambuc if (access("_wx", R_OK | W_OK | X_OK) != 0) e(143);
577433d6423SLionel Sambuc
578433d6423SLionel Sambuc if (access("_w_", F_OK) != 0) e(144);
579433d6423SLionel Sambuc if (access("_w_", R_OK) != 0) e(145);
580433d6423SLionel Sambuc if (access("_w_", W_OK) != 0) e(146);
581433d6423SLionel Sambuc if (access("_w_", R_OK | W_OK) != 0) e(147);
582433d6423SLionel Sambuc
583433d6423SLionel Sambuc if (access("__x", F_OK) != 0) e(148);
584433d6423SLionel Sambuc if (access("__x", R_OK) != 0) e(149);
585433d6423SLionel Sambuc if (access("__x", W_OK) != 0) e(150);
586433d6423SLionel Sambuc if (access("__x", X_OK) != 0) e(151);
587433d6423SLionel Sambuc if (access("__x", R_OK | W_OK) != 0) e(152);
588433d6423SLionel Sambuc if (access("__x", R_OK | X_OK) != 0) e(153);
589433d6423SLionel Sambuc if (access("__x", W_OK | X_OK) != 0) e(154);
590433d6423SLionel Sambuc if (access("__x", R_OK | W_OK | X_OK) != 0) e(155);
591433d6423SLionel Sambuc
592433d6423SLionel Sambuc if (access("___", F_OK) != 0) e(156);
593433d6423SLionel Sambuc if (access("___", R_OK) != 0) e(157);
594433d6423SLionel Sambuc if (access("___", W_OK) != 0) e(158);
595433d6423SLionel Sambuc if (access("___", R_OK | W_OK) != 0) e(159);
596433d6423SLionel Sambuc }
597433d6423SLionel Sambuc }
598433d6423SLionel Sambuc
makelongnames()599433d6423SLionel Sambuc void makelongnames()
600433d6423SLionel Sambuc {
601433d6423SLionel Sambuc register int i;
602433d6423SLionel Sambuc int max_name_length;
603433d6423SLionel Sambuc
604433d6423SLionel Sambuc max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
605433d6423SLionel Sambuc * the same length, hence runtime check */
606433d6423SLionel Sambuc MaxName = malloc(max_name_length + 1);
607433d6423SLionel Sambuc ToLongName = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
608433d6423SLionel Sambuc memset(MaxName, 'a', max_name_length);
609433d6423SLionel Sambuc MaxName[max_name_length] = '\0';
610433d6423SLionel Sambuc
611433d6423SLionel Sambuc for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
612433d6423SLionel Sambuc MaxPath[i++] = '.';
613433d6423SLionel Sambuc MaxPath[i] = '/';
614433d6423SLionel Sambuc }
615433d6423SLionel Sambuc MaxPath[PATH_MAX - 1] = '\0';
616433d6423SLionel Sambuc
617433d6423SLionel Sambuc strcpy(ToLongName, MaxName); /* copy them Max to ToLong */
618433d6423SLionel Sambuc strcpy(ToLongPath, MaxPath);
619433d6423SLionel Sambuc
620433d6423SLionel Sambuc ToLongName[max_name_length] = 'a';
621433d6423SLionel Sambuc ToLongName[max_name_length+1] = '\0';/* extend ToLongName by one too many */
622433d6423SLionel Sambuc ToLongPath[PATH_MAX - 1] = '/';
623433d6423SLionel Sambuc ToLongPath[PATH_MAX] = '\0'; /* inc ToLongPath by one */
624433d6423SLionel Sambuc }
625