You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.7 KiB

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