2015-01-16 22:38:38 +00:00
-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Licensed to the public under the Apache License 2.0.
2008-10-05 19:49:21 +00:00
2009-07-19 00:24:58 +00:00
local fs = require " nixio.fs "
local sys = require " luci.sys "
local uci = require " luci.model.uci " . cursor ( )
2018-10-20 19:22:49 +00:00
local testfullps = sys.exec ( " ps --help 2>&1 | grep BusyBox " ) --check which ps do we have
2014-07-14 02:03:32 +00:00
local psstring = ( string.len ( testfullps ) > 0 ) and " ps w " or " ps axfw " --set command we use to get pid
2008-10-05 19:49:21 +00:00
2009-10-31 15:54:11 +00:00
local m = Map ( " openvpn " , translate ( " OpenVPN " ) )
local s = m : section ( TypedSection , " openvpn " , translate ( " OpenVPN instances " ) , translate ( " Below is a list of configured OpenVPN instances and their current state " ) )
2008-10-09 22:24:21 +00:00
s.template = " cbi/tblsection "
s.template_addremove = " openvpn/cbi-select-input-add "
s.addremove = true
s.add_select_options = { }
2018-10-20 19:22:49 +00:00
2018-11-19 20:20:22 +00:00
local cfg = s : option ( DummyValue , " config " )
function cfg . cfgvalue ( self , section )
2018-10-20 19:22:49 +00:00
local file_cfg = self.map : get ( section , " config " )
if file_cfg then
2019-08-05 14:28:27 +00:00
s.extedit = luci.dispatcher . build_url ( " admin " , " vpn " , " openvpn " , " file " , " %s " )
2018-10-20 19:22:49 +00:00
else
2019-08-05 14:28:27 +00:00
s.extedit = luci.dispatcher . build_url ( " admin " , " vpn " , " openvpn " , " basic " , " %s " )
2018-10-20 19:22:49 +00:00
end
end
2008-10-05 19:49:21 +00:00
2008-10-09 22:24:21 +00:00
uci : load ( " openvpn_recipes " )
uci : foreach ( " openvpn_recipes " , " openvpn_recipe " ,
function ( section )
s.add_select_options [ section [ ' .name ' ] ] =
section [ ' _description ' ] or section [ ' .name ' ]
end
)
2008-10-05 19:49:21 +00:00
2016-06-23 16:12:18 +00:00
function s . getPID ( section ) -- Universal function which returns valid pid # or nil
2018-04-06 21:37:38 +00:00
local pid = sys.exec ( " %s | grep -w '[o]penvpn(%s)' " % { psstring , section } )
2018-04-05 07:32:22 +00:00
if pid and # pid > 0 then
2018-04-13 12:45:02 +00:00
return tonumber ( pid : match ( " ^%s*(%d+) " ) )
2016-06-23 16:12:18 +00:00
else
return nil
end
2016-03-25 16:28:35 +00:00
end
2008-10-09 22:24:21 +00:00
function s . parse ( self , section )
local recipe = luci.http . formvalue (
luci.cbi . CREATE_PREFIX .. self.config .. " . " ..
self.sectiontype .. " .select "
)
if recipe and not s.add_select_options [ recipe ] then
self.invalid_cts = true
else
TypedSection.parse ( self , section )
2008-10-05 19:49:21 +00:00
end
end
2008-10-09 22:24:21 +00:00
function s . create ( self , name )
local recipe = luci.http . formvalue (
luci.cbi . CREATE_PREFIX .. self.config .. " . " ..
self.sectiontype .. " .select "
)
2018-09-16 16:58:26 +00:00
local name = luci.http . formvalue (
2014-07-14 02:03:32 +00:00
luci.cbi . CREATE_PREFIX .. self.config .. " . " ..
self.sectiontype .. " .text "
)
2017-12-19 08:54:09 +00:00
if # name > 3 and not name : match ( " [^a-zA-Z0-9_] " ) then
2018-09-16 16:58:26 +00:00
local s = uci : section ( " openvpn " , " openvpn " , name )
if s then
local options = uci : get_all ( " openvpn_recipes " , recipe )
for k , v in pairs ( options ) do
2018-10-20 19:22:49 +00:00
if k ~= " _role " and k ~= " _description " then
2018-11-24 15:33:54 +00:00
if type ( v ) == " boolean " then
v = v and " 1 " or " 0 "
end
2018-10-20 19:22:49 +00:00
uci : set ( " openvpn " , name , k , v )
end
2018-09-16 16:58:26 +00:00
end
uci : save ( " openvpn " )
2018-11-24 15:33:54 +00:00
uci : commit ( " openvpn " )
2018-11-19 20:20:22 +00:00
if extedit then
luci.http . redirect ( self.extedit : format ( name ) )
end
2018-09-16 16:58:26 +00:00
end
2017-12-19 08:54:09 +00:00
elseif # name > 0 then
2009-08-11 16:00:26 +00:00
self.invalid_cts = true
end
2017-12-19 08:54:09 +00:00
return 0
2008-10-09 22:24:21 +00:00
end
2008-10-05 19:49:21 +00:00
2018-11-24 15:33:54 +00:00
function s . remove ( self , name )
local cfg_file = " /etc/openvpn/ " .. name .. " .ovpn "
local auth_file = " /etc/openvpn/ " .. name .. " .auth "
if fs.access ( cfg_file ) then
fs.unlink ( cfg_file )
end
if fs.access ( auth_file ) then
fs.unlink ( auth_file )
end
uci : delete ( " openvpn " , name )
uci : save ( " openvpn " )
uci : commit ( " openvpn " )
end
2012-07-09 06:25:04 +00:00
s : option ( Flag , " enabled " , translate ( " Enabled " ) )
2008-10-05 19:49:21 +00:00
2009-10-31 15:54:11 +00:00
local active = s : option ( DummyValue , " _active " , translate ( " Started " ) )
2008-10-09 22:24:21 +00:00
function active . cfgvalue ( self , section )
2016-03-25 16:28:35 +00:00
local pid = s.getPID ( section )
2016-06-23 16:12:18 +00:00
if pid ~= nil then
2009-07-19 00:24:58 +00:00
return ( sys.process . signal ( pid , 0 ) )
2009-10-31 15:54:11 +00:00
and translatef ( " yes (%i) " , pid )
or translate ( " no " )
2008-10-05 19:49:21 +00:00
end
2009-10-31 15:54:11 +00:00
return translate ( " no " )
2008-10-09 22:24:21 +00:00
end
2008-10-05 19:49:21 +00:00
2009-10-31 15:54:11 +00:00
local updown = s : option ( Button , " _updown " , translate ( " Start/Stop " ) )
2009-08-12 14:05:31 +00:00
updown._state = false
2014-07-14 02:03:32 +00:00
updown.redirect = luci.dispatcher . build_url (
2019-08-05 14:28:27 +00:00
" admin " , " vpn " , " openvpn "
2014-07-14 02:03:32 +00:00
)
2009-08-12 14:05:31 +00:00
function updown . cbid ( self , section )
2016-03-25 16:28:35 +00:00
local pid = s.getPID ( section )
2016-06-23 16:12:18 +00:00
self._state = pid ~= nil and sys.process . signal ( pid , 0 )
2009-08-12 14:05:31 +00:00
self.option = self._state and " stop " or " start "
return AbstractValue.cbid ( self , section )
end
function updown . cfgvalue ( self , section )
self.title = self._state and " stop " or " start "
self.inputstyle = self._state and " reset " or " reload "
end
function updown . write ( self , section , value )
if self.option == " stop " then
2018-10-20 19:22:49 +00:00
sys.call ( " /etc/init.d/openvpn stop %s " % section )
2009-08-12 14:05:31 +00:00
else
2018-10-20 19:22:49 +00:00
sys.call ( " /etc/init.d/openvpn start %s " % section )
2009-08-12 14:05:31 +00:00
end
2014-07-14 02:03:32 +00:00
luci.http . redirect ( self.redirect )
2009-08-12 14:05:31 +00:00
end
2009-10-31 15:54:11 +00:00
local port = s : option ( DummyValue , " port " , translate ( " Port " ) )
2008-10-09 22:24:21 +00:00
function port . cfgvalue ( self , section )
local val = AbstractValue.cfgvalue ( self , section )
2018-11-24 15:33:54 +00:00
if not val then
local file_cfg = self.map : get ( section , " config " )
if file_cfg and fs.access ( file_cfg ) then
val = sys.exec ( " awk '{if(match(tolower($1),/^port$/)&&match($2,/[0-9]+/)){cnt++;printf $2;exit}}END{if(cnt==0)printf \" - \" }' " .. file_cfg )
if val == " - " then
val = sys.exec ( " awk '{if(match(tolower($1),/^remote$/)&&match($3,/[0-9]+/)){cnt++;printf $3;exit}}END{if(cnt==0)printf \" - \" }' " .. file_cfg )
end
end
end
2018-10-20 19:22:49 +00:00
return val or " - "
2008-10-09 22:24:21 +00:00
end
2009-10-31 15:54:11 +00:00
local proto = s : option ( DummyValue , " proto " , translate ( " Protocol " ) )
2008-10-09 22:24:21 +00:00
function proto . cfgvalue ( self , section )
local val = AbstractValue.cfgvalue ( self , section )
2018-11-24 15:33:54 +00:00
if not val then
local file_cfg = self.map : get ( section , " config " )
if file_cfg and fs.access ( file_cfg ) then
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 )
if val == " - " then
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 )
end
end
end
2018-10-20 19:22:49 +00:00
return val or " - "
2008-10-05 19:49:21 +00:00
end
2018-10-20 19:22:49 +00:00
function m . on_after_apply ( self , map )
sys.call ( ' /etc/init.d/openvpn reload ' )
2017-12-19 08:56:03 +00:00
end
2008-10-09 22:24:21 +00:00
2008-10-05 19:49:21 +00:00
return m