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