ipset.lua 1.08 KB
Newer Older
1 2
--[[
Ipset file dumper for Alpine Wall
3
Copyright (C) 2012-2013 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
5 6 7 8 9
]]--


module(..., package.seeall)

10
require 'awall.object'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
11

12
IPSet = awall.object.class()
13

14
function IPSet:init(config) self.config = config or {} end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
15

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

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
22
function IPSet:create()
23 24
   for name, ipset in pairs(self.config) do
      local pid = lpc.run('ipset', '-!', 'create', name,
25
			  unpack(ipset.options))
26
      if lpc.wait(pid) ~= 0 then
27
	 io.stderr:write('ipset creation failed: '..name)
28 29 30 31
      end
   end
end

32 33 34 35 36
function IPSet:print()
   for name, ipset in pairs(self.config) do
      self:dumpfile(name, io.stdout)
      io.stdout:write('\n')
   end
37 38
end

39 40 41 42 43 44 45 46 47 48
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
49
end