weblog-model.lua 58.7 KB
Newer Older
Ted Trask's avatar
Ted Trask committed
1 2 3 4 5 6 7 8
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
require("fs")
require("format")
require("validator")
require("luasql.postgres")
9
require("date")
Ted Trask's avatar
Ted Trask committed
10

Ted Trask's avatar
Ted Trask committed
11
local DatabaseName = "weblog"
Ted Trask's avatar
Ted Trask committed
12 13 14 15 16 17
local DatabaseOwner = "weblogowner"
local DatabaseUser = "webloguser"

local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
local env
local con
18
local configfile = "/etc/weblog/weblog.conf"
Ted Trask's avatar
Ted Trask committed
19 20
local configcontent = fs.read_file(configfile) or ""
local config = format.parse_ini_file(configcontent, "") or {}
21 22
local goodwordslist = "/etc/weblog/goodwords"
local badwordslist = "/etc/weblog/badwords"
Ted Trask's avatar
Ted Trask committed
23 24
local ignorewordslist = "/etc/weblog/ignorewords"
local files = {badwordslist, goodwordslist, ignorewordslist, configfile}
Ted Trask's avatar
Ted Trask committed
25 26

local database_creation_script = {
27
	"CREATE TABLE dbhistlog (logdatetime timestamp(3) without time zone NOT NULL, msgtext text)",
28 29
	"CREATE TABLE pubweblog(sourcename character varying(40), clientip inet NOT NULL, clientuserid character varying(64) NOT NULL, logdatetime timestamp(3) without time zone NOT NULL, uri text NOT NULL, bytes bigint NOT NULL, reason text, score integer, shortreason text, badyesno int, deniedyesno int, bypassyesno int, wordloc text, goodwordloc text, selected boolean, id serial)",
	"CREATE TABLE pubweblog_history(sourcename character varying(40), clientip inet NOT NULL, clientuserid character varying(64) NOT NULL, logdatetime timestamp(3) without time zone NOT NULL, uri text NOT NULL, bytes bigint NOT NULL, reason text, score integer, shortreason text, badyesno int, deniedyesno int, bypassyesno int, wordloc text, goodwordloc text, selected boolean, id int)",
Ted Trask's avatar
Ted Trask committed
30
	"CREATE TABLE source (sourcename character varying(40) NOT NULL, method character varying(100) NOT NULL, userid character varying(32), passwd character varying(255), source character varying(255) NOT NULL, tzislocal boolean, enabled boolean)",
31 32
	"CREATE TABLE usagestat(sourcename character varying(40) NOT NULL, date timestamp(0) without time zone NOT NULL, numrequest integer, numblock integer)",
	"CREATE TABLE watchlist(clientuserid character varying(64) NOT NULL, expiredatetime timestamp(0) without time zone NOT NULL)",
Ted Trask's avatar
Ted Trask committed
33 34 35 36 37
	"ALTER TABLE ONLY source ADD CONSTRAINT source_pkey PRIMARY KEY (sourcename)",
	"CREATE INDEX dbhistlogdatetimeidx ON dbhistlog USING btree (logdatetime)",
	"CREATE INDEX pubweblogclientdateidx ON pubweblog USING btree (logdatetime, clientuserid)",
	"CREATE INDEX pubweblogclientuserididx ON pubweblog USING btree (clientuserid)",
	"CREATE INDEX pubwebloglogdatetimeidx ON pubweblog USING btree (logdatetime)",
38 39 40
	"CREATE INDEX pubweblog_historyclientdateidx ON pubweblog_history USING btree (logdatetime, clientuserid)",
	"CREATE INDEX pubweblog_historyclientuserididx ON pubweblog_history USING btree (clientuserid)",
	"CREATE INDEX pubweblog_historylogdatetimeidx ON pubweblog_history USING btree (logdatetime)",
41 42
	"GRANT SELECT ON dbhistlog TO "..DatabaseUser,
	"GRANT SELECT ON pubweblog TO "..DatabaseUser,
43
	"GRANT SELECT ON pubweblog_history TO "..DatabaseUser,
44 45 46
	"GRANT SELECT, UPDATE, INSERT, DELETE ON source TO "..DatabaseUser,
	"GRANT SELECT ON usagestat TO "..DatabaseUser,
	"GRANT SELECT, UPDATE, INSERT, DELETE ON watchlist TO "..DatabaseUser,
Ted Trask's avatar
Ted Trask committed
47 48 49 50 51 52 53 54 55 56 57 58 59
}

-- ################################################################################
-- DATABASE FUNCTIONS

local function assert (v, m)
	if not v then
		m = m or "Assertion failed!"
		error(m, 0)
	end
	return v, m
end

60 61
-- Escape special characters in sql statements and truncate to length
local escape = function(sql, length)
Ted Trask's avatar
Ted Trask committed
62
	sql = sql or ""
63
	if length then sql = string.sub(sql, 1, length) end
64 65
	sql = string.gsub(sql, "'", "''")
	return string.gsub(sql, "\\", "\\\\")
Ted Trask's avatar
Ted Trask committed
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
end

-- List the postgres databases on this system
local listdatabases = function()
	local dbs = {}
	local cmd = path.."psql -U postgres -tl 2>&1"
	local f = io.popen(cmd)
	local result = f:read("*a") or ""
	f:close()
	for line in string.gmatch(result, "[^\n]+") do
		dbs[#dbs+1] = string.match(line, "^ (%S+)")
	end
	return dbs
end

-- Create the necessary database
local createdatabase = function(password)
	local result = {}

	-- First, create the users
	local cmd = path..'psql -U postgres -c "CREATE USER '..DatabaseOwner..' WITH PASSWORD \''..password..'\'" 2>&1'
	local f = io.popen(cmd)
	table.insert(result, f:read("*a"))
	f:close()
	cmd = path..'psql -U postgres -c "CREATE USER '..DatabaseUser..'" 2>&1'
	f = io.popen(cmd)
	table.insert(result, f:read("*a"))
	f:close()
Timo Teräs's avatar
Timo Teräs committed
94

Ted Trask's avatar
Ted Trask committed
95 96 97 98 99
	-- Create the database
	cmd = path..'psql -U postgres -c "CREATE DATABASE '..DatabaseName..' WITH OWNER '..DatabaseOwner..'" 2>&1'
	f = io.popen(cmd)
	table.insert(result, f:read("*a"))
	f:close()
Timo Teräs's avatar
Timo Teräs committed
100

Ted Trask's avatar
Ted Trask committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	return table.concat(result, "\n")
end

-- Delete the database and roles
local deletedatabase = function()
	local result = {}

	local cmd = path..'psql -U postgres -c "DROP DATABASE '..DatabaseName..'" 2>&1'
	local f = io.popen(cmd)
	table.insert(result, f:read("*a"))
	f:close()
	cmd = path..'psql -U postgres -c "DROP ROLE '..DatabaseUser..'" 2>&1'
	f = io.popen(cmd)
	table.insert(result, f:read("*a"))
	f:close()
	cmd = path..'psql -U postgres -c "DROP ROLE '..DatabaseOwner..'" 2>&1'
	f = io.popen(cmd)
	table.insert(result, f:read("*a"))
	f:close()
Timo Teräs's avatar
Timo Teräs committed
120

Ted Trask's avatar
Ted Trask committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	return table.concat(result, "\n")
end

-- Run an SQL script
local runSQLscript = function(filename)
	-- Create the database
	local cmd = path..'psql -U postgres -f "'..filename..'" '..DatabaseName..' 2>&1'
	local f = io.popen(cmd)
	local result = f:read("*a") or ""
	f:close()
	-- Create the tables
	print (result)
	return result
end

-- Create the database and tables
Ted Trask's avatar
Ted Trask committed
137
-- pg_dump -U postgres -c weblog > makeweblog.postgres
Ted Trask's avatar
Ted Trask committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
--runSQLscript("/root/work/weblog/makeweblog.postgres")

local databaseconnect = function(username, password)
	if not con then
		-- create environment object
		env = assert (luasql.postgres())
		-- connect to data source
		con = assert (env:connect(DatabaseName, username, password))
	end
end

local databasedisconnect = function()
	if env then
		env:close()
		env = nil
	end
	if con then
		con:close()
		con = nil
	end
end

local logme = function(message)
161
	local sql = string.format("INSERT INTO dbhistlog VALUES ('now', '%s')", escape(message))
Ted Trask's avatar
Ted Trask committed
162 163 164 165 166 167
	local res = assert (con:execute(sql))
end

local listhistorylogentries = function()
	local entries = {}
	-- retrieve a cursor
Timo Teräs's avatar
Timo Teräs committed
168
	cur = assert (con:execute"SELECT logdatetime, msgtext from dbhistlog ORDER BY logdatetime")
Ted Trask's avatar
Ted Trask committed
169 170 171 172 173 174 175 176 177 178
	row = cur:fetch ({}, "a")
	while row do
		entries[#entries+1] = {logdatetime = row.logdatetime, msgtext = row.msgtext}
		row = cur:fetch (row, "a")
	end
	-- close everything
	cur:close()
	return entries
end

179 180
local importsquidlog = function(entry, sourcename)
	if entry then
181
		local sql = string.format("INSERT INTO pubweblog VALUES ('%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s')",
182
			escape(sourcename), escape(entry.clientip), escape(entry.clientuserid, 64):lower(),
183
			escape(entry.logdatetime), escape(entry.URL), escape(entry.bytes), escape(entry.reason), escape(entry.score), escape(entry.shortreason), escape(entry.badyesno), escape(entry.deniedyesno), escape(entry.bypassyesno), escape(entry.wordloc), escape(entry.goodwordloc))
Ted Trask's avatar
Ted Trask committed
184
			
Ted Trask's avatar
Ted Trask committed
185 186 187 188
		local res = assert (con:execute(sql))
	end
end

Ted Trask's avatar
Ted Trask committed
189 190 191 192 193 194 195 196 197
local importsquarklog = function(entry, sourcename)
        if entry then
           local sql = string.format("INSERT INTO pubweblog VALUES ('%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s')",
	          escape(sourcename), escape(entry.clientip), escape(entry.clientuserid, 64):lower(),
                  escape(entry.logdatetime), escape(entry.URL), escape(entry.bytes), escape(entry.reason), escape(entry.score), escape(entry.shortreason), escape(entry.badyesno), escape(entry.deniedyesno), escape(entry.bypassyesno), escape(entry.wordloc), escape(entry.gwordloc))
           local res = assert (con:execute(sql))
        end
end

198 199
local importdglog = function(entry, sourcename)
	if entry then
200 201 202 203
           local sql = string.format("INSERT INTO pubweblog VALUES ('%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s')",
	          escape(sourcename), escape(entry.clientip), escape(entry.clientuserid, 64):lower(),
                  escape(entry.logdatetime), escape(entry.URL), escape(entry.bytes), escape(entry.reason), escape(entry.score), escape(entry.shortreason), escape(entry.badyesno), escape(entry.deniedyesno), escape(entry.bypassyesno), escape(entry.wordloc), escape(entry.gwordloc))
           local res = assert (con:execute(sql))
Ted Trask's avatar
Ted Trask committed
204
	end
205
end 
Ted Trask's avatar
Ted Trask committed
206 207 208 209 210 211 212 213
local importdumplog = function(entry, sourcename)
	if entry then
           local sql = string.format("INSERT INTO pubweblog VALUES ('%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s')",
	          escape(sourcename), escape(entry.clientip), escape(entry.clientuserid, 64):lower(),
                  escape(entry.logdatetime), escape(entry.URL), escape(entry.bytes), escape(entry.reason), escape(entry.score), escape(entry.shortreason), escape(entry.badyesno), escape(entry.deniedyesno), escape(entry.bypassyesno), escape(entry.wordloc), escape(entry.gwordloc))
           local res = assert (con:execute(sql))
	end
end 
Ted Trask's avatar
Ted Trask committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

local listsourceentries = function(sourcename)
	local sources = {}
	-- retrieve a cursor
	local sql = "SELECT sourcename, method, userid, passwd, source, tzislocal, enabled FROM source"
	if sourcename then
		sql = sql .. " WHERE sourcename='" .. escape(sourcename) .. "'"
	end
	sql = sql .. " ORDER BY sourcename"
	cur = assert (con:execute(sql))
	row = cur:fetch ({}, "a")
	while row do
		row.tzislocal = (row.tzislocal == "t")
		row.enabled = (row.enabled == "t")
		sources[#sources+1] = row
		row = cur:fetch ({}, "a")
	end
	cur:close()
	return sources
end

local importsourceentry = function(source)
	local sql = string.format("INSERT INTO source VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
		escape(source.sourcename), escape(source.method), escape(source.userid), escape(source.passwd),
		escape(source.source), escape(tostring(source.tzislocal):upper()), escape(tostring(source.enabled):upper()))
	local res = assert (con:execute(sql))
	return res
end

local updatesourceentry = function(source)
	local sql = string.format("UPDATE source SET method='%s', userid='%s', passwd='%s', source='%s', tzislocal='%s', enabled='%s' WHERE sourcename='%s'",
		escape(source.method), escape(source.userid), escape(source.passwd), escape(source.source),
		escape(tostring(source.tzislocal):upper()), escape(tostring(source.enabled):upper()),
		escape(source.sourcename))
	local res = assert (con:execute(sql))
	return res
end

local deletesourceentry = function(sourcename)
	local sql = string.format("DELETE FROM source WHERE sourcename='%s'", escape(sourcename))
	local res = assert (con:execute(sql))
	return res
end

-- Add blocked users (from blocklog) to the watchlist, removing old entries if updated.
local addtowatchlist = function()
	local watchdays = config.watchdays or 14
	local sql = "insert into watchlist select clientuserid, " ..
		"(max(logdatetime) + INTERVAL '"..watchdays.." days') as expiredatetime " ..
263
		"from pubweblog where bypassyesno > '0' group by clientuserid"
Ted Trask's avatar
Ted Trask committed
264 265 266 267 268 269 270 271 272 273 274 275
	local res1 = assert (con:execute(sql))
	sql = "delete from watchlist where exists " ..
		"(select * from watchlist w where w.clientuserid = watchlist.clientuserid " ..
		"and w.expiredatetime > watchlist.expiredatetime)"
	local res2 = assert (con:execute(sql))
	logme("There are now " .. (res1-res2) .. " new names in the watchlist")
end

-- Generate usage statistics from weblog and blocklog
local updateusagestats = function()
	-- update the usagestat table from weblog
	-- (will result in multiple rows where logs rotated on partial hours)
276 277 278
	local sql = "insert into usagestat select pubweblog.sourcename, " ..
		"date_trunc('hour', pubweblog.logdatetime) as date, " ..
		"count(*), SUM(deniedyesno) from pubweblog group by sourcename,date"
Ted Trask's avatar
Ted Trask committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	local res = assert (con:execute(sql))

end

-- Delete people from the watchlist when they have expired out
local groomwatchlist = function()
	local res = assert (con:execute("delete from watchlist where expiredatetime < now()"))
	logme("removed " .. res .. " names from watchlist")
end

-- Delete useage stats from more than a year ago
local groomusagestat = function()
	local res = assert (con:execute("delete from usagestat where " ..
		"date < (now() - INTERVAL '1 year')"))
	logme("removed " .. res .. " old usage status lines")
end

-- Delete history log information from more than a month ago
local groomdbhistlog = function()
	local res = assert (con:execute("delete from dbhistlog where " ..
		"logdatetime < (now() - INTERVAL '1 month')"))
	logme("removed " .. res .. " old dbhistlog lines")
end

-- Delete old junk from pub tables
local groompublogs = function()
	local purgedays = config.purgedays or 30
306 307 308
	--local watchdays = config.watchdays or 14
	--local historydays = config.historydays or 14
	
Ted Trask's avatar
Ted Trask committed
309 310 311
	local now = os.time()

	local temp = os.date("%Y-%m-%d %H:%M:%S", now - purgedays*86400)
312
	logme("Purgedate is " .. temp .. ". Nothing will exist in pubweblog beyond purgedate.")
Ted Trask's avatar
Ted Trask committed
313

314 315
	-- Move flagged records to histoy and then purge anything older than purgedate
	sql = "Insert into pubweblog_history select * from pubweblog where logdatetime < '" .. temp .."' and (badyesno > 0 or deniedyesno > 0 or bypassyesno > 0 or selected = 'true')"
Ted Trask's avatar
Ted Trask committed
316
	res = assert (con:execute(sql))
317 318 319
	logme("Moved " .. res .. " old records to history")
	
	sql = "Delete from pubweblog where logdatetime < '" .. temp .."'"
Ted Trask's avatar
Ted Trask committed
320
	res = assert (con:execute(sql))
321
	logme("Deleted " .. res .. " old records to from pubweblog")
Ted Trask's avatar
Ted Trask committed
322

Ted Trask's avatar
Ted Trask committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	-- purge anything older than startddate+historydays+watchdays
	--local temp = config.auditstart
	--if not temp or temp == "" then temp = os.date("%Y-%m-%d %H:%M:%S") end
	--logme("Purge date since last audit is " .. tostring(watchdays+historydays) .. " days before " .. temp .. ".")

	--sql = "delete from pubweblog where logdatetime < (timestamp '"..temp.."' - INTERVAL '"..tostring(watchdays+historydays).." days')"
	--res = assert (con:execute(sql))
        --logme("removed " .. res .. " old pubweblog records that are older than history+watchdays")

	-- purge good people after historydays
	--logme("The delete date for non-watchlist users is " .. tostring(historydays) .. " days before " .. temp .. ".")

	--sql = "delete from pubweblog where logdatetime < (timestamp '".. temp.."' - INTERVAL '"..tostring(historydays).." days') and clientuserid NOT IN (select clientuserid from watchlist)"
	--res = assert (con:execute(sql))
	--logme("removed " .. res .. " records for users not on the watchlist.")
Ted Trask's avatar
Ted Trask committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
end

local listwatchlistentries = function()
	local entries = {}
	-- retrieve a cursor
	cur = assert (con:execute("SELECT * from watchlist ORDER BY clientuserid"))
	row = cur:fetch ({}, "a")
	while row do
		entries[#entries+1] = {clientuserid = row.clientuserid, expiredatetime= row.expiredatetime}
		row = cur:fetch (row, "a")
	end
	-- close everything
	cur:close()
	return entries
end

local importwatchlistentry = function(clientuserid, expiredatetime)
	local sql = string.format("INSERT INTO watchlist VALUES ('%s', '%s')",
		escape(clientuserid), escape(expiredatetime))
	local res = assert (con:execute(sql))
	return res
end

local deletewatchlistentry = function(clientuserid)
	local sql = string.format("DELETE FROM watchlist WHERE clientuserid='%s'", escape(clientuserid))
	local res = assert (con:execute(sql))
	return res
end

367
local generatewhereclause = function(clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, selected)
Ted Trask's avatar
Ted Trask committed
368 369 370
	local sql = ""
	local where = {}
	if clientuserid and clientuserid ~= "" then
371
		where[#where+1] = "clientuserid LIKE '%"..escape(clientuserid).."%'"
Ted Trask's avatar
Ted Trask committed
372 373 374 375 376 377 378 379 380 381
	end
	if starttime and starttime ~= "" then
		where[#where+1] = "logdatetime >= '"..escape(starttime).."'"
	end
	if endtime and endtime ~= "" then
		where[#where+1] = "logdatetime <= '"..escape(endtime).."'"
	end
	if clientip and clientip ~= "" then
		where[#where+1] = "clientip = '"..escape(clientip).."'"
	end
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	if badyesno and badyesno ~= "" then
		where[#where+1] = "badyesno = '"..escape(badyesno).."'"
	end
	if deniedyesno and deniedyesno ~= "" then
		where[#where+1] = "deniedyesno = '"..escape(deniedyesno).."'"
	end
	if bypassyesno and bypassyesno ~= "" then
		where[#where+1] = "bypassyesno = '"..escape(bypassyesno).."'"
	end
	if score and score ~= "" then
		where[#where+1] = "score >= '"..escape(score).."'"
	end
	if urisearch and urisearch ~= "" then 
	    	where[#where+1] = "lower(uri) LIKE '%"..escape(urisearch).."%'"
	end
	
	if selected and selected == "true" then
		where[#where+1] = "selected = 'true'"
	end
	
Ted Trask's avatar
Ted Trask committed
402 403 404
	if #where > 0 then
		sql = " WHERE " .. table.concat(where, " AND ")
	end
405
	 
Ted Trask's avatar
Ted Trask committed
406 407 408
	return sql
end

409
local listlogentries = function(activelog, clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, sortby, selected)
Ted Trask's avatar
Ted Trask committed
410 411
	local entries = {}
	-- retrieve a cursor
412 413 414
	local sql = "SELECT * FROM "..activelog
	sql = sql .. generatewhereclause(clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, selected)
	sql = sql .. " ORDER BY "..sortby
Ted Trask's avatar
Ted Trask committed
415 416 417
	cur = assert (con:execute(sql))
	row = cur:fetch ({}, "a")
	while row do
Ted Trask's avatar
Ted Trask committed
418 419
		if config.shorturi == "true" then
			shorturi=string.gsub(row.uri, "[;?].*", "...")
420
		end
421
		entries[#entries+1] = {sourcename=row.sourcename, clientip=row.clientip, clientuserid=row.clientuserid, logdatetime=row.logdatetime, uri=row.uri, shorturi=shorturi, bytes=row.bytes, reason=row.reason, score=row.score, shortreason=row.shortreason, badyesno=row.badyesno, deniedyesno=row.deniedyesno, bypassyesno=row.bypassyesno, wordloc=row.wordloc, id=row.id, selected=row.selected }
422 423 424
		if (config.shortreason ~= "true") then
			entries[#entries].shortreason = nil
		end
Ted Trask's avatar
Ted Trask committed
425 426 427 428 429 430 431 432
		row = cur:fetch (row, "a")
	end
	-- close everything
	cur:close()
	return entries
end

local listpubweblogentries = function(...)
433
	return listlogentries(...)
Ted Trask's avatar
Ted Trask committed
434 435 436 437 438 439
end

local listusagestats = function()
	local entries = {}
	-- retrieve a cursor
	local sql = "SELECT sourcename, date, sum(numrequest) AS numrequest, sum(numblock) AS numblock " ..
440
		"FROM usagestat GROUP BY sourcename, date ORDER BY date, sourcename"
Ted Trask's avatar
Ted Trask committed
441 442 443 444 445 446 447 448 449 450 451
	cur = assert (con:execute(sql))
	row = cur:fetch ({}, "a")
	while row do
		entries[#entries+1] = {sourcename=row.sourcename, date=row.date, numrequest=row.numrequest, numblock=row.numblock}
		row = cur:fetch (row, "a")
	end
	-- close everything
	cur:close()
	return entries
end

452 453 454 455 456 457 458 459 460 461 462 463 464 465
local testdatabaseentry = function(datatype, value)
	local success = true
	local errtxt
	local sql = "CREATE TEMP TABLE testing ( test "..escape(datatype).." DEFAULT '"..escape(value).."' ) ON COMMIT DROP"
	local res, err = pcall(function()
		assert (con:execute(sql))
	end)
	if not res then
		success = false
		errtxt = string.gsub(err or "", "\n.*", "")
	end
	return success, errtxt
end

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
local convertdatabaseentry = function(datatype, value)
	local success = true
	local errtxt
	local result = value
	local res, err = pcall(function()
		local sql = "CREATE TEMP TABLE testing ( test "..escape(datatype).." )"
		assert (con:execute(sql))
		sql = "INSERT INTO testing VALUES ('"..value.."')"
		assert (con:execute(sql))
		sql = "SELECT * FROM testing"
		local cur = assert (con:execute(sql))
		local row = cur:fetch ({}, "a")
		if row then
			result = row.test
		end
	end)
	if not res then
		success = false
		errtxt = string.gsub(err or "", "\n.*", "")
	end
	local res, err = pcall(function()
		local sql = "DROP TABLE testing"
		assert (con:execute(sql))
	end)
	return success, errtxt, result
end

Ted Trask's avatar
Ted Trask committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
local printtableentries = function(tablename)
	-- retrieve a cursor
	local count = 0
	cur = assert (con:execute("SELECT * from "..tablename))
	-- print all rows, the rows will be indexed by field names
	row = cur:fetch ({}, "a")
	while row do
		count = count + 1
		for name,val in pairs(row) do
			APP.logevent(name.." = "..val..", ")
		end
		row = cur:fetch (row, "a")
	end
	-- close everything
	cur:close()
	APP.logevent("Table "..tablename.." contains "..count.." rows")
end

-- ################################################################################
-- LOG FILE FUNCTIONS

514
local function parsesquidlog(line)
515 516 517
	-- Format of squid log (space separated):
	-- time elapsed remotehost code/status bytes method URL rfc931 peerstatus/peerhost
	local words = {}
518
	
519 520 521
	for word in string.gmatch(line, "%S+") do
		words[#words+1] = word
	end
522 523 524 525 526 527 528 529 530 531 532
	
	local goodwordloc=""
	local badwordloc=""
	-- logme("value of word7 is "..words[7])
	local wrdcnt=0
	local isbad=0
	local isdenied=0
	local isbypass=0
	local ignoreme=false
	
	--check for ignored records first
Ted Trask's avatar
Ted Trask committed
533
	for thisline in io.lines(ignorewordslist) do
534 535 536
		if not thisline then
			break
		end
Ted Trask's avatar
Ted Trask committed
537
		_,instcnt = string.lower(words[7]):gsub(thisline, " ")
538 539 540 541 542 543 544
		if instcnt ~= 0 then
		   ignoreme = true
		   break
		end
	end

	if ignoreme ~= true then
Ted Trask's avatar
Ted Trask committed
545
	 --proceed with record analysis
Ted Trask's avatar
Ted Trask committed
546
	  for thisline in io.lines(badwordslist) do
547 548 549 550
		if not thisline then
			break
		end
		
Ted Trask's avatar
Ted Trask committed
551 552
		_,instcnt = string.lower(words[7]):gsub(thisline, " ")  
		--if string.find(words[7],thisline) ~= nil then
553
		if instcnt ~= 0 then
Ted Trask's avatar
Ted Trask committed
554
		 -- logme("instcnt =  "..instcnt)
555 556 557 558 559 560 561 562 563 564
		  isbad=1
		  wrdcnt= wrdcnt + instcnt 
		  if badwordloc ~= "" then 
		    badwordloc = badwordloc.."|"..thisline
		  else
		    badwordloc=thisline
		  end
		 
	        end
		
Ted Trask's avatar
Ted Trask committed
565 566 567 568 569 570 571 572 573 574
		if string.find(words[7],"*DENIED*") then
		   logme("*Denied*")
		   isdenied=1
		elseif string.find(words[7],"GBYPASS") then
		   logme("GBYPASS")
		   isbypass=1
		elseif string.find(words[7],"*OVERRIDE*") then
		   logme("*OVERRIDE*")
		   isbypass=1
		end
575
	  end
Ted Trask's avatar
Ted Trask committed
576
	  for goodline in io.lines(goodwordslist) do
577 578 579
		if not goodline then
		  break
		end
Ted Trask's avatar
Ted Trask committed
580
		_,instcnt = string.lower(words[7]):gsub(goodline, " ")
581 582 583 584 585 586 587 588 589 590 591 592 593
		--if string.find(words[7],goodline) then
		if instcnt ~= 0 then
		  if wrdcnt ~= 0 then
		     wrdcnt = wrdcnt - instcnt
		     if goodwordloc ~= "" then
		        goodwordloc = goodwordloc.."|"..goodline
		     else 
		        goodwordloc = goodline
		     end
		  end
		end
	  end	
	end
Ted Trask's avatar
Ted Trask committed
594 595 596 597 598
	-- Reset bad to reduce number of bad hits if score is zero
	-- if wrdcnt == 0 then
	-- isbad=0
	-- end
		
599 600 601
	local logentry = {logdatetime=words[1],
		elapsed=words[2],
		clientip=words[3],
Ted Trask's avatar
Ted Trask committed
602 603
		code=string.match(words[4] or "", "^[^/]*"),
		status=string.match(words[4] or "", "[^/]*$"),
604
		bytes=words[5],
Ted Trask's avatar
Ted Trask committed
605
		method=words[6],
606 607
		URL=words[7],
		clientuserid=words[8],
Ted Trask's avatar
Ted Trask committed
608
		peerstatus=string.match(words[9] or "", "^[^/]*"),
609 610 611 612 613 614 615
		peerhost=string.match(words[9] or "", "[^/]*$"),
		score=wrdcnt,
		badyesno=isbad,
		deniedyesno=isdenied,
		bypassyesno=isbypass,		
		wordloc=badwordloc,
		gwordloc=goodwordloc}	
616

Ted Trask's avatar
Ted Trask committed
617 618
	-- Don't care about local requests (from DG) (this check also removes blank lines)
	if logentry.clientip and logentry.clientip ~= "127.0.0.1" then
619
	   if logentry.clientuserid and logentry.clientuserid ~= "-" then
Ted Trask's avatar
Ted Trask committed
620
		logentry.logdatetime = os.date("%Y-%m-%d %H:%M:%S", logentry.logdatetime)..string.match(logentry.logdatetime, "%..*")
621
		return logentry
622
           end
Ted Trask's avatar
Ted Trask committed
623
	end
624
	return nil
Ted Trask's avatar
Ted Trask committed
625 626
end

Ted Trask's avatar
Ted Trask committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
local function parsesquarklog(line)
	-- Format of squid log (space separated):
	-- time elapsed remotehost code/status bytes method URL rfc931 peerstatus/peerhost
	local words = {}
	
	for word in string.gmatch(line, "%S+") do
		words[#words+1] = word
	end

	local goodwordloc=""
	local badwordloc=""
	local wrdcnt=0
	local isbad=0
	local isdenied=0
	local isbypass=0
	local ignoreme=false
	
	--check for ignored records first
Ted Trask's avatar
Ted Trask committed
645
	for thisline in io.lines(ignorewordslist) do
Ted Trask's avatar
Ted Trask committed
646 647 648 649 650 651 652 653 654 655 656
		if not thisline then
			break
		end
		_,instcnt = string.lower(words[5]):gsub(thisline, " ")
		if instcnt ~= 0 then
		   ignoreme = true
		   break
		end
	end

 	if ignoreme ~= true then	
Ted Trask's avatar
Ted Trask committed
657
	  for thisline in io.lines(badwordslist) do
Ted Trask's avatar
Ted Trask committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		if not thisline then
			-- logme("This line is apparently empty...")
			break
		end
		
		_,instcnt = string.lower(words[5]):gsub(thisline, " ")
	--	if string.find(words[5],thisline) ~= nil then
		--logme("checking "..thisline.." against "..words[5])
		if instcnt ~= 0 then
		  isbad=1
		  wrdcnt = wrdcnt + instcnt
		if badwordloc ~= "" then 
		    badwordloc = badwordloc.."|"..thisline
		else
		    badwordloc=thisline
		end
		 
		  -- logme("bad "..badwordloc)
	        end
	
		if string.find(words[6],"*DENIED*") then	
		   isdenied=1
		end
		if string.find(words[6],"*OVERRIDE*") then
		   isbypass=1
		end
	end
Ted Trask's avatar
Ted Trask committed
685
	for goodline in io.lines(goodwordslist) do
Ted Trask's avatar
Ted Trask committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
		if not goodline then
		  -- logme("This line is apparently empty...")
		  break
		end
		_,instcnt = string.lower(words[5]):gsub(goodline, " ")
		--if string.find(words[4],goodline) then
		  if instcnt ~= 0 then
		   if wrdcnt ~= 0 then
		     wrdcnt = wrdcnt - instcnt
		     if goodwordloc ~= "" then
		        goodwordloc = goodwordloc.."|"..goodline
		     else 
		        goodwordloc = goodline
		     end
		  end
		end
	  end
	end
	
	local words = format.string_to_table(line, "\t")	
	local logentry = {logdatetime=words[1],
			clientuserid=words[2],
			clientip=words[3],
			URL=words[4],
			reason=words[5],
			method=words[6],
			bytes=words[7],
			shortreason=words[9],
			score=wrdcnt,
			badyesno=isbad,
			deniedyesno=isdenied,
			bypassyesno=isbypass,		
			wordloc=badwordloc,
			gwordloc=goodwordloc}	

	if logentry.reason and logentry.reason ~= "" then
           	if logentry.shortreason == "" then
           		logentry.shortreason = logentry.reason
           	end
	   	return logentry
	end
	return nil
end

730
local function parsedglog(line)
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	-- Format of squid log (space separated):
	-- time elapsed remotehost code/status bytes method URL rfc931 peerstatus/peerhost
	local words = {}
	
	for word in string.gmatch(line, "%S+") do
		words[#words+1] = word
	end

	local goodwordloc=""
	local badwordloc=""
	-- logme("value of word4 is "..words[4])
	local wrdcnt=0
	local isbad=0
	local isdenied=0
	local isbypass=0
	local ignoreme=false

	--check for ignored records first
Ted Trask's avatar
Ted Trask committed
749
	for thisline in io.lines(ignorewordslist) do
750 751
		if not thisline then
			break
Ted Trask's avatar
Ted Trask committed
752
		end
Ted Trask's avatar
Ted Trask committed
753
		_,instcnt = string.lower(words[4]):gsub(thisline, " ")
754 755 756 757 758 759 760
		if instcnt ~= 0 then
		   ignoreme = true
		   break
		end
	end

	if ignoreme ~= true then
Ted Trask's avatar
Ted Trask committed
761
	  for thisline in io.lines(badwordslist) do
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
		if not thisline then
			-- logme("This line is apparently empty...")
			break
		end
		
		_,instcnt = string.lower(words[4]):gsub(thisline, " ")
		--if string.find(words[4],thisline) ~= nil then
		  if instcnt ~= 0 then
		   if wrdcnt ~= 0 then
		     isbad=1
		     wrdcnt= wrdcnt + instcnt
		     if badwordloc ~= "" then 
		       badwordloc = badwordloc.."|"..thisline
		     else
		       badwordloc=thisline
		     end
		 
		  -- logme("bad "..badwordloc)
	        end
		
		if string.find(words[5],"*DENIED*") then
		   isdenied=1
		elseif string.find(words[5],"GBYPASS") then
		   isdenied=1 
		elseif string.find(words[5],"*OVERRIDE*") then
		   isbypass=1
		end
	  end
Ted Trask's avatar
Ted Trask committed
790
	  for goodline in io.lines(goodwordslist) do
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
		if not goodline then
		  -- logme("This line is apparently empty...")
		  break
		end
		_,instcnt = string.lower(words[4]):gsub(goodline, " ")
		--if string.find(words[4],goodline) then
		  
		  if instcnt ~= 0 then
		     wrdcnt = wrdcnt - instcnt
		     if goodwordloc ~= "" then
		        goodwordloc = goodwordloc.."|"..goodline
		     else 
		        goodwordloc = goodline
		     end
		  end
		end
	  end	
	
	end
	
	local words = format.string_to_table(line, "\t")	
	local logentry = {logdatetime=words[1],
			clientuserid=words[2],
			clientip=words[3],
			URL=words[4],
			reason=words[5],
			method=words[6],
			bytes=words[7],
			shortreason=words[9],
			score=wrdcnt,
			badyesno=isbad,
			deniedyesno=isdenied,
			bypassyesno=isbypass,		
			wordloc=badwordloc,
			gwordloc=goodwordloc}	

	if logentry.reason and logentry.reason ~= "" then
           	if logentry.shortreason == "" then
           		logentry.shortreason = logentry.reason
           	end
	   	return logentry
Ted Trask's avatar
Ted Trask committed
832
	end
833
	return nil
Ted Trask's avatar
Ted Trask committed
834 835
end

Ted Trask's avatar
Ted Trask committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849
local function parsedumplog(line)
	-- Format of squid log (space separated):
	-- time elapsed remotehost code/status bytes method URL rfc931 peerstatus/peerhost
	local words = {}
	
	for word in string.gmatch(line, "%S+") do
		words[#words+1] = word
	end
	goodwordloc=""
	badwordloc=""
	wrdcnt=0
	isbad=0
	isdenied=0
	isbypass=0
Ted Trask's avatar
Ted Trask committed
850
	for thisline in io.lines(badwordslist) do
Ted Trask's avatar
Ted Trask committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
		if not thisline then
			 logme("This line is apparently empty...")
			break
		end
		_,instcnt = string.lower(words[5]):gsub(thisline, " ")
		if instcnt ~= 0 then
		  isbad=1
		  wrdcnt = wrdcnt + instcnt
		  if badwordloc ~= "" then 
		    badwordloc = badwordloc.."|"..thisline
		  else
		    badwordloc=thisline
		  end
		 
		   -- logme("bad "..badwordloc)
	        end
		if string.find(words[6],"*DENIED*") then	
		   isdenied=1
		end
		if string.find(words[5],"GBYPASS") then
		   isbypass=1
		elseif string.find(words[6],"*OVERRIDE*") then
		   isbypass=1
		end
	end
Ted Trask's avatar
Ted Trask committed
876
	for goodline in io.lines(goodwordslist) do
Ted Trask's avatar
Ted Trask committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
		if not goodline then
		  -- logme("This line is apparently empty...")
		  break
		end
		_,instcnt = string.lower(words[4]):gsub(goodline, " ")
		--if string.find(words[4],goodline) then
		  if instcnt ~= 0 then
		   if wrdcnt ~= 0 then
		     wrdcnt = wrdcnt - instcnt
		     if goodwordloc ~= "" then
		        goodwordloc = goodwordloc.."|"..goodline
		     else 
		        goodwordloc = goodline
		     end
		  end
		end
	end	
	
	local words = format.string_to_table(line, "\t")	
	local logentry = {logdatetime=words[1],
			clientuserid=words[2],
			clientip=words[3],
			URL=words[4],
			reason=words[6],
			method=words[5],
			bytes=words[7],
			shortreason=words[9],
			score=wrdcnt,
			badyesno=isbad,
			deniedyesno=isdenied,
			bypassyesno=isbypass,		
			wordloc=badwordloc,
			gwordloc=goodwordloc}	

	if logentry.reason and logentry.reason ~= "" then
           	if logentry.shortreason == "" then
           		logentry.shortreason = logentry.reason
           	end
	   	return logentry
	end
	return nil
end

--local function parsedglog(line)
--	local words = format.string_to_table(line, "\t")
--	local logentry = { logdatetime=words[1], clientuserid=words[2], clientip=words[3],
--		URL=words[4], reason=words[5], method=words[6], bytes=words[7],
--		shortreason=words[9]}
--	if logentry.reason and logentry.reason ~= "" then
--		if logentry.shortreason == "" then
--			logentry.shortreason = logentry.reason
--		end
--		logentry.score = string.match(logentry.reason, "^.*: ([0-9]+) ")
--		logentry.logdatetime = string.gsub(logentry.logdatetime, "%.", "-")
--		return logentry
--	end
--	return nil
--end


Ted Trask's avatar
Ted Trask committed
937 938 939 940 941 942 943 944 945
-- ################################################################################
-- DOWNLOAD FILE FUNCTIONS

-- must do apk_add wget first

local connecttosource = function(source, cookiesfile)
	local success = false
	logme("Connecting to source "..source.sourcename)
	if source.method == "http" or source.method == "https" then
946 947
		fs.write_file(cookiesfile, "password="..source.passwd.."&userid="..source.userid.."&Logon=Logon")
		local cmd = "wget -O - --no-check-certificate --save-cookies "..cookiesfile.." --keep-session-cookies --post-file '"..cookiesfile.."' '"..source.method.."://"..format.escapespecialcharacters(source.source).."/cgi-bin/acf/acf-util/logon/logon' 2>/dev/null"
Ted Trask's avatar
Ted Trask committed
948 949 950 951 952 953 954 955 956 957
		local f = io.popen(cmd)
		local resultpage = f:read("*a")
		f:close()
		if resultpage == "" then
			logme("Failed to connect to "..source.sourcename)
		elseif string.find(resultpage, "Log in") then
			logme("Failed to log in to "..source.sourcename)
		else
			success = true
		end
958 959
	elseif source.method == "local" then
		success = true
Ted Trask's avatar
Ted Trask committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973
	end
	return success
end

local getlogcandidates = function(source, cookiesfile)
	local candidates = {}
	if source.method == "http" or source.method == "https" then
		local cmd = "wget -O - --no-check-certificate --load-cookies "..cookiesfile.." '"..source.method.."://"..source.source.."/cgi-bin/acf/alpine-baselayout/logfiles/status' 2>/dev/null"
		local f = io.popen(cmd)
		local resultpage = f:read("*a")
		f:close()
		for file in string.gmatch(resultpage, "download%?name=([^\"]+)") do
			candidates[#candidates+1] = file
		end
974 975
	elseif source.method == "local" then
		candidates = fs.find_files_as_array(nil, source.source)
Ted Trask's avatar
Ted Trask committed
976 977 978 979
	end
	return candidates
end

980 981
local openlogfile = function(source, cookiesfile, logfile)
	local handle
Ted Trask's avatar
Ted Trask committed
982 983
	if source.method == "http" or source.method == "https" then
		local cmd = "wget -O - --no-check-certificate --load-cookies "..cookiesfile.." --post-data 'name="..logfile.."' '"..source.method.."://"..source.source.."/cgi-bin/acf/alpine-baselayout/logfiles/download' 2>/dev/null"
984 985 986
		if string.find(logfile, "%.gz$") then
			cmd = cmd.." | gunzip -c"
		end
987
		handle = io.popen(cmd)
988
	elseif source.method == "local" then
989 990
		if string.find(logfile, "%.gz$") then
			local cmd = "gunzip -c "..logfile
991
			handle = io.popen(cmd)
992
		else
993
			handle = io.open(logfile)
994
		end
Ted Trask's avatar
Ted Trask committed
995
	end
996
	return handle
Ted Trask's avatar
Ted Trask committed
997 998 999 1000 1001 1002 1003
end

local deletelogfile = function(source, cookiesfile, logfile)
	if source.method == "http" or source.method == "https" then
		local cmd = "wget -O - --no-check-certificate --load-cookies "..cookiesfile.." --post-data 'name="..logfile.."' '"..source.method.."://"..source.source.."/cgi-bin/acf/alpine-baselayout/logfiles/delete' 2>/dev/null"
		local f = io.popen(cmd)
		f:close()
1004 1005
	elseif source.method == "local" then
		os.remove(logfile)
Ted Trask's avatar
Ted Trask committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	end
end

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

function getsourcelist()
	local retval = cfe({ type="list", value={}, label="Weblog Source List" })
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		retval.value = listsourceentries()
		databasedisconnect()
	end)
	if not res then
		retval.errtxt = err
	end

	return retval
end

function getsource(sourcename)
	local sourcedata = getnewsource()
	sourcedata.value.sourcename.value = sourcename
	sourcedata.value.sourcename.errtxt = "Source name does not exist"

	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		local sourcelist = listsourceentries()
		databasedisconnect()
		for i,source in ipairs(sourcelist) do
			if source.sourcename == sourcename then
				sourcedata.value.sourcename.errtxt = nil
				for name,val in pairs(source) do
					if sourcedata.value[name] then
						sourcedata.value[name].value = val
					end
				end
				break
			end
		end
	end)
	if not res then
		sourcedata.errtxt = err
	end

	return sourcedata
end

local validatesource = function(sourcedata)
	local success = modelfunctions.validateselect(sourcedata.value.method)
1056 1057 1058 1059 1060 1061
	local test = {"sourcename", "source"}
	if sourcedata.value.method.value ~= "local" then
		test[#test+1] = "userid"
		test[#test+1] = "passwd"
	end
	for i,name in ipairs(test) do
Ted Trask's avatar
Ted Trask committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
		if sourcedata.value[name].value == "" then
			sourcedata.value[name].errtxt = "Cannot be empty"
			success = false
		end
	end

	return success
end

function updatesource(sourcedata)
	local success = validatesource(sourcedata)
	sourcedata.errtxt = "Failed to update source"
	if success then
		local source = {}
		for name,val in pairs(sourcedata.value) do
			source[name] = val.value
		end

		local res, err = pcall(function()
			databaseconnect(DatabaseUser)
			sourcedata.descr = updatesourceentry(source)
			databasedisconnect()
			sourcedata.errtxt = nil
		end)
		if not res and err then
			sourcedata.errtxt = sourcedata.errtxt .. "\n" .. err
		end
	end

	return sourcedata
end

function getnewsource()
	local source = {}
	source.sourcename = cfe({ label="Source Name" })
1097
	source.method = cfe({ type="select", value="local", label="Method", option={"http", "https", "local"} })
Ted Trask's avatar
Ted Trask committed
1098 1099
	source.userid = cfe({ label="UserID" })
	source.passwd = cfe({ label="Password" })
1100
	source.source = cfe({ value="/var/log", label="Source Location / Address" })
Ted Trask's avatar
Ted Trask committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	source.tzislocal = cfe({ type="boolean", value=false, label="Using local timezone" })
	source.enabled = cfe({ type="boolean", value=false, label="Enabled" })
	return cfe({ type="group", value=source, label="Source" })
end

function createsource(sourcedata)
	local success = validatesource(sourcedata)
	sourcedata.errtxt = "Failed to create source"
	if success then
		local source = {}
		for name,val in pairs(sourcedata.value) do
			source[name] = val.value
		end
1114 1115
		-- remove spaces from sourcename
		source.sourcename = string.gsub(source.sourcename, "%s+$", "")
Ted Trask's avatar
Ted Trask committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

		local res, err = pcall(function()
			databaseconnect(DatabaseUser)
			sourcedata.descr = importsourceentry(source)
			databasedisconnect()
			sourcedata.errtxt = nil
		end)
		if not res and err then
			sourcedata.errtxt = sourcedata.errtxt .. "\n" .. err
		end
	else
	end

	return sourcedata
end

function deletesource(sourcename)
	local result = cfe({ errtxt="Failed to delete source", label="Delete source result" })
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		local number = deletesourceentry(sourcename)
		databasedisconnect()
		if number > 0 then
			result.errtxt = nil
			result.value = "Source Deleted"
		end
	end)
	if not res and err then
		result.errtxt = result.errtxt .. "\n" .. err
	end
	return result
end

function testsource(sourcename)
	local result = cfe({ label="Test source result" })

	-- temporary override of logme function to capture messages to result.value
	result.value = {}
	local temp = logme
	logme = function(message) table.insert(result.value, message) end

	local cookiesfile = "/tmp/cookies-"..tostring(os.time())
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		local sources = listsourceentries(sourcename)
		databasedisconnect()
		if #sources < 1 then
			result.errtxt = "Failed to find source"
		else
			local source = sources[1]
			-- run the test
			if connecttosource(source, cookiesfile) then
				local files = getlogcandidates(source, cookiesfile)
				if #files == 0 then
					logme("No log files found")
				else
					for i,file in ipairs(files) do
						logme("Found log file "..file)
					end
				end
			end
		end
	end)
	if not res then
		result.errtxt = "Failed to connect to source"
		if err then
			result.errtxt = result.errtxt .. "\n" .. err
		end
	end
	os.remove(cookiesfile)

	-- fix the result
	result.value = table.concat(result.value, "\n") or ""
	logme = temp

	return result
end

1194 1195
-- import either squid or dg log file.
-- delete logfile after
1196
local function importlogfile(source, cookiesfile, file, parselog_func, importlog_func)
Ted Trask's avatar
Ted Trask committed
1197
	logme("Getting " .. file )
1198
	local loghandle = openlogfile(source, cookiesfile, file)
Ted Trask's avatar
Ted Trask committed
1199
	logme("Processing " .. file )
1200 1201 1202
	local res, err = pcall(function()
		con:execute("START TRANSACTION")
		for line in loghandle:lines() do
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
			assert(con:execute("SAVEPOINT before_line"))
			local res2, err2 = pcall(function()
				local logentry = parselog_func(line)
				importlog_func(logentry, source.sourcename)
			end)
			if not res2 then
				if (config.stoponerror == "true") then
					pcall(function() con:execute("ROLLBACK") end)
				else
					assert(con:execute("ROLLBACK TO before_line"))
1213
					con:execute("COMMIT")
1214 1215 1216
				end
				pcall(function() logme("Exception on line:"..line) end)
				if err2 then
1217
					pcall(function() logme("err2 "..err2) end)
1218 1219 1220
				end
				if (config.stoponerror == "true") then
					assert(res2, "Import halted on exception")
1221 1222
				else
					con:execute("START TRANSACTION")
1223
				end
1224 1225
			else
				assert(con:execute("RELEASE SAVEPOINT before_line"))
1226
			end
1227 1228 1229 1230 1231 1232 1233 1234
		end
		con:execute("COMMIT")
	end)
	if not res then
		pcall(function() con:execute("ROLLBACK") end)
		if err then
			pcall(function() logme(err) end)
		end
1235
	end
1236
	loghandle:close()
1237 1238 1239 1240 1241
	if res then
		logme("Deleting " .. file )
		deletelogfile(source, cookiesfile, file)
	end
	return res
1242 1243
end

Ted Trask's avatar
Ted Trask committed
1244 1245 1246
function importlogs()
	local result = cfe({ label="Weblog Import Logs Result" })
	local count = 0
1247
	local success = true
Ted Trask's avatar
Ted Trask committed
1248 1249 1250 1251 1252

	local res, err = pcall(function()
		databaseconnect(DatabaseOwner, config.password)

		-- Download, parse, and import the logs
1253 1254 1255 1256 1257 1258 1259 1260
		logme("Executing importlogs")
		logme("Analyzing...")
		local sql = "ANALYZE"
		res = assert (con:execute(sql))
		
		-- Determine sources
		local sources = listsourceentries(sourcename)
		
Ted Trask's avatar
Ted Trask committed
1261 1262 1263 1264 1265 1266 1267
		for i,source in ipairs(sources) do
			if source.enabled then
				logme("Getting logs from source " .. source.sourcename)
				local cookiesfile = "/tmp/cookies-"..tostring(os.time())
				if connecttosource(source, cookiesfile) then
					local files = getlogcandidates(source, cookiesfile)
					for j,file in ipairs(files) do
1268
						if string.match(file, "dansguardian/access%.log[%.%-]") then
Ted Trask's avatar
Ted Trask committed
1269
							count = count + 1
1270
							success = importlogfile(source, cookiesfile, file, parsedglog, importdglog) and success
1271
						end
Ted Trask's avatar
Ted Trask committed
1272 1273 1274 1275
						if string.match(file, "squark/access%.log[%.%-]") then
							count = count + 1
							success = importlogfile(source, cookiesfile, file, parsesquarklog, importsquarklog) and success
						end
1276
						if string.match(file, "squid/access%.log[%.%-]") then
Ted Trask's avatar
Ted Trask committed
1277
							count = count + 1
1278
							success = importlogfile(source, cookiesfile, file, parsesquidlog, importsquidlog) and success
Ted Trask's avatar
Ted Trask committed
1279
						end
Ted Trask's avatar
Ted Trask committed
1280 1281 1282 1283
						if string.match(file, "dump/access%.log[%.%-]") then
							count = count + 1
							success = importlogfile(source, cookiesfile, file, parsedumplog, importdumplog) and success
						end
Ted Trask's avatar
Ted Trask committed
1284 1285 1286 1287 1288 1289 1290
					end
				end
				os.remove(cookiesfile)
			end
		end

		-- Process the logs
1291 1292 1293 1294 1295
		if success then
			addtowatchlist()
			updateusagestats()
			groomwatchlist()
		end
Ted Trask's avatar
Ted Trask committed
1296 1297 1298 1299 1300 1301 1302
		-- Purge old database entries
		groomusagestat()
		groomdbhistlog()
		groompublogs()

		databasedisconnect()
	end)
1303
	if not res or not success then
Ted Trask's avatar
Ted Trask committed
1304 1305 1306 1307 1308
		result.errtxt = "Import Logs Failure"
		if err then
			pcall(function() logme(err) end)
			result.errtxt = result.errtxt .. "\n" .. err
		end
1309
		pcall(function() databasedisconnect() end)
Ted Trask's avatar
Ted Trask committed
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	end

	result.value = "Imported "..tostring(count).." logs"

	return result
end

function getactivitylog()
	local retval = cfe({ type="list", value={}, label="Weblog Activity Log" })
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		retval.value = listhistorylogentries() or {}
		databasedisconnect()
	end)
	if not res then
		retval.errtxt = err
	end

	return retval
end

function getwatchlist()
	local retval = cfe({ type="list", value={}, label="Weblog Watchlist" })
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		retval.value = listwatchlistentries() or {}
		databasedisconnect()
	end)
	if not res then
		retval.errtxt = err
	end

	return retval
end

function getnewwatchlistentry()
	local watch = {}
	watch.clientuserid = cfe({ label="User ID" })
	local watchdays = config.watchdays or 14
	watch.expiredatetime = cfe({ value=os.date("%Y-%m-%d %H:%M:%S", os.time() + watchdays*86400), label="Expiration Date" })

	return cfe({ type="group", value=watch, label="Watchlist Entry" })
end

1354
local function validatewatchlistentry(watch)
Ted Trask's avatar
Ted Trask committed
1355 1356 1357 1358 1359 1360 1361
	local success = true
	for i,name in ipairs({"clientuserid", "expiredatetime"}) do
		if watch.value[name].value == "" then
			watch.value[name].errtxt = "Cannot be empty"
			success = false
		end
	end
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	if not watch.value.expiredatetime.errtxt then
		local res, err = pcall(function()
			databaseconnect(DatabaseUser)
			local s
			s,watch.value.expiredatetime.errtxt = testdatabaseentry("TIMESTAMP", watch.value.expiredatetime.value)
			databasedisconnect()
			success = success and s
		end)
		if not res and err then
			watch.value.expiredatetime.errtxt = err
			success = false
		end
	end
	return success
end

function createwatchlistentry(watch)
Ted Trask's avatar
Ted Trask committed
1379 1380
	watch.value.clientuserid.value = watch.value.clientuserid.value:lower()
	watch.errtxt = "Failed to create watchlist entry"
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	local success = validatewatchlistentry(watch)
	if success then
		local res, err = pcall(function()
			databaseconnect(DatabaseUser)
			local watchlist = listwatchlistentries() or {}
			for i,w in ipairs(watchlist) do
				if w.clientuserid == watch.value.clientuserid.value then
					watch.value.clientuserid.errtxt = "User ID already exists"
					success = false
					break
				end
Ted Trask's avatar
Ted Trask committed
1392
			end
1393 1394 1395 1396 1397
			if success then
				local count = importwatchlistentry(watch.value.clientuserid.value, watch.value.expiredatetime.value)
				if count > 0 then
					watch.errtxt = nil
				end
Ted Trask's avatar
Ted Trask committed
1398
			end
1399 1400 1401 1402
			databasedisconnect()
		end)
		if not res and err then
			watch.errtxt = watch.errtxt .. "\n" .. err
Ted Trask's avatar
Ted Trask committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
		end
	end

	return watch
end

function deletewatchlistent(clientuserid)
	local result = cfe({ errtxt="Failed to delete watchlist entry", label="Delete watchlist entry result" })
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		local number = deletewatchlistentry(clientuserid)
		databasedisconnect()
		if number > 0 then
			result.value = "Watchlist entry deleted"
			result.errtxt = nil
		end
	end)
	if not res and err then
		result.errtxt = result.errtxt .. "\n" .. err
	end
Timo Teräs's avatar
Timo Teräs committed
1423

Ted Trask's avatar
Ted Trask committed
1424 1425 1426
	return result
end

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
local validateparameters = function(params)
	local success = true
	if params.clientip.value ~= "" and string.find(params.clientip.value, "[^%d%.]") then
		params.clientip.errtxt = "Invalid IP Address"
		success = false
	end
	if params.window.value ~= "" and not validator.is_integer(params.window.value) then
		params.window.errtxt = "Must be an integer"
		success = false
	end
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		local s
		if params.starttime.value ~= "" then
1441
			s,params.starttime.errtxt,params.starttime.value = convertdatabaseentry("TIMESTAMP", params.starttime.value)
1442 1443 1444
			success = success and s
		end
		if params.endtime.value ~= "" then
1445
			s,params.endtime.errtxt,params.endtime.value = convertdatabaseentry("TIMESTAMP", params.endtime.value)
1446 1447 1448
			success = success and s
		end
		if params.focus.value ~= "" then
1449
			s,params.focus.errtxt,params.focus.value = convertdatabaseentry("TIMESTAMP", params.focus.value)
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
			success = success and s
		end
		databasedisconnect()
	end)
	if not res and err then
		params.starttime.errtxt = err
		params.endtime.errtxt = err
		params.focus.errtxt = err
		success = false
	end
	return success
end

1463
local handleparameters = function(activelog, clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, sortby, selected, focus)
Ted Trask's avatar
Ted Trask committed
1464
	local result = {}
1465
	result.activelog = cfe({ value=activelog or "pubweblog", label="Active Weblog" })
Ted Trask's avatar
Ted Trask committed
1466 1467 1468 1469
	result.clientuserid = cfe({ value=clientuserid or "", label="User ID" })
	result.starttime = cfe({ value=starttime or "", label="Start Time" })
	result.endtime = cfe({ value=endtime or "", label="End Time" })
	result.clientip = cfe({ value=clientip or "", label="Client IP" })
1470 1471 1472 1473 1474 1475 1476
	result.badyesno = cfe({ value=badyesno, label="Show Dodgy Records", descr="Limit search to Dodgy records"})
	result.deniedyesno = cfe({ value=deniedyesno, label="Show Denied Records", descr="Limit search to Denied uri"})
	result.bypassyesno = cfe({ value=bypassyesno, label="Show Bypass Actions", descr="Limit search to Bypass attempts"})
	result.score = cfe({ value=score, label="Minimum Score", descr="Minimum score to search on"})
	result.urisearch = cfe({ value=urisearch or "", label="URI Contains", descr="Retrieve records where the URI contains this word"})
	result.sortby = cfe({ value=sortby, label="Sort By field", descr="Sort by this field when displaying records"})
	result.selected = cfe({ value=selected, label="Show Selected Records", descr="Show only records that have been selected"})
Ted Trask's avatar
Ted Trask committed
1477
	result.window = cfe({ value=config.window or "5", label="Time Window" })
1478
	result.focus = cfe({ value=focus or "", label="Focus Time" })
Ted Trask's avatar
Ted Trask committed
1479 1480 1481
	return result
end

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
function getselected(csvdata)
	local result = {}
	result.id = cfe({ value=csvdata or "", label="Record ID", descr="Id of Record"})
        result.log = cfe({ type="list", value={}, label="Weblog Access Log" })
        local res, err = pcall(function()
	 	databaseconnect(DatabaseUser)
		local entries = {}
		-- retrieve a cursor
		local sql = "SELECT * FROM pubweblog where selected = true and logdatetime >= '" .. starttime .. "' and logdatetime <= '" .. endtime .. "'" 
		local idcnt = 0
		sql = sql .. " ORDER BY logdatetime;"
		cur = assert (con:execute(sql))
		row = cur:fetch ({}, "a")
		while row do
			entries[#entries+1] = {sourcename=row.sourcename, clientip=row.clientip, clientuserid=row.clientuserid, logdatetime=row.logdatetime, uri=row.uri, shorturi=shorturi, bytes=row.bytes, reason=row.reason, core=row.score, shortreason=row.shortreason, badyesno=row.badyesno, deniedyesno=row.deniedyesno, bypassyesno=row.bypassyesno, wordloc=row.wordloc, selected=row.selected }
			row = cur:fetch (row, "a")
		end
		-- close everything
		cur:close()
		result.log.value = entries or {} 
		--result.log.value = sql
		databasedisconnect()
 	end)
	return cfe({ type="group", value=result, errtxt=err, label="Weblog Selected" })	
end

function editselected(chkdata)
	keycnt = 0
	sql = "UPDATE pubweblog SET selected = "
	databaseconnect(DatabaseOwner)
Ted Trask's avatar
Ted Trask committed
1512 1513 1514 1515 1516 1517 1518 1519 1520
	idarray = format.string_to_table(chkdata, "|")
	for key,x in pairs(idarray) do
		keycnt = keycnt + 1
		if keycnt == 1 then 
			sql = sql..x.." WHERE id = "
		else 
			sql = sql..x
		end
	end
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	assert (con:execute(sql))
	databasedisconnect()
end

function clearselected()
        local retval = cfe({ label="Clear selected records", errtxt = "Failed to clear selected records - who knows why" })
	sql = "UPDATE pubweblog SET selected = false WHERE selected = true"
	databaseconnect(DatabaseOwner)
	assert (con:execute(sql))
	databasedisconnect()
	retval.value = "Cleared"
	retval.errtxt = nil
	return retval 
end

function getweblog(activelog, clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, sortby, selected, focus )

	if (not activelog or activelog=="") then
		activelog = "pubweblog"
	end
	
Ted Trask's avatar
Ted Trask committed
1542
	if (not starttime or starttime=="") and (not endtime or endtime=="") and config.auditstart~="" and config.auditend~="" then
1543 1544 1545 1546 1547 1548 1549
		
		 starttime = config.auditstart
		 endtime = config.auditend
	
		if config.badyesno=="true" then
		   badyesno = '1'
		end
Ted Trask's avatar
Ted Trask committed
1550
	end
1551 1552 1553 1554 1555 1556 1557 1558 1559
	if (not score or score=="") and config.minimumscore~="" then
	  score = config.minimumscore
	end
	
	if (not sortby or sortby=="") and config.sortby~="" then
	   sortby = config.sortby
	end
	
	local result = handleparameters(activelog, clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, sortby, selected, focus)
Ted Trask's avatar
Ted Trask committed
1560
	result.log = cfe({ type="list", value={}, label="Weblog Access Log" })
1561 1562 1563 1564
	local success = validateparameters(result)
	if success then
		local res, err = pcall(function()
			databaseconnect(DatabaseUser)
1565
			result.log.value = listpubweblogentries(activelog, clientuserid, starttime, endtime, clientip, badyesno, deniedyesno, bypassyesno, score, urisearch, sortby, selected ) or {}
1566 1567 1568 1569 1570
			databasedisconnect()
		end)
	else
		err = "Invalid search parameters"
	end
Ted Trask's avatar
Ted Trask committed
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	return cfe({ type="group", value=result, errtxt=err, label="Weblog Access Log" })

end

function getusagestats()
	local retval = cfe({ type="list", value={}, label="Weblog Usage Stats" })
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		retval.value = listusagestats() or {}
		databasedisconnect()
	end)
	if not res then
		retval.errtxt = err
	end

	return retval
end


function getconfig()
	local result = {}
	result.auditstart = cfe({ value=config.auditstart or "", label="Audit Start Time" })
	result.auditend = cfe({ value=config.auditend or "", label="Audit End Time" })
1594 1595 1596
	result.badyesno = cfe({ type="boolean", value=(config.badyesno == "1"), label="Display Suspect Records", descr="Show only records flagged as suspect on initial display" })
	result.minimumscore = cfe({ value=config.minimumscore or "0", label="Minimum Score", descr="Minimum Score to search for" })
	result.sortby = cfe({ type="select", value=config.sortby or "logdatetime", label="Sort By field", option={"logdatetime", "logdatetime DESC", "clientuserid", "clientuserid DESC", "clientip", "clientip DESC", "bytes", "bytes DESC", "score", "score DESC", "reason"} })
Ted Trask's avatar
Ted Trask committed
1597 1598 1599 1600
	result.window = cfe({ value=config.window or "5", label="Time Window", descr="Minutes of activity to display before and after selected block" })
	result.watchdays = cfe({ value=config.watchdays or "14", label="Days to Watch", descr="Number of additional days to keep history for users in watchlist" })
	result.purgedays = cfe({ value=config.purgedays or "30", label="Days before Purge", descr="Days to keep history, regardless of audit" })
	result.historydays = cfe({ value=config.historydays or "14", label="Days to keep History", descr="Days beyond Audit Start Time to keep complete log history" })
1601
	result.shorturi = cfe({ type="boolean", value=(config.shorturi == "true"), label="Truncate URLs", descr="You can limit the length of displayed URLs by enabling this option"})
1602
	result.shortreason = cfe({ type="boolean", value=(config.shortreason == "true"), label="Short Reason", descr="Display a short reason (dansguardian only)"})
1603
	result.stoponerror = cfe({ type="boolean", value=(config.stoponerror == "true"), label="Stop on Error", descr="Stop import of logs if an error is encountered"})
Ted Trask's avatar
Ted Trask committed
1604 1605 1606
	return cfe({ type="group", value=result, label="Weblog Config" })
end

1607
local function validateconfig(newconfig)
1608
	local success = modelfunctions.validateselect(newconfig.value.sortby)
Ted Trask's avatar
Ted Trask committed
1609 1610 1611 1612 1613 1614
	if newconfig.value.window.value == "" then
		newconfig.value.window.errtxt = "Cannot be blank"
		success = false
	elseif not validator.is_integer(newconfig.value.window.value) then
		newconfig.value.window.errtxt = "Must be a number"
		success = false
Timo Teräs's avatar
Timo Teräs committed
1615
	end
Ted Trask's avatar
Ted Trask committed
1616 1617 1618
	if not validator.is_integer(newconfig.value.watchdays.value) then
		newconfig.value.watchdays.errtxt = "Must be a number"
		success = false
Timo Teräs's avatar
Timo Teräs committed
1619
	end
Ted Trask's avatar
Ted Trask committed
1620 1621 1622
	if not validator.is_integer(newconfig.value.purgedays.value) then
		newconfig.value.purgedays.errtxt = "Must be a number"
		success = false
Timo Teräs's avatar
Timo Teräs committed
1623
	end
Ted Trask's avatar
Ted Trask committed
1624 1625 1626
	if not validator.is_integer(newconfig.value.historydays.value) then
		newconfig.value.historydays.errtxt = "Must be a number"
		success = false
Timo Teräs's avatar
Timo Teräs committed
1627
	end
1628 1629 1630 1631
	local res, err = pcall(function()
		databaseconnect(DatabaseUser)
		local s
		if newconfig.value.auditstart.value ~= "" then
1632
			s,newconfig.value.auditstart.errtxt,newconfig.value.auditstart.value = convertdatabaseentry("TIMESTAMP", newconfig.value.auditstart.value)
1633 1634 1635
			success = success and s
		end
		if newconfig.value.auditend.value ~= "" then
1636
			s,newconfig.value.auditend.errtxt,newconfig.value.auditend.value = convertdatabaseentry("TIMESTAMP", newconfig.value.auditend.value)
1637 1638 1639 1640 1641 1642 1643 1644 1645
			success = success and s
		end
		databasedisconnect()
	end)
	if not res and err then
		newconfig.value.auditstart.errtxt = err
		newconfig.value.auditend.errtxt = err
		success = false
	end
1646
	return success, newconfig
1647 1648 1649 1650
end

function updateconfig(newconfig)
	local success = validateconfig(newconfig)
Ted Trask's avatar
Ted Trask committed
1651 1652 1653 1654 1655 1656 1657
	if success then
		configcontent = format.update_ini_file(configcontent, "", "auditstart", newconfig.value.auditstart.value)
		configcontent = format.update_ini_file(configcontent, "", "auditend", newconfig.value.auditend.value)
		configcontent = format.update_ini_file(configcontent, "", "window", newconfig.value.window.value)
		configcontent = format.update_ini_file(configcontent, "", "watchdays", newconfig.value.watchdays.value)
		configcontent = format.update_ini_file(configcontent, "", "purgedays", newconfig.value.purgedays.value)
		configcontent = format.update_ini_file(configcontent, "", "historydays", newconfig.value.historydays.value)
1658
		--configcontent = format.update_ini_file(configcontent, "", "groupby", newconfig.value.groupby.value)
Ted Trask's avatar
Ted Trask committed
1659
		configcontent = format.update_ini_file(configcontent, "", "shorturi", tostring(newconfig.value.shorturi.value))
1660
		configcontent = format.update_ini_file(configcontent, "", "shortreason", tostring(newconfig.value.shortreason.value))
1661
		configcontent = format.update_ini_file(configcontent, "", "stoponerror", tostring(newconfig.value.stoponerror.value))
1662 1663 1664 1665
		configcontent = format.update_ini_file(configcontent, "", "badyesno", tostring(newconfig.value.badyesno.value))
		configcontent = format.update_ini_file(configcontent, "", "minimumscore", tostring(newconfig.value.minimumscore.value))
		configcontent = format.update_ini_file(configcontent, "", "sortby", tostring(newconfig.value.sortby.value))
			
Ted Trask's avatar
Ted Trask committed
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
		fs.write_file(configfile, configcontent)
		config = format.parse_ini_file(configcontent, "") or {}
	else
		newconfig.errtxt = "Failed to update config"
	end

	return newconfig
end

function getnewadhocquery()
	local query = {}
	query.query = cfe({ label="Query select statement" })
	return cfe({ type="group", value=query, label="Ad-hoc Query" })
end

function adhocquery(query)
	local success = true
	query.value.query.value = query.value.query.value:lower()
	if query.value.query.value == "" then
		query.value.query.errtxt = "Empty select statement"
		success = false
	elseif not string.find(query.value.query.value, "^%s*select%s") then
		query.value.query.errtxt = "Must be a select statement"
		success = false
	end

	if success then
		local cur
		local res, err = pcall(function()
			databaseconnect(DatabaseUser)
			cur = assert (con:execute(query.value.query.value))
			databasedisconnect()
		end)
		if not res or not cur then
			query.value.query.errtxt = err or "Select failed"
			query.errtxt = "Query failed"
		else
			query.value.result = cfe({ type="list", value={}, label="Select result" })
			local result = query.value.result.value
			local row = cur:fetch ({}, "a")
			while row do
				result[#result+1] = {}
				for name,val in pairs(row) do
					result[#result][name] = val
				end
1711
				row = cur:fetch (row, "a")
Ted Trask's avatar
Ted Trask committed
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
			end
			-- close everything
			cur:close()
		end
	else
		query.errtxt = "Query failed"
	end

	return query
end

function testdatabase()
	local retval = cfe({ type="boolean", value=false, label="Database present" })
	local dbs = listdatabases()
	for i,db in ipairs(dbs) do
		if db == DatabaseName then
			retval.value = true
			break
		end
	end
	return retval
end

function getnewdatabase()
	local database = {}
	local errtxt
	database.password = cfe({ label="Password" })
	database.password_confirm = cfe({ label="Password (confirm)" })
	local test = testdatabase()
	if test.value then
		errtxt = "Database already exists!"
		success = false
	end
	return cfe({ type="group", value=database, label="Create Database", errtxt=errtxt })
end

function create_database(database)
	local success = true