1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or https://opensource.org/licenses/CDDL-1.0.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2016, 2018 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/tests/functional/acl/acl.cfg
32. $STF_SUITE/include/libtest.shlib
33
34#
35# Get the given file/directory ACL
36#
37# $1 object -- file or directory
38#
39function get_acl #<obj>
40{
41        typeset obj=$1
42	if (( ${#obj} == 0 )); then
43		return 1
44	fi
45
46	ls -vd $obj | awk '(NR != 1) {print $0}'
47}
48
49#
50# Get the given file/directory ACL
51#
52# $1 object -- file or directory
53#
54function get_compact_acl #<obj>
55{
56        typeset obj=$1
57	if (( ${#obj} == 0 )); then
58		return 1
59	fi
60
61	ls -Vd $obj | awk '(NR != 1) {print $0}'
62}
63
64#
65# Check the given two files/directories have the same ACLs
66#
67# Return 0, if source object acl is equal to target object acl.
68#
69# $1 source object
70# $2 target object
71#
72function compare_acls #<src> <tgt>
73{
74        typeset src=$1
75        typeset tgt=$2
76
77	(( ${#src} == 0 || ${#tgt} == 0 )) && return 1
78	[[ $src == $tgt ]] && return 0
79
80	typeset tmpsrc=$TEST_BASE_DIR/compare_acls.src.$$
81	typeset tmptgt=$TEST_BASE_DIR/compare_acls.tgt.$$
82
83	get_acl $src > $tmpsrc
84	get_acl $tgt > $tmptgt
85	typeset -i ret=0
86	cmp $tmpsrc $tmptgt > /dev/null
87	ret=$?
88	rm -f $tmpsrc $tmptgt
89
90	if (( ret != 0 )); then
91		return $ret
92	fi
93
94	get_compact_acl $src > $tmpsrc
95	get_compact_acl $tgt > $tmptgt
96	cmp $tmpsrc $tmptgt > /dev/null
97	ret=$?
98	rm -f $tmpsrc $tmptgt
99
100	return $ret
101}
102
103#
104# Check that the given two objects have the same xattrs.
105# Return 0, if their xattrs are equal with each other. Otherwise, return 1.
106#
107# $1 source object
108# $2 target object
109#
110function compare_xattrs #<src> <tgt>
111{
112        typeset src=$1
113        typeset tgt=$2
114
115	(( ${#src} == 0 || ${#tgt} == 0 )) && return 1
116	[[ $src == $tgt ]] && return 0
117
118	typeset tmpsrc=$TEST_BASE_DIR/compare_xattrs.src.$$
119	typeset tmptgt=$TEST_BASE_DIR/compare_xattrs.tgt.$$
120
121	get_xattr $src > $tmpsrc
122	get_xattr $tgt > $tmptgt
123	typeset -i ret=0
124	cmp $tmpsrc $tmptgt > /dev/null
125	ret=$?
126	rm -f $tmpsrc $tmptgt
127
128        return $ret
129}
130
131#
132# Run commands by $ZFS_ACL_CUR_USER
133#
134# $1-n commands and options
135#
136function usr_exec #<commands> [...]
137{
138	chg_usr_exec "$ZFS_ACL_CUR_USER" $@
139}
140
141#
142# Cleanup exist user/group.
143#
144function cleanup_user_group
145{
146	del_user $ZFS_ACL_ADMIN
147
148	del_user $ZFS_ACL_STAFF1
149	del_user $ZFS_ACL_STAFF2
150	del_group $ZFS_ACL_STAFF_GROUP
151
152	del_user $ZFS_ACL_OTHER1
153	del_user $ZFS_ACL_OTHER2
154	del_group $ZFS_ACL_OTHER_GROUP
155
156	return 0
157}
158
159#
160# Clean up testfile and test directory
161#
162function cleanup
163{
164	if [[ -d $TESTDIR ]]; then
165		cd $TESTDIR
166		rm -rf $TESTDIR/*
167	fi
168}
169
170#
171# Get the given file/directory xattr
172#
173# $1 object -- file or directory
174#
175function get_xattr #<obj>
176{
177        typeset obj=$1
178	typeset xattr
179	if (( ${#obj} == 0 )); then
180		return 1
181	fi
182
183	for xattr in $(runat $obj ls | grep -v 'SUNWattr_r[ow]'); do
184		runat $obj cksum $xattr
185	done
186}
187
188#
189# This function compare two cksum results array.
190#
191# $1 The array name which stored the cksum before operation.
192# $2 The array name which stored the cksum after operation.
193#
194function compare_cksum #<array1> <array2>
195{
196	typeset before=$1
197	typeset after=$2
198	eval typeset -i count=\${#$before[@]}
199
200	typeset -i i=0
201	while (( i < count )); do
202		eval typeset var1=\${$before[$i]}
203		eval typeset var2=\${$after[$i]}
204
205		if [[ $var1 != $var2 ]]; then
206			return 1
207		fi
208
209		(( i += 1 ))
210	done
211
212	return 0
213}
214
215#
216# The function create_files creates the directories and files that the script
217# will operate on to test extended attribute functionality.
218#
219# $1 The base directory in which to create directories and files.
220#
221function create_files #<directory>
222{
223	typeset basedir=$1
224
225	[[ ! -d $basedir ]] && usr_exec mkdir -m 777 $basedir
226	[[ ! -d $RES_DIR  ]] && usr_exec mkdir -m 777 $RES_DIR
227	[[ ! -d $INI_DIR ]] && usr_exec mkdir -m 777 $INI_DIR
228	[[ ! -d $TST_DIR ]] && usr_exec mkdir -m 777 $TST_DIR
229	[[ ! -d $TMP_DIR  ]] && usr_exec mkdir -m 777 $TMP_DIR
230
231	#
232	# Create the original file and its attribute files.
233	#
234	[[ ! -a $RES_DIR/file ]] && \
235		usr_exec file_write -o create -f $RES_DIR/file \
236			-b 1024 -d 0 -c 1
237	[[ ! -a $RES_DIR/attribute ]] && \
238		usr_exec cp $RES_DIR/file $RES_DIR/attribute
239
240	typeset oldpwd=$PWD
241	cd $INI_DIR
242
243	typeset -i i=0
244	while (( i < NUM_FILE )); do
245		typeset dstfile=$INI_DIR/file.$$.$i
246		usr_exec cp $RES_DIR/file $dstfile
247
248		typeset -i j=0
249		while (( j < NUM_ATTR )); do
250			usr_exec runat $dstfile \
251				cp $RES_DIR/attribute ./attribute.$j
252			(( j += 1 ))
253		done
254
255		(( i += 1 ))
256	done
257
258	cd $oldpwd
259}
260