#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: named
# REQUIRE: SERVERS FILESYSTEMS
# KEYWORD: shutdown

. /etc/rc.subr

name="named"
rcvar=named_enable

extra_commands="reload"

start_precmd="named_prestart"
start_postcmd="named_poststart"
reload_cmd="named_reload"
stop_cmd="named_stop"

named_enable=${named_enable:-"NO"}		# Run named, the DNS server (or NO).
named_program=${named_program:-"/usr/local/sbin/named"}	# Path to named, if you want a different one.
named_conf=${named_conf:-"/usr/local/etc/namedb/named.conf"}	# Path to the configuration file
named_flags=${named_flags:-""}			# Use this for flags OTHER than -u and -c
named_uid=${named_uid:-"bind"}			# User to run named as
named_wait=${named_wait:-"NO"}			# Wait for working name service before exiting
named_wait_host=${named_wait_host:-"localhost"}	# Hostname to check if named_wait is enabled
named_auto_forward=${named_auto_forward:-"NO"}	# Set up forwarders from /etc/resolv.conf
named_auto_forward_only=${named_auto_forward_only:-"NO"}	# Do "forward only" instead of "forward first"

named_poststart() {
	if checkyesno named_wait; then
		until ${command%/sbin/named}/bin/host $named_wait_host >/dev/null 2>&1; do
			echo "	Waiting for nameserver to resolve $named_wait_host"
			sleep 1
		done
	fi
}

named_reload()
{
	${command%/named}/rndc reload
}

find_pidfile()
{
	if get_pidfile_from_conf pid-file $named_conf; then
		pidfile="$_pidfile_from_conf"
	else
		pidfile="/var/run/named/pid"
	fi
}

named_stop()
{
	find_pidfile

	# This duplicates an undesirably large amount of code from the stop
	# routine in rc.subr in order to use rndc to shut down the process,
	# and to give it a second chance in case rndc fails.
	rc_pid=$(check_pidfile $pidfile $command)
	if [ -z "$rc_pid" ]; then
		[ -n "$rc_fast" ] && return 0
		_run_rc_notrunning
		return 1
	fi
	echo 'Stopping named.'
	if ${command%/named}/rndc stop 2>/dev/null; then
		wait_for_pids $rc_pid
	else
		echo -n 'rndc failed, trying kill: '
		kill -TERM $rc_pid
		wait_for_pids $rc_pid
  	fi
}

create_file() {
	if [ -e "$1" ]; then
		unlink $1
	fi
	> $1
	chown root:wheel $1
	chmod 644 $1
}

named_prestart()
{
	find_pidfile

	if [ -n "$named_pidfile" ]; then
		warn 'named_pidfile: now determined from the conf file'
	fi

	echo ${pidfile%/pid}
	if [ ! -d ${pidfile%/pid} ]; then
		mkdir -p ${pidfile%/pid}
		chown ${named_uid}:${named_uid} ${pidfile%/pid}
	fi

	command_args="-u ${named_uid:=root} -c $named_conf $command_args"

	local line nsip firstns

	# Create an rndc.key file for the user if none exists
	#
	confgen_command="${command%/named}/rndc-confgen -a -b256 -u $named_uid \
	    -c ${named_confdir}/rndc.key"
	if [ -s "${named_confdir}/rndc.conf" ]; then
		unset confgen_command
	fi
	if [ -s "${named_confdir}/rndc.key" ]; then
		case `stat -f%Su ${named_confdir}/rndc.key` in
		root|$named_uid) ;;
		*) $confgen_command ;;
		esac
	else
		$confgen_command
	fi

	local checkconf

	checkconf="${command%/named}/named-checkconf"

	# Create a forwarder configuration based on /etc/resolv.conf
	if checkyesno named_auto_forward; then
		if [ ! -s /etc/resolv.conf ]; then
			warn "named_auto_forward enabled, but no /etc/resolv.conf"

			# Empty the file in case it is included in named.conf
			[ -s "${named_confdir}/auto_forward.conf" ] &&
			    create_file ${named_confdir}/auto_forward.conf

			$checkconf $named_conf ||
			    err 3 'named-checkconf for $named_conf failed'
			return
		fi

		create_file /var/run/naf-resolv.conf
		create_file /var/run/auto_forward.conf

		echo '	forwarders {' > /var/run/auto_forward.conf

		while read line; do
			case "$line" in
			'nameserver '*|'nameserver	'*)
				nsip=${line##nameserver[         ]}

				if [ -z "$firstns" ]; then
					if [ ! "$nsip" = '127.0.0.1' ]; then
						echo 'nameserver 127.0.0.1'
						echo "		${nsip};" >> /var/run/auto_forward.conf
					fi

					firstns=1
				else
					[ "$nsip" = '127.0.0.1' ] && continue
					echo "		${nsip};" >> /var/run/auto_forward.conf
				fi
				;;
			esac

			echo $line
		done < /etc/resolv.conf > /var/run/naf-resolv.conf

		echo '	};' >> /var/run/auto_forward.conf
		echo '' >> /var/run/auto_forward.conf
		if checkyesno named_auto_forward_only; then
			echo "	forward only;" >> /var/run/auto_forward.conf
		else
			echo "	forward first;" >> /var/run/auto_forward.conf
		fi

		if cmp -s /etc/resolv.conf /var/run/naf-resolv.conf; then
			unlink /var/run/naf-resolv.conf
		else
			[ -e /etc/resolv.conf ] && unlink /etc/resolv.conf
			mv /var/run/naf-resolv.conf /etc/resolv.conf
		fi

		if cmp -s ${named_confdir}/auto_forward.conf \
		    /var/run/auto_forward.conf; then
			unlink /var/run/auto_forward.conf
		else
			[ -e "${named_confdir}/auto_forward.conf" ] &&
			    unlink ${named_confdir}/auto_forward.conf
			mv /var/run/auto_forward.conf \
			    ${named_confdir}/auto_forward.conf
		fi
	else
		# Empty the file in case it is included in named.conf
		[ -s "${named_confdir}/auto_forward.conf" ] &&
		    create_file ${named_confdir}/auto_forward.conf
	fi

	$checkconf $named_conf || err 3 'named-checkconf for $named_conf failed'
}

load_rc_config $name

named_confdir="${named_conf%/*}"

run_rc_command "$1"
