xfstests: 219: fix awk filter for duplicate users
[xfstests-dev.git] / 193
1 #! /bin/bash
2 # FS QA Test No. 193
3 #
4 # Test permission checks in ->setattr
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2008 Christoph Hellwig.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #-----------------------------------------------------------------------
22 #
23 # creator
24 owner=hch@lst.de
25
26 seq=`basename $0`
27 echo "QA output created by $seq"
28
29 here=`pwd`
30 tmp=/tmp/$$
31 status=1        # failure is the default!
32 trap "_cleanup_files; exit \$status" 0 1 2 3 15
33 tag="added by qa $seq"
34
35 #
36 # For some tests we need a secondary group for the qa_user.  Currently
37 # that's not available in the framework, so the tests using it are
38 # commented out.
39 #
40 #group2=foo
41
42 #
43 # Create two files, one owned by root, one by the qa_user
44 #
45 _create_files()
46 {
47         touch test.root
48         touch test.${qa_user}
49         chown ${qa_user}:${qa_user} test.${qa_user}
50 }
51
52 #
53 # Remove our files again
54 #
55 _cleanup_files()
56 {
57         rm -f test.${qa_user}
58         rm -f test.root
59 }
60
61 # get standard environment, filters and checks
62 . ./common.rc
63 . ./common.filter
64
65 # real QA test starts here
66 _supported_fs generic
67 _supported_os Linux
68
69 _require_user
70 _need_to_be_root
71
72
73 #
74 # make sure we have a normal umask set
75 #
76 umask 022
77
78
79 #
80 # Test the ATTR_UID case
81 #
82 echo
83 echo "testing ATTR_UID"
84 echo
85
86 _create_files
87
88 echo "user: chown root owned file to qa_user (should fail)"
89 su ${qa_user} -c "chown ${qa_user} test.root"
90
91 echo "user: chown root owned file to root (should fail)"
92 su ${qa_user} -c "chown root test.root"
93
94 echo "user: chown qa_user owned file to qa_user (should succeed)"
95 su ${qa_user} -c "chown ${qa_user} test.${qa_user}"
96
97 # this would work without _POSIX_CHOWN_RESTRICTED
98 echo "user: chown qa_user owned file to root (should fail)"
99 su ${qa_user} -c "chown root test.${qa_user}"
100
101 _cleanup_files
102
103 #
104 # Test the ATTR_GID case
105 #
106 echo
107 echo "testing ATTR_GID"
108 echo
109
110 _create_files
111
112 echo "user: chgrp root owned file to root (should fail)"
113 su ${qa_user} -c "chgrp root test.root"
114
115 echo "user: chgrp qa_user owned file to root (should fail)"
116 su ${qa_user} -c "chgrp root test.${qa_user}"
117
118 echo "user: chgrp root owned file to qa_user (should fail)"
119 su ${qa_user} -c "chgrp ${qa_user} test.root"
120
121 echo "user: chgrp qa_user owned file to qa_user (should succeed)"
122 su ${qa_user} -c "chgrp ${qa_user} test.${qa_user}"
123
124 #echo "user: chgrp qa_user owned file to secondary group (should succeed)"
125 #su ${qa_user} -c "chgrp ${group2} test.${qa_user}"
126
127 _cleanup_files
128
129
130 #
131 # Test the ATTR_MODE case
132 #
133 echo
134 echo "testing ATTR_MODE"
135 echo
136
137 _create_files
138
139 echo "user: chmod a+r on qa_user owned file (should succeed)"
140 su ${qa_user} -c "chmod a+r test.${qa_user}"
141
142 echo "user: chmod a+r on root owned file (should fail)"
143 su ${qa_user} -c "chmod a+r test.root"
144
145 #
146 # Setup a file owned by the qa_user, but with a group ID that
147 # is not present in the qa_users group list (use root to make it easier for it)
148 # and mark it with set sgid bit
149 #
150 # From Posix (www.opengroup.org) for chmod:
151 #       "If the calling process does not have appropriate privileges, and
152 #       if the group ID of the file does not match the effective group ID
153 #       or one of the supplementary group IDs and if the file is a regular
154 #       file, bit S_ISGID (set-group-ID on execution) in the file's mode
155 #       shall be cleared upon successful return from chmod()."
156 # i.e.
157 # reg file + file's gid not in process' group set + no approp. privileges -> clear sgid
158 #
159 echo "check that the sgid bit is cleared"
160 chown ${qa_user}:root test.${qa_user}
161 chmod g+s test.${qa_user}
162
163 # and let the qa_user change permission bits
164 su ${qa_user} -c "chmod a+w test.${qa_user}"
165 stat -c '%A' test.${qa_user}
166
167 #
168 # Setup a file owned by the qa_user and with the suid bit set.
169 # A chmod by root should not clear the suid bit.
170 # There is nothing in Posix that says it should but just checking.
171 #
172 echo "check that suid bit is not cleared"
173 chmod u+s test.${qa_user}
174 chmod a+w test.${qa_user}
175 stat -c '%A' test.${qa_user}
176
177 _cleanup_files
178
179 _create_files
180 # Now test out the clear of suid/sgid for chown
181 #
182 # From Posix (www.opengroup.org) for chown:
183 #       "If the specified file is a regular file, one or more of the S_IXUSR,
184 #       S_IXGRP, or S_IXOTH bits of the file mode are set, and the process
185 #       does not have appropriate privileges, the set-user-ID (S_ISUID) and
186 #       set-group-ID (S_ISGID) bits of the file mode shall be cleared upon
187 #       successful return from chown(). If the specified file is a regular
188 #       file, one or more of the S_IXUSR, S_IXGRP, or S_IXOTH bits of the
189 #       file mode are set, and the process has appropriate privileges, it
190 #       is implementation-defined whether the set-user-ID and set-group-ID
191 #       bits are altered. If the chown() function is successfully invoked
192 #       on a file that is not a regular file and one or more of the S_IXUSR,
193 #       S_IXGRP, or S_IXOTH bits of the file mode are set, the set-user-ID
194 #       and set-group-ID bits may be cleared."
195 # i.e.
196 # reg file + exec-mode-bits set + no appropriate privileges -> clear suid,sgid
197 # reg file + exec-mode-bits set + appropriate privileges -> maybe clear suid,sgid
198 # non reg file + exec-mode-bits set + chown success on file (??) -> maybe clear suid/sgid
199 #
200 echo "check that suid/sgid bits are cleared after successful chown..."
201
202 echo "with no exec perm"
203 chmod ug+s test.${qa_user}
204 echo -n "before: "; stat -c '%A' test.${qa_user}
205 chown root test.${qa_user}
206 echo -n "after:  "; stat -c '%A' test.${qa_user}
207
208 echo "with user exec perm"
209 chmod ug+s test.${qa_user}
210 chmod u+x test.${qa_user}
211 echo -n "before: "; stat -c '%A' test.${qa_user}
212 chown root test.${qa_user}
213 echo -n "after:  "; stat -c '%A' test.${qa_user}
214
215 echo "with group exec perm"
216 chmod ug+s test.${qa_user}
217 chmod g+x test.${qa_user}
218 chmod u-x test.${qa_user}
219 echo -n "before: "; stat -c '%A' test.${qa_user}
220 chown root test.${qa_user}
221 echo -n "after:  "; stat -c '%A' test.${qa_user}
222
223 echo "with user+group exec perm"
224 chmod ug+s test.${qa_user}
225 chmod ug+x test.${qa_user}
226 echo -n "before: "; stat -c '%A' test.${qa_user}
227 chown root test.${qa_user}
228 echo -n "after:  "; stat -c '%A' test.${qa_user}
229
230 _cleanup_files
231
232
233 #
234 # Test ATTR_*TIMES_SET
235 #
236 echo
237 echo "testing ATTR_*TIMES_SET"
238 echo
239
240 _create_files
241
242 echo "user: touch qa_user file (should succeed)"
243 su ${qa_user} -c "touch test.${qa_user}"
244
245 echo "user: touch root file (should fail)"
246 su ${qa_user} -c "touch test.root"
247
248 _cleanup_files
249
250 # success, all done
251 echo "*** done"
252 rm -f $seq.full
253 status=0