#HOSTS_CHARSET='utf-8'
HOSTS_INCLUDE="$ETC/hosts"
NAMED_CHECKZONE=/usr/sbin/named-checkzone
RNDC=/usr/sbin/rndc
RNDC_KEY=/etc/bind/rndc.key
## Generate forward zone from hosts hosts using pvl.hosts-forward
#
# update_hosts_forward $out $src
function update_hosts_forward {
local out="$1"
local src="$2"
local srcs=($src/*)
local msg="$out: Generating forward hosts zone: $src"
if apply_check "$out" "${srcs[@]}"; then
log_skip "$msg"
else
log_apply "$msg"
apply_cmd "$out" $OPT/bin/pvl.hosts-forward \
--hosts-include="$HOSTS_INCLUDE" \
"$src"
fi
}
## Generate reverse zone from hosts hosts using pvl.hosts-reverse
#
# update_hosts_reverse $out $src
function update_hosts_reverse {
local out="$1"
local src="$2"
local srcs=($src/*)
local msg="$out: Generating reverse hosts zone: $src"
if apply_check "$out" "${srcs[@]}"; then
log_skip "$msg"
else
log_apply "$msg"
apply_cmd "$out" $OPT/bin/pvl.hosts-reverse \
--hosts-include="$HOSTS_INCLUDE" \
"$src"
fi
}
function update_zone_include {
local out="$1"
local src="$2"
local msg="$out: Copy zone include: $src"
if apply_check "$out" "${srcs[@]}"; then
log_skip "$msg"
else
log_apply "$msg"
apply_cmd "$out" cat \
"$src"
fi
}
## Read include paths from file
function read_zone_includes {
cmd sed -n -E 's/^\$INCLUDE\s+"(.+)"/\1/p' "$@"
}
## (cached) include paths for zone file
function zone_includes {
local cache="$1"
local src="$2"
local prefix="${3:-}"
if [ ! -e "$cache" -o "$cache" -ot "$src" ]; then
read_zone_includes "$src" > "$cache"
fi
while read include; do
echo -n "$prefix$include "
done < "$cache"
}
## Search for prefix-matching includes in zone file
function zone_includes_grep {
local cache="$1"
local src="$2"
local prefix="$3"
for include in $(zone_includes $cache $src); do
if [ "${include#$prefix}" != "$include" ]; then
echo -n " ${include#$prefix}"
fi
done
}
## Generate new serial for zone using pvl.dns-serial, if the zone data has changed:
#
# update_serial .../serials/$zone $serial $deps...
#
# Supports SERIAL_FORCE/NOOP.
# Updates $SERIALS/$zone.serial.
function update_zone_serial {
local out="$1"
local serial="$2"
local old=$(test -e "$out" && cat "$out" || echo '')
# test
if [ "$SERIAL" = 1 ]; then
log_force "$out: Force serial $old <- $serial"
elif apply_check "$out" "${@:3}"; then
log_skip "$out: Skip serial: $old <- $serial"
return
elif [ "$SERIAL" = 0 ]; then
log_noop "$out: Noop serial: $old <- $serial"
return
else
log_apply "$out: Update serial: $old <- $serial"
fi
echo "$serial" > $out
}
## Generate zone file from source using pvl.dns-zone:
#
# update_zone out/zones/$zone in/zones/$zone var/serials/$zone
function update_zone {
local out="$1"
local src="$2"
local serial="$3"
local serial_opt=
local msg="$out: Generate zone: $src"
if [ -n "$serial" -a -f "$serial" ]; then
serial_opt="--serial=$(cat "$serial")"
elif [ "$SERIAL" = 0 ]; then
warn "$out: noop'd serial, omitting"
else
fail "$out: missing serial: $serial"
fi
if apply_check "$@"; then
log_skip "$msg"
else
log_apply "$msg"
apply_cmd "$out" $OPT/bin/pvl.dns-zone \
--include-path=$VAR/zones \
$serial_opt \
"$src"
fi
}
## Test zone file for validity using named-checkzone:
#
# check_zone ..../$zone $origin
function test_zone {
local zone=$1
local origin=$2
log_check "$zone: Checking zone @$origin..."
# checkzone is very specific about the order of arguments, -q must be first
test_cmd $zone \
$NAMED_CHECKZONE $origin $zone
}
# set by do_reload_zone if zone data has actually been reloaded
RELOAD_ZONES=
## Load update zonefiles into bind:
#
# reload_zones
#
# Invokes `rndc reload`, showing its output.
function reload_zones {
if [ "$RELOAD" = 1 ]; then
log_force "Reload zones"
elif [ "$RELOAD" = 0 ]; then
log_noop "Skip reload zones"
return
elif [ ! -e "$RNDC" ]; then
warn "Skip with missing RNDC: $RNDC"
return
elif [ ! -e "$RNDC_KEY" ]; then
warn "Skip with missing RNDC_KEY: $RNDC_KEY"
return
elif [ ! -r $RNDC_KEY ]; then
error "Permission denied for RNDC_KEY: $RNDC_KEY"
return 1
else
log_apply "Reload zones"
fi
indent " rndc: " \
$RNDC reload
# set flag for dhcp
RELOAD_ZONES=1
}