xref: /minix3/etc/rc.d/named (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 #!/bin/sh
2 #
3 # $NetBSD: named,v 1.25 2014/07/13 22:06:56 tls 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 
13 name="named"
14 rcvar=$name
15 command="/usr/sbin/${name}"
16 pidfile="/var/run/${name}/${name}.pid"
17 start_precmd="named_precmd"
18 extra_commands="reload"
19 required_dirs="$named_chrootdir"	# if it is set, it must exist
20 
21 named_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 
64 named_precmd()
65 {
66 	if [ ! -e "/etc/rndc.key" ]; then
67 		echo "Generating rndc.key"
68 		/usr/sbin/rndc-confgen -a
69 	fi
70 
71 	if [ -z "$named_chrootdir" ]; then
72 		if [ ! -d "/etc/namedb/keys" ]; then
73 			mkdir -m 775 "/etc/namedb/keys"
74 			chown named:named "/etc/namedb/keys"
75 		fi
76 		return 0;
77 	fi
78 
79 	# If running in a chroot cage, ensure that the appropriate files
80 	# exist inside the cage, as well as helper symlinks into the cage
81 	# from outside.
82 	#
83 	# As this is called after the is_running and required_dir checks
84 	# are made in run_rc_command(), we can safely assume ${named_chrootdir}
85 	# exists and named isn't running at this point (unless forcestart
86 	# is used).
87 	#
88 	case "$($command -v)" in
89 	BIND*)	# 9 no group, named-xfer, or ndc
90 		;;
91 	named*)	# 4 and 8
92 		rc_flags="-g named $rc_flags"
93 		if [ ! -x "${named_chrootdir}/usr/libexec/named-xfer" -o \
94 		    "${named_chrootdir}/usr/libexec/named-xfer" -ot \
95 		    /usr/libexec/named-xfer ]; then
96 			rm -f "${named_chrootdir}/usr/libexec/named-xfer"
97 			cp -p /usr/libexec/named-xfer \
98 			    "${named_chrootdir}/usr/libexec"
99 		fi
100 		ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc
101 		;;
102 	esac
103 
104 	for i in null random urandom; do
105 		if [ ! -c "${named_chrootdir}/dev/$i" ]; then
106 			rm -f "${named_chrootdir}/dev/$i"
107 			(cd /dev &&
108 			    /bin/pax -rw -pe "$i" "${named_chrootdir}/dev")
109 		fi
110 	done
111 
112 	if [ ! -h /etc/namedb ]; then
113 		named_migrate /etc/namedb ${named_chrootdir}
114 	fi
115 
116 	for i in named.conf rndc.key; do
117 		if [ \( -r "/etc/$i" \) -a \( ! -h "/etc/$i" \) -a \
118 		     \( ! -r "${named_chrootdir}/etc/$i" \) ]; then
119 			mv "/etc/$i" "${named_chrootdir}/etc/$i"
120 			ln -s "${named_chrootdir}/etc/$i" "/etc/$i"
121 		fi
122 	done
123 
124 	if [ \( ! -r ${named_chrootdir}/etc/named.conf \) -a \
125 	    \( -r ${named_chrootdir}/etc/namedb/named.conf \) ]; then
126 		ln -s namedb/named.conf ${named_chrootdir}/etc
127 	fi
128 
129 	if [ -f /etc/localtime ]; then
130 		cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || \
131 		    cp -p /etc/localtime "${named_chrootdir}/etc/localtime"
132 	fi
133 
134 	local piddir="$(dirname "${pidfile}")"
135 	mkdir -p "${named_chrootdir}${piddir}" "${piddir}"
136 	chmod 755 "${named_chrootdir}${piddir}" "${piddir}"
137 	chown named:named "${named_chrootdir}${piddir}" "${piddir}"
138 	ln -fs "${named_chrootdir}${pidfile}" "${pidfile}"
139 
140 	#	Change run_rc_commands()'s internal copy of $named_flags
141 	#
142 	rc_flags="-u named -t ${named_chrootdir} $rc_flags"
143 }
144 
145 load_rc_config $name
146 run_rc_command "$1"
147