init.lua 1.38 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
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 50 51 52 53 54 55 56 57 58 59 60 61 62
--[[
Alpine Wall main module
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--

module(..., package.seeall)

require 'json'

require 'awall.iptables'
require 'awall.util'


local modules = {}

local modpath = arg[0] == '/usr/sbin/awall' and '/usr/share/lua/5.1' or '.'
for line in io.popen('cd '..modpath..' && ls awall/model.lua awall/modules/*.lua'):lines() do
   local name = string.gsub(string.sub(line, 1, -5), '/', '.')
   require(name)
   table.insert(modules, package.loaded[name])
end


function translate()

   local data = ''
   for line in io.lines('config.json') do data = data..line end
   config = json.decode(data)

   function insertrule(trule)
      local t = awall.iptables.config[trule.family][trule.table][trule.chain]
      if trule.position == 'prepend' then
	 table.insert(t, 1, trule.opts)
      else
	 table.insert(t, trule.opts)
      end
   end

   local locations = {}

   for i, mod in ipairs(modules) do
      for path, cls in pairs(mod.classmap) do
	 if config[path] then	    
	    awall.util.map(config[path], cls.morph)
	    table.insert(locations, config[path])
	 end
      end

      for i, rule in ipairs(mod.defrules) do insertrule(rule) end
   end


   for i, location in ipairs(locations) do
      for i, rule in ipairs(location) do
	 for i, trule in ipairs(rule:trules()) do insertrule(trule) end
      end
   end

   awall.iptables.dump()

end