Merge pull request #840 from cjkoenig/update_liburcu
[feed/packages.git] / net / ddns-scripts / files / usr / lib / ddns / dynamic_dns_lucihelper.sh
1 #!/bin/sh
2 # /usr/lib/ddns/luci_dns_helper.sh
3 #
4 # Written in August 2014
5 # by Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
6 # This script is used by luci-app-ddns
7 # - getting registered IP
8 # - check if possible to get local IP
9 # - verifing given DNS- or Proxy-Server
10 #
11 # variables in small chars are read from /etc/config/ddns as parameter given here
12 # variables in big chars are defined inside these scripts as gloval vars
13 # variables in big chars beginning with "__" are local defined inside functions only
14 # set -vx #script debugger
15
16 [ $# -lt 2 ] && exit 1
17
18 . /usr/lib/ddns/dynamic_dns_functions.sh # global vars are also defined here
19
20 # preset some variables, wrong or not set in dynamic_dns_functions.sh
21 SECTION_ID="lucihelper"
22 LOGFILE="$LOGDIR/$SECTION_ID.log"
23 DATFILE="$RUNDIR/$SECTION_ID.dat" # save stdout data of WGet and other extern programs called
24 ERRFILE="$RUNDIR/$SECTION_ID.err" # save stderr output of WGet and other extern programs called
25 VERBOSE_MODE=0 # no console logging
26 # global variables normally set by reading DDNS UCI configuration
27 use_syslog=0 # no syslog
28 use_logfile=0 # by default no logfile, can be changed here
29
30 __RET=0
31 case "$1" in
32 get_registered_ip)
33 local IP
34 domain=$2 # Hostname/Domain
35 use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
36 force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
37 force_dnstcp=${5:-"0"} # Force TCP on DNS - default 0 - No
38 dns_server=${6:-""} # DNS server - default No DNS
39 write_log 7 "-----> get_registered_ip IP"
40 get_registered_ip IP
41 __RET=$?
42 [ $__RET -ne 0 ] && IP=""
43 echo -n "$IP" # suppress LF
44 ;;
45 verify_dns)
46 # $2 : dns-server to verify # no need for force_dnstcp because
47 # verify with nc (netcat) uses tcp anyway
48 use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
49 force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
50 write_log 7 "-----> verify_dns '$2'"
51 verify_dns "$2"
52 __RET=$?
53 ;;
54 verify_proxy)
55 # $2 : proxy string to verify
56 use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
57 force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
58 write_log 7 "-----> verify_proxy '$2'"
59 verify_proxy "$2"
60 __RET=$?
61 ;;
62 get_local_ip)
63 local IP
64 use_ipv6="$2" # Use IPv6
65 ip_source="$3" # IP source
66 ip_network="$4" # set if source = "network" otherwise "-"
67 ip_url="$5" # set if source = "web" otherwise "-"
68 ip_interface="$6" # set if source = "interface" itherwiase "-"
69 ip_script="$7" # set if source = "script" otherwise "-"
70 proxy="$8" # proxy if set
71 force_ipversion="0" # not needed but must be set
72 use_https="0" # not needed but must be set
73 [ -n "$proxy" -a "$ip_source" = "web" ] && {
74 # proxy defined, used for ip_source=web
75 export HTTP_PROXY="http://$proxy"
76 export HTTPS_PROXY="http://$proxy"
77 export http_proxy="http://$proxy"
78 export https_proxy="http://$proxy"
79 }
80 # don't need IP only the return code
81 [ "$ip_source" = "web" -o "$ip_source" = "script" ] && {
82 # we wait only 3 seconds for an
83 # answer from "web" or "script"
84 write_log 7 "-----> timeout 3 -- get_local_ip IP"
85 timeout 3 -- get_local_ip IP
86 } || {
87 write_log 7 "-----> get_local_ip IP"
88 get_local_ip IP
89 }
90 __RET=$?
91 ;;
92 *)
93 __RET=255
94 ;;
95 esac
96
97 # remove out and err file
98 [ -f $DATFILE ] && rm -f $DATFILE
99 [ -f $ERRFILE ] && rm -f $ERRFILE
100 return $__RET