2 # /usr/lib/ddns/dynamic_dns_updater.sh
4 # Original written by Eric Paul Bishop, January 2008
5 # Distributed under the terms of the GNU General Public License (GPL) version 2.0
6 # (Loosely) based on the script on the one posted by exobyte in the forums here:
7 # http://forum.openwrt.org/viewtopic.php?id=14040
9 # extended and partial rewritten in August 2014
10 # by Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
12 # - IPv6 DDNS services
13 # - DNS Server to retrieve registered IP including TCP transport
14 # - Proxy Server to send out updates
15 # - force_interval=0 to run once
16 # - the usage of BIND's host command instead of BusyBox's nslookup if installed
17 # - extended Verbose Mode and log file support for better error detection
19 # variables in small chars are read from /etc/config/ddns
20 # variables in big chars are defined inside these scripts as global vars
21 # variables in big chars beginning with "__" are local defined inside functions only
22 #set -vx #script debugger
24 [ $# -lt 1 -o -n "${2//[0-3]/}" -o ${#2} -gt 1 ] && {
26 echo -e " $0 [SECTION] [VERBOSE_MODE]\n"
27 echo " [SECTION] - service section as defined in /etc/config/ddns"
28 echo " [VERBOSE_MODE] - '0' NO output to console"
29 echo " '1' output to console"
30 echo " '2' output to console AND logfile"
31 echo " + run once WITHOUT retry on error"
32 echo " '3' output to console AND logfile"
33 echo " + run once WITHOUT retry on error"
34 echo -e " + NOT sending update to DDNS service\n"
38 .
/usr
/lib
/ddns
/dynamic_dns_functions.sh
# global vars are also defined here
41 VERBOSE_MODE
=${2:-1} #default mode is log to console
44 PIDFILE
="$RUNDIR/$SECTION_ID.pid" # Process ID file
45 UPDFILE
="$RUNDIR/$SECTION_ID.update" # last update successful send (system uptime)
46 LOGFILE
="$LOGDIR/$SECTION_ID.log" # log file
48 # VERBOSE_MODE > 1 delete logfile if exist to create an empty one
49 # only with this data of this run for easier diagnostic
50 # new one created by verbose_echo function
51 [ $VERBOSE_MODE -gt 1 -a -f $LOGFILE ] && rm -f $LOGFILE
53 ################################################################################
54 # Leave this comment here, to clearly document variable names that are expected/possible
55 # Use load_all_config_options to load config options, which is a much more flexible solution.
58 # config_get <variable> $SECTION_ID <option]>
60 # defined options (also used as variable):
62 # enable self-explanatory
63 # interface network interface used by hotplug.d i.e. 'wan' or 'wan6'
65 # service_name Which DDNS service do you use or "custom"
66 # update_url URL to use to update your "custom" DDNS service
67 # update_script SCRIPT to use to update your "custom" DDNS service
69 # domain Your DNS name / replace [DOMAIN] in update_url
70 # username Username of your DDNS service account / replace [USERNAME] in update_url
71 # password Password of your DDNS service account / replace [PASSWORD] in update_url
73 # use_https use HTTPS to update DDNS service
74 # cacert file or directory where HTTPS can find certificates to verify server; 'IGNORE' ignore check of server certificate
76 # use_syslog log activity to syslog
78 # ip_source source to detect current local IP ('network' or 'web' or 'script' or 'interface')
79 # ip_network local defined network to read IP from i.e. 'wan' or 'wan6'
80 # ip_url URL to read local address from i.e. http://checkip.dyndns.com/ or http://checkipv6.dyndns.com/
81 # ip_script full path and name of your script to detect local IP
82 # ip_interface physical interface to use for detecting
84 # check_interval check for changes every !!! checks below 10 minutes make no sense because the Internet
85 # check_unit 'days' 'hours' 'minutes' !!! needs about 5-10 minutes to sync an IP-change for an DNS entry
87 # force_interval force to send an update to your service if no change was detected
88 # force_unit 'days' 'hours' 'minutes' !!! force_interval="0" runs this script once for use i.e. with cron
90 # retry_interval if error was detected retry in
91 # retry_unit 'days' 'hours' 'minutes' 'seconds'
92 # retry_count #NEW# number of retries before scripts stops
94 # use_ipv6 #NEW# detecting/sending IPv6 address
95 # force_ipversion #NEW# force usage of IPv4 or IPv6 for the whole detection and update communication
96 # dns_server #NEW# using a non default dns server to get Registered IP from Internet
97 # force_dnstcp #NEW# force communication with DNS server via TCP instead of default UDP
98 # proxy #NEW# using a proxy for communication !!! ALSO used to detect local IP via web => return proxy's IP !!!
99 # use_logfile #NEW# self-explanatory "/var/log/ddns/$SECTION_ID.log"
101 # some functionality needs
102 # - GNU Wget or cURL installed for sending updates to DDNS service
103 # - BIND host installed to detect Registered IP
105 ################################################################################
107 # verify and load SECTION_ID is exists
108 [ "$(uci_get ddns $SECTION_ID)" != "service" ] && {
109 [ $VERBOSE_MODE -le 1 ] && VERBOSE_MODE
=2 # force console out and logfile output
110 [ -f $LOGFILE ] && rm -f $LOGFILE # clear logfile before first entry
111 verbose_echo
"\n ************** =: ************** ************** **************"
112 verbose_echo
" STARTED =: PID '$$' at $(eval $DATE_PROG)"
113 verbose_echo
" UCI CONFIG =:\n$(uci -q show ddns | grep '=service' | sort)"
114 critical_error
"Service '$SECTION_ID' not defined"
116 load_all_config_options
"ddns" "$SECTION_ID"
118 verbose_echo
"\n ************** =: ************** ************** **************"
119 verbose_echo
" STARTED =: PID '$$' at $(eval $DATE_PROG)"
120 case $VERBOSE_MODE in
121 0) verbose_echo
" verbose mode =: '0' - run normal, NO console output";;
122 1) verbose_echo
" verbose mode =: '1' - run normal, console mode";;
123 2) verbose_echo
" verbose mode =: '2' - run once, NO retry on error";;
124 3) verbose_echo
" verbose mode =: '3' - run once, NO retry on error, NOT sending update";;
125 *) critical_error
"ERROR detecting VERBOSE_MODE '$VERBOSE_MODE'"
127 verbose_echo
" UCI CONFIG =:\n$(uci -q show ddns.$SECTION_ID | sort)"
129 # set defaults if not defined
130 [ -z "$enabled" ] && enabled
=0
131 [ -z "$retry_count" ] && retry_count
=5
132 [ -z "$use_syslog" ] && use_syslog
=0 # not use syslog
133 [ -z "$use_https" ] && use_https
=0 # not use https
134 [ -z "$use_logfile" ] && use_logfile
=1 # NEW - use logfile by default
135 [ -z "$use_ipv6" ] && use_ipv6
=0 # NEW - use IPv4 by default
136 [ -z "$force_ipversion" ] && force_ipversion
=0 # NEW - default let system decide
137 [ -z "$force_dnstcp" ] && force_dnstcp
=0 # NEW - default UDP
138 [ -z "$ip_source" ] && ip_source
="network"
139 [ "$ip_source" = "network" -a -z "$ip_network" -a $use_ipv6 -eq 0 ] && ip_network
="wan" # IPv4: default wan
140 [ "$ip_source" = "network" -a -z "$ip_network" -a $use_ipv6 -eq 1 ] && ip_network
="wan6" # IPv6: default wan6
141 [ "$ip_source" = "web" -a -z "$ip_url" -a $use_ipv6 -eq 0 ] && ip_url
="http://checkip.dyndns.com"
142 [ "$ip_source" = "web" -a -z "$ip_url" -a $use_ipv6 -eq 1 ] && ip_url
="http://checkipv6.dyndns.com"
143 [ "$ip_source" = "interface" -a -z "$ip_interface" ] && ip_interface
="eth1"
145 # check configuration and enabled state
146 [ -z "$domain" -o -z "$username" -o -z "$password" ] && critical_error
"Service Configuration not correctly configured"
147 [ $enabled -eq 0 ] && critical_error
"Service Configuration is disabled"
149 # verify script if configured and executable
150 if [ "$ip_source" = "script" ]; then
151 [ -z "$ip_script" ] && critical_error
"No script defined to detect local IP"
152 [ -x "$ip_script" ] || critical_error
"Script to detect local IP not found or not executable"
155 # compute update interval in seconds
156 get_seconds CHECK_SECONDS
${check_interval:-10} ${check_unit:-"minutes"} # default 10 min
157 get_seconds FORCE_SECONDS
${force_interval:-72} ${force_unit:-"hours"} # default 3 days
158 get_seconds RETRY_SECONDS
${retry_interval:-60} ${retry_unit:-"seconds"} # default 60 sec
159 verbose_echo
"check interval =: $CHECK_SECONDS seconds"
160 verbose_echo
"force interval =: $FORCE_SECONDS seconds"
161 verbose_echo
"retry interval =: $RETRY_SECONDS seconds"
162 verbose_echo
" retry counter =: $retry_count times"
164 # determine what update url we're using if a service_name is supplied
165 # otherwise update_url is set inside configuration (custom service)
166 # or update_script is set inside configuration (custom update script)
167 [ -n "$service_name" ] && get_service_data update_url update_script
168 [ -z "$update_url" -a -z "$update_script" ] && critical_error
"no update_url found/defined or no update_script found/defined"
169 [ -n "$update_script" -a ! -f "$update_script" ] && critical_error
"custom update_script not found"
171 #kill old process if it exists & set new pid file
172 if [ -d $RUNDIR ]; then
173 #if process is already running, stop it
174 if [ -e "$PIDFILE" ]; then
175 OLD_PID
=$
(cat $PIDFILE)
176 ps |
grep -q "^[\t ]*$OLD_PID" && {
177 verbose_echo
" old process =: PID '$OLD_PID'"
179 } || verbose_echo
"old process id =: PID 'none'"
181 verbose_echo
"old process id =: PID 'none'"
184 #make dir since it doesn't exist
186 verbose_echo
"old process id =: PID 'none'"
190 # determine when the last update was
191 # the following lines should prevent multiple updates if hotplug fires multiple startups
192 # as described in Ticket #7820, but did not function if never an update take place
193 # i.e. after a reboot (/var is linked to /tmp)
194 # using uptime as reference because date might not be updated via NTP client
196 [ -e "$UPDFILE" ] && {
197 LAST_TIME
=$
(cat $UPDFILE)
198 # check also LAST > CURR because link of /var/run to /tmp might be removed
199 # i.e. boxes with larger filesystems
200 [ -z "$LAST_TIME" ] && LAST_TIME
=0
201 [ $LAST_TIME -gt $CURR_TIME ] && LAST_TIME
=0
203 if [ $LAST_TIME -eq 0 ]; then
204 verbose_echo
" last update =: never"
206 EPOCH_TIME
=$
(( $
(date +%s
) - CURR_TIME
+ LAST_TIME
))
207 EPOCH_TIME
="date -d @$EPOCH_TIME +'$DATE_FORMAT'"
208 verbose_echo
" last update =: $(eval $EPOCH_TIME)"
211 # we need time here because hotplug.d is fired by netifd
212 # but IP addresses are not set by DHCP/DHCPv6 etc.
213 verbose_echo
" waiting =: 10 seconds for interfaces to fully come up"
217 # do with retry's because there might be configurations
218 # not directly could connect to outside dns when interface is already up
219 ERR_VERIFY
=0 # reset err counter
220 while [ -n "$dns_server" ]; do
221 [ $ERR_VERIFY -eq 0 ] && verbose_echo
"******* VERIFY =: DNS server '$dns_server'"
222 verify_dns
"$dns_server"
223 ERR_LAST
=$?
# save return value
224 [ $ERR_LAST -eq 0 ] && break # everything ok leave while loop
225 ERR_VERIFY
=$
(( $ERR_VERIFY + 1 ))
226 # if error count > retry_count leave here with critical error
227 [ $ERR_VERIFY -gt $retry_count ] && {
229 2) critical_error
"Invalid DNS server Error: '2' - nslookup can not resolve host";;
230 3) critical_error
"Invalid DNS server Error: '3' - nc (netcat) can not connect";;
231 *) critical_error
"Invalid DNS server Error: '$ERR_LAST' - unspecific error";;
235 2) syslog_err
"Invalid DNS server Error: '2' - nslookup can not resolve host - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
236 3) syslog_err
"Invalid DNS server Error: '3' - nc (netcat) can not connect - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
237 *) syslog_err
"Invalid DNS server Error: '$ERR_LAST' - unspecific error - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
239 [ $VERBOSE_MODE -gt 1 ] && {
240 # VERBOSE_MODE > 1 then NO retry
241 verbose_echo
"\n!!!!!!!!! ERROR =: Verbose Mode - NO retry\n"
244 verbose_echo
"******** RETRY =: DNS server '$dns_server' - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds"
248 # verify Proxy server and set environment
249 # do with retry's because there might be configurations
250 # not directly could connect to outside dns when interface is already up
251 ERR_VERIFY
=0 # reset err counter
253 [ $ERR_VERIFY -eq 0 ] && verbose_echo
"******* VERIFY =: Proxy server 'http://$proxy'"
254 verify_proxy
"$proxy"
255 ERR_LAST
=$?
# save return value
256 [ $ERR_LAST -eq 0 ] && {
257 # everything ok set proxy and leave while loop
258 export HTTP_PROXY
="http://$proxy"
259 export HTTPS_PROXY
="http://$proxy"
260 export http_proxy
="http://$proxy"
261 export https_proxy
="http://$proxy"
264 ERR_VERIFY
=$
(( $ERR_VERIFY + 1 ))
265 # if error count > retry_count leave here with critical error
266 [ $ERR_VERIFY -gt $retry_count ] && {
268 2) critical_error
"Invalid Proxy server Error '2' - nslookup can not resolve host";;
269 3) critical_error
"Invalid Proxy server Error '3' - nc (netcat) can not connect";;
270 *) critical_error
"Invalid Proxy server Error '$ERR_LAST' - unspecific error";;
274 2) syslog_err
"Invalid Proxy server Error '2' - nslookup can not resolve host - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
275 3) syslog_err
"Invalid Proxy server Error '3' - nc (netcat) can not connect - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
276 *) syslog_err
"Invalid Proxy server Error '$ERR_LAST' - unspecific error - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds\n";;
278 [ $VERBOSE_MODE -gt 1 ] && {
279 # VERBOSE_MODE > 1 then NO retry
280 verbose_echo
"\n!!!!!!!!! ERROR =: Verbose Mode - NO retry\n"
283 verbose_echo
"******** RETRY =: Proxy server 'http://$proxy' - retry $ERR_VERIFY/$retry_count in $RETRY_SECONDS seconds"
287 # let's check if there is already an IP registered at the web
288 # but ignore errors if not
289 verbose_echo
"******* DETECT =: Registered IP"
290 get_registered_ip REGISTERED_IP
292 # loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
293 # NEW: ### Luci Ticket 538
294 # a "force_interval" of "0" will run this script only once
295 # the update is only done once when an interface goes up
296 # or you run /etc/init.d/ddns start or you can use a cron job
297 # it will force an update without check when lastupdate happen
298 # but it will verify after "check_interval" if update is seen in the web
299 # and retries on error retry_count times
300 # CHANGES: ### Ticket 16363
301 # modified nslookup / sed / grep to detect registered ip
302 # NEW: ### Ticket 7820
303 # modified nslookup to support non standard dns_server (needs to be defined in /etc/config/ddns)
304 # support for BIND host command.
305 # Wait for interface to fully come up, before the first update is done
306 verbose_echo
"*** START LOOP =: $(eval $DATE_PROG)"
308 [ $FORCE_SECONDS -gt 0 -o $VERBOSE_MODE -le 1 ] && syslog_info
"Starting main loop"
313 verbose_echo
"******* DETECT =: Local IP"
314 get_local_ip LOCAL_IP
315 ERR_LAST
=$?
# save return value
317 [ $ERR_LAST -gt 0 ] && {
318 if [ $VERBOSE_MODE -le 1 ]; then # VERBOSE_MODE <= 1 then retry
319 # we can't read local IP
320 ERR_LOCAL_IP
=$
(( $ERR_LOCAL_IP + 1 ))
321 [ $ERR_LOCAL_IP -gt $retry_count ] && critical_error
"Can not detect local IP"
322 verbose_echo
"\n!!!!!!!!! ERROR =: detecting local IP - retry $ERR_LOCAL_IP/$retry_count in $RETRY_SECONDS seconds\n"
323 syslog_err
"Error detecting local IP - retry $ERR_LOCAL_IP/$retry_count in $RETRY_SECONDS seconds"
325 continue # jump back to the beginning of while loop
327 verbose_echo
"\n!!!!!!!!! ERROR =: detecting local IP - NO retry\n"
330 ERR_LOCAL_IP
=0 # reset err counter
333 # never updated or forced immediate then NEXT_TIME = 0
334 [ $FORCE_SECONDS -eq 0 -o $LAST_TIME -eq 0 ] \
336 || NEXT_TIME
=$
(( $LAST_TIME + $FORCE_SECONDS ))
340 # send update when current time > next time or local ip different from registered ip (as loop on error)
342 while [ $CURR_TIME -ge $NEXT_TIME -o "$LOCAL_IP" != "$REGISTERED_IP" ]; do
343 if [ $VERBOSE_MODE -gt 2 ]; then
344 verbose_echo
" VERBOSE MODE =: NO UPDATE send to DDNS provider"
345 elif [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
346 verbose_echo
"******* UPDATE =: LOCAL: '$LOCAL_IP' <> REGISTERED: '$REGISTERED_IP'"
348 verbose_echo
"******* FORCED =: LOCAL: '$LOCAL_IP' == REGISTERED: '$REGISTERED_IP'"
350 # only send if VERBOSE_MODE < 3
352 [ $VERBOSE_MODE -lt 3 ] && {
353 send_update
"$LOCAL_IP"
354 ERR_LAST
=$?
# save return value
358 if [ $ERR_LAST -gt 0 ]; then
359 if [ $VERBOSE_MODE -le 1 ]; then # VERBOSE_MODE <=1 then retry
360 # error sending local IP
361 ERR_SEND
=$
(( $ERR_SEND + 1 ))
362 [ $ERR_SEND -gt $retry_count ] && critical_error
"can not send update to DDNS Provider"
363 verbose_echo
"\n!!!!!!!!! ERROR =: sending update - retry $ERR_SEND/$retry_count in $RETRY_SECONDS seconds\n"
364 syslog_err
"Error sending update - retry $ERR_SEND/$retry_count in $RETRY_SECONDS seconds"
368 verbose_echo
"\n!!!!!!!!! ERROR =: sending update to DDNS service - NO retry\n"
372 # we send data so save "last time"
374 echo $LAST_TIME > $UPDFILE # save LASTTIME to file
375 [ "$LOCAL_IP" != "$REGISTERED_IP" ] \
376 && syslog_notice
"Changed IP: '$LOCAL_IP' successfully send" \
377 || syslog_notice
"Forced Update: IP: '$LOCAL_IP' successfully send"
382 # now we wait for check interval before testing if update was recognized
383 # only sleep if VERBOSE_MODE <= 2 because nothing send so do not wait
384 [ $VERBOSE_MODE -le 2 ] && {
385 verbose_echo
"****** WAITING =: $CHECK_SECONDS seconds (Check Interval) before continue"
387 } || verbose_echo
" VERBOSE MODE =: NO WAITING for Check Interval\n"
389 # read at DDNS service registered IP (in loop on error)
393 verbose_echo
"******* DETECT =: Registered IP"
394 get_registered_ip REGISTERED_IP
395 ERR_LAST
=$?
# save return value
397 # No Error in function we leave while loop
398 [ $ERR_LAST -eq 0 ] && break
400 # we can't read Registered IP
401 if [ $VERBOSE_MODE -le 1 ]; then # VERBOSE_MODE <=1 then retry
402 ERR_REG_IP
=$
(( $ERR_REG_IP + 1 ))
403 [ $ERR_REG_IP -gt $retry_count ] && critical_error
"can not detect registered local IP"
404 verbose_echo
"\n!!!!!!!!! ERROR =: detecting Registered IP - retry $ERR_REG_IP/$retry_count in $RETRY_SECONDS seconds\n"
405 syslog_err
"Error detecting Registered IP - retry $ERR_REG_IP/$retry_count in $RETRY_SECONDS seconds"
408 verbose_echo
"\n!!!!!!!!! ERROR =: detecting Registered IP - NO retry\n"
409 break # leave while loop
413 # IP's are still different
414 if [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
415 if [ $VERBOSE_MODE -le 1 ]; then # VERBOSE_MODE <=1 then retry
416 ERR_UPDATE
=$
(( $ERR_UPDATE + 1 ))
417 [ $ERR_UPDATE -gt $retry_count ] && critical_error
"Registered IP <> Local IP - LocalIP: '$LOCAL_IP' - RegisteredIP: '$REGISTERED_IP'"
418 verbose_echo
"\n!!!!!!!!! ERROR =: Registered IP <> Local IP - starting retry $ERR_UPDATE/$retry_count\n"
419 syslog_warn
"Warning: Registered IP <> Local IP - starting retry $ERR_UPDATE/$retry_count"
420 continue # loop to beginning
422 verbose_echo
"\n!!!!!!!!! ERROR =: Registered IP <> Local IP - LocalIP: '$LOCAL_IP' - RegisteredIP: '$REGISTERED_IP' - NO retry\n"
426 # we checked successful the last update
427 ERR_UPDATE
=0 # reset error counter
429 # force_update=0 or VERBOSE_MODE > 1 - leave the main loop
430 [ $FORCE_SECONDS -eq 0 -o $VERBOSE_MODE -gt 1 ] && {
431 verbose_echo
"****** LEAVING =: $(eval $DATE_PROG)"
432 syslog_info
"Leaving"
435 verbose_echo
"********* LOOP =: $(eval $DATE_PROG)"
436 syslog_info
"Rerun IP check"
439 verbose_echo
"****** STOPPED =: PID '$$' at $(eval $DATE_PROG)\n"