e55890
#!/bin/bash
e55890
e55890
ROOTDIR="$1"
e55890
CONFIG_FILES="${3:-/etc/named-chroot.files}"
e55890
e55890
usage()
e55890
{
e55890
  echo
e55890
  echo 'This script setups chroot environment for BIND'
e55890
  echo 'Usage: setup-named-chroot.sh ROOTDIR <on|off> [chroot.files]'
e55890
}
e55890
e55890
if ! [ "$#" -ge 2 -a "$#" -le 3 ]; then
e55890
  echo 'Wrong number of arguments'
e55890
  usage
e55890
  exit 1
e55890
fi
e55890
e55890
# Exit if ROOTDIR doesn't exist
e55890
if ! [ -d "$ROOTDIR" ]; then
e55890
  echo "Root directory $ROOTDIR doesn't exist"
e55890
  usage
e55890
  exit 1
e55890
fi
e55890
e55890
if ! [ -r "$CONFIG_FILES" ]; then
e55890
  echo "Files list $CONFIG_FILES doesn't exist" 2>&1
e55890
  usage
e55890
  exit 1
e55890
fi
e55890
e55890
dev_create()
e55890
{
e55890
  DEVNAME="$ROOTDIR/dev/$1"
e55890
  shift
e55890
  if ! [ -e "$DEVNAME" ]; then
e55890
    /bin/mknod -m 0664 "$DEVNAME" $@
e55890
    /bin/chgrp named "$DEVNAME"
e55890
    if [ -x /usr/sbin/selinuxenabled -a -x /sbin/restorecon ]; then
e55890
      /usr/sbin/selinuxenabled && /sbin/restorecon "$DEVNAME" > /dev/null || :
e55890
    fi
e55890
  fi
e55890
}
e55890
e55890
dev_chroot_prep()
e55890
{
e55890
  dev_create random  c 1 8
e55890
  dev_create urandom c 1 9
e55890
  dev_create zero    c 1 5
e55890
  dev_create null    c 1 3
e55890
}
e55890
e55890
files_comment_filter()
e55890
{
e55890
  if [ -d "$1" ]; then
e55890
    grep -v '^[[:space:]]*#' "$1"/*.files
e55890
  else
e55890
    grep -v '^[[:space:]]*#' "$1"
e55890
  fi
e55890
}
e55890
e55890
mount_chroot_conf()
e55890
{
e55890
  if [ -n "$ROOTDIR" ]; then
e55890
    # Check devices are prepared
e55890
    dev_chroot_prep
e55890
    files_comment_filter "$CONFIG_FILES" | while read -r all; do
e55890
      # Skip nonexistant files
e55890
      [ -e "$all" ] || continue
e55890
e55890
      # If mount source is a file
e55890
      if ! [ -d "$all" ]; then
e55890
        # mount it only if it is not present in chroot or it is empty
e55890
        if ! [ -e "$ROOTDIR$all" ] || [ `stat -c'%s' "$ROOTDIR$all"` -eq 0 ]; then
e55890
          touch "$ROOTDIR$all"
e55890
          mount --bind "$all" "$ROOTDIR$all"
e55890
        fi
e55890
      else
e55890
        # Mount source is a directory. Mount it only if directory in chroot is
e55890
        # empty.
e55890
        if [ -e "$all" ] && [ `ls -1A $ROOTDIR$all | wc -l` -eq 0 ]; then
e55890
          mount --bind --make-private "$all" "$ROOTDIR$all"
e55890
        fi
e55890
      fi
e55890
    done
e55890
  fi
e55890
}
e55890
e55890
umount_chroot_conf()
e55890
{
e55890
  if [ -n "$ROOTDIR" ]; then
e55890
    files_comment_filter "$CONFIG_FILES" | while read -r all; do
e55890
      # Check if file is mount target. Do not use /proc/mounts because detecting
e55890
      # of modified mounted files can fail.
e55890
      if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
e55890
        umount "$ROOTDIR$all"
e55890
        # Remove temporary created files
e55890
        [ -f "$all" ] && rm -f "$ROOTDIR$all"
e55890
      fi
e55890
    done
e55890
  fi
e55890
}
e55890
e55890
case "$2" in
e55890
  on)
e55890
    mount_chroot_conf
e55890
    ;;
e55890
  off)
e55890
    umount_chroot_conf
e55890
    ;;
e55890
  *)
e55890
    echo 'Second argument has to be "on" or "off"'
e55890
    usage
e55890
    exit 1
e55890
esac
e55890
e55890
exit 0