1 #! /usr/bin/ksh -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 
  28 . $STF_SUITE/tests/acl/acl_common.kshlib
  29 
  30 #################################################################################
  31 #
  32 # __stc_assertion_start
  33 #
  34 # ID: acl_chmod_compact_pos001
  35 #
  36 # DESCRIPTION:
  37 #       chmod A{+|-|=} could set compact ACL correctly.
  38 #
  39 # STRATEGY:
  40 #       1. Loop root and non-root user.
  41 #       2. Get the random compact ACL string.
  42 #       4. Separately chmod +|-|=
  43 #       5. Check compact ACL display as expected 
  44 #
  45 # TESTABILITY: explicit
  46 #
  47 # TEST_AUTOMATION_LEVEL: automated
  48 #
  49 # CODING_STATUS: COMPLETED (2006-08-11)
  50 #
  51 # __stc_assertion_end
  52 #
  53 ################################################################################
  54 
  55 [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
  56         && set -x
  57 
  58 echo "ASSERTION: chmod A{+|=} should set compact ACL correctly."
  59 
  60 set -A a_flag owner group everyone
  61 set -A a_access r w x p d D a A R W c C o s 
  62 set -A a_inherit_object f d
  63 set -A a_inherit_strategy i n
  64 set -A a_type allow deny
  65 
  66 #
  67 # Get a random item from an array.
  68 #
  69 # $1 the base set
  70 #
  71 function random_select #array_name
  72 {
  73         [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
  74                 && set -x
  75 
  76         typeset arr_name=$1
  77         typeset -i ind
  78 
  79         eval typeset -i cnt=\${#${arr_name}[@]}
  80         (( ind = $RANDOM % cnt ))
  81 
  82         eval print \${${arr_name}[$ind]}
  83 }
  84 
  85 #
  86 # Create a random string according to array name, the item number and 
  87 # separated tag.
  88 #
  89 # $1 array name where the function get the elements
  90 # $2 the items number which you want to form the random string
  91 # $3 the separated tag
  92 #
  93 function form_random_str #<array_name> <count> <sep>
  94 {
  95         [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
  96                 && set -x
  97 
  98         typeset arr_name=$1
  99         typeset -i count=${2:-1}
 100         typeset sep=${3:-""}
 101 
 102         typeset str=""
 103         while (( count > 0 )); do
 104                 str="${str}$(random_select $arr_name)${sep}"
 105 
 106                 (( count -= 1 ))
 107         done
 108 
 109         print $str
 110 }
 111 
 112 #
 113 # Get the sub string from specified source string
 114 #
 115 # $1 source string
 116 # $2 start position. Count from 1
 117 # $3 offset
 118 #
 119 function get_substr #src_str pos offset
 120 {
 121         [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
 122                 && set -x
 123 
 124         typeset pos offset
 125 
 126         $ECHO $1 | \
 127                 $NAWK -v pos=$2 -v offset=$3 '{print substr($0, pos, offset)}'
 128 }
 129 
 130 #
 131 # According to the input ACE access,ACE type, and inherit flags, return the
 132 # expect compact ACE that could be used by chmod A0{+|=}'.
 133 #
 134 # $1 ACE flag which is owner, group, or everyone
 135 # $2 ACE access generated by the element of a_access
 136 # $3 ACE inherit_object generated by the element of a_inherit_object
 137 # $4 ACE inherit_strategy generated by the element of a_inherit_strategy
 138 # $5 ACE type which is allow or deny
 139 #
 140 function cal_ace # acl_flag acl_access \
 141                  # acl_inherit_object acl_inherit_strategy acl_type
 142 {
 143         [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
 144                 && set -x
 145 
 146         typeset acl_flag=$1
 147         typeset acl_access=$2
 148         typeset acl_inherit_object=$3
 149         typeset acl_inherit_strategy=$4
 150         typeset acl_type=$5
 151 
 152         tmp_ace=${acl_flag}@:
 153 
 154         for element in ${a_access[@]} ; do
 155                 if [[ $acl_access == *"$element"* ]]; then
 156                         tmp_ace="${tmp_ace}${element}"
 157                 else
 158                         tmp_ace="${tmp_ace}-"
 159                 fi
 160         done
 161         tmp_ace=${tmp_ace}:
 162 
 163         for element in ${a_inherit_object[@]} ; do
 164                 if [[ $acl_inherit_object == *"$element"* ]]; then
 165                         tmp_ace="${tmp_ace}${element}"
 166                 else
 167                         tmp_ace="${tmp_ace}-"
 168                 fi
 169         done
 170         for element in ${a_inherit_strategy[@]} ; do
 171                 if [[ $acl_inherit_strategy == *"$element"* ]]; then
 172                         tmp_ace="${tmp_ace}${element}"
 173                 else
 174                         tmp_ace="${tmp_ace}-"
 175                 fi
 176         done
 177 
 178         tmp_ace=${tmp_ace}---:${acl_type}
 179         
 180         echo "${tmp_ace}"
 181 }
 182 
 183 #
 184 # Check if chmod set the compact ACE correctly.
 185 #
 186 function check_test_result # node acl_flag acl_access \
 187                            # acl_inherit_object acl_inherit_strategy acl_type
 188 {
 189         [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
 190                 && set -x
 191 
 192         typeset node=$1
 193         typeset acl_flag=$2
 194         typeset acl_access=$3
 195         typeset acl_inherit_object=$4
 196         typeset acl_inherit_strategy=$5
 197         typeset acl_type=$6
 198 
 199         typeset expect_ace=$(cal_ace "$acl_flag" "$acl_access" \
 200                 "$acl_inherit_object" "$acl_inherit_strategy" "$acl_type")      
 201 
 202         typeset cur_ace=$(get_ACE $node 0 "compact")
 203 
 204         if [[ $cur_ace != $expect_ace ]]; then
 205                 echo "FAIL: Current map($cur_ace) !=  \
 206                         expected ace($expect_ace)"
 207                 cleanup $STF_FAIL
 208         fi
 209 }
 210 
 211 function test_chmod_map #<node>
 212 {
 213         [[ :$NFSGEN_DEBUG: = *:${NAME}:* || :${NFSGEN_DEBUG}: = *:all:* ]] \
 214                 && set -x
 215 
 216         typeset node=$1 
 217         typeset acl_flag acl_access acl_inherit_object acl_inherit_strategy acl_type
 218         typeset -i cnt
 219 
 220         if (( ${#node} == 0 )); then
 221                 echo "FAIL: file name or directroy name is not defined."
 222                 cleanup $STF_FAIL
 223         fi
 224 
 225         # Get ACL flag, access & type
 226         eval "acl_flag=$(form_random_str a_flag)"
 227         (( cnt = ($RANDOM % ${#a_access[@]}) + 1 ))
 228         eval "acl_access=$(form_random_str a_access $cnt)"
 229         acl_access=${acl_access%/}
 230         eval "acl_type=$(form_random_str a_type 1)"
 231 
 232         acl_spec=${acl_flag}@:${acl_access}
 233         if [[ -d $node ]]; then
 234                 # Get ACL inherit_object & inherit_strategy
 235                 (( cnt = ($RANDOM % ${#a_inherit_object[@]}) + 1 ))
 236                 eval "acl_inherit_object=$(form_random_str a_inherit_object $cnt)"
 237                 (( cnt = ($RANDOM % ${#a_inherit_strategy[@]}) + 1 ))
 238                 eval "acl_inherit_strategy=$(form_random_str a_inherit_strategy $cnt)"
 239                 acl_spec=${acl_spec}:${acl_inherit_object}${acl_inherit_strategy}
 240         fi
 241         acl_spec=${acl_spec}:${acl_type}
 242 
 243         # Set the initial map and back the initial ACEs
 244         typeset orig_ace=$STF_TMPDIR/orig_ace.$$
 245         typeset cur_ace=$STF_TMPDIR/cur_ace.$$
 246 
 247         for operator in "A0+" "A0="; do
 248                 RUN_CHECK usr_exec eval "$LS -Vd $node > $orig_ace" \
 249                         || cleanup $STF_FAIL
 250 
 251                 # To "A=", firstly add one ACE which can't modify map
 252                 if [[ $operator == "A0=" ]]; then
 253                         RUN_CHECK $CHMOD A0+user:$ACL_OTHER1:execute:deny \
 254                                 $node || cleanup $STF_FAIL
 255                 fi
 256                 RUN_CHECK usr_exec $CHMOD ${operator}${acl_spec} $node \
 257                         || cleanup $STF_FAIL
 258                         
 259                 check_test_result \
 260                         "$node" "$acl_flag" "$acl_access" \
 261                         "$acl_inherit_object" "$acl_inherit_strategy" \
 262                         "$acl_type"
 263 
 264                 # Check "chmod A-"
 265                 RUN_CHECK usr_exec $CHMOD A0- $node || cleanup $STF_FAIL
 266                 RUN_CHECK usr_exec eval "$LS -Vd $node > $cur_ace" \
 267                         || cleanup $STF_FAIL
 268 
 269                 $DIFF $orig_ace $cur_ace
 270                 [[ $? -ne 0 ]] && \
 271                         echo "FAIL: 'chmod A-' failed." && \
 272                         cleanup $STF_FAIL
 273         done
 274 
 275         [[ -f $orig_ace ]] && RUN_CHECK usr_exec $RM -f $orig_ace
 276         [[ -f $cur_ace ]] &&  RUN_CHECK usr_exec $RM -f $cur_ace
 277 }
 278 
 279 #for user in root $ACL_STAFF1; do
 280 for user in $ACL_STAFF1; do
 281         set_cur_usr $user
 282         
 283         typeset -i loop_cnt=2
 284         while (( loop_cnt > 0 )); do
 285                 RUN_CHECK usr_exec $TOUCH $testfile || cleanup $STF_FAIL
 286                 test_chmod_map $testfile
 287                 RUN_CHECK $RM -f $testfile || cleanup $STF_FAIL
 288 
 289                 RUN_CHECK usr_exec $MKDIR $testdir || cleanup $STF_FAIL
 290                 test_chmod_map $testdir
 291                 RUN_CHECK $RM -rf $testdir || cleanup $STF_FAIL
 292 
 293                 (( loop_cnt -= 1 ))
 294         done
 295 done
 296 
 297 # chmod A{+|=} set compact ACL correctly.
 298 cleanup $STF_PASS