From f14ce45bcdcbbe99cb5693966019d0deb39371e5 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Mon, 3 Mar 2008 14:55:02 +0000 Subject: Some changes to get 'save' button to work in expert-tab git-svn-id: svn://svn.alpinelinux.org/acf/snort/trunk@785 ab2d0c66-481e-0410-8bed-d214d4d58bed --- snort-controller.lua | 105 +++++++++++++++++++++++++++++++++++++------ snort-expert-html.lsp | 120 +++++++++++++++++++++++++++++--------------------- snort-model.lua | 59 ++++++++++++++++++++----- snort-status-html.lsp | 42 +++++++++++++----- 4 files changed, 242 insertions(+), 84 deletions(-) diff --git a/snort-controller.lua b/snort-controller.lua index 19d55ea..926bab3 100644 --- a/snort-controller.lua +++ b/snort-controller.lua @@ -4,7 +4,7 @@ require("posix") -- We use the self.conf table because it already has prefix,controller,etc -- The redir code is defined in the application error handler (acf-controller) local list_redir = function (self) - self.conf.action = "read" + self.conf.action = "status" self.conf.type = "redir" error (self.conf) end @@ -16,36 +16,115 @@ mvc.on_load = function(self, parent) end end +local function getstatus(self) + local status = self.model.getstatus() + if (#status.status.value > 0) then + status.status.value = "Enabled" + else + status.status.value = "Disabled" + end + return status +end + +local function displaycmdmanagement(disablestart,disablestop,disablerestart) + -- Add a management buttons + local management = {} + management.start = cfe({ name="cmdmanagement", + label="Program control-panel", + value="Start", + type="submit", + }) + management.stop = cfe({ name="cmdmanagement", + label="Program control-panel", + value="Stop", + type="submit", + }) + management.restart = cfe({ name="cmdmanagement", + label="Program control-panel", + value="Restart", + type="submit", + }) + + -- Disable management buttons based on if the process is running or not + if (disablestart) then management.start.disabled = "yes" end + if (disablestop) then management.stop.disabled = "yes" end + if (disablerestart) then management.restart.disabled = "yes" end + + return management +end + function status(self) local srvcmdresult = nil local srvcmd = self.clientdata.srvcmd - if (srvcmd ~= nil) then - srvcmdresult = self.model:startstop_service(srvcmd) - end + local status = getstatus(self) + local alerts,alertresult = self.model:read_alert() - return ({status = self.model:getstatus(), - srvcmdresult=srvcmdresult, + return ({ + status = status, alerts=alerts, alertresult=alertresult, url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller} ) end function expert(self) - local modifications = self.clientdata.modifications or "" local cmd = self.clientdata.cmd local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller - if ( modifications ~= "") then + local modifications = self.clientdata.filecontent or "" + if ( self.clientdata.cmdsave ) then modifications = self.model:update_filecontent(modifications) end - if ( cmd ~= nil ) then - startstop = self.model:startstop_service( cmd ) + -- Start/Stop/Restart process + local cmdmanagement + if ( self.clientdata.cmdmanagement) then + cmdmanagement = cfe({ + name="cmdmanagement", + label="Previous action result", + action=cfe({ + name="cmdmanagement", + value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) + }), + }) + local actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) + end + + local status = getstatus(self) + local file = self.model:get_filedetails() + + -- Add buttons + file.cmdsave = cfe ({ + name="cmdsave", + label="Apply settings", + value="Apply", + type="submit", + }) + if (self.clientdata.cmdsave) then + file.cmdsave.descr="* Changes has been saved!" + end + + -- Management buttons + local disablestart,disablestop,disablerestart + -- Disable management buttons based on if the process is running or not + if (string.lower(status.status.value) == "enabled" ) then + disablestart = "yes" + else + disablestop = "yes" end + -- Display management buttons + local management = displaycmdmanagement(disablestart,disablestop,disablerestart) + + + +-- if ( cmd ~= nil ) then +-- startstop = self.model:startstop_service( cmd ) +-- end - return ( {startstop = startstop, - status = self.model:getstatus(), - file = self.model:get_filedetails(), + return ( { + management = management, + cmdmanagement = cmdmanagement, + status = status, + file = file, modifications = modifications, url = url, } ) end diff --git a/snort-expert-html.lsp b/snort-expert-html.lsp index ab2ee56..2217089 100644 --- a/snort-expert-html.lsp +++ b/snort-expert-html.lsp @@ -6,69 +6,91 @@ io.write(html.cfe_unpack(view)) io.write("") --]] ?> -

SYSTEM INFO

- -
-
Program status -
-
+ 0) then + val.class = "error" + io.write(" class='error'") + end + io.write(">" .. val.label .. "\n") + if (viewonly) then + io.write("\t\t
" .. val.value .. "\n") + else + io.write("\t\t
" .. html.form[val.type](val) .. "\n") + end + if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

\n") end + if (#val.errtxt > 0) then io.write("\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

\n") end + io.write("\t\t
\n") + end + end +end +?> -
-
Program version
-
-
+

SYSTEM INFO

+
+ +
+

CONFIGURATION

-

Expert config

-

File details

-
-
File name
-
+
-
-
File size
-
-
+

FILE CONTENT

+ +

SAVE AND APPLY ABOVE SETTINGS

-
Last modified
-
+
-

File content

- - - - - -

Save and apply above settings

-
-
Apply settings
-
-
-

MANAGEMENT

- -
-
Program controll-panel
-
- - - -
-
- - -
-
Previous action result
-
-
- +') + io.write('

MANAGEMENT

') + io.write('
') + io.write('
' .. cmdform[tags[1]]["label"] .. '
') + io.write('
') + for k,v in pairs(tags) do + if (cmdform[v]) then + io.write(html.form[cmdform[v].type](cmdform[v])) + end + end + io.write('
') + + if (cmdresult) and (cmdresult.action) and (#cmdresult.action.descr > 0) then + io.write('
' .. cmdresult.label .. '
') + io.write('
' .. cmdresult.action.descr .. '
') + end + io.write('
') +end ?> &1 | grep Version | sed 's/.*ersion\ /snort-/'" + local cmd = "/sbin/apk_version -v -s " .. processname .. " | cut -d ' ' -f 1" local cmd_output = io.popen( cmd ) local cmd_output_result = cmd_output:read("*a") or "" cmd_output:close() @@ -29,22 +28,60 @@ end function getstatus () local status = {} - status["version"] = string.match(get_version(), "^(%S*)" ) - status["enabled"] = procps.pidof("snort") + + status.version = cfe({ + name = "version", + label="Program version", + value=get_version(), + }) + + status.status = cfe({ + name="status", + label="Program status", + value=procps.pidof(processname) or "", + }) + return status end function get_filedetails() - local filedetails = {} local path = configfile - filedetails.details = fs.stat(path) - filedetails.content = fs.read_file(path) - return filedetails + local filedetails = fs.stat(path) + local file = {} + + file["filename"] = cfe({ + name="filename", + label="File name", + value=path, + }) + file["filesize"] = cfe({ + name="filesize", + label="File size", + value=filedetails.size or 0, + }) + file["mtime"] = cfe({ + name="mtime", + label="File date", + value=filedetails.mtime or "---", + }) + file["filecontent"] = cfe({ + type="longtext", + name="filecontent", + label="File content", + value=fs.read_file(path), + }) + return file end -function startstop_service ( self, state ) - local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, state) - return cmdmessage + +-- action should be a CFE +function startstop_service ( self, action ) + local cmd = action.value + local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol("snort", cmd) + action.descr=cmdmessage + action.errtxt=cmderror + -- Reporting back (true|false, the original acition) + return cmdresult,action end function read_alert() diff --git a/snort-status-html.lsp b/snort-status-html.lsp index 5c35e30..c7c8cef 100644 --- a/snort-status-html.lsp +++ b/snort-status-html.lsp @@ -1,17 +1,37 @@ -

SYSTEM INFO

- -
-
Program status
-
-
- -
-
Program version
-
-
+ 0) then + val.class = "error" + io.write(" class='error'") + end + io.write(">" .. val.label .. "\n") + if (viewonly) then + io.write("\t\t
" .. val.value .. "\n") + else + io.write("\t\t
" .. html.form[val.type](val) .. "\n") + end + if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

\n") end + if (#val.errtxt > 0) then io.write("\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

\n") end + io.write("\t\t
\n") + end + end +end +?> +

SYSTEM INFO

+
+ +

PROGRAM SPECIFIC OPTIONS/INFORMATION

-- cgit v1.2.3