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 2008 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 # ACL getfacl positive test
  28 #     call getfacl(1) get to the ACL from an (file, directory or 
  29 #     attribute) object after chmod(1) with different permissions.
  30 #
  31 
  32 if [ -z "$DEBUG" ]; then
  33         export DEBUG=0 
  34 else
  35         [ "$DEBUG" != "0" ] && set -x
  36 fi
  37 
  38 NAME=`basename $0`
  39 CDIR=`pwd`
  40 
  41 # Source for common functions
  42 . $CDIR/ACL.utils
  43 
  44 # Preparation: create temp file/directory for testing 
  45 # Assume MNTPTR is the base test directory.
  46 TDIR=$MNTPTR/$NAME.dir.$$
  47 TFILE=$MNTPTR/$NAME.file.$$
  48 AFILE=$NAME.attr.$$
  49 EFILE=$TMPDIR/$NAME.err.$$
  50 touch $EFILE
  51 
  52 # setup test file and directory
  53 function test_setup
  54 {
  55     [ "$DEBUG" != "0" ] && set -x
  56     mkdir -m 0777 $TDIR >> $EFILE 2>&1 || return $UNINTIATED
  57     echo "This is test file for $NAME" > $TFILE  2>> $EFILE || \
  58         return $UNINTIATED
  59     chmod 0666 $TFILE >> $EFILE 2>&1 || return $UNINTIATED
  60     runat $TFILE "echo \"This is test file for $TFILE\" > $AFILE" 2>> $EFILE ||\
  61         return $UNINTIATED
  62     runat $TFILE "chmod 0777.; chmod 0666 $AFILE" 2>> $EFILE 
  63 }
  64 
  65 # cleanup to remove tmp files/dirs
  66 function cleanup
  67 {
  68     [ "$DEBUG" != "0" ] && set -x
  69     rm -fr $TDIR $TFILE $EFILE $TMPDIR/$NAME.*.$$
  70     exit $1
  71 }
  72 
  73 # verify the acl permission match the mode bits of an object
  74 function ckace
  75 {
  76     [ "$DEBUG" != "0" ] && set -x
  77     ace=$1              # permission of an ACL entry
  78     mbit=$2             # a file mode bit to be compared
  79     obj=$3              # the object name for printing error message
  80     msgfile=$4          # message file to be printed if DEBUG
  81     case $mbit in
  82         7)      exp_ace="rwx"   ;;
  83         6)      exp_ace="rw-"   ;;
  84         5)      exp_ace="r-x"   ;;
  85         4)      exp_ace="r--"   ;;
  86         3)      exp_ace="-wx"   ;;
  87         2)      exp_ace="-w-"   ;;
  88         1)      exp_ace="--x"   ;;
  89         0)      exp_ace="---"   ;;
  90         *)      exp_ace=""      ;;
  91     esac
  92     if [ "$ace" != "$exp_ace" ]; then
  93         echo "\t Test FAIL: $obj - ACE does not match"
  94         echo "\t\t expect <$exp_ace>, got <$ace>"
  95         [ "$DEBUG" != "0" ] && [ -f $msgfile ] && cat $msgfile
  96         return 1
  97     else
  98         [ "$DEBUG" != "0" ] && \
  99                 echo "ACE match - expect <$exp_ace>, got <$ace>"
 100         return 0
 101     fi
 102 }    
 103 
 104 
 105 # Test assertion driver to loop throught different modes
 106 function run_assert
 107 {
 108     [ "$DEBUG" != "0" ] && set -x
 109     TOBJ=$1             # the object to verify ACL entries
 110     RUNAT=$2            # flag for attribute file
 111 
 112     # Loop throught different modes
 113     modes="777 755 711 700 055 051 011 001 765 654 543 432 321 210 100 000"
 114     for m in $modes
 115     do
 116         # split the bits
 117         user=`echo "$m" | cut -c1`
 118         group=`echo "$m" | cut -c2`
 119         other=`echo "$m" | cut -c3`
 120 
 121         # set the mode; then get its ACL
 122         # should get the default as no ACL was set
 123         CMD="chmod $m $TOBJ" 
 124         if [ "$RUNAT" = "runat" ]; then
 125                 runat $TFILE "$CMD" > $TMPDIR/$NAME.chm.$$ 2>&1
 126         else
 127                 $CMD > $TMPDIR/$NAME.chm.$$ 2>&1
 128         fi
 129         ckreturn $? "$CMD failed" $TMPDIR/$NAME.chm.$$ || return $FAIL
 130         CMD="getfacl ${TOBJ}"
 131         if [ "$RUNAT" = "runat" ]; then
 132                 runat $TFILE "$CMD" > $TMPDIR/$NAME.ga.$$ 2>&1 
 133         else
 134                 $CMD > $TMPDIR/$NAME.ga.$$ 2>&1
 135         fi
 136         ckreturn $? "$CMD failed" $TMPDIR/$NAME.ga.$$ || return $FAIL
 137 
 138         # Verify the default ACEs look OK based on the mode
 139         nusr=`egrep "^user::" $TMPDIR/$NAME.ga.$$ | \
 140                 nawk -F\: '{print $3}' | nawk '{print $1}'`
 141         ngrp=`egrep "^group::" $TMPDIR/$NAME.ga.$$ | \
 142                 nawk -F\: '{print $3}' | nawk '{print $1}'`
 143         noth=`egrep "^other:" $TMPDIR/$NAME.ga.$$ | \
 144                 nawk -F\: '{print $2}' | nawk '{print $1}'`
 145         ckace $nusr $user "$TOBJ|user|$m" $TMPDIR/$NAME.ga.$$ || return $FAIL
 146         ckace $ngrp $group "$TOBJ|group|$m" $TMPDIR/$NAME.ga.$$ || return $FAIL
 147         ckace $noth $other "$TOBJ|other|$m" $TMPDIR/$NAME.ga.$$ || return $FAIL
 148     done
 149 
 150     # restore the original mode
 151     CMD="chmod 0777 $TOBJ"
 152     if [ "$RUNAT" = "runat" ]; then
 153         runat $TFILE "$CMD" 
 154     else
 155         $CMD
 156     fi
 157     echo "\t Test PASS"
 158 }
 159 
 160 
 161 # Start main program here:
 162 # ----------------------------------------------------------------------
 163 test_setup
 164 if [ $? -ne 0 ]; then
 165         echo "$NAME{setup}: preparation for $NAME test"
 166         echo "\t UNINITIATED - no assertions will be run"
 167         cat $EFILE
 168         cleanup $UNINITIATED
 169 fi
 170 
 171 # ----------------------------------------------------------------------
 172 # a: Verify getfacl of a dir with different modes, expect successful
 173 ASSERTION="getfacl of a dir w/different modes, expect successful"
 174 echo "$NAME{a}: $ASSERTION"
 175 run_assert $TDIR reg
 176 
 177 # b: Verify getfacl of a file with different modes, expect successful
 178 ASSERTION="getfacl of a file w/different modes, expect successful"
 179 echo "$NAME{b}: $ASSERTION"
 180 run_assert $TFILE reg
 181 
 182 # c: Verify getfacl of attr file with different modes, expect successful
 183 ASSERTION="getfacl of an attr file w/different modes, expect successful"
 184 echo "$NAME{c}: $ASSERTION"
 185 run_assert $AFILE runat
 186 
 187 
 188 # cleanup and exit
 189 cleanup $PASS