Print the ocserv's certificate hash and key ID
[project/luci.git] / applications / luci-ocserv / luasrc / model / cbi / ocserv / main.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 fs = require "nixio.fs"
15 local has_ipv6 = fs.access("/proc/net/ipv6_route")
16
17 m = Map("ocserv", translate("OpenConnect VPN"))
18
19 s = m:section(TypedSection, "ocserv", "OpenConnect")
20 s.anonymous = true
21
22 s:tab("general",  translate("General Settings"))
23 s:tab("ca", translate("CA certificate"))
24 s:tab("template", translate("Edit Template"))
25
26 local e = s:taboption("general", Flag, "enable", translate("Enable server"))
27 e.rmempty = false
28 e.default = "1"
29
30 local o_sha = s:taboption("general", DummyValue, "sha_hash", translate("Server's certificate SHA1 hash"),
31                           translate("That value should be communicated to the client to verify the server's certificate"))
32 local o_pki = s:taboption("general", DummyValue, "pkid", translate("Server's Public Key ID"),
33                           translate("An alternative value to be communicated to the client to verify the server's certificate; this value only depends on the public key"))
34
35 local fd = io.popen("/usr/bin/certtool -i --infile /etc/ocserv/server-cert.pem", "r")
36 if fd then local ln
37         local found_sha = false
38         local found_pki = false
39         local complete = 0
40         while complete < 2 do
41                 local ln = fd:read("*l")
42                 if not ln then
43                         break
44                 elseif ln:match("SHA%-?1 fingerprint:") then
45                         found_sha = true
46                 elseif found_sha then
47                         local hash = ln:match("([a-f0-9]+)")
48                         o_sha.default = hash and hash:upper()
49                         complete = complete + 1
50                         found_sha = false
51                 elseif ln:match("Public Key I[Dd]:") then
52                         found_pki = true
53                 elseif found_pki then
54                         local hash = ln:match("([a-f0-9]+)")
55                         o_pki.default = hash and hash:upper()
56                         complete = complete + 1
57                         found_pki = false
58                 end
59         end
60         fd:close()
61 end
62
63 function m.on_commit(map)
64         luci.sys.call("/usr/bin/occtl reload  >/dev/null 2>&1")
65 end
66
67 function e.write(self, section, value)
68         if value == "0" then
69                 luci.sys.call("/etc/init.d/ocserv stop >/dev/null 2>&1")
70                 luci.sys.call("/etc/init.d/ocserv disable  >/dev/null 2>&1")
71         else
72                 luci.sys.call("/etc/init.d/ocserv enable  >/dev/null 2>&1")
73                 luci.sys.call("/etc/init.d/ocserv restart  >/dev/null 2>&1")
74         end
75         Flag.write(self, section, value)
76 end
77
78 local o
79
80 o = s:taboption("general", ListValue, "auth", translate("User Authentication"),
81         translate("The authentication method for the users. The simplest is plain with a single username-password pair. Use PAM modules to authenticate using another server (e.g., LDAP, Radius)."))
82 o.rmempty = false
83 o.default = "plain"
84 o:value("plain")
85 o:value("PAM")
86
87 o = s:taboption("general", Value, "zone", translate("Firewall Zone"),
88         translate("The firewall zone that the VPN clients will be set to"))
89 o.nocreate = true
90 o.default = "lan"
91 o.template = "cbi/firewall_zonelist"
92
93 s:taboption("general", Value, "port", translate("Port"),
94         translate("The same UDP and TCP ports will be used"))
95 s:taboption("general", Value, "max_clients", translate("Max clients"))
96 s:taboption("general", Value, "max_same", translate("Max same clients"))
97 s:taboption("general", Value, "dpd", translate("Dead peer detection time (secs)"))
98
99 local pip = s:taboption("general", Flag, "predictable_ips", translate("Predictable IPs"),
100         translate("The assigned IPs will be selected deterministically"))
101 pip.default = "1"
102
103 local udp = s:taboption("general", Flag, "udp", translate("Enable UDP"),
104         translate("Enable UDP channel support; this must be enabled unless you know what you are doing"))
105 udp.default = "1"
106
107 local cisco = s:taboption("general", Flag, "cisco_compat", translate("AnyConnect client compatibility"),
108         translate("Enable support for CISCO AnyConnect clients"))
109 cisco.default = "1"
110
111 ipaddr = s:taboption("general", Value, "ipaddr", translate("VPN <abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Network-Address"))
112 ipaddr.datatype = "ip4addr"
113 ipaddr.default = "192.168.100.1"
114
115 nm = s:taboption("general", Value, "netmask", translate("VPN <abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
116 nm.datatype = "ip4addr"
117 nm.default = "255.255.255.0"
118 nm:value("255.255.255.0")
119 nm:value("255.255.0.0")
120 nm:value("255.0.0.0")
121
122 if has_ipv6 then
123         ip6addr = s:taboption("general", Value, "ip6addr", translate("VPN <abbr title=\"Internet Protocol Version 6\">IPv6</abbr>-Network-Address"), translate("<abbr title=\"Classless Inter-Domain Routing\">CIDR</abbr>-Notation: address/prefix"))
124         ip6addr.datatype = "ip6addr"
125 end
126
127
128 tmpl = s:taboption("template", Value, "_tmpl",
129         translate("Edit the template that is used for generating the ocserv configuration."))
130
131 tmpl.template = "cbi/tvalue"
132 tmpl.rows = 20
133
134 function tmpl.cfgvalue(self, section)
135         return nixio.fs.readfile("/etc/ocserv/ocserv.conf.template")
136 end
137
138 function tmpl.write(self, section, value)
139         value = value:gsub("\r\n?", "\n")
140         nixio.fs.writefile("/etc/ocserv/ocserv.conf.template", value)
141 end
142
143 ca = s:taboption("ca", Value, "_ca",
144         translate("View the CA certificate used by this server. You will need to save it as 'ca.pem' and import it into the clients."))
145
146 ca.template = "cbi/tvalue"
147 ca.rows = 20
148
149 function ca.cfgvalue(self, section)
150         return nixio.fs.readfile("/etc/ocserv/ca.pem")
151 end
152
153 --[[DNS]]--
154
155 s = m:section(TypedSection, "dns", translate("DNS servers"),
156         translate("The DNS servers to be provided to clients; can be either IPv6 or IPv4"))
157 s.anonymous = true
158 s.addremove = true
159 s.template = "cbi/tblsection"
160
161 s:option(Value, "ip", translate("IP Address")).rmempty = true
162 s.datatype = "ipaddr"
163
164 --[[Routes]]--
165
166 s = m:section(TypedSection, "routes", translate("Routing table"),
167         translate("The routing table to be provided to clients; you can mix IPv4 and IPv6 routes, the server will send only the appropriate. Leave empty to set a default route"))
168 s.anonymous = true
169 s.addremove = true
170 s.template = "cbi/tblsection"
171
172 s:option(Value, "ip", translate("IP Address")).rmempty = true
173
174 o = s:option(Value, "netmask", translate("Netmask (or IPv6-prefix)"))
175 o.default = "255.255.255.0"
176 o:value("255.255.255.0")
177 o:value("255.255.0.0")
178 o:value("255.0.0.0")
179
180
181 return m