1 -- Copyright 2018 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 ds = require "luci.dispatcher"
11 local resolvfile = "/tmp/resolv.conf.auto"
12 local logerr = ut.exec("logread -e 'unbound.*error.*ssl library'")
15 s5 = m5:section(TypedSection, "zone", "Zones",
16 translatef("Organize directed forward, stub, and authoritative zones"
17 .. " <a href=\"%s\" target=\"_blank\">(help)</a>.",
18 "https://www.unbound.net/",
19 "https://github.com/openwrt/packages/blob/master/net/unbound/files/README.md"))
24 s5.template = "cbi/tblsection"
25 s5.extedit = ds.build_url("admin/services/unbound/zones/%s")
27 ztype = s5:option(DummyValue, "DummyType", translate("Type"))
30 zones = s5:option(DummyValue, "DummyZones", translate("Zones"))
33 servers = s5:option(DummyValue, "DummyServers", translate("Servers"))
34 servers.rawhtml = true
36 fallback = s5:option(Flag, "fallback", translate("Fallback"))
37 fallback.rmempty = false
39 enabled = s5:option(Flag, "enabled", translate("Enable"))
40 enabled.rmempty = false
43 if logerr and (#logerr > 0) then
44 logerr = logerr:sub((1 + #logerr - math.min(#logerr, 250)), #logerr)
45 m5.message = translatef( "Note: SSL/TLS library is missing an API. "
46 .. "Please review syslog. >> logread ... " .. logerr )
50 function s5.create(self, section)
51 created = TypedSection.create(self, section)
55 function s5.parse(self, ...)
56 TypedSection.parse(self, ...)
60 function ztype.cfgvalue(self, s)
61 -- Format a meaningful tile for the Zone Type column
62 local itxt = self.map:get(s, "zone_type")
63 local itls = self.map:get(s, "tls_upstream")
66 if itxt and itxt:match("forward") then
67 if itls and (itls == "1") then
68 return translate("Forward TLS")
71 return translate("Forward")
74 elseif itxt and itxt:match("stub") then
75 return translate("Recurse")
77 elseif itxt and itxt:match("auth") then
78 return translate("AXFR")
81 return translate("Undefined")
86 function zones.cfgvalue(self, s)
87 -- Format a meaningful sentence for the Zones viewed column
89 local itxt = self.map:get(s, "zone_name")
90 local itype = self.map:get(s, "zone_type")
93 for xtxt in ut.imatch(itxt) do
96 xtxt = translate("(root)")
100 if otxt and (#otxt > 0) then
101 otxt = otxt .. ", <var>%s</var>" % xtxt
104 otxt = "<var>%s</var>" % xtxt
109 if otxt and (#otxt > 0) then
110 if itype and itype:match("forward") then
111 -- from zone_type create a readable hint for the action
112 otxt = translate("accept upstream results for ") .. otxt
114 elseif itype and itype:match("stub") then
115 otxt = translate("select recursion for ") .. otxt
117 elseif itype and itype:match("auth") then
118 otxt = translate("prefetch zone files for ") .. otxt
121 otxt = translate("unknown action for ") .. otxt
133 function servers.cfgvalue(self, s)
134 -- Format a meaningful sentence for the Servers (and URL) column
135 local xtxt, otxt, rtxt, found
136 local itxt = self.map:get(s, "server")
137 local iurl = self.map:get(s, "url_dir")
138 local itype = self.map:get(s, "zone_type")
139 local itls = self.map:get(s, "tls_upstream")
140 local iidx = self.map:get(s, "tls_index")
141 local irslv = self.map:get(s, "resolv_conf")
144 for xtxt in ut.imatch(itxt) do
145 if otxt and (#otxt > 0) then
146 -- bundle and make pretty the server list
147 otxt = otxt .. ", <var>%s</var>" % xtxt
150 otxt = "<var>%s</var>" % xtxt
155 if otxt and (#otxt > 0) then
156 otxt = translate("use nameservers ") .. otxt
160 if otxt and (#otxt > 0)
161 and itls and (itls == "1")
162 and iidx and (#iidx > 0) then
163 -- show TLS certificate name index if provided
164 otxt = otxt .. translatef(
165 " with default certificate for <var>%s</var>", iidx)
169 if iurl and (#iurl > 0) and itype and itype:match("auth") then
170 if otxt and (#otxt > 0) then
171 -- include optional URL filed for auth-zone: type
172 otxt = otxt .. translatef(", and try <var>%s</var>", iurl)
175 otxt = translatef("download from <var>%s</var>", iurl)
180 if irslv and (irslv == "1") and itype and itype:match("forward") then
181 for xtxt in ut.imatch(fs.readfile(resolvfile)) do
182 if xtxt:match("nameserver") then
185 elseif (found == true) then
186 if rtxt and (#rtxt > 0) then
187 -- fetch name servers from resolv.conf
188 rtxt = rtxt .. ", <var>%s</var>" % xtxt
191 rtxt = "<var>%s</var>" % xtxt
200 if otxt and (#otxt > 0) and rtxt and (#rtxt > 0) then
201 otxt = otxt .. translatef(
202 ", and <var>%s</var> entries ", resolvfile) .. rtxt
204 elseif rtxt and (#rtxt > 0) then
206 "use <var>%s</var> nameservers ", resolvfile) .. rtxt
211 if otxt and (#otxt > 0) then
220 function m5.on_commit(self)
221 if sy.init.enabled("unbound") then
222 -- Restart Unbound with configuration
223 sy.call("/etc/init.d/unbound restart >/dev/null 2>&1")
226 sy.call("/etc/init.d/unbound stop >/dev/null 2>&1")