main.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. -- Copyright 2014 Nikos Mavrogiannopoulos <n.mavrogiannopoulos@gmail.com>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local fs = require "nixio.fs"
  4. local has_ipv6 = fs.access("/proc/net/ipv6_route")
  5. m = Map("ocserv", translate("OpenConnect VPN"))
  6. s = m:section(TypedSection, "ocserv", "OpenConnect")
  7. s.anonymous = true
  8. s:tab("general", translate("General Settings"))
  9. s:tab("ca", translate("CA certificate"))
  10. s:tab("template", translate("Edit Template"))
  11. local e = s:taboption("general", Flag, "enable", translate("Enable server"))
  12. e.rmempty = false
  13. e.default = "1"
  14. local o_sha = s:taboption("general", DummyValue, "sha_hash", translate("Server's certificate SHA1 hash"),
  15. translate("That value should be communicated to the client to verify the server's certificate"))
  16. local o_pki = s:taboption("general", DummyValue, "pkid", translate("Server's Public Key ID"),
  17. translate("An alternative value to be communicated to the client to verify the server's certificate; this value only depends on the public key"))
  18. local fd = io.popen("/usr/bin/certtool -i --infile /etc/ocserv/server-cert.pem", "r")
  19. if fd then local ln
  20. local found_sha = false
  21. local found_pki = false
  22. local complete = 0
  23. while complete < 2 do
  24. local ln = fd:read("*l")
  25. if not ln then
  26. break
  27. elseif ln:match("SHA%-?1 fingerprint:") then
  28. found_sha = true
  29. elseif found_sha then
  30. local hash = ln:match("([a-f0-9]+)")
  31. o_sha.default = hash and hash:upper()
  32. complete = complete + 1
  33. found_sha = false
  34. elseif ln:match("Public Key I[Dd]:") then
  35. found_pki = true
  36. elseif found_pki then
  37. local hash = ln:match("([a-f0-9]+)")
  38. o_pki.default = hash and "sha1:" .. hash:upper()
  39. complete = complete + 1
  40. found_pki = false
  41. end
  42. end
  43. fd:close()
  44. end
  45. function m.on_commit(map)
  46. luci.sys.call("/usr/bin/occtl reload >/dev/null 2>&1")
  47. end
  48. function e.write(self, section, value)
  49. if value == "0" then
  50. luci.sys.call("/etc/init.d/ocserv stop >/dev/null 2>&1")
  51. luci.sys.call("/etc/init.d/ocserv disable >/dev/null 2>&1")
  52. else
  53. luci.sys.call("/etc/init.d/ocserv enable >/dev/null 2>&1")
  54. luci.sys.call("/etc/init.d/ocserv restart >/dev/null 2>&1")
  55. end
  56. Flag.write(self, section, value)
  57. end
  58. local o
  59. o = s:taboption("general", ListValue, "auth", translate("User Authentication"),
  60. 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)."))
  61. o.rmempty = false
  62. o.default = "plain"
  63. o:value("plain")
  64. o:value("PAM")
  65. o = s:taboption("general", Value, "zone", translate("Firewall Zone"),
  66. translate("The firewall zone that the VPN clients will be set to"))
  67. o.nocreate = true
  68. o.default = "lan"
  69. o.template = "cbi/firewall_zonelist"
  70. s:taboption("general", Value, "port", translate("Port"),
  71. translate("The same UDP and TCP ports will be used"))
  72. s:taboption("general", Value, "max_clients", translate("Max clients"))
  73. s:taboption("general", Value, "max_same", translate("Max same clients"))
  74. s:taboption("general", Value, "dpd", translate("Dead peer detection time (secs)"))
  75. local pip = s:taboption("general", Flag, "predictable_ips", translate("Predictable IPs"),
  76. translate("The assigned IPs will be selected deterministically"))
  77. pip.default = "1"
  78. local compr = s:taboption("general", Flag, "compression", translate("Enable compression"),
  79. translate("Enable compression"))
  80. compr.default = "1"
  81. local udp = s:taboption("general", Flag, "udp", translate("Enable UDP"),
  82. translate("Enable UDP channel support; this must be enabled unless you know what you are doing"))
  83. udp.default = "1"
  84. local cisco = s:taboption("general", Flag, "cisco_compat", translate("AnyConnect client compatibility"),
  85. translate("Enable support for CISCO AnyConnect clients"))
  86. cisco.default = "1"
  87. ipaddr = s:taboption("general", Value, "ipaddr", translate("VPN <abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Network-Address"))
  88. ipaddr.datatype = "ip4addr"
  89. ipaddr.default = "192.168.100.1"
  90. nm = s:taboption("general", Value, "netmask", translate("VPN <abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Netmask"))
  91. nm.datatype = "ip4addr"
  92. nm.default = "255.255.255.0"
  93. nm:value("255.255.255.0")
  94. nm:value("255.255.0.0")
  95. nm:value("255.0.0.0")
  96. if has_ipv6 then
  97. 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"))
  98. ip6addr.datatype = "ip6addr"
  99. end
  100. tmpl = s:taboption("template", Value, "_tmpl",
  101. translate("Edit the template that is used for generating the ocserv configuration."))
  102. tmpl.template = "cbi/tvalue"
  103. tmpl.rows = 20
  104. function tmpl.cfgvalue(self, section)
  105. return nixio.fs.readfile("/etc/ocserv/ocserv.conf.template")
  106. end
  107. function tmpl.write(self, section, value)
  108. value = value:gsub("\r\n?", "\n")
  109. nixio.fs.writefile("/etc/ocserv/ocserv.conf.template", value)
  110. end
  111. ca = s:taboption("ca", Value, "_ca",
  112. translate("View the CA certificate used by this server. You will need to save it as 'ca.pem' and import it into the clients."))
  113. ca.template = "cbi/tvalue"
  114. ca.rows = 20
  115. function ca.cfgvalue(self, section)
  116. return nixio.fs.readfile("/etc/ocserv/ca.pem")
  117. end
  118. --[[DNS]]--
  119. s = m:section(TypedSection, "dns", translate("DNS servers"),
  120. translate("The DNS servers to be provided to clients; can be either IPv6 or IPv4"))
  121. s.anonymous = true
  122. s.addremove = true
  123. s.template = "cbi/tblsection"
  124. s:option(Value, "ip", translate("IP Address")).rmempty = true
  125. s.datatype = "ipaddr"
  126. --[[Routes]]--
  127. s = m:section(TypedSection, "routes", translate("Routing table"),
  128. 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"))
  129. s.anonymous = true
  130. s.addremove = true
  131. s.template = "cbi/tblsection"
  132. s:option(Value, "ip", translate("IP Address")).rmempty = true
  133. o = s:option(Value, "netmask", translate("Netmask (or IPv6-prefix)"))
  134. o.default = "255.255.255.0"
  135. o:value("255.255.255.0")
  136. o:value("255.255.0.0")
  137. o:value("255.0.0.0")
  138. return m