Skip to content

Instantly share code, notes, and snippets.

@pederkl
Created November 7, 2014 20:51
Show Gist options
  • Save pederkl/f2d6306308d5a877869c to your computer and use it in GitHub Desktop.
Save pederkl/f2d6306308d5a877869c to your computer and use it in GitHub Desktop.
How to update a dynamic DNS zone in Synology DSM with nsupdate directly, without going through the various web services.
#!/bin/sh
# Copyright (c) 2014, Peder O. Klingenberg
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
##########
# This is a script to allow direct use of nsupdate to update a dynamic
# DNS zone from Synology DSM. Not supported or endorsed by Synology
# Inc. Developed on DSM 5.0, no guarantees on earlier or later
# versions. IPv4 only, because that's all I use.
# Instructions for use:
#
# Prerequisites:
# 1) Set up ssh access as root to your DiskStation.
# 2) Install the DNS Server package. This provides the necessary
# nsupdate binary and a convenient way to store keys. You don't
# need to set up any zones, and unless importing new keys, the
# DNS server doesn't need to run.
#
# Setup (this is the tricky bit, requiring shell use and file
# editing):
# 1) Drop this script somewhere convenient on the DS, for instance
# /volume1/DS-hacks/ddns_nsupdate.sh, and make sure it's executable.
# 2) Log on to the DS as root, and append the following to two files,
# which should already exist:
# /etc.default/ddns_provider.conf and
# /etc/ddns_provider.conf
# (It doesn't seem like the latter matters, but it seems they are
# equal originally, so I kept them equal). Anyway, append this,
# adjusting path as necessary:
# [Direct NSUpdate]
# modulepath=/volume1/DS-hacks/ddns_nsupdate.sh
# queryurl=Direct_NSupdate
#
# Use:
# 1) Import the key to use for updates to the Synology DNS Server
# using the DNS Server GUI, tab "Keys", button "create" ->
# "import". This will give you a key of the same name as your
# dynamic domain, with a trailing dot.
# 2) Go to Control Panel - section "External Access", tab "DDNS".
# Click "Add".
# 3) "Direct NSUpdate" should now be a choice of service provider.
# Choose it.
# 4) "Hostname" is your desired name in the dynamic zone. Not a FQDN.
# 5) "Username/Email". We repurpose this field for the name of the
# dynamic zone. So enter your zone here, no trailing dot.
# 6) Enter whatever. We ignore it.
# 7) "External address" is fairly self-explanatory, and pre-filled in
# my GUI.
# 8) Click "Test Connection" or "OK". Status should flip to "Normal"
# after a while, and your DNS server should now answer requests for
# <yourhost>.<dynamic-domain>. You're done. If it says something
# else, check your upstream logs and start debugging.
##########
## Code follows
# Arguments as specified in /etc.default/ddns_provider.conf:
username=$1
password=$2 # We ignore this.
hostname=$3
ip=$4
# But we instead use
domain=$username
keyfile=/volume1/@appstore/DNSServer/named/etc/key/$domain.
if [ ! -r $keyfile ]; then
echo badauth
exit 1
fi
/volume1/@appstore/DNSServer/bin/nsupdate -k $keyfile <<EOF
zone $domain
update delete ${hostname}.${domain}. A
update add ${hostname}.${domain}. 600 A $ip
send
EOF
rc=$?
if [ $rc != 0 ]; then
echo 911
exit 2
fi
echo good
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment