summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/authenticator-plaintext.lua20
-rw-r--r--lib/authenticator.lua38
-rw-r--r--lib/htmlviewfunctions.lua44
-rw-r--r--lib/menubuilder.lua6
-rw-r--r--lib/modelfunctions.lua30
-rw-r--r--lib/roles.lua50
-rw-r--r--lib/session.lua33
7 files changed, 114 insertions, 107 deletions
diff --git a/lib/authenticator-plaintext.lua b/lib/authenticator-plaintext.lua
index caa6748..ecfca03 100644
--- a/lib/authenticator-plaintext.lua
+++ b/lib/authenticator-plaintext.lua
@@ -7,12 +7,12 @@ create a different file for each field.
]]--
-module (..., package.seeall)
+local mymodule = {}
fs = require("acf.fs")
posix = require("posix")
-list_fields = function(self, tabl)
+mymodule.list_fields = function(self, tabl)
if not self or not tabl or tabl == "" then
return {}
end
@@ -27,7 +27,7 @@ list_fields = function(self, tabl)
return fields
end
-read_field = function(self, tabl, field)
+mymodule.read_field = function(self, tabl, field)
if not self or not tabl or tabl == "" or not field then
return nil
end
@@ -53,7 +53,7 @@ read_field = function(self, tabl, field)
end
end
-delete_field = function(self, tabl, field)
+mymodule.delete_field = function(self, tabl, field)
if not self or not tabl or tabl == "" or not field then
return false
end
@@ -62,7 +62,7 @@ delete_field = function(self, tabl, field)
return true
end
-write_entry = function(self, tabl, field, id, entry)
+mymodule.write_entry = function(self, tabl, field, id, entry)
if not self or not tabl or tabl == "" or not field or not id or not entry then
return false
end
@@ -83,7 +83,7 @@ write_entry = function(self, tabl, field, id, entry)
return true
end
-read_entry = function(self, tabl, field, id)
+mymodule.read_entry = function(self, tabl, field, id)
if not self or not tabl or tabl == "" or not field or not id then
return nil
end
@@ -99,7 +99,7 @@ read_entry = function(self, tabl, field, id)
return nil
end
-delete_entry = function (self, tabl, field, id)
+mymodule.delete_entry = function (self, tabl, field, id)
if not self or not tabl or tabl == "" or not field or not id then
return false
end
@@ -123,11 +123,13 @@ delete_entry = function (self, tabl, field, id)
-- If deleting the main field, delete all other fields also
if field == "" then
- local fields = list_fields(self, tabl)
+ local fields = mymodule.list_fields(self, tabl)
for i,fld in ipairs(fields) do
- delete_entry(self, tabl, fld, id)
+ mymodule.delete_entry(self, tabl, fld, id)
end
end
return result
end
+
+return mymodule
diff --git a/lib/authenticator.lua b/lib/authenticator.lua
index 789ecde..975d0e6 100644
--- a/lib/authenticator.lua
+++ b/lib/authenticator.lua
@@ -1,7 +1,7 @@
-- ACF Authenticator - does validation and loads sub-authenticator to read/write database
-- We store the logon info in the passwd table, "" field. It looks like
-- password:username:ROLE1[,ROLE2...]
-module (..., package.seeall)
+local mymodule = {}
modelfunctions = require("modelfunctions")
format = require("acf.format")
@@ -92,8 +92,8 @@ auth.delete_entry = function (self, tabl, field, id)
end
-- Publicly define the pre-defined tables
-usertable = "passwd"
-roletable = "roles"
+mymodule.usertable = "passwd"
+mymodule.roletable = "roles"
-- This will hold the auth structure from the database
local authstruct = {}
@@ -120,7 +120,7 @@ end
local load_database = function(self)
if not complete then
- local authtable = auth.read_field(self, usertable, "") or {}
+ local authtable = auth.read_field(self, mymodule.usertable, "") or {}
authstruct = {}
for i,value in ipairs(authtable) do
parse_entry(value.id, value.entry)
@@ -131,7 +131,7 @@ end
local get_id = function(self, userid)
if not authstruct[userid] then
- parse_entry(userid, auth.read_entry(self, usertable, "", userid))
+ parse_entry(userid, auth.read_entry(self, mymodule.usertable, "", userid))
end
return authstruct[userid]
end
@@ -184,7 +184,7 @@ end
--- public methods
-get_subauth = function(self)
+mymodule.get_subauth = function(self)
if not auth.subauths then
auth.subauths = {}
if self and self.conf and self.conf.authenticator and self.conf.authenticator ~= "" then
@@ -200,8 +200,8 @@ end
-- This function returns true or false, and
-- if false: the reason for failure
-authenticate = function(self, userid, password)
- auth = get_subauth(self)
+mymodule.authenticate = function(self, userid, password)
+ auth = mymodule.get_subauth(self)
local errtxt
if not userid or not password then
@@ -220,8 +220,8 @@ authenticate = function(self, userid, password)
end
-- This function returns the username, roles, ...
-get_userinfo = function(self, userid)
- auth = get_subauth(self)
+mymodule.get_userinfo = function(self, userid)
+ auth = mymodule.get_subauth(self)
local id = get_id(self, userid)
if id then
-- Make a copy so roles don't get changed in the authstruct
@@ -239,8 +239,8 @@ get_userinfo = function(self, userid)
return nil
end
-write_userinfo = function(self, userinfo)
- auth = get_subauth(self)
+mymodule.write_userinfo = function(self, userinfo)
+ auth = mymodule.get_subauth(self)
if not userinfo or not userinfo.userid or userinfo.userid == "" then
return false
end
@@ -253,7 +253,7 @@ write_userinfo = function(self, userinfo)
if userinfo.skin then id.skin = userinfo.skin end
if userinfo.home then id.home = userinfo.home end
- local success = auth.write_entry(self, usertable, "", id.userid, (id.password or "")..":"..(id.username or "")..":"..(id.roles or "")..":"..(id.skin or "")..":"..(id.home or ""))
+ local success = auth.write_entry(self, mymodule.usertable, "", id.userid, (id.password or "")..":"..(id.username or "")..":"..(id.roles or "")..":"..(id.skin or "")..":"..(id.home or ""))
authstruct[userinfo.userid] = nil
get_id(self, id.userid)
@@ -276,8 +276,8 @@ write_userinfo = function(self, userinfo)
return success
end
-list_users = function (self)
- auth = get_subauth(self)
+mymodule.list_users = function (self)
+ auth = mymodule.get_subauth(self)
load_database(self)
local output = {}
for k in pairs(authstruct) do
@@ -286,8 +286,10 @@ list_users = function (self)
return output
end
-delete_user = function (self, userid)
- auth = get_subauth(self)
+mymodule.delete_user = function (self, userid)
+ auth = mymodule.get_subauth(self)
authstruct[userid] = nil
- return auth.delete_entry(self, usertable, "", userid)
+ return auth.delete_entry(self, mymodule.usertable, "", userid)
end
+
+return mymodule
diff --git a/lib/htmlviewfunctions.lua b/lib/htmlviewfunctions.lua
index 6f7512a..35d4ca4 100644
--- a/lib/htmlviewfunctions.lua
+++ b/lib/htmlviewfunctions.lua
@@ -1,4 +1,4 @@
-module(..., package.seeall)
+local mymodule = {}
html = require("acf.html")
session = require("session")
@@ -22,12 +22,12 @@ local function getlabel(myitem, value)
return tostring(value)
end
-function displayitem(myitem, header_level, page_info)
+function mymodule.displayitem(myitem, header_level, page_info)
if not myitem then return end
if myitem.type == "form" then
header_level = header_level or 1
io.write("<H"..tostring(header_level)..">"..html.html_escape(myitem.label).."</H"..tostring(header_level)..">")
- displayform(myitem, nil, nil, page_info, header_level)
+ mymodule.displayform(myitem, nil, nil, page_info, header_level)
elseif myitem.type == "group" then
header_level = header_level or 1
io.write("<H"..tostring(header_level)..">"..html.html_escape(myitem.label).."</H"..tostring(header_level)..">")
@@ -49,7 +49,7 @@ function displayitem(myitem, header_level, page_info)
end
for x,name in ipairs(order) do
if myitem.value[name] then
- displayitem(myitem.value[name], tonumber(header_level)+1)
+ mymodule.displayitem(myitem.value[name], tonumber(header_level)+1)
end
end
elseif myitem.type ~= "hidden" then
@@ -67,7 +67,7 @@ function displayitem(myitem, header_level, page_info)
end
end
-function displayformitem(myitem, name, viewtype, header_level, group)
+function mymodule.displayformitem(myitem, name, viewtype, header_level, group)
if not myitem then return end
if name then myitem.name = name end
if group and group ~= "" then myitem.name = group.."."..myitem.name end
@@ -88,7 +88,7 @@ function displayformitem(myitem, name, viewtype, header_level, group)
io.write("<H"..tostring(header_level)..">"..html.html_escape(myitem.label).."</H"..tostring(header_level)..">")
if myitem.descr then io.write('<P CLASS="descr">' .. string.gsub(html.html_escape(myitem.descr), "\n", "<BR>") .. "</P>\n") end
if myitem.errtxt then io.write('<P CLASS="error">' .. string.gsub(html.html_escape(myitem.errtxt), "\n", "<BR>") .. "</P>\n") end
- displayformcontents(myitem, nil, nil, tonumber(header_level)+1, myitem.name)
+ mymodule.displayformcontents(myitem, nil, nil, tonumber(header_level)+1, myitem.name)
elseif myitem.type == "multi" then
-- FIXME multiple select doesn't work in haserl, so use series of checkboxes
--myitem.type = "select"
@@ -156,7 +156,7 @@ function displayformitem(myitem, name, viewtype, header_level, group)
end
end
-function displayformstart(myform, page_info)
+function mymodule.displayformstart(myform, page_info)
if not myform then return end
if not myform.action and page_info then
myform.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action
@@ -170,11 +170,11 @@ function displayformstart(myform, page_info)
end
io.write('method="POST">\n')
if myform.value.redir then
- displayformitem(myform.value.redir, "redir")
+ mymodule.displayformitem(myform.value.redir, "redir")
end
end
-function displayformcontents(myform, order, finishingorder, header_level, group)
+function mymodule.displayformcontents(myform, order, finishingorder, header_level, group)
if not myform then return end
if not order and not finishingorder then
tmporder = {}
@@ -197,7 +197,7 @@ function displayformcontents(myform, order, finishingorder, header_level, group)
reverseorder[name] = x
if myform.value[name] then
myform.value[name].name = name
- displayformitem(myform.value[name], nil, nil, header_level, group)
+ mymodule.displayformitem(myform.value[name], nil, nil, header_level, group)
end
end
end
@@ -210,20 +210,20 @@ function displayformcontents(myform, order, finishingorder, header_level, group)
for name,item in pairs(myform.value) do
if nil == reverseorder[name] and nil == reversefinishingorder[name] then
item.name = name
- displayformitem(item, nil, nil, header_level, group)
+ mymodule.displayformitem(item, nil, nil, header_level, group)
end
end
if finishingorder then
for x,name in ipairs(finishingorder) do
if myform.value[name] then
myform.value[name].name = name
- displayformitem(myform.value[name], nil, nil, header_level, group)
+ mymodule.displayformitem(myform.value[name], nil, nil, header_level, group)
end
end
end
end
-function displayformend(myform)
+function mymodule.displayformend(myform)
if not myform then return end
local option = myform.submit or myform.option
io.write('<DT></DT><DD>')
@@ -239,14 +239,14 @@ function displayformend(myform)
io.write('</DL>\n')
end
-function displayform(myform, order, finishingorder, page_info, header_level)
+function mymodule.displayform(myform, order, finishingorder, page_info, header_level)
if not myform then return end
- displayformstart(myform, page_info)
- displayformcontents(myform, order, finishingorder, header_level)
- displayformend(myform)
+ mymodule.displayformstart(myform, page_info)
+ mymodule.displayformcontents(myform, order, finishingorder, header_level)
+ mymodule.displayformend(myform)
end
-function displaycommandresults(commands, session, preserveerrors)
+function mymodule.displaycommandresults(commands, session, preserveerrors)
local cmdresult = {}
for i,cmd in ipairs(commands) do
if session[cmd.."result"] then
@@ -269,7 +269,7 @@ end
-- Divide up data into pages of size pagesize
-- clientdata can be a page number or a table where clientdata.page is the page number
-function paginate(data, clientdata, pagesize)
+function mymodule.paginate(data, clientdata, pagesize)
local subset = data
local page_data = { numpages=1, page=1, pagesize=pagesize, num=#data }
if #data > pagesize then
@@ -294,7 +294,7 @@ function paginate(data, clientdata, pagesize)
return subset, page_data
end
-function displaypagination(page_data, page_info)
+function mymodule.displaypagination(page_data, page_info)
local min, max
if page_data.page == 0 then
min = 1
@@ -363,10 +363,12 @@ end
-- give a cfe and get back a string of what is inside
-- great for troubleshooting and seeing what is really being passed to the view
-function cfe_unpack ( a )
+function mymodule.cfe_unpack ( a )
if type(a) == "table" then
value = session.serialize("cfe", a)
value = "<pre>" .. html.html_escape(value) .. "</pre>"
return value
end
end
+
+return mymodule
diff --git a/lib/menubuilder.lua b/lib/menubuilder.lua
index b40348a..6bb981f 100644
--- a/lib/menubuilder.lua
+++ b/lib/menubuilder.lua
@@ -3,7 +3,7 @@
Copyright (C) 2007 Nathan Angelacos
Licensed under the terms of GPL2
]]--
-module(..., package.seeall)
+local mymodule = {}
posix = require("posix")
format = require("acf.format")
@@ -60,7 +60,7 @@ local prio_compare = function(x,y)
end
-- returns a table of all the menu items found, sorted by priority
-get_menuitems = function (self)
+mymodule.get_menuitems = function (self)
local cats = {}
local reversecats = {}
local foundcontrollers = {}
@@ -193,4 +193,4 @@ get_menuitems = function (self)
return cats
end
-
+return mymodule
diff --git a/lib/modelfunctions.lua b/lib/modelfunctions.lua
index fb85102..2819911 100644
--- a/lib/modelfunctions.lua
+++ b/lib/modelfunctions.lua
@@ -1,4 +1,4 @@
-module(..., package.seeall)
+local mymodule = {}
-- Load libraries
fs = require("acf.fs")
@@ -7,7 +7,7 @@ processinfo = require("acf.processinfo")
posix = require("posix")
subprocess = require("subprocess")
-function getenabled(servicename)
+function mymodule.getenabled(servicename)
local result = cfe({ label = "Program status", name=servicename })
result.value, result.errtxt = processinfo.daemoncontrol(servicename, "status")
if string.find(result.value, ": not found") then
@@ -20,7 +20,7 @@ function getenabled(servicename)
return result
end
-function get_startstop(servicename)
+function mymodule.get_startstop(servicename)
local service = cfe({ type="hidden", value=servicename, label="Service Name" })
local actions, descr = processinfo.daemon_actions(servicename)
local errtxt
@@ -34,7 +34,7 @@ function get_startstop(servicename)
return cfe({ type="group", label="Management", value={servicename=service}, option=actions, errtxt=errtxt })
end
-function startstop_service(startstop, action)
+function mymodule.startstop_service(startstop, action)
if not action then
startstop.errtxt = "Invalid Action"
else
@@ -51,7 +51,7 @@ function startstop_service(startstop, action)
return startstop
end
-function getstatus(servicename, packagename, label)
+function mymodule.getstatus(servicename, packagename, label)
local status = {}
if packagename then
@@ -65,7 +65,7 @@ function getstatus(servicename, packagename, label)
end
if servicename then
- status.status = getenabled(servicename)
+ status.status = mymodule.getenabled(servicename)
local autostart_value, autostart_errtxt = processinfo.process_autostart(servicename)
status.autostart = cfe({
@@ -79,7 +79,7 @@ function getstatus(servicename, packagename, label)
return cfe({ type="group", value=status, label=label })
end
-function getfiledetails(file, validatefilename, validatefiledetails)
+function mymodule.getfiledetails(file, validatefilename, validatefiledetails)
local filename = cfe({ value=file or "", label="File name" })
local filecontent = cfe({ type="longtext", label="File content" })
local filesize = cfe({ value="0", label="File size" })
@@ -117,7 +117,7 @@ function getfiledetails(file, validatefilename, validatefiledetails)
return filedetails
end
-function setfiledetails(self, filedetails, validatefilename, validatefiledetails)
+function mymodule.setfiledetails(self, filedetails, validatefilename, validatefiledetails)
filedetails.value.filecontent.value = string.gsub(format.dostounix(filedetails.value.filecontent.value), "\n+$", "")
local success = true
if type(validatefilename) == "function" then
@@ -140,8 +140,8 @@ function setfiledetails(self, filedetails, validatefilename, validatefiledetails
end
if success then
--fs.write_file(filedetails.value.filename.value, filedetails.value.filecontent.value)
- write_file_with_audit(self, filedetails.value.filename.value, filedetails.value.filecontent.value)
- filedetails = getfiledetails(filedetails.value.filename.value)
+ mymodule.write_file_with_audit(self, filedetails.value.filename.value, filedetails.value.filecontent.value)
+ filedetails = mymodule.getfiledetails(filedetails.value.filename.value)
else
filedetails.errtxt = "Failed to set file"
end
@@ -149,7 +149,7 @@ function setfiledetails(self, filedetails, validatefilename, validatefiledetails
return filedetails
end
-function validateselect(select)
+function mymodule.validateselect(select)
for i,option in ipairs(select.option) do
if type(option) == "string" and option == select.value then
return true
@@ -161,7 +161,7 @@ function validateselect(select)
return false
end
-function validatemulti(multi)
+function mymodule.validatemulti(multi)
local reverseoption = {}
for i,option in ipairs(multi.option) do
if type(option) == "string" then
@@ -179,7 +179,7 @@ function validatemulti(multi)
return true
end
-function write_file_with_audit (self, path, str)
+function mymodule.write_file_with_audit (self, path, str)
if self then
local pre = ""
local post = ""
@@ -240,7 +240,7 @@ end
-- output will never be nil
-- errtxt will be nil for success and non-nil for failure
-- if include_err, then stderr will be prepended to stdout (if executable doesn't fail)
-run_executable = function(args, include_err, input)
+mymodule.run_executable = function(args, include_err, input)
local output = ""
local errtxt
local res, err = pcall(function()
@@ -291,3 +291,5 @@ run_executable = function(args, include_err, input)
end
return output, errtxt
end
+
+return mymodule
diff --git a/lib/roles.lua b/lib/roles.lua
index 5cc293d..eb64305 100644
--- a/lib/roles.lua
+++ b/lib/roles.lua
@@ -4,9 +4,9 @@ authenticator = require ("authenticator")
fs = require ("acf.fs")
format = require ("acf.format")
-module (..., package.seeall)
+local mymodule = {}
-guest_role = "GUEST"
+mymodule.guest_role = "GUEST"
-- Global variables so we don't have to figure out all the roles multiple times
local defined_roles, default_roles, reverseroles, roles_candidates, role_table, table_perm, array_perm
@@ -27,7 +27,7 @@ local get_roles_candidates = function(self)
end
-- Return a list of *controller.lua files
-list_controllers = function(self)
+mymodule.list_controllers = function(self)
local list = {}
for p in string.gmatch(self.conf.appdir, "[^,]+") do
for file in fs.find(".*controller%.lua", p, true) do
@@ -41,9 +41,9 @@ list_controllers = function(self)
end
-- Return information about all or specified controller files
-get_controllers = function(self,pre,controller)
+mymodule.get_controllers = function(self,pre,controller)
--we get all the controllers
- local list = list_controllers(self)
+ local list = mymodule.list_controllers(self)
--we need to grab the directory and name of file
local temp = {}
for k,v in pairs(list) do
@@ -64,7 +64,7 @@ get_controllers = function(self,pre,controller)
end
-- Find all public functions in a controller
-get_controllers_func = function(self,controller_info)
+mymodule.get_controllers_func = function(self,controller_info)
if controller_info == nil then
return "Could not be processed"
else
@@ -91,7 +91,7 @@ get_controllers_func = function(self,controller_info)
end
-- Find all views for a controller
-get_controllers_view = function(self,controller_info)
+mymodule.get_controllers_view = function(self,controller_info)
local temp = {}
for file in fs.find(controller_info.sname.."%-[^%.]+%-html%.lsp", controller_info.path) do
temp[#temp + 1] = string.match(file, controller_info.sname.."%-([^%./]+)%-html%.lsp")
@@ -99,10 +99,10 @@ get_controllers_view = function(self,controller_info)
return temp
end
-get_all_permissions = function(self)
+mymodule.get_all_permissions = function(self)
if not table_perm or not array_perm then
-- need to get a list of all the controllers
- controllers = get_controllers(self)
+ controllers = mymodule.get_controllers(self)
table_perm = {}
array_perm = {}
for a,b in pairs(controllers) do
@@ -112,12 +112,12 @@ get_all_permissions = function(self)
if nil == table_perm[b.prefix][b.sname] then
table_perm[b.prefix][b.sname] = {}
end
- local temp = get_controllers_func(self,b)
+ local temp = mymodule.get_controllers_func(self,b)
for x,y in ipairs(temp) do
table_perm[b.prefix][b.sname][y] = {}
array_perm[#array_perm + 1] = b.prefix .. b.sname .. "/" .. y
end
- temp = get_controllers_view(self,b)
+ temp = mymodule.get_controllers_view(self,b)
for x,y in ipairs(temp) do
if not table_perm[b.prefix][b.sname][y] then
table_perm[b.prefix][b.sname][y] = {}
@@ -130,7 +130,7 @@ get_all_permissions = function(self)
return table_perm, array_perm
end
-list_default_roles = function(self)
+mymodule.list_default_roles = function(self)
if not default_roles then
default_roles = {}
reverseroles = {}
@@ -174,7 +174,7 @@ list_default_roles = function(self)
return default_roles, reverseroles
end
-list_defined_roles = function(self)
+mymodule.list_defined_roles = function(self)
if not defined_roles then
local auth = authenticator.get_subauth(self)
-- Open the roles file and parse for defined roles
@@ -191,15 +191,15 @@ list_defined_roles = function(self)
return defined_roles
end
-list_roles = function(self)
- local default_roles = list_default_roles(self)
- local defined_roles = list_defined_roles(self)
+mymodule.list_roles = function(self)
+ local default_roles = mymodule.list_default_roles(self)
+ local defined_roles = mymodule.list_defined_roles(self)
return defined_roles, default_roles
end
-list_all_roles = function(self)
- local defined_roles, default_roles = list_roles(self)
+mymodule.list_all_roles = function(self)
+ local defined_roles, default_roles = mymodule.list_roles(self)
-- put the defined roles first
for x,role in ipairs(default_roles) do
defined_roles[#defined_roles + 1] = role
@@ -270,7 +270,7 @@ local determine_perms = function(self,roles)
temp = format.string_to_table(entry.entry, ",")
for z,perm in pairs(temp) do
local prefix,control,action = self.parse_path_info(perm)
- if control then
+ if control and "" ~= control then
if nil == permissions[prefix] then
permissions[prefix] = {}
end
@@ -290,18 +290,18 @@ local determine_perms = function(self,roles)
end
-- Go through the roles files and determine the permissions for the specified list of roles (including guest)
-get_roles_perm = function(self,roles)
- roles[#roles+1] = guest_role
+mymodule.get_roles_perm = function(self,roles)
+ roles[#roles+1] = mymodule.guest_role
return determine_perms(self, roles)
end
-- Go through the roles files and determine the permissions for the specified role
-get_role_perm = function(self,role)
+mymodule.get_role_perm = function(self,role)
return determine_perms(self, {role})
end
-- Delete a role from role file
-delete_role = function(self, role)
+mymodule.delete_role = function(self, role)
local auth = authenticator.get_subauth(self)
local result = auth.delete_entry(self, authenticator.roletable, "", role)
local cmdresult = "Role entry not found"
@@ -311,7 +311,7 @@ delete_role = function(self, role)
end
-- Set permissions for a role in role file
-set_role_perm = function(self, role, permissions, permissions_array)
+mymodule.set_role_perm = function(self, role, permissions, permissions_array)
if role==nil or role=="" then
return false, "Invalid Role"
end
@@ -332,3 +332,5 @@ set_role_perm = function(self, role, permissions, permissions_array)
local auth = authenticator.get_subauth(self)
return auth.write_entry(self, authenticator.roletable, "", role, table.concat(permissions_array or {},","))
end
+
+return mymodule
diff --git a/lib/session.lua b/lib/session.lua
index 12f0c28..34b9789 100644
--- a/lib/session.lua
+++ b/lib/session.lua
@@ -1,7 +1,6 @@
-- Session handling routines - written for acf
-- Copyright (C) 2007 N. Angelacos - GPL2 License
-
--[[ Note that in this library, we use empty (0 byte) files
-- everwhere we can, as they only take up dir entries, not inodes
-- as the tmpfs blocksize is 4K, and under denial of service
@@ -10,7 +9,7 @@
-- not take this precaution.
-- ]]--
-module (..., package.seeall)
+local mymodule = {}
posix = require("posix")
@@ -23,7 +22,7 @@ cached_content=nil
local b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
-- Return a sessionid of at least size bits length
-random_hash = function (size)
+mymodule.random_hash = function (size)
local file = io.open("/dev/urandom")
local str = ""
if file == nil then return nil end
@@ -36,8 +35,7 @@ random_hash = function (size)
end
-- FIXME: only hashes ipv4
-
-hash_ip_addr = function (string)
+mymodule.hash_ip_addr = function (string)
local str = ""
for i in string.gmatch(string, "%d+") do
str = str .. string.format("%02x", i )
@@ -45,7 +43,7 @@ hash_ip_addr = function (string)
return str
end
-ip_addr_from_hash = function (string)
+mymodule.ip_addr_from_hash = function (string)
local str = ""
for i in string.gmatch(string, "..") do
str = str .. string.format("%d", "0x" .. i) .. "."
@@ -53,7 +51,6 @@ ip_addr_from_hash = function (string)
return string.sub(str, 1, string.len(str)-1)
end
-
--[[
These functions serialize a table, including nested tables.
The code based on code in PiL 2nd edition p113
@@ -66,8 +63,7 @@ local function basicSerialize (o)
end
end
-
-function serialize (name, value, saved, output )
+mymodule.serialize = function(name, value, saved, output )
local need_to_concat = (output == nil)
output = output or {}
saved = saved or {}
@@ -82,7 +78,7 @@ function serialize (name, value, saved, output )
table.insert(output, str .. "{}")
for k,v in pairs(value) do
local fieldname = string.format("%s[%s]", name, basicSerialize(k))
- serialize (fieldname, v, saved, output)
+ mymodule.serialize (fieldname, v, saved, output)
end
end
elseif type(value) == "boolean" then
@@ -99,7 +95,7 @@ end
-- Save the session (unless all it contains is the id)
-- return true or false for success
-save_session = function( sessionpath, sessiontable)
+mymodule.save_session = function( sessionpath, sessiontable)
if nil == sessiontable or nil == sessiontable.id then return false end
-- clear the id key, don't need to store that
@@ -110,7 +106,7 @@ save_session = function( sessionpath, sessiontable)
if #sessiontable then
local output = {}
output[#output+1] = "-- This is an ACF session table."
- output[#output+1] = "local " .. serialize("s", sessiontable)
+ output[#output+1] = "local " .. mymodule.serialize("s", sessiontable)
output[#output+1] = "return s"
local content = table.concat(output, "\n") .. "\n"
@@ -132,11 +128,10 @@ save_session = function( sessionpath, sessiontable)
return true
end
-
-- Loads a session
-- Returns a timestamp (when the session data was saved) and the session table.
-- Insert the session into the "id" field
-load_session = function ( sessionpath, session )
+mymodule.load_session = function ( sessionpath, session )
if type(session) ~= "string" then return nil, {} end
local s = {}
-- session can only have b64 characters in it
@@ -171,7 +166,7 @@ end
-- Unlinks a session (deletes the session file)
-- return nil for failure, ?? for success
-unlink_session = function (sessionpath, session)
+mymodule.unlink_session = function (sessionpath, session)
if type(session) ~= "string" then return nil end
local s = string.gsub (session, "[^" .. b64 .. "]", "")
if s ~= session then
@@ -185,7 +180,7 @@ end
-- Record an invalid logon event
-- ID would typically be an ip address or username
-- the format is lockevent.id.datetime.processid
-record_event = function( sessionpath, id_u, id_ip )
+mymodule.record_event = function( sessionpath, id_u, id_ip )
local x = io.open (string.format ("%s/lockevent.%s.%s.%s.%s",
sessionpath or "/", id_u or "", id_ip or "", os.time(),
(posix.getpid("pid")) or "" ), "w")
@@ -195,7 +190,7 @@ end
-- Check how many invalid logon events
-- have happened for this id in the last n minutes
-- this will only effect the lockevent files
-count_events = function (sessionpath, id_user, ipaddr, minutes, limit)
+mymodule.count_events = function (sessionpath, id_user, ipaddr, minutes, limit)
--we need to have the counts added up? deny off any and or all
local now = os.time()
local minutes_ago = now - ((minutes or minutes_count_events) * 60)
@@ -225,7 +220,7 @@ count_events = function (sessionpath, id_user, ipaddr, minutes, limit)
end
-- Clear events that are older than n minutes
-expired_events = function (sessionpath, minutes)
+mymodule.expired_events = function (sessionpath, minutes)
--current os time in seconds
local now = os.time()
--take minutes and convert to seconds
@@ -252,3 +247,5 @@ expired_events = function (sessionpath, minutes)
end
return 0
end
+
+return mymodule