Advertisement
briped

GratisDNS.sh

Mar 3rd, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. # gratisdns.sh - A GratisDNS.dk DDNSd module for Synology DSM
  3. #
  4. # Author:
  5. #       Brian Schmidt Pedersen (http://briped.net)
  6. #
  7. # Version:
  8. #       1.0.0
  9. #
  10. # Description:
  11. #       This is a custom DDNSd module, for use with my Synology DiskStation. It
  12. #       allows me to use the DSM interface for GratisDNS.dk DDNS service.
  13. #
  14. #       To use this script, put it in the "/sbin" folder of the DiskStation,
  15. #       i.e. "/sbin/gratisdns.sh"
  16. #
  17. #       Then edit the "/etc.defaults/ddnsd_provider.conf" file and add the
  18. #       following section at the end of that file:
  19. #
  20. #       [GratisDNS.dk]
  21. #               modulepath=/sbin/gratisdns.sh
  22. #               queryurl=https://ssl.gratisdns.dk/ddns.phtml?u=__USERNAME__&p=__PASSWORD__&d=__DOMAIN__&h=__HOSTNAME__&i=__MYIP__
  23. #
  24. #
  25. # Changelist:
  26. #       1.0.0
  27. #           + Release
  28. #
  29. # Todo:
  30. #       Add support for multiple DDNS hosts, either through a conf file, or
  31. #       through a delimited string from DSM. Only with same admin user though.
  32. #       F.ex.: sub1.example.com|sub2.example.com
  33. #
  34. #       Specify the target interface. This would probably be a hack that won't
  35. #       be visible through DSM, since it appears DSM only uses the LAN1/eth0
  36. #       interface for DDNS.
  37. #
  38. #       Convert the update code block to a function. That way it's simple to
  39. #       call the function on each iteration of a loop containing hostnames that
  40. #       should be updated.
  41. #
  42. ###############################################################################
  43.  
  44. # Set variables based on parameters.
  45. __USERNAME__="$(echo ${@} | cut -d' ' -f1)"
  46. __PASSWORD__="$(echo ${@} | cut -d' ' -f2)"
  47. __HOSTNAME__="$(echo ${@} | cut -d' ' -f3)"
  48. __DOMAIN__="$(echo ${__HOSTNAME__} | cut -d. -f2-3)"
  49. __MYIP__="$(echo ${@}  | cut -d' ' -f4)"
  50.  
  51. # System Configuration
  52. __IP_CACHE__=/tmp/gratisdns_ddnsd_ip.txt
  53. __LOGFILE__=/var/log/gratisdns_ddnsd.log
  54.  
  55.  
  56. log() {
  57.     __LOGTIME__=$(date +"%b %e %T")
  58.     if [ "${#}" -lt 1 ]; then
  59.         false
  60.     else
  61.         __LOGMSG__="${1}"
  62.     fi
  63.     if [ "${#}" -lt 2 ]; then
  64.         __LOGPRIO__=7
  65.     else
  66.         __LOGPRIO__=${2}
  67.     fi
  68.  
  69.     #logger -p ${__LOGPRIO__} -t "$(basename ${0})" "${__LOGMSG__}"
  70.     echo "${__LOGTIME__} $(basename ${0}) (${__LOGPRIO__}): ${__LOGMSG__}" >> ${__LOGFILE__}
  71. }
  72.  
  73. # Get the last known DDNS IP
  74. if [ ! -f ${__IP_CACHE__} ]; then
  75.     # If the file wasn't found, create it to avoid errors.
  76.     echo "127.0.0.1" > ${__IP_CACHE__}
  77. fi
  78. __OLDIP__=$(cat ${__IP_CACHE__})
  79.  
  80. # Get the current IP
  81. #__MYIP__=$(curl --silent --interface ${__INTERFACE__} "http://automation.whatismyip.com/n09230945.asp")
  82.  
  83. if [ "${__OLDIP__}" == "${__MYIP__}" ]; then
  84.     log "IP not changed. ${__MYIP__}. Not updating." 6
  85. else
  86.     log "IP changed. ${__OLDIP__} > ${__MYIP__}. Attempting to update DNS." 6
  87.  
  88.     __URL__="https://ssl.gratisdns.dk/ddns.phtml?u=${__USERNAME__}&p=${__PASSWORD__}&d=${__DOMAIN__}&h=${__HOSTNAME__}&i=${__MYIP__}"
  89.  
  90.     # The cURL query needs the --insecure option, or the update will fail. I'm
  91.     # assuming this is because there's no CA bundle with cURL in DSM.
  92.     # I could of course add a check to see whether or not there's a local CA
  93.     # bundle, and if it's "out of date", then download one and point to it.
  94.     #
  95.     # Set the location of the CA bundle:
  96.     #__CABUNDLE__="/tmp/cacert.pem"
  97.     #
  98.     # If the CA bundle is more than a week old, fetch a fresh copy:
  99.     #if [ some date comparison here ]; then
  100.     #   curl --silent --continue-at - --output "${__CABUNDLE__}" "http://curl.haxx.se/ca/cacert.pem"
  101.     #fi
  102.     #
  103.     # Update DNS record:
  104.     #__STATUS__=$(curl --silent --cacert "__CABUNDLE__" "${__URL__}")
  105.     __STATUS__=$(curl --silent --insecure "${__URL__}")
  106.  
  107.     # Output:
  108.     #    When you write your own module, you can use the following words to tell user what happen by print it.
  109.     #    You can use your own message, but there is no multiple-language support.
  110.     #
  111.     #       good -  Update successfully.
  112.     #       nochg - Update successfully but the IP address have not changed.
  113.     #       nohost - The hostname specified does not exist in this user account.
  114.     #       abuse - The hostname specified is blocked for update abuse.
  115.     #       notfqdn - The hostname specified is not a fully-qualified domain name.
  116.     #       badauth - Authenticate failed.
  117.     #       911 - There is a problem or scheduled maintenance on provider side
  118.     #       badagent - The user agent sent bad request(like HTTP method/parameters is not permitted)
  119.     #       badresolv - Failed to connect to  because failed to resolve provider address.
  120.     #       badconn - Failed to connect to provider because connection timeout.
  121.     case ${__STATUS__} in
  122.         'OK<br>')
  123.             echo ${__MYIP__} > ${__IP_CACHE__}
  124.             __OUTPUT__='good'
  125.             ;;
  126.         'OK<br>Opdateret i forvejen')
  127.             __OUTPUT__='nochg'
  128.             ;;
  129.         'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.'|'Domæne kan IKKE administreres af bruger')
  130.             __OUTPUT__='nohost'
  131.             ;;
  132.         'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.Hostnavn er ulovligt.')
  133.             __OUTPUT__='notfqdn'
  134.             ;;
  135.         '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.')
  136.             __OUTPUT__='badauth'
  137.             ;;
  138.         '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.
  139.             __OUTPUT__='badagent'
  140.             ;;
  141.         *)
  142.             __OUTPUT__="${__STATUS__}"
  143.             ;;
  144.     esac
  145.     log "DDNS Response: ${__OUTPUT__}" 6
  146.     printf "%s" "${__OUTPUT__}"
  147. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement