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
64 if not sane(sessionpath .. "/" .. id) then
67 return luci.fs.readfile(sessionpath .. "/" .. id)
71 --- Check whether Session environment is sane.
72 -- @return Boolean status
74 return luci.sys.process.info("uid")
75 == luci.fs.stat(file or sessionpath, "uid")
76 and luci.fs.stat(file or sessionpath, "mode")
77 == (file and "rw-------" or "rwx------")
81 --- Write session data to a session file.
82 -- @param id Session identifier
83 -- @param data Session data
84 function write(id, data)
88 luci.fs.writefile(sessionpath .. "/" .. id, data)
89 luci.fs.chmod(sessionpath .. "/" .. id, "a-rwx,u+rw")