1 1.1 lukem #!/bin/sh 2 1.1 lukem # 3 1.27 christos # $NetBSD: named,v 1.27 2021/03/31 04:57:25 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.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.27 christos copy_if_newer() 65 1.27 christos { 66 1.27 christos local chrootdir="$1" 67 1.27 christos local dir="$2" 68 1.27 christos local file="$3" 69 1.27 christos if [ ! -x "${chrootdir}${dir}/${file}" -o \ 70 1.27 christos "${chrootdir}${dir}/${file}" -ot "${dir}/${file}" ]; then 71 1.27 christos rm -f "${chrootdir}${dir}/${file}" 72 1.27 christos cp -p "${dir}/${file}" "${chrootdir}${dir}/${file}" 73 1.27 christos fi 74 1.27 christos } 75 1.27 christos 76 1.7 lukem named_precmd() 77 1.7 lukem { 78 1.24 spz if [ ! -e "/etc/rndc.key" ]; then 79 1.24 spz echo "Generating rndc.key" 80 1.24 spz /usr/sbin/rndc-confgen -a 81 1.24 spz fi 82 1.24 spz 83 1.7 lukem if [ -z "$named_chrootdir" ]; then 84 1.23 christos if [ ! -d "/etc/namedb/keys" ]; then 85 1.23 christos mkdir -m 775 "/etc/namedb/keys" 86 1.23 christos chown named:named "/etc/namedb/keys" 87 1.23 christos fi 88 1.7 lukem return 0; 89 1.7 lukem fi 90 1.7 lukem 91 1.7 lukem # If running in a chroot cage, ensure that the appropriate files 92 1.26 skrll # exist inside the cage, as well as helper symlinks into the cage 93 1.7 lukem # from outside. 94 1.7 lukem # 95 1.7 lukem # As this is called after the is_running and required_dir checks 96 1.7 lukem # are made in run_rc_command(), we can safely assume ${named_chrootdir} 97 1.7 lukem # exists and named isn't running at this point (unless forcestart 98 1.7 lukem # is used). 99 1.7 lukem # 100 1.12 christos case "$($command -v)" in 101 1.12 christos BIND*) # 9 no group, named-xfer, or ndc 102 1.27 christos named_plugindir="/usr/libexec/named" 103 1.27 christos if [ ! -d "${named_chrootdir}${named_plugindir}" ]; then 104 1.27 christos mkdir -p -m 755 "${named_chrootdir}${named_plugindir}" 105 1.27 christos chown root:wheel "${named_chrootdir}${named_plugindir}" 106 1.27 christos fi 107 1.27 christos for p in filter-aaaa.so; do 108 1.27 christos copy_if_newer "${named_chrootdir}" \ 109 1.27 christos "${named_plugindir}" "$p" 110 1.27 christos done 111 1.12 christos ;; 112 1.12 christos named*) # 4 and 8 113 1.12 christos rc_flags="-g named $rc_flags" 114 1.27 christos copy_if_newer "${named_chrootdir}" "/usr/libexec" "named-xfer" 115 1.12 christos ln -fs "${named_chrootdir}/var/run/ndc" /var/run/ndc 116 1.12 christos ;; 117 1.12 christos esac 118 1.12 christos 119 1.25 tls for i in null random urandom; do 120 1.13 christos if [ ! -c "${named_chrootdir}/dev/$i" ]; then 121 1.13 christos rm -f "${named_chrootdir}/dev/$i" 122 1.26 skrll (cd /dev && 123 1.13 christos /bin/pax -rw -pe "$i" "${named_chrootdir}/dev") 124 1.13 christos fi 125 1.13 christos done 126 1.4 lukem 127 1.17 christos if [ ! -h /etc/namedb ]; then 128 1.17 christos named_migrate /etc/namedb ${named_chrootdir} 129 1.16 christos fi 130 1.23 christos 131 1.23 christos for i in named.conf rndc.key; do 132 1.23 christos if [ \( -r "/etc/$i" \) -a \( ! -h "/etc/$i" \) -a \ 133 1.23 christos \( ! -r "${named_chrootdir}/etc/$i" \) ]; then 134 1.23 christos mv "/etc/$i" "${named_chrootdir}/etc/$i" 135 1.23 christos ln -s "${named_chrootdir}/etc/$i" "/etc/$i" 136 1.23 christos fi 137 1.23 christos done 138 1.23 christos 139 1.16 christos if [ \( ! -r ${named_chrootdir}/etc/named.conf \) -a \ 140 1.16 christos \( -r ${named_chrootdir}/etc/namedb/named.conf \) ]; then 141 1.16 christos 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.7 lukem 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