libs/web: add support for string templates to the template parser
[project/luci.git] / modules / base / 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 --- Prepare session storage by creating the session directory.
30 function prepare()
31         fs.mkdir(sessionpath, 700)
32         if not sane() then
33                 error("Security Exception: Session path is not sane!")
34         end
35 end
36
37 local function _read(id)
38         local blob = fs.readfile(sessionpath .. "/" .. id)
39         return blob
40 end
41
42 local function _write(id, data)
43         local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
44         f:writeall(data)
45         f:close()
46 end
47
48 local function _checkid(id)
49         return not not (id and #id == 32 and id:match("^[a-fA-F0-9]+$"))
50 end
51
52 --- Write session data to a session file.
53 -- @param id    Session identifier
54 -- @param data  Session data table
55 function write(id, data)
56         if not sane() then
57                 prepare()
58         end
59
60         assert(_checkid(id), "Security Exception: Session ID is invalid!")
61         assert(type(data) == "table", "Security Exception: Session data invalid!")
62
63         data.atime = luci.sys.uptime()
64
65         _write(id, luci.util.get_bytecode(data))
66 end
67
68 --- Read a session and return its content.
69 -- @param id    Session identifier
70 -- @return              Session data table or nil if the given id is not found
71 function read(id)
72         if not id or #id == 0 then
73                 return nil
74         end
75
76         assert(_checkid(id), "Security Exception: Session ID is invalid!")
77
78         if not sane(sessionpath .. "/" .. id) then
79                 return nil
80         end
81
82         local blob = _read(id)
83         local func = loadstring(blob)
84         setfenv(func, {})
85
86         local sess = func()
87         assert(type(sess) == "table", "Session data invalid!")
88
89         if sess.atime and sess.atime + sessiontime < luci.sys.uptime() then
90                 kill(id)
91                 return nil
92         end
93
94         -- refresh atime in session
95         write(id, sess)
96
97         return sess
98 end
99
100 --- Check whether Session environment is sane.
101 -- @return Boolean status
102 function sane(file)
103         return luci.sys.process.info("uid")
104                         == fs.stat(file or sessionpath, "uid")
105                 and fs.stat(file or sessionpath, "modestr")
106                         == (file and "rw-------" or "rwx------")
107 end
108
109 --- Kills a session
110 -- @param id    Session identifier
111 function kill(id)
112         assert(_checkid(id), "Security Exception: Session ID is invalid!")
113         fs.unlink(sessionpath .. "/" .. id)
114 end
115
116 --- Remove all expired session data files
117 function reap()
118         if sane() then
119                 local id
120                 for id in nixio.fs.dir(sessionpath) do
121                         if _checkid(id) then
122                                 -- reading the session will kill it if it is expired
123                                 read(id)
124                         end
125                 end
126         end
127 end