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