2006-03-07 04:25:38 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Script to control the bind-chroot ISC BIND named(8) server runtime environment.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# [ -e | --enable ] [ -d | --disable ] | [ -s --sync ]
|
|
|
|
#
|
|
|
|
# -e | --enable: enable the bind-chroot environment
|
|
|
|
# -d | --disable: disable the bind-chroot environment
|
|
|
|
# -s | --sync: sync files between the bind chroot and / environments,
|
|
|
|
# so they are correct for the current state of the bind-chroot
|
|
|
|
# (enabled / disabled)
|
|
|
|
# $BIND_CHROOT_PREFIX, default /var/named/chroot, is the location of the chroot.
|
|
|
|
# $BIND_DIR, default /var/named, is the default un-chrooted bind directory.
|
|
|
|
#
|
|
|
|
# Copyright(C) 2006 Jason Vas Dias <jvdias@redhat.com>, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This software is provided under the terms of the GNU
|
|
|
|
# General Public License (GPL), as published at:
|
|
|
|
# http://www.gnu.org/licenses/gpl.html .
|
|
|
|
#
|
|
|
|
#
|
|
|
|
BIND_CHROOT_PREFIX=${BIND_CHROOT_PREFIX:-@BIND_CHROOT_PREFIX@}
|
|
|
|
BIND_DIR=${BIND_DIR:-@BIND_DIR@}
|
|
|
|
|
|
|
|
function usage()
|
|
|
|
{
|
|
|
|
echo 'Usage:
|
|
|
|
-e | --enable: enable the bind-chroot environment
|
|
|
|
-d | --disable: disable the bind-chroot environment
|
|
|
|
-s | --sync: sync files between the bind chroot and / environments,
|
|
|
|
so they are correct for the current state of the bind-chroot
|
|
|
|
(enabled / disabled)
|
|
|
|
$BIND_CHROOT_PREFIX, default /var/named/chroot, is the location of the chroot.
|
|
|
|
$BIND_DIR, default /var/named, is the default un-chrooted bind directory.
|
|
|
|
';
|
|
|
|
}
|
|
|
|
|
|
|
|
function rootdir()
|
|
|
|
{
|
|
|
|
. /etc/sysconfig/named
|
|
|
|
if [ -n "$ROOTDIR" ]; then
|
|
|
|
BIND_CHROOT_PREFIX="$ROOTDIR";
|
|
|
|
BIND_CHROOT_PREFIX=`echo $BIND_CHROOT_PREFIX | sed 's#//*#/#g;s#/$##'`;
|
|
|
|
if [ -L "$BIND_CHROOT_PREFIX" ]; then
|
|
|
|
BIND_CHROOT_PREFIX=`/usr/bin/readlink "$BIND_CHROOT_PREFIX"`;
|
|
|
|
fi
|
2007-02-15 15:46:08 +00:00
|
|
|
ENABLED=0;
|
|
|
|
else
|
|
|
|
ENABLED=1;
|
2006-03-07 04:25:38 +00:00
|
|
|
fi;
|
|
|
|
}
|
|
|
|
|
2006-06-14 05:26:43 +00:00
|
|
|
function selinux_enabled()
|
|
|
|
{
|
2007-03-16 15:25:39 +00:00
|
|
|
if [ -x /usr/sbin/selinuxenabled ]; then
|
|
|
|
/usr/sbin/selinuxenabled;
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
return 1;
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
return 0;
|
2006-06-14 05:26:43 +00:00
|
|
|
}
|
|
|
|
|
2006-03-07 04:25:38 +00:00
|
|
|
function check_dirs()
|
|
|
|
{
|
|
|
|
if [ -z "$BIND_CHROOT_PREFIX" ]; then
|
2007-02-15 15:46:08 +00:00
|
|
|
usage;
|
|
|
|
exit 1;
|
|
|
|
fi;
|
2006-03-07 04:25:38 +00:00
|
|
|
BIND_DIR=`echo $BIND_DIR | sed 's#//*#/#g;s#/$##'`;
|
|
|
|
if [ -L "$BIND_DIR" ]; then
|
|
|
|
BIND_DIR=`/usr/bin/readlink "$BIND_DIR"`;
|
|
|
|
fi
|
|
|
|
BIND_CHROOT_PREFIX=`echo $BIND_CHROOT_PREFIX | sed 's#//*#/#g;s#/$##'`;
|
|
|
|
if [ -L "$BIND_CHROOT_PREFIX" ]; then
|
|
|
|
BIND_CHROOT_PREFIX=`/usr/bin/readlink "$BIND_CHROOT_PREFIX"`;
|
|
|
|
fi
|
2006-04-18 23:54:14 +00:00
|
|
|
if [ -e /etc/sysconfig/named ]; then
|
|
|
|
/bin/chown root:named /etc/sysconfig/named;
|
|
|
|
/bin/chmod 0640 /etc/sysconfig/named;
|
|
|
|
fi
|
2006-03-07 04:25:38 +00:00
|
|
|
/bin/mkdir -p ${BIND_DIR}/{slaves,data};
|
2006-11-21 16:33:52 +00:00
|
|
|
/bin/chown --preserve-root root:named ${BIND_DIR};
|
|
|
|
/bin/chown --preserve-root named:named ${BIND_DIR}/{slaves,data};
|
|
|
|
/bin/chmod --preserve-root 750 ${BIND_DIR}
|
|
|
|
/bin/chmod --preserve-root 770 ${BIND_DIR}/{slaves,data};
|
2006-03-07 04:25:38 +00:00
|
|
|
|
2006-03-08 00:42:02 +00:00
|
|
|
mkdir -p ${BIND_CHROOT_PREFIX}/{etc,dev,var/{run/named,named/{slaves,data}}};
|
2006-11-21 16:33:52 +00:00
|
|
|
/bin/chown --preserve-root root:named ${BIND_CHROOT_PREFIX}/{etc,dev,var/{run,named/}};
|
|
|
|
/bin/chown --preserve-root root:named ${BIND_CHROOT_PREFIX}/var;
|
|
|
|
/bin/chmod --preserve-root 750 ${BIND_CHROOT_PREFIX}/{,etc,dev,var,var/{run,named/}};
|
|
|
|
/bin/chown --preserve-root named:named ${BIND_CHROOT_PREFIX}/var/{run/named,named/{data,slaves}};
|
|
|
|
/bin/chmod --preserve-root 770 ${BIND_CHROOT_PREFIX}/var/{run/named,named/{slaves,data}};
|
2006-04-18 23:54:14 +00:00
|
|
|
|
2006-03-08 00:42:02 +00:00
|
|
|
[ ! -e "${BIND_CHROOT_PREFIX}/dev/random" ] && /bin/mknod "${BIND_CHROOT_PREFIX}/dev/random" c 1 8
|
2006-03-07 04:25:38 +00:00
|
|
|
[ ! -e "${BIND_CHROOT_PREFIX}/dev/zero" ] && /bin/mknod "${BIND_CHROOT_PREFIX}/dev/zero" c 1 5
|
|
|
|
[ ! -e "${BIND_CHROOT_PREFIX}/dev/null" ] && /bin/mknod "${BIND_CHROOT_PREFIX}/dev/null" c 1 3
|
|
|
|
[ ! -e "${BIND_CHROOT_PREFIX}/etc/localtime" ] && [ -e /etc/localtime ] && /bin/cp -fp /etc/localtime "${BIND_CHROOT_PREFIX}/etc/localtime";
|
2006-11-21 16:33:52 +00:00
|
|
|
/bin/chown --preserve-root root:named "${BIND_CHROOT_PREFIX}"/dev/{random,null,zero};
|
|
|
|
/bin/chmod --preserve-root 660 "${BIND_CHROOT_PREFIX}"/dev/{random,null,zero};
|
2007-03-06 15:32:16 +00:00
|
|
|
if selinux_enabled && [ -x /sbin/restorecon ]; then
|
2006-03-08 00:42:02 +00:00
|
|
|
for dev in random zero null; do
|
2007-03-06 15:32:16 +00:00
|
|
|
/sbin/restorecon ${BIND_CHROOT_PREFIX}/dev/$dev;
|
2006-03-08 00:42:02 +00:00
|
|
|
done
|
|
|
|
fi;
|
2006-03-07 04:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check_dirs;
|
|
|
|
|
|
|
|
function replace_with_link()
|
|
|
|
{ # replaces $dst second arg file with link to $src first arg file
|
|
|
|
if [ $# -lt 2 ]; then
|
|
|
|
return 1;
|
|
|
|
fi;
|
|
|
|
src=$1
|
|
|
|
dst=$2
|
|
|
|
if [ -z "$src" ] || [ -z "$dst" ] || [ "$src" = "$dst" ]; then
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
if [ ! -e "$src" ]; then
|
|
|
|
if [ ! -e "$dst" ]; then
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
if [ -L "$dst" ]; then
|
|
|
|
dstlnk=`/usr/bin/readlink "$dst"`;
|
|
|
|
if [ ! -e "$dstlnk" ] ; then
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
rm -f "$dst";
|
|
|
|
/bin/cp -fp "$dstlnk" "$dst";
|
|
|
|
fi;
|
|
|
|
/bin/mv "$dst" "$src";
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ -e "$dst" ]; then
|
|
|
|
if [ ! -L "$dst" ]; then
|
2006-04-18 23:54:14 +00:00
|
|
|
if [ ! -s "$dst" ] || /usr/bin/cmp "$dst" "$src" > /dev/null 2>&1; then
|
2006-03-07 04:25:38 +00:00
|
|
|
/bin/rm -f "$dst";
|
|
|
|
else
|
|
|
|
if [ "$src" -nt "$dst" ] || [ ! "$dst" -nt "$src" ] ; then
|
|
|
|
/bin/mv "$dst" "$dst".`/bin/date +'%Y-%m-%d_%H-%M-%S.%N'`;
|
|
|
|
else # [ "$dst" -nt "$src" ]
|
|
|
|
/bin/mv "$src" "$src".`/bin/date +'%Y-%m-%d_%H-%M-%S.%N'`;
|
|
|
|
/bin/mv "$dst" "$src";
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
else
|
|
|
|
dstlnk=`/usr/bin/readlink "$dst"`
|
|
|
|
if [ "$dstlnk" != $src ]; then
|
|
|
|
/bin/rm -f $dst;
|
2006-04-18 23:54:14 +00:00
|
|
|
if ! /usr/bin/cmp "$dstlnk" "$src" > /dev/null 2>&1; then
|
|
|
|
if [ "$dstlnk" != "$dst" ] && [ -s $dstlnk ]; then
|
|
|
|
if [ "$dstlnk" -nt "$src" ] || [ ! "$dstlnk" -nt "$src" ] ; then
|
|
|
|
/bin/cp -fp "$dstlnk" "$dst".`/bin/date +'%Y-%m-%d_%H-%M-%S.%N'`;
|
|
|
|
else
|
|
|
|
/bin/mv "$src" "$src".`/bin/date +'%Y-%m-%d_%H-%M-%S.%N'`;
|
|
|
|
/bin/cp -fp "$dstlnk" "$src";
|
|
|
|
fi;
|
|
|
|
fi;
|
2006-03-07 04:25:38 +00:00
|
|
|
fi;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
/bin/ln -sf "$src" "$dst";
|
|
|
|
return $?;
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace_with_file()
|
|
|
|
{
|
|
|
|
if [ $# -lt 2 ]; then
|
|
|
|
return 1;
|
|
|
|
fi;
|
|
|
|
src=$1;
|
|
|
|
dst=$2;
|
|
|
|
if [ -z "$src" ] || [ -z "$dst" ] || [ "$src" = "$dst" ]; then
|
|
|
|
return 1;
|
|
|
|
fi
|
|
|
|
if [ ! -e "$src" ]; then
|
|
|
|
if [ -e "$dst" ]; then
|
|
|
|
/bin/rm -f $dst;
|
|
|
|
fi;
|
|
|
|
return 1;
|
|
|
|
fi;
|
|
|
|
if [ -e "$dst" ]; then
|
|
|
|
if [ ! -L "$dst" ]; then
|
|
|
|
/bin/mv "$dst" "$dst".`/bin/date +'%Y-%m-%d_%H-%M-%S.%N'`;
|
|
|
|
else
|
|
|
|
/bin/rm -f "$dst";
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
/bin/mv -f "$src" "$dst";
|
|
|
|
}
|
|
|
|
|
|
|
|
function enable_bind_chroot()
|
|
|
|
{
|
2007-03-16 15:25:39 +00:00
|
|
|
rootdir;
|
2006-03-08 00:42:02 +00:00
|
|
|
if /bin/egrep -q '^ROOTDIR=' /etc/sysconfig/named; then
|
2006-03-07 04:25:38 +00:00
|
|
|
/bin/sed -i -e 's#^ROOTDIR=.*$#ROOTDIR='${BIND_CHROOT_PREFIX}'#' /etc/sysconfig/named ;
|
|
|
|
else
|
|
|
|
echo 'ROOTDIR='${BIND_CHROOT_PREFIX} >> /etc/sysconfig/named;
|
2007-03-16 15:25:39 +00:00
|
|
|
fi;
|
2006-03-07 04:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function disable_bind_chroot()
|
|
|
|
{
|
2007-03-16 15:25:39 +00:00
|
|
|
/bin/sed -i -e '/^ROOTDIR=/d' /etc/sysconfig/named;
|
2006-03-07 04:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sync_files()
|
|
|
|
{
|
2007-03-16 15:25:39 +00:00
|
|
|
rootdir;
|
2006-03-07 04:25:38 +00:00
|
|
|
shopt -q nullglob;
|
|
|
|
ng=$?
|
|
|
|
shopt -s nullglob;
|
|
|
|
pfx=''
|
2006-03-30 00:52:34 +00:00
|
|
|
changed=`/bin/mktemp /tmp/XXXXXX`;
|
|
|
|
rm -f $changed
|
2007-02-15 15:46:08 +00:00
|
|
|
if [ $ENABLED -eq 0 ] ; then # chroot is enabled
|
2006-03-07 04:25:38 +00:00
|
|
|
/usr/bin/find /{etc/{named.*,rndc.*},${BIND_DIR#/}{/*,/data/*,/slaves/*}} -maxdepth 0 -type f |
|
|
|
|
while read f;
|
|
|
|
do
|
|
|
|
replace_with_link ${BIND_CHROOT_PREFIX}/$f $f;
|
2006-03-30 00:52:34 +00:00
|
|
|
[ ! -e $changed ] && touch $changed;
|
2006-03-07 04:25:38 +00:00
|
|
|
done;
|
2006-04-18 23:54:14 +00:00
|
|
|
/usr/bin/find /etc/{named.*,rndc.*}.rpmsave ${BIND_DIR}/*.rpmsave -maxdepth 0 -type l 2>/dev/null |
|
|
|
|
while read f;
|
|
|
|
do
|
|
|
|
/bin/rm -f $f >/dev/null 2>&1;
|
|
|
|
done
|
2006-03-07 04:25:38 +00:00
|
|
|
pfx=${BIND_CHROOT_PREFIX}
|
|
|
|
else # chroot is disabled
|
|
|
|
/usr/bin/find /var/named/chroot/{etc/{named.*,rndc.*},var/named{/*,/data/*,/slaves/*}} -maxdepth 0 |
|
|
|
|
while read f;
|
|
|
|
do
|
|
|
|
if [ ! -d "$f" ]; then
|
|
|
|
replace_with_file $f ${f#$BIND_CHROOT_PREFIX};
|
2006-03-30 00:52:34 +00:00
|
|
|
[ ! -e $changed ] && touch $changed;
|
2006-03-07 04:25:38 +00:00
|
|
|
fi;
|
|
|
|
done
|
|
|
|
fi;
|
2006-03-30 00:52:34 +00:00
|
|
|
if [ ! -e ${pfx}/etc/rndc.key ]; then
|
|
|
|
echo 'key "rndckey" {
|
|
|
|
algorithm hmac-md5;
|
|
|
|
secret "'`/usr/sbin/dns-keygen`'";
|
|
|
|
};' > /etc/rndc.key;
|
|
|
|
elif /bin/egrep -q '@KEY@' /etc/rndc.key; then
|
|
|
|
/bin/sed -i -e 's^@KEY@^'`/usr/sbin/dns-keygen`'^' /etc/rndc.key ;
|
2006-04-18 23:54:14 +00:00
|
|
|
fi
|
|
|
|
chown -h root:named /var/named/* >/dev/null 2>&1;
|
|
|
|
chown -h root:named ${BIND_CHROOT_PREFIX}/var/named/* >/dev/null 2>&1;
|
|
|
|
chown -h root:named /etc/{named,rndc}.* >/dev/null 2>&1;
|
|
|
|
chown -h root:named ${BIND_CHROOT_PREFIX}/etc/{named,rndc}.* >/dev/null 2>&1;
|
2006-03-07 04:25:38 +00:00
|
|
|
chmod 750 ${pfx}/var/named >/dev/null 2>&1;
|
|
|
|
chmod 640 ${pfx}/var/named/* >/dev/null 2>&1;
|
2006-04-03 16:45:30 +00:00
|
|
|
chmod 750 ${pfx}/var/named/*/. >/dev/null 2>&1;
|
2006-04-18 23:54:14 +00:00
|
|
|
chown -h named:named /var/named/{data{,/*},slaves{,*/}} >/dev/null 2>&1;
|
|
|
|
chown -h named:named ${BIND_CHROOT_PREFIX}/var/named/{data{,/*},slaves{,*/}} >/dev/null 2>&1;
|
2006-03-07 04:25:38 +00:00
|
|
|
chmod 770 ${pfx}/var/named/{data,slaves} >/dev/null 2>&1;
|
2006-04-03 16:45:30 +00:00
|
|
|
chmod 660 ${pfx}/var/named/{data/*,slaves/*} >/dev/null 2>&1;
|
|
|
|
chmod 770 ${pfx}/var/named/{data/*/.,slaves/*/.} >/dev/null 2>&1;
|
2006-03-30 00:52:34 +00:00
|
|
|
if [ -e $changed ]; then
|
2006-06-14 05:26:43 +00:00
|
|
|
if selinux_enabled && [ -x /sbin/restorecon ]; then
|
2006-04-18 23:54:14 +00:00
|
|
|
/sbin/restorecon -R ${BIND_CHROOT_PREFIX}/etc ${BIND_CHROOT_PREFIX}/var/named ${BIND_CHROOT_PREFIX}/var/run/named >/dev/null 2>&1;
|
|
|
|
if [ -e ${BIND_CHROOT_PREFIX}/etc/localtime ] && [ -e /etc/localtime ] ; then
|
2007-03-06 15:32:16 +00:00
|
|
|
/usr/bin/ ${BIND_CHROOT_PREFIX}/etc/localtime >/dev/null 2>&1;
|
2006-04-18 23:54:14 +00:00
|
|
|
fi;
|
2007-03-06 15:32:16 +00:00
|
|
|
/sbin/restorecon /etc/named.* >/dev/null 2>&1;
|
|
|
|
/sbin/restorecon /etc/rndc.key >/dev/null 2>&1;
|
|
|
|
/sbin/restorecon /etc/rndc.conf >/dev/null 2>&1;
|
|
|
|
/sbin/restorecon /var/named{/,/*} >/dev/null 2>&1;
|
|
|
|
/sbin/restorecon /var/named/{slaves,data}{/,/*} >/dev/null 2>&1;
|
2006-04-18 23:54:14 +00:00
|
|
|
/sbin/restorecon /var/named/named.ca ${BIND_CHROOT_PREFIX}/var/named/named.ca >/dev/null 2>&1;
|
2006-09-05 11:03:16 +00:00
|
|
|
/sbin/restorecon ${BIND_CHROOT_PREFIX} >/dev/null 2>&1;
|
2007-03-06 15:32:16 +00:00
|
|
|
/sbin/restorecon /var/named/named.ca >/dev/null 2>&1;
|
2006-03-30 00:52:34 +00:00
|
|
|
fi;
|
|
|
|
/sbin/service named condrestart
|
|
|
|
rm -f $changed;
|
|
|
|
fi;
|
|
|
|
if [ $ng -eq 1 ]; then
|
|
|
|
shopt -u nullglob;
|
|
|
|
fi;
|
2006-03-07 04:25:38 +00:00
|
|
|
}
|
|
|
|
|
2006-11-03 15:04:41 +00:00
|
|
|
function clean_root()
|
|
|
|
{
|
2007-02-15 15:46:08 +00:00
|
|
|
if [ $ENABLED -eq 0 ] ; then # chroot is disabled, clean it up
|
2006-11-03 15:04:41 +00:00
|
|
|
if [ -n "${BIND_CHROOT_PREFIX}" -a "x${BIND_CHROOT_PREFIX}" != "x/" ]; then
|
|
|
|
rm -r ${BIND_CHROOT_PREFIX}/dev >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/proc >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/etc >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/run/named >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/run/dbus >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/run >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/named/slaves >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/named/data >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/named >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var/tmp >/dev/null 2>&1 || :;
|
|
|
|
rmdir ${BIND_CHROOT_PREFIX}/var >/dev/null 2>&1 || :;
|
|
|
|
fi;
|
|
|
|
fi;
|
|
|
|
}
|
|
|
|
|
2006-03-07 04:25:38 +00:00
|
|
|
case $1 in
|
|
|
|
-e|--enable)
|
|
|
|
enable_bind_chroot;
|
|
|
|
sync_files;
|
|
|
|
exit $?;
|
|
|
|
;;
|
|
|
|
-d|--disable)
|
|
|
|
disable_bind_chroot;
|
|
|
|
sync_files;
|
2006-03-08 00:42:02 +00:00
|
|
|
/bin/umount ${BIND_CHROOT_PREFIX}/proc >/dev/null 2>&1 || :;
|
|
|
|
/bin/umount ${BIND_CHROOT_PREFIX}/var/run/dbus >/dev/null 2>&1 || :;
|
2006-11-03 15:04:41 +00:00
|
|
|
clean_root;
|
2006-03-07 04:25:38 +00:00
|
|
|
exit $?;
|
|
|
|
;;
|
|
|
|
-s|--sync)
|
|
|
|
sync_files;
|
|
|
|
exit $?;
|
|
|
|
;;
|
|
|
|
-q)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage;
|
|
|
|
exit 1;
|
2006-11-21 16:33:52 +00:00
|
|
|
esac
|