1#!/usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zfs_acl_chmod_xattr_002_pos.ksh	1.4	09/01/13 SMI"
30#
31
32. $STF_SUITE/tests/acl/acl_common.kshlib
33
34#################################################################################
35#
36# __stc_assertion_start
37#
38# ID: zfs_acl_chmod_xattr_002_pos
39#
40# DESCRIPTION:
41#	Verify that the write_xattr for remove the extended attributes of
42#	owner/group/everyone are correct.
43#
44# STRATEGY:
45# 1. Create file and  directory in zfs filesystem
46# 2. Set special write_xattr ACE to the file and directory
47# 3. Try to remove the extended attributes of the file and directory
48# 4. Verify above operation is successful.
49#
50# TESTABILITY: explicit
51#
52# TEST_AUTOMATION_LEVEL: automated
53#
54# CODING_STATUS: COMPLETED (2005-11-29)
55#
56# __stc_assertion_end
57#
58################################################################################
59
60verify_runnable "both"
61
62function cleanup
63{
64	cd $cwd
65
66	cleanup_test_files $TESTDIR/basedir
67
68	if [[ -e $TESTDIR/$ARCHIVEFILE ]]; then
69		log_must $RM -f $TESTDIR/$ARCHIVEFILE
70	fi
71
72	return 0
73}
74
75#	owner@	group	group_users		other_users
76set -A users \
77	"root"	"root"	"$ZFS_ACL_ADMIN" 	"$ZFS_ACL_OTHER1" \
78	"$ZFS_ACL_STAFF1"	"$ZFS_ACL_STAFF_GROUP"	"$ZFS_ACL_STAFF2" 	"$ZFS_ACL_OTHER1"
79
80set -A a_access \
81	"write_xattr:allow" \
82	"write_xattr:deny"
83
84set -A a_flag "owner@" "group@" "everyone@"
85
86MYTESTFILE=$STF_SUITE/include/default.cfg
87
88log_assert "Verify that the permission of write_xattr for " \
89	"owner/group/everyone while remove extended attributes are correct."
90log_onexit cleanup
91
92function operate_node #user node acl
93{
94	typeset user=$1
95	typeset node=$2
96	typeset acl_t=$3
97	typeset ret
98
99	if [[ $user == "" || $node == "" ]]; then
100		log_fail "user, node are not defined."
101	fi
102
103	chgusr_exec $user $RUNAT $node $RM -f attr.0 ; ret=$?
104
105	if [[ $ret -eq 0 ]]; then
106		log_must cleanup_test_files $TESTDIR/basedir
107		log_must $TAR xpf@ $TESTDIR/$ARCHIVEFILE
108	fi
109
110	return $ret
111}
112
113function logname #acl_target owner user
114{
115	typeset acl_target=$1
116	typeset owner=$2
117	typeset user=$3
118	typeset ret="log_mustnot"
119
120	# To super user, read and write deny permission was override.
121	if [[ $user == root || $owner == $user ]] then
122		ret="log_must"
123	fi
124
125	print $ret
126}
127
128function check_chmod_results #node flag acl_target owner g_usr o_usr
129{
130	typeset node=$1
131	typeset flag=$2
132	typeset acl_target=$2:$3
133	typeset owner=$4
134	typeset g_usr=$5
135	typeset o_usr=$6
136	typeset log
137
138	if [[ $flag == "owner@" || $flag == "everyone@" ]]; then
139		log=$(logname $acl_target $owner $ZFS_ACL_CUR_USER)
140		$log operate_node $ZFS_ACL_CUR_USER $node $acl_target
141	fi
142	if [[ $flag == "group@" || $flag == "everyone@" ]]; then
143		log=$(logname $acl_target $owner $g_usr)
144		$log operate_node $g_usr $node $acl_target
145	fi
146	if [[ $flag == "everyone@" ]]; then
147		log=$(logname $acl_target $owner $o_usr)
148		$log operate_node $o_usr $node $acl_target
149	fi
150}
151
152function test_chmod_basic_access #node owner g_usr o_usr
153{
154	typeset node=${1%/}
155	typeset owner=$2
156	typeset g_usr=$3
157	typeset o_usr=$4
158	typeset flag acl_p acl_t parent
159
160	parent=${node%/*}
161
162	for flag in ${a_flag[@]}; do
163		for acl_t in "${a_access[@]}"; do
164			log_must usr_exec $CHMOD A+$flag:$acl_t $node
165
166			log_must $TAR cpf@ $TESTDIR/$ARCHIVEFILE basedir
167
168			check_chmod_results "$node" "$flag" \
169				"$acl_t" "$owner" "$g_usr" "$o_usr"
170
171			log_must usr_exec $CHMOD A0- $node
172		done
173	done
174}
175
176function setup_test_files #base_node user group
177{
178	typeset base_node=$1
179	typeset user=$2
180	typeset group=$3
181
182	cleanup_test_files $base_node
183
184	log_must $MKDIR -p $base_node
185	log_must $CHOWN $user:$group $base_node
186
187	log_must set_cur_usr $user
188
189	# Prepare all files/sub-dirs for testing.
190
191	file0=$base_node/testfile_rm
192
193	dir0=$base_node/testdir_rm
194
195	log_must usr_exec $TOUCH $file0
196	log_must usr_exec $CHMOD 444 $file0
197
198	log_must usr_exec $RUNAT $file0 $CP $MYTESTFILE attr.0
199
200	log_must usr_exec $MKDIR -p $dir0
201	log_must usr_exec $CHMOD 555 $dir0
202
203	log_must usr_exec $RUNAT $dir0 $CP $MYTESTFILE attr.0
204
205	log_must usr_exec $CHMOD 555 $base_node
206	return 0
207}
208
209function cleanup_test_files #base_node
210{
211	typeset base_node=$1
212
213	if [[ -d $base_node ]]; then
214		log_must $RM -rf $base_node
215	elif [[ -e $base_node ]]; then
216		log_must $RM -f $base_node
217	fi
218
219	return 0
220}
221
222typeset cwd=$PWD
223typeset ARCHIVEFILE=archive.tar
224
225test_requires RUNAT ZFS_XATTR
226
227typeset -i i=0
228typeset -i j=0
229typeset target
230
231while (( i < ${#users[@]} )); do
232	setup_test_files $TESTDIR/basedir ${users[i]} ${users[((i+1))]}
233	cd $TESTDIR
234
235	j=0
236	while (( j < 1 )); do
237		eval target=\$file$j
238		test_chmod_basic_access $target ${users[i]} \
239			"${users[((i+2))]}" "${users[((i+3))]}"
240
241		eval target=\$dir$j
242		test_chmod_basic_access $target ${users[i]} \
243			"${users[((i+2))]}" "${users[((i+3))]}"
244
245		(( j = j + 1 ))
246	done
247
248	(( i += 4 ))
249done
250
251log_pass "Verify that the permission of write_xattr for " \
252	"owner/group/everyone while remove extended attributes are correct."
253