4 (c) 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 --- LuCI session library.
17 module("luci.sauth", package.seeall)
20 require("luci.config")
21 local nixio = require "nixio", require "nixio.util"
22 local fs = require "nixio.fs"
25 luci.config.sauth = luci.config.sauth or {}
26 sessionpath = luci.config.sauth.sessionpath
27 sessiontime = tonumber(luci.config.sauth.sessiontime) or 15 * 60
29 --- Prepare session storage by creating the session directory.
31 fs.mkdir(sessionpath, 700)
33 error("Security Exception: Session path is not sane!")
38 return luci.util.get_bytecode({
42 atime=luci.sys.uptime()
47 local t = loadstring(blob)()
56 --- Read a session and return its content.
57 -- @param id Session identifier
58 -- @return Session data
59 local function _read(id)
60 local blob = fs.readfile(sessionpath .. "/" .. id)
64 --- Write session data to a session file.
65 -- @param id Session identifier
66 -- @param data Session data
67 local function _write(id, data)
68 local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
73 function write(id, data)
78 if not id or #id == 0 or not id:match("^%w+$") then
79 error("Session ID is not sane!")
86 if not id or #id == 0 then
89 if not id:match("^%w+$") then
90 error("Session ID is not sane!")
92 if not sane(sessionpath .. "/" .. id) then
96 local blob = _read(id)
97 if decode(blob).atime + sessiontime < luci.sys.uptime()then
98 fs.unlink(sessionpath .. "/" .. id)
101 -- refresh atime in session
102 refreshed = encode(decode(blob))
108 --- Check whether Session environment is sane.
109 -- @return Boolean status
111 return luci.sys.process.info("uid")
112 == fs.stat(file or sessionpath, "uid")
113 and fs.stat(file or sessionpath, "modestr")
114 == (file and "rw-------" or "rwx------")
118 -- @param id Session identifier
120 if not id:match("^%w+$") then
121 error("Session ID is not sane!")
123 fs.unlink(sessionpath .. "/" .. id)