Unter OpenWRT kein =~; IP-Adressvalidierung korrigiert; Optionale Nutzung von rndc -> Vorteil: nur eine Zone wird neu geladen; Refaktoring

This commit is contained in:
Blackyfff 2021-01-04 20:53:39 +01:00
parent bc38442387
commit 038acaa2eb
1 changed files with 24 additions and 21 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
DomainZone="50.10.in-addr.arpa."
ForwardZoneFiles=("/srv/fff-dns/db.fff.community")
@ -15,24 +15,27 @@ ReverseServerName=aquarius.gw.fff.community.
#################################################################
function dnsreload {
systemctl reload bind9
function GetZoneFileSerial() {
local FirstSOALineAndFollowing="/\S\+\s\+IN\s\+SOA\s/,\$!d;"
local RemoveComments=":a;s/;.*$//g;"
local EleminateLineBreaks=":a;N;\$!ba;s/\n//g;"
local SearchPrintSerial="s/\S\+\s\+IN\s\+SOA\s\+\S\+\s\+\S\+\s\+(\?\s\+\([0-9]*\).*/\1/"
local serial=$(sed -e "$FirstSOALineAndFollowing""$RemoveComments""$EleminateLineBreaks""$SearchPrintSerial" "$1")
echo "$serial"
}
function validate_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
function dnsreload {
if [[ -n "$Usernc" ]]; then
rndc reload "$DomainZone" IN "icvpn-internal-view"
else
systemctl reload bind9
fi
return $stat
}
function validate_ipv4() {
[[ -n "$(echo "$1" | sed -e '/^\(\(25[0-5]\|\(2[0-4]\|1[0-9]\|[1-9]\)\?[0-9]\)\.\)\{0,3\}\(25[0-5]\|\(2[0-4]\|1[0-9]\|[1-9]\)\?[0-9]\)$/!d')" ]]
return $?
}
@ -42,7 +45,7 @@ for ForwardZoneFile in "${ForwardZoneFiles[@]}"
do
ZoneName=$(sed -ne 's/\(\S\+\)\s\+IN\s\+SOA\s\+\S\+.*/\1/p' "$ForwardZoneFile")
named-compilezone -o "$TempDir/$ZoneName" $ZoneName $ForwardZoneFile >/dev/null 2>&1
serial=$(sed -e '/\S\+\s\+IN\s\+SOA\s/,$!d;:a;s/;.*$//g;:a;N;$!ba;s/\n//g;s/\S\+\s\+IN\s\+SOA\s\+\S\+\s\+\S\+\s\+(\?\s\+\([0-9]*\).*/\1/' "$TempDir/$ZoneName" )
serial="$(GetZoneFileSerial '$TempDir/$ZoneName')"
Serials+=( "$serial" )
done
@ -58,7 +61,7 @@ do
i=0
for IP in $IPs
do
if validate_ip $IP
if validate_ipv4 $IP
then
echo $(echo $IP | awk 'BEGIN { FS = "." } ; { print $4 "." $3 "." $2 "." $1 }')".in-addr.arpa." $TTL IN PTR ${Hosts[$i]} >> "$TempDir/$DomainZone"
fi
@ -66,13 +69,13 @@ do
done
done
if [ -f $ReverseZoneFile ]; then
oldSerial=$(grep SOA $ReverseZoneFile | awk 'NR==1{print $7}')
if [[ -f $ReverseZoneFile ]]; then
oldSerial="$(GetZoneFileSerial '$ReverseZoneFile')"
else
oldSerial=0
fi
if [ $serial -gt $oldSerial ]
if [[ $serial -gt $oldSerial ]]
then
named-compilezone -o $ReverseZoneFile $DomainZone "$TempDir/$DomainZone" >/dev/null 2>&1
dnsreload