Typo
[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.util")
19 require("luci.sys")
20 require("luci.config")
21 local nixio = require "nixio", require "nixio.util"
22 local fs = require "nixio.fs"
23
24
25 luci.config.sauth = luci.config.sauth or {}
26 sessionpath = luci.config.sauth.sessionpath
27 sessiontime = tonumber(luci.config.sauth.sessiontime) or 15 * 60
28
29 --- Manually clean up expired sessions.
30 function clean()
31         local now   = os.time()
32         local files = fs.dir(sessionpath)
33         
34         if not files then
35                 return nil
36         end
37         
38         for file in files do
39                 local fname = sessionpath .. "/" .. file
40                 local stat = fs.stat(fname)
41                 if stat and stat.type == "reg" and stat.mtime + sessiontime < now then
42                         fs.unlink(fname)
43                 end 
44         end
45 end
46
47 --- Prepare session storage by creating the session directory.
48 function prepare()
49         fs.mkdir(sessionpath, 700)
50          
51         if not sane() then
52                 error("Security Exception: Session path is not sane!")
53         end
54 end
55
56 --- Read a session and return its content.
57 -- @param id    Session identifier
58 -- @return              Session data
59 function read(id)
60         if not id or #id == 0 then
61                 return
62         end
63         if not id:match("^%w+$") then
64                 error("Session ID is not sane!")
65         end
66         clean()
67         if not sane(sessionpath .. "/" .. id) then
68                 return
69         end
70         fs.utimes(sessionpath .. "/" .. id)
71         return fs.readfile(sessionpath .. "/" .. id)
72 end
73
74
75 --- Check whether Session environment is sane.
76 -- @return Boolean status
77 function sane(file)
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------")
82 end
83
84
85 --- Write session data to a session file.
86 -- @param id    Session identifier
87 -- @param data  Session data
88 function write(id, data)
89         if not sane() then
90                 prepare()
91         end
92         if not id:match("^%w+$") then
93                 error("Session ID is not sane!")
94         end
95         
96         local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
97         f:writeall(data)
98         f:close()
99 end
100
101
102 --- Kills a session
103 -- @param id    Session identifier
104 function kill(id)
105         if not id:match("^%w+$") then
106                 error("Session ID is not sane!")
107         end
108         fs.unlink(sessionpath .. "/" .. id)
109 end