Blame SOURCES/setup-named-chroot.sh

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