summaryrefslogtreecommitdiffstats
path: root/openvpn-model.lua
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2008-11-25 19:27:55 +0000
committerTed Trask <ttrask01@yahoo.com>2008-11-25 19:27:55 +0000
commit3459e6bf19a13f35e419b0adabf8c3456a0a5025 (patch)
tree9330301f44a88ee2f3cb8b5d0271204294aa99c6 /openvpn-model.lua
parent99b9d1c5e938d818ca3e78a1f37d6a80da4b88f1 (diff)
downloadacf-openvpn-3459e6bf19a13f35e419b0adabf8c3456a0a5025.tar.bz2
acf-openvpn-3459e6bf19a13f35e419b0adabf8c3456a0a5025.tar.xz
Rewrite of openvpn to use cfes and new style. Added ability to edit/create/delete configs. Combined three view functions into one. Added a new status and moved old status to listconfigs. Still needs work, including ability to start/stop/restart.v0.3.0
git-svn-id: svn://svn.alpinelinux.org/acf/openvpn/trunk@1611 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'openvpn-model.lua')
-rw-r--r--openvpn-model.lua274
1 files changed, 127 insertions, 147 deletions
diff --git a/openvpn-model.lua b/openvpn-model.lua
index 26e2874..304b155 100644
--- a/openvpn-model.lua
+++ b/openvpn-model.lua
@@ -1,46 +1,36 @@
module (..., package.seeall)
+require ("modelfunctions")
require ("posix")
require ("format")
require ("fs")
require ("processinfo")
+require ("validator")
+require ("date")
+local processname = "openvpn"
+local packagename = "openvpn"
local baseurl = "/etc/openvpn/"
--- no initializer in model - use controller.init for that
-
-- ################################################################################
-- LOCAL FUNCTIONS
-local function file_info ( path )
- require("posix")
- local filedetails = posix.stat(path)
- filedetails["owner"]=rawget((posix.getpasswd(filedetails["uid"])),"name")
- filedetails["group"]=rawget((posix.getgroup(filedetails["gid"])),"name")
- filedetails["atimelong"]=os.date("%c", filedetails["atime"])
- filedetails["mtimelong"]=os.date("%c", filedetails["mtime"])
- filedetails["longname"]=path
- filedetails["name"]=basename(path)
- filedetails["size"]=filedetails["size"] .. " bytes"
- return filedetails
-end
local function config_content( f )
local config = {}
- config.name = baseurl .. 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[string.lower(a)]=b
- end
- if (a == "remote") then
- config["remoteport"]=string.match ( l, "^%s*%S+%s+%S+%s+(%S*)" )
- end
+ local lines = format.parse_linesandwords(fs.read_file(f) or "", "[#;]")
+ -- there can be multiple entries
+ for i,linetable in ipairs(lines) do
+ if config[linetable[1]] then
+ config[linetable[1]] = config[linetable[1]] .. "\n" .. (table.concat(linetable, " ", 2) or "")
+ else
+ config[linetable[1]] = table.concat(linetable, " ", 2) or ""
end
end
+
+ config.name = f
+ if config.remote then
+ config.remoteport = string.match ( config.remote, "^%S+%s+(%S*)" )
+ end
if not ( config.log ) then
config.log = config["log-append"]
end
@@ -50,13 +40,12 @@ local function config_content( f )
if not ( config["local"] ) then
config["local"] = "0.0.0.0"
end
- config["linkname"]=basename(f)
return config
end
local is_running = function( process, parameters )
- local retval = ""
+ local retval = "Stopped"
-- local tst = ""
-- local pidofsx, error = io.popen("pidof " .. process ,r)
-- local pidofs = string.gsub(pidofsx:read("*a"), "\n", "")
@@ -65,7 +54,7 @@ local is_running = function( process, parameters )
local i,v
for i,v in ipairs(processinfo.pidof(process) or {}) do
local path = string.gsub("/proc/".. v .. "/cmdline", "%s", "")
- local f,err = io.open(path,r)
+ local f = io.open(path,r)
local file_resultx = f:read("*a")
local file_result = string.match(file_resultx, parameters)
f:close()
@@ -76,77 +65,62 @@ local is_running = function( process, parameters )
return retval
end
-local function check_valid_config ( f )
- config.err = ""
+local function check_valid_config (config)
+ config.errtxt = nil
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 = ""
+ config.errtxt = ""
if not (config.ca) then
- config.err = config.err .. "Check CA; "
+ config.errtxt = config.errtxt .. "Check CA; "
end
if not (config.cert) then
- config.err = config.err .. "Check CERT; "
+ config.errtxt = config.errtxt .. "Check CERT; "
end
if not (config.key) then
- config.err = config.err .. "Check KEY; "
+ config.errtxt = config.errtxt .. "Check KEY; "
end
if not (config.dev) then
- config.err = config.err .. "Check DEV; "
+ config.errtxt = config.errtxt .. "Check DEV; "
end
if not (config.proto) then
- config.err = config.err .. "Check PROTO; "
+ config.errtxt = config.errtxt .. "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 = ""
+ config.errtxt = nil
end
else
config.type = "client"
- config.err = ""
+ config.errtxt = nil
end
if not (config.type) then config.type = "unknown" end
- return config.type, config.err
+ return config.type, config.errtxt
end
local function list_conffiles()
local configfiles = {}
- local config = {}
- local files , errstr, errno = posix.dir ( baseurl )
- 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
+ for file in fs.find(".*%.conf", baseurl) do
+ configfiles[#configfiles+1] = file
end
+ return configfiles
end
--- ################################################################################
--- PUBLIC FUNCTIONS
-
-function clientlist( self, path )
- local libdate = require("date")
+local function clientlist( statusfile )
local clientlist = {}
local routinglist = {}
local datechange = {}
local list = {}
- local f = ""
- local clientlst = nil
- local routinglst = nil
- if ( path ) then
- config = config_content ( path )
- end
- if (config.status) then
- local f = fs.read_file_as_array( config.status )
+ if (statusfile) then
+ local f = fs.read_file_as_array( statusfile )
+ local clientlst = false
+ local routinglst = false
if ( f ) then
for k,v in ipairs(f) do
local col = format.string_to_table(v, ",")
if ( col[1] == "ROUTING TABLE" ) or ( col[1] == "GLOBAL STATS" ) then
- clientlst = nil
- routinglst = nil
+ clientlst = false
+ routinglst = false
end
if ( clientlst ) then
table.insert(clientlist, { CN=col[1],
@@ -163,7 +137,7 @@ function clientlist( self, path )
if (col[4]) then
local month,day,hour,min,sec,year = string.match(col[4],"^%S+%s+(%S+)%s+(%S+)%s+(%d%d):(%d%d):(%d%d)%s+(%S+)")
table.insert(datechange, { year=year,
- month=libdate.abr_month_num(month),
+ month=date.abr_month_num(month),
day=day,
hour=hour,
min=min,
@@ -171,10 +145,10 @@ function clientlist( self, path )
end
end
if ( col[1] == "Virtual Address" ) then
- routinglst = "YES"
+ routinglst = true
end
if ( col[1] == "Common Name" ) then
- clientlst = "YES"
+ clientlst = true
end
end
@@ -188,10 +162,9 @@ function clientlist( self, path )
end
end
end
- connclients = table.maxn(clientlist)
- if ( connclients > 0 ) then
- -- FIXME: If possible, use lib/date.lua instead of the following code.
- local lastdatechange = libdate.date_to_seconds(datechange)
+ local lastdatechangetxt, lastdatechangediff
+ if ( #clientlist > 0 ) then
+ local lastdatechange = date.date_to_seconds(datechange)
lastdatechangetxt = os.date("%c", lastdatechange[#lastdatechange])
lastdatechangediff = os.time() - os.date(lastdatechange[table.maxn(lastdatechange)])
if (lastdatechangediff > 60) then
@@ -200,95 +173,102 @@ function clientlist( self, path )
lastdatechangediff = lastdatechangediff .. " sec"
end
end
- return list, connclients, lastdatechangetxt, lastdatechangediff
+ return list, #clientlist, lastdatechangetxt, lastdatechangediff
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
+-- ################################################################################
+-- PUBLIC FUNCTIONS
-function get_config ( self, f )
- local path = basename(f)
- local configresult = {}
- config = nil
- config = config_content ( path )
- local clientlist, client_count, client_lastupdate, client_lastdatechangediff = clientlist ()
- local status_isrunning = is_running ("openvpn", path)
- if (client_lastupdate == nil) then
- config["client_lastupdate"] = "?"
- else
- config["client_lastupdate"]=client_lastupdate
- end
- if (client_lastupdate == nil) then
- config["client_lastdatechangediff"] = "? min"
- else
- config["client_lastdatechangediff"]=client_lastdatechangediff
- end
- config["client_count"]=client_count
- config["status_isrunning"]=status_isrunning
- configresult = config
- return configresult
+function getstatus()
+ return modelfunctions.getstatus(processname, packagename, "OpenVPN Status")
end
-function update_filecontent (self, f, modifications)
- name = basename(f)
- path = baseurl .. name
- local available_files = list_conffiles()
- for k,v in pairs(available_files) do
- if ( available_files[k].name == name ) then
- local file = io.open( path, "w+" )
- local file_result,err = file:write(format.dostounix(modifications))
- file:close()
- if (err ~= nil) then
- local filedetails = get_config(name)
- file_content = {name=name, value=file_result, filedetails=filedetails, err=err}
- end
- end
- end
- return file_content
+function getclientinfo(f)
+ local config = config_content(f)
+ return cfe({ type="structure", value=clientlist(config.status), label="Client info" })
end
-function get_logfile( self, path)
- config = config_content ( path )
- local logfilecontent = fs.read_file ( config.log )
- if not (logfilecontent) then
- logfilecontent = "File is empty or missing!"
+function get_config(f)
+ local config = config_content(f)
+ check_valid_config(config)
+ if config.type == "server" then
+ local clientlist, client_count, client_lastupdate, client_lastdatechangediff = clientlist(config.status)
+ config["client_lastupdate"] = client_lastupdate or "?"
+ config["client_lastdatechangediff"] = client_lastdatechangediff or "? min"
+ config["client_count"] = client_count or 0
end
- return ( { name = config.log, value = logfilecontent } )
+ config["status_isrunning"] = is_running ("openvpn", basename(f))
+ return cfe({ type="structure", value=config, label="OpenVPN Config" })
end
-function get_filecontent( self, f)
- local path = basename(f)
- local configresult = {}
- config = config_content ( path )
- local file = io.open( config.name )
- local file_result = file:read("*a") or "unknown"
- file:close()
- local conf_type, err = check_valid_config ( path )
- local filedetails = file_info( config.name )
- file_content = cfe{name=config.name, value=file_result, type=conf_type, filedetails=filedetails}
- return file_content
+function get_logfile(f)
+ local config = config_content(f)
+ return cfe({ value=config.log or "", label="Config file" })
end
function get_conflist ()
local configlist = {}
- for k,v in pairs(list_conffiles() or {}) 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, err = err, status = isrunning, clients = connclients } )
+ for i,file in ipairs(list_conffiles()) do
+ config = config_content ( file )
+ local conf_type, errtxt = check_valid_config(config)
+ local isrunning = is_running ("openvpn", basename(file))
+ local clientlist, connclients = clientlist (config.status)
+ table.insert ( configlist, { name = file, type = conf_type, errtxt = errtxt, status = isrunning, clients = connclients } )
end
- local countconfigs = table.maxn(configlist)
- return configlist, countconfigs
+ return cfe({ type="structure", value=configlist, label="Configuration List" })
+end
+
+function get_filecontent(f)
+ --FIXME validate
+ return modelfunctions.getfiledetails(f, list_conffiles())
end
-get = function (self)
- return list_conffiles()
+
+function update_filecontent(filedetails)
+ --FIXME validate
+ return modelfunctions.setfiledetails(filedetails, list_conffiles())
+end
+
+function create_new_config()
+ config = {
+ name = cfe({ label="File Name" }),
+ }
+
+ return cfe({ type="group", value=config, label="Config" })
end
+function create_config(config)
+ local success = true
+ local path = config.value.name.value
+ if not string.find(path, "/") then
+ path = baseurl .. path
+ end
+
+ if not validator.is_valid_filename(path, baseurl) then
+ success = false
+ config.value.name.errtxt = "Invalid path"
+ elseif posix.stat(path) then
+ success = false
+ config.value.name.errtxt = "File already exists"
+ end
+
+ if success then
+ if not posix.stat(baseurl) then posix.mkdir(baseurl) end
+ fs.create_file(path)
+ else
+ config.errtxt = "Failed to create config"
+ end
+
+ return config
+end
+
+function delete_config(name)
+ local cmdresult = cfe({ label="Delete config result", errtxt="Failed to delete config - not found" })
+
+ if validator.is_valid_filename(name, baseurl) and fs.is_file(name) then
+ os.remove(name)
+ cmdresult.value = "Config Deleted"
+ cmdresult.errtxt = nil
+ end
+
+ return cmdresult
+end