summaryrefslogtreecommitdiffstats
path: root/openvpn-model.lua
blob: 22ea045cfc357278a103a07f73dee37a4e0e2974 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
-- 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
	--DEBUG
	return path
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

-- FIXME: This should probably go in the time/date library
-- This function gives diff (in seconds) between 'date' and current time.
local function month_to_num ( dt )
	local strsplit = require("split")
	-- date is something like "Fri 11 23 10:34:07 2007"
	local olddate = strsplit(" ",dt)
	local hour,min,sec = string.match ( rawget(olddate,4), "(%d%d):(%d%d):(%d%d)" )
	local olddatetable = { year = rawget(olddate,5), month = rawget(olddate,2), day = rawget(olddate,3), hour = hour, min = min, sec = sec }

	-- fetch current time
	local f,err = io.popen("date")
	currdate = f:read("*l")
	f:close()
	local newdate = strsplit(" ",currdate)
	local hour,min,sec = string.match ( rawget(newdate,4), "(%d%d):(%d%d):(%d%d)" )
	local month = rawget(newdate,2)
	local nummonth = ""
	if (string.lower(month) == "jan") then nummonth = "1" end
	if (string.lower(month) == "feb") then nummonth = "2" end
	if (string.lower(month) == "mar") then nummonth = "3" end
	if (string.lower(month) == "apr") then nummonth = "4" end
	if (string.lower(month) == "may") then nummonth = "5" end
	if (string.lower(month) == "jun") then nummonth = "6" end
	if (string.lower(month) == "jul") then nummonth = "7" end
	if (string.lower(month) == "aug") then nummonth = "8" end
	if (string.lower(month) == "sep") then nummonth = "9" end
	if (string.lower(month) == "oct") then nummonth = "10" end
	if (string.lower(month) == "nov") then nummonth = "11" end
	if (string.lower(month) == "dec") then nummonth = "12" end
	local newdatetable = { year = rawget(newdate,6), month = nummonth, day = rawget(newdate,3), hour = hour, min = min, sec = sec }

--	return os.difftime(os.time(newdatetable),os.time(olddatetable))
	-- Return currdate, olddate, datediff
	return os.time(newdatetable), os.time(olddatetable), os.difftime(os.time(newdatetable),os.time(olddatetable))
end

-- ################################################################################
-- PUBLIC FUNCTIONS

function clientlist( self, path )
	local clientlist = {}
	local routinglist = {}
	local list = {}
	local f = ""
	local clientlst = nil
	local routinglst = nil
	local strsplit = require("split")
	if ( path ) then
		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
				local col = strsplit(",", v)
				if ( col[1] == "ROUTING TABLE" ) then
					clientlst = nil
				end

				if ( clientlst ) then
					table.insert(clientlist, { CN=col[1],
						REALADDR=col[2],
						BYTESRCV=col[3],
						BYTESSND=col[4],
						CONN=col[5] } )
				end
				if ( routinglst ) then
					table.insert(routinglist, { VIRTADDR=col[1],
						CN=col[2],
						REALADDR=col[3],
						LAST=col[4] } )
				end

				if ( col[1] == "Common Name" ) then
					clientlst = "YES"
				end
				if ( col[1] == "ROUTING TABLE" ) then
					routinglst = "YES"
				end
				if ( col[1] == "GLOBAL STATS" ) then
					routinglst = nil
				end
			end
		end
	end
	-- JOIN 'CLIENT_LIST' and 'ROUTING_LIST' TABLES INTO ONE TABLE
	for k,v in ipairs(clientlist) do
		for kk,vv in ipairs(routinglist) do
			if ( v.CN == vv.CN ) then
--				local difftime = month_to_num("Fri 11 23 10:34:07 2007")
				table.insert(list, { CN=v.CN, REALADDR=v.REALADDR, BYTESRCV=v.BYTESRCV, BYTESSND=v.BYTESSND, VIRTADDR=vv.VIRTADDR, CONN=v.CONN, LAST = LAST } )
			end
		end
	end
	local connclients = table.maxn(list)
	local difftime = "xXx"
	-- FIXME: This should probably be modifiead and go into a library!!!
	return list, connclients, difftime
end


function clientlistWORKING( 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, lastupdate = clientlist ()
	local isrunning = is_running ("openvpn", f)
--	local isrunning = f
	-- 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"], longname = config.name, lastupdate = lastupdate }
	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, shortname = path, 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