ipset.lua 1.04 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 13 14
IPSet = awall.object.class(awall.object.Object)

function IPSet:init(config) self.config = config end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
15 16

function IPSet:commands()
17
   local res = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
18
   if self.config then
19 20 21
      for name, ipset in pairs(self.config) do
	 if not ipset.type then ipset:error('Type not defined') end
	 if not ipset.family then ipset:error('Family not defined') end
22
	 table.insert(res,
23
		      'create '..name..' '..ipset.type..' family '..ipset.family..'\n')
24 25 26 27 28
      end
   end
   return res
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
29 30
function IPSet:create()
   for i, line in ipairs(self:commands()) do
31 32 33 34 35 36 37 38 39
      local pid, stdin = lpc.run('ipset', '-!', 'restore')
      stdin:write(line)
      stdin:close()
      if lpc.wait(pid) ~= 0 then
	 io.stderr:write('ipset command failed: '..line)
      end
   end
end

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
40
function IPSet:dump(ipsfile)
41
   local file = io.output(ipsfile)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
42
   for i, line in ipairs(self:commands()) do file:write(line) end
43 44
   file:close()
end