From 132a51a124fb1fa5e48f3b0982b9742572bc922c Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Tue, 8 Apr 2008 15:13:54 +0000 Subject: Cleaning up code. git-svn-id: svn://svn.alpinelinux.org/acf/snort/trunk@962 ab2d0c66-481e-0410-8bed-d214d4d58bed --- snort-controller.lua | 91 ++++++++++++++++++++++++++------------------------- snort-expert-html.lsp | 79 ++++++++++---------------------------------- snort-model.lua | 47 ++++++++------------------ snort-status-html.lsp | 65 ++++++++++++------------------------ 4 files changed, 98 insertions(+), 184 deletions(-) diff --git a/snort-controller.lua b/snort-controller.lua index 926bab3..bc49d4a 100644 --- a/snort-controller.lua +++ b/snort-controller.lua @@ -1,32 +1,18 @@ module (..., package.seeall) + +-- Load libraries require("posix") --- Cause an http redirect to our "read" action --- 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) + +-- ################################################################################ +-- LOCAL FUNCTIONS + +local function list_redir(self) self.conf.action = "status" self.conf.type = "redir" error (self.conf) end -mvc={} -mvc.on_load = function(self, parent) - if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then - self.worker[self.conf.action] = list_redir(self) - 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) +local function displaycmdmanagement(pidofstatus) -- Add a management buttons local management = {} management.start = cfe({ name="cmdmanagement", @@ -44,19 +30,38 @@ local function displaycmdmanagement(disablestart,disablestop,disablerestart) value="Restart", type="submit", }) + -- next CFE can be used to present the result of the previous action + management.actionresult = cfe({ name="actionresult", + label="Previous action result", + descr="", --Content of this variable is displayed as
 ... 
in BLACK text + errtxt="", --Content of this variable is displayed as
 ... 
in RED text + }) -- 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 + if (pidofstatus) then + management.start.disabled = "yes" + else + management.stop.disabled = "yes" + management.restart.disabled = "yes" + end return management end +-- ################################################################################ +-- PUBLIC FUNCTIONS + +mvc={} +function mvc.on_load(self, parent) + if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then + self.worker[self.conf.action] = list_redir(self) + end +end + function status(self) local srvcmdresult = nil local srvcmd = self.clientdata.srvcmd - local status = getstatus(self) + local status = self.model:getstatus(self) local alerts,alertresult = self.model:read_alert() return ({ @@ -76,7 +81,7 @@ function expert(self) end -- Start/Stop/Restart process - local cmdmanagement + local cmdmanagement, actionresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", @@ -86,10 +91,10 @@ function expert(self) 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 ) + actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) end - local status = getstatus(self) + local status = self.model:getstatus(self) local file = self.model:get_filedetails() -- Add buttons @@ -103,26 +108,22 @@ function expert(self) 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" + -- Management buttons (Hide/show buttons + local pidofstatus + if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end + management = displaycmdmanagement(pidofstatus) + if (actionresult) then + management.actionresult.descr=cmdmanagement.descr + management.actionresult.errtxt=cmdmanagement.errtxt + end + if (status) and (status.version) and (#status.version.value == 0) then + management.start.disabled = "yes" + management.stop.disabled = "yes" + management.restart.disabled = "yes" end - -- Display management buttons - local management = displaycmdmanagement(disablestart,disablestop,disablerestart) - - - --- if ( cmd ~= nil ) then --- startstop = self.model:startstop_service( cmd ) --- end return ( { management = management, - cmdmanagement = cmdmanagement, status = status, file = file, modifications = modifications, diff --git a/snort-expert-html.lsp b/snort-expert-html.lsp index 2217089..2e4dc31 100644 --- a/snort-expert-html.lsp +++ b/snort-expert-html.lsp @@ -1,40 +1,19 @@ - + DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(view)) +io.write(html.cfe_unpack(form)) io.write("
") --]] ?> - 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

@@ -45,7 +24,7 @@ displayinfo(myform,tags,"viewonly")

File details

@@ -53,7 +32,7 @@ displayinfo(myform,tags,"viewonly")

FILE CONTENT

@@ -65,38 +44,14 @@ displayinfo(myform,tags) ?>
- - -') - 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 ?> - DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] +-- Management buttons +local myform = form.management +local tags = { "start", "stop", "restart" } +if (myform) then + io.write("

MANAGEMENT

\n
") + displaymanagement(myform,tags) + io.write("
") +end ?> - + diff --git a/snort-model.lua b/snort-model.lua index 8a4450a..f933283 100644 --- a/snort-model.lua +++ b/snort-model.lua @@ -1,46 +1,29 @@ -- acf model for displaying logfiles recusivly module (..., package.seeall) --- no initializer in model - use controller.init for that - +-- Load libraries require("fs") require("posix") require("procps") require("daemoncontrol") require("format") +require("processinfo") +-- Set variables +local packagename = "snort" local processname = "snort" local configfile = "/etc/snort/snort.conf" -- ################################################################################ -- LOCAL FUNCTIONS -local function get_version() - local cmd_output_result, cmd_output_error - local cmd = "/sbin/apk_version -vs " .. processname .." 2>/dev/null" - local f = io.popen( cmd ) - local cmdresult = f:read("*l") - if (cmdresult) and (#cmdresult > 0) then - cmd_output_result = string.match(cmdresult,"^%S*") or "Unknown" +local function process_status_text(procname) + local t = procps.pidof(procname) + if (t) and (#t > 0) then + return "Enabled" else - cmd_output_error = "Program not installed" - end - f:close() - return cmd_output_result,cmd_output_error -end - -local function autostarts() - local cmd_output_result, cmd_output_error - local cmd = "/sbin/rc_status | egrep '^S' | egrep '" .. processname .."' 2>/dev/null" - local f = io.popen( cmd ) - local cmdresult = f:read("*a") - if (cmdresult) and (#cmdresult > 0) then - cmd_output_result = "Process will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')" - else - cmd_output_error = "Not programmed to autostart" - end - f:close() - return cmd_output_result,cmd_output_error + return "Disabled" + end end -- ################################################################################ @@ -49,20 +32,19 @@ end function getstatus () local status = {} - local value, errtxt = get_version() + local value, errtxt = processinfo.package_version(packagename) status.version = cfe({ name = "version", label="Program version", value=value, errtxt=errtxt, }) - status.status = cfe({ - name="status", + status.status = cfe({ name="status", label="Program status", - value=procps.pidof(processname) or "", + value=process_status_text(processname), }) - local autostart_sequense, autostart_errtxt = autostarts() + local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname) status.autostart = cfe({ name="autostart", label="Autostart sequence", value=autostart_sequense, @@ -139,7 +121,6 @@ function startstop_service ( self, action ) 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 diff --git a/snort-status-html.lsp b/snort-status-html.lsp index 9233f84..2efe386 100644 --- a/snort-status-html.lsp +++ b/snort-status-html.lsp @@ -1,33 +1,18 @@ - - + 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 +--[[ DEBUG INFORMATION +io.write("

DEBUGGING

DEBUG INFO: CFE

") +io.write(html.cfe_unpack(form)) +io.write("
") +--]] ?>

SYSTEM INFO

@@ -36,25 +21,25 @@ displayinfo(myform,tags,"viewonly")

PROGRAM SPECIFIC OPTIONS/INFORMATION

Counted alerts
-
alert(s)
+
alert(s)

ALERT LIST

- -

- + +

+

- -


- -
+ +


+ +
- (This alarm is repeated times) - + (This alarm is repeated times) + ( URL's: - - " target="_new"><?= view.alertresult[i]["> + + " target="_new"><?= form.alertresult[i]["> ) @@ -63,11 +48,3 @@ displayinfo(myform,tags,"viewonly") - - - -- cgit v1.2.3