dns-scripts/update-dns.sh

130 lines
4.9 KiB
Bash
Raw Normal View History

2019-09-04 18:15:10 +02:00
#!/bin/sh
# exit script when command fails
set -e
# Communityconfig
CommunityDomain="fff.community"
CommunitySubnets="10.50.0/16 10.83.0/16 fd43:5602:29bd::/48"
MasterFileRemoteLocation="https://git.freifunk-franken.de/freifunk-franken/dns/raw/branch/master/db.fff.community"
# Serverconfig
export DNSSCRIPT_CONTACT_EMAIL=info.freifunk-herpf.de.
# DNSSCRIPT_SERVER_NAME must be the server given in community zone files NS entry
export DNSSCRIPT_SERVER_NAME=dns.herpf.fff.community
UpdateScriptsFolder="/usr/lib/ffdns/"
ZoneFilesFolder="/etc/bind/fff/"
2021-01-15 17:12:33 +01:00
BindIncludeFileFolder="/etc/bind/"
# 0 -> Debian (and like) systemctl reload bind9
# 1 -> use rndc to reload zone (recommended; rndc needs setup first)
# 2 -> OpenWRT /etc/init.d/named reload
#
export DNSSCRIPT_BIND_RELOAD_VER=0
2021-01-14 23:50:36 +01:00
# only necessary when rndc is used
InternalViews="icvpn-internal-view icvpn-internal-dns64-view"
ExternalView="external-view"
# TTL Refresh Retry Expire Minimum
TTLReReExMi="3600 2000 6400 2419200 86400"
# ForwardZones: "<Zone>/<Zonendatei>" ; optionaly multiple " ""<ZoneX>/<ZonendateiX>" no spaces in full filename
ForwardZones="$CommunityDomain""/""$ZoneFilesFolder""db.""$CommunityDomain"
#############################################################
cd "$UpdateScriptsFolder"
. ./dns-functions.sh
MasterFile="$ZoneFilesFolder""db.""$CommunityDomain"
2021-01-15 17:20:50 +01:00
BindInternalConf="$BindIncludeFileFolder""$CommunityDomain""-internal.conf"
BindExternalConf="$BindIncludeFileFolder""$CommunityDomain""-external.conf"
2021-01-15 17:12:33 +01:00
BindIcvpnAcl="$BindIncludeFileFolder""icvpn-acl.conf"
PreFetchMasterSerial="$(GetZoneFileSerial "$MasterFile")"
curl "$MasterFileRemoteLocation" --output "$MasterFile"
PostFetchMasterSerial="$(GetZoneFileSerial "$MasterFile")"
if [ $((PostFetchMasterSerial)) -gt $((PreFetchMasterSerial)) ]; then
ReloadZone "$CommunityDomain" "$InternalViews"
2019-09-04 18:15:10 +02:00
fi
2021-01-15 17:20:50 +01:00
rm -f "$BindExternalConf"".tmp"
rm -f "$BindInternalConf"".tmp"
InsertZoneToIncludeFile "$CommunityDomain" "$ZoneFilesFolder""db.""$CommunityDomain" "$BindExternalConf"".tmp"
2021-01-15 17:12:33 +01:00
for Subnet in $CommunitySubnets; do
ReverseDomains="$(GetReverseDomains "$Subnet")"
for RDomain in $ReverseDomains; do
./update-rdnszone.sh "$RDomain" "$ForwardZones" "$ZoneFilesFolder" "$TTLReReExMi" "$InternalViews"
2021-01-15 17:20:50 +01:00
InsertZoneToIncludeFile "$RDomain" "$ZoneFilesFolder""$(GetReverseZoneFileFromZone "${RDomain%*.}")" "$BindInternalConf"".tmp"
done
done
2021-01-15 17:12:33 +01:00
InternFile="$MasterFile"
ExternFile="$ZoneFilesFolder""dbextern.""$CommunityDomain"
Domain="$CommunityDomain"
./update-extzone.sh "$InternFile" "$ExternFile" "$Domain" "$ExternalView"
2019-09-04 18:15:10 +02:00
# set shorter TTL for Hoods
TTLReReExMi="420 360 180 1800 360"
Hoods="$(GetOwnHoods "$CommunityDomain" "$MasterFile")"
for Hood in $Hoods; do
HoodSubDomain="${Hood%%\#*}"
Subnets="$(echo "${Hood#*\#}" | sed -e 's/#/ /g')"
HoodZoneFile="$ZoneFilesFolder""db.""$HoodSubDomain"".""$CommunityDomain"
if [ ! -f "$HoodZoneFile" ]; then
echo "\$TTL ${TTLReReExMi%% *}" > "$HoodZoneFile"
echo "$HoodSubDomain"".""$CommunityDomain"". ${TTLReReExMi%% *} IN SOA $DNSSCRIPT_SERVER_NAME""."" $DNSSCRIPT_CONTACT_EMAIL 1 ${TTLReReExMi#* }" >> "$HoodZoneFile"
echo "$HoodSubDomain"".""$CommunityDomain"". ${TTLReReExMi%% *} IN NS $DNSSCRIPT_SERVER_NAME"".""" >> "$HoodZoneFile"
echo "$(GetOwnGlueRecords "$CommunityDomain" "$MasterFile")" >> "$HoodZoneFile"
fi
./update-hoodzone.sh "$HoodZoneFile" "$HoodSubDomain"".""$CommunityDomain" "$Subnets" "$InternalViews"
2021-01-15 17:20:50 +01:00
InsertZoneToIncludeFile "$HoodSubDomain"".""$CommunityDomain" "$ZoneFilesFolder""db.""$HoodSubDomain"".""$CommunityDomain" "$BindExternalConf"".tmp"
2021-01-15 17:12:33 +01:00
HoodForwardZones="$ForwardZones $HoodSubDomain"".""$CommunityDomain""/""$ZoneFilesFolder""db.""$HoodSubDomain"".""$CommunityDomain"
for Subnet in $Subnets; do
ReverseDomains="$(GetReverseDomains "$Subnet")"
for RDomain in $ReverseDomains; do
./update-rdnszone.sh "$RDomain" "$HoodForwardZones" "$ZoneFilesFolder" "$TTLReReExMi" "$InternalViews"
2021-01-15 17:20:50 +01:00
InsertZoneToIncludeFile "$RDomain" "$ZoneFilesFolder""$(GetReverseZoneFileFromZone "${RDomain%*.}")" "$BindInternalConf"".tmp"
done
done
2021-01-15 17:12:33 +01:00
InternFile="$ZoneFilesFolder""db.""$HoodSubDomain"".""$CommunityDomain"
ExternFile="$ZoneFilesFolder""dbextern.""$HoodSubDomain"".""$CommunityDomain"
Domain="$HoodSubDomain"".""$CommunityDomain"
./update-extzone.sh "$InternFile" "$ExternFile" "$Domain" "$ExternalView"
2021-01-15 17:12:33 +01:00
done
2021-01-15 17:20:50 +01:00
sed -i "1i include \"""$BindExternalConf""\";\n" "$BindInternalConf"".tmp"
2021-01-15 17:12:33 +01:00
./update-public-acl.sh "$BindIcvpnAcl"".tmp"
ReConfigBind=1
UpdateBindConfig() {
if [ -f "$1"".tmp" ] && ! cmp -s "$1"".tmp" "$1"; then
mv "$1"".tmp" "$1"
ReConfigBind=0
else
rm -f "$1"".tmp"
fi
2021-01-15 17:12:33 +01:00
}
UpdateBindConfig "$BindIcvpnAcl"
2021-01-15 17:20:50 +01:00
UpdateBindConfig "$BindInternalConf"
UpdateBindConfig "$BindExternalConf"
2021-01-15 17:12:33 +01:00
if [ $ReConfigBind -eq 0 ]; then
if [ $DNSSCRIPT_BIND_RELOAD_VER -eq 0 ]; then
systemctl restart bind9
elif [ $DNSSCRIPT_BIND_RELOAD_VER -eq 1 ]; then
rndc reconfig
else
/etc/init.d/named restart
fi
fi