luci-app-ddns: Update to support ddns-scripts 2.1.0-3
[project/luci.git] / applications / luci-ddns / luasrc / controller / ddns.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6 Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12         http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id$
15 ]]--
16
17 module("luci.controller.ddns", package.seeall)
18
19 local NX   = require "nixio"
20 local NXFS = require "nixio.fs"
21 local DISP = require "luci.dispatcher"
22 local HTTP = require "luci.http"
23 local UCI  = require "luci.model.uci"
24 local SYS  = require "luci.sys"
25 local DDNS = require "luci.tools.ddns"          -- ddns multiused functions
26 local UTIL = require "luci.util"
27
28 local luci_ddns_version = "2.1.0-3"     -- luci-app-ddns / openwrt Makefile compatible version
29 local ddns_scripts_min  = "2.1.0-2"     -- minimum version of ddns-scripts required
30
31 function index()
32         -- no services_ipv6 file or no dynamic_dns_lucihelper.sh
33         -- do NOT start
34         if not nixio.fs.access("/usr/lib/ddns/services_ipv6") 
35         or not nixio.fs.access("/usr/lib/ddns/dynamic_dns_lucihelper.sh") then
36                 return
37         end
38         -- no config create an empty one
39         if not nixio.fs.access("/etc/config/ddns") then
40                 nixio.fs.writefile("/etc/config/ddns", "")
41         end
42
43         entry( {"admin", "services", "ddns"}, cbi("ddns/overview"), _("Dynamic DNS"), 59)
44         entry( {"admin", "services", "ddns", "detail"}, cbi("ddns/detail"), nil ).leaf = true
45         entry( {"admin", "services", "ddns", "hints"}, cbi("ddns/hints",
46                 {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), nil ).leaf = true
47         entry( {"admin", "services", "ddns", "logview"}, call("logread") ).leaf = true
48         entry( {"admin", "services", "ddns", "startstop"}, call("startstop") ).leaf = true
49         entry( {"admin", "services", "ddns", "status"}, call("status") ).leaf = true
50 end
51
52 -- function to read all sections status and return data array
53 local function _get_status()
54         local uci        = UCI.cursor()
55         local service    = SYS.init.enabled("ddns") and 1 or 0
56         local url_start  = DISP.build_url("admin", "system", "startup")
57         local luci_build = DDNS.ipkg_version("luci-app-ddns").version
58         local ddns_act   = DDNS.ipkg_version("ddns-scripts").version
59         local data       = {}   -- Array to transfer data to javascript
60
61         data[#data+1]   = {
62                 enabled    = service,           -- service enabled
63                 url_up     = url_start,         -- link to enable DDS (System-Startup)
64                 luci_ver   = luci_ddns_version, -- luci-app-ddns / openwrt Makefile compatible version
65                 luci_build = luci_build,        -- installed luci build
66                 script_min = ddns_scripts_min,  -- minimum version of ddns-scripts needed
67                 script_ver = ddns_act           -- installed ddns-scripts
68         }
69
70         uci:foreach("ddns", "service", function (s)
71
72                 -- Get section we are looking at
73                 -- and enabled state
74                 local section   = s[".name"]
75                 local enabled   = tonumber(s["enabled"]) or 0
76                 local datelast  = "_empty_"     -- formated date of last update
77                 local datenext  = "_empty_"     -- formated date of next update
78
79                 -- get force seconds
80                 local force_seconds = DDNS.calc_seconds(
81                                 tonumber(s["force_interval"]) or 72 ,
82                                 s["force_unit"] or "hours" )
83                 -- get/validate pid and last update
84                 local pid      = DDNS.get_pid(section)
85                 local uptime   = SYS.uptime()
86                 local lasttime = DDNS.get_lastupd(section)
87                 if lasttime > uptime then       -- /var might not be linked to /tmp
88                         lasttime = 0            -- and/or not cleared on reboot
89                 end
90
91                 -- no last update happen
92                 if lasttime == 0 then
93                         datelast = "_never_"
94
95                 -- we read last update
96                 else
97                         -- calc last update
98                         --             sys.epoch - sys uptime   + lastupdate(uptime)
99                         local epoch = os.time() - uptime + lasttime
100                         -- use linux date to convert epoch
101                         datelast = DDNS.epoch2date(epoch)
102                         -- calc and fill next update
103                         datenext = DDNS.epoch2date(epoch + force_seconds)
104                 end
105
106                 -- process running but update needs to happen
107                 -- problems it force_seconds > uptime
108                 force_seconds = (force_seconds > uptime) and uptime or force_seconds
109                 if pid > 0 and ( lasttime + force_seconds - uptime ) <= 0 then
110                         datenext = "_verify_"
111
112                 -- run once
113                 elseif force_seconds == 0 then
114                         datenext = "_runonce_"
115
116                 -- no process running and NOT enabled
117                 elseif pid == 0 and enabled == 0 then
118                         datenext  = "_disabled_"
119
120                 -- no process running and NOT
121                 elseif pid == 0 and enabled ~= 0 then
122                         datenext = "_stopped_"
123                 end
124
125                 -- get/set monitored interface and IP version
126                 local iface     = s["interface"] or "_nonet_"
127                 local use_ipv6  = tonumber(s["use_ipv6"]) or 0
128                 if iface ~= "_nonet_" then
129                         local ipv = (use_ipv6 == 1) and "IPv6" or "IPv4"
130                         iface = ipv .. " / " .. iface
131                 end
132
133                 -- try to get registered IP
134                 local domain    = s["domain"] or "_nodomain_"
135                 local dnsserver = s["dns_server"] or ""
136                 local force_ipversion = tonumber(s["force_ipversion"] or 0)
137                 local force_dnstcp = tonumber(s["force_dnstcp"] or 0)
138                 local command = [[/usr/lib/ddns/dynamic_dns_lucihelper.sh]]
139                 command = command .. [[ get_registered_ip ]] .. domain .. [[ ]] .. use_ipv6 ..
140                         [[ ]] .. force_ipversion .. [[ ]] .. force_dnstcp .. [[ ]] .. dnsserver
141                 local reg_ip = SYS.exec(command)
142                 if reg_ip == "" then
143                         reg_ip = "_nodata_"
144                 end
145
146                 -- fill transfer array
147                 data[#data+1]   = {
148                         section  = section,
149                         enabled  = enabled,
150                         iface    = iface,
151                         domain   = domain,
152                         reg_ip   = reg_ip,
153                         pid      = pid,
154                         datelast = datelast,
155                         datenext = datenext
156                 }
157         end)
158
159         uci:unload("ddns")
160         return data
161 end
162
163 -- called by XHR.get from detail_logview.htm
164 function logread(section)
165         -- read application settings
166         local uci         = UCI.cursor()
167         local log_dir     = uci:get("ddns", "global", "log_dir") or "/var/log/ddns"
168         local lfile=log_dir .. "/" .. section .. ".log"
169
170         local ldata=NXFS.readfile(lfile)
171         if not ldata or #ldata == 0 then
172                 ldata="_nodata_"
173         end
174         uci:unload("ddns")
175         HTTP.write(ldata)
176 end
177
178 -- called by XHR.get from overview_status.htm
179 function startstop(section, enabled)
180         local uci  = UCI.cursor()
181         local data = {}         -- Array to transfer data to javascript
182
183         -- if process running we want to stop and return
184         local pid = DDNS.get_pid(section)
185         if pid > 0 then
186                 local tmp = NX.kill(pid, 15)    -- terminate
187                 NX.nanosleep(2) -- 2 second "show time"
188                 -- status changed so return full status
189                 data = _get_status()
190                 HTTP.prepare_content("application/json")
191                 HTTP.write_json(data)
192                 return
193         end
194
195         -- read uncommited changes
196         -- we don't save and commit data from other section or other options
197         -- only enabled will be done
198         local exec        = true
199         local changed     = uci:changes("ddns")
200         for k_config, v_section in pairs(changed) do
201                 -- security check because uci.changes only gets our config
202                 if k_config ~= "ddns" then
203                         exec = false
204                         break
205                 end
206                 for k_section, v_option in pairs(v_section) do
207                         -- check if only section of button was changed
208                         if k_section ~= section then
209                                 exec = false
210                                 break
211                         end
212                         for k_option, v_value in pairs(v_option) do
213                                 -- check if only enabled was changed
214                                 if k_option ~= "enabled" then
215                                         exec = false
216                                         break
217                                 end
218                         end
219                 end
220         end
221
222         -- we can not execute because other
223         -- uncommited changes pending, so exit here
224         if not exec then
225                 HTTP.write("_uncommited_")
226                 return
227         end
228
229         -- save enable state
230         uci:set("ddns", section, "enabled", ( (enabled == "true") and "1" or "0") )
231         uci:save("ddns")
232         uci:commit("ddns")
233         uci:unload("ddns")
234
235         -- start dynamic_dns_updater.sh script
236         os.execute ([[/usr/lib/ddns/dynamic_dns_updater.sh %s 0 > /dev/null 2>&1 &]] % section)
237         NX.nanosleep(3) -- 3 seconds "show time"
238
239         -- status changed so return full status
240         data = _get_status()
241         HTTP.prepare_content("application/json")
242         HTTP.write_json(data)
243 end
244
245 -- called by XHR.poll from overview_status.htm
246 function status()
247         local data = _get_status()
248         HTTP.prepare_content("application/json")
249         HTTP.write_json(data)
250 end
251
252 -- check if installed ddns-scripts version < required version
253 function update_needed()
254         local sver = DDNS.ipkg_version("ddns-scripts")
255         local rver = UTIL.split(ddns_scripts_min, "[%.%-]", nil, true)
256         return (sver.major < (tonumber(rver[1]) or 0))
257             or (sver.minor < (tonumber(rver[2]) or 0))
258             or (sver.patch < (tonumber(rver[3]) or 0))
259             or (sver.build < (tonumber(rver[4]) or 0))
260 end
261