ソースを参照

ddns-scripts: Copied from previous repository

Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
Nikos Mavrogiannopoulos 10 年 前
コミット
ced8b84f1c

+ 44
- 0
net/ddns-scripts/Makefile ファイルの表示

@@ -0,0 +1,44 @@
1
+include $(TOPDIR)/rules.mk
2
+
3
+PKG_NAME:=ddns-scripts
4
+PKG_VERSION:=1.0.0
5
+PKG_RELEASE:=22
6
+
7
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
8
+
9
+include $(INCLUDE_DIR)/package.mk
10
+
11
+define Package/ddns-scripts
12
+	SECTION:=net
13
+	CATEGORY:=Network
14
+        SUBMENU:=IP Addresses and Names
15
+	TITLE:=Dynamic DNS Scripts
16
+	PKGARCH:=all
17
+endef
18
+
19
+define Package/ddns-scripts/description
20
+	A highly configurable set of scripts for doing
21
+	dynamic dns updates
22
+endef
23
+
24
+define Build/Prepare
25
+endef
26
+
27
+define Build/Configure
28
+endef
29
+
30
+define Build/Compile
31
+endef
32
+
33
+define Package/ddns-scripts/conffiles
34
+/etc/config/ddns
35
+endef
36
+
37
+define Package/ddns-scripts/install
38
+	$(INSTALL_DIR) $(1)
39
+	$(CP) ./files/* $(1)/
40
+	$(INSTALL_DIR) $(1)/etc/init.d
41
+	$(INSTALL_BIN) ./files/etc/init.d/ddns $(1)/etc/init.d/
42
+endef
43
+
44
+$(eval $(call BuildPackage,ddns-scripts))

+ 97
- 0
net/ddns-scripts/files/etc/config/ddns ファイルの表示

@@ -0,0 +1,97 @@
1
+#################################################################
2
+# In order to enable dynamic dns you need at least one section,
3
+# and in that seciton the "enabled" option must be set to one
4
+# 
5
+# Each section represents an update to a different service
6
+#
7
+# You specify your domain name, your username and your password
8
+# with the optins "domain", "username" and "password" respectively
9
+#
10
+# Next you need to specify the name of the service you are 
11
+# connecting to "eg. dyndns.org".  The format of the update
12
+# urls for several different dynamic dns services is specified
13
+# in the /usr/lib/ddns/services file.  This list is hardly complete
14
+# as there are many, many different dynamic dns services.  If your
15
+# service is on the list you can merely specify it with the 
16
+# "service_name" option.  Otherwise you will need to determine
17
+# the format of the url to update with.  You can either add an
18
+# entry to the /usr/lib/ddns/services file or specify this with
19
+# the "update_url" option.
20
+#
21
+# We also need to specify the source of the ip address to associate with
22
+# your domain.  The "ip_source" option can be "network", "interface"
23
+# or "web", with "network" as the default.  
24
+#
25
+# If "ip_source" is "network" you specify a network section in your 
26
+# /etc/network config file (e.g. "wan", which is the default) with
27
+# the "ip_network" option.  If you specify "wan", you will update
28
+# with whatever the ip for your wan is.
29
+# 
30
+# If "ip_source" is "interface" you specify a hardware interface 
31
+# (e.g. "eth1") and whatever the current ip of this interface is
32
+# will be associated with the domain when an update is performed.
33
+#
34
+# If "ip_source" is "script" you specify a script to obtain ip address.
35
+# The "ip_script" option should contain path to your script.
36
+#
37
+# The last possibility is that "ip_source" is "web", which means
38
+# that in order to obtain our ip address we will connect to a 
39
+# website, and the first valid ip address listed on that page
40
+# will be assumed to be ours.  If you are behind another firewall
41
+# this is the best option since none of the local networks or 
42
+# interfaces will have the external ip.  The website to connect
43
+# to is specified by the "ip_url" option.  You may specify multiple
44
+# urls in the option, separated by whitespace.
45
+#
46
+# Finally we need to specify how often to check whether we need
47
+# to check whether the ip address has changed (and if so update
48
+# it) and how often we need to force an update ( many services
49
+# will expire your domain if you don't connect and do an update
50
+# every so often).  Use the "check_interval" to specify how
51
+# often to check whether an update is necessary, the "retry_interval"
52
+# to specify how often to retry in case the update has failed, and
53
+# the "force_interval" option to specify how often to force an
54
+# update.  Specify the units for these values with the "check_unit",
55
+# the "retry_init" and the "force_unit" options.  Units can be
56
+# "days", "hours", "minutes" or "seconds".  The default force_unit
57
+# is hours, the default retry_unit is seconds and the default
58
+# check_unit is seconds.  The default check_interval is 600 seconds,
59
+# or ten minutes.  The default retry_interval is 60 seconds, or one
60
+# minute. The default force_interval is 72 hours or 3 days.
61
+#
62
+#
63
+#########################################################
64
+
65
+config service "myddns"
66
+	option enabled		"0"
67
+	option interface	"wan"
68
+	option use_syslog	"1"
69
+
70
+	option service_name	"dyndns.org"
71
+	option domain		"mypersonaldomain.dyndns.org"
72
+	option username		"myusername"
73
+	option password		"mypassword"	
74
+	option use_https	"0"
75
+
76
+	option force_interval	"72"
77
+	option force_unit	"hours"
78
+	option check_interval	"10"
79
+	option check_unit	"minutes"
80
+	option retry_interval	"60"
81
+	option retry_unit	"seconds"
82
+
83
+	#option ip_source	"network" 
84
+	#option ip_network	"wan"
85
+
86
+	#option ip_source	"interface"
87
+	#option ip_interface	"eth0.1"
88
+
89
+	#option ip_source	"script"
90
+	#option	ip_script	"path to your scrip"
91
+
92
+	option ip_source	"web"
93
+	option ip_url		"http://checkip.dyndns.com/"
94
+
95
+	#option update_url	"http://[USERNAME]:[PASSWORD]@members.dyndns.org/nic/update?hostname=[DOMAIN]&myip=[IP]"
96
+
97
+

+ 9
- 0
net/ddns-scripts/files/etc/hotplug.d/iface/25-ddns ファイルの表示

@@ -0,0 +1,9 @@
1
+#!/bin/sh
2
+
3
+. /usr/lib/ddns/dynamic_dns_functions.sh
4
+
5
+if [ "$ACTION" = "ifup" ]; then
6
+	start_daemon_for_all_ddns_sections "$INTERFACE"
7
+fi
8
+
9
+

+ 12
- 0
net/ddns-scripts/files/etc/init.d/ddns ファイルの表示

@@ -0,0 +1,12 @@
1
+#!/bin/sh /etc/rc.common
2
+START=95
3
+
4
+start() {
5
+	. /usr/lib/ddns/dynamic_dns_functions.sh
6
+	start_daemon_for_all_ddns_sections
7
+}
8
+
9
+stop() {
10
+	killall -9 dynamic_dns_updater.sh
11
+}
12
+

+ 144
- 0
net/ddns-scripts/files/usr/lib/ddns/dynamic_dns_functions.sh ファイルの表示

@@ -0,0 +1,144 @@
1
+# /usr/lib/dynamic_dns/dynamic_dns_functions.sh
2
+#
3
+# Written by Eric Paul Bishop, Janary 2008
4
+# Distributed under the terms of the GNU General Public License (GPL) version 2.0
5
+#
6
+# This script is (loosely) based on the one posted by exobyte in the forums here:
7
+# http://forum.openwrt.org/viewtopic.php?id=14040
8
+
9
+
10
+
11
+. /lib/functions.sh
12
+. /lib/functions/network.sh
13
+
14
+
15
+#loads all options for a given package and section
16
+#also, sets all_option_variables to a list of the variable names
17
+load_all_config_options()
18
+{
19
+	pkg_name="$1"
20
+	section_id="$2"
21
+
22
+	ALL_OPTION_VARIABLES=""
23
+	# this callback loads all the variables
24
+	# in the section_id section when we do
25
+	# config_load. We need to redefine
26
+	# the option_cb for different sections
27
+	# so that the active one isn't still active
28
+	# after we're done with it.  For reference
29
+	# the $1 variable is the name of the option
30
+	# and $2 is the name of the section
31
+	config_cb()
32
+	{
33
+		if [ ."$2" = ."$section_id" ]; then
34
+			option_cb()
35
+			{
36
+				ALL_OPTION_VARIABLES="$ALL_OPTION_VARIABLES $1"
37
+			}
38
+		else
39
+			option_cb() { return 0; }
40
+		fi
41
+	}
42
+
43
+
44
+	config_load "$pkg_name"
45
+	for var in $ALL_OPTION_VARIABLES
46
+	do
47
+		config_get "$var" "$section_id" "$var"
48
+	done
49
+}
50
+
51
+
52
+get_current_ip()
53
+{
54
+
55
+	#if ip source is not defined, assume we want to get ip from wan 
56
+	if [ "$ip_source" != "interface" ] && [ "$ip_source" != "web" ] && [ "$ip_source" != "script" ]
57
+	then
58
+		ip_source="network"
59
+	fi
60
+
61
+	if [ "$ip_source" = "network" ]
62
+	then
63
+		if [ -z "$ip_network" ]
64
+		then
65
+			ip_network="wan"
66
+		fi
67
+	fi
68
+
69
+	current_ip='';
70
+	if [ "$ip_source" = "network" ]
71
+	then
72
+		network_get_ipaddr current_ip "$ip_network" || return
73
+	elif [ "$ip_source" = "interface" ]
74
+	then
75
+		current_ip=$(ifconfig $ip_interface | grep -o 'inet addr:[0-9.]*' | grep -o "$ip_regex")
76
+	elif [ "$ip_source" = "script" ]
77
+	then
78
+		# get ip from script
79
+		current_ip=$($ip_script)
80
+	else
81
+		# get ip from web
82
+		# we check each url in order in ip_url variable, and if no ips are found we use dyndns ip checker
83
+		# ip is set to FIRST expression in page that matches the ip_regex regular expression
84
+		for addr in $ip_url
85
+		do
86
+			if [ -z "$current_ip" ]
87
+			then
88
+				current_ip=$(echo $( wget -O - $addr 2>/dev/null) | grep -o "$ip_regex")
89
+			fi
90
+		done
91
+
92
+		#here we hard-code the dyndns checkip url in case no url was specified
93
+		if [ -z "$current_ip" ]
94
+		then
95
+			current_ip=$(echo $( wget -O - http://checkip.dyndns.org 2>/dev/null) | grep -o "$ip_regex")
96
+		fi
97
+	fi
98
+
99
+	echo "$current_ip"
100
+}
101
+
102
+
103
+verbose_echo()
104
+{
105
+	if [ "$verbose_mode" = 1 ]
106
+	then
107
+		echo $1
108
+	fi
109
+}
110
+
111
+syslog_echo()
112
+{
113
+	if [ "$use_syslog" = 1 ]
114
+	then
115
+		echo $1|logger -t ddns-scripts-$service_id
116
+	fi
117
+}
118
+
119
+start_daemon_for_all_ddns_sections()
120
+{
121
+	local event_interface="$1"
122
+
123
+	SECTIONS=""
124
+	config_cb() 
125
+	{
126
+		SECTIONS="$SECTIONS $2"
127
+	}
128
+	config_load "ddns"
129
+
130
+	for section in $SECTIONS
131
+	do
132
+		local iface
133
+		config_get iface "$section" interface "wan"
134
+		[ "$iface" = "$event_interface" ] || continue
135
+		/usr/lib/ddns/dynamic_dns_updater.sh $section 0 > /dev/null 2>&1 &
136
+	done
137
+}
138
+
139
+monotonic_time()
140
+{
141
+	local uptime
142
+	read uptime < /proc/uptime
143
+	echo "${uptime%%.*}"
144
+}

+ 360
- 0
net/ddns-scripts/files/usr/lib/ddns/dynamic_dns_updater.sh ファイルの表示

@@ -0,0 +1,360 @@
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
+

+ 360
- 0
net/ddns-scripts/files/usr/lib/ddns/dynamic_dns_updater.sh~ ファイルの表示

@@ -0,0 +1,360 @@
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
+

+ 65
- 0
net/ddns-scripts/files/usr/lib/ddns/services ファイルの表示

@@ -0,0 +1,65 @@
1
+# This file contains the update urls for various dynamic dns services.
2
+# Column one contains the service name, column two contains the update url.
3
+# within the update url there are 4 variables you can use: [USERNAME], 
4
+# [PASSWORD], [DOMAIN] and [IP].  These are substituted for the username, 
5
+# password, and domain name specified in the /etc/config/ddns file when an 
6
+# update is performed.  The IP is substituted for the current ip address of the
7
+# router.  These variables are case sensitive, while urls generally are not, so 
8
+# if you need to enter the same text in the url (which seems very unlikely) put 
9
+# that text in lowercase, while the variables should remain in uppercase
10
+
11
+"dyndns.org"		"http://[USERNAME]:[PASSWORD]@members.dyndns.org/nic/update?hostname=[DOMAIN]&myip=[IP]"
12
+"changeip.com"		"http://[USERNAME]:[PASSWORD]@nic.changeip.com/nic/update?u=[USERNAME]&p=[PASSWORD]&cmd=update&hostname=[DOMAIN]&ip=[IP]"
13
+"zoneedit.com"		"http://[USERNAME]:[PASSWORD]@dynamic.zoneedit.com/auth/dynamic.html?host=[DOMAIN]&dnsto=[IP]"
14
+"free.editdns.net"	"http://dyndns-free.editdns.net/api/dynLinux.php?p=[PASSWORD]&r=[DOMAIN]"
15
+
16
+#noip is an alias of no-ip, so allow both names for the same service
17
+"no-ip.com"		"http://[USERNAME]:[PASSWORD]@dynupdate.no-ip.com/nic/update?hostname=[DOMAIN]&myip=[IP]"
18
+"noip.com"		"http://[USERNAME]:[PASSWORD]@dynupdate.no-ip.com/nic/update?hostname=[DOMAIN]&myip=[IP]"
19
+
20
+#freedns.afraid.org is weird, you just need an update code, for which we use the password variable
21
+"freedns.afraid.org"	"http://freedns.afraid.org/dynamic/update.php?[PASSWORD]&address=[IP]"
22
+
23
+#### ADD YOURS HERE! ######################################################################################
24
+#                                                                                                         #
25
+# There are TONS of dynamic dns services out there. There's a huge list of them at:                       #
26
+# http://www.dmoz.org/Computers/Software/Internet/Servers/Address_Management/Dynamic_DNS_Services/        #
27
+# If anyone has time they could update this file to be compatible with a bunch of them                    #
28
+#                                                                                                         #
29
+###########################################################################################################
30
+
31
+# DNS Max and resellers' update urls
32
+"dnsmax.com"	"http://update.dnsmax.com/update/?username=[USERNAME]&password=[PASSWORD]&resellerid=1&clientname=openwrt&clientversion=8.09&protocolversion=2.0&updatehostname=[DOMAIN]&ip=[IP]"
33
+"thatip.com"	"http://update.dnsmax.com/update/?username=[USERNAME]&password=[PASSWORD]&resellerid=2&clientname=openwrt&clientversion=8.09&protocolversion=2.0&updatehostname=[DOMAIN]&ip=[IP]"
34
+
35
+# Hurricane Electric Dynamic DNS
36
+"he.net"		"http://[DOMAIN]:[PASSWORD]@dyn.dns.he.net/nic/update?hostname=[DOMAIN]&myip=[IP]" 
37
+
38
+# DNSdynamic.org
39
+"dnsdynamic.org"	"http://[USERNAME]:[PASSWORD]@www.dnsdynamic.org/api/?hostname=[DOMAIN]&myip=[IP]"
40
+
41
+# dnsExit.com free dynamic DNS update url
42
+"dnsexit.com"		"http://www.dnsexit.com/RemoteUpdate.sv?login=[USERNAME]&password=[PASSWORD]&host=[DOMAIN]&myip=[IP]"
43
+
44
+# OVH
45
+"ovh.com" "http://[USERNAME]:[PASSWORD]@www.ovh.com/nic/update?system=dyndns&hostname=[DOMAIN]&myip=[IP]"
46
+
47
+# dns-o-matic is a free service by opendns.com for updating multiple hosts and
48
+# dynamic dns services in one api call. To update all your configured services
49
+# at once, use "all.dnsomatic.com as the hostname.
50
+"dnsomatic.com" "http://[USERNAME]:[PASSWORD]@updates.dnsomatic.com/nic/update?hostname=[DOMAIN]&myip=[IP]"
51
+
52
+# 3322.org
53
+"3322.org"  "http://[USERNAME]:[PASSWORD]@members.3322.org/dyndns/update?system=dyndns&hostname=[DOMAIN]&myip=[IP]"
54
+
55
+# namecheap.com
56
+"namecheap.com" "http://dynamicdns.park-your-domain.com/update?host=[USERNAME]&domain=[DOMAIN]&password=[PASSWORD]&ip=[IP]"
57
+
58
+# easydns.com dynamic DNS
59
+"easydns.com"		"http://[USERNAME]:[PASSWORD]@api.cp.easydns.com/dyn/tomato.php?hostname=[DOMAIN]&myip=[IP]"
60
+
61
+# Winco DDNS
62
+"ddns.com.br"	"http://[DOMAIN]:[PASSWORD]@members.ddns.com.br/nic/update?hostname=[DOMAIN]&myip=[IP]"
63
+
64
+# Mythic Beasts (https://www.mythic-beasts.com) Dynamic DNS
65
+"mythic-beasts.com"	"http://dnsapi4.mythic-beasts.com/?domain=[USERNAME]&password=[PASSWORD]&command=REPLACE%20[DOMAIN]%2060%20A%20DYNAMIC_IP"

+ 25
- 0
net/ddns-scripts/files/usr/lib/ddns/url_escape.sed ファイルの表示

@@ -0,0 +1,25 @@
1
+# sed url escaping
2
+s:%:%25:g
3
+s: :%20:g
4
+s:<:%3C:g
5
+s:>:%3E:g
6
+s:#:%23:g
7
+s:{:%7B:g
8
+s:}:%7D:g
9
+s:|:%7C:g
10
+s:\\:%5C:g
11
+s:\^:%5E:g
12
+s:~:%7E:g
13
+s:\[:%5B:g
14
+s:\]:%5D:g
15
+s:`:%60:g
16
+s:;:%3B:g
17
+s:/:%2F:g
18
+s:?:%3F:g
19
+s^:^%3A^g
20
+s:@:%40:g
21
+s:=:%3D:g
22
+s:&:%26:g
23
+s:\$:%24:g
24
+s:\!:%21:g
25
+s:\*:%2A:g