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