#!/bin/bash
#
# Scripts that creates the initial config for named.
#
RNDC_CONF=/replicated/etc/rndc.conf
NAMED_CONF=/replicated/jail/named/etc/named.conf
BIND_DIR=/replicated/jail/named/var/cache

# Check if the we already have config installed
if [ -s "${NAMED_CONF}" ]; then
    exit
fi


# Copy named.conf.
cp -f ./conf/named.conf ${NAMED_CONF}
chown named:named ${NAMED_CONF}
chmod 664 ${NAMED_CONF} 


# Copy bind directory.
mkdir -p ${BIND_DIR}
chmod 755 ${BIND_DIR}
cp -rf ./conf/bind ${BIND_DIR}/
chown -R named:named ${BIND_DIR}/bind


# Generate rndc key.
/usr/local/sbin/rndc-confgen -a -A hmac-sha512 -k rndc_key -c ${RNDC_CONF} >> /dev/null 2>&1
chown named:named ${RNDC_CONF}
chmod 660 ${RNDC_CONF}

# Copy the key to the arranged config.
cat ${RNDC_CONF} >> ${NAMED_CONF}

# Explicitly set rndc option defaults.
cat <<EOF >> ${RNDC_CONF}

options
{
	default-key rndc_key;
	default-server localhost;
};

EOF

# Explicitly set controls.
cat <<EOF >> ${NAMED_CONF}

controls
{
    inet * allow { localhost; } keys { rndc_key; };
};

EOF
