Added luci configuration for ocserv
[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 function m.on_commit(map)
31         luci.sys.call("/usr/bin/occtl reload  >/dev/null 2>&1")
32 end
33
34 function e.write(self, section, value)
35         if value == "0" then
36                 luci.sys.call("/etc/init.d/ocserv stop >/dev/null 2>&1")
37                 luci.sys.call("/etc/init.d/ocserv disable  >/dev/null 2>&1")
38         else
39                 luci.sys.call("/etc/init.d/ocserv enable  >/dev/null 2>&1")
40                 luci.sys.call("/etc/init.d/ocserv restart  >/dev/null 2>&1")
41         end
42         Flag.write(self, section, value)
43 end
44
45 local o
46
47 o = s:taboption("general", ListValue, "auth", translate("User Authentication"),
48         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)."))
49 o.rmempty = false
50 o.default = "plain"
51 o:value("plain")
52 o:value("PAM")
53
54 o = s:taboption("general", Value, "zone", translate("Firewall Zone"),
55         translate("The firewall zone that the VPN clients will be set to"))
56 o.nocreate = true
57 o.default = "lan"
58 o.template = "cbi/firewall_zonelist"
59
60 s:taboption("general", Value, "port", translate("Port"),
61         translate("The same UDP and TCP ports will be used"))
62 s:taboption("general", Value, "max_clients", translate("Max clients"))
63 s:taboption("general", Value, "max_same", translate("Max same clients"))
64 s:taboption("general", Value, "dpd", translate("Dead peer detection time (secs)"))
65
66 local pip = s:taboption("general", Flag, "predictable_ips", translate("Predictable IPs"),
67         translate("The assigned IPs will be selected deterministically"))
68 pip.default = "1"
69
70 local udp = s:taboption("general", Flag, "udp", translate("Enable UDP"),
71         translate("Enable UDP channel support; this must be enabled unless you know what you are doing"))
72 udp.default = "1"
73
74 local cisco = s:taboption("general", Flag, "cisco_compat", translate("AnyConnect client compatibility"),
75         translate("Enable support for CISCO AnyConnect clients"))
76 cisco.default = "1"
77
78 ipaddr = s:taboption("general", Value, "ipaddr", translate("VPN <abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Network-Address"))
79 ipaddr.datatype = "ip4addr"
80 ipaddr.default = "192.168.100.1"
81
82 nm = s:taboption("general", Value, "netmask", translate("VPN <abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
83 nm.datatype = "ip4addr"
84 nm.default = "255.255.255.0"
85 nm:value("255.255.255.0")
86 nm:value("255.255.0.0")
87 nm:value("255.0.0.0")
88
89 if has_ipv6 then
90         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"))
91         ip6addr.datatype = "ip6addr"
92 end
93
94
95 tmpl = s:taboption("template", Value, "_tmpl",
96         translate("Edit the template that is used for generating the ocserv configuration."))
97
98 tmpl.template = "cbi/tvalue"
99 tmpl.rows = 20
100
101 function tmpl.cfgvalue(self, section)
102         return nixio.fs.readfile("/etc/ocserv/ocserv.conf.template")
103 end
104
105 function tmpl.write(self, section, value)
106         value = value:gsub("\r\n?", "\n")
107         nixio.fs.writefile("/etc/ocserv/ocserv.conf.template", value)
108 end
109
110 ca = s:taboption("ca", Value, "_ca",
111         translate("View the CA certificate used by this server. You will need to save it as 'ca.pem' and import it into the clients."))
112
113 ca.template = "cbi/tvalue"
114 ca.rows = 20
115
116 function ca.cfgvalue(self, section)
117         return nixio.fs.readfile("/etc/ocserv/ca.pem")
118 end
119
120 --[[DNS]]--
121
122 s = m:section(TypedSection, "dns", translate("DNS servers"),
123         translate("The DNS servers to be provided to clients; can be either IPv6 or IPv4"))
124 s.anonymous = true
125 s.addremove = true
126 s.template = "cbi/tblsection"
127
128 s:option(Value, "ip", translate("IP Address")).rmempty = true
129 s.datatype = "ipaddr"
130
131 --[[Routes]]--
132
133 s = m:section(TypedSection, "routes", translate("Routing table"),
134         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"))
135 s.anonymous = true
136 s.addremove = true
137 s.template = "cbi/tblsection"
138
139 s:option(Value, "ip", translate("IP Address")).rmempty = true
140
141 o = s:option(Value, "netmask", translate("Netmask (or IPv6-prefix)"))
142 o.default = "255.255.255.0"
143 o:value("255.255.255.0")
144 o:value("255.255.0.0")
145 o:value("255.0.0.0")
146
147
148 return m