xref: /freebsd/contrib/pjdfstest/tests/rename/10.t (revision a90b9d0159070121c221b966469c3e36d912bf82)
1#!/bin/sh
2# vim: filetype=sh noexpandtab ts=8 sw=8
3# $FreeBSD: head/tools/regression/pjdfstest/tests/rename/10.t 211352 2010-08-15 21:24:17Z pjd $
4
5desc="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"
6
7dir=`dirname $0`
8. ${dir}/../misc.sh
9
10echo "1..2099"
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 chown ${n0} 65534 65534
24
25expect 0 mkdir ${n1} 0755
26expect 0 chmod ${n1} 01777
27
28for type in regular fifo block char socket symlink; do
29	# User owns both: the sticky directory and the destination file.
30	expect 0 chown ${n1} 65534 65534
31	create_file ${type} ${n0}/${n2} 65534 65534
32	inode=`${fstest} lstat ${n0}/${n2} inode`
33
34	for type in 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} lstat ${n1}/${n3} inode
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 sticky directory, but doesn't own the destination file.
47	for id in 0 65533; do
48		expect 0 chown ${n1} 65534 65534
49		create_file ${type} ${n0}/${n2} 65534 65534
50		inode=`${fstest} lstat ${n0}/${n2} inode`
51
52		for type in regular fifo block char socket symlink; do
53			create_file ${type} ${n1}/${n3} ${id} ${id}
54			expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
55			expect ENOENT lstat ${n0}/${n2} inode
56			expect ${inode} lstat ${n1}/${n3} inode
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 destination file, but doesn't own the sticky directory.
66	for id in 0 65533; do
67		expect 0 chown ${n1} ${id} ${id}
68		create_file ${type} ${n0}/${n2} 65534 65534
69		inode=`${fstest} lstat ${n0}/${n2} inode`
70
71		for type in 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} lstat ${n1}/${n3} inode
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 sticky directory nor the destination file.
85	for id in 0 65533; do
86		expect 0 chown ${n1} ${id} ${id}
87		create_file ${type} ${n0}/${n2} 65534 65534
88		inode=`${fstest} lstat ${n0}/${n2} inode`
89
90		for type in regular fifo block char socket symlink; do
91			create_file ${type} ${n1}/${n3} ${id} ${id}
92			expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
93			expect ${inode} lstat ${n0}/${n2} inode
94			expect ${id},${id} lstat ${n1}/${n3} uid,gid
95			expect 0 unlink ${n1}/${n3}
96		done
97
98		expect 0 unlink ${n0}/${n2}
99	done
100done
101
102# User owns both: the sticky directory and the destination directory.
103expect 0 chown ${n1} 65534 65534
104expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
105inode=`${fstest} lstat ${n0}/${n2} inode`
106
107expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
108expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
109expect ENOENT lstat ${n0}/${n2} type
110expect ${inode} lstat ${n1}/${n3} inode
111expect 0 rmdir ${n1}/${n3}
112
113# User owns the sticky directory, but doesn't own the destination directory.
114for id in 0 65533; do
115	expect 0 chown ${n1} 65534 65534
116	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
117	inode=`${fstest} lstat ${n0}/${n2} inode`
118
119	expect 0 -u ${id} -g ${id} mkdir ${n1}/${n3} 0755
120	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
121	expect ENOENT lstat ${n0}/${n2} type
122	expect ${inode} lstat ${n1}/${n3} inode
123	expect 0 rmdir ${n1}/${n3}
124done
125
126# User owns the destination directory, but doesn't own the sticky directory.
127for id in 0 65533; do
128	expect 0 chown ${n1} ${id} ${id}
129	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
130	inode=`${fstest} lstat ${n0}/${n2} inode`
131
132	expect 0 -u 65534 -g 65534 mkdir ${n1}/${n3} 0755
133	expect 0 -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
134	expect ENOENT lstat ${n0}/${n2} type
135	expect ${inode} lstat ${n1}/${n3} inode
136	expect 0 rmdir ${n1}/${n3}
137done
138
139# User doesn't own the sticky directory nor the destination directory.
140for id in 0 65533; do
141	expect 0 chown ${n1} ${id} ${id}
142	expect 0 -u 65534 -g 65534 mkdir ${n0}/${n2} 0755
143	inode=`${fstest} lstat ${n0}/${n2} inode`
144
145	expect 0 -u ${id} -g ${id} mkdir ${n1}/${n3} 0755
146	expect "EACCES|EPERM" -u 65534 -g 65534 rename ${n0}/${n2} ${n1}/${n3}
147	expect ${inode} lstat ${n0}/${n2} inode
148	expect ${id},${id} lstat ${n1}/${n3} uid,gid
149	expect 0 rmdir ${n0}/${n2}
150	expect 0 rmdir ${n1}/${n3}
151done
152
153expect 0 rmdir ${n1}
154expect 0 rmdir ${n0}
155
156cd ${cdir}
157expect 0 rmdir ${n4}
158