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