Drop support for luaposix and bitlib (obsoleted by nixio)
[project/luci.git] / libs / web / luasrc / sauth.lua
1 --[[
2
3 Session authentication
4 (c) 2008 Steven Barth <steven@midlink.org>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 --- LuCI session library.
17 module("luci.sauth", package.seeall)
18 require("luci.fs")
19 require("luci.util")
20 require("luci.sys")
21 require("luci.config")
22 local nixio = require "nixio", require "nixio.util"
23 local fs = require "nixio.fs"
24
25
26 luci.config.sauth = luci.config.sauth or {}
27 sessionpath = luci.config.sauth.sessionpath
28 sessiontime = tonumber(luci.config.sauth.sessiontime) or 15 * 60
29
30 --- Manually clean up expired sessions.
31 function clean()
32         local now   = os.time()
33         local files = luci.fs.dir(sessionpath)
34         
35         if not files then
36                 return nil
37         end
38         
39         for i, file in pairs(files) do
40                 local fname = sessionpath .. "/" .. file
41                 local stat = luci.fs.stat(fname)
42                 if stat and stat.type == "reg" and stat.mtime + sessiontime < now then
43                         luci.fs.unlink(fname)
44                 end 
45         end
46 end
47
48 --- Prepare session storage by creating the session directory.
49 function prepare()
50         fs.mkdir(sessionpath, 700)
51          
52         if not sane() then
53                 error("Security Exception: Session path is not sane!")
54         end
55 end
56
57 --- Read a session and return its content.
58 -- @param id    Session identifier
59 -- @return              Session data
60 function read(id)
61         if not id then
62                 return
63         end
64         if not id:match("^%w+$") then
65                 error("Session ID is not sane!")
66         end
67         clean()
68         if not sane(sessionpath .. "/" .. id) then
69                 return
70         end
71         luci.fs.utime(sessionpath .. "/" .. id)
72         return luci.fs.readfile(sessionpath .. "/" .. id)
73 end
74
75
76 --- Check whether Session environment is sane.
77 -- @return Boolean status
78 function sane(file)
79         return luci.sys.process.info("uid")
80                         == luci.fs.stat(file or sessionpath, "uid")
81                 and luci.fs.stat(file or sessionpath, "modestr")
82                         == (file and "rw-------" or "rwx------")
83 end
84
85
86 --- Write session data to a session file.
87 -- @param id    Session identifier
88 -- @param data  Session data
89 function write(id, data)
90         if not sane() then
91                 prepare()
92         end
93         if not id:match("^%w+$") then
94                 error("Session ID is not sane!")
95         end
96         
97         local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
98         f:writeall(data)
99         f:close()
100 end
101
102
103 --- Kills a session
104 -- @param id    Session identifier
105 function kill(id)
106         if not id:match("^%w+$") then
107                 error("Session ID is not sane!")
108         end
109         luci.fs.unlink(sessionpath .. "/" .. id)
110 end