luci: Add luci-app-unbound to base
[librecmc/librecmc.git] / package / luci / applications / luci-app-unbound / luasrc / controller / unbound.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Copyright 2017 Eric Luehrsen <ericluehrsen@gmail.com>
4 -- Licensed to the public under the Apache License 2.0.
5
6 module("luci.controller.unbound", package.seeall)
7
8
9 function index()
10     local fs = require "nixio.fs"
11     local ucl = luci.model.uci.cursor()
12     local valman = ucl:get_first("unbound", "unbound", "manual_conf")
13
14
15     if not fs.access("/etc/config/unbound") then
16         return
17     end
18
19
20     -- Expanded View
21     entry({"admin", "services", "unbound"},
22         firstchild(), _("Recursive DNS")).dependent = false
23
24     -- UCI Tab(s)
25     entry({"admin", "services", "unbound", "configure"},
26         cbi("unbound/configure"), _("Unbound"), 10)
27
28
29     if (valman == "0") then
30         entry({"admin", "services", "unbound", "zones"},
31             arcombine(cbi("unbound/zones"), cbi("unbound/zone-details")),
32             _("Zones"), 15).leaf = true
33     end
34
35
36     -- Status Tab(s)
37     entry({"admin", "services", "unbound", "status"},
38         firstchild(), _("Status"), 20)
39
40     entry({"admin", "services", "unbound", "status", "syslog"},
41         call("QuerySysLog"), _("Log"), 50).leaf = true
42
43
44     if fs.access("/usr/sbin/unbound-control") then
45         -- Require unbound-control to execute
46         entry({"admin", "services", "unbound", "status", "statistics"},
47             call("QueryStatistics"), _("Statistics"), 10).leaf = true
48
49         entry({"admin", "services", "unbound", "status", "localdata"},
50             call("QueryLocalData"), _("Local Data"), 20).leaf = true
51
52         entry({"admin", "services", "unbound", "status", "localzone"},
53             call("QueryLocalZone"), _("Local Zones"), 30).leaf = true
54
55         entry({"admin", "services", "unbound", "status", "dumpcache"},
56             call("QueryDumpCache"), _("DNS Cache"), 40).leaf = true
57     else
58         entry({"admin", "services", "unbound", "status", "statistics"},
59             call("ShowEmpty"), _("Statistics"), 10).leaf = true
60     end
61
62
63     -- Raw File Tab(s)
64     entry({"admin", "services", "unbound", "files"},
65         firstchild(), _("Files"), 30)
66
67
68     if (valman == "0") then
69         entry({"admin", "services", "unbound", "files", "uci"},
70             form("unbound/uciedit"), _("Edit: UCI"), 5).leaf = true
71
72         entry({"admin", "services", "unbound", "files", "base"},
73             call("ShowUnboundConf"), _("Show: Unbound"), 10).leaf = true
74
75     else
76         entry({"admin", "services", "unbound", "files", "base"},
77             form("unbound/manual"), _("Edit: Unbound"), 10).leaf = true
78     end
79
80
81     entry({"admin", "services", "unbound", "files", "server"},
82         form("unbound/server"), _("Edit: Server"), 20).leaf = true
83
84     entry({"admin", "services", "unbound", "files", "extended"},
85         form("unbound/extended"), _("Edit: Extended"), 30).leaf = true
86
87
88     if fs.access("/var/lib/unbound/dhcp.conf") then
89         entry({"admin", "services", "unbound", "files", "dhcp"},
90             call("ShowDHCPConf"), _("Show: DHCP"), 40).leaf = true
91     end
92
93
94     if fs.access("/var/lib/unbound/adb_list.overall") then
95         entry({"admin", "services", "unbound", "files", "adblock"},
96             call("ShowAdblock"), _("Show: Adblock"), 50).leaf = true
97     end
98 end
99
100
101 function ShowEmpty()
102     local lclhead = "Unbound Control"
103     local lcldesc = luci.i18n.translate(
104         "This could display more statistics with the unbound-control package.")
105
106     luci.template.render("unbound/show-empty",
107         {heading = lclhead, description = lcldesc})
108 end
109
110
111 function QuerySysLog()
112     local lcldata = luci.util.exec("logread -e 'unbound'")
113     local lcldesc = luci.i18n.translate(
114         "This shows syslog filtered for events involving Unbound.")
115
116     luci.template.render("unbound/show-textbox",
117         {heading = "", description = lcldesc, content = lcldata})
118 end
119
120
121 function QueryStatistics()
122     local lcldata = luci.util.exec(
123         "unbound-control -c /var/lib/unbound/unbound.conf stats_noreset")
124
125     local lcldesc = luci.i18n.translate(
126         "This shows Unbound self reported performance statistics.")
127
128     luci.template.render("unbound/show-textbox",
129         {heading = "", description = lcldesc, content = lcldata})
130 end
131
132
133 function QueryLocalData()
134     local lcldata = luci.util.exec(
135         "unbound-control -c /var/lib/unbound/unbound.conf list_local_data")
136
137     local lcldesc = luci.i18n.translate(
138         "This shows Unbound 'local-data:' entries from default, .conf, or control.")
139
140     luci.template.render("unbound/show-textbox",
141         {heading = "", description = lcldesc, content = lcldata})
142 end
143
144
145 function QueryLocalZone()
146     local lcldata = luci.util.exec(
147         "unbound-control -c /var/lib/unbound/unbound.conf list_local_zones")
148
149     local lcldesc = luci.i18n.translate(
150         "This shows Unbound 'local-zone:' entries from default, .conf, or control.")
151
152     luci.template.render("unbound/show-textbox",
153         {heading = "", description = lcldesc, content = lcldata})
154 end
155
156
157 function QueryDumpCache()
158     local tp = require "luci.template"
159     local tr = require "luci.i18n"
160     local lcldesc 
161     local lcldata = luci.util.exec(
162         "unbound-control -c /var/lib/unbound/unbound.conf dump_cache")
163
164
165     if #lcldata > 262144 then
166         lcldesc = tr.translate(
167             "Unbound cache is too large to display in LuCI.")
168
169         tp.render("unbound/show-empty",
170             {heading = "", description = lcldesc})
171
172     else
173         lcldesc = tr.translate(
174             "This shows 'ubound-control dump_cache' for auditing records including DNSSEC.")
175
176         tp.render("unbound/show-textbox",
177             {heading = "", description = lcldesc, content = lcldata})
178     end
179
180 end
181
182
183 function ShowUnboundConf()
184     local unboundfile = "/var/lib/unbound/unbound.conf"
185     local lcldata = nixio.fs.readfile(unboundfile)
186     local lcldesc = luci.i18n.translate(
187         "This shows '" .. unboundfile .. "' generated from UCI configuration.")
188
189     luci.template.render("unbound/show-textbox",
190         {heading = "", description = lcldesc, content = lcldata})
191 end
192
193
194 function ShowDHCPConf()
195     local dhcpfile = "/var/lib/unbound/dhcp.conf"
196     local lcldata = nixio.fs.readfile(dhcpfile)
197     local lcldesc = luci.i18n.translate(
198         "This shows '" .. dhcpfile .. "' list of hosts from DHCP hook scripts.")
199
200     luci.template.render("unbound/show-textbox",
201         {heading = "", description = lcldesc, content = lcldata})
202 end
203
204
205 function ShowAdblock()
206     local fs = require "nixio.fs"
207     local tp = require "luci.template"
208     local tr = require "luci.i18n"
209     local adblockfile = "/var/lib/unbound/adb_list.overall"
210     local lcldata, lcldesc
211
212
213     if fs.stat(adblockfile).size > 262144 then
214         lcldesc = tr.translate(
215             "Adblock domain list is too large to display in LuCI.")
216
217         tp.render("unbound/show-empty",
218             {heading = "", description = lcldesc})
219
220     else
221         lcldata = fs.readfile(adblockfile)
222         lcldesc = tr.translate(
223             "This shows '" .. adblockfile .. "' list of adblock domains." )
224
225         tp.render("unbound/show-textbox",
226             {heading = "", description = lcldesc, content = lcldata})
227     end
228 end
229