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.
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.
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]
26 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
27 # Use is subject to license terms.
29 # ident "@(#)acl_common.kshlib 1.4 09/05/19 SMI"
32 . $STF_SUITE/include/libtest.kshlib
34 # FreeBSD doesn't support ZFS extended attributes. It also doesn't support the
35 # same ACL mechanisms Solaris does for testing.
36 if [[ $os_name != "FreeBSD" ]]; then
37 export ZFS_XATTR="true"
40 log_note "On FreeBSD most xattr and ACL tests are disabled"
44 # Get the given file/directory access mode
46 # $1 object -- file or directroy
48 function get_mode #<obj>
51 if (( ${#obj} == 0 )); then
55 $LS -ld $obj | $AWK '{print $1}'
59 # Get the given file/directory ACL
61 # $1 object -- file or directroy
63 function get_acl #<obj>
66 if (( ${#obj} == 0 )); then
70 $LS -vd $obj | $NAWK '(NR != 1) {print $0}'
74 # Get the given file/directory ACL
76 # $1 object -- file or directroy
78 function get_compact_acl #<obj>
81 if (( ${#obj} == 0 )); then
85 $LS -Vd $obj | $NAWK '(NR != 1) {print $0}'
89 # Check the given two files/directories have the same ACLs
91 # Return 0, if source object acl is equal to target object acl.
96 function compare_acls #<src> <tgt>
101 (( ${#src} == 0 || ${#tgt} == 0 )) && return 1
102 [[ $src == $tgt ]] && return 0
104 typeset tmpsrc=$TMPDIR/compare_acls.src.${TESTCASE_ID}
105 typeset tmptgt=$TMPDIR/compare_acls.tgt.${TESTCASE_ID}
107 get_acl $src > $tmpsrc
108 get_acl $tgt > $tmptgt
110 $DIFF $tmpsrc $tmptgt > /dev/null 2>&1
112 $RM -f $tmpsrc $tmptgt
114 if (( ret != 0 )); then
118 get_compact_acl $src > $tmpsrc
119 get_compact_acl $tgt > $tmptgt
120 $DIFF $tmpsrc $tmptgt > /dev/null 2>&1
122 $RM -f $tmpsrc $tmptgt
128 # Check that the given two objects have the same modes.
129 # Return 0, if their modes are equal with each other. Otherwise, return 1.
134 function compare_modes #<src> <tgt>
141 (( ${#src} == 0 || ${#tgt} == 0 )) && return 1
142 [[ $src == $tgt ]] && return 0
147 mode[i]=$(get_mode $obj)
152 [[ ${mode[0]} != ${mode[1]} ]] && return 1
158 # Check that the given two objects have the same xattrs.
159 # Return 0, if their xattrs are equal with each other. Otherwise, return 1.
164 function compare_xattrs #<src> <tgt>
169 (( ${#src} == 0 || ${#tgt} == 0 )) && return 1
170 [[ $src == $tgt ]] && return 0
172 typeset tmpsrc=$TMPDIR/compare_xattrs.src.${TESTCASE_ID}
173 typeset tmptgt=$TMPDIR/compare_xattrs.tgt.${TESTCASE_ID}
175 get_xattr $src > $tmpsrc
176 get_xattr $tgt > $tmptgt
178 $DIFF $tmpsrc $tmptgt > /dev/null 2>&1
180 $RM -f $tmpsrc $tmptgt
186 # Check '+' is set for a given file/directory with 'ls [-l]' command
188 # $1 object -- file or directory.
190 function plus_sign_check_l #<obj>
193 if (( ${#obj} == 0 )); then
197 $LS -ld $obj | $AWK '{print $1}' | $GREP "+\>" > /dev/null
203 # Check '+' is set for a given file/directory with 'ls [-v]' command
205 # $1 object -- file or directory.
207 function plus_sign_check_v #<obj>
210 if (( ${#obj} == 0 )); then
214 $LS -vd $obj | $NAWK '(NR == 1) {print $1}' | $GREP "+\>" > /dev/null
220 # A wrapper function of c program
222 # $1 legal login name
223 # $2-n commands and options
225 function chgusr_exec #<login_name> <commands> [...]
232 # Export the current user for the following usr_exec operating.
234 # $1 legal login name
236 function set_cur_usr #<login_name>
238 export ZFS_ACL_CUR_USER=$1
242 # Run commands by $ZFS_ACL_CUR_USER
244 # $1-n commands and options
246 function usr_exec #<commands> [...]
248 $CHG_USR_EXEC "$ZFS_ACL_CUR_USER" $@
253 # Count how many ACEs for the specified file or directory.
255 # $1 file or directroy name
257 function count_ACE #<file or dir name>
259 if [[ ! -e $1 ]]; then
260 log_note "Need input file or directroy name."
264 $LS -vd $1 | $NAWK 'BEGIN {count=0}
265 (NR != 1)&&(/[0-9]:/) {count++}
272 # Get specified number ACE content of specified file or directory.
274 # $1 file or directory name
275 # $2 specified number
277 function get_ACE #<file or dir name> <specified number> <verbose|compact>
279 if [[ ! -e $1 || $2 -ge $(count_ACE $1) ]]; then
285 typeset format=${3:-verbose}
286 typeset -i next_num=-1
288 typeset tmpfile=$TMPDIR/tmp_get_ACE.${TESTCASE_ID}
297 *) log_fail "Invalid parameter as ($format), " \
298 "only verbose|compact is supported."
302 $LS $args $file > $tmpfile
303 (( $? != 0 )) && log_fail "FAIL: $LS $args $file > $tmpfile"
305 [[ -z $line ]] && continue
306 if [[ $args == -vd ]]; then
307 if [[ $line == "$num":* ]]; then
308 (( next_num = num + 1 ))
310 if [[ $line == "$next_num":* ]]; then
313 if (( next_num != -1 )); then
317 if (( next_num == num )); then
325 (( $? != 0 )) && log_fail "FAIL: $RM -f $tmpfile"
329 # Cleanup exist user/group.
331 function cleanup_user_group
333 del_user $ZFS_ACL_ADMIN
335 del_user $ZFS_ACL_STAFF1
336 del_user $ZFS_ACL_STAFF2
337 del_group $ZFS_ACL_STAFF_GROUP
339 del_user $ZFS_ACL_OTHER1
340 del_user $ZFS_ACL_OTHER2
341 del_group $ZFS_ACL_OTHER_GROUP
347 # Clean up testfile and test directory
351 if [[ -d $TESTDIR ]]; then
358 # According to specified access or acl_spec, do relevant operating by using the
363 # $3 acl_spec or access
365 function rwx_node #user node acl_spec|access
371 if [[ $user == "" || $node == "" || $acl_spec == "" ]]; then
372 log_note "node or acl_spec are not defined."
376 if [[ -d $node ]]; then
378 *:read_data:*|read_data)
379 chgusr_exec $user $LS -l $node > /dev/null 2>&1
381 *:write_data:*|write_data)
382 if [[ -f ${node}/tmpfile ]]; then
383 log_must $RM -f ${node}/tmpfile
385 chgusr_exec $user $TOUCH ${node}/tmpfile > \
388 *"execute:"*|execute)
389 chgusr_exec $user $FIND $node > /dev/null 2>&1
394 *:read_data:*|read_data)
395 chgusr_exec $user $CAT $node > /dev/null 2>&1
397 *:write_data:*|write_data)
398 chgusr_exec $user $DD if=/bin/ls of=$node > \
401 *"execute:"*|execute)
402 ZFS_ACL_ERR_STR=$(chgusr_exec $user $node 2>&1)
409 # Get the given file/directory xattr
411 # $1 object -- file or directroy
413 function get_xattr #<obj>
417 if (( ${#obj} == 0 )); then
421 for xattr in `$RUNAT $obj $LS | \
422 /usr/bin/egrep -v -e SUNWattr_ro -e SUNWattr_rw` ; do
423 $RUNAT $obj $SUM $xattr
428 # Get the owner of a file/directory
430 function get_owner #node
435 if [[ -z $node ]]; then
436 log_fail "node are not defined."
439 if [[ -d $node ]]; then
440 value=$($LS -dl $node | $AWK '{print $3}')
441 elif [[ -e $node ]]; then
442 value=$($LS -l $node | $AWK '{print $3}')
449 # Get the group of a file/directory
451 function get_group #node
456 if [[ -z $node ]]; then
457 log_fail "node are not defined."
460 if [[ -d $node ]]; then
461 value=$($LS -dl $node | $AWK '{print $4}')
462 elif [[ -e $node ]]; then
463 value=$($LS -l $node | $AWK '{print $4}')
471 # Get the group name that a UID belongs to
473 function get_user_group #uid
478 if [[ -z $uid ]]; then
479 log_fail "UID not defined."
484 if [[ $? -eq 0 ]]; then
489 log_fail "Invalid UID (uid)."
494 # Get the specified item of the specified string
496 # $1: Item number, count from 0.
509 # This function calculate the specified directory files checksum and write
510 # to the specified array.
512 # $1 directory in which the files will be cksum.
513 # $2 file array name which was used to store file cksum information.
514 # $3 attribute array name which was used to store attribute information.
516 function cksum_files #<dir> <file_array_name> <attribute_array_name>
522 [[ ! -d $dir ]] && return
525 typeset files=$($LS file*)
529 while (( i < NUM_FILE )); do
530 typeset f=$(getitem $i $files)
531 eval $farr_name[$i]=\$\(\$CKSUM $f\)
534 while (( j < NUM_ATTR )); do
535 eval $aarr_name[$n]=\$\(\$RUNAT \$f \$CKSUM \
549 # This function compare two cksum results array.
551 # $1 The array name which stored the cksum before operation.
552 # $2 The array name which stored the cksum after operation.
554 function compare_cksum #<array1> <array2>
558 eval typeset -i count=\${#$before[@]}
561 while (( i < count )); do
562 eval typeset var1=\${$before[$i]}
563 eval typeset var2=\${$after[$i]}
565 if [[ $var1 != $var2 ]]; then
576 # This function calculate all the files cksum information in current directory
577 # and output them to the specified file.
579 # $1 directory from which the files will be cksum.
580 # $2 cksum output file
582 function record_cksum #<outfile>
587 [[ ! -d ${outfile%/*} ]] && usr_exec $MKDIR -p ${outfile%/*}
589 usr_exec cd $dir ; $FIND . -depth -type f -exec cksum {} \\\; | $SORT > $outfile
590 usr_exec cd $dir ; $FIND . -depth -type f -xattr -exec runat {} \
591 cksum attribute* \\\; | $SORT >> $outfile
595 # The function create_files creates the directories and files that the script
596 # will operate on to test extended attribute functionality.
598 # $1 The base directory in which to create directories and files.
600 function create_files #<directory>
604 [[ ! -d $basedir ]] && usr_exec $MKDIR -m 777 $basedir
605 [[ ! -d $RES_DIR ]] && usr_exec $MKDIR -m 777 $RES_DIR
606 [[ ! -d $INI_DIR ]] && usr_exec $MKDIR -m 777 $INI_DIR
607 [[ ! -d $TST_DIR ]] && usr_exec $MKDIR -m 777 $TST_DIR
608 [[ ! -d $TMP_DIR ]] && usr_exec $MKDIR -m 777 $TMP_DIR
611 # Create the original file and its attribute files.
613 [[ ! -a $RES_DIR/file ]] && \
614 usr_exec $FILE_WRITE -o create -f $RES_DIR/file \
616 [[ ! -a $RES_DIR/attribute ]] && \
617 usr_exec $CP $RES_DIR/file $RES_DIR/attribute
623 while (( i < NUM_FILE )); do
624 typeset dstfile=$INI_DIR/file.${TESTCASE_ID}.$i
625 usr_exec $CP $RES_DIR/file $dstfile
628 while (( j < NUM_ATTR )); do
629 usr_exec $RUNAT $dstfile \
630 $CP $RES_DIR/attribute ./attribute.$j