Merge pull request #2043 from Ansuel/materialfix
[project/luci.git] / applications / luci-app-unbound / luasrc / model / cbi / unbound / zones.lua
1 -- Copyright 2017 Eric Luehrsen <ericluehrsen@gmail.com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local m5, s5
5 local ztype, zones, servers, fallback, enabled
6
7 local fs = require "nixio.fs"
8 local ut = require "luci.util"
9 local sy = require "luci.sys"
10 local resolvfile = "/tmp/resolv.conf.auto"
11
12 m5 = Map("unbound")
13 s5 = m5:section(TypedSection, "zone", "Zones",
14 translatef("This shows extended zones and more details can be "
15 .. "changed in Files tab and <a href=\"%s\">Edit:UCI</a> subtab.",
16 "/cgi-bin/luci/admin/services/unbound/files" ))
17
18 s5.addremove = false
19 s5.anonymous = true
20 s5.sortable = true
21 s5.template = "cbi/tblsection"
22
23 ztype = s5:option(DummyValue, "DummyType", translate("Type"))
24 ztype.rawhtml = true
25
26 zones = s5:option(DummyValue, "DummyZones", translate("Zones"))
27 zones.rawhtml = true
28
29 servers = s5:option(DummyValue, "DummyServers", translate("Servers"))
30 servers.rawhtml = true
31
32 fallback = s5:option(Flag, "fallback", translate("Fallback"))
33 fallback.rmempty = false
34
35 enabled = s5:option(Flag, "enabled", translate("Enable"))
36 enabled.rmempty = false
37
38
39 function ztype.cfgvalue(self, s)
40 -- Format a meaninful tile for the Zone Type column
41 local itxt = self.map:get(s, "zone_type")
42 local itls = self.map:get(s, "tls_upstream")
43
44
45 if itxt and itxt:match("forward") then
46 if itls and (itls == "1") then
47 return translate("Forward TLS")
48
49 else
50 return translate("Forward")
51 end
52
53 elseif itxt and itxt:match("stub") then
54 return translate("Recurse")
55
56 elseif itxt and itxt:match("auth") then
57 return translate("AXFR")
58
59 else
60 return translate("Error")
61 end
62 end
63
64
65 function zones.cfgvalue(self, s)
66 -- Format a meaninful sentence for the Zones viewed column
67 local xtxt, otxt
68 local itxt = self.map:get(s, "zone_name")
69 local itype = self.map:get(s, "zone_type")
70
71
72 for xtxt in ut.imatch(itxt) do
73 if (xtxt == ".") then
74 -- zone_name lists
75 xtxt = translate("(root)")
76 end
77
78
79 if otxt and (#otxt > 0) then
80 otxt = otxt .. ", <var>%s</var>" % xtxt
81
82 else
83 otxt = "<var>%s</var>" % xtxt
84 end
85 end
86
87
88 if itype and itype:match("forward") then
89 -- from zone_type create a readable hint for the action
90 otxt = translate("accept upstream results for ") .. otxt
91
92 elseif itype and itype:match("stub") then
93 otxt = translate("select recursion for ") .. otxt
94
95 elseif itype and itype:match("auth") then
96 otxt = translate("prefetch zone files for ") .. otxt
97
98 else
99 otxt = translate("unknown action for ") .. otxt
100 end
101
102
103 if otxt and (#otxt > 0) then
104 return otxt
105
106 else
107 return "(empty)"
108 end
109 end
110
111
112 function servers.cfgvalue(self, s)
113 -- Format a meaninful sentence for the Servers (and URL) column
114 local xtxt, otxt, rtxt, found
115 local itxt = self.map:get(s, "server")
116 local iurl = self.map:get(s, "url_dir")
117 local itype = self.map:get(s, "zone_type")
118 local itls = self.map:get(s, "tls_upstream")
119 local iidx = self.map:get(s, "tls_index")
120 local irslv = self.map:get(s, "resolv_conf")
121
122
123 for xtxt in ut.imatch(itxt) do
124 if otxt and (#otxt > 0) then
125 -- bundle and make pretty the server list
126 otxt = otxt .. ", <var>%s</var>" % xtxt
127
128 else
129 otxt = "<var>%s</var>" % xtxt
130 end
131 end
132
133
134 if otxt and (#otxt > 0)
135 and itls and (itls == "1")
136 and iidx and (#iidx > 0) then
137 -- show TLS certificate name index if provided
138 otxt = translatef("use nameservers by <var>%s</var> at ", iidx) .. otxt
139
140 elseif otxt and (#otxt > 0) then
141 otxt = translate("use nameservers ") .. otxt
142 end
143
144
145 if iurl and (#iurl > 0) and itype and itype:match("auth") then
146 if otxt and (#otxt > 0) then
147 -- include optional URL filed for auth-zone: type
148 otxt = otxt .. translatef(", and try <var>%s</var>", iurl)
149
150 else
151 otxt = translatef("download from <var>%s</var>", iurl)
152 end
153 end
154
155
156 if irslv and (irslv == "1") and itype and itype:match("forward") then
157 for xtxt in ut.imatch(fs.readfile(resolvfile)) do
158 if xtxt:match("nameserver") then
159 found = true
160
161 elseif (found == true) then
162 if rtxt and (#rtxt > 0) then
163 -- fetch name servers from resolv.conf
164 rtxt = rtxt .. ", <var>%s</var>" % xtxt
165
166 else
167 rtxt = "<var>%s</var>" % xtxt
168 end
169
170
171 found = false
172 end
173 end
174
175
176 if otxt and (#otxt > 0) and rtxt and (#rtxt > 0) then
177 otxt = otxt
178 .. translatef(", and <var>%s</var> entries ", resolvfile) .. rtxt
179
180 elseif rtxt and (#rtxt > 0) then
181 otxt = translatef("use <var>%s</var> nameservers ", resolvfile) .. rtxt
182 end
183 end
184
185
186 if otxt and (#otxt > 0) then
187 return otxt
188
189 else
190 return "(empty)"
191 end
192 end
193
194
195 function m5.on_commit(self)
196 if sy.init.enabled("unbound") then
197 -- Restart Unbound with configuration
198 sy.call("/etc/init.d/unbound restart >/dev/null 2>&1")
199
200 else
201 sy.call("/etc/init.d/unbound stop >/dev/null 2>&1")
202 end
203 end
204
205
206 return m5
207