treewide: Fix typos in comments
[project/luci.git] / applications / luci-app-unbound / luasrc / model / cbi / unbound / zones.lua
1 -- Copyright 2018 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 ds = require "luci.dispatcher"
11 local resolvfile = "/tmp/resolv.conf.auto"
12 local logerr = ut.exec("logread -e 'unbound.*error.*ssl library'")
13
14 m5 = Map("unbound")
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"))
20
21 s5.addremove = true
22 s5.anonymous = true
23 s5.sortable = true
24 s5.template = "cbi/tblsection"
25 s5.extedit = ds.build_url("admin/services/unbound/zones/%s")
26
27 ztype = s5:option(DummyValue, "DummyType", translate("Type"))
28 ztype.rawhtml = true
29
30 zones = s5:option(DummyValue, "DummyZones", translate("Zones"))
31 zones.rawhtml = true
32
33 servers = s5:option(DummyValue, "DummyServers", translate("Servers"))
34 servers.rawhtml = true
35
36 fallback = s5:option(Flag, "fallback", translate("Fallback"))
37 fallback.rmempty = false
38
39 enabled = s5:option(Flag, "enabled", translate("Enable"))
40 enabled.rmempty = false
41
42
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 )
47 end
48
49
50 function s5.create(self, section)
51 created = TypedSection.create(self, section)
52 end
53
54
55 function s5.parse(self, ...)
56 TypedSection.parse(self, ...)
57 end
58
59
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")
64
65
66 if itxt and itxt:match("forward") then
67 if itls and (itls == "1") then
68 return translate("Forward TLS")
69
70 else
71 return translate("Forward")
72 end
73
74 elseif itxt and itxt:match("stub") then
75 return translate("Recurse")
76
77 elseif itxt and itxt:match("auth") then
78 return translate("AXFR")
79
80 else
81 return translate("Undefined")
82 end
83 end
84
85
86 function zones.cfgvalue(self, s)
87 -- Format a meaningful sentence for the Zones viewed column
88 local xtxt, otxt
89 local itxt = self.map:get(s, "zone_name")
90 local itype = self.map:get(s, "zone_type")
91
92
93 for xtxt in ut.imatch(itxt) do
94 if (xtxt == ".") then
95 -- zone_name lists
96 xtxt = translate("(root)")
97 end
98
99
100 if otxt and (#otxt > 0) then
101 otxt = otxt .. ", <var>%s</var>" % xtxt
102
103 else
104 otxt = "<var>%s</var>" % xtxt
105 end
106 end
107
108
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
113
114 elseif itype and itype:match("stub") then
115 otxt = translate("select recursion for ") .. otxt
116
117 elseif itype and itype:match("auth") then
118 otxt = translate("prefetch zone files for ") .. otxt
119
120 else
121 otxt = translate("unknown action for ") .. otxt
122 end
123
124
125 return otxt
126
127 else
128 return "(empty)"
129 end
130 end
131
132
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")
142
143
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
148
149 else
150 otxt = "<var>%s</var>" % xtxt
151 end
152 end
153
154
155 if otxt and (#otxt > 0) then
156 otxt = translate("use nameservers ") .. otxt
157 end
158
159
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)
166 end
167
168
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)
173
174 else
175 otxt = translatef("download from <var>%s</var>", iurl)
176 end
177 end
178
179
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
183 found = true
184
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
189
190 else
191 rtxt = "<var>%s</var>" % xtxt
192 end
193
194
195 found = false
196 end
197 end
198
199
200 if otxt and (#otxt > 0) and rtxt and (#rtxt > 0) then
201 otxt = otxt .. translatef(
202 ", and <var>%s</var> entries ", resolvfile) .. rtxt
203
204 elseif rtxt and (#rtxt > 0) then
205 otxt = translatef(
206 "use <var>%s</var> nameservers ", resolvfile) .. rtxt
207 end
208 end
209
210
211 if otxt and (#otxt > 0) then
212 return otxt
213
214 else
215 return "(empty)"
216 end
217 end
218
219
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")
224
225 else
226 sy.call("/etc/init.d/unbound stop >/dev/null 2>&1")
227 end
228 end
229
230
231 return m5
232