summaryrefslogtreecommitdiffstats
path: root/shorewall-controller.lua
blob: 1c87fbe469703e9bb4d0180ae33df94ba5d45a8e (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module(..., package.seeall)

local list_redir = function(self)
    self.conf.action = "read"
    self.conf.type = "redir"
    error(self.conf)
end

mvc={}
mvc.on_load = function(self, parent)
    --TODO: This needs to be looked at 
    --there has to be cute, standard way of loading models into controller
    self.cfgfile = self:soft_require("cfgfile-model")
    setmetatable(self.cfgfile, self.cfgfile)
    self.cfgfile.__index = self.worker
    self.service = self:soft_require("service-model")
    setmetatable(self.service, self.service)
    self.service.__index = self.worker
    if (self.worker[self.conf.action] == nil) or (self.conf.action == "init") then
        self.worker[self.conf.action] = list_redir(self)
    end
end

-- Public methods
-- <prefix>/hostname/get

local function getNotes(self)
    ret = {}
    for k,v in pairs(self.cfgfile:list(nil)) do
        if v.status then
	    ret[#ret + 1] = {
		content = "There are some configuration changes. Please do not forget to save."
	    }
	    break
	end
    end
    return ret
end

read = function(self)
    return {
        list=self.cfgfile:list(function(x) return x.app == "firewall" end),
        script=ENV["SCRIPT_NAME"],
        prefix=self.conf.prefix,
        controller=self.conf.controller,
        action="update",
	note=getNotes(self),
   }
end

update = function(self)
    local id = tonumber(self.clientdata.id) or -1
    local result
    local data

    result, data = self.cfgfile:get(id)
    if not result then return list_redir(self) end

    if self.clientdata.cmd then
        for k,v in pairs (data) do
            if self.clientdata[k] then 
                data[k].value = self.clientdata[k]
            end
        end
        result, data = self.cfgfile:set(id, data)
	if result then return list_redir(self) end
    end
                
    data.cmd = cfe { type="action", value="save", label="action" }
    return cfe{ type="form", 
        option={ script=ENV["SCRIPT_NAME"],
            prefix=self.conf.prefix,
            controller = self.conf.controller,
            action = "update",
            extra = ""},
        value = data}
end

local function service(self, action)
    local id = tonumber(self.clientdata.id) or -1
    local svc = self.service:list("firewall")
    local ret = {
        script=ENV["SCRIPT_NAME"],
        prefix=self.conf.prefix,
        controller = self.conf.controller,
        action={},
	title="Firewall",
	text={},
	active={ id=id }
    }
    for i,s in pairs(svc) do
        for i,a in ipairs(s.actions) do
	    ret.action[#ret.action + 1] = {
                name = a,
		section = s.name .. " (" .. tostring(s.status) .. ")",
		id = s.id,
		label = a,
	    }
	end
    end
    if self.clientdata[action] then
        local result, report = self.service:update(id, action)
	local label = "Error"
	if result then
	    ret.active.action = action
            label = "Report"
	end
	ret.text[#ret.text + 1] = { label=label, content=report }
    end
    ret.note=getNotes(self)
    return ret
end

start = function(self) return service(self, "start") end
stop = function(self) return service(self, "stop") end
restart = function(self) return service(self, "restart") end