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 (Ticket 7820)
14 # - Proxy Server to send out updates
15 # - force_interval=0 to run once (Luci Ticket 538)
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
18 # - wait for interface to fully come up, before the first update is done
20 # variables in small chars are read from /etc/config/ddns
21 # variables in big chars are defined inside these scripts as global vars
22 # variables in big chars beginning with "__" are local defined inside functions only
23 #set -vx #script debugger
25 [ $# -lt 1 -o -n "${2//[0-3]/}" -o ${#2} -gt 1 ] && {
27 echo -e " $0 [SECTION] [VERBOSE_MODE]\n"
28 echo " [SECTION] - service section as defined in /etc/config/ddns"
29 echo " [VERBOSE_MODE] - '0' NO output to console"
30 echo " '1' output to console"
31 echo " '2' output to console AND logfile"
32 echo " + run once WITHOUT retry on error"
33 echo " '3' output to console AND logfile"
34 echo " + run once WITHOUT retry on error"
35 echo -e " + NOT sending update to DDNS service\n"
39 .
/usr
/lib
/ddns
/dynamic_dns_functions.sh
# global vars are also defined here
42 VERBOSE_MODE
=${2:-1} # default mode is log to console
45 PIDFILE
="$RUNDIR/$SECTION_ID.pid" # Process ID file
46 UPDFILE
="$RUNDIR/$SECTION_ID.update" # last update successful send (system uptime)
47 DATFILE
="$RUNDIR/$SECTION_ID.dat" # save stdout data of WGet and other extern programs called
48 ERRFILE
="$RUNDIR/$SECTION_ID.err" # save stderr output of WGet and other extern programs called
49 LOGFILE
="$LOGDIR/$SECTION_ID.log" # log file
51 # VERBOSE_MODE > 1 delete logfile if exist to create an empty one
52 # only with this data of this run for easier diagnostic
53 # new one created by write_log function
54 [ $VERBOSE_MODE -gt 1 -a -f $LOGFILE ] && rm -f $LOGFILE
57 trap "trap_handler 0 \$?" 0 # handle script exit with exit status
58 trap "trap_handler 1" 1 # SIGHUP Hangup / reload config
59 trap "trap_handler 2" 2 # SIGINT Terminal interrupt
60 trap "trap_handler 3" 3 # SIGQUIT Terminal quit
61 #trap "trap_handler 9" 9 # SIGKILL no chance to trap
62 trap "trap_handler 15" 15 # SIGTERM Termination
64 ################################################################################
65 # Leave this comment here, to clearly document variable names that are expected/possible
66 # Use load_all_config_options to load config options, which is a much more flexible solution.
69 # config_get <variable> $SECTION_ID <option>
71 # defined options (also used as variable):
73 # enable self-explanatory
74 # interface network interface used by hotplug.d i.e. 'wan' or 'wan6'
76 # service_name Which DDNS service do you use or "custom"
77 # update_url URL to use to update your "custom" DDNS service
78 # update_script SCRIPT to use to update your "custom" DDNS service
80 # domain Your DNS name / replace [DOMAIN] in update_url
81 # username Username of your DDNS service account / replace [USERNAME] in update_url
82 # password Password of your DDNS service account / replace [PASSWORD] in update_url
84 # use_https use HTTPS to update DDNS service
85 # cacert file or directory where HTTPS can find certificates to verify server; 'IGNORE' ignore check of server certificate
87 # use_syslog log activity to syslog
89 # ip_source source to detect current local IP ('network' or 'web' or 'script' or 'interface')
90 # ip_network local defined network to read IP from i.e. 'wan' or 'wan6'
91 # ip_url URL to read local address from i.e. http://checkip.dyndns.com/ or http://checkipv6.dyndns.com/
92 # ip_script full path and name of your script to detect local IP
93 # ip_interface physical interface to use for detecting
95 # check_interval check for changes every !!! checks below 10 minutes make no sense because the Internet
96 # check_unit 'days' 'hours' 'minutes' !!! needs about 5-10 minutes to sync an IP-change for an DNS entry
98 # force_interval force to send an update to your service if no change was detected
99 # force_unit 'days' 'hours' 'minutes' !!! force_interval="0" runs this script once for use i.e. with cron
101 # retry_interval if error was detected retry in
102 # retry_unit 'days' 'hours' 'minutes' 'seconds'
103 # retry_count number of retries before scripts stops
105 # use_ipv6 detecting/sending IPv6 address
106 # force_ipversion force usage of IPv4 or IPv6 for the whole detection and update communication
107 # dns_server using a non default dns server to get Registered IP from Internet
108 # force_dnstcp force communication with DNS server via TCP instead of default UDP
109 # proxy using a proxy for communication !!! ALSO used to detect local IP via web => return proxy's IP !!!
110 # use_logfile self-explanatory "/var/log/ddns/$SECTION_ID.log"
112 # some functionality needs
113 # - GNU Wget or cURL installed for sending updates to DDNS service
114 # - BIND host installed to detect Registered IP
116 ################################################################################
118 # verify and load SECTION_ID is exists
119 [ "$(uci_get ddns $SECTION_ID)" != "service" ] && {
120 [ $VERBOSE_MODE -le 1 ] && VERBOSE_MODE
=2 # force console out and logfile output
121 [ -f $LOGFILE ] && rm -f $LOGFILE # clear logfile before first entry
122 write_log
7 "************ ************** ************** **************"
123 write_log
5 "PID '$$' started at $(eval $DATE_PROG)"
124 write_log
7 "uci configuration:\n$(uci -q show ddns | grep '=service' | sort)"
125 write_log
14 "Service section '$SECTION_ID' not defined"
127 load_all_config_options
"ddns" "$SECTION_ID"
129 write_log
7 "************ ************** ************** **************"
130 write_log
5 "PID '$$' started at $(eval $DATE_PROG)"
131 write_log
7 "uci configuraion:\n$(uci -q show ddns.$SECTION_ID | sort)"
132 case $VERBOSE_MODE in
133 0) write_log
7 "verbose mode : 0 - run normal, NO console output";;
134 1) write_log
7 "verbose mode : 1 - run normal, console mode";;
135 2) write_log
7 "verbose mode : 2 - run once, NO retry on error";;
136 3) write_log
7 "verbose mode : 3 - run once, NO retry on error, NOT sending update";;
137 *) write_log
14 "error detecting VERBOSE_MODE '$VERBOSE_MODE'";;
140 # set defaults if not defined
141 [ -z "$enabled" ] && enabled
=0
142 [ -z "$retry_count" ] && retry_count
=5
143 [ -z "$use_syslog" ] && use_syslog
=2 # syslog "Notice"
144 [ -z "$use_https" ] && use_https
=0 # not use https
145 [ -z "$use_logfile" ] && use_logfile
=1 # use logfile by default
146 [ -z "$use_ipv6" ] && use_ipv6
=0 # use IPv4 by default
147 [ -z "$force_ipversion" ] && force_ipversion
=0 # default let system decide
148 [ -z "$force_dnstcp" ] && force_dnstcp
=0 # default UDP
149 [ -z "$ip_source" ] && ip_source
="network"
150 [ "$ip_source" = "network" -a -z "$ip_network" -a $use_ipv6 -eq 0 ] && ip_network
="wan" # IPv4: default wan
151 [ "$ip_source" = "network" -a -z "$ip_network" -a $use_ipv6 -eq 1 ] && ip_network
="wan6" # IPv6: default wan6
152 [ "$ip_source" = "web" -a -z "$ip_url" -a $use_ipv6 -eq 0 ] && ip_url
="http://checkip.dyndns.com"
153 [ "$ip_source" = "web" -a -z "$ip_url" -a $use_ipv6 -eq 1 ] && ip_url
="http://checkipv6.dyndns.com"
154 [ "$ip_source" = "interface" -a -z "$ip_interface" ] && ip_interface
="eth1"
156 # check enabled state otherwise we don't need to continue
157 [ $enabled -eq 0 ] && write_log
14 "Service section disabled!"
159 # without domain or username or password we can do nothing for you
160 [ -z "$domain" -o -z "$username" -o -z "$password" ] && write_log
14 "Service section not correctly configured!"
161 urlencode URL_USER
"$username" # encode username, might be email or something like this
162 urlencode URL_PASS
"$password" # encode password, might have special chars for security reason
164 # verify ip_source script if configured and executable
165 if [ "$ip_source" = "script" ]; then
166 set -- $ip_script #handling script with parameters, we need a trick
167 [ -z "$1" ] && write_log
14 "No script defined to detect local IP!"
168 [ -x "$1" ] || write_log
14 "Script to detect local IP not executable!"
171 # compute update interval in seconds
172 get_seconds CHECK_SECONDS
${check_interval:-10} ${check_unit:-"minutes"} # default 10 min
173 get_seconds FORCE_SECONDS
${force_interval:-72} ${force_unit:-"hours"} # default 3 days
174 get_seconds RETRY_SECONDS
${retry_interval:-60} ${retry_unit:-"seconds"} # default 60 sec
175 [ $CHECK_SECONDS -lt 300 ] && CHECK_SECONDS
=300 # minimum 5 minutes
176 [ $FORCE_SECONDS -gt 0 -a $FORCE_SECONDS -lt $CHECK_SECONDS ] && FORCE_SECONDS
=$CHECK_SECONDS # FORCE_SECONDS >= CHECK_SECONDS or 0
177 write_log
7 "check interval: $CHECK_SECONDS seconds"
178 write_log
7 "force interval: $FORCE_SECONDS seconds"
179 write_log
7 "retry interval: $RETRY_SECONDS seconds"
180 write_log
7 "retry counter : $retry_count times"
182 # determine what update url we're using if a service_name is supplied
183 # otherwise update_url is set inside configuration (custom update url)
184 # or update_script is set inside configuration (custom update script)
185 [ -n "$service_name" ] && get_service_data update_url update_script
186 [ -z "$update_url" -a -z "$update_script" ] && write_log
14 "No update_url found/defined or no update_script found/defined!"
187 [ -n "$update_script" -a ! -f "$update_script" ] && write_log
14 "Custom update_script not found!"
189 #kill old process if it exists & set new pid file
190 stop_section_processes
"$SECTION_ID"
191 [ $?
-gt 0 ] && write_log
7 "Send 'SIGTERM' to old process" || write_log
7 "No old process"
194 # determine when the last update was
195 # the following lines should prevent multiple updates if hotplug fires multiple startups
196 # as described in Ticket #7820, but did not function if never an update take place
197 # i.e. after a reboot (/var is linked to /tmp)
198 # using uptime as reference because date might not be updated via NTP client
200 [ -e "$UPDFILE" ] && {
201 LAST_TIME
=$
(cat $UPDFILE)
202 # check also LAST > CURR because link of /var/run to /tmp might be removed
203 # i.e. boxes with larger filesystems
204 [ -z "$LAST_TIME" ] && LAST_TIME
=0
205 [ $LAST_TIME -gt $CURR_TIME ] && LAST_TIME
=0
207 if [ $LAST_TIME -eq 0 ]; then
208 write_log
7 "last update: never"
210 EPOCH_TIME
=$
(( $
(date +%s
) - CURR_TIME
+ LAST_TIME
))
211 EPOCH_TIME
="date -d @$EPOCH_TIME +'$DATE_FORMAT'"
212 write_log
7 "last update: $(eval $EPOCH_TIME)"
215 # we need time here because hotplug.d is fired by netifd
216 # but IP addresses are not set by DHCP/DHCPv6 etc.
217 write_log
7 "Waiting 10 seconds for interfaces to fully come up"
220 wait $PID_SLEEP # enable trap-handler
224 [ -n "$dns_server" ] && verify_dns
"$dns_server"
226 # verify Proxy server and set environment
228 verify_proxy
"$proxy" && {
229 # everything ok set proxy
230 export HTTP_PROXY
="http://$proxy"
231 export HTTPS_PROXY
="http://$proxy"
232 export http_proxy
="http://$proxy"
233 export https_proxy
="http://$proxy"
237 # let's check if there is already an IP registered at the web
238 # but ignore errors if not
239 get_registered_ip REGISTERED_IP
"NO_RETRY"
241 # loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
242 write_log
6 "Starting main loop at $(eval $DATE_PROG)"
245 get_local_ip LOCAL_IP
# read local IP
248 # never updated or forced immediate then NEXT_TIME = 0
249 [ $FORCE_SECONDS -eq 0 -o $LAST_TIME -eq 0 ] \
251 || NEXT_TIME
=$
(( $LAST_TIME + $FORCE_SECONDS ))
253 get_uptime CURR_TIME
# get current uptime
255 # send update when current time > next time or local ip different from registered ip
256 if [ $CURR_TIME -ge $NEXT_TIME -o "$LOCAL_IP" != "$REGISTERED_IP" ]; then
257 if [ $VERBOSE_MODE -gt 2 ]; then
258 write_log
7 "Verbose Mode: $VERBOSE_MODE - NO UPDATE send"
259 elif [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
260 write_log
7 "Update needed - L: '$LOCAL_IP' <> R: '$REGISTERED_IP'"
262 write_log
7 "Forced Update - L: '$LOCAL_IP' == R: '$REGISTERED_IP'"
266 [ $VERBOSE_MODE -lt 3 ] && {
267 # only send if VERBOSE_MODE < 3
268 send_update
"$LOCAL_IP"
269 ERR_LAST
=$?
# save return value
272 # error sending local IP to provider
273 # we have no communication error (handled inside send_update/do_transfer)
274 # but update was not recognized
275 # do NOT retry after RETRY_SECONDS, do retry after CHECK_SECONDS
276 # to early retrys will block most DDNS provider
277 # providers answer is checked inside send_update() function
278 if [ $ERR_LAST -eq 0 ]; then
279 get_uptime LAST_TIME
# we send update, so
280 echo $LAST_TIME > $UPDFILE # save LASTTIME to file
281 [ "$LOCAL_IP" != "$REGISTERED_IP" ] && write_log
6 "Update successful - IP '$LOCAL_IP' send"
282 [ "$LOCAL_IP" = "$REGISTERED_IP" ] || write_log
6 "Forced update successful - IP: '$LOCAL_IP' send"
284 write_log
3 "Can not update IP at DDNS Provider"
288 # now we wait for check interval before testing if update was recognized
289 # only sleep if VERBOSE_MODE <= 2 because otherwise nothing was send
290 [ $VERBOSE_MODE -le 2 ] && {
291 write_log
7 "Waiting $CHECK_SECONDS seconds (Check Interval)"
292 sleep $CHECK_SECONDS &
294 wait $PID_SLEEP # enable trap-handler
296 } || write_log
7 "Verbose Mode: $VERBOSE_MODE - NO Check Interval waiting"
298 REGISTERED_IP
="" # clear variable
299 get_registered_ip REGISTERED_IP
# get registered/public IP
301 # IP's are still different
302 if [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then
303 if [ $VERBOSE_MODE -le 1 ]; then # VERBOSE_MODE <=1 then retry
304 ERR_UPDATE
=$
(( $ERR_UPDATE + 1 ))
305 [ $retry_count -gt 0 -a $ERR_UPDATE -gt $retry_count ] && \
306 write_log
14 "Updating IP at DDNS provider failed after $retry_count retries"
307 write_log
4 "Updating IP at DDNS provider failed - starting retry $ERR_UPDATE/$retry_count"
308 continue # loop to beginning
310 write_log
4 "Updating IP at DDNS provider failed"
311 write_log
7 "Verbose Mode: $VERBOSE_MODE - NO retry"; exit 1
314 # we checked successful the last update
315 ERR_UPDATE
=0 # reset error counter
318 # force_update=0 or VERBOSE_MODE > 1 - leave here
319 [ $VERBOSE_MODE -gt 1 ] && write_log
7 "Verbose Mode: $VERBOSE_MODE - NO reloop"
320 [ $FORCE_SECONDS -eq 0 ] && write_log
6 "Configured to run once"
321 [ $VERBOSE_MODE -gt 1 -o $FORCE_SECONDS -eq 0 ] && exit 0
323 write_log
6 "Rerun IP check at $(eval $DATE_PROG)"
325 # we should never come here there must be a programming error
326 write_log
12 "Error in 'dynamic_dns_updater.sh - program coding error"