#!/bin/sh

# customized items of package
PACKAGE_NAME=$SYNOPKG_PKGNAME
DSM_VERSION_NUMBER=$SYNOPKG_DSM_VERSION_BUILD
DSM_VERSION_MAJOR=$SYNOPKG_DSM_VERSION_MAJOR
PKG_STATUS=$SYNOPKG_PKG_STATUS
SZF_TMP_DNS_LOG="/tmp/dnslog"
DSM_STATUS_DEMO=`/usr/syno/bin/synogetkeyvalue /etc.defaults/synoinfo.conf support_demomode`

# For Compatibility between DNS Server and DHCP Server(process named and dnsmasq conflict on port 53)
COUNT_DNSMASQ=`/bin/ps | /bin/grep -c dnsmasq`
SZD_DHCPD="/etc/dhcpd"
SZF_DHCP_DNSMSQ_CONF="${SZD_DHCPD}/dhcpd-dns-dns.conf"
SZF_DHCP_DNSMSQ_INFO="${SZD_DHCPD}/dhcpd-dns-dns.info"

# DNS Server
PRIVATE_LOCATION="/var/packages/DNSServer/target"
PKG_APP_PATH="${PRIVATE_LOCATION}/app"
PKG_INDEXDB_PATH="${PRIVATE_LOCATION}/indexdb"
DNS_PID=$PRIVATE_LOCATION"/named/var/run/named/named.pid"
DNS_LOG=$PRIVATE_LOCATION"/named/var/log/default-log"
SZF_DNS_RNDC_KEY="${PRIVATE_LOCATION}/named/rndc.key"

# Before bind9 start, we need check 1.interface lo is up (if not up, open lo) 2.
SZV_IFCONFIG_LO=`ifconfig | awk '{print $1}' | grep lo`

# general routines
DSM_INDEX_ADD="/usr/syno/bin/pkgindexer_add"
DSM_INDEX_DEL="/usr/syno/bin/pkgindexer_del"
PACKAGE_ENABLED="/var/packages/${PACKAGE_NAME}/enabled"

ChrootDir="/var/packages/DNSServer/target/named"
BinDir="/var/packages/DNSServer/target/bin"
RunAsUser="nobody"

ChangeOwner()
{
	chown -R nobody:nobody ${ChrootDir}/etc
	chown -R nobody:nobody ${ChrootDir}/var
	chown nobody:nobody ${ChrootDir}
	chmod 500 ${ChrootDir}/etc/key
}

InitDirectory()
{
	if [ ! -f "${ChrootDir}/var/run/named" ]; then
		mkdir -p ${ChrootDir}/var/run/named
	fi
	if [ ! -f "${ChrootDir}/var/log" ]; then
		mkdir -p ${ChrootDir}/var/log
	fi
	if [ ! -d "${ChrootDir}/etc/key" ]; then
		mkdir -p ${ChrootDir}/etc/key
	fi
	if [ ! -f "${ChrootDir}/etc/zone/master" ]; then
		mkdir -p ${ChrootDir}/etc/zone/master
	fi
	if [ ! -f "${ChrootDir}/etc/zone/slave" ]; then
		mkdir ${ChrootDir}/etc/zone/slave
	fi
	if [ ! -f "${ChrootDir}/etc/zone/data" ]; then
		mkdir ${ChrootDir}/etc/zone/data
	fi
	if [ ! -f "${ChrootDir}/dev" ]; then
		mkdir ${ChrootDir}/dev
	fi
	if [ ! -f "${ChrootDir}/tmp" ]; then
		mkdir ${ChrootDir}/tmp
	fi
}

initial()
{
	InitDirectory

	if [ ! -f "${ChrootDir}/etc/localtime" ]; then
		 cp /etc/localtime ${ChrootDir}/etc/
	fi

	if [ ! -c "${ChrootDir}/dev/null"  ]; then
		mknod  ${ChrootDir}/dev/null c 1 3
	fi

	if [ ! -c "${ChrootDir}/dev/random" ]; then
		mknod  ${ChrootDir}/dev/random c 1 8
	fi

	if [ ! -c "${ChrootDir}/dev/urandom" ]; then
		mknod  ${ChrootDir}/dev/urandom c 1 9
	fi

	if [ ! -c "${ChrootDir}/dev/zero" ]; then
		mknod  ${ChrootDir}/dev/zero c 1 5
	fi
}

DNS_START_DHCPCheck()
{
	if [ ! -d "${SZD_DHCPD}" ]; then
		mkdir ${SZD_DHCPD}
	fi
	# create a conf file and an info file to avoid port conflict between DHCP Server and DNS Server
	/bin/echo "dhcp-option=tag:dns,option:dns-server,127.0.0.1" > ${SZF_DHCP_DNSMSQ_CONF}
	/bin/echo "dhcp-option=tag:dns,option:router,127.0.0.1" >> ${SZF_DHCP_DNSMSQ_CONF}
	/bin/echo "port=0" >> ${SZF_DHCP_DNSMSQ_CONF}
	/bin/echo "interface=dns" >> ${SZF_DHCP_DNSMSQ_CONF}
	/bin/echo "dhcp-range=set:dns,127.0.0.1,127.0.0.1,255.255.255.0,86400" >> ${SZF_DHCP_DNSMSQ_CONF}
	/bin/echo "enable=\"yes\"" > ${SZF_DHCP_DNSMSQ_INFO}
	# restart dhcp if dnsmasq is active
	if [ 1 -lt ${COUNT_DNSMASQ} ]; then
		/etc/rc.network nat-restart-dhcp
	fi
}

DNS_STOP_DHCPCheck()
{
	# remove conf and info file
	/bin/rm ${SZF_DHCP_DNSMSQ_CONF}
	/bin/rm ${SZF_DHCP_DNSMSQ_INFO}
	# restart dhcp if dnsmasq is active
	if [ 1 -lt ${COUNT_DNSMASQ} ]; then
		/etc/rc.network nat-restart-dhcp
	fi
}

DNS_LoopBack_Interface_Check()
{
	# before start named, we need check if interface lo is up, otherwise rndc command will failed
	if [ "${SZV_IFCONFIG_LO}" != "lo" ]; then
		/bin/echo "Network interface lo is not enable, please check your network status" > ${SYNOPKG_TEMP_LOGFILE}
		exit 1
	fi
}

start()
{
	#kill named if pid exist
	if [ -f ${DNS_PID} ]; then
		kill -TERM `cat ${DNS_PID}`
	fi

	# create a key for rndc if key not exist
	if [ ! -f ${SZF_DNS_RNDC_KEY} ]; then
		${BinDir}/rndc-confgen -a -b 512 -r /dev/urandom -s 127.0.0.1 -c /var/packages/DNSServer/target/named/rndc.key -u root
	fi
	chown nobody:nobody ${SZF_DNS_RNDC_KEY}
	chmod 770 ${SZF_DNS_RNDC_KEY}

	# initial and check run env for bind9
	initial
	ChangeOwner
	case ${DSM_STATUS_DEMO} in
		[Yy][Ee][Ss])
			echo "allow-recursion {none;};" > ${PRIVATE_LOCATION}/named/etc/conf/named.options.resolution.conf
			echo "blackhole {any;};" >> ${PRIVATE_LOCATION}/named/etc/conf/named.options.resolution.conf
			# disable auto initial process
			/usr/syno/bin/synosetkeyvalue ${PRIVATE_LOCATION}/etc/synodns.conf need_initial no
			/usr/syno/bin/synosetkeyvalue ${PRIVATE_LOCATION}/etc/synodns.conf resolve_enable no
			${BinDir}/named -t ${ChrootDir} -u $RunAsUser
			;;
		*)
			# check env for named
			DNS_LoopBack_Interface_Check
			# start bind9 if not demo mode
			DNS_START_DHCPCheck
			# call named start DNS server
			${BinDir}/named -t ${ChrootDir} -u $RunAsUser
			if [ 0 -ne $? ]; then
				/bin/echo "DNS Server start failed" > ${SYNOPKG_TEMP_LOGFILE}
				DNS_STOP_DHCPCheck
				exit 1
			fi
			;;
	esac

	# index help and add help to dsm
	${DSM_INDEX_ADD} ${PKG_APP_PATH}/index.conf ${PKG_INDEXDB_PATH}/appindexdb
	${DSM_INDEX_ADD} ${PKG_APP_PATH}/helptoc.conf ${PKG_INDEXDB_PATH}/helpindexdb
}

stop()
{
	# remove index and help entry
	${DSM_INDEX_DEL} ${PKG_APP_PATH}/index.conf ${PKG_INDEXDB_PATH}/appindexdb
	${DSM_INDEX_DEL} ${PKG_APP_PATH}/helptoc.conf ${PKG_INDEXDB_PATH}/helpindexdb

	#stop bind9
	kill -TERM `cat ${DNS_PID}`

	# do DNS_STOP_DHCPCheck after bind9 stop
	DNS_STOP_DHCPCheck
}

case $1 in
	start)
		# start routine
		start
		exit 0
		;;
	stop)
		stop
		exit 0
		;;
	status)
		# don't check pid on demo mode
		case ${DSM_STATUS_DEMO} in
			[Yy][Ee][Ss])
				if [ -f ${PACKAGE_ENABLED} ]; then
					exit 0
				else
					exit 1
				fi
				;;
			*)
				COUNT_DNS=`ps | grep -c named`
				if [ -f ${DNS_PID} ] && [ 0 -lt ${COUNT_DNS} ]; then
					exit 0
				else
					exit 1
				fi
				;;
		esac
	;;
	killall)
		;;
	log)
		echo ${DNS_LOG}
		exit 0
		;;
esac

