#!/bin/sh

. scripts/functions

#
# Run a backup  with some acls then restore it.
#
# For Linux:
#    Your filesystem must be mounted with the acl option (mount -o remount,acl,user_xattr /tmp)
#    on ubuntu, the attr package must be installed
#
#    For this script to work, you will also need a number of acl packages loaded
#    not default on Debian derivatives:
#       apt-get install acl libacl1 libacl1-dev attr
#
# For Solaris:
#
#    Defaults should be ok, but restoring xattrs requires that bareos-fd is started as root.
#
# For OSX:
#
#    Defaults should be ok
#
# For FreeBSD:
#
#    Your filesystem must have acls enabled and needs to be mounted with acls.
#       tunefs -a enable <device>
#       acls mountoption in /etc/fstab
#
# For NetBSD:
#
#    Defaults should be ok (As NetBSD doesn't support native acls we skip that test).
#
TestName="acl-xattr-test"
JobName=backup

# Directory to backup.
# This directory will be created by setup_data().
# If set, $BackupDirectory will be used
# as base directory by check_restore_diff().
# Don't use a "/" at the end of the directory path,
# as it might cause problems
# (filenames given as parameter to restore must match 100%. "//" are not identical with "/").
BackupDirectory="${tmp}/data"

#
# See if the right software is installed.
#
case `uname -s` in
   Linux)
      # Require getfacl to be installed
      getfacl Makefile 2>&1 >/dev/null
      if test  $? -ne 0; then
        echo "$TestName skipped: getfacl not installed"
        exit 0
      fi

      # Require getfattr to be installed
      getfattr -d Makefile 2>&1 >/dev/null
      if test $? -ne 0; then
        echo "$TestName skipped: getfattr not installed"
        exit 0
      fi
      ;;
   Darwin)
      ;;
   SunOS)
      ;;
   FreeBSD)
      # Require getfacl to be installed
      getfacl Makefile 2>&1 >/dev/null
      if test  $? -ne 0; then
        echo "$TestName skipped: getfacl not installed"
        exit 0
      fi

      # Require lsextattr to be installed
      lsextattr user Makefile 2>&1 >/dev/null
      if test $? -ne 0; then
        echo "$TestName skipped: lsextattr not installed"
        exit 0
      fi
      ;;
   NetBSD)
      # Require lsextattr to be installed
      lsextattr user Makefile 2>&1 >/dev/null
      if test $? -ne 0; then
        echo "$TestName skipped: lsextattr not installed"
        exit 0
      fi
      ;;
   *)
      echo "Unsupported OS"
      exit 0
      ;;
esac

# Remove old configuration, working and tmp files. Setup the database.
cleanup


start_test


scripts/copy-confs
change_jobname BackupClient1FileList $JobName

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" > $outf
echo "s/Options {/Options { aclsupport=yes; xattrsupport = yes/" >> $outf
cp ${conf}/bareos-dir.conf  $cwd/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${conf}/bareos-dir.conf


# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data".
setup_data data/small.tgz

d="${BackupDirectory}/acl"

uid=`id -u`
rm -rf $d
mkdir -p $d/acl-dir
cp ${bin}/bconsole $d
ls -la $d

case `uname -s` in
   Linux)
      setfacl -m d:user:$uid:r-x $d/acl-dir 2>/dev/null 1>/dev/null
      setfacl -m d:user:root:-wx $d/acl-dir 2>/dev/null 1>/dev/null
      setfacl -m user:nobody:--- $d/acl-dir 2>/dev/null 1>/dev/null
      setfacl -m user:nobody:--- $d/bconsole 2>/dev/null 1>/dev/null
      setfacl -m group:nogroup:--x $d/bconsole 2>/dev/null 1>/dev/null
      cp ${bin}/bconsole $d/acl-dir
      cp ${bin}/bconsole $d/other
      setfattr -h -n user.bareos.test -v rulez $d/other 2>/dev/null 1>/dev/null
      setfattr -h -n user.bareos.secondtest -v rulez $d/other 2>/dev/null 1>/dev/null

      ( cd ${BackupDirectory}
        getfacl --omit-header -R acl 2>/dev/null > $cwd/tmp/org
        getfattr --only-values -h -n user.bareos.test $d/other 2>/dev/null > $cwd/tmp/attr.org
        getfattr --only-values -h -n user.bareos.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.org
      )
      ;;
   Darwin)
      username=`id -un`
      chmod +a "user:$username allow read,execute" $d/acl-dir
      chmod +a "user:root allow write,execute" $d/acl-dir
      chmod +a "user:nobody deny read,write,execute" $d/acl-dir
      chmod +a "user:nobody deny read,write,execute" $d/bconsole
      chmod +a "group:nogroup allow execute" $d/bconsole
      cp ${bin}/bconsole $d/acl-dir
      cp ${bin}/bconsole $d/other
      xattr -w  bareos.test "rulez" $d/other 2>/dev/null 1>/dev/null
      xattr -w  bareos.secondtest "rulez" $d/other 2>/dev/null 1>/dev/null

      ( cd ${BackupDirectory}
        ls -lde -R acl > $cwd/tmp/org
        xattr -p bareos.test $d/other 2>/dev/null > $cwd/tmp/attr.org
        xattr -p bareos.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.org
      )
      ;;
   SunOS)
      #
      # See if we need to set ZFS or POSIX acls
      #
      df -F zfs $d > /dev/null 2>&1
      if [ $? = 0 ]; then
         /bin/chmod A+user:$uid:rx:allow $d/acl-dir
         /bin/chmod A+user:root:wx:allow $d/acl-dir
         /bin/chmod A+user:nobody:rwx:deny $d/acl-dir
         /bin/chmod A+user:nobody:rwx:deny $d/bconsole
         /bin/chmod A+group:nogroup:x:allow $d/bconsole
      else
         /bin/chmod A+user:$uid:r-x $d/acl-dir
         /bin/chmod A+user:root:-wx $d/acl-dir
         /bin/chmod A+user:nobody:--- $d/acl-dir
         /bin/chmod A+user:nobody:--- $d/bconsole
         /bin/chmod A+group:nogroup:--x $d/bconsole
      fi
      cp ${bin}/bconsole $d/other
      /bin/runat $d/other 'cat > bareos.test' << EOF
rulez
EOF
      /bin/runat $d/other 'cat > bareos.secondtest' << EOF
rulez
EOF
      ( cd ${BackupDirectory}
        /bin/ls -ldv -R acl > $cwd/tmp/org
        /bin/runat $d/other 'cat bareos.test' > $cwd/tmp/attr.org
        /bin/runat $d/other 'cat bareos.secondtest' >> $cwd/tmp/attr.org
      )
      ;;
   FreeBSD)
      rootfs=`mount -p | head -1 | awk '{print $3}'`

      if [ "$rootfs" = "zfs" ]; then
         setfacl -m u:$uid:rx::allow $d/acl-dir
         setfacl -m u:root:wx::allow $d/acl-dir
         setfacl -m u:nobody::deny $d/acl-dir
         setfacl -m u:nobody::deny $d/bconsole
         setfacl -m g:nogroup:x::allow $d/bconsole
      fi

      if [ "$rootfs" = "ufs" ]; then
         setfacl -m u:$uid:rx $d/acl-dir
         setfacl -m u:root:wx $d/acl-dir
         setfacl -m u:nobody: $d/acl-dir
         setfacl -m u:nobody: $d/bconsole
         setfacl -m g:nogroup:x $d/bconsole
      fi

      cp ${bin}/bconsole $d/acl-dir
      cp ${bin}/bconsole $d/other
      setextattr user bareos.test rulez $d/other 2>/dev/null 1>/dev/null
      setextattr user bareos.secondtest rulez $d/other 2>/dev/null 1>/dev/null

      ( cd ${BackupDirectory}
      for file in acl/*
         do
            getfacl $file
      done > $cwd/tmp/org
      getextattr -q user bareos.test $d/other 2>/dev/null > $cwd/tmp/attr.org
      getextattr -q user bareos.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.org
      )
      ;;
   NetBSD)
      cp ${bin}/bconsole $d/other
      setextattr user bareos.test rulez $d/other 2>/dev/null 1>/dev/null
      setextattr user bareos.secondtest rulez $d/other 2>/dev/null 1>/dev/null

      ( cd ${BackupDirectory}
        cp /dev/null $cwd/tmp/org
        getextattr user bareos.test $d/other > $cwd/tmp/attr.org
        getextattr user bareos.secondtest $d/other >> $cwd/tmp/attr.org
      )
      ;;
esac


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label volume=TestVolume001 storage=File pool=Full slot=1 drive=0
@#setdebug level=400 trace=1 client
@#setdebug level=300 trace=1 director
@#setdebug level=300 trace=1 storage=File1
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore restorejob=RestoreFiles where=${cwd}/tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

rd=$cwd/tmp/bareos-restores${BackupDirectory}/acl
case `uname -s` in
   Linux)
      ( cd $rd/..
        getfacl --omit-header -R acl > $cwd/tmp/new
        getfattr --only-values -h -n user.bareos.test $rd/other 2>/dev/null > $cwd/tmp/attr.new
        getfattr --only-values -h -n user.bareos.secondtest $rd/other 2>/dev/null >> $cwd/tmp/attr.new
      )
      ;;
   Darwin)
      ( cd $rd/..
        ls -lde -R acl > $cwd/tmp/new
        xattr -p bareos.test $rd/other > $cwd/tmp/attr.new
        xattr -p bareos.secondtest $rd/other >> $cwd/tmp/attr.new
      )
      ;;
   SunOS)
      ( cd $rd/..
        /bin/ls -ldv -R acl > $cwd/tmp/new
        /bin/runat $rd/other 'cat bareos.test' > $cwd/tmp/attr.new
        /bin/runat $rd/other 'cat bareos.secondtest' >> $cwd/tmp/attr.new
      )
      ;;
   FreeBSD)
      ( cd $rd/..
        for file in acl/*
        do
           getfacl $file
        done > $cwd/tmp/new
        getextattr -q user bareos.test $rd/other > $cwd/tmp/attr.new
        getextattr -q user bareos.secondtest $rd/other >> $cwd/tmp/attr.new
      )
      ;;
   NetBSD)
      ( cd $rd/..
        cp /dev/null $cwd/tmp/new
        getextattr user bareos.test $rd/other > $cwd/tmp/attr.new
        getextattr user bareos.secondtest $rd/other >> $cwd/tmp/attr.new
      )
      ;;
esac

if [ "$rootfs" = "zfs" ]; then
   echo "NFSv4 ACL Test skipped. Not supported yet."
else
   diff -u $cwd/tmp/org $cwd/tmp/new
   if [ $? -ne 0 ]; then
      rstat=1
   fi
fi

diff $cwd/tmp/attr.org $cwd/tmp/attr.new
if [ $? -ne 0 ]; then
   rstat=1
fi

check_two_logs
check_restore_diff

end_test
