Home | History | Annotate | Line # | Download | only in rc.d
named revision 1.23.8.1
      1       1.1     lukem #!/bin/sh
      2       1.1     lukem #
      3  1.23.8.1       tls # $NetBSD: named,v 1.23.8.1 2014/08/10 06:49:24 tls 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.23  christos 	local src="$1"
     24      1.23  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.23  christos 	cd "$src"
     29      1.23  christos 	mkdir -p "$dst"
     30      1.17  christos 	for f in $(find . -type f)
     31      1.17  christos 	do
     32      1.23  christos 		f="${f##./}"
     33      1.23  christos 		case "$f" in
     34      1.17  christos 		*/*)
     35      1.23  christos 			ds="$(dirname "$f")"
     36      1.23  christos 			dd="$dst/$ds"
     37      1.23  christos 			mkdir -p "$dd"
     38      1.23  christos 			chmod "$(stat -f "%p" "$ds" |
     39      1.23  christos 			    sed -e 's/.*\([0-7][0-7][0-7][0-7]\)$/\1/g')" "$dd"
     40      1.23  christos 			chown "$(stat -f %u:%g "$ds")" "$dd"
     41      1.17  christos 			;;
     42      1.23  christos 		*)
     43      1.17  christos 			;;
     44      1.17  christos 		esac
     45      1.18  christos 		if [ -r "$dst/$f" ]
     46      1.17  christos 		then
     47      1.23  christos 			if ! cmp "$f" "$dst/$f"; then
     48      1.17  christos 				diff=true
     49      1.17  christos 			fi
     50      1.17  christos 		else
     51      1.23  christos 			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.23  christos 		rm -fr "$src"
     59      1.23  christos 		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.23.8.1       tls 	if [ ! -e "/etc/rndc.key" ]; then
     67  1.23.8.1       tls 		echo "Generating rndc.key"
     68  1.23.8.1       tls 		/usr/sbin/rndc-confgen -a
     69  1.23.8.1       tls 	fi
     70  1.23.8.1       tls 
     71       1.7     lukem 	if [ -z "$named_chrootdir" ]; then
     72      1.23  christos 		if [ ! -d "/etc/namedb/keys" ]; then
     73      1.23  christos 			mkdir -m 775 "/etc/namedb/keys"
     74      1.23  christos 			chown named:named "/etc/namedb/keys"
     75      1.23  christos 		fi
     76       1.7     lukem 		return 0;
     77       1.7     lukem 	fi
     78       1.7     lukem 
     79       1.7     lukem 	# If running in a chroot cage, ensure that the appropriate files
     80       1.7     lukem 	# exist inside the cage, as well as helper symlinks into the cage 
     81       1.7     lukem 	# from outside.
     82       1.7     lukem 	#
     83       1.7     lukem 	# As this is called after the is_running and required_dir checks
     84       1.7     lukem 	# are made in run_rc_command(), we can safely assume ${named_chrootdir}
     85       1.7     lukem 	# exists and named isn't running at this point (unless forcestart
     86       1.7     lukem 	# is used).
     87       1.7     lukem 	#
     88      1.12  christos 	case "$($command -v)" in
     89      1.12  christos 	BIND*)	# 9 no group, named-xfer, or ndc
     90      1.12  christos 		;;
     91      1.12  christos 	named*)	# 4 and 8
     92      1.12  christos 		rc_flags="-g named $rc_flags"
     93      1.12  christos 		if [ ! -x "${named_chrootdir}/usr/libexec/named-xfer" -o \
     94      1.12  christos 		    "${named_chrootdir}/usr/libexec/named-xfer" -ot \
     95      1.12  christos 		    /usr/libexec/named-xfer ]; then
     96      1.12  christos 			rm -f "${named_chrootdir}/usr/libexec/named-xfer"
     97      1.12  christos 			cp -p /usr/libexec/named-xfer \
     98      1.12  christos 			    "${named_chrootdir}/usr/libexec"
     99      1.12  christos 		fi
    100      1.12  christos 		ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc
    101      1.12  christos 		;;
    102      1.12  christos 	esac
    103      1.12  christos 
    104  1.23.8.1       tls 	for i in null random urandom; do
    105      1.13  christos 		if [ ! -c "${named_chrootdir}/dev/$i" ]; then
    106      1.13  christos 			rm -f "${named_chrootdir}/dev/$i"
    107      1.13  christos 			(cd /dev && 
    108      1.13  christos 			    /bin/pax -rw -pe "$i" "${named_chrootdir}/dev")
    109      1.13  christos 		fi
    110      1.13  christos 	done
    111       1.4     lukem 
    112      1.17  christos 	if [ ! -h /etc/namedb ]; then
    113      1.17  christos 		named_migrate /etc/namedb ${named_chrootdir}
    114      1.16  christos 	fi
    115      1.23  christos 
    116      1.23  christos 	for i in named.conf rndc.key; do
    117      1.23  christos 		if [ \( -r "/etc/$i" \) -a \( ! -h "/etc/$i" \) -a \
    118      1.23  christos 		     \( ! -r "${named_chrootdir}/etc/$i" \) ]; then
    119      1.23  christos 			mv "/etc/$i" "${named_chrootdir}/etc/$i"
    120      1.23  christos 			ln -s "${named_chrootdir}/etc/$i" "/etc/$i"
    121      1.23  christos 		fi
    122      1.23  christos 	done
    123      1.23  christos 
    124      1.16  christos 	if [ \( ! -r ${named_chrootdir}/etc/named.conf \) -a \
    125      1.16  christos 	    \( -r ${named_chrootdir}/etc/namedb/named.conf \) ]; then
    126      1.16  christos 		ln -s namedb/named.conf ${named_chrootdir}/etc
    127      1.16  christos 	fi
    128      1.16  christos 
    129       1.7     lukem 	if [ -f /etc/localtime ]; then
    130       1.7     lukem 		cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || \
    131       1.7     lukem 		    cp -p /etc/localtime "${named_chrootdir}/etc/localtime"
    132       1.7     lukem 	fi
    133      1.21  christos 
    134      1.21  christos 	local piddir="$(dirname "${pidfile}")"
    135      1.21  christos 	mkdir -p "${named_chrootdir}${piddir}" "${piddir}"
    136      1.21  christos 	chmod 755 "${named_chrootdir}${piddir}" "${piddir}"
    137      1.21  christos 	chown named:named "${named_chrootdir}${piddir}" "${piddir}"
    138      1.21  christos 	ln -fs "${named_chrootdir}${pidfile}" "${pidfile}"
    139       1.7     lukem 
    140       1.7     lukem 	#	Change run_rc_commands()'s internal copy of $named_flags
    141       1.7     lukem 	#
    142      1.12  christos 	rc_flags="-u named -t ${named_chrootdir} $rc_flags"
    143       1.7     lukem }
    144       1.1     lukem 
    145       1.5     lukem load_rc_config $name
    146       1.3     lukem run_rc_command "$1"
    147