2 # /usr/lib/ddns/dynamic_dns_updater.sh
4 #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
5 # Original written by Eric Paul Bishop, January 2008
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
8 # extended and partial rewritten
9 #.2014-2016 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
11 # variables in small chars are read from /etc/config/ddns
12 # variables in big chars are defined inside these scripts as global vars
13 # variables in big chars beginning with "__" are local defined inside functions only
14 # set -vx #script debugger
16 .
/usr
/lib
/ddns
/dynamic_dns_functions.sh
# global vars are also defined here
18 [ $# -lt 1 -o -n "${2//[0-3]/}" -o ${#2} -gt 1 ] && {
19 echo -e "\n ddns-scripts Version: $VERSION"
22 echo " [OPTION] '-V' or '--version' display version and exit"
23 echo -e "\n $0 [SECTION] [VERBOSE_MODE]\n"
24 echo " [SECTION] - service section as defined in /etc/config/ddns"
25 echo " [VERBOSE_MODE] - '0' NO output to console"
26 echo " '1' output to console"
27 echo " '2' output to console AND logfile"
28 echo " + run once WITHOUT retry on error"
29 echo " '3' output to console AND logfile"
30 echo " + run once WITHOUT retry on error"
31 echo -e " + NOT sending update to DDNS service\n"
35 [ "$1" = "-V" -o "$1" = "--version" ] && {
36 echo -e "ddns-scripts $VERSION\n"
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 DATFILE
="$RUNDIR/$SECTION_ID.dat" # save stdout data of WGet and other extern programs called
47 ERRFILE
="$RUNDIR/$SECTION_ID.err" # save stderr output of WGet and other extern programs called
48 LOGFILE
="$LOGDIR/$SECTION_ID.log" # log file
50 # VERBOSE_MODE > 1 delete logfile if exist to create an empty one
51 # only with this data of this run for easier diagnostic
52 # new one created by write_log function
53 [ $VERBOSE_MODE -gt 1 -a -f $LOGFILE ] && rm -f $LOGFILE
56 trap "trap_handler 0 \$?" 0 # handle script exit with exit status
57 trap "trap_handler 1" 1 # SIGHUP Hangup / reload config
58 trap "trap_handler 2" 2 # SIGINT Terminal interrupt
59 trap "trap_handler 3" 3 # SIGQUIT Terminal quit
60 # trap "trap_handler 9" 9 # SIGKILL no chance to trap
61 trap "trap_handler 15" 15 # SIGTERM Termination
63 ################################################################################
64 # Leave this comment here, to clearly document variable names that are expected/possible
65 # Use load_all_config_options to load config options, which is a much more flexible solution.
68 # config_get <variable> $SECTION_ID <option>
70 # defined options (also used as variable):
72 # enable self-explanatory
73 # interface network interface used by hotplug.d i.e. 'wan' or 'wan6'
75 # service_name Which DDNS service do you use or "custom"
76 # update_url URL to use to update your "custom" DDNS service
77 # update_script SCRIPT to use to update your "custom" DDNS service
79 # lookup_host FQDN of ONE of your at DDNS service defined host / required to validate if IP update happen/necessary
80 # domain Nomally your DDNS hostname / replace [DOMAIN] in update_url
81 # username Username of your DDNS service account / urlenceded and replace [USERNAME] in update_url
82 # password Password of your DDNS service account / urlencoded and replace [PASSWORD] in update_url
83 # param_enc Optional parameter for (later) usage / urlencoded and replace [PARAMENC] in update_url
84 # param_opt Optional parameter for (later) usage / replace [PARAMOPT] in update_url
86 # use_https use HTTPS to update DDNS service
87 # cacert file or directory where HTTPS can find certificates to verify server; 'IGNORE' ignore check of server certificate
89 # use_syslog log activity to syslog
91 # ip_source source to detect current local IP ('network' or 'web' or 'script' or 'interface')
92 # ip_network local defined network to read IP from i.e. 'wan' or 'wan6'
93 # ip_url URL to read local address from i.e. http://checkip.dyndns.com/ or http://checkipv6.dyndns.com/
94 # ip_script full path and name of your script to detect local IP
95 # ip_interface physical interface to use for detecting
97 # check_interval check for changes every !!! checks below 10 minutes make no sense because the Internet
98 # check_unit 'days' 'hours' 'minutes' !!! needs about 5-10 minutes to sync an IP-change for an DNS entry
100 # force_interval force to send an update to your service if no change was detected
101 # force_unit 'days' 'hours' 'minutes' !!! force_interval="0" runs this script once for use i.e. with cron
103 # retry_interval if error was detected retry in
104 # retry_unit 'days' 'hours' 'minutes' 'seconds'
105 # retry_count number of retries before scripts stops
107 # use_ipv6 detecting/sending IPv6 address
108 # force_ipversion force usage of IPv4 or IPv6 for the whole detection and update communication
109 # dns_server using a non default dns server to get Registered IP from Internet
110 # force_dnstcp force communication with DNS server via TCP instead of default UDP
111 # proxy using a proxy for communication !!! ALSO used to detect local IP via web => return proxy's IP !!!
112 # use_logfile self-explanatory "/var/log/ddns/$SECTION_ID.log"
114 # some functionality needs
115 # - GNU Wget or cURL installed for sending updates to DDNS service
116 # - BIND host installed to detect Registered IP
118 ################################################################################
120 load_all_config_options
"ddns" "$SECTION_ID"
121 ERR_LAST
=$?
# save return code - equal 0 if SECTION_ID found
123 # set defaults if not defined
124 [ -z "$enabled" ] && enabled
=0
125 [ -z "$retry_count" ] && retry_count
=0 # endless retry
126 [ -z "$use_syslog" ] && use_syslog
=2 # syslog "Notice"
127 [ -z "$use_https" ] && use_https
=0 # not use https
128 [ -z "$use_logfile" ] && use_logfile
=1 # use logfile by default
129 [ -z "$use_ipv6" ] && use_ipv6
=0 # use IPv4 by default
130 [ -z "$force_ipversion" ] && force_ipversion
=0 # default let system decide
131 [ -z "$force_dnstcp" ] && force_dnstcp
=0 # default UDP
132 [ -z "$ip_source" ] && ip_source
="network"
133 [ "$ip_source" = "network" -a -z "$ip_network" -a $use_ipv6 -eq 0 ] && ip_network
="wan" # IPv4: default wan
134 [ "$ip_source" = "network" -a -z "$ip_network" -a $use_ipv6 -eq 1 ] && ip_network
="wan6" # IPv6: default wan6
135 [ "$ip_source" = "web" -a -z "$ip_url" -a $use_ipv6 -eq 0 ] && ip_url
="http://checkip.dyndns.com"
136 [ "$ip_source" = "web" -a -z "$ip_url" -a $use_ipv6 -eq 1 ] && ip_url
="http://checkipv6.dyndns.com"
137 [ "$ip_source" = "interface" -a -z "$ip_interface" ] && ip_interface
="eth1"
139 # SECTION_ID does not exists
140 [ $ERR_LAST -ne 0 ] && {
141 [ $VERBOSE_MODE -le 1 ] && VERBOSE_MODE
=2 # force console out and logfile output
142 [ -f $LOGFILE ] && rm -f $LOGFILE # clear logfile before first entry
143 write_log
7 "************ ************** ************** **************"
144 write_log
5 "PID '$$' started at $(eval $DATE_PROG)"
145 write_log
7 "ddns version : $VERSION"
146 write_log
7 "uci configuration:\n$(uci -q show ddns | grep '=service' | sort)"
147 write_log
14 "Service section '$SECTION_ID' not defined"
150 write_log
7 "************ ************** ************** **************"
151 write_log
5 "PID '$$' started at $(eval $DATE_PROG)"
152 write_log
7 "ddns version : $VERSION"
153 write_log
7 "uci configuration:\n$(uci -q show ddns.$SECTION_ID | sort)"
154 # write_log 7 "ddns version : $(opkg list-installed ddns-scripts | cut -d ' ' -f 3)"
155 case $VERBOSE_MODE in
156 0) write_log
7 "verbose mode : 0 - run normal, NO console output";;
157 1) write_log
7 "verbose mode : 1 - run normal, console mode";;
158 2) write_log
7 "verbose mode : 2 - run once, NO retry on error";;
159 3) write_log
7 "verbose mode : 3 - run once, NO retry on error, NOT sending update";;
160 *) write_log
14 "error detecting VERBOSE_MODE '$VERBOSE_MODE'";;
163 # check enabled state otherwise we don't need to continue
164 [ $enabled -eq 0 ] && write_log
14 "Service section disabled!"
166 # determine what update url we're using if a service_name is supplied
167 # otherwise update_url is set inside configuration (custom update url)
168 # or update_script is set inside configuration (custom update script)
169 [ -n "$service_name" ] && get_service_data update_url update_script SRV_ANSWER
170 [ -z "$update_url" -a -z "$update_script" ] && write_log
14 "No update_url found/defined or no update_script found/defined!"
171 [ -n "$update_script" -a ! -f "$update_script" ] && write_log
14 "Custom update_script not found!"
173 # temporary needed to convert existing uci settings
174 [ -z "$lookup_host" ] && {
175 uci
-q set ddns.
$SECTION_ID.lookup_host
="$domain"
177 lookup_host
="$domain"
179 # later versions only check if configured correctly
181 # without lookup host and possibly other required options we can do nothing for you
182 [ -z "$lookup_host" ] && write_log
14 "Service section not configured correctly! Missing 'lookup_host'"
184 [ -n "$update_url" ] && {
185 # only check if update_url is given, update_scripts have to check themselves
186 [ -z "$domain" ] && $
(echo "$update_url" |
grep "\[DOMAIN\]" >/dev
/null
2>&1) && \
187 write_log
14 "Service section not configured correctly! Missing 'domain'"
188 [ -z "$username" ] && $
(echo "$update_url" |
grep "\[USERNAME\]" >/dev
/null
2>&1) && \
189 write_log
14 "Service section not configured correctly! Missing 'username'"
190 [ -z "$password" ] && $
(echo "$update_url" |
grep "\[PASSWORD\]" >/dev
/null
2>&1) && \
191 write_log
14 "Service section not configured correctly! Missing 'password'"
192 [ -z "$param_enc" ] && $
(echo "$update_url" |
grep "\[PARAMENC\]" >/dev
/null
2>&1) && \
193 write_log
14 "Service section not configured correctly! Missing 'param_enc'"
194 [ -z "$param_opt" ] && $
(echo "$update_url" |
grep "\[PARAMOPT\]" >/dev
/null
2>&1) && \
195 write_log
14 "Service section not configured correctly! Missing 'param_opt'"
198 # url encode username (might be email or something like this)
199 # and password (might have special chars for security reason)
200 # and optional parameter "param_enc"
201 [ -n "$username" ] && urlencode URL_USER
"$username"
202 [ -n "$password" ] && urlencode URL_PASS
"$password"
203 [ -n "$param_enc" ] && urlencode URL_PENC
"$param_enc"
205 # verify ip_source 'script' if script is configured and executable
206 if [ "$ip_source" = "script" ]; then
207 set -- $ip_script #handling script with parameters, we need a trick
208 [ -z "$1" ] && write_log
14 "No script defined to detect local IP!"
209 [ -x "$1" ] || write_log
14 "Script to detect local IP not executable!"
212 # compute update interval in seconds
213 get_seconds CHECK_SECONDS
${check_interval:-10} ${check_unit:-"minutes"} # default 10 min
214 get_seconds FORCE_SECONDS
${force_interval:-72} ${force_unit:-"hours"} # default 3 days
215 get_seconds RETRY_SECONDS
${retry_interval:-60} ${retry_unit:-"seconds"} # default 60 sec
216 [ $CHECK_SECONDS -lt 300 ] && CHECK_SECONDS
=300 # minimum 5 minutes
217 [ $FORCE_SECONDS -gt 0 -a $FORCE_SECONDS -lt $CHECK_SECONDS ] && FORCE_SECONDS
=$CHECK_SECONDS # FORCE_SECONDS >= CHECK_SECONDS or 0
218 write_log
7 "check interval: $CHECK_SECONDS seconds"
219 write_log
7 "force interval: $FORCE_SECONDS seconds"
220 write_log
7 "retry interval: $RETRY_SECONDS seconds"
221 write_log
7 "retry counter : $retry_count times"
223 # kill old process if it exists & set new pid file
224 stop_section_processes
"$SECTION_ID"
225 [ $?
-gt 0 ] && write_log
7 "'SIGTERM' was send to old process" || write_log
7 "No old process"
228 # determine when the last update was
229 # the following lines should prevent multiple updates if hotplug fires multiple startups
230 # as described in Ticket #7820, but did not function if never an update take place
231 # i.e. after a reboot (/var is linked to /tmp)
232 # using uptime as reference because date might not be updated via NTP client
234 [ -e "$UPDFILE" ] && {
235 LAST_TIME
=$
(cat $UPDFILE)
236 # check also LAST > CURR because link of /var/run to /tmp might be removed
237 # i.e. boxes with larger filesystems
238 [ -z "$LAST_TIME" ] && LAST_TIME
=0
239 [ $LAST_TIME -gt $CURR_TIME ] && LAST_TIME
=0
241 if [ $LAST_TIME -eq 0 ]; then
242 write_log
7 "last update: never"
244 EPOCH_TIME
=$
(( $
(date +%s
) - CURR_TIME
+ LAST_TIME
))
245 EPOCH_TIME
="date -d @$EPOCH_TIME +'$DATE_FORMAT'"
246 write_log
7 "last update: $(eval $EPOCH_TIME)"
250 [ -n "$dns_server" ] && verify_dns
"$dns_server"
252 # verify Proxy server and set environment
254 verify_proxy
"$proxy" && {
255 # everything ok set proxy
256 export HTTP_PROXY
="http://$proxy"
257 export HTTPS_PROXY
="http://$proxy"
258 export http_proxy
="http://$proxy"
259 export https_proxy
="http://$proxy"
263 # let's check if there is already an IP registered on the web
264 get_registered_ip REGISTERED_IP
"NO_RETRY"
266 # No error or No IP set otherwise retry
267 [ $ERR_LAST -eq 0 -o $ERR_LAST -eq 127 ] || get_registered_ip REGISTERED_IP
268 # on IPv6 we use expanded version to be shure when comparing
269 [ $use_ipv6 -eq 1 ] && expand_ipv6
"$REGISTERED_IP" REGISTERED_IP
271 # loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
272 write_log
6 "Starting main loop at $(eval $DATE_PROG)"
275 get_local_ip LOCAL_IP
# read local IP
276 [ $use_ipv6 -eq 1 ] && expand_ipv6
"$LOCAL_IP" LOCAL_IP
# on IPv6 we use expanded version
279 # never updated or forced immediate then NEXT_TIME = 0
280 [ $FORCE_SECONDS -eq 0 -o $LAST_TIME -eq 0 ] \
282 || NEXT_TIME
=$
(( $LAST_TIME + $FORCE_SECONDS ))
284 get_uptime CURR_TIME
# get current uptime
286 # send update when current time > next time or local ip different from registered ip
287 if [ $CURR_TIME -ge $NEXT_TIME -o "$LOCAL_IP" != "$REGISTERED_IP" ]; then
288 if [ $VERBOSE_MODE -gt 2 ]; then
289 write_log
7 "Verbose Mode: $VERBOSE_MODE - NO UPDATE send"
290 elif [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
291 write_log
7 "Update needed - L: '$LOCAL_IP' <> R: '$REGISTERED_IP'"
293 write_log
7 "Forced Update - L: '$LOCAL_IP' == R: '$REGISTERED_IP'"
297 [ $VERBOSE_MODE -lt 3 ] && {
298 # only send if VERBOSE_MODE < 3
299 send_update
"$LOCAL_IP"
300 ERR_LAST
=$?
# save return value
303 # error sending local IP to provider
304 # we have no communication error (handled inside send_update/do_transfer)
305 # but update was not recognized
306 # do NOT retry after RETRY_SECONDS, do retry after CHECK_SECONDS
307 # to early retrys will block most DDNS provider
308 # providers answer is checked inside send_update() function
309 if [ $ERR_LAST -eq 0 ]; then
310 get_uptime LAST_TIME
# we send update, so
311 echo $LAST_TIME > $UPDFILE # save LASTTIME to file
312 [ "$LOCAL_IP" != "$REGISTERED_IP" ] \
313 && write_log
6 "Update successful - IP '$LOCAL_IP' send" \
314 || write_log
6 "Forced update successful - IP: '$LOCAL_IP' send"
316 write_log
3 "IP update not accepted by DDNS Provider"
320 # now we wait for check interval before testing if update was recognized
321 # only sleep if VERBOSE_MODE <= 2 because otherwise nothing was send
322 [ $VERBOSE_MODE -le 2 ] && {
323 write_log
7 "Waiting $CHECK_SECONDS seconds (Check Interval)"
324 sleep $CHECK_SECONDS &
326 wait $PID_SLEEP # enable trap-handler
328 } || write_log
7 "Verbose Mode: $VERBOSE_MODE - NO Check Interval waiting"
330 REGISTERED_IP
="" # clear variable
331 get_registered_ip REGISTERED_IP
# get registered/public IP
332 [ $use_ipv6 -eq 1 ] && expand_ipv6
"$REGISTERED_IP" REGISTERED_IP
# on IPv6 we use expanded version
334 # IP's are still different
335 if [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
336 if [ $VERBOSE_MODE -le 1 ]; then # VERBOSE_MODE <=1 then retry
337 ERR_UPDATE
=$
(( $ERR_UPDATE + 1 ))
338 [ $retry_count -gt 0 -a $ERR_UPDATE -gt $retry_count ] && \
339 write_log
14 "Updating IP at DDNS provider failed after $retry_count retries"
340 write_log
4 "Updating IP at DDNS provider failed - starting retry $ERR_UPDATE/$retry_count"
341 continue # loop to beginning
343 write_log
4 "Updating IP at DDNS provider failed"
344 write_log
7 "Verbose Mode: $VERBOSE_MODE - NO retry"; exit 1
347 # we checked successful the last update
348 ERR_UPDATE
=0 # reset error counter
351 # force_update=0 or VERBOSE_MODE > 1 - leave here
352 [ $VERBOSE_MODE -gt 1 ] && write_log
7 "Verbose Mode: $VERBOSE_MODE - NO reloop"
353 [ $FORCE_SECONDS -eq 0 ] && write_log
6 "Configured to run once"
354 [ $VERBOSE_MODE -gt 1 -o $FORCE_SECONDS -eq 0 ] && exit 0
356 write_log
6 "Rerun IP check at $(eval $DATE_PROG)"
358 # we should never come here there must be a programming error
359 write_log
12 "Error in 'dynamic_dns_updater.sh - program coding error"