1 #!/bin/sh 2 # 3 # $NetBSD: named,v 1.29 2024/07/10 07:27: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 [ -z "$named_chrootdir" ]; then 82 if ! [ -d "/etc/namedb/keys" ]; then 83 mkdir -m 775 "/etc/namedb/keys" 84 chown named:named "/etc/namedb/keys" 85 fi 86 return 0; 87 fi 88 89 # If running in a chroot cage, ensure that the appropriate files 90 # exist inside the cage, as well as helper symlinks into the cage 91 # from outside. 92 # 93 # As this is called after the is_running and required_dir checks 94 # are made in run_rc_command(), we can safely assume ${named_chrootdir} 95 # exists and named isn't running at this point (unless forcestart 96 # is used). 97 # 98 case "$($command -v)" in 99 BIND*) # 9 no group, named-xfer, or ndc 100 named_plugindir="/usr/lib/named" 101 if ! [ -d "${named_chrootdir}${named_plugindir}" ]; then 102 mkdir -p -m 755 "${named_chrootdir}${named_plugindir}" 103 chown root:wheel "${named_chrootdir}${named_plugindir}" 104 fi 105 for p in filter-aaaa.so.0; do 106 copy_if_newer "${named_chrootdir}" \ 107 "${named_plugindir}" "$p" 108 done 109 ;; 110 named*) # 4 and 8 111 rc_flags="-g named $rc_flags" 112 copy_if_newer "${named_chrootdir}" "/usr/libexec" "named-xfer" 113 ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc 114 ;; 115 esac 116 117 for i in null random urandom; do 118 if ! [ -c "${named_chrootdir}/dev/$i" ]; then 119 rm -f "${named_chrootdir}/dev/$i" 120 ( cd /dev && 121 /bin/pax -rw -pe "$i" "${named_chrootdir}/dev" ) 122 fi 123 done 124 125 if ! [ -h /etc/namedb ]; then 126 named_migrate /etc/namedb "${named_chrootdir}" 127 fi 128 129 for i in named.conf rndc.key; do 130 if [ -r "/etc/$i" ] && ! [ -h "/etc/$i" ] && 131 ! [ -r "${named_chrootdir}/etc/$i" ] 132 then 133 mv "/etc/$i" "${named_chrootdir}/etc/$i" 134 ln -s "${named_chrootdir}/etc/$i" "/etc/$i" 135 fi 136 done 137 138 if ! [ -r ${named_chrootdir}/etc/named.conf ] && 139 [ -r ${named_chrootdir}/etc/namedb/named.conf ] 140 then 141 ln -s namedb/named.conf "${named_chrootdir}/etc" 142 fi 143 144 if [ -f /etc/localtime ]; then 145 cmp -s /etc/localtime "${named_chrootdir}/etc/localtime" || 146 cp -p /etc/localtime "${named_chrootdir}/etc/localtime" 147 fi 148 149 local piddir="$(dirname "${pidfile}")" 150 mkdir -p "${named_chrootdir}${piddir}" "${piddir}" 151 chmod 755 "${named_chrootdir}${piddir}" "${piddir}" 152 chown named:named "${named_chrootdir}${piddir}" "${piddir}" 153 ln -fs "${named_chrootdir}${pidfile}" "${pidfile}" 154 155 # Change run_rc_commands()'s internal copy of $named_flags 156 # 157 rc_flags="-u named -t ${named_chrootdir} $rc_flags" 158 } 159 160 load_rc_config $name 161 run_rc_command "$1" 162