Adam Tkac d218af
#!/bin/bash
Adam Tkac d218af
Tomas Hozza e09c55
ROOTDIR_MOUNT='/etc/localtime /etc/named /etc/pki/dnssec-keys /etc/named.root.key /etc/named.conf
Adam Tkac d218af
/etc/named.dnssec.keys /etc/named.rfc1912.zones /etc/rndc.conf /etc/rndc.key
Tomas Hozza 71f9fb
/usr/lib64/bind /usr/lib/bind /etc/named.iscdlv.key /run/named /var/named
Tomas Hozza 71f9fb
/etc/crypto-policies/back-ends/bind.config'
Adam Tkac d218af
Adam Tkac d218af
usage()
Adam Tkac d218af
{
Adam Tkac d218af
  echo
Adam Tkac d218af
  echo 'This script setups chroot environment for BIND'
Adam Tkac d218af
  echo 'Usage: setup-named-chroot.sh ROOTDIR [on|off]'
Adam Tkac d218af
}
Adam Tkac d218af
Adam Tkac d218af
if ! [ "$#" -eq 2 ]; then
Adam Tkac d218af
  echo 'Wrong number of arguments'
Adam Tkac d218af
  usage
Adam Tkac d218af
  exit 1
Adam Tkac d218af
fi
Adam Tkac d218af
Adam Tkac d218af
ROOTDIR="$1"
Adam Tkac d218af
Adam Tkac d218af
# Exit if ROOTDIR doesn't exist
Adam Tkac d218af
if ! [ -d "$ROOTDIR" ]; then
Adam Tkac d218af
  echo "Root directory $ROOTDIR doesn't exist"
Adam Tkac d218af
  usage
Adam Tkac d218af
  exit 1
Adam Tkac d218af
fi
Adam Tkac d218af
Adam Tkac d218af
mount_chroot_conf()
Adam Tkac d218af
{
Adam Tkac d218af
  if [ -n "$ROOTDIR" ]; then
Adam Tkac d218af
    for all in $ROOTDIR_MOUNT; do
Adam Tkac d218af
      # Skip nonexistant files
Adam Tkac d218af
      [ -e "$all" ] || continue
Adam Tkac d218af
Adam Tkac d218af
      # If mount source is a file
Adam Tkac d218af
      if ! [ -d "$all" ]; then
Adam Tkac d218af
        # mount it only if it is not present in chroot or it is empty
Adam Tkac d218af
        if ! [ -e "$ROOTDIR$all" ] || [ `stat -c'%s' "$ROOTDIR$all"` -eq 0 ]; then
Adam Tkac d218af
          touch "$ROOTDIR$all"
Adam Tkac d218af
          mount --bind "$all" "$ROOTDIR$all"
Adam Tkac d218af
        fi
Adam Tkac d218af
      else
Adam Tkac d218af
        # Mount source is a directory. Mount it only if directory in chroot is
Adam Tkac d218af
        # empty.
Adam Tkac d218af
        if [ -e "$all" ] && [ `ls -1A $ROOTDIR$all | wc -l` -eq 0 ]; then
Tomas Hozza 7eb562
          mount --bind --make-private "$all" "$ROOTDIR$all"
Adam Tkac d218af
        fi
Adam Tkac d218af
      fi
Adam Tkac d218af
    done
Adam Tkac d218af
  fi
Adam Tkac d218af
}
Adam Tkac d218af
Adam Tkac d218af
umount_chroot_conf()
Adam Tkac d218af
{
Adam Tkac 773ac2
  if [ -n "$ROOTDIR" ]; then
Adam Tkac 773ac2
    for all in $ROOTDIR_MOUNT; do
Adam Tkac 773ac2
      # Check if file is mount target. Do not use /proc/mounts because detecting
Adam Tkac 773ac2
      # of modified mounted files can fail.
Adam Tkac 773ac2
      if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
Adam Tkac 773ac2
        umount "$ROOTDIR$all"
Adam Tkac 773ac2
        # Remove temporary created files
Adam Tkac 773ac2
        [ -f "$all" ] && rm -f "$ROOTDIR$all"
Adam Tkac 773ac2
      fi
Adam Tkac 773ac2
    done
Adam Tkac 773ac2
  fi
Adam Tkac d218af
}
Adam Tkac d218af
Adam Tkac d218af
case "$2" in
Adam Tkac d218af
  on)
Adam Tkac d218af
    mount_chroot_conf
Adam Tkac d218af
    ;;
Adam Tkac d218af
  off)
Adam Tkac d218af
    umount_chroot_conf
Adam Tkac d218af
    ;;
Adam Tkac d218af
  *)
Adam Tkac d218af
    echo 'Second argument has to be "on" or "off"'
Adam Tkac d218af
    usage
Adam Tkac d218af
    exit 1
Adam Tkac d218af
esac
Adam Tkac d218af
Adam Tkac d218af
exit 0