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