summaryrefslogtreecommitdiffstats
path: root/openvpn-model.lua
blob: 5d17dbbcf6a440a0e403cdb42ef24e164b6fb006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
-- hostname model methods
module (..., package.seeall)

require ("posix")
require ("fs")

-- no initializer in model - use controller.init for that

-- ################################################################################
-- LOCAL FUNCTIONS
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 is_cmdline(path, name)
	local f = io.open(path.."/cmdline")
	if f == nil then
		return false
	end
	local line = f:read()
	f:close()
	if line == nil then 
		return false
	end
	local arg0 = string.gsub(line, string.char(0)..".*", "")
	return posix.basename(arg0) == name
end





local is_running = function( process, parameters )
	strsplit = require("split")
	local retval = nil
	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)
			local file_resultx = f:read("*a")
			local file_result = string.match(file_resultx, parameters)
			f:close()
			if ( file_result ) then
				retval = "Running"
			end
		end
	end
	return retval
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 function check_valid_config ( f )
	conf_ca     = ""
	conf_auth   = ""
	conf_type   = "server"
	conf_cert   = ""
	conf_key    = ""
	conf_dev    = ""
	conf_proto  = ""
	conf_remote = ""
	conf_dev    = ""
	local conf_file_content = read_file_as_array( "/etc/openvpn/".. f )
	for i =1,table.maxn(conf_file_content) do
		local lin = conf_file_content[i]
		-- Filter out commented lines
		if not string.find ( lin, "^[;#].*" ) then
			-- The following code could probably de done much easier
			-- Check for parameter of a valid configuration
			if string.find ( lin, "^ca[%s \v]" ) then
				conf_ca="ca"
			end
			if string.find ( lin, "^auth\-user\-pass[%s \v]" ) then
				conf_auth="auth-user-pass"
			end
			if string.find ( lin, "^client[%s$]" ) then
				conf_type = "client"
			end
			if string.find ( lin, "^cert[%s \v]" ) then
				conf_cert = "cert"
			end
			if string.find ( lin, "^key[%s \v]" ) then
				conf_key = "key"
			end
			if string.find ( lin, "^dev[%s \v]" ) then
				conf_dev = "dev"
			end
			if string.find ( lin, "^proto[%s \v]" ) then
				conf_proto = "proto"
			end
			if string.find ( lin, "^remote[%s \v]" ) then
				conf_remote = "remote"
			end
		end
	end
	-- Check if config is invalid (missing parameters)
	if conf_type == "client" then
		if conf_dev == "" or conf_remote == "" then
			conf_type="unknown"
		end
	else
		if conf_dev == "" or conf_port == "" then
			conf_type="unknown"
		end
	end
	return conf_type
end

local function list_rootfolder()
	local files , errstr, errno = posix.dir ( "/etc/openvpn/" )
	return files
end

-- ################################################################################
-- PUBLIC FUNCTIONS
function openvpn_version()
	local f,error = io.popen("/usr/sbin/openvpn --version")
	openvpnversion = f:read("*l")
	f:close()
	return openvpnversion
end

function list_conffiles()
	conlistfiles = {}
	local files = list_rootfolder()
	if files then
	  	for a,b in ipairs(files) do
			if string.match (b, "^.*conf$") then
				local conf_type = check_valid_config ( b )
--				local init_script = is_running ( string.gsub(b, "(%w+)(\..*)", "%1") )
			--	local init_script = string.gsub(b, "(%w+)(\..*)", "%1")
				local init_script = is_running ("openvpn", b)
				table.insert ( conlistfiles, cfe{ value = b, type = conf_type, init = init_script} )
			end
		end
	return conlistfiles
	end
end

get = function (self)
	return list_conffiles()
end