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