luci-app-ddns: remove first grep
[project/luci.git] / applications / luci-app-ddns / luasrc / tools / ddns.lua
old mode 100644 (file)
new mode 100755 (executable)
index 6d53931..fb8d0d2
--- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
+-- Copyright 2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
 -- Licensed to the public under the Apache License 2.0.
 
 module("luci.tools.ddns", package.seeall)
 
 local NX   = require "nixio"
 local NXFS = require "nixio.fs"
-local OPKG = require "luci.model.ipkg"
 local UCI  = require "luci.model.uci"
 local SYS  = require "luci.sys"
-local UTIL = require "luci.util"
+
+function env_info(type)
+
+       if ( type == "has_ssl" ) or ( type == "has_proxy" ) or ( type == "has_forceip" )
+           or ( type == "has_bindnet" ) or ( type == "has_fetch" )
+               or ( type == "has_wgetssl" ) or ( type == "has_curl" )
+               or ( type == "has_curlssl" ) or ( type == "has_curlpxy" )
+               or ( type == "has_fetchssl" ) or ( type == "has_bbwget" ) then
+
+               local function has_wgetssl()
+                       return (SYS.call( [[which wget-ssl >/dev/null 2>&1]] ) == 0)    -- and true or nil
+               end
+
+               local function has_curlssl()
+                       return (SYS.call( [[$(which curl) -V 2>&1 | grep -qF "https"]] ) == 0)
+               end
+
+               local function has_fetch()
+                       return (SYS.call( [[which uclient-fetch >/dev/null 2>&1]] ) == 0)
+               end
+
+               local function has_fetchssl()
+                       return NXFS.access("/lib/libustream-ssl.so")
+               end
+
+               local function has_curl()
+                       return (SYS.call( [[which curl >/dev/null 2>&1]] ) == 0)
+               end
+
+               local function has_curlpxy()
+                       return (SYS.call( [[grep -i "all_proxy" /usr/lib/libcurl.so* >/dev/null 2>&1]] ) == 0)
+               end
+
+               local function has_bbwget()
+                       return (SYS.call( [[$(which wget) -V 2>&1 | grep -iqF "busybox"]] ) == 0)
+               end
+
+               if type == "has_wgetssl" then
+                       return has_wgetssl()
+
+               elseif type == "has_curl" then
+                       return has_curl()
+
+               elseif type == "has_curlssl" then
+                       return has_curlssl()
+
+               elseif type == "has_curlpxy" then
+                       return has_curlpxy()
+
+               elseif type == "has_fetch" then
+                       return has_fetch()
+
+               elseif type == "has_fetchssl" then
+                       return has_fetchssl()
+
+               elseif type == "has_bbwget" then
+                       return has_bbwget()
+
+               elseif type == "has_ssl" then
+                       if has_wgetssl() then return true end
+                       if has_curlssl() then return true end
+                       if (has_fetch() and has_fetchssl()) then return true end
+                       return false
+
+               elseif type == "has_proxy" then
+                       if has_wgetssl() then return true end
+                       if has_curlpxy() then return true end
+                       if has_fetch() then return true end
+                       if has_bbwget() then return true end
+                       return false
+
+               elseif type == "has_forceip" then
+                       if has_wgetssl() then return true end
+                       if has_curl() then return true end
+                       if has_fetch() then return true end -- only really needed for transfer
+                       return false
+
+               elseif type == "has_bindnet" then
+                       if has_curl() then return true end
+                       if has_wgetssl() then return true end
+                       return false
+               end
+
+       elseif ( type == "has_dnsserver" ) or ( type == "has_bindhost" ) or ( type == "has_hostip" ) or ( type == "has_nslookup" ) then
+               local function has_bindhost()
+                       if (SYS.call( [[which host >/dev/null 2>&1]] ) == 0) then return true end
+                       if (SYS.call( [[which khost >/dev/null 2>&1]] ) == 0) then return true end
+                       if (SYS.call( [[which drill >/dev/null 2>&1]] ) == 0) then return true end
+                       return false
+               end
+
+               local function has_hostip()
+                       return (SYS.call( [[which hostip >/dev/null 2>&1]] ) == 0)
+               end
+
+               local function has_nslookup()
+                       return (SYS.call( [[$(which nslookup) localhost 2>&1 | grep -qF "(null)"]] ) ~= 0)
+               end
+
+               if type == "has_bindhost" then
+                       return has_bindhost()
+               elseif type == "has_hostip" then
+                       return has_hostip()
+               elseif type == "has_nslookup" then
+                       return has_nslookup()
+               elseif type == "has_dnsserver" then
+                       if has_bindhost() then return true end
+                       if has_hostip() then return true end
+                       if has_nslookup() then return true end
+                       return false
+               end
+
+       elseif type == "has_ipv6" then
+               return (NXFS.access("/proc/net/ipv6_route") and NXFS.access("/usr/sbin/ip6tables"))
+
+       elseif type == "has_cacerts" then
+               --old _check_certs() local function
+               local _, v = NXFS.glob("/etc/ssl/certs/*.crt")
+               if ( v == 0 ) then _, v = NXFS.glob("/etc/ssl/certs/*.pem") end
+               return (v > 0)
+       else
+               return
+       end
+
+end
 
 -- function to calculate seconds from given interval and unit
 function calc_seconds(interval, unit)
@@ -27,47 +150,11 @@ function calc_seconds(interval, unit)
        end
 end
 
--- check if IPv6 supported by OpenWrt
-function check_ipv6()
-       return NXFS.access("/proc/net/ipv6_route")
-          and NXFS.access("/usr/sbin/ip6tables")
-end
-
--- check if Wget with SSL support or cURL installed
-function check_ssl()
-       if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) then
-               return true
-       else
-               return NXFS.access("/usr/bin/curl")
-       end
-end
-
--- check if Wget with SSL or cURL with proxy support installed
-function check_proxy()
-       -- we prefere GNU Wget for communication
-       if (SYS.call([[ grep -i "\+ssl" /usr/bin/wget >/dev/null 2>&1 ]]) == 0) then
-               return true
-
-       -- if not installed cURL must support proxy
-       elseif NXFS.access("/usr/bin/curl") then
-               return (SYS.call([[ grep -i all_proxy /usr/lib/libcurl.so* >/dev/null 2>&1 ]]) == 0)
-
-       -- only BusyBox Wget is installed
-       else
-               return NXFS.access("/usr/bin/wget")
-       end
-end
-
--- check if BIND host installed
-function check_bind_host()
-       return NXFS.access("/usr/bin/host")
-end
-
 -- convert epoch date to given format
 function epoch2date(epoch, format)
        if not format or #format < 2 then
                local uci = UCI.cursor()
-               format    = uci:get("ddns", "global", "date_format") or "%F %R"
+               format    = uci:get("ddns", "global", "ddns_dateformat") or "%F %R"
                uci:unload("ddns")
        end
        format = format:gsub("%%n", "<br />")   -- replace newline
@@ -77,18 +164,35 @@ end
 
 -- read lastupdate from [section].update file
 function get_lastupd(section)
-       local uci     = UCI.cursor()
-       local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
-       local etime   = tonumber(NXFS.readfile("%s/%s.update" % { run_dir, section } ) or 0 )
+       local uci   = UCI.cursor()
+       local rdir  = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
+       local etime = tonumber(NXFS.readfile("%s/%s.update" % { rdir, section } ) or 0 )
        uci:unload("ddns")
        return etime
 end
 
+-- read registered IP from [section].ip file
+function get_regip(section, chk_sec)
+       local uci   = UCI.cursor()
+       local rdir  = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
+       local ip = "NOFILE"
+       if NXFS.access("%s/%s.ip" % { rdir, section }) then
+               local ftime = NXFS.stat("%s/%s.ip" % { rdir, section }, "ctime") or 0
+               local otime = os.time()
+               -- give ddns-scripts time (9 sec) to update file
+               if otime < (ftime + chk_sec + 9) then
+                       ip = NXFS.readfile("%s/%s.ip" % { rdir, section })
+               end
+       end
+       uci:unload("ddns")
+       return ip
+end
+
 -- read PID from run file and verify if still running
 function get_pid(section)
-       local uci     = UCI.cursor()
-       local run_dir = uci:get("ddns", "global", "run_dir") or "/var/run/ddns"
-       local pid     = tonumber(NXFS.readfile("%s/%s.pid" % { run_dir, section } ) or 0 )
+       local uci  = UCI.cursor()
+       local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
+       local pid  = tonumber(NXFS.readfile("%s/%s.pid" % { rdir, section } ) or 0 )
        if pid > 0 and not NX.kill(pid, 0) then
                pid = 0
        end
@@ -96,73 +200,6 @@ function get_pid(section)
        return pid
 end
 
--- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
-function ipkg_ver_compare(ver1, comp, ver2)
-       if not ver1 or not (#ver1 > 0)
-       or not ver2 or not (#ver2 > 0)
-       or not comp or not (#comp > 0) then return nil end
-       -- correct compare string
-       if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
-       elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
-       elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
-       elseif comp == "="  or comp == "==" then comp = "=="
-       elseif comp == "<<" then comp = "<"
-       elseif comp == ">>" then comp = ">"
-       else return nil end
-
-       local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
-       local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
-
-       for i = 1, math.max(table.getn(av1),table.getn(av2)), 1  do
-               local s1 = av1[i] or ""
-               local s2 = av2[i] or ""
-               local n1 = tonumber(s1)
-               local n2 = tonumber(s2)
-
-               -- one numeric and other empty string then set other to 0
-               if n1 and not n2 and (not s2 or #s2 == 0) then n2 = 0 end
-               if n2 and not n1 and (not s1 or #s1 == 0) then n1 = 0 end
-
-               local nc = (n1 and n2)  -- numeric compare
-
-               if nc then
-                       -- first "not equal" found return true
-                       if comp == "~=" and (n1 ~= n2) then return true end
-                       -- first "lower" found return true
-                       if (comp == "<" or comp == "<=") and (n1 < n2) then return true end
-                       -- first "greater" found return true
-                       if (comp == ">" or comp == ">=") and (n1 > n2) then return true end
-                       -- not equal then return false
-                       if (n1 ~= n2) then return false end
-               else
-                       if comp == "~=" and (s1 ~= s2) then return true end
-                       if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
-                       if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
-                       if (s1 ~= s2) then return false end
-               end
-       end
-       -- all equal then true
-       return true
-end
-
--- read version information for given package if installed
-function ipkg_ver_installed(pkg)
-       local version = nil
-       local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
-       if control then
-               local ln
-               repeat
-                       ln = control:read("*l")
-                       if ln and ln:match("^Version: ") then
-                               version = ln:gsub("^Version: ", "")
-                               break
-                       end
-               until not ln
-               control:close()
-       end
-       return version
-end
-
 -- replacement of build-in read of UCI option
 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
 -- needed to read from other option then current value definition
@@ -187,24 +224,98 @@ function read_value(self, section, option)
        end
 end
 
--- replacement of build-in Flag.parse of cbi.lua
--- modified to mark section as changed if value changes
--- current parse did not do this, but it is done AbstaractValue.parse()
-function flag_parse(self, section)
-       local fexists = self.map:formvalue(
-               luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
-
-       if fexists then
-               local fvalue = self:formvalue(section) and self.enabled or self.disabled
-               local cvalue = self:cfgvalue(section)
-               if fvalue ~= self.default or (not self.optional and not self.rmempty) then
-                       self:write(section, fvalue)
-               else
-                       self:remove(section)
+-- replacement of build-in parse of "Value"
+-- modified AbstractValue.parse(self, section, novld) from cbi.lua
+-- validate is called if rmempty/optional true or false
+-- before write check if forcewrite, value eq default, and more
+function value_parse(self, section, novld)
+       local fvalue = self:formvalue(section)
+       local fexist = ( fvalue and (#fvalue > 0) )     -- not "nil" and "not empty"
+       local cvalue = self:cfgvalue(section)
+       local rm_opt = ( self.rmempty or self.optional )
+       local eq_cfg                                    -- flag: equal cfgvalue
+
+       -- If favlue and cvalue are both tables and have the same content
+       -- make them identical
+       if type(fvalue) == "table" and type(cvalue) == "table" then
+               eq_cfg = (#fvalue == #cvalue)
+               if eq_cfg then
+                       for i=1, #fvalue do
+                               if cvalue[i] ~= fvalue[i] then
+                                       eq_cfg = false
+                               end
+                       end
                end
-               if (fvalue ~= cvalue) then self.section.changed = true end
-       else
-               self:remove(section)
+               if eq_cfg then
+                       fvalue = cvalue
+               end
+       end
+
+       -- removed parameter "section" from function call because used/accepted nowhere
+       -- also removed call to function "transfer"
+       local vvalue, errtxt = self:validate(fvalue)
+
+       -- error handling; validate return "nil"
+       if not vvalue then
+               if novld then           -- and "novld" set
+                       return          -- then exit without raising an error
+               end
+
+               if fexist then          -- and there is a formvalue
+                       self:add_error(section, "invalid", errtxt or self.title .. ": invalid")
+                       return          -- so data are invalid
+               elseif not rm_opt then  -- and empty formvalue but NOT (rmempty or optional) set
+                       self:add_error(section, "missing", errtxt or self.title .. ": missing")
+                       return          -- so data is missing
+               elseif errtxt then
+                       self:add_error(section, "invalid", errtxt)
+                       return
+               end
+--             error  ("\n option: " .. self.option ..
+--                     "\n fvalue: " .. tostring(fvalue) ..
+--                     "\n fexist: " .. tostring(fexist) ..
+--                     "\n cvalue: " .. tostring(cvalue) ..
+--                     "\n vvalue: " .. tostring(vvalue) ..
+--                     "\n vexist: " .. tostring(vexist) ..
+--                     "\n rm_opt: " .. tostring(rm_opt) ..
+--                     "\n eq_cfg: " .. tostring(eq_cfg) ..
+--                     "\n eq_def: " .. tostring(eq_def) ..
+--                     "\n novld : " .. tostring(novld) ..
+--                     "\n errtxt: " .. tostring(errtxt) )
+       end
+
+       -- lets continue with value returned from validate
+       eq_cfg  = ( vvalue == cvalue )                                  -- update equal_config flag
+       local vexist = ( vvalue and (#vvalue > 0) ) and true or false   -- not "nil" and "not empty"
+       local eq_def = ( vvalue == self.default )                       -- equal_default flag
+
+       -- (rmempty or optional) and (no data or equal_default)
+       if rm_opt and (not vexist or eq_def) then
+               if self:remove(section) then            -- remove data from UCI
+                       self.section.changed = true     -- and push events
+               end
+               return
+       end
+
+       -- not forcewrite and no changes, so nothing to write
+       if not self.forcewrite and eq_cfg then
+               return
+       end
+
+       -- we should have a valid value here
+       assert (vvalue, "\n option: " .. self.option ..
+                       "\n fvalue: " .. tostring(fvalue) ..
+                       "\n fexist: " .. tostring(fexist) ..
+                       "\n cvalue: " .. tostring(cvalue) ..
+                       "\n vvalue: " .. tostring(vvalue) ..
+                       "\n vexist: " .. tostring(vexist) ..
+                       "\n rm_opt: " .. tostring(rm_opt) ..
+                       "\n eq_cfg: " .. tostring(eq_cfg) ..
+                       "\n eq_def: " .. tostring(eq_def) ..
+                       "\n errtxt: " .. tostring(errtxt) )
+
+       -- write data to UCI; raise event only on changes
+       if self:write(section, vvalue) and not eq_cfg then
                self.section.changed = true
        end
 end