Merge pull request #336 from legendtang/fix
[project/luci.git] / applications / luci-app-ocserv / luasrc / model / cbi / ocserv / users.lua
1 -- Copyright 2014 Nikos Mavrogiannopoulos <n.mavrogiannopoulos@gmail.com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local dsp = require "luci.dispatcher"
5 local nixio  = require "nixio"
6
7 m = Map("ocserv", translate("OpenConnect VPN"))
8
9 if m.uci:get("ocserv", "config", "auth") == "plain" then
10
11 --[[Users]]--
12
13 function m.on_commit(map)
14         luci.sys.call("/etc/init.d/ocserv restart >/dev/null 2>&1")
15 end
16
17 s = m:section(TypedSection, "ocservusers", translate("Available users"))
18 s.anonymous = true
19 s.addremove = true
20 s.template = "cbi/tblsection"
21
22 s:option(Value, "name", translate("Name")).rmempty = true
23 s:option(DummyValue, "group", translate("Group")).rmempty = true
24 pwd = s:option(Value, "password", translate("Password"))
25 pwd.password = false
26
27 function pwd.write(self, section, value)
28         local pass
29         if string.match(value, "^\$%d\$.*") then
30                 pass = value
31         else
32                 local t = tonumber(nixio.getpid()*os.time())
33                 local salt = "$1$" .. t .. "$"
34                 pass = nixio.crypt(value, salt)
35         end
36         Value.write(self, section, pass)
37 end     
38
39 --[[if plain]]--
40 end
41
42 local lusers = { }
43 local fd = io.popen("/usr/bin/occtl show users", "r")
44 if fd then local ln
45         repeat
46                 ln = fd:read("*l")
47                 if not ln then break end
48
49                 local id, user, group, vpn_ip, ip, device, time, cipher, status = 
50                         ln:match("^%s*(%d+)%s+([-_%w]+)%s+([%.%*-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%:%.-_%w]+)%s+([%(%)%:%.-_%w]+)%s+([%:%.-_%w]+).*")
51                 if id then
52                         table.insert(lusers, {id, user, group, vpn_ip, ip, device, time, cipher, status})
53                 end
54         until not ln
55         fd:close()
56 end
57
58
59 --[[Active Users]]--
60
61 local s = m:section(Table, lusers, translate("Active users"))
62 s.anonymous = true
63 s.template = "cbi/tblsection"
64
65 s:option(DummyValue, 1, translate("ID"))
66 s:option(DummyValue, 2, translate("Username"))
67 s:option(DummyValue, 3, translate("Group"))
68 s:option(DummyValue, 4, translate("IP"))
69 s:option(DummyValue, 5, translate("VPN IP"))
70 s:option(DummyValue, 6, translate("Device"))
71 s:option(DummyValue, 7, translate("Time"))
72 s:option(DummyValue, 8, translate("Cipher"))
73 s:option(DummyValue, 9, translate("Status"))
74
75 return m