summaryrefslogtreecommitdiffstats
path: root/shorewall-model.lua
blob: fe9c18e642e482c40397dffc30ab7a26a3741874 (plain)
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
-- shorewall model methods
module (..., package.seeall)
require("format")
require("fs")
require("daemoncontrol")

local baseurl = "/etc/shorewall/"

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service ( self, state )
	return daemoncontrol.daemoncontrol("shorewall", state)
end

function check_config ()
	check = nil
	check = {}
	local f,err = io.popen("/bin/echo -n '>> Check starts at: ';/bin/date; /bin/echo; /etc/init.d/shorewall check; /bin/echo; /bin/echo -n '>> Check stops at: '; /bin/date;")
	check.result = f:read("*a")
	f:close()
	check["error"]=err
	return check
end

function getstatus ()
	local f,error = io.popen("/sbin/shorewall status")
	local fake = f:read("*l")
	local fake = f:read("*l")
	local programstatus = f:read("*l") or ""
	local programstate = f:read("*l") or ""
	f:close()
	local f,error = io.popen("/sbin/shorewall version")
	local programversion = "shorewall-" .. f:read("*l")
	f:close()
	return {version=programversion,status=programstatus,state=programstate}
end

function get_filelist ()
	local filepath = baseurl
	local listed_files = {}
	local k,v
	for name in posix.files(filepath) do
		if not string.match(name, "^%.") and not string.match(name, "^Makefile") then
			local filedetails = fs.stat(filepath .. name)
			table.insert ( listed_files , {name=name, path=filepath .. name, filedetails=filedetails} )
		end
	end
	table.sort(listed_files, function (a,b) return (a.name < b.name) end )
	return listed_files
end

function get_filedetails (self, name)
	local name = basename(name)
	local path = baseurl .. basename(name)
	local file_content = nil
	local available_files = get_filelist()
	for k,v in pairs(available_files) do
		if ( available_files[k].name == name ) then
			file_content = cfe{name=name, content=fs.read_file(path), details=fs.stat(path)}
		end
	end
	return file_content
end

function get_logfile ()
	local me = {}
	local cmdtxt = "cat /var/log/messages | grep Shorewall"
	local cmd, error = io.popen(cmdtxt ,r)
	local cmdoutput = cmd:read("*a")
	cmd:close()
	me.value = cmdoutput
	me.cmd = cmdtxt
	return me
end

function update_filecontent (self, name, modifications)
	path = baseurl .. basename(name)
	name=basename(name)
	local available_files = get_filelist()
	for k,v in pairs(available_files) do
		if ( available_files[k].name == name ) then
			local file = io.open( path, "w+" )
			local file_result,err = file:write(format.dostounix(modifications))
			file:close()
			if (err ~= nil) then
				local filedetails = fs.stat(path)
				file_content = {name=name, value=file_result, filedetails=filedetails, err=err}
			end
		end
	end
	return name, file_content
end