dynamic_dns_functions.sh 49 KB

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