diff options
| author | Mika Havela <mika.havela@gmail.com> | 2008-04-08 15:13:54 +0000 | 
|---|---|---|
| committer | Mika Havela <mika.havela@gmail.com> | 2008-04-08 15:13:54 +0000 | 
| commit | 132a51a124fb1fa5e48f3b0982b9742572bc922c (patch) | |
| tree | 8f776bdec1d5228dad70a6ae9a87c02037c1e6cf | |
| parent | 4baa9420979051e89c0758fd66ce71f0179b1532 (diff) | |
| download | acf-snort-132a51a124fb1fa5e48f3b0982b9742572bc922c.tar.bz2 acf-snort-132a51a124fb1fa5e48f3b0982b9742572bc922c.tar.xz | |
Cleaning up code.
git-svn-id: svn://svn.alpinelinux.org/acf/snort/trunk@962 ab2d0c66-481e-0410-8bed-d214d4d58bed
| -rw-r--r-- | snort-controller.lua | 91 | ||||
| -rw-r--r-- | snort-expert-html.lsp | 79 | ||||
| -rw-r--r-- | snort-model.lua | 47 | ||||
| -rw-r--r-- | 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 <PRE> ... </PRE> in BLACK text +		errtxt="",	--Content of this variable is displayed as <PRE> ... </PRE> 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 @@ -<? local view = ... ?> +<? local form = ...  +require("viewfunctions") +?>  <?  --[[ DEBUG INFORMATION  io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(view)) +io.write(html.cfe_unpack(form))  io.write("</span>")  --]]  ?> -<? -function displayinfo(myform,tags,viewonly) -	for k,v in pairs(tags) do  -		if (myform[v]) and (myform[v]["value"]) then -			local val = myform[v]  -			io.write("\t<DT") -			if (#val.errtxt > 0) then  -				val.class = "error" -				io.write(" class='error'") -			end -			io.write(">" .. val.label .. "</DT>\n") -			if (viewonly) then -				io.write("\t\t<DD>" .. val.value .. "\n") -			else -				io.write("\t\t<DD>" .. html.form[val.type](val) .. "\n") -			end -			if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end -			if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end -			io.write("\t\t</DD>\n") -		end -	end -end -?>  <H1>SYSTEM INFO</H1>  <DL>  <?  -local myform = view.status  -local tags = { "status", "version", } +local myform = form.status  +local tags = { "status", "version", "autostart", }  displayinfo(myform,tags,"viewonly")  ?>  </DL> @@ -45,7 +24,7 @@ displayinfo(myform,tags,"viewonly")  <h3>File details</h3>  <DL>  <?  -local myform = view.file  +local myform = form.file   local tags = { "filename", "filesize", "mtime", "sumerrors", }  displayinfo(myform,tags,"viewonly")  ?> @@ -53,7 +32,7 @@ displayinfo(myform,tags,"viewonly")  <H3>FILE CONTENT</H3>  <?  -local myform = view.file +local myform = form.file  io.write(html.form[myform.filecontent.type](myform.filecontent))  ?> @@ -65,38 +44,14 @@ displayinfo(myform,tags)  ?>  </DL> -</form> - -<? -- MANAGEMENT BUTTONS -local cmdform = view.management -local cmdresult = view.cmdmanagement -local tags = { "start", "stop", "restart" } -if (cmdform) and (cmdform[tags[1]]) then - -	io.write('<form name="management" action="" method="POST">') -	io.write('<H1>MANAGEMENT</H1>') -	io.write('<dl>') -	io.write('<dt>' .. cmdform[tags[1]]["label"] .. '</dt>') -	io.write('<dd>') -	for k,v in pairs(tags) do -		if (cmdform[v]) then -			io.write(html.form[cmdform[v].type](cmdform[v])) -		end -	end -	io.write('</dd>') - -	if (cmdresult) and (cmdresult.action) and (#cmdresult.action.descr > 0) then -		io.write('<dt>' .. cmdresult.label .. '</dt>') -		io.write('<dd><pre>' .. cmdresult.action.descr .. '</pre></dd>') -	end -	io.write('</dl></form>') -end ?> -  <? ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] +-- Management buttons +local myform = form.management +local tags = { "start", "stop", "restart" } +if (myform) then +	io.write("<H1>MANAGEMENT</H1>\n<DL>") +	displaymanagement(myform,tags) +	io.write("</DL>") +end  ?> - +</form> 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 @@ -<? local view = ... ?> - +<? local form = ...  +require("viewfunctions") +?>  <? -function displayinfo(myform,tags,viewonly) -	for k,v in pairs(tags) do  -		if (myform[v]) and (myform[v]["value"]) then -			local val = myform[v]  -			io.write("\t<DT") -			if (#val.errtxt > 0) then  -				val.class = "error" -				io.write(" class='error'") -			end -			io.write(">" .. val.label .. "</DT>\n") -			if (viewonly) then -				io.write("\t\t<DD>" .. val.value .. "\n") -			else -				io.write("\t\t<DD>" .. html.form[val.type](val) .. "\n") -			end -			if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end -			if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end -			io.write("\t\t</DD>\n") -		end -	end -end +--[[ DEBUG INFORMATION +io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") +io.write(html.cfe_unpack(form)) +io.write("</span>") +--]]  ?>  <H1>SYSTEM INFO</H1>  <DL>  <?  -local myform = view.status  +local myform = form.status   local tags = { "status", "version", "autostart", }  displayinfo(myform,tags,"viewonly")  ?> @@ -36,25 +21,25 @@ displayinfo(myform,tags,"viewonly")  <H2>PROGRAM SPECIFIC OPTIONS/INFORMATION</H2>  <DL>  <dt>Counted alerts</dt> -<dd><?= view.alerts ?> alert(s)</dd> +<dd><?= form.alerts ?> alert(s)</dd>  </DL>  <h1>ALERT LIST</h1> -<? for i=1, table.maxn(view.alertresult) do ?> -	<h2><?= view.alertresult[i]["name"] ?></h2> -	<? for j in pairs(view.alertresult[i]["value"]) do ?> +<? for i=1, table.maxn(form.alertresult) do ?> +	<h2><?= form.alertresult[i]["name"] ?></h2> +	<? for j in pairs(form.alertresult[i]["value"]) do ?>  		<h3><?= j ?></h3> -		<? for k in pairs(view.alertresult[i]["value"][j]) do ?> -			<p><B><?= view.alertresult[i]["value"][j][k]["value"][1] ?></B><BR> -			<? for l=2, table.maxn(view.alertresult[i]["value"][j][k]["value"]) do ?> -				<?= view.alertresult[i]["value"][j][k]["value"][l] ?><BR> +		<? for k in pairs(form.alertresult[i]["value"][j]) do ?> +			<p><B><?= form.alertresult[i]["value"][j][k]["value"][1] ?></B><BR> +			<? for l=2, table.maxn(form.alertresult[i]["value"][j][k]["value"]) do ?> +				<?= form.alertresult[i]["value"][j][k]["value"][l] ?><BR>  			<? end ?> -			<I>(This alarm is repeated <B><?= view.alertresult[i]["value"][j][k]["count"] ?></B> times)</I> -			<? if (table.maxn(view.alertresult[i]["value"][j][k]["url"]) ~= 0) then ?> +			<I>(This alarm is repeated <B><?= form.alertresult[i]["value"][j][k]["count"] ?></B> times)</I> +			<? if (table.maxn(form.alertresult[i]["value"][j][k]["url"]) ~= 0) then ?>  				( URL's: -				<? for q=1, table.maxn(view.alertresult[i]["value"][j][k]["url"]) do ?> -					<a href="<?= view.alertresult[i]["value"][j][k]["url"][q] ?>" target="_new"><img src="/static/tango/16x16/categories/applications-internet.png" alt="<?= view.alertresult[i]["value"][j][k]["url"][q] ?>"></a> +				<? for q=1, table.maxn(form.alertresult[i]["value"][j][k]["url"]) do ?> +					<a href="<?= form.alertresult[i]["value"][j][k]["url"][q] ?>" target="_new"><img src="/static/tango/16x16/categories/applications-internet.png" alt="<?= form.alertresult[i]["value"][j][k]["url"][q] ?>"></a>  				<? end ?>  			)  			<? end ?> @@ -63,11 +48,3 @@ displayinfo(myform,tags,"viewonly")  	<? end ?>  <? end ?> - -<? ---[[ DEBUG INFORMATION -require("debugs") -io.write(debugs.variables(view)) ---]] -?> - | 
