dns-scripts/usr/lib/ffdns/update-dns.sh
Blackyfff d829a08b54 version 1.0.0rc2
Signed-off-by: Blackyfff <freifunk@freifunk-herpf.de>
2023-04-06 01:32:33 +02:00

165 lines
5.4 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: GPL-3.0
#
# freifunk-franken dns-scipts (c) 2016 mayosemmel
# (c) 2020-2021 Fabian Bläse
# (c) 2021-2023 Blackyfff
# exit script when command fails
set -e
export DNSSCRIPT_VERSION="1.0.0rc2"
. /etc/ffdns/community.conf
. /etc/ffdns/local.conf
export DNSSCRIPT_CONTACT_EMAIL
export DNSSCRIPT_SERVER_NAME
export DNSSCRIPT_BIND_RELOAD_VER
DNSSCRIPT_DNSSECPolicy="$DNSSECPolicy"
export DNSSCRIPT_DNSSECPolicy
DNSSCRIPT_TEMP_FOLDER="$TempFolder"
export DNSSCRIPT_TEMP_FOLDER
cd /usr/lib/ffdns/
. ./dns-functions.sh
. ./update-dns-functions.sh
MasterDomain="$CommunityDomain"
[ -z "$SubCommunityDomain" ] || MasterDomain="$SubCommunityDomain"".""$MasterDomain"
if [ -n "$CommunityExternPrefix" ];then
MasterExternDomain="$CommunityExternPrefix"".""$CommunityDomain"
[ -z "$SubCommunityDomain" ] || MasterExternDomain="$SubCommunityDomain"".""$MasterExternDomain"
fi
FirstInternalView="$( echo "$InternalViews" | sed -e 's/\s.*//')"
# ForwardZones: "<Zone>/<Zonendatei>" ; optionaly multiple " ""<ZoneX>/<ZonendateiX>" no spaces in full filename
ForwardZones="$MasterDomain""/""$ZoneFilesFolder""db.""$FirstInternalView"".""$MasterDomain"
[ -n "$DNSSCRIPT_DNSSECPolicy" ] || DNSSECKeyFolder=""
SetupCache
flock -n "$DNSSCRIPT_TEMP_FOLDER""script.lock" -c :
CachedMasterFile="$DNSSCRIPT_TEMP_FOLDER""cache/db.""$MasterDomain"
PreFetchMasterSerial="$(GetZoneFileSerial "$CachedMasterFile")"
GetMasterFile
PostFetchMasterSerial="$(GetZoneFileSerial "$CachedMasterFile")"
ServeMasterZone="$(DoServeMasterZone)"
if [ -n "$ServeMasterZone" ] || [ -n "$(DoServeOnlyExternZone)" ]; then
RemoveDNSSECKeysFromCacheFile
if [ -n "$ServeMasterZone" ]; then
MasterFile="$ZoneFilesFolder""db.""$FirstInternalView"".""$MasterDomain"
FileForExternGeneration="$MasterFile"
else
FileForExternGeneration="$CachedMasterFile"
fi
if [ -n "$ExternalView" ]; then
ExternFile="$ZoneFilesFolder""db.""$ExternalView"".""$MasterDomain"
else
ExternFile="$ZoneFilesFolder""db.extern.""$MasterDomain"
fi
SerialIntern="$(UpdateMasterZone)"
InternalZoneFile="$FileForExternGeneration"
ExternalZoneFile="$ExternFile"
InternalDomain="$MasterDomain"
ExternDomain="$MasterExternDomain"
UpdateExternal
fi
if [ -z "$MasterFile" ]; then
MasterFile="$ZoneFilesFolder""db.""$FirstInternalView"".""$MasterDomain"
cp -f "$CachedMasterFile" "$MasterFile"
fi
UpdateReverseZones "$CommunitySubnets" "$ForwardZones" "$RemoteLocation"
# set shorter TTL for Hoods
TTLReReExMi="420 360 180 1800 360"
Hoods="$(GetOwnHoods "$MasterDomain" "$MasterFile")"
DNSSECKeyFolder=""
for Hood in $Hoods; do
HoodDomain="${Hood%%\#*}"".""$MasterDomain"
Subnets="$(echo "${Hood#*\#}" | sed -e 's/#/ /g')"
HoodZoneFile="$ZoneFilesFolder""db.""$FirstInternalView"".""$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 "$MasterDomain" "$HoodDomain" "$MasterFile"
echo ";"
} > "$HoodZoneFile"
fi
ZoneTempFolder="$DNSSCRIPT_TEMP_FOLDER""cache/"
./update-hoodzone.sh "$HoodZoneFile" "$HoodDomain" "$Subnets" "$InternalViews" "$ZoneTempFolder" "$ZoneFilesFolder"
InsertZoneToViews "$InternalViews" "$ZoneFilesFolder" "$HoodDomain" "$HoodZoneFile" "/etc/ffdns/"
HoodForwardZones="$ForwardZones $HoodDomain""/""$HoodZoneFile"
UpdateReverseZones "$Subnets" "$HoodForwardZones"
if [ -n "$MasterExternDomain" ]; then
HoodExternDomain="${Hood%%\#*}"".""$MasterExternDomain"
else
HoodExternDomain=""
fi
if [ -n "$ExternalView" ]; then
ExternFile="$ZoneFilesFolder""db.""$ExternalView"".""$HoodDomain"
elif [ -n "$HoodExternDomain" ]; then
ExternFile="$ZoneFilesFolder""db.""$HoodExternDomain"
else
ExternFile=""
fi
if [ -n "$ExternFile" ]; then
InternalZoneFile="$HoodZoneFile"
ExternalZoneFile="$ExternFile"
InternalDomain="$HoodDomain"
ExternDomain="$HoodExternDomain"
SerialIntern="$(GetZoneFileSerial "$InternalZoneFile")"
UpdateExternal
fi
done
ReConfigBind=0
UpdateBindConfig() {
if [ -f "$1" ] && ! cmp -s "$1" "$2"; then
mv "$1" "$2"
ReConfigBind=1
else
rm -f "$1"
fi
}
for IView in $InternalViews; do
UpdateBindConfig "$DNSSCRIPT_TEMP_FOLDER""$IView"".conf" "$GeneratedIncludeFileFolder""$IView"".conf"
done
if [ -n "$ExternalView" ]; then
UpdateBindConfig "$DNSSCRIPT_TEMP_FOLDER""$ExternalView"".conf" "$GeneratedIncludeFileFolder""$ExternalView"".conf"
fi
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