1#!/bin/sh
2#
3# $NetBSD: named,v 1.22.8.1 2012/10/09 22:58:35 riz Exp $
4#
5
6# PROVIDE: named
7# REQUIRE: NETWORKING mountcritremote syslogd
8# BEFORE:  DAEMON
9# KEYWORD: chrootdir
10
11$_rc_subr_loaded . /etc/rc.subr
12
13name="named"
14rcvar=$name
15command="/usr/sbin/${name}"
16pidfile="/var/run/${name}/${name}.pid"
17start_precmd="named_precmd"
18extra_commands="reload"
19required_dirs="$named_chrootdir"	# if it is set, it must exist
20
21named_migrate()
22{
23	local src="$1"
24	local dst="$2$1"
25	echo "Migrating $src to $dst"
26(
27	diff=false
28	cd "$src"
29	mkdir -p "$dst"
30	for f in $(find . -type f)
31	do
32		f="${f##./}"
33		case "$f" in
34		*/*)
35			ds="$(dirname "$f")"
36			dd="$dst/$ds"
37			mkdir -p "$dd"
38			chmod "$(stat -f "%p" "$ds" |
39			    sed -e 's/.*\([0-7][0-7][0-7][0-7]\)$/\1/g')" "$dd"
40			chown "$(stat -f %u:%g "$ds")" "$dd"
41			;;
42		*)
43			;;
44		esac
45		if [ -r "$dst/$f" ]
46		then
47			if ! cmp "$f" "$dst/$f"; then
48				diff=true
49			fi
50		else
51			cp -p "$f" "$dst/$f"
52		fi
53	done
54	if $diff; then
55		echo "Cannot complete migration because files are different"
56		echo "Run 'diff -r $src $dst' resolve the differences"
57	else
58		rm -fr "$src"
59		ln -s "$dst" "$src"
60	fi
61)
62}
63
64named_precmd()
65{
66	if [ -z "$named_chrootdir" ]; then
67		if [ ! -d "/etc/namedb/keys" ]; then
68			mkdir -m 775 "/etc/namedb/keys"
69			chown named:named "/etc/namedb/keys"
70		fi
71		return 0;
72	fi
73
74	# If running in a chroot cage, ensure that the appropriate files
75	# exist inside the cage, as well as helper symlinks into the cage 
76	# from outside.
77	#
78	# As this is called after the is_running and required_dir checks
79	# are made in run_rc_command(), we can safely assume ${named_chrootdir}
80	# exists and named isn't running at this point (unless forcestart
81	# is used).
82	#
83	case "$($command -v)" in
84	BIND*)	# 9 no group, named-xfer, or ndc
85		;;
86	named*)	# 4 and 8
87		rc_flags="-g named $rc_flags"
88		if [ ! -x "${named_chrootdir}/usr/libexec/named-xfer" -o \
89		    "${named_chrootdir}/usr/libexec/named-xfer" -ot \
90		    /usr/libexec/named-xfer ]; then
91			rm -f "${named_chrootdir}/usr/libexec/named-xfer"
92			cp -p /usr/libexec/named-xfer \
93			    "${named_chrootdir}/usr/libexec"
94		fi
95		ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc
96		;;
97	esac
98
99	for i in null random; do
100		if [ ! -c "${named_chrootdir}/dev/$i" ]; then
101			rm -f "${named_chrootdir}/dev/$i"
102			(cd /dev && 
103			    /bin/pax -rw -pe "$i" "${named_chrootdir}/dev")
104		fi
105	done
106
107	if [ ! -h /etc/namedb ]; then
108		named_migrate /etc/namedb ${named_chrootdir}
109	fi
110
111	for i in named.conf rndc.key; do
112		if [ \( -r "/etc/$i" \) -a \( ! -h "/etc/$i" \) -a \
113		     \( ! -r "${named_chrootdir}/etc/$i" \) ]; then
114			mv "/etc/$i" "${named_chrootdir}/etc/$i"
115			ln -s "${named_chrootdir}/etc/$i" "/etc/$i"
116		fi
117	done
118
119	if [ \( ! -r ${named_chrootdir}/etc/named.conf \) -a \
120	    \( -r ${named_chrootdir}/etc/namedb/named.conf \) ]; then
121		ln -s namedb/named.conf ${named_chrootdir}/etc
122	fi
123
124	if [ -f /etc/localtime ]; then
125		cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || \
126		    cp -p /etc/localtime "${named_chrootdir}/etc/localtime"
127	fi
128
129	local piddir="$(dirname "${pidfile}")"
130	mkdir -p "${named_chrootdir}${piddir}" "${piddir}"
131	chmod 755 "${named_chrootdir}${piddir}" "${piddir}"
132	chown named:named "${named_chrootdir}${piddir}" "${piddir}"
133	ln -fs "${named_chrootdir}${pidfile}" "${pidfile}"
134
135	#	Change run_rc_commands()'s internal copy of $named_flags
136	#
137	rc_flags="-u named -t ${named_chrootdir} $rc_flags"
138}
139
140load_rc_config $name
141run_rc_command "$1"
142