Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active May 9, 2019 04:51
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 AfroThundr3007730/e57952bd6625553a59ab5efc5761f40f to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/e57952bd6625553a59ab5efc5761f40f to your computer and use it in GitHub Desktop.
Certbot hook to update a DDNS zone using nsupdate
#!/bin/bash
# Pre- and post-hook for certbot dns-01 to use DDNS
# Where we put our TSIG for dynamic udpates
TSIG=/etc/letsencrypt/acme.key
# We need the authoritative nameserver
NS="dig +short $(host -v $CERTBOT_DOMAIN | awk '/SOA/ {print $5}' | head -1)"
# The actual challenge record to use
RECORD="_acme-challenge.${CERTBOT_DOMAIN}"
# Create the challenge record with TTL of 60s
add_challenge=$(cat <<EOF
server $NS
update add $RECORD 60 TXT $CERTBOT_VALIDATION
send
EOF
)
# Remove all existing challenge records
clean_challenge=$(cat <<EOF
server $NS
prereq yxdomain $RECORD
update delete $RECORD
send
EOF
)
# If this is null, we are running in auth mode
[[ -n CERTBOT_AUTH_OUTPUT ]] || {{
echo 'Adding challenge records...'
nsupdate -k $TSIG <<< "$add_challenge"
}
# If not null, we are running in cleanup mode
[[ -n CERTBOT_AUTH_OUTPUT ]] && {{
echo 'Cleaning challenge records...'
nsupdate -k $TSIG <<< "$clean_challenge"
}
exit 0
@AfroThundr3007730
Copy link
Author

This is handy when your _acme-challenge record isn't in the same zone as your certificate domain, which was a scenario the certbot-dns-rfc2136 plugin only recently added support for.

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