dns-scripts/update-dns.sh

187 lines
7.9 KiB
Bash
Executable File

#!/bin/sh
# exit script when command fails
set -e
# Communityconfig
CommunityDomain="fff.community"
CommunityExternDomain="extern.fff.community"
CommunitySubnets="10.50.0/16 10.83.0/16 fd43:5602:29bd::/48"
RemoteLocation="https://git.freifunk-franken.de/freifunk-franken/dns/raw/branch/master/"
DNSSECPolicy=""
ServeMasterZone=0
# Serverconfig
export DNSSCRIPT_CONTACT_EMAIL=info.freifunk-herpf.de.
# DNSSCRIPT_SERVER_NAME must be the server given in community zone files NS entry (Full Hostname, w/o trailing dot)
export DNSSCRIPT_SERVER_NAME=dns.herpf.fff.community
UpdateScriptsFolder="/usr/lib/ffdns/"
ZoneFilesFolder="/etc/bind/fff/"
BindIncludeFileFolder="/etc/bind/"
# specify the bird/babel or other routing table[s]
# if RoutingTables is empty, the ICVPN-ACL-List will be fetched remotely (for servers that are no gateways)
RoutingTables="10"
# -1 -> disable bind [restart|reload]
# 0 -> Debian (and like) systemctl [reload|restart] bind9
# 1 -> use rndc to [reload zone|reconfig] (recommended; rndc needs setup first)
# 2 -> OpenWRT /etc/init.d/named [reload|restart]
export DNSSCRIPT_BIND_RELOAD_VER=0
# 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.icvpn-internal-view.""$CommunityDomain"
#############################################################
cd "$UpdateScriptsFolder"
. ./dns-functions.sh
FirstInternal="$( echo "$InternalViews" | sed -ne 's/^\(\S\+\)\s.*$/\1/p')"
MasterFile="$ZoneFilesFolder""db.""$FirstInternal"".""$CommunityDomain"
BindIcvpnAclTmp="/tmp/icvpn-acl.conf"
BindIcvpnAcl="$BindIncludeFileFolder""icvpn-acl.conf"
for IView in $InternalViews; do
rm -f "/tmp/""$IView"".conf"
done
rm -f "/tmp/""$ExternalView"".conf"
PreFetchMasterSerial="$(GetZoneFileSerial "$MasterFile")"
curl -s -S -f "$RemoteLocation""db.""$CommunityDomain" --output "$MasterFile"
if [ $ServeMasterZone -ne 0 ]; then
PostFetchMasterSerial="$(GetZoneFileSerial "$MasterFile")"
if [ $((PostFetchMasterSerial)) -gt $((PreFetchMasterSerial)) ]; then
ReloadZone "$CommunityDomain" "$InternalViews"
fi
for Subnet in $CommunitySubnets; do
ReverseDomains="$(GetReverseDomains "$Subnet")"
for RDomain in $ReverseDomains; do
ReverseZoneFile="$(GetReverseZoneFileFromZone "${RDomain%*.}")"
! curl -s -f "$RemoteLocation""static.""$ReverseZoneFile" \
--output "$ZoneFilesFolder""static.""$ReverseZoneFile" && \
rm -f "$ZoneFilesFolder""static.""$ReverseZoneFile"
./update-rdnszone.sh "$RDomain" "$ForwardZones" "$ZoneFilesFolder""$ReverseZoneFile" "$TTLReReExMi" "$InternalViews"
for IView in $InternalViews; do
InsertZoneToIncludeFile "${RDomain%*.}" "$ZoneFilesFolder""$ReverseZoneFile" "/tmp/""$IView"".conf"
done
done
done
ExternFile="$ZoneFilesFolder""db.""$ExternalView"".""$CommunityDomain"
./update-extzone.sh "$MasterFile" "$ExternFile" "$CommunityDomain" "$ExternalView"
for IView in $InternalViews; do
InternViewMasterZone="$ZoneFilesFolder""db.""$IView"".""$CommunityDomain"
[ -f "$InternViewMasterZone" ] || ln -s "$MasterFile" "$InternViewMasterZone"
InsertZoneToIncludeFile "$CommunityDomain" "$InternViewMasterZone" "/tmp/""$IView"".conf" "$DNSSECPolicy"
done
InsertZoneToIncludeFile "$CommunityDomain" "$ExternFile" "/tmp/""$ExternalView"".conf" "$DNSSECPolicy"
if [ -n "$CommunityExternDomain" ]; then
for IView in $InternalViews; do
InternViewExternZone="$ZoneFilesFolder""db.""$IView"".""$CommunityExternDomain"
[ -f "$InternViewExternZone" ] || ln -s "$ExternFile" "$InternViewExternZone"
InsertZoneToIncludeFile "$CommunityExternDomain" "$InternViewExternZone" "/tmp/""$IView"".conf" "$DNSSECPolicy"
done
ExternViewExternZone="$ZoneFilesFolder""db.""$ExternalView"".""$CommunityExternDomain"
[ -f "$ExternViewExternZone" ] || ln -s "$ExternFile" "$ExternViewExternZone"
InsertZoneToIncludeFile "$CommunityExternDomain" "$ExternViewExternZone" "/tmp/""$ExternalView"".conf" "$DNSSECPolicy"
fi
fi
# set shorter TTL for Hoods
TTLReReExMi="420 360 180 1800 360"
Hoods="$(GetOwnHoods "$CommunityDomain" "$MasterFile")"
for Hood in $Hoods; do
HoodDomain="${Hood%%\#*}"".""$CommunityDomain"
Subnets="$(echo "${Hood#*\#}" | sed -e 's/#/ /g')"
HoodZoneFile="$ZoneFilesFolder""db.""$FirstInternal"".""$HoodDomain"
if [ ! -f "$HoodZoneFile" ]; then
{
echo "\$TTL ${TTLReReExMi%% *}"
echo "@ IN SOA $DNSSCRIPT_SERVER_NAME""."" $DNSSCRIPT_CONTACT_EMAIL ("
echo " 1 ; Serial"
echo " ""$(echo "$TTLReReExMi" | awk '{print $2}')"" ; Refresh"
echo " ""$(echo "$TTLReReExMi" | awk '{print $3}')"" ; Retry"
echo " ""$(echo "$TTLReReExMi" | awk '{print $4}')"" ; Expire"
echo " ""$(echo "$TTLReReExMi" | awk '{print $5}')"" ) ; Negative Cache TTL"
echo ";"
echo "@ IN NS $DNSSCRIPT_SERVER_NAME""."""
GetOwnGlueRecords "$CommunityDomain" "$HoodDomain" "$MasterFile"
echo ";"
} > "$HoodZoneFile"
fi
./update-hoodzone.sh "$HoodZoneFile" "$HoodDomain" "$Subnets" "$InternalViews"
HoodForwardZones="$ForwardZones $HoodDomain""/""$HoodZoneFile"
for Subnet in $Subnets; do
ReverseDomains="$(GetReverseDomains "$Subnet")"
for RDomain in $ReverseDomains; do
ReverseZoneFileFullPath="$ZoneFilesFolder""$(GetReverseZoneFileFromZone "${RDomain%*.}")"
./update-rdnszone.sh "$RDomain" "$HoodForwardZones" "$ReverseZoneFileFullPath" "$TTLReReExMi" "$InternalViews"
for IView in $InternalViews; do
InsertZoneToIncludeFile "${RDomain%*.}" "$ReverseZoneFileFullPath" "/tmp/""$IView"".conf"
done
done
done
ExternFile="$ZoneFilesFolder""db.""$ExternalView"".""$HoodDomain"
./update-extzone.sh "$HoodZoneFile" "$ExternFile" "$HoodDomain" "$ExternalView"
for IView in $InternalViews; do
InternViewMasterZone="$ZoneFilesFolder""db.""$IView"".""$HoodDomain"
[ -f "$InternViewMasterZone" ] || ln -s "$HoodZoneFile" "$InternViewMasterZone"
InsertZoneToIncludeFile "$HoodDomain" "$InternViewMasterZone" "/tmp/""$IView"".conf" "$DNSSECPolicy"
done
InsertZoneToIncludeFile "$HoodDomain" "$ExternFile" "/tmp/""$ExternalView"".conf" "$DNSSECPolicy"
if [ -n "$CommunityExternDomain" ]; then
HoodExternDomain="${Hood%%\#*}"".""$CommunityExternDomain"
for IView in $InternalViews; do
InternViewExternZone="$ZoneFilesFolder""db.""$IView"".""$HoodExternDomain"
[ -f "$InternViewExternZone" ] || ln -s "$ExternFile" "$InternViewExternZone"
InsertZoneToIncludeFile "$HoodExternDomain" "$InternViewExternZone" "/tmp/""$IView"".conf" "$DNSSECPolicy"
done
ExternViewExternZone="$ZoneFilesFolder""db.""$ExternalView"".""$HoodExternDomain"
[ -f "$ExternViewExternZone" ] || ln -s "$ExternFile" "$ExternViewExternZone"
InsertZoneToIncludeFile "$HoodExternDomain" "$ExternViewExternZone" "/tmp/""$ExternalView"".conf" "$DNSSECPolicy"
fi
done
./update-public-acl.sh "$BindIcvpnAclTmp" "$RemoteLocation" "$RoutingTables"
ReConfigBind=0
UpdateBindConfig() {
if [ -f "$1" ] && ! cmp -s "$1" "$2"; then
mv "$1" "$2"
ReConfigBind=1
else
rm -f "$1"
fi
}
UpdateBindConfig "$BindIcvpnAclTmp" "$BindIcvpnAcl"
for IView in $InternalViews; do
UpdateBindConfig "/tmp/""$IView"".conf" "$BindIncludeFileFolder""$IView"".conf"
done
UpdateBindConfig "/tmp/""$ExternalView"".conf" "$BindIncludeFileFolder""$ExternalView"".conf"
if [ $ReConfigBind -ne 0 ] || [ -f "/tmp/dnsscript-forcereconf" ]; then
if [ $((DNSSCRIPT_BIND_RELOAD_VER)) -eq 0 ]; then
systemctl restart bind9
elif [ $((DNSSCRIPT_BIND_RELOAD_VER)) -eq 1 ]; then
rndc reconfig
elif [ $((DNSSCRIPT_BIND_RELOAD_VER)) -eq 2 ]; then
/etc/init.d/named restart
fi
rm -f "/tmp/dnsscript-forcereconf"
fi