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