-- hostname model methods module (..., package.seeall) require ("posix") require ("fs") -- no initializer in model - use controller.init for that -- ################################################################################ -- UNKNOWN --local function read_file_as_array ( path ) -- local file, error = io.open(path) -- if ( file == nil ) then -- return nil, error -- end -- local f = {} -- for line in file:lines() do -- table.insert ( f , line ) -- end -- file:close() -- return f --end local function has_init_script ( f ) local initprefix = "/etc/init.d/openvpn" local file = initprefix .. "." .. f if f ~= "openvpn" then if ( fs.is_file(file)) then init = "yes" else init = nil end else if ( fs.is_file(initprefix)) then init = "yes" else init = nil end end return init end -- ################################################################################ -- LOCAL FUNCTIONS local function config_content( f ) local config = {} config.name = "/etc/openvpn/" .. f local conf_file = fs.read_file_as_array ( config.name ) for i=1,table.maxn(conf_file) do local l = conf_file[i] -- Filter out commented lines if not string.find ( l, "^[;#].*" ) then local a,b = string.match ( l, "^%s*(%S*)%s+(%S*).*$" ) if (a) then config[a]=b end end end if not ( config.log ) then config.log = config["log-append"] end if not ( config["max-clients"] ) then config["max-clients"] = "Unlimited" end if not ( config["tls-auth"] ) then config["tls-auth"] = "" end if not ( config["crl-verify"] ) then config["crl-verify"] = "" end if not ( config["local"] ) then config["local"] = "0.0.0.0" end return config end local is_running = function( process, parameters ) local strsplit = require("split") local retval = "" local pidofsx,error = io.popen("pidof " .. process ,r) local pidofs = strsplit(" ", pidofsx:read("*a")) pidofsx:close() if ( pidofs ~= nil ) then for k,v in pairs(pidofs) do local path = string.gsub("/proc/".. v .. "/cmdline", "%s", "") local f = io.open(path) if (f) then local file_resultx = f:read("*a") local file_result = string.match(file_resultx, parameters) f:close() end if ( file_result ) then retval = "Running" end end end return retval end local function check_valid_config ( f ) config.err = "" if not (config.client) or not (config.ca) or not (config.cert) or not (config.key) or not (config.dev) or not (config.proto) or not (config.remote) then config.type = nil config.err = "" if not (config.ca) then config.err = config.err .. "Check CA; " end if not (config.cert) then config.err = config.err .. "Check CERT; " end if not (config.key) then config.err = config.err .. "Check KEY; " end if not (config.dev) then config.err = config.err .. "Check DEV; " end if not (config.proto) then config.err = config.err .. "Check PROTO; " end if (config.client) or not (config.ca) or not (config.cert) or not (config.key) or not (config.dev) or not (config.proto) or not (config.port) then config.type = nil else config.type = "server" config.err = "" end else config.type = "client" config.err = "" end if not (config.type) then config.type = "unknown" end return config.type, config.err end local function list_conffiles() local configfiles = {} local config = {} local files , errstr, errno = posix.dir ( "/etc/openvpn/" ) if files then for k,v in ipairs(files) do if string.match (v, "^.*conf$") then table.insert ( configfiles, cfe{ name = v } ) end end return configfiles end end -- ################################################################################ -- PUBLIC FUNCTIONS function clientlist( self, path ) -- DEBUG -- local path = "openvpn.conf" local clientlist = {} local f = "" local f2 = "" if ( path ) then -- config = config_content ( config.name ) -- config = {} config = config_content ( path ) end if (config.status) then local f = fs.read_file_as_array( config.status ) if ( f ) then for k,v in ipairs(f) do -- The reason for this compex regexp is that I want to filter away the first 2-3 rows -- that doesn't mach this regexp. local clientname,clientip,clientport,bytesreceived,bytessent = string.match ( v, "([^,]*)[,]+([%w]+[.*][%w]+[.*][%w]+[.*][%w]+)[:]([%w]+)[,](%w*)[,](%w*)" ) -- Routing table is now intresting at this moment. So stop reading file. if ( v == "ROUTING TABLE" ) then break end if ( clientname ~= nil ) then table.insert ( clientlist, cfe{ name = clientname, ip = clientip , virtualip = "xXx.xXx.xXx.xXx", port = clientport, received = bytesreceived, sent = bytessent } ) end end end end local connclients = table.maxn(clientlist) return clientlist, connclients end function openvpn_version() local f,error = io.popen("/usr/sbin/openvpn --version") openvpnversion = f:read("*l") f:close() if not (openvpnversion) then openvpnversion = "Not installed!" end return openvpnversion end function get_serverconfig ( self, f ) local serverconfig = {} config = config_content ( f ) -- FIXME: change nex row to clientlist(config.name) local clientlist, connclients = clientlist () local isrunning = is_running ("openvpn", config.name) -- FIXME: Get status for autostart_status = 'rc_status | grep this process' local autostart_status = "" serverconfig = cfe{ name = f, device = config.dev, log = config.log, verb = config.verb, maxclients = config["max-clients"], clients = connclients, status = isrunning, autostart = autostart_status, dh = config.dh, ca = config.ca, cert = config.cert, key = config.key, tls = config["tls-auth"] , crl = config["crl-verify"], port = config.port, proto = config.proto, loca = config["local"] } return serverconfig end function get_logfile( self, path) local logcontent = {} config = config_content ( path ) local logfilecontent = fs.read_file ( config.log ) if not (logfilecontent) then logfilecontent = "File is empty or missing!" end return ( { name = config.log, value = logfilecontent } ) end function get_config( self, path) local logcontent = {} config = config_content ( path ) local logfilecontent = fs.read_file ( config.name ) if not (logfilecontent) then logfilecontent = "File is empty or missing!" end return ( { name = config.name, value = logfilecontent } ) end function get_conflist () local configlist = {} for k,v in pairs(list_conffiles()) do config = config_content ( v.name ) local conf_type, err = check_valid_config ( v.name ) local isrunning = is_running ("openvpn", v.name) local clientlist, connclients = clientlist () table.insert ( configlist, cfe{ name = v.name, type = conf_type, test = config.ca, err = err, status = isrunning, clients = connclients } ) end local countconfigs = table.maxn(configlist) return configlist, countconfigs end get = function (self) return list_conffiles() end