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 --- Manually clean up expired sessions.
32 local files = fs.dir(sessionpath)
39 local fname = sessionpath .. "/" .. file
40 local stat = fs.stat(fname)
41 if stat and stat.type == "reg" and stat.mtime + sessiontime < now then
47 --- Prepare session storage by creating the session directory.
49 fs.mkdir(sessionpath, 700)
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
60 if not id or #id == 0then
63 if not id:match("^%w+$") then
64 error("Session ID is not sane!")
67 if not sane(sessionpath .. "/" .. id) then
70 fs.utimes(sessionpath .. "/" .. id)
71 return fs.readfile(sessionpath .. "/" .. id)
75 --- Check whether Session environment is sane.
76 -- @return Boolean status
78 return luci.sys.process.info("uid")
79 == fs.stat(file or sessionpath, "uid")
80 and fs.stat(file or sessionpath, "modestr")
81 == (file and "rw-------" or "rwx------")
85 --- Write session data to a session file.
86 -- @param id Session identifier
87 -- @param data Session data
88 function write(id, data)
92 if not id:match("^%w+$") then
93 error("Session ID is not sane!")
96 local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
103 -- @param id Session identifier
105 if not id:match("^%w+$") then
106 error("Session ID is not sane!")
108 fs.unlink(sessionpath .. "/" .. id)