Blame SOURCES/setup-named-chroot.sh

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