named revision 1.24
11.1Slukem#!/bin/sh
21.1Slukem#
31.24Sspz# $NetBSD: named,v 1.24 2014/07/08 07:04:07 spz Exp $
41.1Slukem#
51.1Slukem
61.1Slukem# PROVIDE: named
71.22Sperry# REQUIRE: NETWORKING mountcritremote syslogd
81.10Sthorpej# BEFORE:  DAEMON
91.11Slukem# KEYWORD: chrootdir
101.1Slukem
111.14Smycroft$_rc_subr_loaded . /etc/rc.subr
121.1Slukem
131.1Slukemname="named"
141.6Slukemrcvar=$name
151.1Slukemcommand="/usr/sbin/${name}"
161.19Schristospidfile="/var/run/${name}/${name}.pid"
171.7Slukemstart_precmd="named_precmd"
181.7Slukemextra_commands="reload"
191.7Slukemrequired_dirs="$named_chrootdir"	# if it is set, it must exist
201.7Slukem
211.17Schristosnamed_migrate()
221.17Schristos{
231.23Schristos	local src="$1"
241.23Schristos	local dst="$2$1"
251.17Schristos	echo "Migrating $src to $dst"
261.17Schristos(
271.17Schristos	diff=false
281.23Schristos	cd "$src"
291.23Schristos	mkdir -p "$dst"
301.17Schristos	for f in $(find . -type f)
311.17Schristos	do
321.23Schristos		f="${f##./}"
331.23Schristos		case "$f" in
341.17Schristos		*/*)
351.23Schristos			ds="$(dirname "$f")"
361.23Schristos			dd="$dst/$ds"
371.23Schristos			mkdir -p "$dd"
381.23Schristos			chmod "$(stat -f "%p" "$ds" |
391.23Schristos			    sed -e 's/.*\([0-7][0-7][0-7][0-7]\)$/\1/g')" "$dd"
401.23Schristos			chown "$(stat -f %u:%g "$ds")" "$dd"
411.17Schristos			;;
421.23Schristos		*)
431.17Schristos			;;
441.17Schristos		esac
451.18Schristos		if [ -r "$dst/$f" ]
461.17Schristos		then
471.23Schristos			if ! cmp "$f" "$dst/$f"; then
481.17Schristos				diff=true
491.17Schristos			fi
501.17Schristos		else
511.23Schristos			cp -p "$f" "$dst/$f"
521.17Schristos		fi
531.17Schristos	done
541.17Schristos	if $diff; then
551.17Schristos		echo "Cannot complete migration because files are different"
561.17Schristos		echo "Run 'diff -r $src $dst' resolve the differences"
571.17Schristos	else
581.23Schristos		rm -fr "$src"
591.23Schristos		ln -s "$dst" "$src"
601.17Schristos	fi
611.17Schristos)
621.17Schristos}
631.17Schristos
641.7Slukemnamed_precmd()
651.7Slukem{
661.24Sspz	if [ ! -e "/etc/rndc.key" ]; then
671.24Sspz		echo "Generating rndc.key"
681.24Sspz		/usr/sbin/rndc-confgen -a
691.24Sspz	fi
701.24Sspz
711.7Slukem	if [ -z "$named_chrootdir" ]; then
721.23Schristos		if [ ! -d "/etc/namedb/keys" ]; then
731.23Schristos			mkdir -m 775 "/etc/namedb/keys"
741.23Schristos			chown named:named "/etc/namedb/keys"
751.23Schristos		fi
761.7Slukem		return 0;
771.7Slukem	fi
781.7Slukem
791.7Slukem	# If running in a chroot cage, ensure that the appropriate files
801.7Slukem	# exist inside the cage, as well as helper symlinks into the cage 
811.7Slukem	# from outside.
821.7Slukem	#
831.7Slukem	# As this is called after the is_running and required_dir checks
841.7Slukem	# are made in run_rc_command(), we can safely assume ${named_chrootdir}
851.7Slukem	# exists and named isn't running at this point (unless forcestart
861.7Slukem	# is used).
871.7Slukem	#
881.12Schristos	case "$($command -v)" in
891.12Schristos	BIND*)	# 9 no group, named-xfer, or ndc
901.12Schristos		;;
911.12Schristos	named*)	# 4 and 8
921.12Schristos		rc_flags="-g named $rc_flags"
931.12Schristos		if [ ! -x "${named_chrootdir}/usr/libexec/named-xfer" -o \
941.12Schristos		    "${named_chrootdir}/usr/libexec/named-xfer" -ot \
951.12Schristos		    /usr/libexec/named-xfer ]; then
961.12Schristos			rm -f "${named_chrootdir}/usr/libexec/named-xfer"
971.12Schristos			cp -p /usr/libexec/named-xfer \
981.12Schristos			    "${named_chrootdir}/usr/libexec"
991.12Schristos		fi
1001.12Schristos		ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc
1011.12Schristos		;;
1021.12Schristos	esac
1031.12Schristos
1041.23Schristos	for i in null random; do
1051.13Schristos		if [ ! -c "${named_chrootdir}/dev/$i" ]; then
1061.13Schristos			rm -f "${named_chrootdir}/dev/$i"
1071.13Schristos			(cd /dev && 
1081.13Schristos			    /bin/pax -rw -pe "$i" "${named_chrootdir}/dev")
1091.13Schristos		fi
1101.13Schristos	done
1111.4Slukem
1121.17Schristos	if [ ! -h /etc/namedb ]; then
1131.17Schristos		named_migrate /etc/namedb ${named_chrootdir}
1141.16Schristos	fi
1151.23Schristos
1161.23Schristos	for i in named.conf rndc.key; do
1171.23Schristos		if [ \( -r "/etc/$i" \) -a \( ! -h "/etc/$i" \) -a \
1181.23Schristos		     \( ! -r "${named_chrootdir}/etc/$i" \) ]; then
1191.23Schristos			mv "/etc/$i" "${named_chrootdir}/etc/$i"
1201.23Schristos			ln -s "${named_chrootdir}/etc/$i" "/etc/$i"
1211.23Schristos		fi
1221.23Schristos	done
1231.23Schristos
1241.16Schristos	if [ \( ! -r ${named_chrootdir}/etc/named.conf \) -a \
1251.16Schristos	    \( -r ${named_chrootdir}/etc/namedb/named.conf \) ]; then
1261.16Schristos		ln -s namedb/named.conf ${named_chrootdir}/etc
1271.16Schristos	fi
1281.16Schristos
1291.7Slukem	if [ -f /etc/localtime ]; then
1301.7Slukem		cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || \
1311.7Slukem		    cp -p /etc/localtime "${named_chrootdir}/etc/localtime"
1321.7Slukem	fi
1331.21Schristos
1341.21Schristos	local piddir="$(dirname "${pidfile}")"
1351.21Schristos	mkdir -p "${named_chrootdir}${piddir}" "${piddir}"
1361.21Schristos	chmod 755 "${named_chrootdir}${piddir}" "${piddir}"
1371.21Schristos	chown named:named "${named_chrootdir}${piddir}" "${piddir}"
1381.21Schristos	ln -fs "${named_chrootdir}${pidfile}" "${pidfile}"
1391.7Slukem
1401.7Slukem	#	Change run_rc_commands()'s internal copy of $named_flags
1411.7Slukem	#
1421.12Schristos	rc_flags="-u named -t ${named_chrootdir} $rc_flags"
1431.7Slukem}
1441.1Slukem
1451.5Slukemload_rc_config $name
1461.3Slukemrun_rc_command "$1"
147