summaryrefslogtreecommitdiffstats
path: root/awall/ipset.lua
blob: 28cc05bcbfde2661b6fa8750c8d20f87b2c756a4 (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
--[[
Ipset file dumper for Alpine Wall
Copyright (C) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
]]--


module(..., package.seeall)

require 'awall.object'

IPSet = awall.object.class()

function IPSet:init(config) self.config = config or {} end

function IPSet:dumpfile(name, ipsfile)
   ipsfile:write('# ipset '..name..'\n')
   ipsfile:write(table.concat(self.config[name].options, ' '))
   ipsfile:write('\n')
end

function IPSet:create()
   for name, ipset in pairs(self.config) do
      local pid = lpc.run('ipset', '-!', 'create', name,
			  unpack(ipset.options))
      if lpc.wait(pid) ~= 0 then
	 io.stderr:write('ipset creation failed: '..name)
      end
   end
end

function IPSet:print()
   for name, ipset in pairs(self.config) do
      self:dumpfile(name, io.stdout)
      io.stdout:write('\n')
   end
end

function IPSet:dump(ipsdir)
   for name, ipset in pairs(self.config) do
      local fname = ipsdir..'/'..name
      local file = io.open(fname)
      if not file then
	 file = io.open(fname, 'w')
	 self:dumpfile(name, file)
      end
      file:close()
   end
end