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.

172 lines
5.3 KiB

  1. #!/bin/sh
  2. # /usr/lib/ddns/dynamic_dns_lucihelper.sh
  3. #
  4. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  5. #.2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  6. # This script is used by luci-app-ddns
  7. #
  8. # variables in small chars are read from /etc/config/ddns as parameter given here
  9. # variables in big chars are defined inside these scripts as gloval vars
  10. # variables in big chars beginning with "__" are local defined inside functions only
  11. # set -vx #script debugger
  12. . /usr/lib/ddns/dynamic_dns_functions.sh # global vars are also defined here
  13. usage() {
  14. cat << EOF
  15. Usage:
  16. $MYPROG [options] -- command
  17. Commands:
  18. get_local_ip using given INTERFACE or NETWORK or SCRIPT or URL
  19. get_registered_ip for given FQDN
  20. verify_dns given DNS-SERVER
  21. verify_proxy given PROXY
  22. start start given SECTION
  23. reload force running ddns processes to reload changed configuration
  24. restart restart all ddns processes
  25. Parameters:
  26. -6 => use_ipv6=1 (default 0)
  27. -d DNS-SERVER => dns_server=SERVER[:PORT]
  28. -f => force_ipversion=1 (default 0)
  29. -g => is_glue=1 (default 0)
  30. -i INTERFACE => ip_interface=INTERFACE; ip_source="interface"
  31. -l FQDN => lookup_host=FQDN
  32. -n NETWORK => ip_network=NETWORK; ip_source="network"
  33. -p PROXY => proxy=[USER:PASS@]PROXY:PORT
  34. -s SCRIPT => ip_script=SCRIPT; ip_source="script"
  35. -t => force_dnstcp=1 (default 0)
  36. -u URL => ip_url=URL; ip_source="web"
  37. -S SECTION SECTION to start
  38. -h => show this help and exit
  39. -L => use_logfile=1 (default 0)
  40. -v LEVEL => VERBOSE=LEVEL (default 0)
  41. -V => show version and exit
  42. EOF
  43. }
  44. usage_err() {
  45. printf %s\\n "$MYPROG: $@" >&2
  46. usage >&2
  47. exit 255
  48. }
  49. # preset some variables, wrong or not set in ddns-functions.sh
  50. SECTION_ID="lucihelper"
  51. LOGFILE="$ddns_logdir/$SECTION_ID.log"
  52. DATFILE="$ddns_rundir/$SECTION_ID.$$.dat" # save stdout data of WGet and other extern programs called
  53. ERRFILE="$ddns_rundir/$SECTION_ID.$$.err" # save stderr output of WGet and other extern programs called
  54. DDNSPRG="/usr/lib/ddns/dynamic_dns_updater.sh"
  55. VERBOSE=0 # no console logging
  56. # global variables normally set by reading DDNS UCI configuration
  57. use_syslog=0 # no syslog
  58. use_logfile=0 # no logfile
  59. use_ipv6=0 # Use IPv6 - default IPv4
  60. force_ipversion=0 # Force IP Version - default 0 - No
  61. force_dnstcp=0 # Force TCP on DNS - default 0 - No
  62. is_glue=0 # Is glue record - default 0 - No
  63. use_https=0 # not needed but must be set
  64. while getopts ":6d:fghi:l:n:p:s:S:tu:Lv:V" OPT; do
  65. case "$OPT" in
  66. 6) use_ipv6=1;;
  67. d) dns_server="$OPTARG";;
  68. f) force_ipversion=1;;
  69. g) is_glue=1;;
  70. i) ip_interface="$OPTARG"; ip_source="interface";;
  71. l) lookup_host="$OPTARG";;
  72. n) ip_network="$OPTARG"; ip_source="network";;
  73. p) proxy="$OPTARG";;
  74. s) ip_script="$OPTARG"; ip_source="script";;
  75. t) force_dnstcp=1;;
  76. u) ip_url="$OPTARG"; ip_source="web";;
  77. h) usage; exit 255;;
  78. L) use_logfile=1;;
  79. v) VERBOSE=$OPTARG;;
  80. S) SECTION=$OPTARG;;
  81. V) printf %s\\n "ddns-scripts $VERSION"; exit 255;;
  82. :) usage_err "option -$OPTARG missing argument";;
  83. \?) usage_err "invalid option -$OPTARG";;
  84. *) usage_err "unhandled option -$OPT $OPTARG";;
  85. esac
  86. done
  87. shift $((OPTIND - 1 )) # OPTIND is 1 based
  88. [ $# -eq 0 ] && usage_err "missing command"
  89. __RET=0
  90. case "$1" in
  91. get_registered_ip)
  92. [ -z "$lookup_host" ] && usage_err "command 'get_registered_ip': 'lookup_host' not set"
  93. write_log 7 "-----> get_registered_ip IP"
  94. [ -z "$SECTION" ] || IPFILE="$ddns_rundir/$SECTION.ip"
  95. IP=""
  96. get_registered_ip IP
  97. __RET=$?
  98. [ $__RET -ne 0 ] && IP=""
  99. printf "%s" "$IP"
  100. ;;
  101. verify_dns)
  102. [ -z "$dns_server" ] && usage_err "command 'verify_dns': 'dns_server' not set"
  103. write_log 7 "-----> verify_dns '$dns_server'"
  104. verify_dns "$dns_server"
  105. __RET=$?
  106. ;;
  107. verify_proxy)
  108. [ -z "$proxy" ] && usage_err "command 'verify_proxy': 'proxy' not set"
  109. write_log 7 "-----> verify_proxy '$proxy'"
  110. verify_proxy "$proxy"
  111. __RET=$?
  112. ;;
  113. get_local_ip)
  114. [ -z "$ip_source" ] && usage_err "command 'get_local_ip': 'ip_source' not set"
  115. [ -n "$proxy" -a "$ip_source" = "web" ] && {
  116. # proxy defined, used for ip_source=web
  117. export HTTP_PROXY="http://$proxy"
  118. export HTTPS_PROXY="http://$proxy"
  119. export http_proxy="http://$proxy"
  120. export https_proxy="http://$proxy"
  121. }
  122. # don't need IP only the return code
  123. IP=""
  124. if [ "$ip_source" = "web" -o "$ip_source" = "script" ]; then
  125. # we wait only 3 seconds for an
  126. # answer from "web" or "script"
  127. write_log 7 "-----> timeout 3 -- get_local_ip IP"
  128. timeout 3 -- get_local_ip IP
  129. else
  130. write_log 7 "-----> get_local_ip IP"
  131. get_local_ip IP
  132. fi
  133. __RET=$?
  134. ;;
  135. start)
  136. [ -z "$SECTION" ] && usage_err "command 'start': 'SECTION' not set"
  137. if [ $VERBOSE -eq 0 ]; then # start in background
  138. $DDNSPRG -v 0 -S $SECTION -- start &
  139. else
  140. $DDNSPRG -v $VERBOSE -S $SECTION -- start
  141. fi
  142. ;;
  143. reload)
  144. $DDNSPRG -- reload
  145. ;;
  146. restart)
  147. $DDNSPRG -- stop
  148. sleep 1
  149. $DDNSPRG -- start
  150. ;;
  151. *)
  152. __RET=255
  153. ;;
  154. esac
  155. # remove out and err file
  156. [ -f $DATFILE ] && rm -f $DATFILE
  157. [ -f $ERRFILE ] && rm -f $ERRFILE
  158. return $__RET