lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

update_godaddy_com_v1.sh (5971B)


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