model.lua 10.1 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2 3 4 5 6 7 8 9 10 11 12
--[[
Base data model for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--


module(..., package.seeall)

require 'awall'
require 'awall.host'
require 'awall.util'
13
require 'awall.object'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
14 15 16 17 18
require 'awall.optfrag'

local util = awall.util
local combinations = awall.optfrag.combinations

19
class = awall.object.class
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
20 21


22
ConfigObject = class(awall.object.Object)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
23

24
function ConfigObject:init(context, location)
25 26
   if context then
      self.context = context
27
      self.root = context.objects
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
28
   end
29
   self.location = location
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
30 31
end

32
function ConfigObject:create(cls, params)
33 34 35 36 37 38 39
   if type(cls) == 'string' then
      local name = cls
      cls = awall.classmap[cls]
      if not cls then
	 self:error('Support for '..name..' objects not installed')
      end
   end
40 41 42 43 44
   return cls.morph(params, self.context, self.location)
end

function ConfigObject:error(msg) error(self.location..': '..msg) end

45
function ConfigObject:trules() return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
46

47 48 49 50 51 52 53 54 55 56
function ConfigObject:info()
   local res = {}
   for i, trule in ipairs(self:trules()) do
      table.insert(res,
		   {'  '..trule.family..'/'..trule.table..'/'..trule.chain,
		    trule.opts})
   end
   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
57

58
Zone = class(ConfigObject)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
59 60 61 62 63 64 65 66 67

function Zone:optfrags(dir)
   local iopt, aopt, iprop, aprop
   if dir == 'in' then
      iopt, aopt, iprop, aprop = 'i', 's', 'in', 'src'
   elseif dir == 'out' then
      iopt, aopt, iprop, aprop = 'o', 'd', 'out', 'dest'
   else assert(false) end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
68 69 70 71
   local aopts = nil
   if self.addr then
      aopts = {}
      for i, hostdef in util.listpairs(self.addr) do
72
	 for i, addr in ipairs(awall.host.resolve(hostdef, self)) do
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
73 74 75 76 77
	    table.insert(aopts,
			 {family=addr[1],
			  [aprop]=addr[2],
			  opts='-'..aopt..' '..addr[2]})
	 end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
      end
   end

   return combinations(util.maplist(self.iface,
				    function(x)
				       return {[iprop]=x,
					       opts='-'..iopt..' '..x}
				    end),
		       aopts)
end


fwzone = Zone.new()


93 94
Rule = class(ConfigObject)

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
95

96 97
function Rule:init(...)
   ConfigObject.init(self, unpack(arg))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
98

99 100
   self.newchains = {}

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
101 102 103
   for i, prop in ipairs({'in', 'out'}) do
      self[prop] = self[prop] and util.maplist(self[prop],
					       function(z)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
104
						  if type(z) ~= 'string' then return z end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
105
						  return z == '_fw' and fwzone or
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
106
						     self.root.zone[z] or
107
						     self:error('Invalid zone: '..z)
108
					       end)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
109
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
110

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
111
   if self.service then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
112
      if type(self.service) == 'string' then self.label = self.service end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
113 114
      self.service = util.maplist(self.service,
				  function(s)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
115
				     if type(s) ~= 'string' then return s end
116
				     return self.root.service[s] or self:error('Invalid service: '..s)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
117 118 119 120 121 122 123 124 125
				  end)
   end
end

function Rule:defaultzones() return {nil, fwzone} end


function Rule:zoneoptfrags()

126
   local function zonepair(zin, zout)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
127

128
      local function zofs(zone, dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
129
	 if not zone then return zone end
130
	 return zone:optfrags(dir)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
131 132 133 134 135
      end

      local chain, ofrags

      if zin == fwzone or zout == fwzone then
136
	 if zin == zout then return {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
137 138 139 140 141 142 143
	 local dir, z = 'in', zin
	 if zin == fwzone then dir, z = 'out', zout end
	 chain = string.upper(dir)..'PUT'
	 ofrags = zofs(z, dir)

      else
	 chain = 'FORWARD'
144 145 146 147 148 149 150 151 152
	 ofrags = combinations(zofs(zin, 'in'), zofs(zout, 'out'))

	 if ofrags then
	    ofrags = util.filter(ofrags,
				 function(of)
				    return not (of['in'] and of.out and
						of['in'] == of.out)
				 end)
	 end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
153 154 155 156 157 158 159 160 161 162
      end

      if not ofrags then ofrags = {{}} end

      for i, ofrag in ipairs(ofrags) do ofrag.fchain = chain end

      return ofrags
   end

   local res = {}
163 164
   local izones = self['in'] or self:defaultzones()
   local ozones = self.out or self:defaultzones()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
165

166 167 168
   for i = 1,math.max(1, table.maxn(izones)) do
      for j = 1,math.max(1, table.maxn(ozones)) do
	 util.extend(res, zonepair(izones[i], ozones[j]))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
169 170 171 172 173 174 175 176 177 178 179
      end
   end

   return res
end


function Rule:servoptfrags()

   if not self.service then return end

180
   local function containskey(tbl, key)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
181 182 183 184 185 186 187 188 189
      for k, v in pairs(tbl) do if k == key then return true end end
      return false
   end

   local ports = {}
   local res = {}

   for i, serv in ipairs(self.service) do
      for i, sdef in util.listpairs(serv) do
190
	 if not sdef.proto then self:error('Protocol not defined') end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
191 192 193 194 195 196 197

	 if util.contains({6, 'tcp', 17, 'udp'}, sdef.proto) then
	    local new = not containskey(ports, sdef.proto)
	    if new then ports[sdef.proto] = {} end

	    if new or ports[sdef.proto][1] then
	       if sdef.port then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
198
		  util.extend(ports[sdef.proto], sdef.port)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
199 200 201 202 203 204 205 206
	       else ports[sdef.proto] = {} end
	    end

	 else

	    local opts = '-p '..sdef.proto
	    local family = nil

207 208 209 210 211 212 213 214 215
	    -- TODO multiple ICMP types per rule
	    local oname
	    if util.contains({1, 'icmp'}, sdef.proto) then
	       family = 'inet'
	       oname = 'icmp-type'
	    elseif util.contains({58, 'ipv6-icmp', 'icmpv6'}, sdef.proto) then
	       family = 'inet6'
	       oname = 'icmpv6-type'
	    elseif sdef.type then
216
	       self:error('Type specification not valid with '..sdef.proto)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
217
	    end
218
	    if sdef.type then opts = opts..' --'..oname..' '..sdef.type end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

	    table.insert(res, {family=family, opts=opts})
	 end
      end
   end

   for proto, plist in pairs(ports) do
      local opts = '-p '..proto
      local len = table.maxn(plist)

      if len == 1 then
	 opts = opts..' --dport '..plist[1]
      elseif len > 1 then
	 opts = opts..' -m multiport --dports '
	 for i, port in ipairs(plist) do
	    if i > 1 then opts = opts..',' end
	    opts = opts..port
	 end
      end

      table.insert(res, {opts=opts})
   end

   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
245
function Rule:destoptfrags()
246
   return self:create(Zone, {addr=self.dest}):optfrags('out')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
247 248
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
249 250 251 252 253 254 255
function Rule:table() return 'filter' end

function Rule:chain() return nil end

function Rule:position() return 'append' end

function Rule:target()
256
   if not self.action then self:error('Action not defined') end
257 258 259 260
   if util.contains({'accept', 'drop', 'reject'}, self.action) then
      return string.upper(self.action)
   end
   return self.action
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
261 262 263 264 265
end


function Rule:trules()

266
   local function tag(ofrags, tag, value)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
267 268 269 270 271 272 273 274
      for i, ofrag in ipairs(ofrags) do
	 assert(not ofrag[tag])
	 ofrag[tag] = value
      end
   end

   local families

275
   local function setfamilies(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
276 277 278 279 280 281 282 283 284 285 286 287
      if ofrags then
	 families = {}
	 for i, ofrag in ipairs(ofrags) do
	    if not ofrag.family then
	       families = nil
	       return
	    end
	    table.insert(families, ofrag.family)
	 end
      else families = nil end
   end

288
   local function ffilter(ofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
289
      if not ofrags or not ofrags[1] or not families then return ofrags end
290 291 292 293 294
      return util.filter(ofrags,
			 function(of)
			    return not of.family or util.contains(families,
								  of.family)
			 end)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
295 296
   end

297
   local function appendtarget(ofrag, target)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
298 299 300 301 302
      ofrag.opts = (ofrag.opts and ofrag.opts..' ' or '')..'-j '..target
   end

   local res = self:zoneoptfrags()

303
   if self.ipset then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
304 305
      local ipsetofrags = {}
      for i, ipset in util.listpairs(self.ipset) do
306
	 if not ipset.name then self:error('Set name not defined') end
307

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
308
	 local setdef = self.root.ipset and self.root.ipset[ipset.name]
309
	 if not setdef then self:error('Invalid set name') end
310

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
311
	 if not ipset.args then
312
	    self:error('Set direction arguments not defined')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
313
	 end
314

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
315 316 317 318 319
	 local setopts = '-m set --match-set '..ipset.name..' '
	 for i, arg in util.listpairs(ipset.args) do
	    if i > 1 then setopts = setopts..',' end
	    if arg == 'in' then setopts = setopts..'src'
	    elseif arg == 'out' then setopts = setopts..'dst'
320
	    else self:error('Invalid set direction argument') end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
321
	 end
322
	 table.insert(ipsetofrags, {family=setdef.family, opts=setopts})
323
      end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
324
      res = combinations(res, ipsetofrags)
325 326
   end

327 328
   if self.ipsec then
      res = combinations(res, {{opts='-m policy --pol ipsec --dir '..self.ipsec}})
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
329 330 331 332 333 334 335
   end

   res = combinations(res, self:servoptfrags())

   setfamilies(res)
   tag(res, 'chain', self:chain())

336
   local addrofrags = combinations(self:create(Zone, {addr=self.src}):optfrags('in'),
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
337
				   self:destoptfrags())
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

   if addrofrags then
      addrofrags = ffilter(addrofrags)
      setfamilies(addrofrags)
      res = ffilter(res)
   end

   local addrchain = false
   for i, ofrag in ipairs(res) do
      if not ofrag.chain then ofrag.chain = ofrag.fchain end
      addrchain = addrchain or (self.src and ofrag.src) or (self.dest and ofrag.dest)
   end

   local target
   if addrchain then
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
353
      target = self:newchain('address')
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
354 355
   else
      target = self:target()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
356
      res = combinations(res, addrofrags)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370
   end

   tag(res, 'position', self:position())

   for i, ofrag in ipairs(res) do appendtarget(ofrag, target) end

   if addrchain then
      for i, ofrag in ipairs(addrofrags) do
	 ofrag.chain = target
	 appendtarget(ofrag, self:target())
	 table.insert(res, ofrag)
      end      
   end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
371
   util.extend(res, ffilter(self:extraoptfrags()))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
372 373

   tag(res, 'table', self:table(), false)
374 375 376 377 378 379 380 381 382 383 384

   local function checkzof(ofrag, dir, chains)
      if ofrag[dir] and util.contains(chains, ofrag.chain) then
	 self:error('Cannot specify '..dir..'bound interface ('..ofrag[dir]..')')
      end
   end

   for i, ofrag in ipairs(res) do
      checkzof(ofrag, 'in', {'OUTPUT', 'POSTROUTING'})
      checkzof(ofrag, 'out', {'INPUT', 'PREROUTING'})
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
385
   
386
   return combinations(res, ffilter({{family='inet'}, {family='inet6'}}))
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
387 388 389 390
end

function Rule:extraoptfrags() return {} end

391 392 393
function Rule:newchain(key)
   if self.newchains[key] then return self.newchains[key] end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
394 395 396
   if not self.context.lastid then self.context.lastid = {} end
   local lastid = self.context.lastid

397 398 399 400 401 402 403 404
   local res = key
   if self.label then res = res..'-'..self.label end
   if not lastid[res] then lastid[res] = -1 end
   lastid[res] = lastid[res] + 1
   res = res..'-'..lastid[res]

   self.newchains[key] = res
   return res
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
405 406
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
407

408 409 410 411 412
ForwardOnlyRule = class(Rule)

function ForwardOnlyRule:init(...)
   Rule.init(self, unpack(arg))
   for i, dir in ipairs({'in', 'out'}) do
413
      if self[dir] and util.contains(self[dir], fwzone) then
414 415 416 417 418 419 420 421 422 423
	 self:error('Not applicable to the firewall zone')
      end
   end
end

function ForwardOnlyRule:defaultzones() return {nil} end

function ForwardOnlyRule:chain() return 'PREROUTING' end


424
classes = {{'zone', Zone}}
425