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)
21 require("luci.config")
24 luci.config.sauth = luci.config.sauth or {}
25 sessionpath = luci.config.sauth.sessionpath
26 sessiontime = tonumber(luci.config.sauth.sessiontime) or 15 * 60
28 --- Manually clean up expired sessions.
31 local files = luci.fs.dir(sessionpath)
37 for i, file in pairs(files) do
38 local fname = sessionpath .. "/" .. file
39 local stat = luci.fs.stat(fname)
40 if stat and stat.type == "regular" and stat.atime + sessiontime < now then
46 --- Prepare session storage by creating the session directory.
48 luci.fs.mkdir(sessionpath)
49 luci.fs.chmod(sessionpath, "a-rwx,u+rwx")
52 error("Security Exception: Session path is not sane!")
56 --- Read a session and return its content.
57 -- @param id Session identifier
58 -- @return Session data
63 if not id:match("^%w+$") then
64 error("Session ID is not sane!")
67 if not sane(sessionpath .. "/" .. id) then
70 return luci.fs.readfile(sessionpath .. "/" .. id)
74 --- Check whether Session environment is sane.
75 -- @return Boolean status
77 return luci.sys.process.info("uid")
78 == luci.fs.stat(file or sessionpath, "uid")
79 and luci.fs.stat(file or sessionpath, "mode")
80 == (file and "rw-------" or "rwx------")
84 --- Write session data to a session file.
85 -- @param id Session identifier
86 -- @param data Session data
87 function write(id, data)
91 if not id:match("^%w+$") then
92 error("Session ID is not sane!")
94 luci.fs.writefile(sessionpath .. "/" .. id, data)
95 luci.fs.chmod(sessionpath .. "/" .. id, "a-rwx,u+rw")
100 -- @param id Session identifier
102 if not id:match("^%w+$") then
103 error("Session ID is not sane!")
105 luci.fs.unlink(sessionpath .. "/" .. id)