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