ipset.lua 1.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
--[[
Ipset file dumper for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--


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 18 19 20 21 22 23 24
function IPSet:options(name)
   local ipset = self.config[name]
   if not ipset.type then ipset:error('Type not defined') end
   if not ipset.family then ipset:error('Family not defined') end
   return {ipset.type, 'family', ipset.family}
end

function IPSet:dumpfile(name, ipsfile)
   ipsfile:write('# ipset '..name..'\n')
25
   ipsfile:write(table.concat(self:options(name), ' '))
26
   ipsfile:write('\n')
27 28
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
29
function IPSet:create()
30 31 32
   for name, ipset in pairs(self.config) do
      local pid = lpc.run('ipset', '-!', 'create', name,
			  unpack(self:options(name)))
33
      if lpc.wait(pid) ~= 0 then
34
	 io.stderr:write('ipset creation failed: '..name)
35 36 37 38
      end
   end
end

39 40 41 42 43
function IPSet:print()
   for name, ipset in pairs(self.config) do
      self:dumpfile(name, io.stdout)
      io.stdout:write('\n')
   end
44 45
end

46 47 48 49 50 51 52 53 54 55
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
56
end