Home | History | Annotate | Line # | Download | only in rc.d
named revision 1.18.32.1
      1        1.1     lukem #!/bin/sh
      2        1.1     lukem #
      3  1.18.32.1       riz # $NetBSD: named,v 1.18.32.1 2011/01/10 00:44:53 riz Exp $
      4        1.1     lukem #
      5        1.1     lukem 
      6        1.1     lukem # PROVIDE: named
      7        1.2     lukem # REQUIRE: SERVERS
      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.18.32.1       riz 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.17  christos 	local src=$1
     24       1.17  christos 	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.17  christos 	cd $src
     29       1.17  christos 	for f in $(find . -type f)
     30       1.17  christos 	do
     31       1.17  christos 		f=${f##./}
     32       1.17  christos 		case $f in
     33       1.17  christos 		*/*)
     34       1.17  christos 			d=$dst$(dirname $f)
     35       1.17  christos 			;;
     36       1.17  christos 		*)	d=$dst
     37       1.17  christos 			;;
     38       1.17  christos 		esac
     39       1.17  christos 		mkdir -p $d
     40       1.18  christos 		if [ -r "$dst/$f" ]
     41       1.17  christos 		then
     42       1.18  christos 			if ! cmp $f $dst/$f; then
     43       1.17  christos 				diff=true
     44       1.17  christos 			fi
     45       1.17  christos 		else
     46       1.18  christos 			cp -p $f $dst/$f
     47       1.17  christos 		fi
     48       1.17  christos 	done
     49       1.17  christos 	if $diff; then
     50       1.17  christos 		echo "Cannot complete migration because files are different"
     51       1.17  christos 		echo "Run 'diff -r $src $dst' resolve the differences"
     52       1.17  christos 	else
     53       1.17  christos 		rm -fr $src
     54       1.17  christos 		ln -s $dst $src
     55       1.17  christos 	fi
     56       1.17  christos )
     57       1.17  christos }
     58       1.17  christos 
     59        1.7     lukem named_precmd()
     60        1.7     lukem {
     61        1.7     lukem 	if [ -z "$named_chrootdir" ]; then
     62        1.7     lukem 		return 0;
     63        1.7     lukem 	fi
     64        1.7     lukem 
     65        1.7     lukem 	# If running in a chroot cage, ensure that the appropriate files
     66        1.7     lukem 	# exist inside the cage, as well as helper symlinks into the cage 
     67        1.7     lukem 	# from outside.
     68        1.7     lukem 	#
     69        1.7     lukem 	# As this is called after the is_running and required_dir checks
     70        1.7     lukem 	# are made in run_rc_command(), we can safely assume ${named_chrootdir}
     71        1.7     lukem 	# exists and named isn't running at this point (unless forcestart
     72        1.7     lukem 	# is used).
     73        1.7     lukem 	#
     74       1.12  christos 	case "$($command -v)" in
     75       1.12  christos 	BIND*)	# 9 no group, named-xfer, or ndc
     76       1.12  christos 		;;
     77       1.12  christos 	named*)	# 4 and 8
     78       1.12  christos 		rc_flags="-g named $rc_flags"
     79       1.12  christos 		if [ ! -x "${named_chrootdir}/usr/libexec/named-xfer" -o \
     80       1.12  christos 		    "${named_chrootdir}/usr/libexec/named-xfer" -ot \
     81       1.12  christos 		    /usr/libexec/named-xfer ]; then
     82       1.12  christos 			rm -f "${named_chrootdir}/usr/libexec/named-xfer"
     83       1.12  christos 			cp -p /usr/libexec/named-xfer \
     84       1.12  christos 			    "${named_chrootdir}/usr/libexec"
     85       1.12  christos 		fi
     86       1.12  christos 		ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc
     87       1.12  christos 		;;
     88       1.12  christos 	esac
     89       1.12  christos 
     90       1.13  christos 	for i in null random
     91       1.13  christos 	do
     92       1.13  christos 		if [ ! -c "${named_chrootdir}/dev/$i" ]; then
     93       1.13  christos 			rm -f "${named_chrootdir}/dev/$i"
     94       1.13  christos 			(cd /dev && 
     95       1.13  christos 			    /bin/pax -rw -pe "$i" "${named_chrootdir}/dev")
     96       1.13  christos 		fi
     97       1.13  christos 	done
     98        1.4     lukem 
     99       1.17  christos 	if [ ! -h /etc/namedb ]; then
    100       1.17  christos 		named_migrate /etc/namedb ${named_chrootdir}
    101       1.16  christos 	fi
    102       1.18  christos 	if [ \( -r /etc/named.conf \) -a \( ! -h /etc/named.conf \) -a \
    103       1.18  christos 	     \( ! -r ${named_chrootdir}/etc/named.conf \) ]
    104       1.18  christos 	then
    105       1.18  christos 		mv /etc/named.conf ${named_chrootdir}/etc/named.conf
    106       1.18  christos 		ln -s ${named_chrootdir}/etc/named.conf /etc/named.conf
    107       1.18  christos 	fi
    108       1.16  christos 	if [ \( ! -r ${named_chrootdir}/etc/named.conf \) -a \
    109       1.16  christos 	    \( -r ${named_chrootdir}/etc/namedb/named.conf \) ]; then
    110       1.16  christos 		ln -s namedb/named.conf ${named_chrootdir}/etc
    111       1.16  christos 	fi
    112       1.16  christos 
    113        1.7     lukem 	if [ -f /etc/localtime ]; then
    114        1.7     lukem 		cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || \
    115        1.7     lukem 		    cp -p /etc/localtime "${named_chrootdir}/etc/localtime"
    116        1.7     lukem 	fi
    117  1.18.32.1       riz 
    118  1.18.32.1       riz 	local piddir="$(dirname "${pidfile}")"
    119  1.18.32.1       riz 	mkdir -p "${named_chrootdir}${piddir}" "${piddir}"
    120  1.18.32.1       riz 	chmod 755 "${named_chrootdir}${piddir}" "${piddir}"
    121  1.18.32.1       riz 	chown named:named "${named_chrootdir}${piddir}" "${piddir}"
    122  1.18.32.1       riz 	ln -fs "${named_chrootdir}${pidfile}" "${pidfile}"
    123        1.7     lukem 
    124        1.7     lukem 	#	Change run_rc_commands()'s internal copy of $named_flags
    125        1.7     lukem 	#
    126       1.12  christos 	rc_flags="-u named -t ${named_chrootdir} $rc_flags"
    127        1.7     lukem }
    128        1.1     lukem 
    129        1.5     lukem load_rc_config $name
    130        1.3     lukem run_rc_command "$1"
    131