4172d84ae14c365a255f839ae1ea4321d9a00800
[project/luci.git] / applications / luci-ddns / luasrc / tools / ddns.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 shared module for luci-app-ddns-v2
5 Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
6
7 function parse_url copied from https://svn.nmap.org/nmap/nselib/url.lua
8 Parses a URL and returns a table with all its parts according to RFC 2396.
9 @author Diego Nehab     @author Eddie Bell <ejlbell@gmail.com>
10
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14
15         http://www.apache.org/licenses/LICENSE-2.0
16
17 ]]--
18
19 module("luci.tools.ddns", package.seeall)
20
21 require "luci.sys"
22 require "nixio.fs"
23
24 function check_ipv6()
25         return nixio.fs.access("/proc/net/ipv6_route") 
26            and nixio.fs.access("/usr/sbin/ip6tables")
27 end
28
29 function check_ssl()
30         if (luci.sys.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
31                 return true
32         else
33                 return nixio.fs.access("/usr/bin/curl")
34         end
35 end
36
37 function check_proxy()
38         -- we prefere GNU Wget for communication
39         if (luci.sys.call([[ grep -iq "\+ssl" /usr/bin/wget 2>/dev/null ]]) == 0) then
40                 return true
41
42         -- if not installed cURL must support proxy
43         elseif nixio.fs.access("/usr/bin/curl") then
44                 return (luci.sys.call([[ grep -iq all_proxy /usr/lib/libcurl.so* 2>/dev/null ]]) == 0)
45
46         -- only BusyBox Wget is installed
47         else
48                 return nixio.fs.access("/usr/bin/wget")
49         end
50 end
51
52 function check_bind_host()
53         return nixio.fs.access("/usr/bin/host")
54 end
55
56 -- function to calculate seconds from given interval and unit
57 function calc_seconds(interval, unit)
58         if not tonumber(interval) then
59                 return nil
60         elseif unit == "days" then
61                 return (tonumber(interval) * 86400)     -- 60 sec * 60 min * 24 h
62         elseif unit == "hours" then
63                 return (tonumber(interval) * 3600)      -- 60 sec * 60 min
64         elseif unit == "minutes" then
65                 return (tonumber(interval) * 60)        -- 60 sec
66         elseif unit == "seconds" then
67                 return tonumber(interval)
68         else
69                 return nil
70         end
71 end
72
73 -- read PID from run file and verify if still running
74 function get_pid(section, run_dir)
75         local pid = tonumber(nixio.fs.readfile("%s/%s.pid" % { run_dir, section } ) or 0 )
76         if pid > 0 and not luci.sys.process.signal(pid, 0) then
77                 pid = 0
78         end
79         return pid
80 end
81
82 -- replacement of build-in read of UCI option
83 -- modified AbstractValue.cfgvalue(self, section) from cbi.lua
84 -- needed to read from other option then current value definition
85 function read_value(self, section, option)
86         local value
87         if self.tag_error[section] then
88                 value = self:formvalue(section)
89         else
90                 value = self.map:get(section, option)
91         end
92
93         if not value then
94                 return nil
95         elseif not self.cast or self.cast == type(value) then
96                 return value
97         elseif self.cast == "string" then
98                 if type(value) == "table" then
99                         return value[1]
100                 end
101         elseif self.cast == "table" then
102                 return { value }
103         end
104 end
105
106 -----------------------------------------------------------------------------
107 -- copied from https://svn.nmap.org/nmap/nselib/url.lua
108 -- @author Diego Nehab
109 -- @author Eddie Bell <ejlbell@gmail.com>
110 --[[
111     URI parsing, composition and relative URL resolution
112     LuaSocket toolkit.
113     Author: Diego Nehab
114     RCS ID: $Id: url.lua,v 1.37 2005/11/22 08:33:29 diego Exp $
115     parse_query and build_query added For nmap (Eddie Bell <ejlbell@gmail.com>)
116 ]]--
117 ---
118 -- Parses a URL and returns a table with all its parts according to RFC 2396.
119 --
120 -- The following grammar describes the names given to the URL parts.
121 -- <code>
122 -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
123 -- <authority> ::= <userinfo>@<host>:<port>
124 -- <userinfo> ::= <user>[:<password>]
125 -- <path> :: = {<segment>/}<segment>
126 -- </code>
127 --
128 -- The leading <code>/</code> in <code>/<path></code> is considered part of
129 -- <code><path></code>.
130 -- @param url URL of request.
131 -- @param default Table with default values for each field.
132 -- @return A table with the following fields, where RFC naming conventions have
133 --   been preserved:
134 --     <code>scheme</code>, <code>authority</code>, <code>userinfo</code>,
135 --     <code>user</code>, <code>password</code>, <code>host</code>,
136 --     <code>port</code>, <code>path</code>, <code>params</code>,
137 --     <code>query</code>, and <code>fragment</code>.
138 -----------------------------------------------------------------------------
139 function parse_url(url) --, default)
140         -- initialize default parameters
141         local parsed = {}
142 --      for i,v in base.pairs(default or parsed) do 
143 --              parsed[i] = v
144 --      end
145
146         -- remove whitespace
147 --      url = string.gsub(url, "%s", "")
148         -- get fragment
149         url = string.gsub(url, "#(.*)$", 
150                 function(f)
151                         parsed.fragment = f
152                         return ""
153                 end)
154         -- get scheme. Lower-case according to RFC 3986 section 3.1.
155         url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
156                 function(s)
157                         parsed.scheme = string.lower(s);
158                         return ""
159                 end)
160         -- get authority
161         url = string.gsub(url, "^//([^/]*)",
162                 function(n)
163                         parsed.authority = n
164                         return ""
165                 end)
166         -- get query stringing
167         url = string.gsub(url, "%?(.*)",
168                 function(q)
169                         parsed.query = q
170                         return ""
171                 end)
172         -- get params
173         url = string.gsub(url, "%;(.*)",
174                 function(p)
175                         parsed.params = p
176                         return ""
177                 end)
178         -- path is whatever was left
179         parsed.path = url
180
181         local authority = parsed.authority
182         if not authority then 
183                 return parsed
184         end
185         authority = string.gsub(authority,"^([^@]*)@",
186                 function(u)
187                         parsed.userinfo = u;
188                         return ""
189                 end)
190         authority = string.gsub(authority, ":([0-9]*)$",
191                 function(p)
192                         if p ~= "" then
193                                 parsed.port = p
194                         end;
195                         return ""
196                 end)
197         if authority ~= "" then
198                 parsed.host = authority
199         end
200
201         local userinfo = parsed.userinfo
202         if not userinfo then
203                 return parsed
204         end
205         userinfo = string.gsub(userinfo, ":([^:]*)$",
206                 function(p)
207                         parsed.password = p;
208                         return ""
209                 end)
210         parsed.user = userinfo
211         return parsed
212 end