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