Advertisement
briped

GratisDNS.sh

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