#!/bin/sh
# SBCI : Simple Bind Chroot Installer
# Author : Christophe Casalegno / Brain 0verride
# Website : http://www.christophe-casalegno.com
# Twitter : https://twitter.com/Brain0verride
# Email : brain@christophe-casalegno.com

# Vars

installer="apt-get" # installer (apt-get, yum, urpmi, zypper, etc.)
installer_options="-q -y -f install" # options for the installer
replacer="sed" # command used for replacement 
replacer_options="-i" # options for the replacer
syslogger="/etc/init.d/rsyslog" # syslogger start script
syslogger_options="restart" # start command option for syslogger
syslogserviceconf="/etc/rsyslog.d/bind-chroot.conf" # syslogger configuration file
namedlogconf="/etc/bind/named.conf.log" # logs configuration for bind
bind9service="/etc/systemd/system/bind9.service" #service file
sysctlservicename="bind9" #sysctl service name
chrootdir="/var/bind9/chroot" #the bind chroot directory
confdir="/etc/bind" #the bind conf directory
startsh="/etc/init.d/bind9" #the starting bind script
packages="bind9 dnsutils" #packages to be installed
originpidfile="PIDFILE=/var/run/named/named.pid" # origin pid file for bind
newpidfile="PIDFILE=$chrootdir/var/run/named/named.pid" # new pid file for chrooted bind

# Content for file or other stuff 

content_bind9service="
[Unit]
Description=BIND Domain Name Server
Documentation=man:named(8)
After=network.target

[Service]
ExecStart=/usr/sbin/named -f -u bind -t $chrootdir
ExecReload=/usr/sbin/rndc reload
ExecStop=/usr/sbin/rndc stop

[Install]
WantedBy=multi-user.target
"
content_namedlogconf='
logging {
        channel update_debug {
                file "/var/log/update_debug.log" versions 3 size 100k;
                severity debug;
                print-severity  yes;
                print-time      yes;
        };
        channel security_info {
                file "/var/log/security_info.log" versions 1 size 100k;
                severity info;
                print-severity  yes;
                print-time      yes;
        };
        channel bind_log {
                file "/var/log/bind.log" versions 3 size 1m;
                severity info;
                print-category  yes;
                print-severity  yes;
                print-time      yes;
        };

        category default { bind_log; };
        category lame-servers { null; };
        category update { update_debug; };
        category update-security { update_debug; };
        category security { security_info; };
};
'

# Be sure to have launched just one time the script

SCRIPTNAME=`basename $0`
export LOCKSYNC="/var/run/$SCRIPTNAME.lock"
PID=$$
echo "["`date +"%Y/%m/%d %H:%M:%S"`"] Running script $0"
if [ -f "$LOCKSYNC" ]
then
        echo "Fichier lock "$LOCKSYNC" still here, you have probably launched the script in another term"
        PIDLOCK=`cat $LOCKSYNC`
        if [ -f /proc/$PIDLOCK/exe ]
        then
                echo "The script is running, impossible to launch $SCRIPTNAME more than once time."
                exit
        else
                echo "The script doesn't running anymore, launching $SCRIPTNAME"
                rm $LOCKSYNC
        fi
fi
echo "Creating lock "$LOCKSYNC
echo $PID > $LOCKSYNC


# Packages installation
echo "Installing packages : $packages..."
$installer $installer_options $packages

echo "Servicefile configuration for systemctl..."
touch $bind9service

cat > $bind9service << EOF
$content_bind9service
EOF

systemctl reenable $sysctlservicename

# Creating the chroot
echo "Creating the chroot directory $chrootdir and subdirectories..."

mkdir -p $chrootdir
mkdir -p $chrootdir/etc
mkdir -p $chrootdir/dev
mkdir -p $chrootdir/var/log
mkdir -p $chrootdir/var/cache/bind
mkdir -p $chrootdir/var/run/named

mknod $chrootdir/dev/null c 1 3
mknod $chrootdir/dev/random c 1 8
chmod 660 $chrootdir/dev/null
chmod 600 $chrootdir /dev/random
mv $confdir $chrootdir/etc

ln -s $chrootdir$confdir $confdir 
cp /etc/localtime $chrootdir/etc/
chown bind:bind $chrootdir/$confdir/rndc.key

chmod 775 $chrootdir/var/cache
chmod 775 $chrootdir/var/cache/bind
chmod 775 $chrootdir/var/run/named
chmod 775 $chrootdir/var/log
chmod 705 $chrootdir

chgrp bind $chrootdir/var/cache
chgrp bind $chrootdir/etc/bind
chgrp bind $chrootdir/var/run/named
chgrp bind $chrootdir/var/log

cat > $namedlogconf << EOF
$content_namedlogconf
EOF

echo "include \"$namedlogconf\";" >> /etc/bind/named.conf

# Modify starting scripts
echo "starting script modifications..."
$replacer $replacer_options s#$originpidfile#$newpidfile# $startsh

echo "\$AddUnixListenSocket $chrootdir/dev/log" > $syslogserviceconf

# Start services
echo " starting services..."
$syslogger $syslogger_options && $startsh restart


