You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.0 KiB

  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 cloudflare.com
  6. #.based on Ben Kulbertis cloudflare-update-record.sh found at http://gist.github.com/benkulbertis
  7. #.and on George Johnson's cf-ddns.sh found at https://github.com/gstuartj/cf-ddns.sh
  8. #.2016-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  9. # CloudFlare API documentation at https://api.cloudflare.com/
  10. #
  11. # This script is parsed by dynamic_dns_functions.sh inside send_update() function
  12. #
  13. # using following options from /etc/config/ddns
  14. # option username - your cloudflare e-mail
  15. # option password - cloudflare api key, you can get it from cloudflare.com/my-account/
  16. # option domain - "hostname@yourdomain.TLD" # syntax changed to remove split_FQDN() function and tld_names.dat.gz
  17. #
  18. # The proxy status would not be changed by this script. Please change it in Cloudflare dashboard manually.
  19. #
  20. # variable __IP already defined with the ip-address to use for update
  21. #
  22. # check parameters
  23. [ -z "$CURL" ] && [ -z "$CURL_SSL" ] && write_log 14 "Cloudflare communication require cURL with SSL support. Please install"
  24. [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing key as 'username'"
  25. [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing secret as 'password'"
  26. [ $use_https -eq 0 ] && use_https=1 # force HTTPS
  27. # used variables
  28. local __HOST __DOMAIN __TYPE __URLBASE __PRGBASE __RUNPROG __DATA __IPV6 __ZONEID __RECID __PROXIED
  29. local __URLBASE="https://api.cloudflare.com/client/v4"
  30. # split __HOST __DOMAIN from $domain
  31. # given data:
  32. # @example.com for "domain record"
  33. # host.sub@example.com for a "host record"
  34. __HOST=$(printf %s "$domain" | cut -d@ -f1)
  35. __DOMAIN=$(printf %s "$domain" | cut -d@ -f2)
  36. # Cloudflare v4 needs:
  37. # __DOMAIN = the base domain i.e. example.com
  38. # __HOST = the FQDN of record to modify
  39. # i.e. example.com for the "domain record" or host.sub.example.com for "host record"
  40. # handling domain record then set __HOST = __DOMAIN
  41. [ -z "$__HOST" ] && __HOST=$__DOMAIN
  42. # handling host record then rebuild fqdn host@domain.tld => host.domain.tld
  43. [ "$__HOST" != "$__DOMAIN" ] && __HOST="${__HOST}.${__DOMAIN}"
  44. # set record type
  45. [ $use_ipv6 -eq 0 ] && __TYPE="A" || __TYPE="AAAA"
  46. # transfer function to use for godaddy
  47. # all needed variables are set global here
  48. # so we can use them directly
  49. cloudflare_transfer() {
  50. local __CNT=0
  51. local __ERR
  52. while : ; do
  53. write_log 7 "#> $__RUNPROG"
  54. eval "$__RUNPROG"
  55. __ERR=$? # save communication error
  56. [ $__ERR -eq 0 ] && break # no error break while
  57. write_log 3 "cURL Error: '$__ERR'"
  58. write_log 7 "$(cat $ERRFILE)" # report error
  59. [ $VERBOSE_MODE -gt 1 ] && {
  60. # VERBOSE_MODE > 1 then NO retry
  61. write_log 4 "Transfer failed - Verbose Mode: $VERBOSE_MODE - NO retry on error"
  62. break
  63. }
  64. __CNT=$(( $__CNT + 1 )) # increment error counter
  65. # if error count > retry_count leave here
  66. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  67. write_log 14 "Transfer failed after $retry_count retries"
  68. write_log 4 "Transfer failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  69. sleep $RETRY_SECONDS &
  70. PID_SLEEP=$!
  71. wait $PID_SLEEP # enable trap-handler
  72. PID_SLEEP=0
  73. done
  74. # check for error
  75. grep -q '"success":true' $DATFILE || {
  76. write_log 4 "CloudFlare reported an error:"
  77. write_log 7 "$(cat $DATFILE)" # report error
  78. return 1 # HTTP-Fehler
  79. }
  80. }
  81. # Build base command to use
  82. __PRGBASE="$CURL -RsS -o $DATFILE --stderr $ERRFILE"
  83. # force network/interface-device to use for communication
  84. if [ -n "$bind_network" ]; then
  85. local __DEVICE
  86. network_get_physdev __DEVICE $bind_network || \
  87. write_log 13 "Can not detect local device using 'network_get_physdev $bind_network' - Error: '$?'"
  88. write_log 7 "Force communication via device '$__DEVICE'"
  89. __PRGBASE="$__PRGBASE --interface $__DEVICE"
  90. fi
  91. # force ip version to use
  92. if [ $force_ipversion -eq 1 ]; then
  93. [ $use_ipv6 -eq 0 ] && __PRGBASE="$__PRGBASE -4" || __PRGBASE="$__PRGBASE -6" # force IPv4/IPv6
  94. fi
  95. # set certificate parameters
  96. if [ "$cacert" = "IGNORE" ]; then # idea from Ticket #15327 to ignore server cert
  97. __PRGBASE="$__PRGBASE --insecure" # but not empty better to use "IGNORE"
  98. elif [ -f "$cacert" ]; then
  99. __PRGBASE="$__PRGBASE --cacert $cacert"
  100. elif [ -d "$cacert" ]; then
  101. __PRGBASE="$__PRGBASE --capath $cacert"
  102. elif [ -n "$cacert" ]; then # it's not a file and not a directory but given
  103. write_log 14 "No valid certificate(s) found at '$cacert' for HTTPS communication"
  104. fi
  105. # disable proxy if not set (there might be .wgetrc or .curlrc or wrong environment set)
  106. # or check if libcurl compiled with proxy support
  107. if [ -z "$proxy" ]; then
  108. __PRGBASE="$__PRGBASE --noproxy '*'"
  109. elif [ -z "$CURL_PROXY" ]; then
  110. # if libcurl has no proxy support and proxy should be used then force ERROR
  111. write_log 13 "cURL: libcurl compiled without Proxy support"
  112. fi
  113. # set headers
  114. __PRGBASE="$__PRGBASE --header 'X-Auth-Email: $username' "
  115. __PRGBASE="$__PRGBASE --header 'X-Auth-Key: $password' "
  116. __PRGBASE="$__PRGBASE --header 'Content-Type: application/json' "
  117. # __PRGBASE="$__PRGBASE --header 'Accept: application/json' "
  118. # read zone id for registered domain.TLD
  119. __RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones?name=$__DOMAIN'"
  120. cloudflare_transfer || return 1
  121. # extract zone id
  122. __ZONEID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
  123. [ -z "$__ZONEID" ] && {
  124. write_log 4 "Could not detect 'zone id' for domain.tld: '$__DOMAIN'"
  125. return 127
  126. }
  127. # read record id for A or AAAA record of host.domain.TLD
  128. __RUNPROG="$__PRGBASE --request GET '$__URLBASE/zones/$__ZONEID/dns_records?name=$__HOST&type=$__TYPE'"
  129. cloudflare_transfer || return 1
  130. # extract record id
  131. __RECID=$(grep -o '"id":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
  132. [ -z "$__RECID" ] && {
  133. write_log 4 "Could not detect 'record id' for host.domain.tld: '$__HOST'"
  134. return 127
  135. }
  136. # extract current stored IP
  137. __DATA=$(grep -o '"content":"[^"]*' $DATFILE | grep -o '[^"]*$' | head -1)
  138. # check data
  139. [ $use_ipv6 -eq 0 ] \
  140. && __DATA=$(printf "%s" "$__DATA" | grep -m 1 -o "$IPV4_REGEX") \
  141. || __DATA=$(printf "%s" "$__DATA" | grep -m 1 -o "$IPV6_REGEX")
  142. # we got data so verify
  143. [ -n "$__DATA" ] && {
  144. # expand IPv6 for compare
  145. if [ $use_ipv6 -eq 1 ]; then
  146. expand_ipv6 $__IP __IPV6
  147. expand_ipv6 $__DATA __DATA
  148. [ "$__DATA" = "$__IPV6" ] && { # IPv6 no update needed
  149. write_log 7 "IPv6 at CloudFlare.com already up to date"
  150. return 0
  151. }
  152. else
  153. [ "$__DATA" = "$__IP" ] && { # IPv4 no update needed
  154. write_log 7 "IPv4 at CloudFlare.com already up to date"
  155. return 0
  156. }
  157. fi
  158. }
  159. # update is needed
  160. # let's build data to send
  161. # set proxied parameter
  162. __PROXIED=$(grep -o '"proxied":[^",]*' $DATFILE | grep -o '[^:]*$')
  163. # use file to work around " needed for json
  164. cat > $DATFILE << EOF
  165. {"id":"$__ZONEID","type":"$__TYPE","name":"$__HOST","content":"$__IP","proxied":$__PROXIED}
  166. EOF
  167. # let's complete transfer command
  168. __RUNPROG="$__PRGBASE --request PUT --data @$DATFILE '$__URLBASE/zones/$__ZONEID/dns_records/$__RECID'"
  169. cloudflare_transfer || return 1
  170. return 0