dynamic_dns_functions.sh 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. #!/bin/sh
  2. # /usr/lib/ddns/dynamic_dns_functions.sh
  3. #
  4. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  5. # Original written by Eric Paul Bishop, January 2008
  6. # (Loosely) based on the script on the one posted by exobyte in the forums here:
  7. # http://forum.openwrt.org/viewtopic.php?id=14040
  8. # extended and partial rewritten
  9. #.2014-2017 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  10. #
  11. # function timeout
  12. # copied from http://www.ict.griffith.edu.au/anthony/software/timeout.sh
  13. # @author Anthony Thyssen 6 April 2011
  14. #
  15. # variables in small chars are read from /etc/config/ddns
  16. # variables in big chars are defined inside these scripts as global vars
  17. # variables in big chars beginning with "__" are local defined inside functions only
  18. # set -vx #script debugger
  19. . /lib/functions.sh
  20. . /lib/functions/network.sh
  21. # GLOBAL VARIABLES #
  22. VERSION="2.7.6-13"
  23. SECTION_ID="" # hold config's section name
  24. VERBOSE=0 # default mode is log to console, but easily changed with parameter
  25. MYPROG=$(basename $0) # my program call name
  26. LOGFILE="" # logfile - all files are set in dynamic_dns_updater.sh
  27. PIDFILE="" # pid file
  28. UPDFILE="" # store UPTIME of last update
  29. DATFILE="" # save stdout data of WGet and other external programs called
  30. ERRFILE="" # save stderr output of WGet and other external programs called
  31. TLDFILE=/usr/share/public_suffix_list.dat.gz # TLD file used by split_FQDN
  32. CHECK_SECONDS=0 # calculated seconds out of given
  33. FORCE_SECONDS=0 # interval and unit
  34. RETRY_SECONDS=0 # in configuration
  35. LAST_TIME=0 # holds the uptime of last successful update
  36. CURR_TIME=0 # holds the current uptime
  37. NEXT_TIME=0 # calculated time for next FORCED update
  38. EPOCH_TIME=0 # seconds since 1.1.1970 00:00:00
  39. REGISTERED_IP="" # holds the IP read from DNS
  40. LOCAL_IP="" # holds the local IP read from the box
  41. URL_USER="" # url encoded $username from config file
  42. URL_PASS="" # url encoded $password from config file
  43. URL_PENC="" # url encoded $param_enc from config file
  44. UPD_ANSWER="" # Answer given by service on success
  45. ERR_LAST=0 # used to save $? return code of program and function calls
  46. ERR_UPDATE=0 # error counter on different local and registered ip
  47. PID_SLEEP=0 # ProcessID of current background "sleep"
  48. # regular expression to detect IPv4 / IPv6
  49. # IPv4 0-9 1-3x "." 0-9 1-3x "." 0-9 1-3x "." 0-9 1-3x
  50. IPV4_REGEX="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
  51. # IPv6 ( ( 0-9a-f 1-4char ":") min 1x) ( ( 0-9a-f 1-4char )optional) ( (":" 0-9a-f 1-4char ) min 1x)
  52. IPV6_REGEX="\(\([0-9A-Fa-f]\{1,4\}:\)\{1,\}\)\(\([0-9A-Fa-f]\{1,4\}\)\{0,1\}\)\(\(:[0-9A-Fa-f]\{1,4\}\)\{1,\}\)"
  53. # detect if called by ddns-lucihelper.sh script, disable retrys (empty variable == false)
  54. LUCI_HELPER=$(printf %s "$MYPROG" | grep -i "luci")
  55. # Name Server Lookup Programs
  56. BIND_HOST=$(which host)
  57. KNOT_HOST=$(which khost)
  58. DRILL=$(which drill)
  59. HOSTIP=$(which hostip)
  60. NSLOOKUP=$(which nslookup)
  61. NSLOOKUP_MUSL=$($(which nslookup) localhost 2>&1 | grep -F "(null)") # not empty busybox compiled with musl
  62. # Transfer Programs
  63. WGET=$(which wget)
  64. WGET_SSL=$(which wget-ssl)
  65. CURL=$(which curl)
  66. # CURL_SSL not empty then SSL support available
  67. CURL_SSL=$($(which curl) -V 2>/dev/null | grep "Protocols:" | grep -F "https")
  68. # CURL_PROXY not empty then Proxy support available
  69. CURL_PROXY=$(find /lib /usr/lib -name libcurl.so* -exec grep -i "all_proxy" {} 2>/dev/null \;)
  70. UCLIENT_FETCH=$(which uclient-fetch)
  71. # UCLIENT_FETCH_SSL not empty then SSL support available
  72. UCLIENT_FETCH_SSL=$(find /lib /usr/lib -name libustream-ssl.so* 2>/dev/null)
  73. # Global configuration settings
  74. # allow NON-public IP's
  75. upd_privateip=$(uci -q get ddns.global.upd_privateip) || upd_privateip=0
  76. # directory to store run information to.
  77. ddns_rundir=$(uci -q get ddns.global.ddns_rundir) || ddns_rundir="/var/run/ddns"
  78. [ -d $ddns_rundir ] || mkdir -p -m755 $ddns_rundir
  79. # directory to store log files
  80. ddns_logdir=$(uci -q get ddns.global.ddns_logdir) || ddns_logdir="/var/log/ddns"
  81. [ -d $ddns_logdir ] || mkdir -p -m755 $ddns_logdir
  82. # number of lines to before rotate logfile
  83. ddns_loglines=$(uci -q get ddns.global.ddns_loglines) || ddns_loglines=250
  84. ddns_loglines=$((ddns_loglines + 1)) # correct sed handling
  85. # format to show date information in log and luci-app-ddns default ISO 8601 format
  86. ddns_dateformat=$(uci -q get ddns.global.ddns_dateformat) || ddns_dateformat="%F %R"
  87. DATE_PROG="date +'$ddns_dateformat'"
  88. # USE_CURL if GNU Wget and cURL installed normally Wget is used by do_transfer()
  89. # to change this use global option use_curl '1'
  90. USE_CURL=$(uci -q get ddns.global.use_curl) || USE_CURL=0 # read config
  91. [ -n "$CURL" ] || USE_CURL=0 # check for cURL
  92. # loads all options for a given package and section
  93. # also, sets all_option_variables to a list of the variable names
  94. # $1 = ddns, $2 = SECTION_ID
  95. load_all_config_options()
  96. {
  97. local __PKGNAME="$1"
  98. local __SECTIONID="$2"
  99. local __VAR
  100. local __ALL_OPTION_VARIABLES=""
  101. # this callback loads all the variables in the __SECTIONID section when we do
  102. # config_load. We need to redefine the option_cb for different sections
  103. # so that the active one isn't still active after we're done with it. For reference
  104. # the $1 variable is the name of the option and $2 is the name of the section
  105. config_cb()
  106. {
  107. if [ ."$2" = ."$__SECTIONID" ]; then
  108. option_cb()
  109. {
  110. __ALL_OPTION_VARIABLES="$__ALL_OPTION_VARIABLES $1"
  111. }
  112. else
  113. option_cb() { return 0; }
  114. fi
  115. }
  116. config_load "$__PKGNAME"
  117. # Given SECTION_ID not found so no data, so return 1
  118. [ -z "$__ALL_OPTION_VARIABLES" ] && return 1
  119. for __VAR in $__ALL_OPTION_VARIABLES
  120. do
  121. config_get "$__VAR" "$__SECTIONID" "$__VAR"
  122. done
  123. return 0
  124. }
  125. # read's all service sections from ddns config
  126. # $1 = Name of variable to store
  127. load_all_service_sections() {
  128. local __DATA=""
  129. config_cb()
  130. {
  131. # only look for section type "service", ignore everything else
  132. [ "$1" = "service" ] && __DATA="$__DATA $2"
  133. }
  134. config_load "ddns"
  135. eval "$1=\"$__DATA\""
  136. return
  137. }
  138. # starts updater script for all given sections or only for the one given
  139. # $1 = interface (Optional: when given only scripts are started
  140. # configured for that interface)
  141. # used by /etc/hotplug.d/iface/95-ddns on IFUP
  142. # and by /etc/init.d/ddns start
  143. start_daemon_for_all_ddns_sections()
  144. {
  145. local __EVENTIF="$1"
  146. local __SECTIONS=""
  147. local __SECTIONID=""
  148. local __IFACE=""
  149. load_all_service_sections __SECTIONS
  150. for __SECTIONID in $__SECTIONS; do
  151. config_get __IFACE "$__SECTIONID" interface "wan"
  152. [ -z "$__EVENTIF" -o "$__IFACE" = "$__EVENTIF" ] || continue
  153. if [ $VERBOSE -eq 0 ]; then # start in background
  154. /usr/lib/ddns/dynamic_dns_updater.sh -v 0 -S "$__SECTIONID" -- start &
  155. else
  156. /usr/lib/ddns/dynamic_dns_updater.sh -v "$VERBOSE" -S "$__SECTIONID" -- start
  157. fi
  158. done
  159. }
  160. # stop sections process incl. childs (sleeps)
  161. # $1 = section
  162. stop_section_processes() {
  163. local __PID=0
  164. local __PIDFILE="$ddns_rundir/$1.pid"
  165. [ $# -ne 1 ] && write_log 12 "Error calling 'stop_section_processes()' - wrong number of parameters"
  166. [ -e "$__PIDFILE" ] && {
  167. __PID=$(cat $__PIDFILE)
  168. ps | grep "^[\t ]*$__PID" >/dev/null 2>&1 && kill $__PID || __PID=0 # terminate it
  169. }
  170. [ $__PID -eq 0 ] # report if process was running
  171. }
  172. # stop updater script for all defines sections or only for one given
  173. # $1 = interface (optional)
  174. # used by /etc/hotplug.d/iface/95-ddns on 'ifdown'
  175. # and by /etc/init.d/ddns stop
  176. # needed because we also need to kill "sleep" child processes
  177. stop_daemon_for_all_ddns_sections() {
  178. local __EVENTIF="$1"
  179. local __SECTIONS=""
  180. local __SECTIONID=""
  181. local __IFACE=""
  182. load_all_service_sections __SECTIONS
  183. for __SECTIONID in $__SECTIONS; do
  184. config_get __IFACE "$__SECTIONID" interface "wan"
  185. [ -z "$__EVENTIF" -o "$__IFACE" = "$__EVENTIF" ] || continue
  186. stop_section_processes "$__SECTIONID"
  187. done
  188. }
  189. # reports to console, logfile, syslog
  190. # $1 loglevel 7 == Debug to 0 == EMERG
  191. # value +10 will exit the scripts
  192. # $2..n text to report
  193. write_log() {
  194. local __LEVEL __EXIT __CMD __MSG
  195. local __TIME=$(date +%H%M%S)
  196. [ $1 -ge 10 ] && {
  197. __LEVEL=$(($1-10))
  198. __EXIT=1
  199. } || {
  200. __LEVEL=$1
  201. __EXIT=0
  202. }
  203. shift # remove loglevel
  204. [ $__EXIT -eq 0 ] && __MSG="$*" || __MSG="$* - TERMINATE"
  205. case $__LEVEL in # create log message and command depending on loglevel
  206. 0) __CMD="logger -p user.emerg -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  207. __MSG=" $__TIME EMERG : $__MSG" ;;
  208. 1) __CMD="logger -p user.alert -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  209. __MSG=" $__TIME ALERT : $__MSG" ;;
  210. 2) __CMD="logger -p user.crit -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  211. __MSG=" $__TIME CRIT : $__MSG" ;;
  212. 3) __CMD="logger -p user.err -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  213. __MSG=" $__TIME ERROR : $__MSG" ;;
  214. 4) __CMD="logger -p user.warn -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  215. __MSG=" $__TIME WARN : $__MSG" ;;
  216. 5) __CMD="logger -p user.notice -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  217. __MSG=" $__TIME note : $__MSG" ;;
  218. 6) __CMD="logger -p user.info -t ddns-scripts[$$] $SECTION_ID: $__MSG"
  219. __MSG=" $__TIME info : $__MSG" ;;
  220. 7) __MSG=" $__TIME : $__MSG";;
  221. *) return;;
  222. esac
  223. # verbose echo
  224. [ $VERBOSE -gt 0 -o $__EXIT -gt 0 ] && echo -e "$__MSG"
  225. # write to logfile
  226. if [ ${use_logfile:-1} -eq 1 -o $VERBOSE -gt 1 ]; then
  227. echo -e "$__MSG" >> $LOGFILE
  228. # VERBOSE > 1 then NO loop so NO truncate log to $ddns_loglines lines
  229. [ $VERBOSE -gt 1 ] || sed -i -e :a -e '$q;N;'$ddns_loglines',$D;ba' $LOGFILE
  230. fi
  231. [ -n "$LUCI_HELPER" ] && return # nothing else todo when running LuCI helper script
  232. [ $__LEVEL -eq 7 ] && return # no syslog for debug messages
  233. __CMD=$(echo -e "$__CMD" | tr -d '\n' | tr '\t' ' ') # remove \n \t chars
  234. [ $__EXIT -eq 1 ] && {
  235. $__CMD # force syslog before exit
  236. exit 1
  237. }
  238. [ $use_syslog -eq 0 ] && return
  239. [ $((use_syslog + __LEVEL)) -le 7 ] && $__CMD
  240. return
  241. }
  242. # replace all special chars to their %hex value
  243. # used for USERNAME and PASSWORD in update_url
  244. # unchanged: "-"(minus) "_"(underscore) "."(dot) "~"(tilde)
  245. # to verify: "'"(single quote) '"'(double quote) # because shell delimiter
  246. # "$"(Dollar) # because used as variable output
  247. # tested with the following string stored via Luci Application as password / username
  248. # A B!"#AA$1BB%&'()*+,-./:;<=>?@[\]^_`{|}~ without problems at Dollar or quotes
  249. urlencode() {
  250. # $1 Name of Variable to store encoded string to
  251. # $2 string to encode
  252. local __STR __LEN __CHAR __OUT
  253. local __ENC=""
  254. local __POS=1
  255. [ $# -ne 2 ] && write_log 12 "Error calling 'urlencode()' - wrong number of parameters"
  256. __STR="$2" # read string to encode
  257. __LEN=${#__STR} # get string length
  258. while [ $__POS -le $__LEN ]; do
  259. # read one chat of the string
  260. __CHAR=$(expr substr "$__STR" $__POS 1)
  261. case "$__CHAR" in
  262. [-_.~a-zA-Z0-9] )
  263. # standard char
  264. __OUT="${__CHAR}"
  265. ;;
  266. * )
  267. # special char get %hex code
  268. __OUT=$(printf '%%%02x' "'$__CHAR" )
  269. ;;
  270. esac
  271. __ENC="${__ENC}${__OUT}" # append to encoded string
  272. __POS=$(( $__POS + 1 )) # increment position
  273. done
  274. eval "$1=\"$__ENC\"" # transfer back to variable
  275. return 0
  276. }
  277. # extract url or script for given DDNS Provider from
  278. # file /etc/ddns/services for IPv4 or from
  279. # file /etc/ddns/services_ipv6 for IPv6
  280. # $1 Name of Variable to store url to
  281. # $2 Name of Variable to store script to
  282. # $3 Name of Variable to store service answer to
  283. get_service_data() {
  284. [ $# -ne 3 ] && write_log 12 "Error calling 'get_service_data()' - wrong number of parameters"
  285. __FILE="/etc/ddns/services" # IPv4
  286. [ $use_ipv6 -ne 0 ] && __FILE="/etc/ddns/services_ipv6" # IPv6
  287. # workaround with variables; pipe create subshell with no give back of variable content
  288. mkfifo pipe_$$
  289. # only grep without # or whitespace at linestart | remove "
  290. # grep -v -E "(^#|^[[:space:]]*$)" $__FILE | sed -e s/\"//g > pipe_$$ &
  291. sed '/^#/d; /^[ \t]*$/d; s/\"//g' $__FILE > pipe_$$ &
  292. while read __SERVICE __DATA __ANSWER; do
  293. if [ "$__SERVICE" = "$service_name" ]; then
  294. # check if URL or SCRIPT is given
  295. __URL=$(echo "$__DATA" | grep "^http")
  296. [ -z "$__URL" ] && __SCRIPT="/usr/lib/ddns/$__DATA"
  297. eval "$1=\"$__URL\""
  298. eval "$2=\"$__SCRIPT\""
  299. eval "$3=\"$__ANSWER\""
  300. rm pipe_$$
  301. return 0
  302. fi
  303. done < pipe_$$
  304. rm pipe_$$
  305. eval "$1=\"\"" # no service match clear variables
  306. eval "$2=\"\""
  307. eval "$3=\"\""
  308. return 1
  309. }
  310. # Calculate seconds from interval and unit
  311. # $1 Name of Variable to store result in
  312. # $2 Number and
  313. # $3 Unit of time interval
  314. get_seconds() {
  315. [ $# -ne 3 ] && write_log 12 "Error calling 'get_seconds()' - wrong number of parameters"
  316. case "$3" in
  317. "days" ) eval "$1=$(( $2 * 86400 ))";;
  318. "hours" ) eval "$1=$(( $2 * 3600 ))";;
  319. "minutes" ) eval "$1=$(( $2 * 60 ))";;
  320. * ) eval "$1=$2";;
  321. esac
  322. return 0
  323. }
  324. timeout() {
  325. #.copied from http://www.ict.griffith.edu.au/anthony/software/timeout.sh
  326. # only did the following changes
  327. # - commented out "#!/bin/bash" and usage section
  328. # - replace exit by return for usage as function
  329. # - some reformatting
  330. #
  331. # timeout [-SIG] time [--] command args...
  332. #
  333. # Run the given command until completion, but kill it if it runs too long.
  334. # Specifically designed to exit immediately (no sleep interval) and clean up
  335. # nicely without messages or leaving any extra processes when finished.
  336. #
  337. # Example use
  338. # timeout 5 countdown
  339. #
  340. # Based on notes in my "Shell Script Hints", section "Command Timeout"
  341. # http://www.ict.griffith.edu.au/~anthony/info/shell/script.hints
  342. #
  343. # This script uses a lot of tricks to terminate both the background command,
  344. # the timeout script, and even the sleep process. It also includes trap
  345. # commands to prevent sub-shells reporting expected "Termination Errors".
  346. #
  347. # It took years of occasional trials, errors and testing to get a pure bash
  348. # timeout command working as well as this does.
  349. #
  350. #.Anthony Thyssen 6 April 2011
  351. #
  352. # PROGNAME=$(type $0 | awk '{print $3}') # search for executable on path
  353. # PROGDIR=$(dirname $PROGNAME) # extract directory of program
  354. # PROGNAME=$(basename $PROGNAME) # base name of program
  355. # output the script comments as docs
  356. # Usage() {
  357. # echo >&2 "$PROGNAME:" "$@"
  358. # sed >&2 -n '/^###/q; /^#/!q; s/^#//; s/^ //; 3s/^/Usage: /; 2,$ p' "$PROGDIR/$PROGNAME"
  359. # exit 10;
  360. # }
  361. SIG=-TERM
  362. while [ $# -gt 0 ]; do
  363. case "$1" in
  364. --)
  365. # forced end of user options
  366. shift;
  367. break ;;
  368. # -\?|--help|--doc*)
  369. # Usage ;;
  370. [0-9]*)
  371. TIMEOUT="$1" ;;
  372. -*)
  373. SIG="$1" ;;
  374. *)
  375. # unforced end of user options
  376. break ;;
  377. esac
  378. shift # next option
  379. done
  380. # run main command in backgrounds and get its pid
  381. "$@" &
  382. command_pid=$!
  383. # timeout sub-process abort countdown after ABORT seconds! also backgrounded
  384. sleep_pid=0
  385. (
  386. # cleanup sleep process
  387. trap 'kill -TERM $sleep_pid; return 1' 1 2 3 15
  388. # sleep timeout period in background
  389. sleep $TIMEOUT &
  390. sleep_pid=$!
  391. wait $sleep_pid
  392. # Abort the command
  393. kill $SIG $command_pid >/dev/null 2>&1
  394. return 1
  395. ) &
  396. timeout_pid=$!
  397. # Wait for main command to finished or be timed out
  398. wait $command_pid
  399. status=$?
  400. # Clean up timeout sub-shell - if it is still running!
  401. kill $timeout_pid 2>/dev/null
  402. wait $timeout_pid 2>/dev/null
  403. # Uncomment to check if a LONG sleep still running (no sleep should be)
  404. # sleep 1
  405. # echo "-----------"
  406. # /bin/ps j # uncomment to show if abort "sleep" is still sleeping
  407. return $status
  408. }
  409. # verify given host and port is connectable
  410. # $1 Host/IP to verify
  411. # $2 Port to verify
  412. verify_host_port() {
  413. local __HOST=$1
  414. local __PORT=$2
  415. local __NC=$(which nc)
  416. local __NCEXT=$($(which nc) --help 2>&1 | grep "\-w" 2>/dev/null) # busybox nc compiled with extensions
  417. local __IP __IPV4 __IPV6 __RUNPROG __PROG __ERR
  418. # return codes
  419. # 1 system specific error
  420. # 2 nslookup/host error
  421. # 3 nc (netcat) error
  422. # 4 unmatched IP version
  423. [ $# -ne 2 ] && write_log 12 "Error calling 'verify_host_port()' - wrong number of parameters"
  424. # check if ip or FQDN was given
  425. __IPV4=$(echo $__HOST | grep -m 1 -o "$IPV4_REGEX$") # do not detect ip in 0.0.0.0.example.com
  426. __IPV6=$(echo $__HOST | grep -m 1 -o "$IPV6_REGEX")
  427. # if FQDN given get IP address
  428. [ -z "$__IPV4" -a -z "$__IPV6" ] && {
  429. if [ -n "$BIND_HOST" ]; then # use BIND host if installed
  430. __PROG="BIND host"
  431. __RUNPROG="$BIND_HOST $__HOST >$DATFILE 2>$ERRFILE"
  432. elif [ -n "$KNOT_HOST" ]; then # use Knot host if installed
  433. __PROG="Knot host"
  434. __RUNPROG="$KNOT_HOST $__HOST >$DATFILE 2>$ERRFILE"
  435. elif [ -n "$DRILL" ]; then # use drill if installed
  436. __PROG="drill"
  437. __RUNPROG="$DRILL -V0 $__HOST A >$DATFILE 2>$ERRFILE" # IPv4
  438. __RUNPROG="$__RUNPROG; $DRILL -V0 $__HOST AAAA >>$DATFILE 2>>$ERRFILE" # IPv6
  439. elif [ -n "$HOSTIP" ]; then # use hostip if installed
  440. __PROG="hostip"
  441. __RUNPROG="$HOSTIP $__HOST >$DATFILE 2>$ERRFILE" # IPv4
  442. __RUNPROG="$__RUNPROG; $HOSTIP -6 $__HOST >>$DATFILE 2>>$ERRFILE" # IPv6
  443. else # use BusyBox nslookup
  444. __PROG="BusyBox nslookup"
  445. __RUNPROG="$NSLOOKUP $__HOST >$DATFILE 2>$ERRFILE"
  446. fi
  447. write_log 7 "#> $__RUNPROG"
  448. eval $__RUNPROG
  449. __ERR=$?
  450. # command error
  451. [ $__ERR -gt 0 ] && {
  452. write_log 3 "DNS Resolver Error - $__PROG Error '$__ERR'"
  453. write_log 7 "$(cat $ERRFILE)"
  454. return 2
  455. }
  456. # extract IP address
  457. if [ -n "$BIND_HOST" -o -n "$KNOT_HOST" ]; then # use BIND host or Knot host if installed
  458. __IPV4=$(cat $DATFILE | awk -F "address " '/has address/ {print $2; exit}' )
  459. __IPV6=$(cat $DATFILE | awk -F "address " '/has IPv6/ {print $2; exit}' )
  460. elif [ -n "$DRILL" ]; then # use drill if installed
  461. __IPV4=$(cat $DATFILE | awk '/^'"$lookup_host"'/ {print $5}' | grep -m 1 -o "$IPV4_REGEX")
  462. __IPV6=$(cat $DATFILE | awk '/^'"$lookup_host"'/ {print $5}' | grep -m 1 -o "$IPV6_REGEX")
  463. elif [ -n "$HOSTIP" ]; then # use hostip if installed
  464. __IPV4=$(cat $DATFILE | grep -m 1 -o "$IPV4_REGEX")
  465. __IPV6=$(cat $DATFILE | grep -m 1 -o "$IPV6_REGEX")
  466. else # use BusyBox nslookup
  467. __IPV4=$(cat $DATFILE | sed -ne "/^Name:/,\$ { s/^Address[0-9 ]\{0,\}: \($IPV4_REGEX\).*$/\\1/p }")
  468. __IPV6=$(cat $DATFILE | sed -ne "/^Name:/,\$ { s/^Address[0-9 ]\{0,\}: \($IPV6_REGEX\).*$/\\1/p }")
  469. fi
  470. }
  471. # check IP version if forced
  472. if [ $force_ipversion -ne 0 ]; then
  473. __ERR=0
  474. [ $use_ipv6 -eq 0 -a -z "$__IPV4" ] && __ERR=4
  475. [ $use_ipv6 -eq 1 -a -z "$__IPV6" ] && __ERR=6
  476. [ $__ERR -gt 0 ] && {
  477. [ -n "$LUCI_HELPER" ] && return 4
  478. write_log 14 "Verify host Error '4' - Forced IP Version IPv$__ERR don't match"
  479. }
  480. fi
  481. # verify nc command
  482. # busybox nc compiled without -l option "NO OPT l!" -> critical error
  483. $__NC --help 2>&1 | grep -i "NO OPT l!" >/dev/null 2>&1 && \
  484. write_log 12 "Busybox nc (netcat) compiled without '-l' option, error 'NO OPT l!'"
  485. # busybox nc compiled with extensions
  486. $__NC --help 2>&1 | grep "\-w" >/dev/null 2>&1 && __NCEXT="TRUE"
  487. # connectivity test
  488. # run busybox nc to HOST PORT
  489. # busybox might be compiled with "FEATURE_PREFER_IPV4_ADDRESS=n"
  490. # then nc will try to connect via IPv6 if there is any IPv6 available on any host interface
  491. # not worrying, if there is an IPv6 wan address
  492. # so if not "force_ipversion" to use_ipv6 then connect test via ipv4, if available
  493. [ $force_ipversion -ne 0 -a $use_ipv6 -ne 0 -o -z "$__IPV4" ] && __IP=$__IPV6 || __IP=$__IPV4
  494. if [ -n "$__NCEXT" ]; then # BusyBox nc compiled with extensions (timeout support)
  495. __RUNPROG="$__NC -w 1 $__IP $__PORT </dev/null >$DATFILE 2>$ERRFILE"
  496. write_log 7 "#> $__RUNPROG"
  497. eval $__RUNPROG
  498. __ERR=$?
  499. [ $__ERR -eq 0 ] && return 0
  500. write_log 3 "Connect error - BusyBox nc (netcat) Error '$__ERR'"
  501. write_log 7 "$(cat $ERRFILE)"
  502. return 3
  503. else # nc compiled without extensions (no timeout support)
  504. __RUNPROG="timeout 2 -- $__NC $__IP $__PORT </dev/null >$DATFILE 2>$ERRFILE"
  505. write_log 7 "#> $__RUNPROG"
  506. eval $__RUNPROG
  507. __ERR=$?
  508. [ $__ERR -eq 0 ] && return 0
  509. write_log 3 "Connect error - BusyBox nc (netcat) timeout Error '$__ERR'"
  510. return 3
  511. fi
  512. }
  513. # verify given DNS server if connectable
  514. # $1 DNS server to verify
  515. verify_dns() {
  516. local __ERR=255 # last error buffer
  517. local __CNT=0 # error counter
  518. [ $# -ne 1 ] && write_log 12 "Error calling 'verify_dns()' - wrong number of parameters"
  519. write_log 7 "Verify DNS server '$1'"
  520. while [ $__ERR -ne 0 ]; do
  521. # DNS uses port 53
  522. verify_host_port "$1" "53"
  523. __ERR=$?
  524. if [ -n "$LUCI_HELPER" ]; then # no retry if called by LuCI helper script
  525. return $__ERR
  526. elif [ $__ERR -ne 0 -a $VERBOSE -gt 1 ]; then # VERBOSE > 1 then NO retry
  527. write_log 4 "Verify DNS server '$1' failed - Verbose Mode: $VERBOSE - NO retry on error"
  528. return $__ERR
  529. elif [ $__ERR -ne 0 ]; then
  530. __CNT=$(( $__CNT + 1 )) # increment error counter
  531. # if error count > retry_count leave here
  532. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  533. write_log 14 "Verify DNS server '$1' failed after $retry_count retries"
  534. write_log 4 "Verify DNS server '$1' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  535. sleep $RETRY_SECONDS &
  536. PID_SLEEP=$!
  537. wait $PID_SLEEP # enable trap-handler
  538. PID_SLEEP=0
  539. fi
  540. done
  541. return 0
  542. }
  543. # analyze and verify given proxy string
  544. # $1 Proxy-String to verify
  545. verify_proxy() {
  546. # complete entry user:password@host:port
  547. # inside user and password NO '@' of ":" allowed
  548. # host and port only host:port
  549. # host only host ERROR unsupported
  550. # IPv4 address instead of host 123.234.234.123
  551. # IPv6 address instead of host [xxxx:....:xxxx] in square bracket
  552. local __TMP __HOST __PORT
  553. local __ERR=255 # last error buffer
  554. local __CNT=0 # error counter
  555. [ $# -ne 1 ] && write_log 12 "Error calling 'verify_proxy()' - wrong number of parameters"
  556. write_log 7 "Verify Proxy server 'http://$1'"
  557. # try to split user:password "@" host:port
  558. __TMP=$(echo $1 | awk -F "@" '{print $2}')
  559. # no "@" found - only host:port is given
  560. [ -z "$__TMP" ] && __TMP="$1"
  561. # now lets check for IPv6 address
  562. __HOST=$(echo $__TMP | grep -m 1 -o "$IPV6_REGEX")
  563. # IPv6 host address found read port
  564. if [ -n "$__HOST" ]; then
  565. # IPv6 split at "]:"
  566. __PORT=$(echo $__TMP | awk -F "]:" '{print $2}')
  567. else
  568. __HOST=$(echo $__TMP | awk -F ":" '{print $1}')
  569. __PORT=$(echo $__TMP | awk -F ":" '{print $2}')
  570. fi
  571. # No Port detected - EXITING
  572. [ -z "$__PORT" ] && {
  573. [ -n "$LUCI_HELPER" ] && return 5
  574. write_log 14 "Invalid Proxy server Error '5' - proxy port missing"
  575. }
  576. while [ $__ERR -gt 0 ]; do
  577. verify_host_port "$__HOST" "$__PORT"
  578. __ERR=$?
  579. if [ -n "$LUCI_HELPER" ]; then # no retry if called by LuCI helper script
  580. return $__ERR
  581. elif [ $__ERR -gt 0 -a $VERBOSE -gt 1 ]; then # VERBOSE > 1 then NO retry
  582. write_log 4 "Verify Proxy server '$1' failed - Verbose Mode: $VERBOSE - NO retry on error"
  583. return $__ERR
  584. elif [ $__ERR -gt 0 ]; then
  585. __CNT=$(( $__CNT + 1 )) # increment error counter
  586. # if error count > retry_count leave here
  587. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  588. write_log 14 "Verify Proxy server '$1' failed after $retry_count retries"
  589. write_log 4 "Verify Proxy server '$1' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  590. sleep $RETRY_SECONDS &
  591. PID_SLEEP=$!
  592. wait $PID_SLEEP # enable trap-handler
  593. PID_SLEEP=0
  594. fi
  595. done
  596. return 0
  597. }
  598. do_transfer() {
  599. # $1 # URL to use
  600. local __URL="$1"
  601. local __ERR=0
  602. local __CNT=0 # error counter
  603. local __PROG __RUNPROG
  604. [ $# -ne 1 ] && write_log 12 "Error in 'do_transfer()' - wrong number of parameters"
  605. # lets prefer GNU Wget because it does all for us - IPv4/IPv6/HTTPS/PROXY/force IP version
  606. if [ -n "$WGET_SSL" -a $USE_CURL -eq 0 ]; then # except global option use_curl is set to "1"
  607. __PROG="$WGET_SSL -nv -t 1 -O $DATFILE -o $ERRFILE" # non_verbose no_retry outfile errfile
  608. # force network/ip to use for communication
  609. if [ -n "$bind_network" ]; then
  610. local __BINDIP
  611. # set correct program to detect IP
  612. [ $use_ipv6 -eq 0 ] && __RUNPROG="network_get_ipaddr" || __RUNPROG="network_get_ipaddr6"
  613. eval "$__RUNPROG __BINDIP $bind_network" || \
  614. write_log 13 "Can not detect local IP using '$__RUNPROG $bind_network' - Error: '$?'"
  615. write_log 7 "Force communication via IP '$__BINDIP'"
  616. __PROG="$__PROG --bind-address=$__BINDIP"
  617. fi
  618. # force ip version to use
  619. if [ $force_ipversion -eq 1 ]; then
  620. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -4" || __PROG="$__PROG -6" # force IPv4/IPv6
  621. fi
  622. # set certificate parameters
  623. if [ $use_https -eq 1 ]; then
  624. if [ "$cacert" = "IGNORE" ]; then # idea from Ticket #15327 to ignore server cert
  625. __PROG="$__PROG --no-check-certificate"
  626. elif [ -f "$cacert" ]; then
  627. __PROG="$__PROG --ca-certificate=${cacert}"
  628. elif [ -d "$cacert" ]; then
  629. __PROG="$__PROG --ca-directory=${cacert}"
  630. elif [ -n "$cacert" ]; then # it's not a file and not a directory but given
  631. write_log 14 "No valid certificate(s) found at '$cacert' for HTTPS communication"
  632. fi
  633. fi
  634. # disable proxy if no set (there might be .wgetrc or .curlrc or wrong environment set)
  635. [ -z "$proxy" ] && __PROG="$__PROG --no-proxy"
  636. __RUNPROG="$__PROG '$__URL'" # build final command
  637. __PROG="GNU Wget" # reuse for error logging
  638. # 2nd choice is cURL IPv4/IPv6/HTTPS
  639. # libcurl might be compiled without Proxy or HTTPS Support
  640. elif [ -n "$CURL" ]; then
  641. __PROG="$CURL -RsS -o $DATFILE --stderr $ERRFILE"
  642. # check HTTPS support
  643. [ -z "$CURL_SSL" -a $use_https -eq 1 ] && \
  644. write_log 13 "cURL: libcurl compiled without https support"
  645. # force network/interface-device to use for communication
  646. if [ -n "$bind_network" ]; then
  647. local __DEVICE
  648. network_get_physdev __DEVICE $bind_network || \
  649. write_log 13 "Can not detect local device using 'network_get_physdev $bind_network' - Error: '$?'"
  650. write_log 7 "Force communication via device '$__DEVICE'"
  651. __PROG="$__PROG --interface $__DEVICE"
  652. fi
  653. # force ip version to use
  654. if [ $force_ipversion -eq 1 ]; then
  655. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -4" || __PROG="$__PROG -6" # force IPv4/IPv6
  656. fi
  657. # set certificate parameters
  658. if [ $use_https -eq 1 ]; then
  659. if [ "$cacert" = "IGNORE" ]; then # idea from Ticket #15327 to ignore server cert
  660. __PROG="$__PROG --insecure" # but not empty better to use "IGNORE"
  661. elif [ -f "$cacert" ]; then
  662. __PROG="$__PROG --cacert $cacert"
  663. elif [ -d "$cacert" ]; then
  664. __PROG="$__PROG --capath $cacert"
  665. elif [ -n "$cacert" ]; then # it's not a file and not a directory but given
  666. write_log 14 "No valid certificate(s) found at '$cacert' for HTTPS communication"
  667. fi
  668. fi
  669. # disable proxy if no set (there might be .wgetrc or .curlrc or wrong environment set)
  670. # or check if libcurl compiled with proxy support
  671. if [ -z "$proxy" ]; then
  672. __PROG="$__PROG --noproxy '*'"
  673. elif [ -z "$CURL_PROXY" ]; then
  674. # if libcurl has no proxy support and proxy should be used then force ERROR
  675. write_log 13 "cURL: libcurl compiled without Proxy support"
  676. fi
  677. __RUNPROG="$__PROG '$__URL'" # build final command
  678. __PROG="cURL" # reuse for error logging
  679. # uclient-fetch possibly with ssl support if /lib/libustream-ssl.so installed
  680. elif [ -n "$UCLIENT_FETCH" ]; then
  681. __PROG="$UCLIENT_FETCH -q -O $DATFILE"
  682. # force network/ip not supported
  683. [ -n "$__BINDIP" ] && \
  684. write_log 14 "uclient-fetch: FORCE binding to specific address not supported"
  685. # force ip version to use
  686. if [ $force_ipversion -eq 1 ]; then
  687. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -4" || __PROG="$__PROG -6" # force IPv4/IPv6
  688. fi
  689. # https possibly not supported
  690. [ $use_https -eq 1 -a -z "$UCLIENT_FETCH_SSL" ] && \
  691. write_log 14 "uclient-fetch: no HTTPS support! Additional install one of ustream-ssl packages"
  692. # proxy support
  693. [ -z "$proxy" ] && __PROG="$__PROG -Y off" || __PROG="$__PROG -Y on"
  694. # https & certificates
  695. if [ $use_https -eq 1 ]; then
  696. if [ "$cacert" = "IGNORE" ]; then
  697. __PROG="$__PROG --no-check-certificate"
  698. elif [ -f "$cacert" ]; then
  699. __PROG="$__PROG --ca-certificate=$cacert"
  700. elif [ -n "$cacert" ]; then # it's not a file; nothing else supported
  701. write_log 14 "No valid certificate file '$cacert' for HTTPS communication"
  702. fi
  703. fi
  704. __RUNPROG="$__PROG '$__URL' 2>$ERRFILE" # build final command
  705. __PROG="uclient-fetch" # reuse for error logging
  706. # Busybox Wget or any other wget in search $PATH (did not support neither IPv6 nor HTTPS)
  707. elif [ -n "$WGET" ]; then
  708. __PROG="$WGET -q -O $DATFILE"
  709. # force network/ip not supported
  710. [ -n "$__BINDIP" ] && \
  711. write_log 14 "BusyBox Wget: FORCE binding to specific address not supported"
  712. # force ip version not supported
  713. [ $force_ipversion -eq 1 ] && \
  714. write_log 14 "BusyBox Wget: Force connecting to IPv4 or IPv6 addresses not supported"
  715. # https not supported
  716. [ $use_https -eq 1 ] && \
  717. write_log 14 "BusyBox Wget: no HTTPS support"
  718. # disable proxy if no set (there might be .wgetrc or .curlrc or wrong environment set)
  719. [ -z "$proxy" ] && __PROG="$__PROG -Y off"
  720. __RUNPROG="$__PROG '$__URL' 2>$ERRFILE" # build final command
  721. __PROG="Busybox Wget" # reuse for error logging
  722. else
  723. write_log 13 "Neither 'Wget' nor 'cURL' nor 'uclient-fetch' installed or executable"
  724. fi
  725. while : ; do
  726. write_log 7 "#> $__RUNPROG"
  727. eval $__RUNPROG # DO transfer
  728. __ERR=$? # save error code
  729. [ $__ERR -eq 0 ] && return 0 # no error leave
  730. [ -n "$LUCI_HELPER" ] && return 1 # no retry if called by LuCI helper script
  731. write_log 3 "$__PROG Error: '$__ERR'"
  732. write_log 7 "$(cat $ERRFILE)" # report error
  733. [ $VERBOSE -gt 1 ] && {
  734. # VERBOSE > 1 then NO retry
  735. write_log 4 "Transfer failed - Verbose Mode: $VERBOSE - NO retry on error"
  736. return 1
  737. }
  738. __CNT=$(( $__CNT + 1 )) # increment error counter
  739. # if error count > retry_count leave here
  740. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  741. write_log 14 "Transfer failed after $retry_count retries"
  742. write_log 4 "Transfer failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  743. sleep $RETRY_SECONDS &
  744. PID_SLEEP=$!
  745. wait $PID_SLEEP # enable trap-handler
  746. PID_SLEEP=0
  747. done
  748. # we should never come here there must be a programming error
  749. write_log 12 "Error in 'do_transfer()' - program coding error"
  750. }
  751. send_update() {
  752. # $1 # IP to set at DDNS service provider
  753. local __IP
  754. [ $# -ne 1 ] && write_log 12 "Error calling 'send_update()' - wrong number of parameters"
  755. if [ $upd_privateip -eq 0 ]; then
  756. # verify given IP / no private IPv4's / no IPv6 addr starting with fxxx of with ":"
  757. [ $use_ipv6 -eq 0 ] && __IP=$(echo $1 | grep -v -E "(^0|^10\.|^100\.6[4-9]\.|^100\.[7-9][0-9]\.|^100\.1[0-1][0-9]\.|^100\.12[0-7]\.|^127|^169\.254|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.|^192\.168)")
  758. [ $use_ipv6 -eq 1 ] && __IP=$(echo $1 | grep "^[0-9a-eA-E]")
  759. else
  760. __IP=$(echo $1 | grep -m 1 -o "$IPV4_REGEX") # valid IPv4 or
  761. [ -z "$__IP" ] && __IP=$(echo $1 | grep -m 1 -o "$IPV6_REGEX") # IPv6
  762. fi
  763. [ -z "$__IP" ] && {
  764. write_log 3 "No or private or invalid IP '$1' given! Please check your configuration"
  765. return 127
  766. }
  767. if [ -n "$update_script" ]; then
  768. write_log 7 "parsing script '$update_script'"
  769. . $update_script
  770. else
  771. local __URL __ERR
  772. # do replaces in URL
  773. __URL=$(echo $update_url | sed -e "s#\[USERNAME\]#$URL_USER#g" -e "s#\[PASSWORD\]#$URL_PASS#g" \
  774. -e "s#\[PARAMENC\]#$URL_PENC#g" -e "s#\[PARAMOPT\]#$param_opt#g" \
  775. -e "s#\[DOMAIN\]#$domain#g" -e "s#\[IP\]#$__IP#g")
  776. [ $use_https -ne 0 ] && __URL=$(echo $__URL | sed -e 's#^http:#https:#')
  777. do_transfer "$__URL" || return 1
  778. write_log 7 "DDNS Provider answered:\n$(cat $DATFILE)"
  779. [ -z "$UPD_ANSWER" ] && return 0 # not set then ignore
  780. grep -i -E "$UPD_ANSWER" $DATFILE >/dev/null 2>&1
  781. return $? # "0" if found
  782. fi
  783. }
  784. get_local_ip () {
  785. # $1 Name of Variable to store local IP (LOCAL_IP)
  786. local __CNT=0 # error counter
  787. local __RUNPROG __DATA __URL __ERR
  788. [ $# -ne 1 ] && write_log 12 "Error calling 'get_local_ip()' - wrong number of parameters"
  789. write_log 7 "Detect local IP on '$ip_source'"
  790. while : ; do
  791. if [ -n "$ip_network" ]; then
  792. # set correct program
  793. [ $use_ipv6 -eq 0 ] && __RUNPROG="network_get_ipaddr" \
  794. || __RUNPROG="network_get_ipaddr6"
  795. eval "$__RUNPROG __DATA $ip_network" || \
  796. write_log 13 "Can not detect local IP using $__RUNPROG '$ip_network' - Error: '$?'"
  797. [ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected on network '$ip_network'"
  798. elif [ -n "$ip_interface" ]; then
  799. local __DATA4=""; local __DATA6=""
  800. if [ -n "$(which ip)" ]; then # ip program installed
  801. write_log 7 "#> ip -o addr show dev $ip_interface scope global >$DATFILE 2>$ERRFILE"
  802. ip -o addr show dev $ip_interface scope global >$DATFILE 2>$ERRFILE
  803. __ERR=$?
  804. if [ $__ERR -eq 0 ]; then
  805. # DATFILE (sample)
  806. # 10: l2tp-inet: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1456 qdisc fq_codel state UNKNOWN qlen 3\ link/ppp
  807. # 10: l2tp-inet inet 95.30.176.51 peer 95.30.176.1/32 scope global l2tp-inet\ valid_lft forever preferred_lft forever
  808. # 5: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP qlen 1000\ link/ether 08:00:27:d0:10:32 brd ff:ff:ff:ff:ff:ff
  809. # 5: eth1 inet 172.27.10.128/24 brd 172.27.10.255 scope global eth1\ valid_lft forever preferred_lft forever
  810. # 5: eth1 inet 172.55.55.155/24 brd 172.27.10.255 scope global eth1\ valid_lft 12345sec preferred_lft 12345sec
  811. # 5: eth1 inet6 2002:b0c7:f326::806b:c629:b8b9:433/128 scope global dynamic \ valid_lft 8026sec preferred_lft 8026sec
  812. # 5: eth1 inet6 fd43:5368:6f6d:6500:806b:c629:b8b9:433/128 scope global dynamic \ valid_lft 8026sec preferred_lft 8026sec
  813. # 5: eth1 inet6 fd43:5368:6f6d:6500:a00:27ff:fed0:1032/64 scope global dynamic \ valid_lft 14352sec preferred_lft 14352sec
  814. # 5: eth1 inet6 2002:b0c7:f326::a00:27ff:fed0:1032/64 scope global dynamic \ valid_lft 14352sec preferred_lft 14352sec
  815. # remove remove remove replace replace
  816. # link inet6 fxxx sec forever=>-1 / => ' ' to separate subnet from ip
  817. sed "/link/d; /inet6 f/d; s/sec//g; s/forever/-1/g; s/\// /g" $DATFILE | \
  818. awk '{ print $3" "$4" "$NF }' > $ERRFILE # temp reuse ERRFILE
  819. # we only need inet? IP prefered time
  820. local __TIME4=0; local __TIME6=0
  821. local __TYP __ADR __TIME
  822. while read __TYP __ADR __TIME; do
  823. __TIME=${__TIME:-0} # supress shell errors on last (empty) line of DATFILE
  824. # IPversion no "-1" record stored - now "-1" record or new time > oldtime
  825. [ "$__TYP" = "inet6" -a $__TIME6 -ge 0 -a \( $__TIME -lt 0 -o $__TIME -gt $__TIME6 \) ] && {
  826. __DATA6="$__ADR"
  827. __TIME6="$__TIME"
  828. }
  829. [ "$__TYP" = "inet" -a $__TIME4 -ge 0 -a \( $__TIME -lt 0 -o $__TIME -gt $__TIME4 \) ] && {
  830. __DATA4="$__ADR"
  831. __TIME4="$__TIME"
  832. }
  833. done < $ERRFILE
  834. else
  835. write_log 3 "ip Error: '$__ERR'"
  836. write_log 7 "$(cat $ERRFILE)" # report error
  837. fi
  838. else # use deprecated ifconfig
  839. write_log 7 "#> ifconfig $ip_interface >$DATFILE 2>$ERRFILE"
  840. ifconfig $ip_interface >$DATFILE 2>$ERRFILE
  841. __ERR=$?
  842. if [ $__ERR -eq 0 ]; then
  843. __DATA4=$(awk '
  844. /inet addr:/ { # Filter IPv4
  845. # inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0
  846. $1=""; # remove inet
  847. $3=""; # remove Bcast: ...
  848. $4=""; # remove Mask: ...
  849. FS=":"; # separator ":"
  850. $0=$0; # reread to activate separator
  851. $1=""; # remove addr
  852. FS=" "; # set back separator to default " "
  853. $0=$0; # reread to activate separator (remove whitespaces)
  854. print $1; # print IPv4 addr
  855. }' $DATFILE
  856. )
  857. __DATA6=$(awk '
  858. /inet6/ && /: [0-9a-eA-E]/ { # Filter IPv6 exclude fxxx
  859. # inet6 addr: 2001:db8::xxxx:xxxx/32 Scope:Global
  860. FS="/"; # separator "/"
  861. $0=$0; # reread to activate separator
  862. $2=""; # remove everything behind "/"
  863. FS=" "; # set back separator to default " "
  864. $0=$0; # reread to activate separator
  865. print $3; # print IPv6 addr
  866. }' $DATFILE
  867. )
  868. else
  869. write_log 3 "ifconfig Error: '$__ERR'"
  870. write_log 7 "$(cat $ERRFILE)" # report error
  871. fi
  872. fi
  873. [ $use_ipv6 -eq 0 ] && __DATA="$__DATA4" || __DATA="$__DATA6"
  874. [ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected on interface '$ip_interface'"
  875. elif [ -n "$ip_script" ]; then
  876. write_log 7 "#> $ip_script >$DATFILE 2>$ERRFILE"
  877. eval $ip_script >$DATFILE 2>$ERRFILE
  878. __ERR=$?
  879. if [ $__ERR -eq 0 ]; then
  880. __DATA=$(cat $DATFILE)
  881. [ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected via script '$ip_script'"
  882. else
  883. write_log 3 "$ip_script Error: '$__ERR'"
  884. write_log 7 "$(cat $ERRFILE)" # report error
  885. fi
  886. elif [ -n "$ip_url" ]; then
  887. do_transfer "$ip_url"
  888. # use correct regular expression
  889. [ $use_ipv6 -eq 0 ] \
  890. && __DATA=$(grep -m 1 -o "$IPV4_REGEX" $DATFILE) \
  891. || __DATA=$(grep -m 1 -o "$IPV6_REGEX" $DATFILE)
  892. [ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected on web at '$ip_url'"
  893. else
  894. write_log 12 "Error in 'get_local_ip()' - unhandled ip_source '$ip_source'"
  895. fi
  896. # valid data found return here
  897. [ -n "$__DATA" ] && {
  898. eval "$1=\"$__DATA\""
  899. return 0
  900. }
  901. [ -n "$LUCI_HELPER" ] && return 1 # no retry if called by LuCI helper script
  902. write_log 7 "Data detected:"
  903. write_log 7 "$(cat $DATFILE)"
  904. [ $VERBOSE -gt 1 ] && {
  905. # VERBOSE > 1 then NO retry
  906. write_log 4 "Get local IP via '$ip_source' failed - Verbose Mode: $VERBOSE - NO retry on error"
  907. return 1
  908. }
  909. __CNT=$(( $__CNT + 1 )) # increment error counter
  910. # if error count > retry_count leave here
  911. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  912. write_log 14 "Get local IP via '$ip_source' failed after $retry_count retries"
  913. write_log 4 "Get local IP via '$ip_source' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  914. sleep $RETRY_SECONDS &
  915. PID_SLEEP=$!
  916. wait $PID_SLEEP # enable trap-handler
  917. PID_SLEEP=0
  918. done
  919. # we should never come here there must be a programming error
  920. write_log 12 "Error in 'get_local_ip()' - program coding error"
  921. }
  922. get_registered_ip() {
  923. # $1 Name of Variable to store public IP (REGISTERED_IP)
  924. # $2 (optional) if set, do not retry on error
  925. local __CNT=0 # error counter
  926. local __ERR=255
  927. local __REGEX __PROG __RUNPROG __DATA __IP
  928. # return codes
  929. # 1 no IP detected
  930. [ $# -lt 1 -o $# -gt 2 ] && write_log 12 "Error calling 'get_registered_ip()' - wrong number of parameters"
  931. [ $is_glue -eq 1 -a -z "$BIND_HOST" ] && write_log 14 "Lookup of glue records is only supported using BIND host"
  932. write_log 7 "Detect registered/public IP"
  933. # set correct regular expression
  934. [ $use_ipv6 -eq 0 ] && __REGEX="$IPV4_REGEX" || __REGEX="$IPV6_REGEX"
  935. if [ -n "$BIND_HOST" ]; then
  936. __PROG="$BIND_HOST"
  937. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -t A" || __PROG="$__PROG -t AAAA"
  938. if [ $force_ipversion -eq 1 ]; then # force IP version
  939. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -4" || __PROG="$__PROG -6"
  940. fi
  941. [ $force_dnstcp -eq 1 ] && __PROG="$__PROG -T" # force TCP
  942. [ $is_glue -eq 1 ] && __PROG="$__PROG -v" # use verbose output to get additional section
  943. __RUNPROG="$__PROG $lookup_host $dns_server >$DATFILE 2>$ERRFILE"
  944. __PROG="BIND host"
  945. elif [ -n "$KNOT_HOST" ]; then
  946. __PROG="$KNOT_HOST"
  947. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -t A" || __PROG="$__PROG -t AAAA"
  948. if [ $force_ipversion -eq 1 ]; then # force IP version
  949. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -4" || __PROG="$__PROG -6"
  950. fi
  951. [ $force_dnstcp -eq 1 ] && __PROG="$__PROG -T" # force TCP
  952. __RUNPROG="$__PROG $lookup_host $dns_server >$DATFILE 2>$ERRFILE"
  953. __PROG="Knot host"
  954. elif [ -n "$DRILL" ]; then
  955. __PROG="$DRILL -V0" # drill options name @server type
  956. if [ $force_ipversion -eq 1 ]; then # force IP version
  957. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG -4" || __PROG="$__PROG -6"
  958. fi
  959. [ $force_dnstcp -eq 1 ] && __PROG="$__PROG -t" || __PROG="$__PROG -u" # force TCP
  960. __PROG="$__PROG $lookup_host"
  961. [ -n "$dns_server" ] && __PROG="$__PROG @$dns_server"
  962. [ $use_ipv6 -eq 0 ] && __PROG="$__PROG A" || __PROG="$__PROG AAAA"
  963. __RUNPROG="$__PROG >$DATFILE 2>$ERRFILE"
  964. __PROG="drill"
  965. elif [ -n "$HOSTIP" ]; then # hostip package installed
  966. __PROG="$HOSTIP"
  967. [ $force_dnstcp -ne 0 ] && \
  968. write_log 14 "hostip - no support for 'DNS over TCP'"
  969. # is IP given as dns_server ?
  970. __IP=$(echo $dns_server | grep -m 1 -o "$IPV4_REGEX")
  971. [ -z "$__IP" ] && __IP=$(echo $dns_server | grep -m 1 -o "$IPV6_REGEX")
  972. # we got NO ip for dns_server, so build command
  973. [ -z "$__IP" -a -n "$dns_server" ] && {
  974. __IP="\`$HOSTIP"
  975. [ $use_ipv6 -eq 1 -a $force_ipversion -eq 1 ] && __IP="$__IP -6"
  976. __IP="$__IP $dns_server | grep -m 1 -o"
  977. [ $use_ipv6 -eq 1 -a $force_ipversion -eq 1 ] \
  978. && __IP="$__IP '$IPV6_REGEX'" \
  979. || __IP="$__IP '$IPV4_REGEX'"
  980. __IP="$__IP \`"
  981. }
  982. [ $use_ipv6 -eq 1 ] && __PROG="$__PROG -6"
  983. [ -n "$dns_server" ] && __PROG="$__PROG -r $__IP"
  984. __RUNPROG="$__PROG $lookup_host >$DATFILE 2>$ERRFILE"
  985. __PROG="hostip"
  986. elif [ -n "$NSLOOKUP" ]; then # last use BusyBox nslookup
  987. [ $force_dnstcp -ne 0 ] && \
  988. write_log 14 "Busybox nslookup - no support for 'DNS over TCP'"
  989. [ -n "$NSLOOKUP_MUSL" -a -n "$dns_server" ] && \
  990. write_log 14 "Busybox compiled with musl - nslookup don't support the use of DNS Server"
  991. [ $force_ipversion -ne 0 ] && \
  992. write_log 5 "Busybox nslookup - no support to 'force IP Version' (ignored)"
  993. __RUNPROG="$NSLOOKUP $lookup_host $dns_server >$DATFILE 2>$ERRFILE"
  994. __PROG="BusyBox nslookup"
  995. else # there must be an error
  996. write_log 12 "Error in 'get_registered_ip()' - no supported Name Server lookup software accessible"
  997. fi
  998. while : ; do
  999. write_log 7 "#> $__RUNPROG"
  1000. eval $__RUNPROG
  1001. __ERR=$?
  1002. if [ $__ERR -ne 0 ]; then
  1003. write_log 3 "$__PROG error: '$__ERR'"
  1004. write_log 7 "$(cat $ERRFILE)"
  1005. else
  1006. if [ -n "$BIND_HOST" -o -n "$KNOT_HOST" ]; then
  1007. if [ $is_glue -eq 1 ]; then
  1008. __DATA=$(cat $DATFILE | grep "^$lookup_host" | grep -om1 "$__REGEX" )
  1009. else
  1010. __DATA=$(cat $DATFILE | awk -F "address " '/has/ {print $2; exit}' )
  1011. fi
  1012. elif [ -n "$DRILL" ]; then
  1013. __DATA=$(cat $DATFILE | awk '/^'"$lookup_host"'/ {print $5; exit}' )
  1014. elif [ -n "$HOSTIP" ]; then
  1015. __DATA=$(cat $DATFILE | grep -om1 "$__REGEX")
  1016. elif [ -n "$NSLOOKUP" ]; then
  1017. __DATA=$(cat $DATFILE | sed -ne "/^Name:/,\$ { s/^Address[0-9 ]\{0,\}: \($__REGEX\).*$/\\1/p }" )
  1018. fi
  1019. [ -n "$__DATA" ] && {
  1020. write_log 7 "Registered IP '$__DATA' detected"
  1021. eval "$1=\"$__DATA\"" # valid data found
  1022. return 0 # leave here
  1023. }
  1024. write_log 4 "NO valid IP found"
  1025. __ERR=127
  1026. fi
  1027. [ -n "$LUCI_HELPER" ] && return $__ERR # no retry if called by LuCI helper script
  1028. [ -n "$2" ] && return $__ERR # $2 is given -> no retry
  1029. [ $VERBOSE -gt 1 ] && {
  1030. # VERBOSE > 1 then NO retry
  1031. write_log 4 "Get registered/public IP for '$lookup_host' failed - Verbose Mode: $VERBOSE - NO retry on error"
  1032. return $__ERR
  1033. }
  1034. __CNT=$(( $__CNT + 1 )) # increment error counter
  1035. # if error count > retry_count leave here
  1036. [ $retry_count -gt 0 -a $__CNT -gt $retry_count ] && \
  1037. write_log 14 "Get registered/public IP for '$lookup_host' failed after $retry_count retries"
  1038. write_log 4 "Get registered/public IP for '$lookup_host' failed - retry $__CNT/$retry_count in $RETRY_SECONDS seconds"
  1039. sleep $RETRY_SECONDS &
  1040. PID_SLEEP=$!
  1041. wait $PID_SLEEP # enable trap-handler
  1042. PID_SLEEP=0
  1043. done
  1044. # we should never come here there must be a programming error
  1045. write_log 12 "Error in 'get_registered_ip()' - program coding error"
  1046. }
  1047. get_uptime() {
  1048. # $1 Variable to store result in
  1049. [ $# -ne 1 ] && write_log 12 "Error calling 'verify_host_port()' - wrong number of parameters"
  1050. local __UPTIME=$(cat /proc/uptime)
  1051. eval "$1=\"${__UPTIME%%.*}\""
  1052. }
  1053. trap_handler() {
  1054. # $1 trap signal
  1055. # $2 optional (exit status)
  1056. local __PIDS __PID
  1057. local __ERR=${2:-0}
  1058. local __OLD_IFS=$IFS
  1059. local __NEWLINE_IFS='
  1060. ' # __NEWLINE_IFS
  1061. [ $PID_SLEEP -ne 0 ] && kill -$1 $PID_SLEEP 2>/dev/null # kill pending sleep if exist
  1062. case $1 in
  1063. 0) if [ $__ERR -eq 0 ]; then
  1064. write_log 5 "PID '$$' exit normal at $(eval $DATE_PROG)\n"
  1065. else
  1066. write_log 4 "PID '$$' exit WITH ERROR '$__ERR' at $(eval $DATE_PROG)\n"
  1067. fi ;;
  1068. 1) write_log 6 "PID '$$' received 'SIGHUP' at $(eval $DATE_PROG)"
  1069. # reload config via starting the script again
  1070. /usr/lib/ddns/dynamic_dns_updater.sh -v "0" -S "$__SECTIONID" -- start || true
  1071. exit 0 ;; # and leave this one
  1072. 2) write_log 5 "PID '$$' terminated by 'SIGINT' at $(eval $DATE_PROG)\n";;
  1073. 3) write_log 5 "PID '$$' terminated by 'SIGQUIT' at $(eval $DATE_PROG)\n";;
  1074. 15) write_log 5 "PID '$$' terminated by 'SIGTERM' at $(eval $DATE_PROG)\n";;
  1075. *) write_log 13 "Unhandled signal '$1' in 'trap_handler()'";;
  1076. esac
  1077. __PIDS=$(pgrep -P $$) # get my childs (pgrep prints with "newline")
  1078. IFS=$__NEWLINE_IFS
  1079. for __PID in $__PIDS; do
  1080. kill -$1 $__PID # terminate it
  1081. done
  1082. IFS=$__OLD_IFS
  1083. # remove out and err file
  1084. [ -f $DATFILE ] && rm -f $DATFILE
  1085. [ -f $ERRFILE ] && rm -f $ERRFILE
  1086. # exit with correct handling:
  1087. # remove trap handling settings and send kill to myself
  1088. trap - 0 1 2 3 15
  1089. [ $1 -gt 0 ] && kill -$1 $$
  1090. }
  1091. split_FQDN() {
  1092. # $1 FQDN to split
  1093. # $2 name of variable to store TLD
  1094. # $3 name of variable to store (reg)Domain
  1095. # $4 name of variable to store Host/Subdomain
  1096. [ $# -ne 4 ] && write_log 12 "Error calling 'split_FQDN()' - wrong number of parameters"
  1097. [ -z "$1" ] && write_log 12 "Error calling 'split_FQDN()' - missing FQDN to split"
  1098. [ -f $TLDFILE ] || write_log 12 "Error calling 'split_FQDN()' - missing file '$TLDFILE'"
  1099. local _HOST _FDOM _CTLD _FTLD
  1100. local _SET="$@" # save given function parameters
  1101. local _PAR=$(echo "$1" | tr [A-Z] [a-z] | tr "." " ") # to lower and replace DOT with SPACE
  1102. set -- $_PAR # set new as function parameters
  1103. _PAR="" # clear variable for later reuse
  1104. while [ -n "$1" ] ; do # as long we have parameters
  1105. _PAR="$1 $_PAR" # invert order of parameters
  1106. shift
  1107. done
  1108. set -- $_PAR # use new as function parameters
  1109. _PAR="" # clear variable
  1110. while [ -n "$1" ] ; do # as long we have parameters
  1111. if [ -z "$_CTLD" ]; then # first loop
  1112. _CTLD="$1" # CURRENT TLD to look at
  1113. shift
  1114. else
  1115. _CTLD="$1.$_CTLD" # Next TLD to look at
  1116. shift
  1117. fi
  1118. # check if TLD exact match in tld_names.dat, save TLD
  1119. zcat $TLDFILE | grep -E "^$_CTLD$" >/dev/null 2>&1 && {
  1120. _FTLD="$_CTLD" # save found
  1121. _FDOM="$1" # save domain next step might be invalid
  1122. continue
  1123. }
  1124. # check if match any "*" in tld_names.dat,
  1125. zcat $TLDFILE | grep -E "^\*.$_CTLD$" >/dev/null 2>&1 && {
  1126. [ -z "$1" ] && break # no more data break
  1127. # check if next level TLD match excludes "!" in tld_names.dat
  1128. if zcat $TLDFILE | grep -E "^!$1.$_CTLD$" >/dev/null 2>&1 ; then
  1129. _FTLD="$_CTLD" # Yes
  1130. else
  1131. _FTLD="$1.$_CTLD"
  1132. shift
  1133. fi
  1134. _FDOM="$1"; shift
  1135. }
  1136. [ -n "$_FTLD" ] && break # we have something valid, break
  1137. done
  1138. # the leftover parameters are the HOST/SUBDOMAIN
  1139. while [ -n "$1" ]; do
  1140. _HOST="$1 $_HOST" # remember we need to invert
  1141. shift
  1142. done
  1143. _HOST=$(echo $_HOST | tr " " ".") # insert DOT
  1144. set -- $_SET # set back parameters from function call
  1145. [ -n "$_FTLD" ] && {
  1146. eval "$2=$_FTLD" # set TLD
  1147. eval "$3=$_FDOM" # set registrable domain
  1148. eval "$4=$_HOST" # set HOST/SUBDOMAIN
  1149. return 0
  1150. }
  1151. eval "$2=''" # clear TLD
  1152. eval "$3=''" # clear registrable domain
  1153. eval "$4=''" # clear HOST/SUBDOMAIN
  1154. return 1
  1155. }
  1156. expand_ipv6() {
  1157. # Original written for bash by
  1158. #.Author: Florian Streibelt <florian@f-streibelt.de>
  1159. # Date: 08.04.2012
  1160. # License: Public Domain, but please be fair and
  1161. # attribute the original author(s) and provide
  1162. # a link to the original source for corrections:
  1163. #. https://github.com/mutax/IPv6-Address-checks
  1164. # $1 IPv6 to expand
  1165. # $2 name of variable to store expanded IPv6
  1166. [ $# -ne 2 ] && write_log 12 "Error calling 'expand_ipv6()' - wrong number of parameters"
  1167. INPUT="$(echo "$1" | tr 'A-F' 'a-f')"
  1168. [ "$INPUT" = "::" ] && INPUT="::0" # special case ::
  1169. O=""
  1170. while [ "$O" != "$INPUT" ]; do
  1171. O="$INPUT"
  1172. # fill all words with zeroes
  1173. INPUT=$( echo "$INPUT" | sed -e 's|:\([0-9a-f]\{3\}\):|:0\1:|g' \
  1174. -e 's|:\([0-9a-f]\{3\}\)$|:0\1|g' \
  1175. -e 's|^\([0-9a-f]\{3\}\):|0\1:|g' \
  1176. -e 's|:\([0-9a-f]\{2\}\):|:00\1:|g' \
  1177. -e 's|:\([0-9a-f]\{2\}\)$|:00\1|g' \
  1178. -e 's|^\([0-9a-f]\{2\}\):|00\1:|g' \
  1179. -e 's|:\([0-9a-f]\):|:000\1:|g' \
  1180. -e 's|:\([0-9a-f]\)$|:000\1|g' \
  1181. -e 's|^\([0-9a-f]\):|000\1:|g' )
  1182. done
  1183. # now expand the ::
  1184. ZEROES=""
  1185. echo "$INPUT" | grep -qs "::"
  1186. if [ "$?" -eq 0 ]; then
  1187. GRPS="$( echo "$INPUT" | sed 's|[0-9a-f]||g' | wc -m )"
  1188. GRPS=$(( GRPS-1 )) # remove carriage return
  1189. MISSING=$(( 8-GRPS ))
  1190. while [ $MISSING -gt 0 ]; do
  1191. ZEROES="$ZEROES:0000"
  1192. MISSING=$(( MISSING-1 ))
  1193. done
  1194. # be careful where to place the :
  1195. INPUT=$( echo "$INPUT" | sed -e 's|\(.\)::\(.\)|\1'$ZEROES':\2|g' \
  1196. -e 's|\(.\)::$|\1'$ZEROES':0000|g' \
  1197. -e 's|^::\(.\)|'$ZEROES':0000:\1|g;s|^:||g' )
  1198. fi
  1199. # an expanded address has 39 chars + CR
  1200. if [ $(echo $INPUT | wc -m) != 40 ]; then
  1201. write_log 4 "Error in 'expand_ipv6()' - invalid IPv6 found: '$1' expanded: '$INPUT'"
  1202. eval "$2='invalid'"
  1203. return 1
  1204. fi
  1205. # echo the fully expanded version of the address
  1206. eval "$2=$INPUT"
  1207. return 0
  1208. }