1 -- Copyright 2017 Eric Luehrsen <ericluehrsen@gmail.com>
2 -- Licensed to the public under the Apache License 2.0.
5 local ztype, zones, servers, fallback, enabled
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"
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" ))
21 s5.template = "cbi/tblsection"
23 ztype = s5:option(DummyValue, "DummyType", translate("Type"))
26 zones = s5:option(DummyValue, "DummyZones", translate("Zones"))
29 servers = s5:option(DummyValue, "DummyServers", translate("Servers"))
30 servers.rawhtml = true
32 fallback = s5:option(Flag, "fallback", translate("Fallback"))
33 fallback.rmempty = false
35 enabled = s5:option(Flag, "enabled", translate("Enable"))
36 enabled.rmempty = false
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")
45 if itxt and itxt:match("forward") then
46 if itls and (itls == "1") then
47 return translate("Forward TLS")
50 return translate("Forward")
53 elseif itxt and itxt:match("stub") then
54 return translate("Recurse")
56 elseif itxt and itxt:match("auth") then
57 return translate("AXFR")
60 return translate("Error")
65 function zones.cfgvalue(self, s)
66 -- Format a meaninful sentence for the Zones viewed column
68 local itxt = self.map:get(s, "zone_name")
69 local itype = self.map:get(s, "zone_type")
72 for xtxt in ut.imatch(itxt) do
75 xtxt = translate("(root)")
79 if otxt and (#otxt > 0) then
80 otxt = otxt .. ", <var>%s</var>" % xtxt
83 otxt = "<var>%s</var>" % xtxt
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
92 elseif itype and itype:match("stub") then
93 otxt = translate("select recursion for ") .. otxt
95 elseif itype and itype:match("auth") then
96 otxt = translate("prefetch zone files for ") .. otxt
99 otxt = translate("unknown action for ") .. otxt
103 if otxt and (#otxt > 0) then
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")
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
129 otxt = "<var>%s</var>" % xtxt
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
140 elseif otxt and (#otxt > 0) then
141 otxt = translate("use nameservers ") .. otxt
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)
151 otxt = translatef("download from <var>%s</var>", iurl)
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
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
167 rtxt = "<var>%s</var>" % xtxt
176 if otxt and (#otxt > 0) and rtxt and (#rtxt > 0) then
178 .. translatef(", and <var>%s</var> entries ", resolvfile) .. rtxt
180 elseif rtxt and (#rtxt > 0) then
181 otxt = translatef("use <var>%s</var> nameservers ", resolvfile) .. rtxt
186 if otxt and (#otxt > 0) then
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")
201 sy.call("/etc/init.d/unbound stop >/dev/null 2>&1")