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