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