Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # gratisdns.sh - A GratisDNS.dk DDNSd module for Synology DSM
- #
- # Author:
- # Brian Schmidt Pedersen (http://briped.net)
- #
- # Version:
- # 1.0.0
- #
- # Description:
- # This is a custom DDNSd module, for use with my Synology DiskStation. It
- # allows me to use the DSM interface for GratisDNS.dk DDNS service.
- #
- # To use this script, put it in the "/sbin" folder of the DiskStation,
- # i.e. "/sbin/gratisdns.sh"
- #
- # Then edit the "/etc.defaults/ddnsd_provider.conf" file and add the
- # following section at the end of that file:
- #
- # [GratisDNS.dk]
- # modulepath=/sbin/gratisdns.sh
- # queryurl=https://ssl.gratisdns.dk/ddns.phtml?u=__USERNAME__&p=__PASSWORD__&d=__DOMAIN__&h=__HOSTNAME__&i=__MYIP__
- #
- #
- # Changelist:
- # 1.0.0
- # + Release
- #
- # Todo:
- # Add support for multiple DDNS hosts, either through a conf file, or
- # through a delimited string from DSM. Only with same admin user though.
- # F.ex.: sub1.example.com|sub2.example.com
- #
- # Specify the target interface. This would probably be a hack that won't
- # be visible through DSM, since it appears DSM only uses the LAN1/eth0
- # interface for DDNS.
- #
- # Convert the update code block to a function. That way it's simple to
- # call the function on each iteration of a loop containing hostnames that
- # should be updated.
- #
- ###############################################################################
- # Set variables based on parameters.
- __USERNAME__="$(echo ${@} | cut -d' ' -f1)"
- __PASSWORD__="$(echo ${@} | cut -d' ' -f2)"
- __HOSTNAME__="$(echo ${@} | cut -d' ' -f3)"
- __DOMAIN__="$(echo ${__HOSTNAME__} | cut -d. -f2-3)"
- __MYIP__="$(echo ${@} | cut -d' ' -f4)"
- # System Configuration
- __IP_CACHE__=/tmp/gratisdns_ddnsd_ip.txt
- __LOGFILE__=/var/log/gratisdns_ddnsd.log
- log() {
- __LOGTIME__=$(date +"%b %e %T")
- if [ "${#}" -lt 1 ]; then
- false
- else
- __LOGMSG__="${1}"
- fi
- if [ "${#}" -lt 2 ]; then
- __LOGPRIO__=7
- else
- __LOGPRIO__=${2}
- fi
- #logger -p ${__LOGPRIO__} -t "$(basename ${0})" "${__LOGMSG__}"
- echo "${__LOGTIME__} $(basename ${0}) (${__LOGPRIO__}): ${__LOGMSG__}" >> ${__LOGFILE__}
- }
- # Get the last known DDNS IP
- if [ ! -f ${__IP_CACHE__} ]; then
- # If the file wasn't found, create it to avoid errors.
- echo "127.0.0.1" > ${__IP_CACHE__}
- fi
- __OLDIP__=$(cat ${__IP_CACHE__})
- # Get the current IP
- #__MYIP__=$(curl --silent --interface ${__INTERFACE__} "http://automation.whatismyip.com/n09230945.asp")
- if [ "${__OLDIP__}" == "${__MYIP__}" ]; then
- log "IP not changed. ${__MYIP__}. Not updating." 6
- else
- log "IP changed. ${__OLDIP__} > ${__MYIP__}. Attempting to update DNS." 6
- __URL__="https://ssl.gratisdns.dk/ddns.phtml?u=${__USERNAME__}&p=${__PASSWORD__}&d=${__DOMAIN__}&h=${__HOSTNAME__}&i=${__MYIP__}"
- # The cURL query needs the --insecure option, or the update will fail. I'm
- # assuming this is because there's no CA bundle with cURL in DSM.
- # I could of course add a check to see whether or not there's a local CA
- # bundle, and if it's "out of date", then download one and point to it.
- #
- # Set the location of the CA bundle:
- #__CABUNDLE__="/tmp/cacert.pem"
- #
- # If the CA bundle is more than a week old, fetch a fresh copy:
- #if [ some date comparison here ]; then
- # curl --silent --continue-at - --output "${__CABUNDLE__}" "http://curl.haxx.se/ca/cacert.pem"
- #fi
- #
- # Update DNS record:
- #__STATUS__=$(curl --silent --cacert "__CABUNDLE__" "${__URL__}")
- __STATUS__=$(curl --silent --insecure "${__URL__}")
- # Output:
- # When you write your own module, you can use the following words to tell user what happen by print it.
- # You can use your own message, but there is no multiple-language support.
- #
- # good - Update successfully.
- # nochg - Update successfully but the IP address have not changed.
- # nohost - The hostname specified does not exist in this user account.
- # abuse - The hostname specified is blocked for update abuse.
- # notfqdn - The hostname specified is not a fully-qualified domain name.
- # badauth - Authenticate failed.
- # 911 - There is a problem or scheduled maintenance on provider side
- # badagent - The user agent sent bad request(like HTTP method/parameters is not permitted)
- # badresolv - Failed to connect to because failed to resolve provider address.
- # badconn - Failed to connect to provider because connection timeout.
- case ${__STATUS__} in
- 'OK<br>')
- echo ${__MYIP__} > ${__IP_CACHE__}
- __OUTPUT__='good'
- ;;
- 'OK<br>Opdateret i forvejen')
- __OUTPUT__='nochg'
- ;;
- 'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.'|'Domæne kan IKKE administreres af bruger')
- __OUTPUT__='nohost'
- ;;
- 'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.Hostnavn er ulovligt.')
- __OUTPUT__='notfqdn'
- ;;
- 'Bruger login: 1Fejl i kodeord, prøv igen. Husk serveren ser forskel på STORE Og små BOGstAvER.'|'Bruger login: Bruger eksistere ikke, husk serveren ser forskel på STORE Og smÅ BOGstAvER.')
- __OUTPUT__='badauth'
- ;;
- 'Bruger login: MD5 invalid') # The error from the provider doesn't really match up with the response I forward to DDNSd. But it seems that a malformed URL, f.ex. if the URL isn't enclosed in quotes or contains spaces etc. will give this error.
- __OUTPUT__='badagent'
- ;;
- *)
- __OUTPUT__="${__STATUS__}"
- ;;
- esac
- log "DDNS Response: ${__OUTPUT__}" 6
- printf "%s" "${__OUTPUT__}"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement