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.

171 lines
5.2 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-2016 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. IP=""
  95. get_registered_ip IP
  96. __RET=$?
  97. [ $__RET -ne 0 ] && IP=""
  98. printf "%s" "$IP"
  99. ;;
  100. verify_dns)
  101. [ -z "$dns_server" ] && usage_err "command 'verify_dns': 'dns_server' not set"
  102. write_log 7 "-----> verify_dns '$dns_server'"
  103. verify_dns "$dns_server"
  104. __RET=$?
  105. ;;
  106. verify_proxy)
  107. [ -z "$proxy" ] && usage_err "command 'verify_proxy': 'proxy' not set"
  108. write_log 7 "-----> verify_proxy '$proxy'"
  109. verify_proxy "$proxy"
  110. __RET=$?
  111. ;;
  112. get_local_ip)
  113. [ -z "$ip_source" ] && usage_err "command 'get_local_ip': 'ip_source' not set"
  114. [ -n "$proxy" -a "$ip_source" = "web" ] && {
  115. # proxy defined, used for ip_source=web
  116. export HTTP_PROXY="http://$proxy"
  117. export HTTPS_PROXY="http://$proxy"
  118. export http_proxy="http://$proxy"
  119. export https_proxy="http://$proxy"
  120. }
  121. # don't need IP only the return code
  122. IP=""
  123. if [ "$ip_source" = "web" -o "$ip_source" = "script" ]; then
  124. # we wait only 3 seconds for an
  125. # answer from "web" or "script"
  126. write_log 7 "-----> timeout 3 -- get_local_ip IP"
  127. timeout 3 -- get_local_ip IP
  128. else
  129. write_log 7 "-----> get_local_ip IP"
  130. get_local_ip IP
  131. fi
  132. __RET=$?
  133. ;;
  134. start)
  135. [ -z "$SECTION" ] && usage_err "command 'start': 'SECTION' not set"
  136. if [ $VERBOSE -eq 0 ]; then # start in background
  137. $DDNSPRG -v 0 -S $SECTION -- start &
  138. else
  139. $DDNSPRG -v $VERBOSE -S $SECTION -- start
  140. fi
  141. ;;
  142. reload)
  143. $DDNSPRG -- reload
  144. ;;
  145. restart)
  146. $DDNSPRG -- stop
  147. sleep 1
  148. $DDNSPRG -- start
  149. ;;
  150. *)
  151. __RET=255
  152. ;;
  153. esac
  154. # remove out and err file
  155. [ -f $DATFILE ] && rm -f $DATFILE
  156. [ -f $ERRFILE ] && rm -f $ERRFILE
  157. return $__RET