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