1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
#!/bin/sh
# /usr/lib/dynamic_dns/dynamic_dns_updater.sh
#
# Written by Eric Paul Bishop, Janary 2008
# Distributed under the terms of the GNU General Public License (GPL) version 2.0
#
# This script is (loosely) based on the one posted by exobyte in the forums here:
# http://forum.openwrt.org/viewtopic.php?id=14040
#
. /usr/lib/ddns/dynamic_dns_functions.sh
service_id=$1
if [ -z "$service_id" ]
then
echo "ERRROR: You must specify a service id (the section name in the /etc/config/ddns file) to initialize dynamic DNS."
return 1
fi
#default mode is verbose_mode, but easily turned off with second parameter
verbose_mode="1"
if [ -n "$2" ]
then
verbose_mode="$2"
fi
###############################################################
# Leave this comment here, to clearly document variable names
# that are expected/possible
#
# Now use load_all_config_options to load config
# options, which is a much more flexible solution.
#
#
#config_load "ddns"
#
#config_get enabled $service_id enabled
#config_get service_name $service_id service_name
#config_get update_url $service_id update_url
#
#
#config_get username $service_id username
#config_get password $service_id password
#config_get domain $service_id domain
#
#
#config_get use_https $service_id use_https
#config_get use_syslog $service_id use_syslog
#config_get cacert $service_id cacert
#
#config_get ip_source $service_id ip_source
#config_get ip_interface $service_id ip_interface
#config_get ip_network $service_id ip_network
#config_get ip_url $service_id ip_url
#
#config_get force_interval $service_id force_interval
#config_get force_unit $service_id force_unit
#
#config_get check_interval $service_id check_interval
#config_get check_unit $service_id check_unit
#########################################################
load_all_config_options "ddns" "$service_id"
#some defaults
if [ -z "$check_interval" ]
then
check_interval=600
fi
if [ -z "$retry_interval" ]
then
retry_interval=60
fi
if [ -z "$check_unit" ]
then
check_unit="seconds"
fi
if [ -z "$force_interval" ]
then
force_interval=72
fi
if [ -z "$force_unit" ]
then
force_unit="hours"
fi
if [ -z $use_syslog ]
then
use_syslog=0
fi
if [ -z "$use_https" ]
then
use_https=0
fi
#some constants
retrieve_prog="/usr/bin/wget -O - ";
if [ "x$use_https" = "x1" ]
then
/usr/bin/wget --version 2>&1 |grep -q "\+ssl"
if [ $? -eq 0 ]
then
if [ -f "$cacert" ]
then
retrieve_prog="${retrieve_prog}--ca-certificate=${cacert} "
elif [ -d "$cacert" ]
then
retrieve_prog="${retrieve_prog}--ca-directory=${cacert} "
fi
else
retrieve_prog="/usr/bin/curl "
if [ -f "$cacert" ]
then
retrieve_prog="${retrieve_prog}--cacert $cacert "
elif [ -d "$cacert" ]
then
retrieve_prog="${retrieve_prog}--capath $cacert "
fi
fi
fi
service_file="/usr/lib/ddns/services"
ip_regex="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
NEWLINE_IFS='
'
#determine what update url we're using if the service_name is supplied
if [ -n "$service_name" ]
then
#remove any lines not containing data, and then make sure fields are enclosed in double quotes
quoted_services=$(cat $service_file | grep "^[\t ]*[^#]" | awk ' gsub("\x27", "\"") { if ($1~/^[^\"]*$/) $1="\""$1"\"" }; { if ( $NF~/^[^\"]*$/) $NF="\""$NF"\"" }; { print $0 }' )
#echo "quoted_services = $quoted_services"
OLD_IFS=$IFS
IFS=$NEWLINE_IFS
for service_line in $quoted_services
do
#grep out proper parts of data and use echo to remove quotes
next_name=$(echo $service_line | grep -o "^[\t ]*\"[^\"]*\"" | xargs -r -n1 echo)
next_url=$(echo $service_line | grep -o "\"[^\"]*\"[\t ]*$" | xargs -r -n1 echo)
if [ "$next_name" = "$service_name" ]
then
update_url=$next_url
fi
done
IFS=$OLD_IFS
fi
if [ "x$use_https" = x1 ]
then
update_url=$(echo $update_url | sed -e 's/^http:/https:/')
fi
verbose_echo "update_url=$update_url"
#if this service isn't enabled then quit
if [ "$enabled" != "1" ]
then
return 0
fi
#compute update interval in seconds
case "$force_unit" in
"days" )
force_interval_seconds=$(($force_interval*60*60*24))
;;
"hours" )
force_interval_seconds=$(($force_interval*60*60))
;;
"minutes" )
force_interval_seconds=$(($force_interval*60))
;;
"seconds" )
force_interval_seconds=$force_interval
;;
* )
#default is hours
force_interval_seconds=$(($force_interval*60*60))
;;
esac
#compute check interval in seconds
case "$check_unit" in
"days" )
check_interval_seconds=$(($check_interval*60*60*24))
;;
"hours" )
check_interval_seconds=$(($check_interval*60*60))
;;
"minutes" )
check_interval_seconds=$(($check_interval*60))
;;
"seconds" )
check_interval_seconds=$check_interval
;;
* )
#default is seconds
check_interval_seconds=$check_interval
;;
esac
#compute retry interval in seconds
case "$retry_unit" in
"days" )
retry_interval_seconds=$(($retry_interval*60*60*24))
;;
"hours" )
retry_interval_seconds=$(($retry_interval*60*60))
;;
"minutes" )
retry_interval_seconds=$(($retry_interval*60))
;;
"seconds" )
retry_interval_seconds=$retry_interval
;;
* )
#default is seconds
retry_interval_seconds=$retry_interval
;;
esac
verbose_echo "force seconds = $force_interval_seconds"
verbose_echo "check seconds = $check_interval_seconds"
#kill old process if it exists & set new pid file
if [ -d /var/run/dynamic_dns ]
then
#if process is already running, stop it
if [ -e "/var/run/dynamic_dns/$service_id.pid" ]
then
old_pid=$(cat /var/run/dynamic_dns/$service_id.pid)
test_match=$(ps | grep "^[\t ]*$old_pid")
verbose_echo "old process id (if it exists) = \"$test_match\""
if [ -n "$test_match" ]
then
kill $old_pid
fi
fi
else
#make dir since it doesn't exist
mkdir /var/run/dynamic_dns
fi
echo $$ > /var/run/dynamic_dns/$service_id.pid
#determine when the last update was
current_time=$(monotonic_time)
last_update=$(( $current_time - (2*$force_interval_seconds) ))
if [ -e "/var/run/dynamic_dns/$service_id.update" ]
then
last_update=$(cat /var/run/dynamic_dns/$service_id.update)
fi
time_since_update=$(($current_time - $last_update))
human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
verbose_echo "time_since_update = $human_time_since_update hours"
#do update and then loop endlessly, checking ip every check_interval and forcing an updating once every force_interval
while [ true ]
do
registered_ip=$(echo $(nslookup "$domain" 2>/dev/null) | grep -o "Name:.*" | grep -o "$ip_regex")
current_ip=$(get_current_ip)
current_time=$(monotonic_time)
time_since_update=$(($current_time - $last_update))
syslog_echo "Running IP check ..."
verbose_echo "Running IP check..."
verbose_echo "current system ip = $current_ip"
verbose_echo "registered domain ip = $registered_ip"
if [ "$current_ip" != "$registered_ip" ] || [ $force_interval_seconds -lt $time_since_update ]
then
verbose_echo "update necessary, performing update ..."
#do replacement
final_url=$update_url
for option_var in $ALL_OPTION_VARIABLES
do
if [ "$option_var" != "update_url" ]
then
replace_name=$(echo "\[$option_var\]" | tr 'a-z' 'A-Z')
replace_value=$(eval echo "\$$option_var")
replace_value=$(echo $replace_value | sed -f /usr/lib/ddns/url_escape.sed)
final_url=$(echo $final_url | sed s^"$replace_name"^"$replace_value"^g )
fi
done
final_url=$(echo $final_url | sed s^"\[HTTPAUTH\]"^"${username//^/\\^}${password:+:${password//^/\\^}}"^g )
final_url=$(echo $final_url | sed s/"\[IP\]"/"$current_ip"/g )
verbose_echo "updating with url=\"$final_url\""
#here we actually connect, and perform the update
update_output=$( $retrieve_prog "$final_url" )
if [ $? -gt 0 ]
then
syslog_echo "update failed, retrying in $retry_interval_seconds seconds"
verbose_echo "update failed"
sleep $retry_interval_seconds
continue
fi
syslog_echo "Update successful"
verbose_echo "Update Output:"
verbose_echo "$update_output"
verbose_echo ""
#save the time of the update
current_time=$(monotonic_time)
last_update=$current_time
time_since_update='0'
registered_ip=$current_ip
human_time=$(date)
verbose_echo "update complete, time is: $human_time"
echo "$last_update" > "/var/run/dynamic_dns/$service_id.update"
else
human_time=$(date)
human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
verbose_echo "update unnecessary"
verbose_echo "time since last update = $human_time_since_update hours"
verbose_echo "the time is now $human_time"
fi
#sleep for 10 minutes, then re-check ip && time since last update
sleep $check_interval_seconds
done
#should never get here since we're a daemon, but I'll throw it in anyway
return 0
|