2 # /usr/lib/dynamic_dns/dynamic_dns_updater.sh
4 # Written by Eric Paul Bishop, Janary 2008
5 # Distributed under the terms of the GNU General Public License (GPL) version 2.0
7 # This script is (loosely) based on the one posted by exobyte in the forums here:
8 # http://forum.openwrt.org/viewtopic.php?id=14040
11 . /usr/lib/ddns/dynamic_dns_functions.sh
15 if [ -z "$service_id" ]
17 echo "ERRROR: You must specify a service id (the section name in the /etc/config/ddns file) to initialize dynamic DNS."
21 #default mode is verbose_mode, but easily turned off with second parameter
28 ###############################################################
29 # Leave this comment here, to clearly document variable names
30 # that are expected/possible
32 # Now use load_all_config_options to load config
33 # options, which is a much more flexible solution.
38 #config_get enabled $service_id enabled
39 #config_get service_name $service_id service_name
40 #config_get update_url $service_id update_url
43 #config_get username $service_id username
44 #config_get password $service_id password
45 #config_get domain $service_id domain
48 #config_get use_https $service_id use_https
49 #config_get cacert $service_id cacert
51 #config_get ip_source $service_id ip_source
52 #config_get ip_interface $service_id ip_interface
53 #config_get ip_network $service_id ip_network
54 #config_get ip_url $service_id ip_url
56 #config_get force_interval $service_id force_interval
57 #config_get force_unit $service_id force_unit
59 #config_get check_interval $service_id check_interval
60 #config_get check_unit $service_id check_unit
61 #########################################################
62 load_all_config_options "ddns" "$service_id"
66 if [ -z "$check_interval" ]
71 if [ -z "$retry_interval" ]
76 if [ -z "$check_unit" ]
82 if [ -z "$force_interval" ]
87 if [ -z "$force_unit" ]
92 if [ -z "$use_https" ]
101 if [ "x$use_https" = "x1" ]
103 retrieve_prog="/usr/bin/curl "
106 retrieve_prog="${retrieve_prog}--cacert $cacert "
107 elif [ -d "$cacert" ]
109 retrieve_prog="${retrieve_prog}--capath $cacert "
112 retrieve_prog="/usr/bin/wget -O - ";
115 service_file="/usr/lib/ddns/services"
117 ip_regex="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
123 #determine what update url we're using if the service_name is supplied
124 if [ -n "$service_name" ]
126 #remove any lines not containing data, and then make sure fields are enclosed in double quotes
127 quoted_services=$(cat $service_file | grep "^[\t ]*[^#]" | awk ' gsub("\x27", "\"") { if ($1~/^[^\"]*$/) $1="\""$1"\"" }; { if ( $NF~/^[^\"]*$/) $NF="\""$NF"\"" }; { print $0 }' )
130 #echo "quoted_services = $quoted_services"
133 for service_line in $quoted_services
135 #grep out proper parts of data and use echo to remove quotes
136 next_name=$(echo $service_line | grep -o "^[\t ]*\"[^\"]*\"" | xargs -r -n1 echo)
137 next_url=$(echo $service_line | grep -o "\"[^\"]*\"[\t ]*$" | xargs -r -n1 echo)
139 if [ "$next_name" = "$service_name" ]
147 if [ "x$use_https" = x1 ]
149 update_url=$(echo $update_url | sed -e 's/^http:/https:/')
153 verbose_echo "update_url=$update_url"
157 #if this service isn't enabled then quit
158 if [ "$enabled" != "1" ]
167 #compute update interval in seconds
168 case "$force_unit" in
170 force_interval_seconds=$(($force_interval*60*60*24))
173 force_interval_seconds=$(($force_interval*60*60))
176 force_interval_seconds=$(($force_interval*60))
179 force_interval_seconds=$force_interval
183 force_interval_seconds=$(($force_interval*60*60))
189 #compute check interval in seconds
190 case "$check_unit" in
192 check_interval_seconds=$(($check_interval*60*60*24))
195 check_interval_seconds=$(($check_interval*60*60))
198 check_interval_seconds=$(($check_interval*60))
201 check_interval_seconds=$check_interval
205 check_interval_seconds=$check_interval
211 #compute retry interval in seconds
212 case "$retry_unit" in
214 retry_interval_seconds=$(($retry_interval*60*60*24))
217 retry_interval_seconds=$(($retry_interval*60*60))
220 retry_interval_seconds=$(($retry_interval*60))
223 retry_interval_seconds=$retry_interval
227 retry_interval_seconds=$retry_interval
233 verbose_echo "force seconds = $force_interval_seconds"
234 verbose_echo "check seconds = $check_interval_seconds"
236 #kill old process if it exists & set new pid file
237 if [ -d /var/run/dynamic_dns ]
239 #if process is already running, stop it
240 if [ -e "/var/run/dynamic_dns/$service_id.pid" ]
242 old_pid=$(cat /var/run/dynamic_dns/$service_id.pid)
243 test_match=$(ps | grep "^[\t ]*$old_pid")
244 verbose_echo "old process id (if it exists) = \"$test_match\""
245 if [ -n "$test_match" ]
252 #make dir since it doesn't exist
253 mkdir /var/run/dynamic_dns
255 echo $$ > /var/run/dynamic_dns/$service_id.pid
260 #determine when the last update was
261 current_time=$(monotonic_time)
262 last_update=$(( $current_time - (2*$force_interval_seconds) ))
263 if [ -e "/var/run/dynamic_dns/$service_id.update" ]
265 last_update=$(cat /var/run/dynamic_dns/$service_id.update)
267 time_since_update=$(($current_time - $last_update))
270 human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
271 verbose_echo "time_since_update = $human_time_since_update hours"
276 registered_ip=$(echo $(nslookup "$domain" 2>/dev/null) | grep -o "Name:.*" | grep -o "$ip_regex")
279 #do update and then loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
283 current_ip=$(get_current_ip)
286 current_time=$(monotonic_time)
287 time_since_update=$(($current_time - $last_update))
290 verbose_echo "Running IP check..."
291 verbose_echo "current system ip = $current_ip"
292 verbose_echo "registered domain ip = $registered_ip"
295 if [ "$current_ip" != "$registered_ip" ] || [ $force_interval_seconds -lt $time_since_update ]
297 verbose_echo "update necessary, performing update ..."
300 final_url=$update_url
301 for option_var in $ALL_OPTION_VARIABLES
303 if [ "$option_var" != "update_url" ]
305 replace_name=$(echo "\[$option_var\]" | tr 'a-z' 'A-Z')
306 replace_value=$(eval echo "\$$option_var")
307 replace_value=$(echo $replace_value | sed -f /usr/lib/ddns/url_escape.sed)
308 final_url=$(echo $final_url | sed s^"$replace_name"^"$replace_value"^g )
311 final_url=$(echo $final_url | sed s/"\[HTTPAUTH\]"/"$username${password:+:$password}"/g )
312 final_url=$(echo $final_url | sed s/"\[IP\]"/"$current_ip"/g )
315 verbose_echo "updating with url=\"$final_url\""
317 #here we actually connect, and perform the update
318 update_output=$( $retrieve_prog "$final_url" )
321 verbose_echo "update failed"
322 sleep $retry_interval_seconds
326 verbose_echo "Update Output:"
327 verbose_echo "$update_output"
330 #save the time of the update
331 current_time=$(monotonic_time)
332 last_update=$current_time
333 time_since_update='0'
334 registered_ip=$current_ip
337 verbose_echo "update complete, time is: $human_time"
339 echo "$last_update" > "/var/run/dynamic_dns/$service_id.update"
342 human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
343 verbose_echo "update unnecessary"
344 verbose_echo "time since last update = $human_time_since_update hours"
345 verbose_echo "the time is now $human_time"
348 #sleep for 10 minutes, then re-check ip && time since last update
349 sleep $check_interval_seconds
352 #should never get here since we're a daemon, but I'll throw it in anyway