#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a backup of the Bacula build directory 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 OSX and Solaris:
#
#    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
. scripts/functions

#
# 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

scripts/cleanup
scripts/copy-confs

#
# 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 ${cwd}/bin/bacula-dir.conf  $cwd/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf

d=${cwd}/build/acl

rm -rf $d
mkdir -p $d
mkdir -p $d/acl-dir
cp ${cwd}/bin/bconsole $d
ln -s $d/bconsole $d/bconsole.lnk

case `uname -s` in
   Linux)
      uid=`id -u`
      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 ${cwd}/bin/bconsole $d/acl-dir
      cp ${cwd}/bin/bconsole $d/other
      setfattr -h -n user.bacula.test -v rulez $d/other 2>/dev/null 1>/dev/null
      setfattr -h -n user.bacula.secondtest -v rulez $d/other 2>/dev/null 1>/dev/null
      setfattr -h -n user.bacula.thirdtest -v rulez $d/bconsole.lnk 2>/dev/null 1>/dev/null

      ( cd $cwd/build
        getfacl -R acl 2>/dev/null > $cwd/tmp/org
        getfattr -h -n user.bacula.test $d/other 2>/dev/null > $cwd/tmp/attr.org
        getfattr -h -n user.bacula.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.org
        getfattr -h -n user.bacula.thirdtest $d/bconsole.lnk 2>/dev/null >> $cwd/tmp/attr.org
      )
      ;;
   Darwin)
      username=`id -un`
      uid=`id -u`
      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 ${cwd}/bin/bconsole $d/acl-dir
      cp ${cwd}/bin/bconsole $d/other
      xattr -w  bacula.test "rulez" $d/other 2>/dev/null 1>/dev/null
      xattr -w  bacula.secondtest "rulez" $d/other 2>/dev/null 1>/dev/null

      ( cd $cwd/build
        ls -lde -R acl > $cwd/tmp/org
        xattr -p bacula.test $d/other 2>/dev/null > $cwd/tmp/attr.org
        xattr -p bacula.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.org
      )
      ;;
   SunOS)
      #
      # See if we need to set ZFS or POSIX acls
      #
      uid=`/usr/xpg4/bin/id -u`
      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 ${cwd}/bin/bconsole $d/other
      /bin/runat $d/other 'cat > bacula.test' << EOF
rulez
EOF
      /bin/runat $d/other 'cat > bacula.secondtest' << EOF
rulez
EOF
      /bin/runat $d/other 'ln bacula.secondtest linking.test'
      ( cd $cwd/build
        /bin/ls -ldv -R acl > $cwd/tmp/org
        /bin/runat $d/other 'cat bacula.test' > $cwd/tmp/attr.org
        /bin/runat $d/other 'cat bacula.secondtest' >> $cwd/tmp/attr.org
      )
      ;;
   FreeBSD)
      uid=`id -u`
      setfacl -m d:user:$uid:r-x $d/acl-dir 
      setfacl -m d:user:root:-wx $d/acl-dir
      setfacl -m user:nobody:--- $d/acl-dir
      setfacl -m user:nobody:--- $d/bconsole
      setfacl -m group:nogroup:--x $d/bconsole
      cp ${cwd}/bin/bconsole $d/acl-dir
      cp ${cwd}/bin/bconsole $d/other
      setextattr user bacula.test rulez $d/other 2>/dev/null 1>/dev/null
      setextattr user bacula.secondtest rulez $d/other 2>/dev/null 1>/dev/null

      ( cd $cwd/build
        for file in acl/*
        do
           getfacl $file
        done > $cwd/tmp/org
        getextattr user bacula.test $d/other 2>/dev/null > $cwd/tmp/attr.org
        getextattr user bacula.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.org
      )
      ;;
   NetBSD)
      uid=`id -u`
      cp ${cwd}/bin/bconsole $d/other
      setextattr user bacula.test rulez $d/other 2>/dev/null 1>/dev/null
      setextattr user bacula.secondtest rulez $d/other 2>/dev/null 1>/dev/null

      ( cd $cwd/build
        cp /dev/null $cwd/tmp/org
        getextattr user bacula.test $d/other > $cwd/tmp/attr.org
        getextattr user bacula.secondtest $d/other >> $cwd/tmp/attr.org
      )
      ;;
esac

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label volume=TestVolume001 storage=File1 pool=File 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 where=${cwd}/tmp/bacula-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File1
stop_bacula

case `uname -s` in
   Linux)
      ( cd $cwd/tmp/bacula-restores/$cwd/build
        getfacl -R acl > $cwd/tmp/new
        getfattr -h -n user.bacula.test $d/other 2>/dev/null > $cwd/tmp/attr.new
        getfattr -h -n user.bacula.secondtest $d/other 2>/dev/null >> $cwd/tmp/attr.new
        getfattr -h -n user.bacula.thirdtest $d/bconsole.lnk 2>/dev/null >> $cwd/tmp/attr.new
      )
      ;;
   Darwin)
      ( cd $cwd/tmp/bacula-restores/$cwd/build
        ls -lde -R acl > $cwd/tmp/new
        xattr -p bacula.test $d/other > $cwd/tmp/attr.new
        xattr -p bacula.secondtest $d/other >> $cwd/tmp/attr.new
      )
      ;;
   SunOS)
      ( cd $cwd/tmp/bacula-restores/$cwd/build
        /bin/ls -ldv -R acl > $cwd/tmp/new
        /bin/runat $d/other 'cat bacula.test' > $cwd/tmp/attr.new
        /bin/runat $d/other 'cat bacula.secondtest' >> $cwd/tmp/attr.new
      )
      ;;
   FreeBSD)
      ( cd $cwd/tmp/bacula-restores/$cwd/build
        for file in acl/*
        do
           getfacl $file
        done > $cwd/tmp/new
        getextattr user bacula.test $d/other > $cwd/tmp/attr.new
        getextattr user bacula.secondtest $d/other >> $cwd/tmp/attr.new
      )
      ;;
   NetBSD)
      ( cd $cwd/tmp/bacula-restores/$cwd/build
        cp /dev/null $cwd/tmp/new
        getextattr user bacula.test $d/other > $cwd/tmp/attr.new
        getextattr user bacula.secondtest $d/other >> $cwd/tmp/attr.new
      )
      ;;
esac

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

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

check_two_logs
check_restore_diff

if [ x$REGRESS_DEBUG != x ]; then
  rm -rf $d
fi

end_test
