Merge pull request #304 from nmav/ocserv-crypt
[project/luci.git] / protocols / luci-proto-openconnect / luasrc / model / cbi / admin_network / proto_openconnect.lua
1 -- Copyright 2014 Nikos Mavrogiannopoulos <nmav@gnutls.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local map, section, net = ...
5
6 local server, username, password, cert, ca
7 local oc_cert_file, oc_key_file, oc_ca_file
8
9 local ifc = net:get_interface():name()
10
11 oc_cert_file = "/etc/openconnect/user-cert-" .. ifc .. ".pem"
12 oc_key_file = "/etc/openconnect/user-key-" .. ifc .. ".pem"
13 oc_ca_file = "/etc/openconnect/ca-" .. ifc .. ".pem"
14
15 server = section:taboption("general", Value, "server", translate("VPN Server"))
16 server.datatype = "host"
17
18 port = section:taboption("general", Value, "port", translate("VPN Server port"))
19 port.placeholder = "443"
20 port.datatype    = "port"
21
22 ifname = section:taboption("general", Value, "interface", translate("Output Interface"))
23 ifname.template = "cbi/network_netlist"
24
25 section:taboption("general", Value, "serverhash", translate("VPN Server's certificate SHA1 hash"))
26
27 section:taboption("general", Value, "authgroup", translate("AuthGroup"))
28
29 username = section:taboption("general", Value, "username", translate("Username"))
30 password = section:taboption("general", Value, "password", translate("Password"))
31 password.password = true
32
33
34 cert = section:taboption("advanced", Value, "usercert", translate("User certificate (PEM encoded)"))
35 cert.template = "cbi/tvalue"
36 cert.rows = 10
37
38 function cert.cfgvalue(self, section)
39         return nixio.fs.readfile(oc_cert_file)
40 end
41
42 function cert.write(self, section, value)
43         value = value:gsub("\r\n?", "\n")
44         nixio.fs.writefile(oc_cert_file, value)
45 end
46
47 cert = section:taboption("advanced", Value, "userkey", translate("User key (PEM encoded)"))
48 cert.template = "cbi/tvalue"
49 cert.rows = 10
50
51 function cert.cfgvalue(self, section)
52         return nixio.fs.readfile(oc_key_file)
53 end
54
55 function cert.write(self, section, value)
56         value = value:gsub("\r\n?", "\n")
57         nixio.fs.writefile(oc_key_file, value)
58 end
59
60
61 ca = section:taboption("advanced", Value, "ca", translate("CA certificate; if empty it will be saved after the first connection."))
62 ca.template = "cbi/tvalue"
63 ca.rows = 10
64
65 function ca.cfgvalue(self, section)
66         return nixio.fs.readfile(oc_ca_file)
67 end
68
69 function ca.write(self, section, value)
70         value = value:gsub("\r\n?", "\n")
71         nixio.fs.writefile(oc_ca_file, value)
72 end