xref: /freebsd/contrib/pjdfstest/tests/rename/10.t (revision 5b9c547c)
1#!/bin/sh
2# $FreeBSD: head/tools/regression/pjdfstest/tests/rename/10.t 211352 2010-08-15 21:24:17Z pjd $
3
4desc="rename returns EACCES or EPERM if the file pointed at by the 'to' argument exists, the directory containing 'to' is marked sticky, and neither the containing directory nor 'to' are owned by the effective user ID"
5
6dir=`dirname $0`
7. ${dir}/../misc.sh
8
9echo "1..2099"
10
11n0=`namegen`
12n1=`namegen`
13n2=`namegen`
14n3=`namegen`
15n4=`namegen`
16
17expect 0 mkdir ${n4} 0755
18cdir=`pwd`
19cd ${n4}
20
21expect 0 mkdir ${n0} 0755
22expect 0 chown ${n0} 65534 65534
23
24expect 0 mkdir ${n1} 0755
25expect 0 chmod ${n1} 01777
26
27for type in regular fifo block char socket symlink; do
28	# User owns both: the sticky directory and the destination file.
29	expect 0 chown ${n1} 65534 65534
30	create_file ${type} ${n0}/${n2} 65534 65534
31	inode=`${fstest} lstat ${n0}/${n2} inode`
32
33	for type in regular fifo block char socket symlink; do
34		create_file ${type} ${n1}/${n3} 65534 65534
35		expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
36		expect ENOENT lstat ${n0}/${n2} inode
37		expect ${inode} lstat ${n1}/${n3} inode
38		expect 0 -u 65534 -g 65534 rename ${n1}/${n3} ${n0}/${n2}
39		expect ${inode} lstat ${n0}/${n2} inode
40		expect ENOENT lstat ${n1}/${n3} inode
41	done
42
43	expect 0 unlink ${n0}/${n2}
44
45	# User owns the sticky directory, but doesn't own the destination file.
46	for id in 0 65533; do
47		expect 0 chown ${n1} 65534 65534
48		create_file ${type} ${n0}/${n2} 65534 65534
49		inode=`${fstest} lstat ${n0}/${n2} inode`
50
51		for type in regular fifo block char socket symlink; do
52			create_file ${type} ${n1}/${n3} ${id} ${id}
53			expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
54			expect ENOENT lstat ${n0}/${n2} inode
55			expect ${inode} lstat ${n1}/${n3} inode
56			expect 0 -u 65534 -g 65534 rename ${n1}/${n3} ${n0}/${n2}
57			expect ${inode} lstat ${n0}/${n2} inode
58			expect ENOENT lstat ${n1}/${n3} inode
59		done
60
61		expect 0 unlink ${n0}/${n2}
62	done
63
64	# User owns the destination file, but doesn't own the sticky directory.
65	for id in 0 65533; do
66		expect 0 chown ${n1} ${id} ${id}
67		create_file ${type} ${n0}/${n2} 65534 65534
68		inode=`${fstest} lstat ${n0}/${n2} inode`
69
70		for type in regular fifo block char socket symlink; do
71			create_file ${type} ${n1}/${n3} 65534 65534
72			expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
73			expect ENOENT lstat ${n0}/${n2} inode
74			expect ${inode} lstat ${n1}/${n3} inode
75			expect 0 -u 65534 -g 65534 rename ${n1}/${n3} ${n0}/${n2}
76			expect ${inode} lstat ${n0}/${n2} inode
77			expect ENOENT lstat ${n1}/${n3} inode
78		done
79
80		expect 0 unlink ${n0}/${n2}
81	done
82
83	# User doesn't own the sticky directory nor the destination file.
84	for id in 0 65533; do
85		expect 0 chown ${n1} ${id} ${id}
86		create_file ${type} ${n0}/${n2} 65534 65534
87		inode=`${fstest} lstat ${n0}/${n2} inode`
88
89		for type in regular fifo block char socket symlink; do
90			create_file ${type} ${n1}/${n3} ${id} ${id}
91			expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
92			expect ${inode} lstat ${n0}/${n2} inode
93			expect ${id},${id} lstat ${n1}/${n3} uid,gid
94			expect 0 unlink ${n1}/${n3}
95		done
96
97		expect 0 unlink ${n0}/${n2}
98	done
99done
100
101# User owns both: the sticky directory and the destination directory.
102expect 0 chown ${n1} 65534 65534
103expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
104inode=`${fstest} lstat ${n0}/${n2} inode`
105
106expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
107expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
108expect ENOENT lstat ${n0}/${n2} type
109expect ${inode} lstat ${n1}/${n3} inode
110expect 0 rmdir ${n1}/${n3}
111
112# User owns the sticky directory, but doesn't own the destination directory.
113for id in 0 65533; do
114	expect 0 chown ${n1} 65534 65534
115	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
116	inode=`${fstest} lstat ${n0}/${n2} inode`
117
118	expect 0 -u ${id} -g ${id} mkdir ${n1}/${n3} 0755
119	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
120	expect ENOENT lstat ${n0}/${n2} type
121	expect ${inode} lstat ${n1}/${n3} inode
122	expect 0 rmdir ${n1}/${n3}
123done
124
125# User owns the destination directory, but doesn't own the sticky directory.
126for id in 0 65533; do
127	expect 0 chown ${n1} ${id} ${id}
128	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
129	inode=`${fstest} lstat ${n0}/${n2} inode`
130
131	expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
132	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
133	expect ENOENT lstat ${n0}/${n2} type
134	expect ${inode} lstat ${n1}/${n3} inode
135	expect 0 rmdir ${n1}/${n3}
136done
137
138# User doesn't own the sticky directory nor the destination directory.
139for id in 0 65533; do
140	expect 0 chown ${n1} ${id} ${id}
141	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
142	inode=`${fstest} lstat ${n0}/${n2} inode`
143
144	expect 0 -u ${id} -g ${id} mkdir ${n1}/${n3} 0755
145	expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
146	expect ${inode} lstat ${n0}/${n2} inode
147	expect ${id},${id} lstat ${n1}/${n3} uid,gid
148	expect 0 rmdir ${n0}/${n2}
149	expect 0 rmdir ${n1}/${n3}
150done
151
152expect 0 rmdir ${n1}
153expect 0 rmdir ${n0}
154
155cd ${cdir}
156expect 0 rmdir ${n4}
157