1 -- Copyright 2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.tools.ddns", package.seeall)
6 local NX = require "nixio"
7 local NXFS = require "nixio.fs"
8 local OPKG = require "luci.model.ipkg"
9 local UCI = require "luci.model.uci"
10 local SYS = require "luci.sys"
11 local UTIL = require "luci.util"
13 local function _check_certs()
14 local _, v = NXFS.glob("/etc/ssl/certs/*.crt")
15 if ( v == 0 ) then _, v = NXFS.glob("/etc/ssl/certs/*.pem") end
19 has_wgetssl = (SYS.call( [[which wget-ssl >/dev/null 2>&1]] ) == 0) -- and true or nil
20 has_curl = (SYS.call( [[which curl >/dev/null 2>&1]] ) == 0)
21 has_curlssl = (SYS.call( [[$(which curl) -V 2>&1 | grep "Protocols:" | grep -qF "https"]] ) ~= 0)
22 has_curlpxy = (SYS.call( [[grep -i "all_proxy" /usr/lib/libcurl.so* >/dev/null 2>&1]] ) == 0)
23 has_fetch = (SYS.call( [[which uclient-fetch >/dev/null 2>&1]] ) == 0)
24 has_fetchssl = NXFS.access("/lib/libustream-ssl.so")
25 has_bbwget = (SYS.call( [[$(which wget) -V 2>&1 | grep -iqF "busybox"]] ) == 0)
26 has_bindhost = (SYS.call( [[which host >/dev/null 2>&1]] ) == 0)
27 or (SYS.call( [[which khost >/dev/null 2>&1]] ) == 0)
28 or (SYS.call( [[which drill >/dev/null 2>&1]] ) == 0)
29 has_hostip = (SYS.call( [[which hostip >/dev/null 2>&1]] ) == 0)
30 has_nslookup = (SYS.call( [[$(which nslookup) localhost 2>&1 | grep -qF "(null)"]] ) ~= 0)
31 has_ipv6 = (NXFS.access("/proc/net/ipv6_route") and NXFS.access("/usr/sbin/ip6tables"))
32 has_ssl = (has_wgetssl or has_curlssl or (has_fetch and has_fetchssl))
33 has_proxy = (has_wgetssl or has_curlpxy or has_fetch or has_bbwget)
34 has_forceip = (has_wgetssl or has_curl or has_fetch) -- only really needed for transfer
35 has_dnsserver = (has_bindhost or has_hostip or has_nslookup)
36 has_bindnet = (has_wgetssl or has_curl)
37 has_cacerts = _check_certs()
39 -- function to calculate seconds from given interval and unit
40 function calc_seconds(interval, unit)
41 if not tonumber(interval) then
43 elseif unit == "days" then
44 return (tonumber(interval) * 86400) -- 60 sec * 60 min * 24 h
45 elseif unit == "hours" then
46 return (tonumber(interval) * 3600) -- 60 sec * 60 min
47 elseif unit == "minutes" then
48 return (tonumber(interval) * 60) -- 60 sec
49 elseif unit == "seconds" then
50 return tonumber(interval)
56 -- convert epoch date to given format
57 function epoch2date(epoch, format)
58 if not format or #format < 2 then
59 local uci = UCI.cursor()
60 format = uci:get("ddns", "global", "ddns_dateformat") or "%F %R"
63 format = format:gsub("%%n", "<br />") -- replace newline
64 format = format:gsub("%%t", " ") -- replace tab
65 return os.date(format, epoch)
68 -- read lastupdate from [section].update file
69 function get_lastupd(section)
70 local uci = UCI.cursor()
71 local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
72 local etime = tonumber(NXFS.readfile("%s/%s.update" % { rdir, section } ) or 0 )
77 -- read registered IP from [section].ip file
78 function get_regip(section, chk_sec)
79 local uci = UCI.cursor()
80 local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
82 if NXFS.access("%s/%s.ip" % { rdir, section }) then
83 local ftime = NXFS.stat("%s/%s.ip" % { rdir, section }, "ctime") or 0
84 local otime = os.time()
85 -- give ddns-scripts time (9 sec) to update file
86 if otime < (ftime + chk_sec + 9) then
87 ip = NXFS.readfile("%s/%s.ip" % { rdir, section })
94 -- read PID from run file and verify if still running
95 function get_pid(section)
96 local uci = UCI.cursor()
97 local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
98 local pid = tonumber(NXFS.readfile("%s/%s.pid" % { rdir, section } ) or 0 )
99 if pid > 0 and not NX.kill(pid, 0) then
106 -- replacement of build-in read of UCI option
107 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
108 -- needed to read from other option then current value definition
109 function read_value(self, section, option)
111 if self.tag_error[section] then
112 value = self:formvalue(section)
114 value = self.map:get(section, option)
119 elseif not self.cast or self.cast == type(value) then
121 elseif self.cast == "string" then
122 if type(value) == "table" then
125 elseif self.cast == "table" then
130 -- replacement of build-in parse of "Value"
131 -- modified AbstractValue.parse(self, section, novld) from cbi.lua
132 -- validate is called if rmempty/optional true or false
133 -- before write check if forcewrite, value eq default, and more
134 function value_parse(self, section, novld)
135 local fvalue = self:formvalue(section)
136 local fexist = ( fvalue and (#fvalue > 0) ) -- not "nil" and "not empty"
137 local cvalue = self:cfgvalue(section)
138 local rm_opt = ( self.rmempty or self.optional )
139 local eq_cfg -- flag: equal cfgvalue
141 -- If favlue and cvalue are both tables and have the same content
142 -- make them identical
143 if type(fvalue) == "table" and type(cvalue) == "table" then
144 eq_cfg = (#fvalue == #cvalue)
147 if cvalue[i] ~= fvalue[i] then
157 -- removed parameter "section" from function call because used/accepted nowhere
158 -- also removed call to function "transfer"
159 local vvalue, errtxt = self:validate(fvalue)
161 -- error handling; validate return "nil"
163 if novld then -- and "novld" set
164 return -- then exit without raising an error
167 if fexist then -- and there is a formvalue
168 self:add_error(section, "invalid", errtxt or self.title .. ": invalid")
169 return -- so data are invalid
170 elseif not rm_opt then -- and empty formvalue but NOT (rmempty or optional) set
171 self:add_error(section, "missing", errtxt or self.title .. ": missing")
172 return -- so data is missing
174 self:add_error(section, "invalid", errtxt)
177 -- error ("\n option: " .. self.option ..
178 -- "\n fvalue: " .. tostring(fvalue) ..
179 -- "\n fexist: " .. tostring(fexist) ..
180 -- "\n cvalue: " .. tostring(cvalue) ..
181 -- "\n vvalue: " .. tostring(vvalue) ..
182 -- "\n vexist: " .. tostring(vexist) ..
183 -- "\n rm_opt: " .. tostring(rm_opt) ..
184 -- "\n eq_cfg: " .. tostring(eq_cfg) ..
185 -- "\n eq_def: " .. tostring(eq_def) ..
186 -- "\n novld : " .. tostring(novld) ..
187 -- "\n errtxt: " .. tostring(errtxt) )
190 -- lets continue with value returned from validate
191 eq_cfg = ( vvalue == cvalue ) -- update equal_config flag
192 local vexist = ( vvalue and (#vvalue > 0) ) and true or false -- not "nil" and "not empty"
193 local eq_def = ( vvalue == self.default ) -- equal_default flag
195 -- (rmempty or optional) and (no data or equal_default)
196 if rm_opt and (not vexist or eq_def) then
197 if self:remove(section) then -- remove data from UCI
198 self.section.changed = true -- and push events
203 -- not forcewrite and no changes, so nothing to write
204 if not self.forcewrite and eq_cfg then
208 -- we should have a valid value here
209 assert (vvalue, "\n option: " .. self.option ..
210 "\n fvalue: " .. tostring(fvalue) ..
211 "\n fexist: " .. tostring(fexist) ..
212 "\n cvalue: " .. tostring(cvalue) ..
213 "\n vvalue: " .. tostring(vvalue) ..
214 "\n vexist: " .. tostring(vexist) ..
215 "\n rm_opt: " .. tostring(rm_opt) ..
216 "\n eq_cfg: " .. tostring(eq_cfg) ..
217 "\n eq_def: " .. tostring(eq_def) ..
218 "\n errtxt: " .. tostring(errtxt) )
220 -- write data to UCI; raise event only on changes
221 if self:write(section, vvalue) and not eq_cfg then
222 self.section.changed = true
226 -----------------------------------------------------------------------------
227 -- copied from https://svn.nmap.org/nmap/nselib/url.lua
228 -- @author Diego Nehab
229 -- @author Eddie Bell <ejlbell@gmail.com>
231 URI parsing, composition and relative URL resolution
234 RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
235 parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
238 -- Parses a URL and returns a table with all its parts according to RFC 2396.
240 -- The following grammar describes the names given to the URL parts.
242 -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
243 -- <authority> ::= <userinfo>@<host>:<port>
244 -- <userinfo> ::= <user>[:<password>]
245 -- <path> :: = {<segment>/}<segment>
248 -- The leading <code>/</code> in <code>/<path></code> is considered part of
249 -- <code><path></code>.
250 -- @param url URL of request.
251 -- @param default Table with default values for each field.
252 -- @return A table with the following fields, where RFC naming conventions have
254 -- <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
255 -- <code>user</code>, <code>password</code>, <code>host</code>,
256 -- <code>port</code>, <code>path</code>, <code>params</code>,
257 -- <code>query</code>, and <code>fragment</code>.
258 -----------------------------------------------------------------------------
259 function parse_url(url) --, default)
260 -- initialize default parameters
262 -- for i,v in base.pairs(default or parsed) do
267 -- url = string.gsub(url, "%s", "")
269 url = string.gsub(url, "#(.*)$",
274 -- get scheme. Lower-case according to RFC 3986 section 3.1.
275 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
277 parsed.scheme = string.lower(s);
281 url = string.gsub(url, "^//([^/]*)",
286 -- get query stringing
287 url = string.gsub(url, "%?(.*)",
293 url = string.gsub(url, "%;(.*)",
298 -- path is whatever was left
301 local authority = parsed.authority
302 if not authority then
305 authority = string.gsub(authority,"^([^@]*)@",
310 authority = string.gsub(authority, ":([0-9]*)$",
317 if authority ~= "" then
318 parsed.host = authority
321 local userinfo = parsed.userinfo
325 userinfo = string.gsub(userinfo, ":([^:]*)$",
330 parsed.user = userinfo