summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-unbound/luasrc/model/cbi/unbound/zones.lua
blob: bbc0e2335f761f5ffcf8eb914b1c978c73128829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
-- Copyright 2017 Eric Luehrsen <ericluehrsen@gmail.com>
-- Licensed to the public under the Apache License 2.0.

local m5, s5
local ztype, zones, servers, fallback, enabled

local fs = require "nixio.fs"
local ut = require "luci.util"
local sy = require "luci.sys"
local resolvfile = "/tmp/resolv.conf.auto"

m5 = Map("unbound")
s5 = m5:section(TypedSection, "zone", "Zones",
    translatef("This shows extended zones and more details can be "
    .. "changed in Files tab and <a href=\"%s\">Edit:UCI</a> subtab.",
    "/cgi-bin/luci/admin/services/unbound/files" ))

s5.addremove = false
s5.anonymous = true
s5.sortable = true
s5.template = "cbi/tblsection"

ztype = s5:option(DummyValue, "DummyType", translate("Type"))
ztype.rawhtml = true

zones = s5:option(DummyValue, "DummyZones", translate("Zones"))
zones.rawhtml = true

servers = s5:option(DummyValue, "DummyServers", translate("Servers"))
servers.rawhtml = true

fallback = s5:option(Flag, "fallback", translate("Fallback"))
fallback.rmempty = false

enabled = s5:option(Flag, "enabled", translate("Enable"))
enabled.rmempty = false


function ztype.cfgvalue(self, s)
    -- Format a meaninful tile for the Zone Type column
    local itxt = self.map:get(s, "zone_type")
    local itls = self.map:get(s, "tls_upstream")


    if itxt and itxt:match("forward") then
        if itls and (itls == "1") then
            return translate("Forward TLS")

        else
            return translate("Forward")
        end

    elseif itxt and itxt:match("stub") then
        return translate("Recurse")

    elseif itxt and itxt:match("auth") then
        return translate("AXFR")

    else
        return translate("Error")
    end
end


function zones.cfgvalue(self, s)
    -- Format a meaninful sentence for the Zones viewed column
    local xtxt, otxt
    local itxt = self.map:get(s, "zone_name")
    local itype = self.map:get(s, "zone_type")


    for xtxt in ut.imatch(itxt) do
        if (xtxt == ".") then
            -- zone_name lists
            xtxt = translate("(root)")
        end


        if otxt and (#otxt > 0) then
            otxt = otxt .. ", <var>%s</var>" % xtxt

        else
            otxt = "<var>%s</var>" % xtxt
        end
    end


    if itype and itype:match("forward") then
        -- from zone_type create a readable hint for the action
        otxt = translate("accept upstream results for ") .. otxt

    elseif itype and itype:match("stub") then
        otxt = translate("select recursion for ") .. otxt

    elseif itype and itype:match("auth") then
        otxt = translate("prefetch zone files for ") .. otxt

    else
        otxt = translate("unknown action for ") .. otxt
    end


    if otxt and (#otxt > 0) then
        return otxt

    else
        return "(empty)"
    end
end


function servers.cfgvalue(self, s)
    -- Format a meaninful sentence for the Servers (and URL) column
    local xtxt, otxt, rtxt, found
    local itxt = self.map:get(s, "server")
    local iurl = self.map:get(s, "url_dir")
    local itype = self.map:get(s, "zone_type")
    local itls = self.map:get(s, "tls_upstream")
    local iidx = self.map:get(s, "tls_index")
    local irslv = self.map:get(s, "resolv_conf")


    for xtxt in ut.imatch(itxt) do
        if otxt and (#otxt > 0) then
            -- bundle and make pretty the server list
            otxt = otxt .. ", <var>%s</var>" % xtxt

        else
            otxt = "<var>%s</var>" % xtxt
        end
    end


    if otxt and (#otxt > 0)
    and itls and (itls == "1")
    and iidx and (#iidx > 0) then
        -- show TLS certificate name index if provided
        otxt = translatef("use nameservers by <var>%s</var> at ", iidx) .. otxt

    elseif otxt and (#otxt > 0) then
        otxt = translate("use nameservers ") .. otxt
    end


    if iurl and (#iurl > 0) and itype and itype:match("auth") then
        if otxt and (#otxt > 0) then
            -- include optional URL filed for auth-zone: type
            otxt = otxt .. translatef(", and try <var>%s</var>", iurl)

        else
            otxt = translatef("download from <var>%s</var>", iurl)
        end
    end


    if irslv and (irslv == "1") and itype and itype:match("forward") then
        for xtxt in ut.imatch(fs.readfile(resolvfile)) do
            if xtxt:match("nameserver") then
                found = true

            elseif (found == true) then
                if rtxt and (#rtxt > 0) then
                    -- fetch name servers from resolv.conf
                    rtxt = rtxt .. ", <var>%s</var>" % xtxt

                else
                    rtxt = "<var>%s</var>" % xtxt
                end


                found = false
            end
        end


        if otxt and (#otxt > 0) and rtxt and (#rtxt > 0) then
            otxt = otxt
                .. translatef(", and <var>%s</var> entries ", resolvfile) .. rtxt

        elseif rtxt and (#rtxt > 0) then
            otxt = translatef("use <var>%s</var> nameservers ", resolvfile) .. rtxt
        end
    end


    if otxt and (#otxt > 0) then
        return otxt

    else
        return "(empty)"
    end
end


function m5.on_commit(self)
    if sy.init.enabled("unbound") then
        -- Restart Unbound with configuration
        sy.call("/etc/init.d/unbound restart >/dev/null 2>&1")

    else
        sy.call("/etc/init.d/unbound stop >/dev/null 2>&1")
    end
end


return m5