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