Advertisement
briped

GratisDNS.sh

Mar 6th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # gratisdns.sh - A GratisDNS.dk DDNSd module for Synology DSM
  4. #
  5. # Author:
  6. #       Brian Schmidt Pedersen (http://briped.net)
  7. #
  8. # Version:
  9. #       1.2.3
  10. #
  11. # Description:
  12. #       This is a custom DDNSd module, for use with my Synology DiskStation. It
  13. #       allows me to use the DSM interface for GratisDNS.dk DDNS service.
  14. #
  15. #       To use this script, put it in the "/sbin" folder of the DiskStation,
  16. #       i.e. "/sbin/gratisdns.sh"
  17. #
  18. #       Then edit the "/etc.defaults/ddnsd_provider.conf" file and add the
  19. #       following section at the end of that file:
  20. #
  21. #       [GratisDNS.dk]
  22. #               modulepath=/sbin/gratisdns.sh
  23. #               queryurl=https://ssl.gratisdns.dk/ddns.phtml?u=__USERNAME__&p=__PASSWORD__&d=__DOMAIN__&h=__HOSTNAME__&i=__MYIP__
  24. #
  25. #
  26. # Changelist:
  27. #       1.2.3
  28. #           * Fixed so the IP cache is also updated, even when DDNS provider
  29. #               responds with 'nochg', since obivously then the cached IP were
  30. #               wrong.
  31. #       1.2.2
  32. #           * Fixed some return code checking.
  33. #       1.2.1
  34. #           + Added more debug logging.
  35. #       1.2.0
  36. #           + Moved the entire update ddns routine into its own function.
  37. #           + Moved the CA bundle checking and updating into its own function.
  38. #       1.1.0
  39. #           + Changed the cURL update query to use CA bundle, for better
  40. #               security.
  41. #       1.0.0
  42. #           + Release.
  43. #
  44. # Todo:
  45. #       Add support for multiple DDNS hosts, either through a conf file, or
  46. #       through a delimited string from DSM. Only with same admin user though.
  47. #       F.ex.: sub1.example.com|sub2.example.com
  48. #
  49. #       Specify the target interface. This would probably be a hack that won't
  50. #       be visible through DSM, since it appears DSM only uses the LAN1/eth0
  51. #       interface for DDNS.
  52. #
  53. ###############################################################################
  54.  
  55. # Set variables based on parameters.
  56. __USERNAME__="$(echo ${@} | cut -d' ' -f1)"
  57. __PASSWORD__="$(echo ${@} | cut -d' ' -f2)"
  58. __HOSTNAME__="$(echo ${@} | cut -d' ' -f3)"
  59. __DOMAIN__="$(echo ${__HOSTNAME__} | cut -d. -f2-3)"
  60. __MYIP__="$(echo ${@}  | cut -d' ' -f4)"
  61.  
  62. # Where to store the IP used for the last DNS update:
  63. __IP_CACHE__="/tmp/gratisdns_ddnsd_ip.txt"
  64. # Where to store the logfile:
  65. __LOGFILE__="/var/log/gratisdns_ddnsd.log"
  66. # Where to store the CA bundle:
  67. __CA_BUNDLE__="/tmp/cacert.pem"
  68.  
  69. log() {
  70.     # Severity Levels:
  71.     # 0 Emergency       System is unusable.
  72.     #       A "panic" condition usually affecting multiple apps/servers/sites.
  73.     #       At this level it would usually notify all tech staff on call.
  74.     # 1 Alert           Action must be taken immediately.
  75.     #       Should be corrected immediately, therefore notify staff who can fix
  76.     #       the problem.An example would be the loss of a backup ISP connection
  77.     # 2 Critical        Critical conditions.
  78.     #       Should be corrected immediately, but indicates failure in a primary
  79.     #       system, an example is a loss of primary ISP connection.
  80.     # 3 Error           Error conditions.
  81.     #       Non-urgent failures, these should be relayed to developers or
  82.     #       admins; each item must be resolved within a given time.
  83.     # 4 Warning         Warning conditions.
  84.     #       Warning messages, not an error, but indication that an error will
  85.     #       occur if action is not taken, e.g. file system 85% full - each item
  86.     #       must be resolved within a given time.
  87.     # 5 Notice          Normal but significant condition.
  88.     #       Events that are unusual but not error conditions - might be
  89.     #       summarized in an email to developers or admins to spot potential
  90.     #       problems - no immediate action required.
  91.     # 6 Informational   Informational messages.
  92.     #       Normal operational messages - may be harvested for reporting,
  93.     #       measuring throughput, etc - no action required.
  94.     # 7 Debug           Debug-level messages.
  95.     #       Info useful to developers for debugging the application, not useful
  96.     #       during operations.
  97.     __LOGTIME__=$(date +"%b %e %T")
  98.     if [ "${#}" -lt 1 ]; then
  99.         false
  100.     else
  101.         __LOGMSG__="${1}"
  102.     fi
  103.     if [ "${#}" -lt 2 ]; then
  104.         __LOGPRIO__=7
  105.     else
  106.         __LOGPRIO__=${2}
  107.     fi
  108.  
  109.     logger -p ${__LOGPRIO__} -t "$(basename ${0})" "${__LOGMSG__}"
  110.     echo "${__LOGTIME__} $(basename ${0}) (${__LOGPRIO__}): ${__LOGMSG__}" >> ${__LOGFILE__}
  111. }
  112.  
  113. update_ca_bundle() {
  114.     if [ ! -z "${1}" ]; then
  115.         __CA_BUNDLE__="${1}"
  116.     else
  117.         __CA_BUNDLE__="${__CA_BUNDLE__}"
  118.     fi
  119.     log "update_ca_bundle(): __CA_BUNDLE__=${__CA_BUNDLE__}" 7
  120.     if [ ! -z "{2}" ]; then
  121.         __MAXAGE__="${2}"
  122.     else
  123.         __MAXAGE__="7"
  124.     fi
  125.     log "update_ca_bundle(): __MAXAGE__=${__MAXAGE}" 7
  126.     if [ test $(find "${__CA_BUNDLE__}" -mtime +${__MAXAGE__}) -o ! -f "${__CA_BUNDLE__}" ]; then
  127.         log "update_ca_bundle(): Local CA bundle older than ${__MAXAGE__} days, or doesn't exist. Attempting to download fresh copy." 6
  128.         curl --silent --output "${__CA_BUNDLE__}" "http://curl.haxx.se/ca/cacert.pem"
  129.         if [ "${?}" -ne "0" ]; then
  130.             log "update_ca_bundle: cURL returned error code ${?} while trying to download CA bundle." 4
  131.         fi
  132.     fi
  133. }
  134.  
  135. update_ddns() {
  136.     if [ -z "${1}" ]; then
  137.         log "update_ddns(): Missing Argument. URL string required." 3
  138.         false
  139.     else
  140.         __URL__="${1}"
  141.     fi
  142.  
  143.     # Update DNS record:
  144.     if [ -f "${__CA_BUNDLE__}" ]; then
  145.         log "update_ddns(): Updating using --cacert \"${__CA_BUNDLE__}\"." 7
  146.         __RESPONSE__=$(curl --silent --cacert "${__CA_BUNDLE__}" "${__URL__}")
  147.     else
  148.         log "update_ddns(): Updating using --insecure." 7
  149.         __RESPONSE__=$(curl --silent --insecure "${__URL__}")
  150.     fi
  151.     if [ "${?}" -ne "0" ]; then
  152.         log "update_ddns(): cURL returned error code ${?} while trying to update DDNS." 3
  153.         false
  154.     fi
  155.     log "update_ddns(): __RESPONSE__=${__RESPONSE__}" 7
  156.  
  157.     # Output:
  158.     #    When you write your own module, you can use the following words to tell user what happen by print it.
  159.     #    You can use your own message, but there is no multiple-language support.
  160.     #
  161.     #       good -  Update successfully.
  162.     #       nochg - Update successfully but the IP address have not changed.
  163.     #       nohost - The hostname specified does not exist in this user account.
  164.     #       abuse - The hostname specified is blocked for update abuse.
  165.     #       notfqdn - The hostname specified is not a fully-qualified domain name.
  166.     #       badauth - Authenticate failed.
  167.     #       911 - There is a problem or scheduled maintenance on provider side
  168.     #       badagent - The user agent sent bad request(like HTTP method/parameters is not permitted)
  169.     #       badresolv - Failed to connect to  because failed to resolve provider address.
  170.     #       badconn - Failed to connect to provider because connection timeout.
  171.     case ${__RESPONSE__} in
  172.         'OK<br>')
  173.             echo ${__MYIP__} > ${__IP_CACHE__}
  174.             __STATUS__='good'
  175.             true
  176.             ;;
  177.         'OK<br>Opdateret i forvejen')
  178.             echo ${__MYIP__} > ${__IP_CACHE__}
  179.             __STATUS__='nochg'
  180.             true
  181.             ;;
  182.         'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.'|'Domæne kan IKKE administreres af bruger')
  183.             __STATUS__='nohost'
  184.             false
  185.             ;;
  186.         'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.Hostnavn er ulovligt.')
  187.             __STATUS__='notfqdn'
  188.             false
  189.             ;;
  190.         '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.')
  191.             __STATUS__='badauth'
  192.             false
  193.             ;;
  194.         'Bruger login: MD5 invalid')
  195.             # The error from the provider doesn't really match up with the
  196.             # status I forward to DDNSd. But it seems that a malformed URL,
  197.             # such as if the URL isn't enclosed in quotes or contains spaces
  198.             # etc. will give this error.
  199.             __STATUS__='badagent'
  200.             false
  201.             ;;
  202.         *)
  203.             __STATUS__="${__RESPONSE__}"
  204.             false
  205.             ;;
  206.     esac
  207.     log "DDNSd Status: ${__STATUS__}" 6
  208. }
  209.  
  210. # Get the current IP
  211. #__INTERFACE_IP__="$(ifconfig ${__INTERFACE__} | sed '/inet\ /!d;s/.*r://g;s/\ .*//g')"
  212. #__MYIP__=$(curl --silent --interface ${__INTERFACE__} "http://automation.whatismyip.com/n09230945.asp")
  213.  
  214. # Get the last known DDNS IP
  215. if [ ! -f ${__IP_CACHE__} ]; then
  216.     # If the file wasn't found, create it to avoid errors.
  217.     echo "127.0.0.1" > ${__IP_CACHE__}
  218. fi
  219. __OLDIP__=$(cat ${__IP_CACHE__})
  220.  
  221. if [ "${__OLDIP__}" == "${__MYIP__}" ]; then
  222.     log "IP not changed. ${__MYIP__}. Not updating." 6
  223. else
  224.     update_ca_bundle "${__CA_BUNDLE__}" "7"
  225.  
  226.     __URL__="https://ssl.gratisdns.dk/ddns.phtml?u=${__USERNAME__}&p=${__PASSWORD__}&d=${__DOMAIN__}&h=${__HOSTNAME__}&i=${__MYIP__}"
  227.  
  228.     # Set the response and status variables, so they're available globally.
  229.     # That way I can read them outside the update_ddns() function.
  230.     __RESPONSE__="No response returned."
  231.     __STATUS__="No status returned."
  232.  
  233.     log "IP changed. ${__OLDIP__} > ${__MYIP__}. Attempting to update DNS." 6
  234.     update_ddns "${__URL__}"
  235.     if [ "${?}" -ne "0" ]; then
  236.         log "update_dns(): ${__RESPONSE__}" 3
  237.     else
  238.         log "update_dns(): ${__RESPONSE__}" 6
  239.     fi
  240.     printf "%s" "${__STATUS__}"
  241. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement