update_godaddy_com_v1.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/sh
  2. #
  3. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  4. #
  5. # script for sending updates to godaddy.com
  6. #.based on GoDaddy.sh v1.0 by Nazar78 @ TeaNazaR.com
  7. #.2017 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  8. # GoDaddy Documentation at https://developer.godaddy.com/doc
  9. #
  10. # This script is parsed by dynamic_dns_functions.sh inside send_update() function
  11. #
  12. # using following options from /etc/config/ddns
  13. # option username - "key" as generated at https://developer.godaddy.com/keys/
  14. # option password - "secret" as generated at https://developer.godaddy.com/keys/
  15. # option domain - "yourdomain.TLD" to update or "hostname@yourdomain.TLD"
  16. #
  17. # variable __IP already defined with the ip-address to use for update
  18. #
  19. # check parameters
  20. [ -z "$CURL_SSL" ] && write_log 14 "GoDaddy communication require cURL with SSL support. Please install"
  21. [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing key as 'username'"
  22. [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing secret as 'password'"
  23. [ $use_https -eq 0 ] && use_https=1 # force HTTPS
  24. # used variables
  25. local __HOST __DOMAIN __TYPE __URL __PRGBASE __RUNPROG __DATA __IPV6
  26. # split __HOST __DOMAIN from $domain
  27. # given data:
  28. # @example.com for "domain record"
  29. # host.sub@example.com for a "host record"
  30. __HOST=$(printf %s "$domain" | cut -d@ -f1)
  31. __DOMAIN=$(printf %s "$domain" | cut -d@ -f2)
  32. # GoDaddy needs:
  33. # __DOMAIN = the base domain i.e. example.com
  34. # __HOST = host.sub if updating a host record or
  35. # __HOST = "@" urlencoded "%40" for a domain record
  36. [ -z "$__HOST" -o "$__HOST" = "$__DOMAIN" ] && __HOST="%40"
  37. # set record type
  38. [ $use_ipv6 -eq 0 ] && __TYPE="A" || __TYPE="AAAA"
  39. # now we know the url to use
  40. # __URL="https://api.ote-godaddy.com/v1/domains/$__DOMAIN/records/$__TYPE/$__HOST" # api test server
  41. __URL="https://api.godaddy.com/v1/domains/$__DOMAIN/records/$__TYPE/$__HOST" # production server
  42. # transfer function to use for godaddy
  43. # all needed variables are set global here
  44. # so we can use them directly
  45. godaddy_transfer() {
  46. local __CNT=0
  47. local __STATUS __ERR __DEVICE
  48. while : ; do
  49. write_log 7 "#> $__RUNPROG"
  50. __STATUS=$(eval "$__RUNPROG")
  51. __ERR=$? # save communication error
  52. [ $__ERR -eq 0 ] && break # no error break while
  53. write_log 4 "cURL error: '$__ERR'"
  54. write_log 7 "$(cat $ERRFILE)" # report error
  55. [ $VERBOSE_MODE -gt 1 ] && {
  56. # VERBOSE_MODE > 1 then NO retry
  57. write_log 4 "Transfer failed - Verbose Mode: $VERBOSE_MODE - NO retry on error"
  58. break
  59. }
  60. __CNT=$(( $__CNT + 1 )) # increment error counter
  61. # if error count > retry_count leave here
  62. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  63. write_log 14 "Transfer failed after $retry_count retries"
  64. write_log 4 "Transfer failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  65. sleep $RETRY_SECONDS &
  66. PID_SLEEP=$!
  67. wait $PID_SLEEP # enable trap-handler
  68. PID_SLEEP=0
  69. done
  70. # handle HTTP error
  71. [ $__STATUS -ne 200 ] && {
  72. write_log 4 "GoDaddy reported an error:"
  73. write_log 7 "$(cat $DATFILE)"
  74. return 1
  75. }
  76. }
  77. # Build base command to use
  78. __PRGBASE="$CURL -RsS -w '%{http_code}' -o $DATFILE --stderr $ERRFILE"
  79. # force network/interface-device to use for communication
  80. if [ -n "$bind_network" ]; then
  81. local __DEVICE
  82. network_get_physdev __DEVICE $bind_network || \
  83. write_log 13 "Can not detect local device using 'network_get_physdev $bind_network' - Error: '$?'"
  84. write_log 7 "Force communication via device '$__DEVICE'"
  85. __PRGBASE="$__PRGBASE --interface $__DEVICE"
  86. fi
  87. # force ip version to use
  88. if [ $force_ipversion -eq 1 ]; then
  89. [ $use_ipv6 -eq 0 ] && __PRGBASE="$__PRGBASE -4" || __PRGBASE="$__PRGBASE -6" # force IPv4/IPv6
  90. fi
  91. # set certificate parameters
  92. if [ "$cacert" = "IGNORE" ]; then # idea from Ticket #15327 to ignore server cert
  93. __PRGBASE="$__PRGBASE --insecure" # but not empty better to use "IGNORE"
  94. elif [ -f "$cacert" ]; then
  95. __PRGBASE="$__PRGBASE --cacert $cacert"
  96. elif [ -d "$cacert" ]; then
  97. __PRGBASE="$__PRGBASE --capath $cacert"
  98. elif [ -n "$cacert" ]; then # it's not a file and not a directory but given
  99. write_log 14 "No valid certificate(s) found at '$cacert' for HTTPS communication"
  100. fi
  101. # disable proxy if no set (there might be .wgetrc or .curlrc or wrong environment set)
  102. # or check if libcurl compiled with proxy support
  103. if [ -z "$proxy" ]; then
  104. __PRGBASE="$__PRGBASE --noproxy '*'"
  105. elif [ -z "$CURL_PROXY" ]; then
  106. # if libcurl has no proxy support and proxy should be used then force ERROR
  107. write_log 13 "cURL: libcurl compiled without Proxy support"
  108. fi
  109. # set headers
  110. __PRGBASE="$__PRGBASE --header 'Authorization: sso-key $username:$password' "
  111. __PRGBASE="$__PRGBASE --header 'Accept: application/json' "
  112. __PRGBASE="$__PRGBASE --header 'Content-Type: application/json; charset=utf-8' "
  113. # read data from godaddy.com
  114. __RUNPROG="$__PRGBASE --request GET $__URL"
  115. godaddy_transfer || return 1
  116. # HTTP 200 OK, now analyse data and check if update needed
  117. __DATA=$(sed -r 's/.+data":"(.+)","t.+/\1/g' $DATFILE)
  118. # check data
  119. [ $use_ipv6 -eq 0 ] \
  120. && __DATA=$(printf "%s" "$__DATA" | grep -m 1 -o "$IPV4_REGEX") \
  121. || __DATA=$(printf "%s" "$__DATA" | grep -m 1 -o "$IPV6_REGEX")
  122. # we got data so verify
  123. [ -n "$__DATA" ] && {
  124. # expand IPv6 for compare
  125. if [ $use_ipv6 -eq 1 ]; then
  126. expand_ipv6 $__IP __IPV6
  127. expand_ipv6 $__DATA __DATA
  128. [ "$__DATA" = "$__IPV6" ] && { # IPv6 no update needed
  129. write_log 7 "IPv6 at GoDaddy.com already up to date"
  130. return 0
  131. }
  132. else
  133. [ "$__DATA" = "$__IP" ] && { # IPv4 no update needed
  134. write_log 7 "IPv4 at GoDaddy.com already up to date"
  135. return 0
  136. }
  137. fi
  138. }
  139. # update is needed
  140. # let's build data to send,
  141. # use file to work around double quotes '"' needed for json
  142. cat > $DATFILE << EOF
  143. [{"data":"$__IP"}]
  144. EOF
  145. # let's complete transfer command
  146. __RUNPROG="$__PRGBASE --request PUT --data @$DATFILE $__URL"
  147. godaddy_transfer || return 1
  148. # HTTP 200 OK
  149. return 0