xref: /minix3/minix/tests/test34.c (revision 7c48de6cc4c6d56f2277d378dba01dbac8a8c3b9)
1433d6423SLionel Sambuc /* test34: chmod() chown() 	Author: Jan-Mark Wams (jms@cs.vu.nl) */
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc /* There is a problem getting valid uids and gids, so we use the passwd
4433d6423SLionel Sambuc ** file (ie. /etc/passwd). I don't like this, but I see no other way.
5433d6423SLionel Sambuc ** The read-only-device-error (EROFS) is not checked!
6433d6423SLionel Sambuc ** Supplementary group IDs are ignored.
7433d6423SLionel Sambuc */
8433d6423SLionel Sambuc 
9433d6423SLionel Sambuc #include <sys/types.h>
10433d6423SLionel Sambuc #include <sys/stat.h>
11433d6423SLionel Sambuc #include <sys/wait.h>
12433d6423SLionel Sambuc #include <stdlib.h>
13433d6423SLionel Sambuc #include <unistd.h>
14433d6423SLionel Sambuc #include <string.h>
15433d6423SLionel Sambuc #include <fcntl.h>
16433d6423SLionel Sambuc #include <limits.h>
17433d6423SLionel Sambuc #include <errno.h>
18433d6423SLionel Sambuc #include <ctype.h>
19433d6423SLionel Sambuc #include <time.h>
20433d6423SLionel Sambuc #include <stdio.h>
21433d6423SLionel Sambuc 
22433d6423SLionel Sambuc int max_error = 	4;
23433d6423SLionel Sambuc #include "common.h"
24433d6423SLionel Sambuc 
25433d6423SLionel Sambuc #define ITERATIONS      4
26433d6423SLionel Sambuc #define N 100
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc #define ALL_RWXB	(S_IRWXU | S_IRWXG | S_IRWXO)
30433d6423SLionel Sambuc #define ALL_SETB	(S_ISUID | S_ISGID)
31433d6423SLionel Sambuc #define ALL_BITS	(ALL_RWXB | ALL_SETB)
32433d6423SLionel Sambuc 
33433d6423SLionel Sambuc #define System(cmd)   if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
34433d6423SLionel Sambuc #define Chdir(dir)    if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
35433d6423SLionel Sambuc #define Stat(a,b)     if (stat(a,b) != 0) printf("Can't stat %s\n", a)
36433d6423SLionel Sambuc #define Mkfifo(f)     if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
37433d6423SLionel Sambuc #define Mkdir(f)      if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
38433d6423SLionel Sambuc #define Creat(f)      if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc /* This program uses /etc/passwd and assumes things about it's contents. */
41433d6423SLionel Sambuc #define PASSWD_FILE	"/etc/passwd"
42433d6423SLionel Sambuc 
43433d6423SLionel Sambuc int superuser;
44433d6423SLionel Sambuc int I_can_chown;
45433d6423SLionel Sambuc char *MaxName;			/* Name of maximum length */
46433d6423SLionel Sambuc char MaxPath[PATH_MAX];		/* Same for path */
47433d6423SLionel Sambuc char *NameTooLong;		/* Name of maximum +1 length */
48433d6423SLionel Sambuc char PathTooLong[PATH_MAX + 1];	/* Same for path, both too long */
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc void test34a(void);
51433d6423SLionel Sambuc void test34b(void);
52433d6423SLionel Sambuc void test34c(void);
53433d6423SLionel Sambuc mode_t mode(char *file_name);
54433d6423SLionel Sambuc void makelongnames(void);
55433d6423SLionel Sambuc void getids(uid_t * uid, gid_t * gid);
56433d6423SLionel Sambuc 
main(int argc,char * argv[])57433d6423SLionel Sambuc int main(int argc, char *argv[])
58433d6423SLionel Sambuc {
59433d6423SLionel Sambuc   int i, m = 0xFFFF;
60433d6423SLionel Sambuc 
61433d6423SLionel Sambuc   sync();
62433d6423SLionel Sambuc   if (argc == 2) m = atoi(argv[1]);
63433d6423SLionel Sambuc   umask(0000);
64433d6423SLionel Sambuc   start(34);
65433d6423SLionel Sambuc   makelongnames();
66433d6423SLionel Sambuc   superuser = (geteuid() == (uid_t) 0);
67433d6423SLionel Sambuc 
68433d6423SLionel Sambuc #ifdef _POSIX_CHOWN_RESTRICTED
69433d6423SLionel Sambuc   I_can_chown = superuser;
70433d6423SLionel Sambuc #else
71433d6423SLionel Sambuc   I_can_chown = 1;
72433d6423SLionel Sambuc #endif
73433d6423SLionel Sambuc 
74433d6423SLionel Sambuc 
75433d6423SLionel Sambuc   for (i = 1; i < ITERATIONS; i++) {
76433d6423SLionel Sambuc 	if (m & 0001) test34a();
77433d6423SLionel Sambuc 	if (m & 0002) test34b();
78433d6423SLionel Sambuc 	if (m & 0004) test34c();
79433d6423SLionel Sambuc   }
80433d6423SLionel Sambuc   quit();
81433d6423SLionel Sambuc 
82433d6423SLionel Sambuc   return(-1);	/* Unreachable */
83433d6423SLionel Sambuc }
84433d6423SLionel Sambuc 
test34a()85433d6423SLionel Sambuc void test34a()
86433d6423SLionel Sambuc {				/* Test normal operation. */
87433d6423SLionel Sambuc   time_t time1, time2;
88433d6423SLionel Sambuc   mode_t mod;
89433d6423SLionel Sambuc   struct stat st1, st2;
90433d6423SLionel Sambuc   int cnt;
91433d6423SLionel Sambuc   uid_t uid, uid2;
92433d6423SLionel Sambuc   gid_t gid, gid2;
93433d6423SLionel Sambuc   int stat_loc;
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc   subtest = 1;
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc   /* Make scratch file. */
98433d6423SLionel Sambuc   Creat("foo");
99433d6423SLionel Sambuc 
100433d6423SLionel Sambuc   for (mod = 0; mod <= ALL_BITS; mod++) {
101433d6423SLionel Sambuc 	if ((mod & ALL_BITS) != mod)	/* If not a valid mod next. */
102433d6423SLionel Sambuc 		continue;
103433d6423SLionel Sambuc 	Stat("foo", &st1);
104433d6423SLionel Sambuc 	if (time(&time1) == (time_t) - 1) e(1);
105433d6423SLionel Sambuc 	if (chmod("foo", mod) != 0) e(2);
106433d6423SLionel Sambuc 	Stat("foo", &st2);
107433d6423SLionel Sambuc 	if (time(&time2) == (time_t) - 1) e(3);
108433d6423SLionel Sambuc 	if (superuser)
109433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_BITS) != mod) e(4);
110433d6423SLionel Sambuc 	if (!superuser)
111433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(5);
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc 	/* Test the C time feald. */
114433d6423SLionel Sambuc 	if (st1.st_ctime > st2.st_ctime) e(6);
115433d6423SLionel Sambuc 	if (st1.st_ctime > time1) e(7);
116433d6423SLionel Sambuc 	if (st1.st_ctime > time2) e(8);
117433d6423SLionel Sambuc #ifndef V1_FILESYSTEM
118433d6423SLionel Sambuc 	if (st2.st_ctime < time1) e(9);
119433d6423SLionel Sambuc #endif
120433d6423SLionel Sambuc 	if (st2.st_ctime > time2) e(10);
121433d6423SLionel Sambuc 	if (st1.st_atime != st2.st_atime) e(11);
122433d6423SLionel Sambuc 	if (st1.st_mtime != st2.st_mtime) e(12);
123433d6423SLionel Sambuc   }				/* End for loop. */
124433d6423SLionel Sambuc 
125433d6423SLionel Sambuc   /* Check if chown(file, geteuid(), getegid()) works. */
126433d6423SLionel Sambuc   for (cnt = 0; cnt < 20; cnt++) {
127433d6423SLionel Sambuc 	/* Set all rights on foo, including the set .id bits. */
128433d6423SLionel Sambuc 	if (chmod("foo", ALL_BITS) != 0) e(13);
129433d6423SLionel Sambuc 	Stat("foo", &st1);
130433d6423SLionel Sambuc 	if (time(&time1) == (time_t) -1) e(14);
131433d6423SLionel Sambuc 
132433d6423SLionel Sambuc 	if (chown("foo", geteuid(), getegid()) != 0) e(15);
133433d6423SLionel Sambuc 	Stat("foo", &st2);
134433d6423SLionel Sambuc 	if (time(&time2) == (time_t) -1) e(16);
135433d6423SLionel Sambuc 
136433d6423SLionel Sambuc 	/* Check ``chown()'' killed the set .id bits. */
137433d6423SLionel Sambuc 	if (!superuser) {
138433d6423SLionel Sambuc 		if ((st1.st_mode & ALL_RWXB) != ALL_RWXB) e(17);
139433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_BITS) != ALL_RWXB) e(18);
140433d6423SLionel Sambuc 	}
141433d6423SLionel Sambuc 	if (superuser) {
142433d6423SLionel Sambuc 		if ((st1.st_mode & ALL_BITS) != ALL_BITS) e(19);
143433d6423SLionel Sambuc 		if ((st1.st_mode & ALL_RWXB) != ALL_RWXB) e(20);
144433d6423SLionel Sambuc 	}
145433d6423SLionel Sambuc 
146433d6423SLionel Sambuc 	/* Check the timing. */
147433d6423SLionel Sambuc 	if (st1.st_ctime > st2.st_ctime) e(21);
148433d6423SLionel Sambuc 	if (st1.st_ctime > time1) e(22);
149433d6423SLionel Sambuc 	if (st1.st_ctime > time2) e(23);
150433d6423SLionel Sambuc #ifndef V1_FILESYSTEM
151433d6423SLionel Sambuc 	if (st2.st_ctime < time1) e(24);
152433d6423SLionel Sambuc #endif
153433d6423SLionel Sambuc 	if (st2.st_ctime > time2) e(25);
154433d6423SLionel Sambuc 	if (st1.st_atime != st2.st_atime) e(26);
155433d6423SLionel Sambuc 	if (st1.st_mtime != st2.st_mtime) e(27);
156433d6423SLionel Sambuc   }				/* End for loop. */
157433d6423SLionel Sambuc 
158433d6423SLionel Sambuc   /* Make scratch file. */
159433d6423SLionel Sambuc   if (chmod("foo", ALL_RWXB) != 0) e(28);
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc   if (I_can_chown) {
162433d6423SLionel Sambuc 	/* Do a 20 tests on a gid and uid. */
163433d6423SLionel Sambuc 	for (cnt = 0; cnt < 20; cnt++) {
164433d6423SLionel Sambuc 		/* Get a uid and a gid, test chown. */
165433d6423SLionel Sambuc 		getids(&uid, &gid);
166433d6423SLionel Sambuc 		Stat("foo", &st1);
167433d6423SLionel Sambuc 		if (time(&time1) == (time_t) -1) e(29);
168433d6423SLionel Sambuc 		if (chown("foo", (uid_t) 0, (gid_t) 0) != 0) e(30);
169433d6423SLionel Sambuc 		Stat("foo", &st2);
170433d6423SLionel Sambuc 		if (time(&time2) == (time_t) -1) e(31);
171433d6423SLionel Sambuc 
172433d6423SLionel Sambuc 		/* Test the C time field. */
173433d6423SLionel Sambuc 		if (st1.st_ctime > st2.st_ctime) e(32);
174433d6423SLionel Sambuc 		if (st1.st_ctime > time1) e(33);
175433d6423SLionel Sambuc 		if (st1.st_ctime > time2) e(34);
176433d6423SLionel Sambuc 		if (st2.st_ctime < time1) e(35);
177433d6423SLionel Sambuc 		if (st2.st_ctime > time2) e(36);
178433d6423SLionel Sambuc 		if (st1.st_atime != st2.st_atime) e(37);
179433d6423SLionel Sambuc 		if (st1.st_mtime != st2.st_mtime) e(38);
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc 		/* Do aditional tests. */
182433d6423SLionel Sambuc 		if (chown("foo", (uid_t) 0, gid) != 0) e(39);
183433d6423SLionel Sambuc 		if (chown("foo", uid, (gid_t) 0) != 0) e(40);
184433d6423SLionel Sambuc 		if (chown("foo", uid, gid) != 0) e(41);
185433d6423SLionel Sambuc 	}
186433d6423SLionel Sambuc   }
187433d6423SLionel Sambuc   if (superuser) {
188433d6423SLionel Sambuc 	/* Check if a non-superuser can change a files gid to gid2 *
189433d6423SLionel Sambuc 	 * if gid2 is the current process gid. */
190433d6423SLionel Sambuc 	for (cnt = 0; cnt < 5; cnt++) {
191433d6423SLionel Sambuc 		switch (fork()) {
192433d6423SLionel Sambuc 		    case -1:
193433d6423SLionel Sambuc 			printf("Can't fork\n");
194433d6423SLionel Sambuc 			break;
195433d6423SLionel Sambuc 		    case 0:
196433d6423SLionel Sambuc 			alarm(20);
197433d6423SLionel Sambuc 
198433d6423SLionel Sambuc 			getids(&uid, &gid);
199433d6423SLionel Sambuc 			if (uid == 0) {
200433d6423SLionel Sambuc 				getids(&uid, &gid);
201433d6423SLionel Sambuc 				if (uid == 0) e(42);
202433d6423SLionel Sambuc 			}
203433d6423SLionel Sambuc 			getids(&uid2, &gid2);
204433d6423SLionel Sambuc 			if (gid == gid2) e(43);
205433d6423SLionel Sambuc 
206433d6423SLionel Sambuc 			/* Creat boo and bar for user uid of group gid. */
207433d6423SLionel Sambuc 			Creat("boo");
208433d6423SLionel Sambuc 			if (chown("boo", uid, gid) != 0) e(44);
209433d6423SLionel Sambuc 			if (chmod("boo", ALL_BITS) != 0) e(45);
210433d6423SLionel Sambuc 			Creat("bar");
211433d6423SLionel Sambuc 			if (chown("bar", uid, gid) != 0) e(46);
212433d6423SLionel Sambuc 			if (chmod("bar", ALL_BITS) != 0) e(47);
213433d6423SLionel Sambuc 
214433d6423SLionel Sambuc 			/* We now become user uid of group gid2. */
215433d6423SLionel Sambuc 			setgid(gid2);
216433d6423SLionel Sambuc 			setuid(uid);
217433d6423SLionel Sambuc 
218433d6423SLionel Sambuc 			Stat("bar", &st1);
219433d6423SLionel Sambuc 			if (time(&time1) == (time_t) -1) e(48);
220433d6423SLionel Sambuc 			if (chown("bar", uid, gid2) != 0) e(49);
221433d6423SLionel Sambuc 			Stat("bar", &st2);
222433d6423SLionel Sambuc 			if (time(&time2) == (time_t) -1) e(50);
223433d6423SLionel Sambuc 
224433d6423SLionel Sambuc 			/* Check if the SET_BITS are cleared. */
225433d6423SLionel Sambuc 			if ((st1.st_mode & ALL_BITS) != ALL_BITS) e(51);
226433d6423SLionel Sambuc 			if ((st2.st_mode & ALL_BITS) != ALL_RWXB) e(52);
227433d6423SLionel Sambuc 
228433d6423SLionel Sambuc 			/* Check the st_times. */
229433d6423SLionel Sambuc 			if (st1.st_ctime > st2.st_ctime) e(53);
230433d6423SLionel Sambuc 			if (st1.st_ctime > time1) e(54);
231433d6423SLionel Sambuc 			if (st1.st_ctime > time2) e(55);
232433d6423SLionel Sambuc 			if (st2.st_ctime < time1) e(56);
233433d6423SLionel Sambuc 			if (st2.st_ctime > time2) e(57);
234433d6423SLionel Sambuc 			if (st1.st_atime != st2.st_atime) e(58);
235433d6423SLionel Sambuc 			if (st1.st_mtime != st2.st_mtime) e(59);
236433d6423SLionel Sambuc 
237433d6423SLionel Sambuc 			Stat("boo", &st1);
238433d6423SLionel Sambuc 			if (chmod("boo", ALL_BITS) != 0) e(60);
239433d6423SLionel Sambuc 			Stat("boo", &st2);
240433d6423SLionel Sambuc 
241433d6423SLionel Sambuc 			/* Check if the set gid bit is cleared. */
242433d6423SLionel Sambuc 			if ((st1.st_mode & ALL_RWXB) != ALL_RWXB) e(61);
243433d6423SLionel Sambuc 			if ((st2.st_mode & S_ISGID) != 0) e(62);
244433d6423SLionel Sambuc 
245433d6423SLionel Sambuc 			if (chown("boo", uid, gid2) != 0) e(63);
246433d6423SLionel Sambuc 			Stat("boo", &st1);
247433d6423SLionel Sambuc 
248433d6423SLionel Sambuc 			/* Check if the set uid bit is cleared. */
249433d6423SLionel Sambuc 			if ((st1.st_mode & S_ISUID) != 0) e(64);
250433d6423SLionel Sambuc 
251433d6423SLionel Sambuc 			exit(0);
252433d6423SLionel Sambuc 		    default:
253433d6423SLionel Sambuc 			wait(&stat_loc);
254433d6423SLionel Sambuc 			if (stat_loc != 0) e(65);	/* Alarm? */
255433d6423SLionel Sambuc 		}
256433d6423SLionel Sambuc 	}			/* end for loop. */
257433d6423SLionel Sambuc   }				/* end if (superuser). */
258433d6423SLionel Sambuc   if (chmod("foo", ALL_BITS) != 0) e(66);
259433d6423SLionel Sambuc   Stat("foo", &st1);
260433d6423SLionel Sambuc   if (chown("foo", geteuid(), getegid()) != 0) e(67);
261433d6423SLionel Sambuc   Stat("foo", &st2);
262433d6423SLionel Sambuc   if ((st1.st_mode & ALL_BITS) != ALL_BITS) e(68);	/* See intro! */
263433d6423SLionel Sambuc   if (superuser)
264433d6423SLionel Sambuc 	if ((st2.st_mode & ALL_RWXB) != ALL_RWXB) e(69);
265433d6423SLionel Sambuc   if (!superuser)
266433d6423SLionel Sambuc 	if ((st2.st_mode & ALL_BITS) != ALL_RWXB) e(70);
267433d6423SLionel Sambuc 
268433d6423SLionel Sambuc   (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
269433d6423SLionel Sambuc   System("rm -rf ../DIR_34/*");
270433d6423SLionel Sambuc }
271433d6423SLionel Sambuc 
test34b()272433d6423SLionel Sambuc void test34b()
273433d6423SLionel Sambuc {
274433d6423SLionel Sambuc   time_t time1, time2;
275433d6423SLionel Sambuc   mode_t mod;
276433d6423SLionel Sambuc   struct stat st1, st2;
277433d6423SLionel Sambuc 
278433d6423SLionel Sambuc   subtest = 2;
279433d6423SLionel Sambuc 
280433d6423SLionel Sambuc   /* Test chmod() and chown() on non regular files and on MaxName and
281433d6423SLionel Sambuc    * MaxPath. * Funny, but dirs should also have S_IS.ID bits.
282433d6423SLionel Sambuc    */
283433d6423SLionel Sambuc   Mkfifo("fifo");
284433d6423SLionel Sambuc   Mkdir("dir");
285433d6423SLionel Sambuc   Creat(MaxName);
286433d6423SLionel Sambuc   MaxPath[strlen(MaxPath) - 2] = '/';
287433d6423SLionel Sambuc   MaxPath[strlen(MaxPath) - 1] = 'a';	/* make ././.../a */
288433d6423SLionel Sambuc   Creat(MaxPath);
289433d6423SLionel Sambuc 
290433d6423SLionel Sambuc   for (mod = 1; mod <= ALL_BITS; mod <<= 1) {
291433d6423SLionel Sambuc 	if ((mod & ALL_BITS) != mod) continue;	/* bad mod */
292433d6423SLionel Sambuc 	Stat("dir", &st1);
293433d6423SLionel Sambuc 	if (time(&time1) == (time_t) -1) e(1);
294433d6423SLionel Sambuc 	if (chmod("dir", mod) != 0) e(2);
295433d6423SLionel Sambuc 	Stat("dir", &st2);
296433d6423SLionel Sambuc 	if (time(&time2) == (time_t) -1) e(3);
297433d6423SLionel Sambuc 	if (superuser)
298433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_BITS) != mod) e(4);
299433d6423SLionel Sambuc 	if (!superuser)
300433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(5);
301433d6423SLionel Sambuc 
302433d6423SLionel Sambuc 	/* Test the C time field. */
303433d6423SLionel Sambuc 	if (st1.st_ctime > st2.st_ctime) e(6);
304433d6423SLionel Sambuc 	if (st1.st_ctime > time1) e(7);
305433d6423SLionel Sambuc 	if (st1.st_ctime > time2) e(8);
306433d6423SLionel Sambuc #ifndef V1_FILESYSTEM
307433d6423SLionel Sambuc 	if (st2.st_ctime < time1) e(9);
308433d6423SLionel Sambuc #endif
309433d6423SLionel Sambuc 	if (st2.st_ctime > time2) e(10);
310433d6423SLionel Sambuc 	if (st1.st_atime != st2.st_atime) e(11);
311433d6423SLionel Sambuc 	if (st1.st_mtime != st2.st_mtime) e(12);
312433d6423SLionel Sambuc 
313433d6423SLionel Sambuc 	Stat("fifo", &st1);
314433d6423SLionel Sambuc 	if (time(&time1) == (time_t) -1) e(13);
315433d6423SLionel Sambuc 	if (chmod("fifo", mod) != 0) e(14);
316433d6423SLionel Sambuc 	Stat("fifo", &st2);
317433d6423SLionel Sambuc 	if (time(&time2) == (time_t) -1) e(15);
318433d6423SLionel Sambuc 	if (superuser)
319433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_BITS) != mod) e(16);
320433d6423SLionel Sambuc 	if (!superuser)
321433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(17);
322433d6423SLionel Sambuc 
323433d6423SLionel Sambuc 	/* Test the C time field. */
324433d6423SLionel Sambuc 	if (st1.st_ctime > st2.st_ctime) e(18);
325433d6423SLionel Sambuc 	if (st1.st_ctime > time1) e(19);
326433d6423SLionel Sambuc 	if (st1.st_ctime > time2) e(20);
327433d6423SLionel Sambuc #ifndef V1_FILESYSTEM
328433d6423SLionel Sambuc 	if (st2.st_ctime < time1) e(21);
329433d6423SLionel Sambuc #endif
330433d6423SLionel Sambuc 	if (st2.st_ctime > time2) e(22);
331433d6423SLionel Sambuc 	if (st1.st_atime != st2.st_atime) e(23);
332433d6423SLionel Sambuc 	if (st1.st_mtime != st2.st_mtime) e(24);
333433d6423SLionel Sambuc 
334433d6423SLionel Sambuc 	Stat(MaxName, &st1);
335433d6423SLionel Sambuc 	if (time(&time1) == (time_t) -1) e(25);
336433d6423SLionel Sambuc 	if (chmod(MaxName, mod) != 0) e(26);
337433d6423SLionel Sambuc 	Stat(MaxName, &st2);
338433d6423SLionel Sambuc 	if (time(&time2) == (time_t) -1) e(27);
339433d6423SLionel Sambuc 	if (superuser)
340433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_BITS) != mod) e(28);
341433d6423SLionel Sambuc 	if (!superuser)
342433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(29);
343433d6423SLionel Sambuc 
344433d6423SLionel Sambuc 	/* Test the C time field. */
345433d6423SLionel Sambuc 	if (st1.st_ctime > st2.st_ctime) e(30);
346433d6423SLionel Sambuc 	if (st1.st_ctime > time1) e(31);
347433d6423SLionel Sambuc 	if (st1.st_ctime > time2) e(32);
348433d6423SLionel Sambuc #ifndef V1_FILESYSTEM
349433d6423SLionel Sambuc 	if (st2.st_ctime < time1) e(33);
350433d6423SLionel Sambuc #endif
351433d6423SLionel Sambuc 	if (st2.st_ctime > time2) e(34);
352433d6423SLionel Sambuc 	if (st1.st_atime != st2.st_atime) e(35);
353433d6423SLionel Sambuc 	if (st1.st_mtime != st2.st_mtime) e(36);
354433d6423SLionel Sambuc 
355433d6423SLionel Sambuc 	Stat(MaxPath, &st1);
356433d6423SLionel Sambuc 	if (time(&time1) == (time_t) -1) e(37);
357433d6423SLionel Sambuc 	if (chmod(MaxPath, mod) != 0) e(38);
358433d6423SLionel Sambuc 	Stat(MaxPath, &st2);
359433d6423SLionel Sambuc 	if (time(&time2) == (time_t) -1) e(39);
360433d6423SLionel Sambuc 	if (superuser)
361433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_BITS) != mod) e(40);
362433d6423SLionel Sambuc 	if (!superuser)
363433d6423SLionel Sambuc 		if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(41);
364433d6423SLionel Sambuc 
365433d6423SLionel Sambuc 	/* Test the C time field. */
366433d6423SLionel Sambuc 	if (st1.st_ctime > st2.st_ctime) e(42);
367433d6423SLionel Sambuc 	if (st1.st_ctime > time1) e(43);
368433d6423SLionel Sambuc 	if (st1.st_ctime > time2) e(44);
369433d6423SLionel Sambuc #ifndef V1_FILESYSTEM
370433d6423SLionel Sambuc 	if (st2.st_ctime < time1) e(45);
371433d6423SLionel Sambuc #endif
372433d6423SLionel Sambuc 	if (st2.st_ctime > time2) e(46);
373433d6423SLionel Sambuc 	if (st1.st_atime != st2.st_atime) e(47);
374433d6423SLionel Sambuc 	if (st1.st_mtime != st2.st_mtime) e(48);
375433d6423SLionel Sambuc   }
376433d6423SLionel Sambuc 
377433d6423SLionel Sambuc   if (chmod("dir", 0777) != 0) e(49);
378433d6423SLionel Sambuc   if (chmod("fifo", 0777) != 0) e(50);
379433d6423SLionel Sambuc   if (chmod(MaxName, 0777) != 0) e(51);
380433d6423SLionel Sambuc   if (chmod(MaxPath, 0777) != 0) e(52);
381433d6423SLionel Sambuc 
382433d6423SLionel Sambuc   (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
383433d6423SLionel Sambuc   System("rm -rf ../DIR_34/*");
384433d6423SLionel Sambuc }
385433d6423SLionel Sambuc 
test34c()386433d6423SLionel Sambuc void test34c()
387433d6423SLionel Sambuc {
388433d6423SLionel Sambuc   struct stat st;
389433d6423SLionel Sambuc   uid_t uid, uid2;
390433d6423SLionel Sambuc   gid_t gid, gid2;
391433d6423SLionel Sambuc   int fd, does_truncate, stat_loc;
392433d6423SLionel Sambuc 
393433d6423SLionel Sambuc   subtest = 3;
394433d6423SLionel Sambuc 
395433d6423SLionel Sambuc   Mkdir("dir");
396433d6423SLionel Sambuc   Creat("dir/try_me");
397433d6423SLionel Sambuc 
398433d6423SLionel Sambuc   /* Disalow search permission and see if chmod() and chown() return
399433d6423SLionel Sambuc    * EACCES.
400433d6423SLionel Sambuc    */
401433d6423SLionel Sambuc   if (chmod("dir", ALL_BITS & ~S_IXUSR) != 0) e(1);
402433d6423SLionel Sambuc   if (!superuser) {
403433d6423SLionel Sambuc 	if (chmod("dir/try_me", 0) != -1) e(2);
404433d6423SLionel Sambuc 	if (errno != EACCES) e(3);
405433d6423SLionel Sambuc 	if (I_can_chown) {
406433d6423SLionel Sambuc 		if (chown("dir/try_me", geteuid(), getegid()) != -1) e(4);
407433d6423SLionel Sambuc 		if (errno != EACCES) e(5);
408433d6423SLionel Sambuc 	}
409433d6423SLionel Sambuc   }
410433d6423SLionel Sambuc 
411433d6423SLionel Sambuc   /* Check ENOTDIR. */
412433d6423SLionel Sambuc   Mkfifo("fifo");
413433d6423SLionel Sambuc   if (chmod("fifo/try_me", 0) != -1) e(6);
414433d6423SLionel Sambuc   if (errno != ENOTDIR) e(7);
415433d6423SLionel Sambuc   if (chown("fifo/try_me", geteuid(), getegid()) != -1) e(8);
416433d6423SLionel Sambuc   if (errno != ENOTDIR) e(9);
417433d6423SLionel Sambuc 
418433d6423SLionel Sambuc   Creat("file");
419433d6423SLionel Sambuc   if (chmod("file/try_me", 0) != -1) e(10);
420433d6423SLionel Sambuc   if (errno != ENOTDIR) e(11);
421433d6423SLionel Sambuc   if (chown("file/try_me", geteuid(), getegid()) != -1) e(12);
422433d6423SLionel Sambuc   if (errno != ENOTDIR) e(13);
423433d6423SLionel Sambuc 
424433d6423SLionel Sambuc   /* Check empty path. */
425433d6423SLionel Sambuc   if (chmod("", 0) != -1) e(14);
426433d6423SLionel Sambuc   if (errno != ENOENT) e(15);
427433d6423SLionel Sambuc   if (chown("", geteuid(), getegid()) != -1) e(16);
428433d6423SLionel Sambuc   if (errno != ENOENT) e(17);
429433d6423SLionel Sambuc 
430433d6423SLionel Sambuc   /* Check non existing file name. */
431433d6423SLionel Sambuc   if (chmod("non_exist", 0) != -1) e(18);
432433d6423SLionel Sambuc   if (errno != ENOENT) e(19);
433433d6423SLionel Sambuc   if (chown("non_exist", geteuid(), getegid()) != -1) e(20);
434433d6423SLionel Sambuc   if (errno != ENOENT) e(21);
435433d6423SLionel Sambuc 
436433d6423SLionel Sambuc   /* Check what we get if we do not have permisson. */
437433d6423SLionel Sambuc   if (!superuser) {
438433d6423SLionel Sambuc 	Stat("/", &st);
439433d6423SLionel Sambuc 	if (st.st_uid == geteuid()) e(22);
440433d6423SLionel Sambuc 
441433d6423SLionel Sambuc 	/* First I had 0, I changed it to st.st_mode 8-). */
442433d6423SLionel Sambuc 	if (chmod("/", st.st_mode) != -1) e(23);
443433d6423SLionel Sambuc 	if (errno != EPERM) e(24);
444433d6423SLionel Sambuc   }
445433d6423SLionel Sambuc   if (!I_can_chown) {
446433d6423SLionel Sambuc 	Stat("/", &st);
447433d6423SLionel Sambuc 	if (st.st_uid == geteuid()) e(25);
448433d6423SLionel Sambuc 	if (chown("/", geteuid(), getegid()) != -1) e(26);
449433d6423SLionel Sambuc 	if (errno != EPERM) e(27);
450433d6423SLionel Sambuc   }
451433d6423SLionel Sambuc 
452433d6423SLionel Sambuc   /* If we are superuser, we can test all id combinations. */
453433d6423SLionel Sambuc   if (superuser) {
454433d6423SLionel Sambuc 	switch (fork()) {
455433d6423SLionel Sambuc 	    case -1:	printf("Can't fork\n");	break;
456433d6423SLionel Sambuc 	    case 0:
457433d6423SLionel Sambuc 		alarm(20);
458433d6423SLionel Sambuc 
459433d6423SLionel Sambuc 		getids(&uid, &gid);
460433d6423SLionel Sambuc 		if (uid == 0) {
461433d6423SLionel Sambuc 			getids(&uid, &gid);
462433d6423SLionel Sambuc 			if (uid == 0) e(28);
463433d6423SLionel Sambuc 		}
464433d6423SLionel Sambuc 		getids(&uid2, &gid2);
465433d6423SLionel Sambuc 		if (gid == gid2) e(29);
466433d6423SLionel Sambuc 		if (uid == uid2) e(30);
467433d6423SLionel Sambuc 
468433d6423SLionel Sambuc 		/* Creat boo, owned by root. */
469433d6423SLionel Sambuc 		Creat("boo");
470433d6423SLionel Sambuc 		if (chmod("boo", ALL_BITS) != 0) e(31);
471433d6423SLionel Sambuc 
472433d6423SLionel Sambuc 		/* Creat boo for user uid2 of group gid2. */
473433d6423SLionel Sambuc 		Creat("bar");
474433d6423SLionel Sambuc 		if (chown("bar", uid2, gid2) != 0) e(32);
475433d6423SLionel Sambuc 		if (chmod("bar", ALL_BITS) != 0) e(33);
476433d6423SLionel Sambuc 
477433d6423SLionel Sambuc 		/* Creat my_gid for user uid2 of group gid. */
478433d6423SLionel Sambuc 		Creat("my_gid");
479433d6423SLionel Sambuc 		if (chown("my_gid", uid2, gid) != 0) e(34);
480433d6423SLionel Sambuc 		if (chmod("my_gid", ALL_BITS) != 0) e(35);
481433d6423SLionel Sambuc 
482433d6423SLionel Sambuc 		/* Creat my_uid for user uid of uid gid. */
483433d6423SLionel Sambuc 		Creat("my_uid");
484433d6423SLionel Sambuc 		if (chown("my_uid", uid, gid) != 0) e(36);
485433d6423SLionel Sambuc 		if (chmod("my_uid", ALL_BITS) != 0) e(37);
486433d6423SLionel Sambuc 
487433d6423SLionel Sambuc 		/* We now become user uid of uid gid. */
488433d6423SLionel Sambuc 		setgid(gid);
489433d6423SLionel Sambuc 		setuid(uid);
490433d6423SLionel Sambuc 
491433d6423SLionel Sambuc 		if (chown("boo", uid, gid) != -1) e(38);
492433d6423SLionel Sambuc 		if (errno != EPERM) e(39);
493433d6423SLionel Sambuc 		if (chown("bar", uid, gid) != -1) e(40);
494433d6423SLionel Sambuc 		if (errno != EPERM) e(41);
495433d6423SLionel Sambuc 		if (chown("my_gid", uid, gid) != -1) e(42);
496433d6423SLionel Sambuc 		if (errno != EPERM) e(43);
497433d6423SLionel Sambuc 		if (chown("my_uid", uid, gid2) != -1) e(44);
498433d6423SLionel Sambuc 
499433d6423SLionel Sambuc 		/* The EPERM is not strict POSIX. */
500433d6423SLionel Sambuc 		if (errno != EPERM) e(45);
501433d6423SLionel Sambuc 
502433d6423SLionel Sambuc 		if (chmod("boo", 0) != -1) e(46);
503433d6423SLionel Sambuc 		if (errno != EPERM) e(47);
504433d6423SLionel Sambuc 		if (chmod("bar", 0) != -1) e(48);
505433d6423SLionel Sambuc 		if (errno != EPERM) e(49);
506433d6423SLionel Sambuc 		if (chmod("my_gid", 0) != -1) e(50);
507433d6423SLionel Sambuc 		if (errno != EPERM) e(51);
508433d6423SLionel Sambuc 
509433d6423SLionel Sambuc 		exit(0);
510433d6423SLionel Sambuc 	    default:
511433d6423SLionel Sambuc 		wait(&stat_loc);
512433d6423SLionel Sambuc 		if (stat_loc != 0) e(52);	/* Alarm? */
513433d6423SLionel Sambuc 	}
514433d6423SLionel Sambuc   }
515433d6423SLionel Sambuc 
516433d6423SLionel Sambuc   /* Check too long path ed. */
517433d6423SLionel Sambuc   does_truncate = does_fs_truncate();
518433d6423SLionel Sambuc   fd = creat(NameTooLong, 0777);
519433d6423SLionel Sambuc   if (does_truncate) {
520433d6423SLionel Sambuc   	if (fd == -1) e(53);
521433d6423SLionel Sambuc 	if (close(fd) != 0) e(54);
522433d6423SLionel Sambuc 	if (chmod(NameTooLong, 0777) != 0) e(55);
523433d6423SLionel Sambuc 	if (chown(NameTooLong, geteuid(), getegid()) != 0) e(56);
524433d6423SLionel Sambuc   } else {
525433d6423SLionel Sambuc   	if (fd != -1) e(57);
526433d6423SLionel Sambuc   	if (errno != ENAMETOOLONG) e(58);
527433d6423SLionel Sambuc   	(void) close(fd);		/* Just in case */
528433d6423SLionel Sambuc   }
529433d6423SLionel Sambuc 
530433d6423SLionel Sambuc   /* Make PathTooLong contain ././.../a */
531433d6423SLionel Sambuc   PathTooLong[strlen(PathTooLong) - 2] = '/';
532433d6423SLionel Sambuc   PathTooLong[strlen(PathTooLong) - 1] = 'a';
533433d6423SLionel Sambuc   Creat("a");
534433d6423SLionel Sambuc   if (chmod(PathTooLong, 0777) != -1) e(59);
535433d6423SLionel Sambuc   if (errno != ENAMETOOLONG) e(60);
536433d6423SLionel Sambuc   if (chown(PathTooLong, geteuid(), getegid()) != -1) e(61);
537433d6423SLionel Sambuc   if (errno != ENAMETOOLONG) e(62);
538433d6423SLionel Sambuc 
539433d6423SLionel Sambuc   (void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
540433d6423SLionel Sambuc   System("rm -rf ../DIR_34/*");
541433d6423SLionel Sambuc }
542433d6423SLionel Sambuc 
makelongnames()543433d6423SLionel Sambuc void makelongnames()
544433d6423SLionel Sambuc {
545433d6423SLionel Sambuc   register int i;
546433d6423SLionel Sambuc   int max_name_length;
547433d6423SLionel Sambuc 
548433d6423SLionel Sambuc   max_name_length = name_max("."); /* Aka NAME_MAX, but not every FS supports
549433d6423SLionel Sambuc 				    * the same length, hence runtime check */
550433d6423SLionel Sambuc   MaxName = malloc(max_name_length + 1);
551433d6423SLionel Sambuc   NameTooLong = malloc(max_name_length + 1 + 1); /* Name of maximum +1 length */
552433d6423SLionel Sambuc   memset(MaxName, 'a', max_name_length);
553433d6423SLionel Sambuc   MaxName[max_name_length] = '\0';
554433d6423SLionel Sambuc 
555433d6423SLionel Sambuc   for (i = 0; i < PATH_MAX - 1; i++) {	/* idem path */
556433d6423SLionel Sambuc 	MaxPath[i++] = '.';
557433d6423SLionel Sambuc 	MaxPath[i] = '/';
558433d6423SLionel Sambuc   }
559433d6423SLionel Sambuc   MaxPath[PATH_MAX - 1] = '\0';
560433d6423SLionel Sambuc 
561433d6423SLionel Sambuc   strcpy(NameTooLong, MaxName);	/* copy them Max to ToLong */
562433d6423SLionel Sambuc   strcpy(PathTooLong, MaxPath);
563433d6423SLionel Sambuc 
564433d6423SLionel Sambuc   NameTooLong[max_name_length] = 'a';
565433d6423SLionel Sambuc   NameTooLong[max_name_length+1] = '\0';/* extend ToLongName by one too many */
566433d6423SLionel Sambuc   PathTooLong[PATH_MAX - 1] = '/';
567433d6423SLionel Sambuc   PathTooLong[PATH_MAX] = '\0';	/* inc ToLongPath by one */
568433d6423SLionel Sambuc }
569433d6423SLionel Sambuc 
570433d6423SLionel Sambuc /* Getids returns a valid uid and gid. Is used PASSWD FILE.
571433d6423SLionel Sambuc  * It assumes the following format for a passwd file line:
572433d6423SLionel Sambuc  * <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
573433d6423SLionel Sambuc  * If no uids and gids can be found, it will only return 0 ids.
574433d6423SLionel Sambuc  */
getids(r_uid,r_gid)575433d6423SLionel Sambuc void getids(r_uid, r_gid)
576433d6423SLionel Sambuc uid_t * r_uid;
577433d6423SLionel Sambuc gid_t * r_gid;
578433d6423SLionel Sambuc {
579433d6423SLionel Sambuc   char line[N];
580cbc8a0dfSDavid van Moolenbroek   unsigned char *p;
581433d6423SLionel Sambuc   uid_t uid;
582433d6423SLionel Sambuc   gid_t gid;
583433d6423SLionel Sambuc   FILE *fp;
584433d6423SLionel Sambuc   int i;
585433d6423SLionel Sambuc 
586433d6423SLionel Sambuc   static uid_t a_uid[N];	/* Array for uids. */
587433d6423SLionel Sambuc   static gid_t a_gid[N];	/* Array for gids. */
588433d6423SLionel Sambuc   static int nuid = 0, ngid = 0;/* The number of user & group ids. */
589433d6423SLionel Sambuc   static int cuid = 0, cgid = 0;/* The current id index. */
590433d6423SLionel Sambuc 
591433d6423SLionel Sambuc   /* If we don't have any uids go read some from the passwd file. */
592433d6423SLionel Sambuc   if (nuid == 0) {
593433d6423SLionel Sambuc 	a_uid[nuid++] = 0;	/* Root uid and gid. */
594433d6423SLionel Sambuc 	a_gid[ngid++] = 0;
595433d6423SLionel Sambuc 	if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
596433d6423SLionel Sambuc 		printf("Can't open ");
597433d6423SLionel Sambuc 		perror(PASSWD_FILE);
598433d6423SLionel Sambuc 	}
599433d6423SLionel Sambuc 	while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
600*7c48de6cSDavid van Moolenbroek 		p = (unsigned char *)strchr(line, ':');
601*7c48de6cSDavid van Moolenbroek 		if (p != NULL) p = (unsigned char *)strchr((char *)p + 1, ':');
602433d6423SLionel Sambuc 		if (p != NULL) {
603433d6423SLionel Sambuc 			p++;
604433d6423SLionel Sambuc 			uid = 0;
605433d6423SLionel Sambuc 			while (isdigit(*p)) {
606433d6423SLionel Sambuc 				uid *= 10;
607433d6423SLionel Sambuc 				uid += (uid_t) (*p - '0');
608433d6423SLionel Sambuc 				p++;
609433d6423SLionel Sambuc 			}
610433d6423SLionel Sambuc 			if (*p != ':') continue;
611433d6423SLionel Sambuc 			p++;
612433d6423SLionel Sambuc 			gid = 0;
613433d6423SLionel Sambuc 			while (isdigit(*p)) {
614433d6423SLionel Sambuc 				gid *= 10;
615433d6423SLionel Sambuc 				gid += (gid_t) (*p - '0');
616433d6423SLionel Sambuc 				p++;
617433d6423SLionel Sambuc 			}
618433d6423SLionel Sambuc 			if (*p != ':') continue;
619433d6423SLionel Sambuc 			if (nuid < N) {
620433d6423SLionel Sambuc 				for (i = 0; i < nuid; i++)
621433d6423SLionel Sambuc 					if (a_uid[i] == uid) break;
622433d6423SLionel Sambuc 				if (i == nuid) a_uid[nuid++] = uid;
623433d6423SLionel Sambuc 			}
624433d6423SLionel Sambuc 			if (ngid < N) {
625433d6423SLionel Sambuc 				for (i = 0; i < ngid; i++)
626433d6423SLionel Sambuc 					if (a_gid[i] == gid) break;
627433d6423SLionel Sambuc 				if (i == ngid) a_gid[ngid++] = gid;
628433d6423SLionel Sambuc 			}
629433d6423SLionel Sambuc 			if (nuid >= N && ngid >= N) break;
630433d6423SLionel Sambuc 		}
631433d6423SLionel Sambuc 	}
632433d6423SLionel Sambuc 	if (fp != NULL) fclose(fp);
633433d6423SLionel Sambuc   }
634433d6423SLionel Sambuc 
635433d6423SLionel Sambuc   /* We now have uids and gids in a_uid and a_gid. */
636433d6423SLionel Sambuc   if (cuid >= nuid) cuid = 0;
637433d6423SLionel Sambuc   if (cgid >= ngid) cgid = 0;
638433d6423SLionel Sambuc   *r_uid = a_uid[cuid++];
639433d6423SLionel Sambuc   *r_gid = a_gid[cgid++];
640433d6423SLionel Sambuc }
641