diff options
Diffstat (limited to 'lib/session.lua')
-rw-r--r-- | lib/session.lua | 33 |
1 files changed, 15 insertions, 18 deletions
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 |