#!/bin/sh
#
# Update list of zones handled by bind to the complete list of zones
# in LDAP.  Run this to Make sure bind see all zones in LDAP, also
# when the zones change.

set -e

update_zone_list() {
    conffile=/etc/bind/named.conf.ldap2zone
    zones="$(ldapsearch -x '(soarecord=*)' zonename | awk '/^zoneName/ { print $2}' | sort)"
    for zone in $zones ; do
	cat <<EOF
zone "$zone" {
	type master;
	notify yes;
	file "/etc/bind/db.$zone";
};

EOF
    done > $conffile.new
    if cmp $conffile.new $conffile ; then
	rm $conffile.new
    else
	logger -t ldap2bind-updatezonelist replacing $conffile with zones $zones
	chown root:bind $conffile.new
	chmod 644 $conffile.new
	mv $conffile.new $conffile
    fi
}

update_zone_list
