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_001_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_001_pos
39#
40# DESCRIPTION:
41#	Verify that the read_xattr/write_xattr for
42#	owner/group/everyone are correct.
43#
44# STRATEGY:
45# 1. Create file and  directory in zfs filesystem
46# 2. Set special read_xattr ACE to the file and directory
47# 3. Try to list the extended attributes of the file and directory
48# 4. Set special write_xattr ACE to the file and directory
49# 5. Try to add new extended attributes to the file and directory
50# 6. Verify above operation is successful.
51#
52# TESTABILITY: explicit
53#
54# TEST_AUTOMATION_LEVEL: automated
55#
56# CODING_STATUS: COMPLETED (2005-11-29)
57#
58# __stc_assertion_end
59#
60################################################################################
61
62verify_runnable "both"
63
64function cleanup
65{
66	cd $cwd
67
68	cleanup_test_files $TESTDIR/basedir
69
70	if [[ -e $TESTDIR/$ARCHIVEFILE ]]; then
71		log_must $RM -f $TESTDIR/$ARCHIVEFILE
72	fi
73
74	return 0
75}
76
77#	owner@	group	group_users		other_users
78set -A users \
79	"root"	"root"	"$ZFS_ACL_ADMIN" 	"$ZFS_ACL_OTHER1" \
80	"$ZFS_ACL_STAFF1"	"$ZFS_ACL_STAFF_GROUP"	"$ZFS_ACL_STAFF2" 	"$ZFS_ACL_OTHER1"
81
82set -A a_access \
83	"read_xattr:allow" \
84	"read_xattr:deny" \
85	"write_xattr:allow" \
86	"write_xattr:deny"
87
88set -A a_flag "owner@" "group@" "everyone@"
89
90MYTESTFILE=$STF_SUITE/include/default.cfg
91
92log_assert "Verify that the permission of read_xattr/write_xattr for " \
93	"owner/group/everyone are correct."
94log_onexit cleanup
95
96function operate_node #user node acl
97{
98	typeset user=$1
99	typeset node=$2
100	typeset acl_t=$3
101	typeset ret
102
103	if [[ $user == "" || $node == "" ]]; then
104		log_fail "user, node are not defined."
105	fi
106
107	if [[ $acl_t == *read_xattr* ]]; then
108		chgusr_exec $user $RUNAT $node $LS > /dev/null 2>&1; ret=$?
109	elif [[ $acl_t == *write_xattr* ]]; then
110		chgusr_exec $user $RUNAT $node $CP $MYTESTFILE attr.1 ; ret=$?
111
112		if [[ $ret -eq 0 ]]; then
113			log_must cleanup_test_files $TESTDIR/basedir
114			log_must $TAR xpf@ $TESTDIR/$ARCHIVEFILE
115		fi
116	fi
117
118	return $ret
119}
120
121function logname #acl_target user
122{
123	typeset acl_target=$1
124	typeset user=$2
125	typeset ret="log_mustnot"
126
127	# To super user, read and write deny permission was override.
128	if [[ $user == root || $acl_target == *:allow ]] then
129		ret="log_must"
130	fi
131
132	print $ret
133}
134
135function check_chmod_results #node flag acl_target g_usr o_usr
136{
137	typeset node=$1
138	typeset flag=$2
139	typeset acl_target=$2:$3
140	typeset g_usr=$4
141	typeset o_usr=$5
142	typeset log
143
144	if [[ $flag == "owner@" || $flag == "everyone@" ]]; then
145		log=$(logname $acl_target $ZFS_ACL_CUR_USER)
146		$log operate_node $ZFS_ACL_CUR_USER $node $acl_target
147	fi
148	if [[ $flag == "group@" || $flag == "everyone@" ]]; then
149		log=$(logname $acl_target $g_usr)
150		$log operate_node $g_usr $node $acl_target
151	fi
152	if [[ $flag == "everyone@" ]]; then
153		log=$(logname $acl_target $o_usr)
154		$log operate_node $o_usr $node $acl_target
155	fi
156}
157
158function test_chmod_basic_access #node g_usr o_usr
159{
160	typeset node=${1%/}
161	typeset g_usr=$2
162	typeset o_usr=$3
163	typeset flag acl_p acl_t parent
164
165	parent=${node%/*}
166
167	for flag in ${a_flag[@]}; do
168		for acl_t in "${a_access[@]}"; do
169			log_must usr_exec $CHMOD A+$flag:$acl_t $node
170
171			log_must $TAR cpf@ $TESTDIR/$ARCHIVEFILE basedir
172
173			check_chmod_results "$node" "$flag" \
174				"$acl_t" "$g_usr" "$o_usr"
175
176			log_must usr_exec $CHMOD A0- $node
177		done
178	done
179}
180
181function setup_test_files #base_node user group
182{
183	typeset base_node=$1
184	typeset user=$2
185	typeset group=$3
186
187	cleanup_test_files $base_node
188
189	log_must $MKDIR -p $base_node
190	log_must $CHOWN $user:$group $base_node
191
192	log_must set_cur_usr $user
193
194	# Prepare all files/sub-dirs for testing.
195
196	file0=$base_node/testfile_rm
197
198	dir0=$base_node/testdir_rm
199
200	log_must usr_exec $TOUCH $file0
201	log_must usr_exec $CHMOD 444 $file0
202
203	log_must usr_exec $RUNAT $file0 $CP $MYTESTFILE attr.0
204
205	log_must usr_exec $MKDIR -p $dir0
206	log_must usr_exec $CHMOD 555 $dir0
207
208	log_must usr_exec $RUNAT $dir0 $CP $MYTESTFILE attr.0
209
210	log_must usr_exec $CHMOD 777 $base_node
211	return 0
212}
213
214function cleanup_test_files #base_node
215{
216	typeset base_node=$1
217
218	if [[ -d $base_node ]]; then
219		log_must $RM -rf $base_node
220	elif [[ -e $base_node ]]; then
221		log_must $RM -f $base_node
222	fi
223
224	return 0
225}
226
227typeset cwd=$PWD
228typeset ARCHIVEFILE=archive.tar
229
230test_requires RUNAT ZFS_ACL ZFS_XATTR
231
232typeset -i i=0
233typeset -i j=0
234typeset target
235
236while (( i < ${#users[@]} )); do
237	setup_test_files $TESTDIR/basedir ${users[i]} ${users[((i+1))]}
238	cd $TESTDIR
239
240	j=0
241	while (( j < 1 )); do
242		eval target=\$file$j
243		test_chmod_basic_access $target \
244			"${users[((i+2))]}" "${users[((i+3))]}"
245
246		eval target=\$dir$j
247		test_chmod_basic_access $target \
248			"${users[((i+2))]}" "${users[((i+3))]}"
249
250		(( j = j + 1 ))
251	done
252
253	(( i += 4 ))
254done
255
256log_pass "Verify that the permission of read_xattr/write_xattr for " \
257	"owner/group/everyone are correct."
258