ddns-scripts: bump to version 2.7.8-1
[feed/packages.git] / net / ddns-scripts / files / ddns.defaults
1 #!/bin/sh
2
3 g_pslfile=/usr/share/public_suffix_list.dat.gz
4 [ -f "$g_pslfile" ] || g_pslfile="$(dirname $0)/public_suffix_list.dat.gz"
5
6 g_pslerr=0
7 g_cfgfile="ddns"
8
9 # modify timer settings from interval and unit to dhms format
10 timer2dhms() {
11 # $1 Number and
12 # $2 Unit of time interval
13 local t=0
14 case $2 in
15 days) t=$(( $1 * 86400 ));;
16 hours) t=$(( $1 * 3600 ));;
17 minutes) t=$(( $1 * 60 ));;
18 *) t=$1;;
19 esac
20
21 local d=$(( $t / 86400 ))
22 local h=$(( $t % 86400 / 3600 ))
23 local m=$(( $t % 3600 / 60 ))
24 local s=$(( $t % 60 ))
25 if [ $d -gt 0 ]; then printf "%dd %02dh %02dm %02ds" "$d" "$h" "$m" "$s"
26 elif [ $h -gt 0 ]; then printf "%dh %02dm %02ds" "$h" "$m" "$s"
27 elif [ $m -gt 0 ]; then printf "%dm %02ds" "$m" "$s"
28 else printf "%ds" "$s"; fi
29
30 unset d h m s t
31 return 0
32 }
33
34 # using function to not confuse function calls with existing ones inside /lib/functions.sh
35 update_config() {
36 uc_uci="$(which uci) -q" # ignore errors
37 uc_cfg=""
38 uc_name=""
39 uc_var=""
40 uc_val=""
41 package() { return 0; }
42 config () {
43 uc_cfg="$1"
44 uc_name="$2"
45
46 # Type = ddns Name = global
47 if [ "$uc_cfg" = "$g_cfgfile" -a "$uc_name" = "global" ]; then
48 option() {
49 uc_var="$1"; shift
50 uc_val="$*"
51 case "$uc_var" in
52 allow_local_ip) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_privateip";;
53 date_format) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_dateformat";;
54 log_lines) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_loglines";;
55 log_dir) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_logdir";;
56 run_dir) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_rundir";;
57 # leave all other options currently unchanged
58 *) ;;
59 esac
60 }
61
62 # Type = service Name = ???
63 elif [ "$uc_cfg" = "service" ]; then
64 option() {
65 uc_var="$1"; shift
66 uc_val="$*"
67 case "$uc_var" in
68 # fix some option service_name values
69 # and some settings for specific providers
70 service_name|upd_provider)
71 case "$uc_val" in
72 freedns\.afraid\.org|afraid\.org)
73 $uc_uci set $g_cfgfile.$uc_name.$uc_var="afraid.org-keyauth";;
74 Bind-nsupdate)
75 $uc_uci set $g_cfgfile.$uc_name.$uc_var="bind-nsupdate";;
76 dyndns\.org|dyndns\.com)
77 $uc_uci set $g_cfgfile.$uc_name.$uc_var="dyn.com";;
78 free\.editdns\.net)
79 $uc_uci set $g_cfgfile.$uc_name.$uc_var="editdns.net";;
80 FreeDNS\.42\.pl)
81 $uc_uci set $g_cfgfile.$uc_name.$uc_var="freedns.42.pl";;
82 domains\.google\.com)
83 $uc_uci set $g_cfgfile.$uc_name.$uc_var="google.com";;
84 loopia\.com)
85 $uc_uci set $g_cfgfile.$uc_name.$uc_var="loopia.se";;
86 NoIP\.com|No-IP\.com)
87 $uc_uci set $g_cfgfile.$uc_name.$uc_var="no-ip.com";;
88 spdns\.de)
89 $uc_uci set $g_cfgfile.$uc_name.$uc_var="spdyn.de";;
90 strato\.de)
91 $uc_uci set $g_cfgfile.$uc_name.$uc_var="strato.com";;
92 *)
93 # all others leave unchanged
94 ;;
95 esac
96 # rename option service_name to option upd_provider
97 # $uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_provider"
98 ;;
99 domain|upd_object)
100 # verify if lookup_host is set
101 $uc_uci get $g_cfgfile.$uc_name.lookup_host >/dev/null 2>&1 || \
102 $uc_uci set $g_cfgfile.$uc_name.lookup_host="$uc_val"
103 if [ -f "$g_pslfile" ]; then
104 # if service_name/upd_provider cloudflare_v1 then change domain/upd_object to new syntax
105 # there is no sort order inside uci data so we need multiple checks
106 uco_provider=$($uc_uci get $g_cfgfile.$uc_name.upd_provider 2>/dev/null) || \
107 uco_provider=$($uc_uci get $g_cfgfile.$uc_name.service_name 2>/dev/null)
108 unset uco_provider
109 fi
110 # rename option domain to option upd_object
111 # $uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_object"
112 ;;
113 # dns_server)
114 # # if bind-nsupdate takeover old "dns_server" value as new "upd_nsupd_server" value
115 # uco_provider=$($uc_uci get $g_cfgfile.$uc_name.upd_provider 2>/dev/null) || \
116 # uco_provider=$($uc_uci get $g_cfgfile.$uc_name.service_name 2>/dev/null)
117 # [ "$uco_provider" = "Bind-nsupdate" -o \
118 # "$uco_provider" = "bind-nsupdate" ] && \
119 # $uc_uci set $g_cfgfile.$uc_name.upd_nsupd_server="$uc_val"
120 # # rename option dns_server to new option global_dnssvr
121 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="global_dnssvr"
122 # ;;
123 # bind_network)
124 # $udc_uci set $g_cfgfile.$uc_name.upd_url_bindnet="$uc_val"
125 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="lip_url_bindnet"
126 # ;;
127 # proxy)
128 # # proxy value must include protocoll
129 # $udc_uci set $g_cfgfile.$uc_name.$uc_var="http://$uc_val"
130 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_url_proxy"
131 # ;;
132 # use_ipv6)
133 # $udc_uci set $g_cfgfile.$uc_name.$uc_var="$(( 4 + ( 2 * $uc_val ) ))"
134 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_ipversion"
135 # TODO update_url)
136 # TODO update_script)
137 # other renames
138 # TODO lookup_host) -> rip_host
139 # enabled) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_enabled";;
140 # force_dnstcp) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="rip_host_dnstcp";;
141 # is_glue) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="rip_host_isglue";;
142 # ip_interface) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="lip_iface";;
143 # ip_network) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="lip_net";;
144 # use_https) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_url_secure";;
145 # cacert) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_url_cacert";;
146 # username) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_username";;
147 # password) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_password";;
148 # param_opt) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_paramopt";;
149 # param_enc) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_paramenc";;
150
151 # leave all other options currently unchanged
152 *) ;;
153 esac
154 return 0
155 }
156 return 0
157
158 # ignore unknown
159 else
160 return 0
161 fi
162 }
163
164 # read config file
165 uc_data=$($uc_uci -S -n export "$g_cfgfile")
166 uc_ret="$?"
167 # Error then create config file
168 [ $uc_ret -ne 0 ] && {
169 touch /etc/config/$uc_cfgfile
170 chmod 644 /etc/config/$uc_cfgfile
171 }
172 # No error and uc_data then execute (eval)
173 # this will call functions defined above
174 [ $uc_ret -eq 0 -a -n "$uc_data" ] && eval "$uc_data"
175
176 # add config ddns "global" (ignore error if exists)
177 $uc_uci set ddns.global="$g_cfgfile"
178
179 # write changes to config file
180 $uc_uci commit "$g_cfgfile"
181
182 unset uc_uci uc_cfg uc_name uc_var uc_val uc_ret uc_data
183 return 0
184 }
185
186 # clear LuCI indexcache
187 rm -f /tmp/luci-indexcache >/dev/null 2>&1
188
189 # do config update
190 update_config
191
192 #cleanup
193 [ $g_pslerr -ne 0 ] && {
194 unset g_pslfile g_pslerr g_cfgfile
195 return 1
196 }
197
198 [ -f "$g_pslfile" ] && rm -f "$g_pslfile"
199 unset g_pslfile g_pslerr g_cfgfile
200 return 0
201