Skip to content

Instantly share code, notes, and snippets.

@booth-f
Created April 21, 2022 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save booth-f/494c9f1b1a6e34af8aab21804a607919 to your computer and use it in GitHub Desktop.
Save booth-f/494c9f1b1a6e34af8aab21804a607919 to your computer and use it in GitHub Desktop.
DNSSEC Signing Script Bash
#!/bin/bash
# DNS Zone Updater 5.0
# Set DNS zone records location
location="/etc/bind/"
# Change to DNS server dir
cd $location
# Set Domains
domains=`ls db.*{.com,.net,.me,.dev,.cloud,.space} | cut -c 4-`
echo $domains
# Fetch current date for serial generation
date=`date +%Y%m%d`
for domain in $domains
do
# Set Control Bools
dateJump=0
serialMismatch=0
hashMissing=0
# Check if a hash for this domain exists
if [ -f $location/.hashes/$domain.sha512 ];
then
# Check our current hash to see if changes to the zone have taken place
HASH=`cat $location/.hashes/$domain.sha512`
else
hashMissing=1
fi
GENHASH=`/usr/bin/sha512sum $location/db.$domain | cut -b 1-128`
# Grab the current DNS serial
SERIAL=`/usr/sbin/named-checkzone $domain db.$domain | egrep -ho '[0-9]{10}'`
# Grab the first 8 numbers of the serial
dateCheck=`echo $SERIAL | cut -b 1-8`
if [ ! $dateCheck == $date ];
then
newSerial="$date"
zero="00"
# Update the DNS serial to have today's date and reset DNS counter to 0
sed -i 's/'$SERIAL'/'$(($newSerial$zero))'/' db.$domain
dateJump=1
else
if [ $hashMissing == 0 ];
then
# Check if the zone hash is different
if [ ! $HASH == $GENHASH ];
then
serialMismatch=1
fi
fi
fi
if [ $dateJump -eq 1 ] || [ $serialMismatch -eq 1 ] || [ $hashMissing -eq 1 ];
then
# Increment the current serial
sed -i 's/'$SERIAL'/'$(($SERIAL+1))'/' db.$domain
# Sign the updated zone
/usr/sbin/dnssec-signzone -o $domain -A -N INCREMENT -3 $(head -c 1000 /dev/urandom | sha1sum | cut -b 1-16) -t db.$domain
# Save new hash
/usr/bin/sha512sum $location/db.$domain | cut -b 1-128 > $location/.hashes/$domain.sha512
else
echo "$domain zone has no changes, moving on"
# Do nothing, no changes
fi
# Run a zone check
/usr/sbin/named-checkzone $domain db.$domain
status=$?
if [ $status -eq 0 ];
then
# Good
echo "$domain OK"
else
# Rollback zone updates and alert of failure
mv $location.backup/db.$domain.backup $location/db.$domain
echo "$domain zone could not be updated due to errors in the zone"
echo "[WARN] $domain zone failed to update!"
fi
# Create "Known working" backup
cp $location/db.$domain $location/.backup/db.$domain.backup
done
# Reload DNS
/usr/sbin/rndc reload
@booth-f
Copy link
Author

booth-f commented Apr 21, 2022

Used this script in a production environment for over 2 years and never once had a DNSSEC-related failure due to zones not being re-signed. There is still a lot left to be desired, this script is heavily tailored to the environment I built it inside so assumptions like "all zone files will start with db.$zone" are environment specific and need to be adjusted for your situation. Same with line 12 where it determines how many domains it needs to cycle through.

If this script catches your eyes, I am working on a Ruby-based replacement that will handle a lot more tasks than this.

I decided to release this version of the DNS maintainer script in the hopes that it helps someone who may be struggling with automating zone signing and all the fun that it entails. The script does not automate the key generation process so you will still have to have a working zone already created before this will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment