Rework LuCI build system
[project/luci.git] / applications / luci-app-firewall / luasrc / tools / firewall.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011-2012 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 ]]--
13
14 module("luci.tools.firewall", package.seeall)
15
16 local ut = require "luci.util"
17 local ip = require "luci.ip"
18 local nx = require "nixio"
19
20 local translate, translatef = luci.i18n.translate, luci.i18n.translatef
21
22 local function tr(...)
23 return tostring(translate(...))
24 end
25
26 function fmt_neg(x)
27 if type(x) == "string" then
28 local v, neg = x:gsub("^ *! *", "")
29 if neg > 0 then
30 return v, "%s " % tr("not")
31 else
32 return x, ""
33 end
34 end
35 return x, ""
36 end
37
38 function fmt_mac(x)
39 if x and #x > 0 then
40 local m, n
41 local l = { tr("MAC"), " " }
42 for m in ut.imatch(x) do
43 m, n = fmt_neg(m)
44 l[#l+1] = "<var>%s%s</var>" %{ n, m }
45 l[#l+1] = ", "
46 end
47 if #l > 1 then
48 l[#l] = nil
49 if #l > 3 then
50 l[1] = tr("MACs")
51 end
52 return table.concat(l, "")
53 end
54 end
55 end
56
57 function fmt_port(x, d)
58 if x and #x > 0 then
59 local p, n
60 local l = { tr("port"), " " }
61 for p in ut.imatch(x) do
62 p, n = fmt_neg(p)
63 local a, b = p:match("(%d+)%D+(%d+)")
64 if a and b then
65 l[1] = tr("ports")
66 l[#l+1] = "<var>%s%d-%d</var>" %{ n, a, b }
67 else
68 l[#l+1] = "<var>%s%d</var>" %{ n, p }
69 end
70 l[#l+1] = ", "
71 end
72 if #l > 1 then
73 l[#l] = nil
74 if #l > 3 then
75 l[1] = tr("ports")
76 end
77 return table.concat(l, "")
78 end
79 end
80 return d and "<var>%s</var>" % d
81 end
82
83 function fmt_ip(x, d)
84 if x and #x > 0 then
85 local l = { tr("IP"), " " }
86 local v, a, n
87 for v in ut.imatch(x) do
88 v, n = fmt_neg(v)
89 a, m = v:match("(%S+)/(%d+%.%S+)")
90 a = a or v
91 a = a:match(":") and ip.IPv6(a, m) or ip.IPv4(a, m)
92 if a and (a:is6() and a:prefix() < 128 or a:prefix() < 32) then
93 l[1] = tr("IP range")
94 l[#l+1] = "<var title='%s - %s'>%s%s</var>" %{
95 a:minhost():string(),
96 a:maxhost():string(),
97 n, a:string()
98 }
99 else
100 l[#l+1] = "<var>%s%s</var>" %{
101 n,
102 a and a:string() or v
103 }
104 end
105 l[#l+1] = ", "
106 end
107 if #l > 1 then
108 l[#l] = nil
109 if #l > 3 then
110 l[1] = tr("IPs")
111 end
112 return table.concat(l, "")
113 end
114 end
115 return d and "<var>%s</var>" % d
116 end
117
118 function fmt_zone(x, d)
119 if x == "*" then
120 return "<var>%s</var>" % tr("any zone")
121 elseif x and #x > 0 then
122 return "<var>%s</var>" % x
123 elseif d then
124 return "<var>%s</var>" % d
125 end
126 end
127
128 function fmt_icmp_type(x)
129 if x and #x > 0 then
130 local t, v, n
131 local l = { tr("type"), " " }
132 for v in ut.imatch(x) do
133 v, n = fmt_neg(v)
134 l[#l+1] = "<var>%s%s</var>" %{ n, v }
135 l[#l+1] = ", "
136 end
137 if #l > 1 then
138 l[#l] = nil
139 if #l > 3 then
140 l[1] = tr("types")
141 end
142 return table.concat(l, "")
143 end
144 end
145 end
146
147 function fmt_proto(x, icmp_types)
148 if x and #x > 0 then
149 local v, n
150 local l = { }
151 local t = fmt_icmp_type(icmp_types)
152 for v in ut.imatch(x) do
153 v, n = fmt_neg(v)
154 if v == "tcpudp" then
155 l[#l+1] = "TCP"
156 l[#l+1] = ", "
157 l[#l+1] = "UDP"
158 l[#l+1] = ", "
159 elseif v ~= "all" then
160 local p = nx.getproto(v)
161 if p then
162 -- ICMP
163 if (p.proto == 1 or p.proto == 58) and t then
164 l[#l+1] = translatef(
165 "%s%s with %s",
166 n, p.aliases[1] or p.name, t
167 )
168 else
169 l[#l+1] = "%s%s" %{
170 n,
171 p.aliases[1] or p.name
172 }
173 end
174 l[#l+1] = ", "
175 end
176 end
177 end
178 if #l > 0 then
179 l[#l] = nil
180 return table.concat(l, "")
181 end
182 end
183 end
184
185 function fmt_limit(limit, burst)
186 burst = tonumber(burst)
187 if limit and #limit > 0 then
188 local l, u = limit:match("(%d+)/(%w+)")
189 l = tonumber(l or limit)
190 u = u or "second"
191 if l then
192 if u:match("^s") then
193 u = tr("second")
194 elseif u:match("^m") then
195 u = tr("minute")
196 elseif u:match("^h") then
197 u = tr("hour")
198 elseif u:match("^d") then
199 u = tr("day")
200 end
201 if burst and burst > 0 then
202 return translatef("<var>%d</var> pkts. per <var>%s</var>, \
203 burst <var>%d</var> pkts.", l, u, burst)
204 else
205 return translatef("<var>%d</var> pkts. per <var>%s</var>", l, u)
206 end
207 end
208 end
209 end
210
211 function fmt_target(x, dest)
212 if dest and #dest > 0 then
213 if x == "ACCEPT" then
214 return tr("Accept forward")
215 elseif x == "REJECT" then
216 return tr("Refuse forward")
217 elseif x == "NOTRACK" then
218 return tr("Do not track forward")
219 else --if x == "DROP" then
220 return tr("Discard forward")
221 end
222 else
223 if x == "ACCEPT" then
224 return tr("Accept input")
225 elseif x == "REJECT" then
226 return tr("Refuse input")
227 elseif x == "NOTRACK" then
228 return tr("Do not track input")
229 else --if x == "DROP" then
230 return tr("Discard input")
231 end
232 end
233 end
234
235
236 function opt_enabled(s, t, ...)
237 if t == luci.cbi.Button then
238 local o = s:option(t, "__enabled")
239 function o.render(self, section)
240 if self.map:get(section, "enabled") ~= "0" then
241 self.title = tr("Rule is enabled")
242 self.inputtitle = tr("Disable")
243 self.inputstyle = "reset"
244 else
245 self.title = tr("Rule is disabled")
246 self.inputtitle = tr("Enable")
247 self.inputstyle = "apply"
248 end
249 t.render(self, section)
250 end
251 function o.write(self, section, value)
252 if self.map:get(section, "enabled") ~= "0" then
253 self.map:set(section, "enabled", "0")
254 else
255 self.map:del(section, "enabled")
256 end
257 end
258 return o
259 else
260 local o = s:option(t, "enabled", ...)
261 o.default = "1"
262 return o
263 end
264 end
265
266 function opt_name(s, t, ...)
267 local o = s:option(t, "name", ...)
268
269 function o.cfgvalue(self, section)
270 return self.map:get(section, "name") or
271 self.map:get(section, "_name") or "-"
272 end
273
274 function o.write(self, section, value)
275 if value ~= "-" then
276 self.map:set(section, "name", value)
277 self.map:del(section, "_name")
278 else
279 self:remove(section)
280 end
281 end
282
283 function o.remove(self, section)
284 self.map:del(section, "name")
285 self.map:del(section, "_name")
286 end
287
288 return o
289 end