#!/bin/sh
-# /usr/lib/ddns/luci_dns_helper.sh
+# /usr/lib/ddns/dynamic_dns_lucihelper.sh
#
-# Written in August 2014
-# by Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
+#.Distributed under the terms of the GNU General Public License (GPL) version 2.0
+#.2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
# This script is used by luci-app-ddns
-# - getting registered IP
-# - check if possible to get local IP
-# - verifing given DNS- or Proxy-Server
#
# variables in small chars are read from /etc/config/ddns as parameter given here
# variables in big chars are defined inside these scripts as gloval vars
# variables in big chars beginning with "__" are local defined inside functions only
# set -vx #script debugger
-[ $# -lt 2 ] && exit 1
-
. /usr/lib/ddns/dynamic_dns_functions.sh # global vars are also defined here
-# set -vx #script debugger
+usage() {
+ cat << EOF
+
+Usage:
+ $MYPROG [options] -- command
+
+Commands:
+ get_local_ip using given INTERFACE or NETWORK or SCRIPT or URL
+ get_registered_ip for given FQDN
+ verify_dns given DNS-SERVER
+ verify_proxy given PROXY
+ start start given SECTION
+ reload force running ddns processes to reload changed configuration
+ restart restart all ddns processes
+
+Parameters:
+ -6 => use_ipv6=1 (default 0)
+ -d DNS-SERVER => dns_server=SERVER[:PORT]
+ -f => force_ipversion=1 (default 0)
+ -g => is_glue=1 (default 0)
+ -i INTERFACE => ip_interface=INTERFACE; ip_source="interface"
+ -l FQDN => lookup_host=FQDN
+ -n NETWORK => ip_network=NETWORK; ip_source="network"
+ -p PROXY => proxy=[USER:PASS@]PROXY:PORT
+ -s SCRIPT => ip_script=SCRIPT; ip_source="script"
+ -t => force_dnstcp=1 (default 0)
+ -u URL => ip_url=URL; ip_source="web"
+ -S SECTION SECTION to start
+
+ -h => show this help and exit
+ -L => use_logfile=1 (default 0)
+ -v LEVEL => VERBOSE=LEVEL (default 0)
+ -V => show version and exit
+
+EOF
+}
+
+usage_err() {
+ printf %s\\n "$MYPROG: $@" >&2
+ usage >&2
+ exit 255
+}
-# preset some variables, wrong or not set in dynamic_dns_functions.sh
+# preset some variables, wrong or not set in ddns-functions.sh
SECTION_ID="lucihelper"
-LOGFILE="$LOGDIR/$SECTION_ID.log"
-DATFILE="$RUNDIR/$SECTION_ID.dat" # save stdout data of WGet and other extern programs called
-ERRFILE="$RUNDIR/$SECTION_ID.err" # save stderr output of WGet and other extern programs called
-VERBOSE_MODE=0 # no console logging
+LOGFILE="$ddns_logdir/$SECTION_ID.log"
+DATFILE="$ddns_rundir/$SECTION_ID.$$.dat" # save stdout data of WGet and other extern programs called
+ERRFILE="$ddns_rundir/$SECTION_ID.$$.err" # save stderr output of WGet and other extern programs called
+DDNSPRG="/usr/lib/ddns/dynamic_dns_updater.sh"
+VERBOSE=0 # no console logging
# global variables normally set by reading DDNS UCI configuration
use_syslog=0 # no syslog
-use_logfile=0 # by default no logfile, can be changed here
+use_logfile=0 # no logfile
+
+use_ipv6=0 # Use IPv6 - default IPv4
+force_ipversion=0 # Force IP Version - default 0 - No
+force_dnstcp=0 # Force TCP on DNS - default 0 - No
+is_glue=0 # Is glue record - default 0 - No
+use_https=0 # not needed but must be set
+
+while getopts ":6d:fghi:l:n:p:s:S:tu:Lv:V" OPT; do
+ case "$OPT" in
+ 6) use_ipv6=1;;
+ d) dns_server="$OPTARG";;
+ f) force_ipversion=1;;
+ g) is_glue=1;;
+ i) ip_interface="$OPTARG"; ip_source="interface";;
+ l) lookup_host="$OPTARG";;
+ n) ip_network="$OPTARG"; ip_source="network";;
+ p) proxy="$OPTARG";;
+ s) ip_script="$OPTARG"; ip_source="script";;
+ t) force_dnstcp=1;;
+ u) ip_url="$OPTARG"; ip_source="web";;
+ h) usage; exit 255;;
+ L) use_logfile=1;;
+ v) VERBOSE=$OPTARG;;
+ S) SECTION=$OPTARG;;
+ V) printf %s\\n "ddns-scripts $VERSION"; exit 255;;
+ :) usage_err "option -$OPTARG missing argument";;
+ \?) usage_err "invalid option -$OPTARG";;
+ *) usage_err "unhandled option -$OPT $OPTARG";;
+ esac
+done
+shift $((OPTIND - 1 )) # OPTIND is 1 based
+
+[ $# -eq 0 ] && usage_err "missing command"
__RET=0
case "$1" in
get_registered_ip)
- local IP
- domain=$2 # Hostname/Domain
- use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
- force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
- force_dnstcp=${5:-"0"} # Force TCP on DNS - default 0 - No
- dns_server=${6:-""} # DNS server - default No DNS
+ [ -z "$lookup_host" ] && usage_err "command 'get_registered_ip': 'lookup_host' not set"
write_log 7 "-----> get_registered_ip IP"
+ [ -z "$SECTION" ] || IPFILE="$ddns_rundir/$SECTION.ip"
+ IP=""
get_registered_ip IP
__RET=$?
[ $__RET -ne 0 ] && IP=""
- echo -n "$IP" # suppress LF
+ printf "%s" "$IP"
;;
verify_dns)
- # $2 : dns-server to verify # no need for force_dnstcp because
- # verify with nc (netcat) uses tcp anyway
- use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
- force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
- write_log 7 "-----> verify_dns '$2'"
- verify_dns "$2"
+ [ -z "$dns_server" ] && usage_err "command 'verify_dns': 'dns_server' not set"
+ write_log 7 "-----> verify_dns '$dns_server'"
+ verify_dns "$dns_server"
__RET=$?
;;
verify_proxy)
- # $2 : proxy string to verify
- use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
- force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
- write_log 7 "-----> verify_proxy '$2'"
- verify_proxy "$2"
+ [ -z "$proxy" ] && usage_err "command 'verify_proxy': 'proxy' not set"
+ write_log 7 "-----> verify_proxy '$proxy'"
+ verify_proxy "$proxy"
__RET=$?
;;
get_local_ip)
- local IP
- use_ipv6="$2" # Use IPv6
- ip_source="$3" # IP source
- ip_network="$4" # set if source = "network" otherwise "-"
- ip_url="$5" # set if source = "web" otherwise "-"
- ip_interface="$6" # set if source = "interface" itherwiase "-"
- ip_script="$7" # set if source = "script" otherwise "-"
- proxy="$8" # proxy if set
- force_ipversion="0" # not needed but must be set
- use_https="0" # not needed but must be set
+ [ -z "$ip_source" ] && usage_err "command 'get_local_ip': 'ip_source' not set"
[ -n "$proxy" -a "$ip_source" = "web" ] && {
# proxy defined, used for ip_source=web
export HTTP_PROXY="http://$proxy"
export https_proxy="http://$proxy"
}
# don't need IP only the return code
- [ "$ip_source" = "web" -o "$ip_source" = "script" ] && {
+ IP=""
+ if [ "$ip_source" = "web" -o "$ip_source" = "script" ]; then
# we wait only 3 seconds for an
# answer from "web" or "script"
- write_log 7 "-----> timeout 3 -- get_local_ip IP"
- timeout 3 -- get_local_ip IP
- } || {
- write_log 7 "-----> get_local_ip IP"
- get_local_ip IP
- }
+ write_log 7 "-----> timeout 3 -- get_current_ip IP"
+ timeout 3 -- get_current_ip IP
+ else
+ write_log 7 "-----> get_current_ip IP"
+ get_current_ip IP
+ fi
__RET=$?
;;
+ start)
+ [ -z "$SECTION" ] && usage_err "command 'start': 'SECTION' not set"
+ if [ $VERBOSE -eq 0 ]; then # start in background
+ $DDNSPRG -v 0 -S $SECTION -- start &
+ else
+ $DDNSPRG -v $VERBOSE -S $SECTION -- start
+ fi
+ ;;
+ reload)
+ $DDNSPRG -- reload
+ ;;
+ restart)
+ $DDNSPRG -- stop
+ sleep 1
+ $DDNSPRG -- start
+ ;;
*)
__RET=255
;;
# remove out and err file
[ -f $DATFILE ] && rm -f $DATFILE
[ -f $ERRFILE ] && rm -f $ERRFILE
-return $__RET
\ No newline at end of file
+return $__RET