1 #!/bin/sh 2 # 3 # $NetBSD: named,v 1.30 2026/05/09 20:07:10 christos 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 diff=false 27 cd "$src" 28 mkdir -p "$dst" 29 for f in $(find . -type f) 30 do 31 f="${f##./}" 32 case "$f" in 33 */*) 34 ds="$(dirname "$f")" 35 dd="$dst/$ds" 36 mkdir -p "$dd" 37 chmod "$(stat -f "%p" "$ds" | 38 sed -e 's/.*\([0-7][0-7][0-7][0-7]\)$/\1/g')" "$dd" 39 chown "$(stat -f %u:%g "$ds")" "$dd" 40 ;; 41 *) 42 ;; 43 esac 44 if [ -r "$dst/$f" ] 45 then 46 if ! cmp "$f" "$dst/$f"; then 47 diff=true 48 fi 49 else 50 cp -p "$f" "$dst/$f" 51 fi 52 done 53 if $diff; then 54 echo "Cannot complete migration because files are different" 55 echo "Run 'diff -r $src $dst' resolve the differences" 56 else 57 rm -fr "$src" 58 ln -s "$dst" "$src" 59 fi 60 ) 61 62 copy_if_newer() 63 { 64 local chrootdir="$1" 65 local dir="$2" 66 local file="$3" 67 if ! [ -x "${chrootdir}${dir}/${file}" ] || 68 [ "${chrootdir}${dir}/${file}" -ot "${dir}/${file}" ]; then 69 rm -f "${chrootdir}${dir}/${file}" 70 cp -p "${dir}/${file}" "${chrootdir}${dir}/${file}" 71 fi 72 } 73 74 named_precmd() 75 { 76 if ! [ -e "/etc/rndc.key" ]; then 77 echo "Generating rndc.key" 78 /usr/sbin/rndc-confgen -a 79 fi 80 81 if ! [ -d "/etc/namedb/keys" ]; then 82 mkdir -m 775 "/etc/namedb/keys" 83 chown named:named "/etc/namedb/keys" 84 fi 85 86 if [ -z "$named_chrootdir" ]; then 87 return 0; 88 fi 89 90 # If running in a chroot cage, ensure that the appropriate files 91 # exist inside the cage, as well as helper symlinks into the cage 92 # from outside. 93 # 94 # As this is called after the is_running and required_dir checks 95 # are made in run_rc_command(), we can safely assume ${named_chrootdir} 96 # exists and named isn't running at this point (unless forcestart 97 # is used). 98 # 99 case "$($command -v)" in 100 BIND*) # 9 no group, named-xfer, or ndc 101 named_plugindir="/usr/lib/named" 102 if ! [ -d "${named_chrootdir}${named_plugindir}" ]; then 103 mkdir -p -m 755 "${named_chrootdir}${named_plugindir}" 104 chown root:wheel "${named_chrootdir}${named_plugindir}" 105 fi 106 for p in filter-aaaa.so.0; do 107 copy_if_newer "${named_chrootdir}" \ 108 "${named_plugindir}" "$p" 109 done 110 ;; 111 named*) # 4 and 8 112 rc_flags="-g named $rc_flags" 113 copy_if_newer "${named_chrootdir}" "/usr/libexec" "named-xfer" 114 ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc 115 ;; 116 esac 117 118 for i in null random urandom; do 119 if ! [ -c "${named_chrootdir}/dev/$i" ]; then 120 rm -f "${named_chrootdir}/dev/$i" 121 ( cd /dev && 122 /bin/pax -rw -pe "$i" "${named_chrootdir}/dev" ) 123 fi 124 done 125 126 if ! [ -h /etc/namedb ]; then 127 named_migrate /etc/namedb "${named_chrootdir}" 128 fi 129 130 for i in named.conf rndc.key; do 131 if [ -r "/etc/$i" ] && ! [ -h "/etc/$i" ] && 132 ! [ -r "${named_chrootdir}/etc/$i" ] 133 then 134 mv "/etc/$i" "${named_chrootdir}/etc/$i" 135 ln -s "${named_chrootdir}/etc/$i" "/etc/$i" 136 fi 137 done 138 139 if ! [ -r ${named_chrootdir}/etc/named.conf ] && 140 [ -r ${named_chrootdir}/etc/namedb/named.conf ] 141 then 142 ln -s namedb/named.conf "${named_chrootdir}/etc" 143 fi 144 145 if [ -f /etc/localtime ]; then 146 cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || 147 cp -p /etc/localtime "${named_chrootdir}/etc/localtime" 148 fi 149 150 local piddir="$(dirname "${pidfile}")" 151 mkdir -p "${named_chrootdir}${piddir}" "${piddir}" 152 chmod 755 "${named_chrootdir}${piddir}" "${piddir}" 153 chown named:named "${named_chrootdir}${piddir}" "${piddir}" 154 ln -fs "${named_chrootdir}${pidfile}" "${pidfile}" 155 156 # Change run_rc_commands()'s internal copy of $named_flags 157 # 158 rc_flags="-u named -t ${named_chrootdir} $rc_flags" 159 } 160 161 load_rc_config $name 162 run_rc_command "$1" 163