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#
24# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"@(#)zfs_acl_chmod_xattr_001_pos.ksh	1.4	09/01/13 SMI"
28#
29
30. $STF_SUITE/tests/acl/acl_common.kshlib
31
32#################################################################################
33#
34# __stc_assertion_start
35#
36# ID: zfs_acl_chmod_xattr_001_pos
37#
38# DESCRIPTION:
39#	Verify that the read_xattr/write_xattr for
40#	owner/group/everyone are correct.
41#
42# STRATEGY:
43# 1. Create file and  directory in zfs filesystem
44# 2. Set special read_xattr ACE to the file and directory
45# 3. Try to list the extended attributes of the file and directory
46# 4. Set special write_xattr ACE to the file and directory
47# 5. Try to add new extended attributes to the file and directory
48# 6. 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	"read_xattr:allow" \
82	"read_xattr:deny" \
83	"write_xattr:allow" \
84	"write_xattr:deny"
85
86set -A a_flag "owner@" "group@" "everyone@"
87
88MYTESTFILE=$STF_SUITE/include/default.cfg
89
90log_assert "Verify that the permission of read_xattr/write_xattr for " \
91	"owner/group/everyone are correct."
92log_onexit cleanup
93
94function operate_node #user node acl
95{
96	typeset user=$1
97	typeset node=$2
98	typeset acl_t=$3
99	typeset ret
100
101	if [[ $user == "" || $node == "" ]]; then
102		log_fail "user, node are not defined."
103	fi
104
105	if [[ $acl_t == *read_xattr* ]]; then
106		chgusr_exec $user $RUNAT $node $LS > /dev/null 2>&1; ret=$?
107	elif [[ $acl_t == *write_xattr* ]]; then
108		chgusr_exec $user $RUNAT $node $CP $MYTESTFILE attr.1 ; ret=$?
109
110		if [[ $ret -eq 0 ]]; then
111			log_must cleanup_test_files $TESTDIR/basedir
112			log_must $TAR xpf@ $TESTDIR/$ARCHIVEFILE
113		fi
114	fi
115
116	return $ret
117}
118
119function logname #acl_target user
120{
121	typeset acl_target=$1
122	typeset user=$2
123	typeset ret="log_mustnot"
124
125	# To super user, read and write deny permission was override.
126	if [[ $user == root || $acl_target == *:allow ]] then
127		ret="log_must"
128	fi
129
130	print $ret
131}
132
133function check_chmod_results #node flag acl_target g_usr o_usr
134{
135	typeset node=$1
136	typeset flag=$2
137	typeset acl_target=$2:$3
138	typeset g_usr=$4
139	typeset o_usr=$5
140	typeset log
141
142	if [[ $flag == "owner@" || $flag == "everyone@" ]]; then
143		log=$(logname $acl_target $ZFS_ACL_CUR_USER)
144		$log operate_node $ZFS_ACL_CUR_USER $node $acl_target
145	fi
146	if [[ $flag == "group@" || $flag == "everyone@" ]]; then
147		log=$(logname $acl_target $g_usr)
148		$log operate_node $g_usr $node $acl_target
149	fi
150	if [[ $flag == "everyone@" ]]; then
151		log=$(logname $acl_target $o_usr)
152		$log operate_node $o_usr $node $acl_target
153	fi
154}
155
156function test_chmod_basic_access #node g_usr o_usr
157{
158	typeset node=${1%/}
159	typeset g_usr=$2
160	typeset o_usr=$3
161	typeset flag acl_p acl_t parent
162
163	parent=${node%/*}
164
165	for flag in ${a_flag[@]}; do
166		for acl_t in "${a_access[@]}"; do
167			log_must usr_exec $CHMOD A+$flag:$acl_t $node
168
169			log_must $TAR cpf@ $TESTDIR/$ARCHIVEFILE basedir
170
171			check_chmod_results "$node" "$flag" \
172				"$acl_t" "$g_usr" "$o_usr"
173
174			log_must usr_exec $CHMOD A0- $node
175		done
176	done
177}
178
179function setup_test_files #base_node user group
180{
181	typeset base_node=$1
182	typeset user=$2
183	typeset group=$3
184
185	cleanup_test_files $base_node
186
187	log_must $MKDIR -p $base_node
188	log_must $CHOWN $user:$group $base_node
189
190	log_must set_cur_usr $user
191
192	# Prepare all files/sub-dirs for testing.
193
194	file0=$base_node/testfile_rm
195
196	dir0=$base_node/testdir_rm
197
198	log_must usr_exec $TOUCH $file0
199	log_must usr_exec $CHMOD 444 $file0
200
201	log_must usr_exec $RUNAT $file0 $CP $MYTESTFILE attr.0
202
203	log_must usr_exec $MKDIR -p $dir0
204	log_must usr_exec $CHMOD 555 $dir0
205
206	log_must usr_exec $RUNAT $dir0 $CP $MYTESTFILE attr.0
207
208	log_must usr_exec $CHMOD 777 $base_node
209	return 0
210}
211
212function cleanup_test_files #base_node
213{
214	typeset base_node=$1
215
216	if [[ -d $base_node ]]; then
217		log_must $RM -rf $base_node
218	elif [[ -e $base_node ]]; then
219		log_must $RM -f $base_node
220	fi
221
222	return 0
223}
224
225typeset cwd=$PWD
226typeset ARCHIVEFILE=archive.tar
227
228test_requires RUNAT ZFS_ACL ZFS_XATTR
229
230typeset -i i=0
231typeset -i j=0
232typeset target
233
234while (( i < ${#users[@]} )); do
235	setup_test_files $TESTDIR/basedir ${users[i]} ${users[((i+1))]}
236	cd $TESTDIR
237
238	j=0
239	while (( j < 1 )); do
240		eval target=\$file$j
241		test_chmod_basic_access $target \
242			"${users[((i+2))]}" "${users[((i+3))]}"
243
244		eval target=\$dir$j
245		test_chmod_basic_access $target \
246			"${users[((i+2))]}" "${users[((i+3))]}"
247
248		(( j = j + 1 ))
249	done
250
251	(( i += 4 ))
252done
253
254log_pass "Verify that the permission of read_xattr/write_xattr for " \
255	"owner/group/everyone are correct."
256