ddns.defaults (10989B)
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 "cloudflare.com-v1" domain to new syntax 10 # returns "host[.subdom]@domain.TLD" of given FQDN ############################# 11 mod_cloudflare_v1_domain() { 12 # $1 entry to validate/split 13 [ -f "$g_pslfile" ] || return 1 14 15 [ $# -ne 1 -o -z "$1" ] && \ 16 { printf "%s\\n" "mod_cloudflare_v1_domain() - Invalid number of parameters" >&2; return 1; } 17 18 local mcd_fqdn=$1 19 local mcd_fsub="" 20 local mcd_fdom="" 21 local mcd_ctld="" 22 local mcd_ftld="" 23 24 # check if already new syntax, "@" inside string 25 if [ $( printf "%s" "$mcd_fqdn" | grep -cF "@" 2>/dev/null ) -gt 0 ]; then 26 # already done 27 printf "%s" "$mcd_fqdn" 28 return 0 29 fi 30 31 # we need to do in one line because otherwise sh doesn't work correctly 32 # to lower | replace "." to " " | awk invert word order 33 set -- $(printf %s "$mcd_fqdn" | tr [A-Z] [a-z] | tr "." " " \ 34 | awk '{do printf "%s"(NF>1?OFS:ORS),$NF;while (--NF)}' ) 35 36 while [ -n "${1:-}" ] ; do # as long we have parameters 37 if [ -z "$mcd_ctld" ]; then # first loop 38 mcd_ctld="$1" # CURRENT TLD to look at 39 shift 40 else 41 mcd_ctld="$1.$mcd_ctld" # Next TLD to look at 42 shift 43 fi 44 # check if TLD exact match in public_suffix_name.dat, save TLD 45 zcat $g_pslfile | grep -E "^$mcd_ctld$" >/dev/null 2>&1 && { 46 mcd_ftld="$mcd_ctld" # save found 47 mcd_fdom="${1:-}" # save domain next step might be invalid 48 continue 49 } 50 # check if match any "*" in public_suffix_name.dat, 51 zcat $g_pslfile | grep -E "^\*.$mcd_ctld$" >/dev/null 2>&1 && { 52 [ -z "${1:-}" ] && break # no more data break 53 # check if next level TLD match excludes "!" in tld_names.dat 54 if zcat $g_pslfile | grep -E "^!$1.$mcd_ctld$" >/dev/null 2>&1 ; then 55 mcd_ftld="$mcd_ctld" # Yes 56 else 57 mcd_ftld="$1.$mcd_ctld" 58 shift 59 fi 60 mcd_fdom="$1"; shift 61 } 62 [ -n "$mcd_ftld" ] && break # we have something valid, break 63 done 64 65 # the leftover parameters are the HOST/SUBDOMAIN 66 while [ -n "${1:-}" ]; do 67 mcd_fsub="${1}${mcd_fsub:+.$mcd_fsub}" # remember we need to invert 68 shift # and insert dot if mcd_fsub not empty 69 done 70 71 # now validate found data 72 [ -z "$mcd_ftld" ] && { printf "%s\\n" "mod_cloudflare_v1_domain() - no TLD not found in '$mcd_fqdn'" >&1; return 1; } 73 [ -z "$mcd_fdom" ] && { printf "%s\\n" "mod_cloudflare_v1_domain() - no registrable Domain not found in '$mcd_fqdn'" >&1; return 1; } 74 75 # return data 76 printf "%s" "${mcd_fsub:+${mcd_fsub}@}${mcd_fdom}.${mcd_ftld}" 77 return 0 78 } 79 80 # modify timer settings from interval and unit to dhms format 81 timer2dhms() { 82 # $1 Number and 83 # $2 Unit of time interval 84 local t=0 85 case $2 in 86 days) t=$(( $1 * 86400 ));; 87 hours) t=$(( $1 * 3600 ));; 88 minutes) t=$(( $1 * 60 ));; 89 *) t=$1;; 90 esac 91 92 local d=$(( $t / 86400 )) 93 local h=$(( $t % 86400 / 3600 )) 94 local m=$(( $t % 3600 / 60 )) 95 local s=$(( $t % 60 )) 96 if [ $d -gt 0 ]; then printf "%dd %02dh %02dm %02ds" "$d" "$h" "$m" "$s" 97 elif [ $h -gt 0 ]; then printf "%dh %02dm %02ds" "$h" "$m" "$s" 98 elif [ $m -gt 0 ]; then printf "%dm %02ds" "$m" "$s" 99 else printf "%ds" "$s"; fi 100 101 unset d h m s t 102 return 0 103 } 104 105 # using function to not confuse function calls with existing ones inside /lib/functions.sh 106 update_config() { 107 uc_uci="$(which uci) -q" # ignore errors 108 uc_cfg="" 109 uc_name="" 110 uc_var="" 111 uc_val="" 112 package() { return 0; } 113 config () { 114 uc_cfg="$1" 115 uc_name="$2" 116 117 # Type = ddns Name = global 118 if [ "$uc_cfg" = "$g_cfgfile" -a "$uc_name" = "global" ]; then 119 option() { 120 uc_var="$1"; shift 121 uc_val="$*" 122 case "$uc_var" in 123 allow_local_ip) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_privateip";; 124 date_format) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_dateformat";; 125 log_lines) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_loglines";; 126 log_dir) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_logdir";; 127 run_dir) $uc_uci rename $g_cfgfile.$uc_name.$uc_var="ddns_rundir";; 128 # leave all other options currently unchanged 129 *) ;; 130 esac 131 } 132 133 # Type = service Name = ??? 134 elif [ "$uc_cfg" = "service" ]; then 135 option() { 136 uc_var="$1"; shift 137 uc_val="$*" 138 case "$uc_var" in 139 # fix some option service_name values 140 # and some settings for specific providers 141 service_name|upd_provider) 142 case "$uc_val" in 143 freedns\.afraid\.org|afraid\.org) 144 $uc_uci set $g_cfgfile.$uc_name.$uc_var="afraid.org-keyauth";; 145 Bind-nsupdate) 146 $uc_uci set $g_cfgfile.$uc_name.$uc_var="bind-nsupdate";; 147 CloudFlare|cloudflare\.com|cloudflare\.com-v1) 148 # verify if lookup_host is set 149 $uc_uci get $g_cfgfile.$uc_name.lookup_host >/dev/null 2>&1 || { 150 ucv_domain=$($uc_uci get $g_cfgfile.$uc_name.domain 2>/dev/null) 151 $uc_uci set $g_cfgfile.$uc_name.lookup_host="$ucv_domain" 152 } 153 if [ -f "$g_pslfile" ]; then 154 # change value of domain/upd_object to new syntax 155 # there is no sort order inside uci data so we need multiple checks 156 ucv_domain=$($uc_uci get $g_cfgfile.$uc_name.domain 2>/dev/null) 157 ucv_object=$($uc_uci get $g_cfgfile.$uc_name.upd_object 2>/dev/null) 158 # still old option domain 159 if [ -n "$ucv_domain" ]; then 160 ucv_new=$(mod_cloudflare_v1_domain "$ucv_domain") || g_pslerr=1 161 # no error save data save data 162 [ $g_pslerr -eq 0 ] && \ 163 $uc_uci set $g_cfgfile.$uc_name.domain="$ucv_new" 164 fi 165 # already new option upd_object 166 if [ -n "$ucv_object" ]; then 167 ucv_new=$(mod_cloudflare_v1_domain "$ucv_object") || g_pslerr=1 168 # no error save data save data 169 [ $g_pslerr -eq 0 ] && \ 170 $uc_uci set $g_cfgfile.$uc_name.upd_object="$ucv_new" 171 fi 172 fi 173 unset ucv_domain ucv_object ucv_new 174 # set new option value 175 $uc_uci set $g_cfgfile.$uc_name.$uc_var="cloudflare.com-v1" 176 ;; 177 dyndns\.org|dyndns\.com) 178 $uc_uci set $g_cfgfile.$uc_name.$uc_var="dyn.com";; 179 free\.editdns\.net) 180 $uc_uci set $g_cfgfile.$uc_name.$uc_var="editdns.net";; 181 domains\.google\.com) 182 $uc_uci set $g_cfgfile.$uc_name.$uc_var="google.com";; 183 loopia\.com) 184 $uc_uci set $g_cfgfile.$uc_name.$uc_var="loopia.se";; 185 NoIP\.com|No-IP\.com) 186 $uc_uci set $g_cfgfile.$uc_name.$uc_var="no-ip.com";; 187 spdns\.de) 188 $uc_uci set $g_cfgfile.$uc_name.$uc_var="spdyn.de";; 189 strato\.de) 190 $uc_uci set $g_cfgfile.$uc_name.$uc_var="strato.com";; 191 *) 192 # all others leave unchanged 193 ;; 194 esac 195 # rename option service_name to option upd_provider 196 # $uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_provider" 197 ;; 198 domain|upd_object) 199 # verify if lookup_host is set 200 $uc_uci get $g_cfgfile.$uc_name.lookup_host >/dev/null 2>&1 || \ 201 $uc_uci set $g_cfgfile.$uc_name.lookup_host="$uc_val" 202 if [ -f "$g_pslfile" ]; then 203 # if service_name/upd_provider cloudflare_v1 then change domain/upd_object to new syntax 204 # there is no sort order inside uci data so we need multiple checks 205 uco_provider=$($uc_uci get $g_cfgfile.$uc_name.upd_provider 2>/dev/null) || \ 206 uco_provider=$($uc_uci get $g_cfgfile.$uc_name.service_name 2>/dev/null) 207 if [ "$uco_provider" = "CloudFlare" \ 208 -o "$uco_provider" = "cloudflare.com" \ 209 -o "$uco_provider" = "cloudflare.com-v1" ]; then 210 ucv_new=$(mod_cloudflare_v1_domain "$uc_val") || g_pslerr=1 211 # no error save data save data 212 [ $g_pslerr -eq 0 ] && \ 213 $uc_uci set $g_cfgfile.$uc_name.$uc_var="$ucv_new" 214 unset ucv_new 215 fi 216 unset uco_provider 217 fi 218 # rename option domain to option upd_object 219 # $uc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_object" 220 ;; 221 # dns_server) 222 # # if bind-nsupdate takeover old "dns_server" value as new "upd_nsupd_server" value 223 # uco_provider=$($uc_uci get $g_cfgfile.$uc_name.upd_provider 2>/dev/null) || \ 224 # uco_provider=$($uc_uci get $g_cfgfile.$uc_name.service_name 2>/dev/null) 225 # [ "$uco_provider" = "Bind-nsupdate" -o \ 226 # "$uco_provider" = "bind-nsupdate" ] && \ 227 # $uc_uci set $g_cfgfile.$uc_name.upd_nsupd_server="$uc_val" 228 # # rename option dns_server to new option global_dnssvr 229 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="global_dnssvr" 230 # ;; 231 # bind_network) 232 # $udc_uci set $g_cfgfile.$uc_name.upd_url_bindnet="$uc_val" 233 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="lip_url_bindnet" 234 # ;; 235 # proxy) 236 # # proxy value must include protocoll 237 # $udc_uci set $g_cfgfile.$uc_name.$uc_var="http://$uc_val" 238 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_url_proxy" 239 # ;; 240 # use_ipv6) 241 # $udc_uci set $g_cfgfile.$uc_name.$uc_var="$(( 4 + ( 2 * $uc_val ) ))" 242 # $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_ipversion" 243 # TODO update_url) 244 # TODO update_script) 245 # other renames 246 # TODO lookup_host) -> rip_host 247 # enabled) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_enabled";; 248 # force_dnstcp) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="rip_host_dnstcp";; 249 # is_glue) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="rip_host_isglue";; 250 # ip_interface) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="lip_iface";; 251 # ip_network) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="lip_net";; 252 # use_https) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_url_secure";; 253 # cacert) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_url_cacert";; 254 # username) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_username";; 255 # password) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_password";; 256 # param_opt) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_paramopt";; 257 # param_enc) $udc_uci rename $g_cfgfile.$uc_name.$uc_var="upd_paramenc";; 258 259 # leave all other options currently unchanged 260 *) ;; 261 esac 262 return 0 263 } 264 return 0 265 266 # ignore unknown 267 else 268 return 0 269 fi 270 } 271 272 # read config file 273 uc_data=$($uc_uci -S -n export "$g_cfgfile") 274 uc_ret="$?" 275 # Error then create config file 276 [ $uc_ret -ne 0 ] && { 277 touch /etc/config/$uc_cfgfile 278 chmod 644 /etc/config/$uc_cfgfile 279 } 280 # No error and uc_data then execute (eval) 281 # this will call functions defined above 282 [ $uc_ret -eq 0 -a -n "$uc_data" ] && eval "$uc_data" 283 284 # add config ddns "global" (ignore error if exists) 285 $uc_uci set ddns.global="$g_cfgfile" 286 287 # write changes to config file 288 $uc_uci commit "$g_cfgfile" 289 290 unset uc_uci uc_cfg uc_name uc_var uc_val uc_ret uc_data 291 return 0 292 } 293 294 # clear LuCI indexcache 295 rm -f /tmp/luci-indexcache >/dev/null 2>&1 296 297 # do config update 298 update_config 299 300 #cleanup 301 [ $g_pslerr -ne 0 ] && { 302 unset g_pslfile g_pslerr g_cfgfile 303 return 1 304 } 305 306 [ -f "$g_pslfile" ] && rm -f "$g_pslfile" 307 unset g_pslfile g_pslerr g_cfgfile 308 return 0 309