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
63
64
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
--[[
Iptables file dumper for Alpine Wall
Copyright (C) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
]]--
module(..., package.seeall)
require 'lpc'
require 'awall.object'
require 'awall.uerror'
local util = require('awall.util')
local sortedkeys = util.sortedkeys
local class = awall.object.class
local families = {inet={cmd='iptables',
file='rules-save',
procfile='/proc/net/ip_tables_names'},
inet6={cmd='ip6tables',
file='rules6-save',
procfile='/proc/net/ip6_tables_names'}}
builtin = {filter={'FORWARD', 'INPUT', 'OUTPUT'},
mangle={'FORWARD', 'INPUT', 'OUTPUT', 'POSTROUTING', 'PREROUTING'},
nat={'INPUT', 'OUTPUT', 'POSTROUTING', 'PREROUTING'},
raw={'OUTPUT', 'PREROUTING'},
security={'FORWARD', 'INPUT', 'OUTPUT'}}
local backupdir = '/var/run/awall'
local BaseIPTables = class()
function BaseIPTables:print()
for family, tbls in pairs(families) do
self:dumpfile(family, io.stdout)
print()
end
end
function BaseIPTables:dump(dir)
for family, tbls in pairs(families) do
local file = io.output(dir..'/'..families[family].file)
self:dumpfile(family, file)
file:close()
end
end
function BaseIPTables:restore(test)
local disabled = true
for family, params in pairs(families) do
local file = io.open(params.procfile)
if file then
io.close(file)
local pid, stdin, stdout = lpc.run(params.cmd..'-restore',
unpack({test and '-t' or nil}))
stdout:close()
self:dumpfile(family, stdin)
stdin:close()
assert(lpc.wait(pid) == 0)
disabled = false
elseif test then
io.stderr:write('Warning: '..family..' rules not tested\n')
end
end
if disabled then awall.uerror.raise('Firewall not enabled in kernel') end
end
function BaseIPTables:activate()
flush()
self:restore(false)
end
function BaseIPTables:test() self:restore(true) end
IPTables = class(BaseIPTables)
function IPTables:init()
self.config = {}
setmetatable(self.config,
{__index=function(t, k)
t[k] = {}
setmetatable(t[k], getmetatable(t))
return t[k]
end})
end
function IPTables:dumpfile(family, iptfile)
iptfile:write('# '..families[family].file..' generated by awall\n')
local tables = self.config[family]
for i, tbl in sortedkeys(tables) do
iptfile:write('*'..tbl..'\n')
local chains = tables[tbl]
for i, chain in sortedkeys(chains) do
local policy = '-'
if util.contains(builtin[tbl], chain) then
policy = tbl == 'filter' and 'DROP' or 'ACCEPT'
end
iptfile:write(':'..chain..' '..policy..' [0:0]\n')
end
for i, chain in sortedkeys(chains) do
for i, rule in ipairs(chains[chain]) do
iptfile:write('-A '..chain..' '..rule..'\n')
end
end
iptfile:write('COMMIT\n')
end
end
local Current = class(BaseIPTables)
function Current:dumpfile(family, iptfile)
local pid, stdin, stdout = lpc.run(families[family].cmd..'-save')
stdin:close()
for line in stdout:lines() do iptfile:write(line..'\n') end
stdout:close()
assert(lpc.wait(pid) == 0)
end
local Backup = class(BaseIPTables)
function Backup:dumpfile(family, iptfile)
for line in io.lines(backupdir..'/'..families[family].file) do
iptfile:write(line..'\n')
end
end
function backup()
Current.new():dump(backupdir)
end
function revert()
Backup.new():activate()
end
function flush()
local empty = IPTables.new()
for family, params in pairs(families) do
local success, lines = pcall(io.lines, params.procfile)
if success then
for tbl in lines do
for i, chain in ipairs(builtin[tbl]) do
empty.config[family][tbl][chain] = {}
end
end
end
end
empty:restore(false)
end
|