xref: /freebsd/contrib/pjdfstest/tests/rename/09.t (revision 42249ef2)
1#!/bin/sh
2# vim: filetype=sh noexpandtab ts=8 sw=8
3# $FreeBSD: head/tools/regression/pjdfstest/tests/rename/09.t 211352 2010-08-15 21:24:17Z pjd $
4
5desc="rename returns EACCES or EPERM if the directory containing 'from' is marked sticky, and neither the containing directory nor 'from' are owned by the effective user ID"
6
7dir=`dirname $0`
8. ${dir}/../misc.sh
9
10echo "1..2353"
11
12n0=`namegen`
13n1=`namegen`
14n2=`namegen`
15n3=`namegen`
16n4=`namegen`
17
18expect 0 mkdir ${n4} 0755
19cdir=`pwd`
20cd ${n4}
21
22expect 0 mkdir ${n0} 0755
23expect 0 chmod ${n0} 01777
24expect 0 chown ${n0} 65534 65534
25
26expect 0 mkdir ${n1} 0755
27expect 0 chown ${n1} 65534 65534
28
29for type in regular fifo block char socket symlink; do
30	# User owns both: the source sticky directory and the source file.
31	expect 0 chown ${n0} 65534 65534
32	create_file ${type} ${n0}/${n2} 65534 65534
33	inode=`${fstest} lstat ${n0}/${n2} inode`
34
35	for type in none regular fifo block char socket symlink; do
36		create_file ${type} ${n1}/${n3} 65534 65534
37		expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
38		expect ENOENT lstat ${n0}/${n2} inode
39		expect ${inode},65534,65534 lstat ${n1}/${n3} inode,uid,gid
40		expect 0 -u 65534 -g 65534 rename ${n1}/${n3} ${n0}/${n2}
41		expect ${inode} lstat ${n0}/${n2} inode
42		expect ENOENT lstat ${n1}/${n3} inode
43	done
44
45	expect 0 unlink ${n0}/${n2}
46
47	# User owns the source sticky directory, but doesn't own the source file.
48	for id in 0 65533; do
49		expect 0 chown ${n0} 65534 65534
50		create_file ${type} ${n0}/${n2} ${id} ${id}
51		inode=`${fstest} lstat ${n0}/${n2} inode`
52
53		for type in none regular fifo block char socket symlink; do
54			create_file ${type} ${n1}/${n3} 65534 65534
55			expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
56			expect ENOENT lstat ${n0}/${n2} inode
57			expect ${inode},${id},${id} lstat ${n1}/${n3} inode,uid,gid
58			expect 0 -u 65534 -g 65534 rename ${n1}/${n3} ${n0}/${n2}
59			expect ${inode} lstat ${n0}/${n2} inode
60			expect ENOENT lstat ${n1}/${n3} inode
61		done
62
63		expect 0 unlink ${n0}/${n2}
64	done
65
66	# User owns the source file, but doesn't own the source sticky directory.
67	for id in 0 65533; do
68		expect 0 chown ${n0} ${id} ${id}
69		create_file ${type} ${n0}/${n2} 65534 65534
70		inode=`${fstest} lstat ${n0}/${n2} inode`
71
72		for type in none regular fifo block char socket symlink; do
73			create_file ${type} ${n1}/${n3} 65534 65534
74			expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
75			expect ENOENT lstat ${n0}/${n2} inode
76			expect ${inode},65534,65534 lstat ${n1}/${n3} inode,uid,gid
77			expect 0 -u 65534 -g 65534 rename ${n1}/${n3} ${n0}/${n2}
78			expect ${inode} lstat ${n0}/${n2} inode
79			expect ENOENT lstat ${n1}/${n3} inode
80		done
81
82		expect 0 unlink ${n0}/${n2}
83	done
84
85	# User doesn't own the source sticky directory nor the source file.
86	for id in 0 65533; do
87		expect 0 chown ${n0} ${id} ${id}
88		create_file ${type} ${n0}/${n2} ${id} ${id}
89		inode=`${fstest} lstat ${n0}/${n2} inode`
90
91		for type in none regular fifo block char socket symlink; do
92			create_file ${type} ${n1}/${n3} 65534 65534
93			expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
94			expect ${inode},${id},${id} lstat ${n0}/${n2} inode,uid,gid
95			if [ "${type}" != "none" ]; then
96				expect 65534,65534 lstat ${n1}/${n3} uid,gid
97				expect 0 unlink ${n1}/${n3}
98			fi
99		done
100
101		expect 0 unlink ${n0}/${n2}
102	done
103done
104
105# User owns both: the source sticky directory and the source directory.
106expect 0 chown ${n0} 65534 65534
107create_file dir ${n0}/${n2} 65534 65534
108inode=`${fstest} lstat ${n0}/${n2} inode`
109
110expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
111expect ENOENT lstat ${n0}/${n2} type
112expect ${inode},65534,65534 lstat ${n1}/${n3} inode,uid,gid
113expect 0 rename ${n1}/${n3} ${n0}/${n2}
114
115expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
116expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
117expect ENOENT lstat ${n0}/${n2} type
118expect ${inode},65534,65534 lstat ${n1}/${n3} inode,uid,gid
119expect 0 rmdir ${n1}/${n3}
120
121# User owns the source sticky directory, but doesn't own the source directory.
122# This fails when changing parent directory, because this will modify
123# source directory inode (the .. link in it), but we can still rename it
124# without changing its parent directory.
125for id in 0 65533; do
126	expect 0 chown ${n0} 65534 65534
127	create_file dir ${n0}/${n2} ${id} ${id}
128	inode=`${fstest} lstat ${n0}/${n2} inode`
129
130	expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
131	expect ${inode},${id},${id} lstat ${n0}/${n2} inode,uid,gid
132	expect ENOENT lstat ${n1}/${n3} type
133
134	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n0}/${n3}
135	expect ENOENT lstat ${n0}/${n2} type
136	expect ${inode},${id},${id} lstat ${n0}/${n3} inode,uid,gid
137	expect 0 rename ${n0}/${n3} ${n0}/${n2}
138
139	expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
140	expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
141	expect ${inode},${id},${id} lstat ${n0}/${n2} inode,uid,gid
142	expect dir,${id},${id} lstat ${n0}/${n2} type,uid,gid
143	expect 0 rmdir ${n1}/${n3}
144
145	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n3} 0755
146	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n0}/${n3}
147	expect ENOENT lstat ${n0}/${n2} type
148	expect ${inode},${id},${id} lstat ${n0}/${n3} inode,uid,gid
149	expect 0 rmdir ${n0}/${n3}
150done
151
152# User owns the source directory, but doesn't own the source sticky directory.
153for id in 0 65533; do
154	expect 0 chown ${n0} ${id} ${id}
155	create_file dir ${n0}/${n2} 65534 65534
156	inode=`${fstest} lstat ${n0}/${n2} inode`
157
158	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
159	expect ENOENT lstat ${n0}/${n2} type
160	expect ${inode},65534,65534 lstat ${n1}/${n3} inode,uid,gid
161	expect 0 rename ${n1}/${n3} ${n0}/${n2}
162
163	expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
164	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
165	expect ENOENT lstat ${n0}/${n2} type
166	expect ${inode},65534,65534 lstat ${n1}/${n3} inode,uid,gid
167	expect 0 rmdir ${n1}/${n3}
168done
169
170# User doesn't own the source sticky directory nor the source directory.
171for id in 0 65533; do
172	expect 0 chown ${n0} ${id} ${id}
173	create_file dir ${n0}/${n2} ${id} ${id}
174	inode=`${fstest} lstat ${n0}/${n2} inode`
175
176	expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
177	expect ${inode},${id},${id} lstat ${n0}/${n2} inode,uid,gid
178	expect ENOENT lstat ${n1}/${n3} type
179
180	expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
181	expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
182	expect ${inode},${id},${id} lstat ${n0}/${n2} inode,uid,gid
183	expect dir,65534,65534 lstat ${n1}/${n3} type,uid,gid
184	expect 0 rmdir ${n0}/${n2}
185	expect 0 rmdir ${n1}/${n3}
186done
187
188expect 0 rmdir ${n1}
189expect 0 rmdir ${n0}
190
191cd ${cdir}
192expect 0 rmdir ${n4}
193