Skip to content

Instantly share code, notes, and snippets.

@ip75
Last active April 27, 2024 21:31
Show Gist options
  • Save ip75/421573f4a8ab140c5b1e1c996a0538a3 to your computer and use it in GitHub Desktop.
Save ip75/421573f4a8ab140c5b1e1c996a0538a3 to your computer and use it in GitHub Desktop.
#!/bin/sh
zone_base_path='/usr/local/etc/nsd/'
update_serial() {
acme_fqdn=$1
zone_full_path=$2
# get serial number
serial=$(grep 'Serial' $zone_full_path | sed -E 's/([ \t]+)([0-9]{10})(.+)/\2/')
# get serial number's date
serialdate=$(echo $serial | cut -b 1-8)
# get today's date in same style
date=$(date +%Y%m%d)
# compare date and serial date
if [ $serialdate = $date ]
then
# if equal, just add 1
newserial=$(expr $serial + 1)
else
# if not equal, make a new one and add 00
newserial=$(echo $date"00")
fi
# edit zonefile in place, leaving a backup (.bak)
# experiment with amount of tabs (whitespace) needed to line up correctly
#sed -i .bak "s/.*Serial.*/ $newserial ; Serial./" $zone_full_path
sed -i .bak "s/$serial/$newserial/g" $zone_full_path
}
dns_local_add() {
fulldomain=$1
txtvalue=$2
# get zone file name
record_name=$(echo $fulldomain | cut -d '.' -f1)
fqdn=$(echo $fulldomain | cut -d '.' -f2,3)
zone_file=$zone_base_path$fqdn
_info "Let's update serial for $fqdn"
update_serial $fulldomain $zone_file
new_acme_record="$record_name IN TXT $txtvalue"
# replace of create new acme record
# old_acme_record=$(grep -E '^$record_name' $zone_file)
# if [ -z "$old_acme_record" ]; then
# # add new record
# echo $new_acme_record >> $zone_file
# else
# # replace record with hash
# sed -i .bak -E "s/^$record_name.+$/$new_acme_record/g" $zone_file
# fi
echo $new_acme_record >> $zone_file
if eval "nsd-control reload $fqdn"; then
_info "Successfully updated the zone $fqdn"
return 0
else
_err "Problem updating the zone $fqdn"
return 1
fi
nsd-control notify $fqdn
}
dns_local_rm() {
fulldomain=$1
txtvalue=$2
# remove record from zonefile
record_name=$(echo $fulldomain | cut -d '.' -f1)
fqdn=$(echo $fulldomain | cut -d '.' -f2,3)
zone_file=$zone_base_path$fqdn
update_serial $fulldomain $zone_file
sed -i bak "/$txtvalue/d" $zone_file
nsd-control reload $fqdn
nsd-control notify $fqdn
}
@ip75
Copy link
Author

ip75 commented Apr 27, 2024

update ssl certificates automatically

  • nsd as a DNS authority
  • crontab to update periodically

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