1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local fs = require "nixio.fs"
5 local sys = require "luci.sys"
6 local uci = require "luci.model.uci".cursor()
7 local testfullps = sys.exec("ps --help 2>&1 | grep BusyBox") --check which ps do we have
8 local psstring = (string.len(testfullps)>0) and "ps w" or "ps axfw" --set command we use to get pid
10 local m = Map("openvpn", translate("OpenVPN"))
11 local s = m:section( TypedSection, "openvpn", translate("OpenVPN instances"), translate("Below is a list of configured OpenVPN instances and their current state") )
12 s.template = "cbi/tblsection"
13 s.template_addremove = "openvpn/cbi-select-input-add"
15 s.add_select_options = { }
17 local cfg = s:option(DummyValue, "config")
18 function cfg.cfgvalue(self, section)
19 local file_cfg = self.map:get(section, "config")
21 s.extedit = luci.dispatcher.build_url("admin", "vpn", "openvpn", "file", "%s")
23 s.extedit = luci.dispatcher.build_url("admin", "vpn", "openvpn", "basic", "%s")
27 uci:load("openvpn_recipes")
28 uci:foreach( "openvpn_recipes", "openvpn_recipe",
30 s.add_select_options[section['.name']] =
31 section['_description'] or section['.name']
35 function s.getPID(section) -- Universal function which returns valid pid # or nil
36 local pid = sys.exec("%s | grep -w '[o]penvpn(%s)'" % { psstring, section })
37 if pid and #pid > 0 then
38 return tonumber(pid:match("^%s*(%d+)"))
44 function s.parse(self, section)
45 local recipe = luci.http.formvalue(
46 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
47 self.sectiontype .. ".select"
50 if recipe and not s.add_select_options[recipe] then
51 self.invalid_cts = true
53 TypedSection.parse( self, section )
57 function s.create(self, name)
58 local recipe = luci.http.formvalue(
59 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
60 self.sectiontype .. ".select"
62 local name = luci.http.formvalue(
63 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
64 self.sectiontype .. ".text"
66 if #name > 3 and not name:match("[^a-zA-Z0-9_]") then
67 local s = uci:section("openvpn", "openvpn", name)
69 local options = uci:get_all("openvpn_recipes", recipe)
70 for k, v in pairs(options) do
71 if k ~= "_role" and k ~= "_description" then
72 if type(v) == "boolean" then
75 uci:set("openvpn", name, k, v)
81 luci.http.redirect( self.extedit:format(name) )
85 self.invalid_cts = true
90 function s.remove(self, name)
91 local cfg_file = "/etc/openvpn/" ..name.. ".ovpn"
92 local auth_file = "/etc/openvpn/" ..name.. ".auth"
93 if fs.access(cfg_file) then
96 if fs.access(auth_file) then
99 uci:delete("openvpn", name)
101 uci:commit("openvpn")
104 s:option( Flag, "enabled", translate("Enabled") )
106 local active = s:option( DummyValue, "_active", translate("Started") )
107 function active.cfgvalue(self, section)
108 local pid = s.getPID(section)
110 return (sys.process.signal(pid, 0))
111 and translatef("yes (%i)", pid)
114 return translate("no")
117 local updown = s:option( Button, "_updown", translate("Start/Stop") )
118 updown._state = false
119 updown.redirect = luci.dispatcher.build_url(
120 "admin", "vpn", "openvpn"
122 function updown.cbid(self, section)
123 local pid = s.getPID(section)
124 self._state = pid ~= nil and sys.process.signal(pid, 0)
125 self.option = self._state and "stop" or "start"
126 return AbstractValue.cbid(self, section)
128 function updown.cfgvalue(self, section)
129 self.title = self._state and "stop" or "start"
130 self.inputstyle = self._state and "reset" or "reload"
132 function updown.write(self, section, value)
133 if self.option == "stop" then
134 sys.call("/etc/init.d/openvpn stop %s" % section)
136 sys.call("/etc/init.d/openvpn start %s" % section)
138 luci.http.redirect( self.redirect )
141 local port = s:option( DummyValue, "port", translate("Port") )
142 function port.cfgvalue(self, section)
143 local val = AbstractValue.cfgvalue(self, section)
145 local file_cfg = self.map:get(section, "config")
146 if file_cfg and fs.access(file_cfg) then
147 val = sys.exec("awk '{if(match(tolower($1),/^port$/)&&match($2,/[0-9]+/)){cnt++;printf $2;exit}}END{if(cnt==0)printf \"-\"}' " ..file_cfg)
149 val = sys.exec("awk '{if(match(tolower($1),/^remote$/)&&match($3,/[0-9]+/)){cnt++;printf $3;exit}}END{if(cnt==0)printf \"-\"}' " ..file_cfg)
156 local proto = s:option( DummyValue, "proto", translate("Protocol") )
157 function proto.cfgvalue(self, section)
158 local val = AbstractValue.cfgvalue(self, section)
160 local file_cfg = self.map:get(section, "config")
161 if file_cfg and fs.access(file_cfg) then
162 val = sys.exec("awk '{if(match(tolower($1),/^proto$/)&&match(tolower($2),/^udp[46]*$|^tcp[46]*-server$|^tcp[46]*-client$/)){cnt++;printf tolower($2);exit}}END{if(cnt==0)printf \"-\"}' " ..file_cfg)
164 val = sys.exec("awk '{if(match(tolower($1),/^remote$/)&&match(tolower($4),/^udp[46]*$|^tcp[46]*-server$|^tcp[46]*-client$/)){cnt++;printf $4;exit}}END{if(cnt==0)printf \"-\"}' " ..file_cfg)
171 function m.on_after_apply(self,map)
172 sys.call('/etc/init.d/openvpn reload')