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 UCI = require "luci.model.uci"
9 local SYS = require "luci.sys"
11 function env_info(type)
13 if ( type == "has_ssl" ) or ( type == "has_proxy" ) or ( type == "has_forceip" )
14 or ( type == "has_bindnet" ) or ( type == "has_fetch" )
15 or ( type == "has_wgetssl" ) or ( type == "has_curl" )
16 or ( type == "has_curlssl" ) or ( type == "has_curlpxy" )
17 or ( type == "has_fetchssl" ) or ( type == "has_bbwget" ) then
19 local function has_wgetssl()
20 return (SYS.call( [[which wget-ssl >/dev/null 2>&1]] ) == 0) -- and true or nil
23 local function has_curlssl()
24 return (SYS.call( [[$(which curl) -V 2>&1 | grep "Protocols:" | grep -qF "https"]] ) == 0)
27 local function has_fetch()
28 return (SYS.call( [[which uclient-fetch >/dev/null 2>&1]] ) == 0)
31 local function has_fetchssl()
32 return NXFS.access("/lib/libustream-ssl.so")
35 local function has_curl()
36 return (SYS.call( [[which curl >/dev/null 2>&1]] ) == 0)
39 local function has_curlpxy()
40 return (SYS.call( [[grep -i "all_proxy" /usr/lib/libcurl.so* >/dev/null 2>&1]] ) == 0)
43 local function has_bbwget()
44 return (SYS.call( [[$(which wget) -V 2>&1 | grep -iqF "busybox"]] ) == 0)
47 if type == "has_wgetssl" then
50 elseif type == "has_curl" then
53 elseif type == "has_curlssl" then
56 elseif type == "has_curlpxy" then
59 elseif type == "has_fetch" then
62 elseif type == "has_fetchssl" then
65 elseif type == "has_bbwget" then
68 elseif type == "has_ssl" then
69 if has_wgetssl() then return true end
70 if has_curlssl() then return true end
71 if (has_fetch() and has_fetchssl()) then return true end
74 elseif type == "has_proxy" then
75 if has_wgetssl() then return true end
76 if has_curlpxy() then return true end
77 if has_fetch() then return true end
78 if has_bbwget() then return true end
81 elseif type == "has_forceip" then
82 if has_wgetssl() then return true end
83 if has_curl() then return true end
84 if has_fetch() then return true end -- only really needed for transfer
87 elseif type == "has_bindnet" then
88 if has_curl() then return true end
89 if has_wgetssl() then return true end
93 elseif ( type == "has_dnsserver" ) or ( type == "has_bindhost" ) or ( type == "has_hostip" ) or ( type == "has_nslookup" ) then
94 local function has_bindhost()
95 if (SYS.call( [[which host >/dev/null 2>&1]] ) == 0) then return true end
96 if (SYS.call( [[which khost >/dev/null 2>&1]] ) == 0) then return true end
97 if (SYS.call( [[which drill >/dev/null 2>&1]] ) == 0) then return true end
101 local function has_hostip()
102 return (SYS.call( [[which hostip >/dev/null 2>&1]] ) == 0)
105 local function has_nslookup()
106 return (SYS.call( [[$(which nslookup) localhost 2>&1 | grep -qF "(null)"]] ) ~= 0)
109 if type == "has_bindhost" then
110 return has_bindhost()
111 elseif type == "has_hostip" then
113 elseif type == "has_nslookup" then
114 return has_nslookup()
115 elseif type == "has_dnsserver" then
116 if has_bindhost() then return true end
117 if has_hostip() then return true end
118 if has_nslookup() then return true end
122 elseif type == "has_ipv6" then
123 return (NXFS.access("/proc/net/ipv6_route") and NXFS.access("/usr/sbin/ip6tables"))
125 elseif type == "has_cacerts" then
126 --old _check_certs() local function
127 local _, v = NXFS.glob("/etc/ssl/certs/*.crt")
128 if ( v == 0 ) then _, v = NXFS.glob("/etc/ssl/certs/*.pem") end
136 -- function to calculate seconds from given interval and unit
137 function calc_seconds(interval, unit)
138 if not tonumber(interval) then
140 elseif unit == "days" then
141 return (tonumber(interval) * 86400) -- 60 sec * 60 min * 24 h
142 elseif unit == "hours" then
143 return (tonumber(interval) * 3600) -- 60 sec * 60 min
144 elseif unit == "minutes" then
145 return (tonumber(interval) * 60) -- 60 sec
146 elseif unit == "seconds" then
147 return tonumber(interval)
153 -- convert epoch date to given format
154 function epoch2date(epoch, format)
155 if not format or #format < 2 then
156 local uci = UCI.cursor()
157 format = uci:get("ddns", "global", "ddns_dateformat") or "%F %R"
160 format = format:gsub("%%n", "<br />") -- replace newline
161 format = format:gsub("%%t", " ") -- replace tab
162 return os.date(format, epoch)
165 -- read lastupdate from [section].update file
166 function get_lastupd(section)
167 local uci = UCI.cursor()
168 local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
169 local etime = tonumber(NXFS.readfile("%s/%s.update" % { rdir, section } ) or 0 )
174 -- read registered IP from [section].ip file
175 function get_regip(section, chk_sec)
176 local uci = UCI.cursor()
177 local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
179 if NXFS.access("%s/%s.ip" % { rdir, section }) then
180 local ftime = NXFS.stat("%s/%s.ip" % { rdir, section }, "ctime") or 0
181 local otime = os.time()
182 -- give ddns-scripts time (9 sec) to update file
183 if otime < (ftime + chk_sec + 9) then
184 ip = NXFS.readfile("%s/%s.ip" % { rdir, section })
191 -- read PID from run file and verify if still running
192 function get_pid(section)
193 local uci = UCI.cursor()
194 local rdir = uci:get("ddns", "global", "ddns_rundir") or "/var/run/ddns"
195 local pid = tonumber(NXFS.readfile("%s/%s.pid" % { rdir, section } ) or 0 )
196 if pid > 0 and not NX.kill(pid, 0) then
203 -- replacement of build-in read of UCI option
204 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
205 -- needed to read from other option then current value definition
206 function read_value(self, section, option)
208 if self.tag_error[section] then
209 value = self:formvalue(section)
211 value = self.map:get(section, option)
216 elseif not self.cast or self.cast == type(value) then
218 elseif self.cast == "string" then
219 if type(value) == "table" then
222 elseif self.cast == "table" then
227 -- replacement of build-in parse of "Value"
228 -- modified AbstractValue.parse(self, section, novld) from cbi.lua
229 -- validate is called if rmempty/optional true or false
230 -- before write check if forcewrite, value eq default, and more
231 function value_parse(self, section, novld)
232 local fvalue = self:formvalue(section)
233 local fexist = ( fvalue and (#fvalue > 0) ) -- not "nil" and "not empty"
234 local cvalue = self:cfgvalue(section)
235 local rm_opt = ( self.rmempty or self.optional )
236 local eq_cfg -- flag: equal cfgvalue
238 -- If favlue and cvalue are both tables and have the same content
239 -- make them identical
240 if type(fvalue) == "table" and type(cvalue) == "table" then
241 eq_cfg = (#fvalue == #cvalue)
244 if cvalue[i] ~= fvalue[i] then
254 -- removed parameter "section" from function call because used/accepted nowhere
255 -- also removed call to function "transfer"
256 local vvalue, errtxt = self:validate(fvalue)
258 -- error handling; validate return "nil"
260 if novld then -- and "novld" set
261 return -- then exit without raising an error
264 if fexist then -- and there is a formvalue
265 self:add_error(section, "invalid", errtxt or self.title .. ": invalid")
266 return -- so data are invalid
267 elseif not rm_opt then -- and empty formvalue but NOT (rmempty or optional) set
268 self:add_error(section, "missing", errtxt or self.title .. ": missing")
269 return -- so data is missing
271 self:add_error(section, "invalid", errtxt)
274 -- error ("\n option: " .. self.option ..
275 -- "\n fvalue: " .. tostring(fvalue) ..
276 -- "\n fexist: " .. tostring(fexist) ..
277 -- "\n cvalue: " .. tostring(cvalue) ..
278 -- "\n vvalue: " .. tostring(vvalue) ..
279 -- "\n vexist: " .. tostring(vexist) ..
280 -- "\n rm_opt: " .. tostring(rm_opt) ..
281 -- "\n eq_cfg: " .. tostring(eq_cfg) ..
282 -- "\n eq_def: " .. tostring(eq_def) ..
283 -- "\n novld : " .. tostring(novld) ..
284 -- "\n errtxt: " .. tostring(errtxt) )
287 -- lets continue with value returned from validate
288 eq_cfg = ( vvalue == cvalue ) -- update equal_config flag
289 local vexist = ( vvalue and (#vvalue > 0) ) and true or false -- not "nil" and "not empty"
290 local eq_def = ( vvalue == self.default ) -- equal_default flag
292 -- (rmempty or optional) and (no data or equal_default)
293 if rm_opt and (not vexist or eq_def) then
294 if self:remove(section) then -- remove data from UCI
295 self.section.changed = true -- and push events
300 -- not forcewrite and no changes, so nothing to write
301 if not self.forcewrite and eq_cfg then
305 -- we should have a valid value here
306 assert (vvalue, "\n option: " .. self.option ..
307 "\n fvalue: " .. tostring(fvalue) ..
308 "\n fexist: " .. tostring(fexist) ..
309 "\n cvalue: " .. tostring(cvalue) ..
310 "\n vvalue: " .. tostring(vvalue) ..
311 "\n vexist: " .. tostring(vexist) ..
312 "\n rm_opt: " .. tostring(rm_opt) ..
313 "\n eq_cfg: " .. tostring(eq_cfg) ..
314 "\n eq_def: " .. tostring(eq_def) ..
315 "\n errtxt: " .. tostring(errtxt) )
317 -- write data to UCI; raise event only on changes
318 if self:write(section, vvalue) and not eq_cfg then
319 self.section.changed = true
323 -----------------------------------------------------------------------------
324 -- copied from https://svn.nmap.org/nmap/nselib/url.lua
325 -- @author Diego Nehab
326 -- @author Eddie Bell <ejlbell@gmail.com>
328 URI parsing, composition and relative URL resolution
331 RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
332 parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
335 -- Parses a URL and returns a table with all its parts according to RFC 2396.
337 -- The following grammar describes the names given to the URL parts.
339 -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
340 -- <authority> ::= <userinfo>@<host>:<port>
341 -- <userinfo> ::= <user>[:<password>]
342 -- <path> :: = {<segment>/}<segment>
345 -- The leading <code>/</code> in <code>/<path></code> is considered part of
346 -- <code><path></code>.
347 -- @param url URL of request.
348 -- @param default Table with default values for each field.
349 -- @return A table with the following fields, where RFC naming conventions have
351 -- <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
352 -- <code>user</code>, <code>password</code>, <code>host</code>,
353 -- <code>port</code>, <code>path</code>, <code>params</code>,
354 -- <code>query</code>, and <code>fragment</code>.
355 -----------------------------------------------------------------------------
356 function parse_url(url) --, default)
357 -- initialize default parameters
359 -- for i,v in base.pairs(default or parsed) do
364 -- url = string.gsub(url, "%s", "")
366 url = string.gsub(url, "#(.*)$",
371 -- get scheme. Lower-case according to RFC 3986 section 3.1.
372 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
374 parsed.scheme = string.lower(s);
378 url = string.gsub(url, "^//([^/]*)",
383 -- get query stringing
384 url = string.gsub(url, "%?(.*)",
390 url = string.gsub(url, "%;(.*)",
395 -- path is whatever was left
398 local authority = parsed.authority
399 if not authority then
402 authority = string.gsub(authority,"^([^@]*)@",
407 authority = string.gsub(authority, ":([0-9]*)$",
414 if authority ~= "" then
415 parsed.host = authority
418 local userinfo = parsed.userinfo
422 userinfo = string.gsub(userinfo, ":([^:]*)$",
427 parsed.user = userinfo