applications/luci-firewall: make rule descriptions fully translateable
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / firewall / rules.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.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 $Id$
13 ]]--
14
15 local ds = require "luci.dispatcher"
16 local ft = require "luci.tools.firewall"
17
18 m = Map("firewall",
19         translate("Firewall - Traffic Rules"),
20         translate("Traffic rules define policies for packets traveling between \
21                 different zones, for example to reject traffic between certain hosts \
22                 or to open WAN ports on the router."))
23
24 --
25 -- Rules
26 --
27
28 s = m:section(TypedSection, "rule", translate("Traffic Rules"))
29 s.addremove = true
30 s.anonymous = true
31 s.sortable  = true
32 s.template = "cbi/tblsection"
33 s.extedit   = ds.build_url("admin/network/firewall/rules/%s")
34 s.defaults.target = "ACCEPT"
35 s.template_addremove = "firewall/cbi_addrule"
36
37
38 function s.create(self, section)
39         created = TypedSection.create(self, section)
40 end
41
42 function s.parse(self, ...)
43         TypedSection.parse(self, ...)
44
45         local i_n = m:formvalue("_newopen.name")
46         local i_p = m:formvalue("_newopen.proto")
47         local i_e = m:formvalue("_newopen.extport")
48         local i_x = m:formvalue("_newopen.submit")
49
50         local f_n = m:formvalue("_newfwd.name")
51         local f_s = m:formvalue("_newfwd.src")
52         local f_d = m:formvalue("_newfwd.dest")
53         local f_x = m:formvalue("_newfwd.submit")
54
55         if i_x then
56                 created = TypedSection.create(self, section)
57
58                 self.map:set(created, "target",    "ACCEPT")
59                 self.map:set(created, "src",       "wan")
60                 self.map:set(created, "proto",     (i_p ~= "other") and i_p or "all")
61                 self.map:set(created, "dest_port", i_e)
62                 self.map:set(created, "_name",     i_n)
63
64                 if i_p ~= "other" and i_e and #i_e > 0 then
65                         created = nil
66                 end
67
68         elseif f_x then
69                 created = TypedSection.create(self, section)
70
71                 self.map:set(created, "target", "ACCEPT")
72                 self.map:set(created, "src",    f_s)
73                 self.map:set(created, "dest",   f_d)
74                 self.map:set(created, "_name",  f_n)
75         end
76
77         if created then
78                 m.uci:save("firewall")
79                 luci.http.redirect(ds.build_url(
80                         "admin/network/firewall/rules", created
81                 ))
82         end
83 end
84
85 name = s:option(DummyValue, "_name", translate("Name"))
86 function name.cfgvalue(self, s)
87         return self.map:get(s, "_name") or "-"
88 end
89
90 family = s:option(DummyValue, "family", translate("Family"))
91 function family.cfgvalue(self, s)
92         local f = self.map:get(s, "family")
93         if f and f:match("4") then
94                 return translate("IPv4")
95         elseif f and f:match("6") then
96                 return translate("IPv6")
97         else
98                 return translate("Any")
99         end
100 end
101
102 proto = s:option(DummyValue, "proto", translate("Protocol"))
103 proto.rawhtml = true
104 proto.width   = "20%"
105 function proto.cfgvalue(self, s)
106         return ft.fmt_proto(self.map:get(s, "proto"), self.map:get(s, "icmp_type"))
107                 or "TCP+UDP"
108 end
109
110 src = s:option(DummyValue, "src", translate("Source"))
111 src.rawhtml = true
112 src.width   = "20%"
113 function src.cfgvalue(self, s)
114         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
115         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
116         local p = ft.fmt_port(self.map:get(s, "src_port"))
117         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
118
119         if p and m then
120                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
121         elseif p or m then
122                 return translatef("From %s in %s with source %s", a, z, p or m)
123         else
124                 return translatef("From %s in %s", a, z)
125         end
126 end
127
128 dest = s:option(DummyValue, "dest", translate("Destination"))
129 dest.rawhtml = true
130 dest.width   = "20%"
131 function dest.cfgvalue(self, s)
132         local z = ft.fmt_zone(self.map:get(s, "dest"))
133         local p = ft.fmt_port(self.map:get(s, "dest_port"))
134
135         -- Forward
136         if z then
137                 local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
138                 if p then
139                         return translatef("To %s, %s in %s", a, p, z)
140                 else
141                         return translatef("To %s in %s", a, z)
142                 end
143
144         -- Input
145         else
146                 local a = ft.fmt_ip(self.map:get(s, "dest_ip"),
147                         translate("any router IP"))
148
149                 if p then
150                         return translatef("To %s at %s on <var>this device</var>", a, p)
151                 else
152                         return translatef("To %s on <var>this device</var>", a)
153                 end
154         end
155 end
156
157
158 target = s:option(DummyValue, "target", translate("Action"))
159 target.rawhtml = true
160 target.width   = "20%"
161 function target.cfgvalue(self, s)
162         local t = ft.fmt_target(self.map:get(s, "target"), self.map:get(s, "dest"))
163         local l = ft.fmt_limit(self.map:get(s, "limit"),
164                 self.map:get(s, "limit_burst"))
165
166         if l then
167                 return translatef("<var>%s</var> and limit to %s", t, l)
168         else
169                 return "<var>%s</var>" % t
170         end
171 end
172
173
174 --
175 -- SNAT
176 --
177
178 s = m:section(TypedSection, "redirect",
179         translate("Source NAT"),
180         translate("Source NAT is a specific form of masquerading which allows \
181                 fine grained control over the source IP used for outgoing traffic, \
182                 for example to map multiple WAN addresses to internal subnets."))
183 s.template  = "cbi/tblsection"
184 s.addremove = true
185 s.anonymous = true
186 s.sortable  = true
187 s.extedit   = ds.build_url("admin/network/firewall/rules/%s")
188 s.template_addremove = "firewall/cbi_addsnat"
189
190 function s.create(self, section)
191         created = TypedSection.create(self, section)
192 end
193
194 function s.parse(self, ...)
195         TypedSection.parse(self, ...)
196
197         local n = m:formvalue("_newsnat.name")
198         local s = m:formvalue("_newsnat.src")
199         local d = m:formvalue("_newsnat.dest")
200         local a = m:formvalue("_newsnat.dip")
201         local p = m:formvalue("_newsnat.dport")
202         local x = m:formvalue("_newsnat.submit")
203
204         if x and a and #a > 0 then
205                 created = TypedSection.create(self, section)
206
207                 self.map:set(created, "target",    "SNAT")
208                 self.map:set(created, "src",       s)
209                 self.map:set(created, "dest",      d)
210                 self.map:set(created, "proto",     "all")
211                 self.map:set(created, "src_dip",   a)
212                 self.map:set(created, "src_dport", p)
213                 self.map:set(created, "_name",     n)
214         end
215
216         if created then
217                 m.uci:save("firewall")
218                 luci.http.redirect(ds.build_url(
219                         "admin/network/firewall/rules", created
220                 ))
221         end
222 end
223
224 function s.filter(self, sid)
225         return (self.map:get(sid, "target") == "SNAT")
226 end
227
228 name = s:option(DummyValue, "_name", translate("Name"))
229 function name.cfgvalue(self, s)
230         return self.map:get(s, "_name") or "-"
231 end
232
233 proto = s:option(DummyValue, "proto", translate("Protocol"))
234 proto.rawhtml = true
235 function proto.cfgvalue(self, s)
236         return ft.fmt_proto(self.map:get(s, "proto")) or "TCP+UDP"
237 end
238
239
240 src = s:option(DummyValue, "src", translate("Source"))
241 src.rawhtml = true
242 src.width   = "20%"
243 function src.cfgvalue(self, s)
244         local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
245         local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
246         local p = ft.fmt_port(self.map:get(s, "src_port"))
247         local m = ft.fmt_mac(self.map:get(s, "src_mac"))
248
249         if p and m then
250                 return translatef("From %s in %s with source %s and %s", a, z, p, m)
251         elseif p or m then
252                 return translatef("From %s in %s with source %s", a, z, p or m)
253         else
254                 return translatef("From %s in %s", a, z)
255         end
256 end
257
258 dest = s:option(DummyValue, "dest", translate("Destination"))
259 dest.rawhtml = true
260 dest.width   = "30%"
261 function dest.cfgvalue(self, s)
262         local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
263         local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
264         local p = ft.fmt_port(self.map:get(s, "dest_port")) or
265                 ft.fmt_port(self.map:get(s, "src_dport"))
266
267         if p then
268                 return translatef("To %s, %s in %s", a, p, z)
269         else
270                 return translatef("To %s in %s", a, z)
271         end
272 end
273
274 snat = s:option(DummyValue, "via", translate("SNAT"))
275 snat.rawhtml = true
276 snat.width   = "20%"
277 function snat.cfgvalue(self, s)
278         local a = ft.fmt_ip(self.map:get(s, "src_dip"))
279         local p = ft.fmt_port(self.map:get(s, "src_dport"))
280
281         if a and p then
282                 return translatef("Rewrite to source %s, %s", a, p)
283         else
284                 return translatef("Rewrite to source %s", a or p)
285         end
286 end
287
288
289 return m