init.lua 3.08 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2 3 4 5 6 7 8 9
--[[
Alpine Wall main module
Copyright (C) 2012 Kaarle Ritvanen
Licensed under the terms of GPL2
]]--

module(..., package.seeall)

require 'json'
10
require 'lfs'
11
require 'stringy'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
12

13
require 'awall.ipset'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
14
require 'awall.iptables'
15
require 'awall.model'
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
16 17 18
require 'awall.util'


19 20 21
confdirs = {'/usr/share/awall', '/etc/awall'}
iptdir = '/etc/iptables'
ipsfile = '/etc/ipset.d/awall'
22

23 24

local modules = {package.loaded['awall.model']}
25 26 27 28 29 30 31 32

function loadmodules(path)
   for modfile in lfs.dir((path or '/usr/share/lua/5.1')..'/awall/modules') do
      if stringy.endswith(modfile, '.lua') then
	 local name = 'awall.modules.'..string.sub(modfile, 1, -5)
	 require(name)
	 table.insert(modules, package.loaded[name])
      end
33
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
34 35 36
end


37
local function readconfig()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
38

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
39 40 41
   local config = {}
   local iptables = awall.iptables.new()
   local context = {input=config, iptables=iptables}
42 43

   for i, dir in ipairs(confdirs) do
44 45 46 47 48
      local fnames = {}
      for fname in lfs.dir(dir) do table.insert(fnames, fname) end
      table.sort(fnames)

      for i, fname in ipairs(fnames) do
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	 if string.sub(fname, 1, 1) ~= '.' then
	    local data = ''
	    for line in io.lines(dir..'/'..fname) do data = data..line end
	    data = json.decode(data)
	    
	    for cls, objs in pairs(data) do
	       if not config[cls] then config[cls] = objs
	       elseif objs[1] then util.extend(config[cls], objs)
	       else
		  for k, v in pairs(objs) do config[cls][k] = v end
	       end
	    end
	 end
      end
   end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
64

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

   function expandvars(obj)
      for k, v in pairs(obj) do
	 if type(v) == 'table' then
	    expandvars(v)

	 else
	    local visited = {}
	    local val = v

	    while type(val) == 'string' and string.sub(val, 1, 1) == '$' do
	       local name = string.sub(val, 2, -1)
		  
	       if util.contains(visited, name) then
		  error('Circular variable definition: '..name)
	       end
	       table.insert(visited, name)
		  
	       val = config.variable[name]
	       if not val then error('Invalid variable reference: '..name) end
	    end

	    obj[k] = val
	 end
      end
   end

   expandvars(config)


Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
95
   function insertrule(trule)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
96
      local t = iptables.config[trule.family][trule.table][trule.chain]
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
97 98 99 100 101 102 103 104 105 106 107 108
      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	    
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
109 110
	    awall.util.map(config[path],
			   function(obj) return cls.morph(obj, context) end)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
111 112 113 114 115 116 117 118 119 120 121 122 123
	    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
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
124 125 126 127

   context.ipset = awall.ipset.new(config.ipset)

   return context
128
end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
129

130
function dump()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
131 132 133
   local context = readconfig()
   context.ipset:dump(ipsfile)
   context.iptables:dump(iptdir)
134
end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
135

136
function test()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
137 138 139
   local context = readconfig()
   context.ipset:create()
   context.iptables:test()
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
140
end