summaryrefslogtreecommitdiffstats
path: root/ipsectools-model.lua
blob: c30fbb722426ee3b3b9bfe3546ceb6f7519e79be (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
module(..., package.seeall)

-- Load libraries
require("fs")
require("procps")
require("format")
require("daemoncontrol")
require("validator")
require("processinfo")

-- Set variables
local configfile = "/etc/racoon/racoon.conf"
local configfile2 = "/etc/ipsec.conf"
local processname = "racoon"
local packagename = "ipsec-tools"
local baseurl = "/etc/racoon/"

local descr = {
	state={
	['9']="Established",
	},
	side={
	['R']="We are 'Responder'.",
	['I']="We 'Initiated' this phase1",
	},
	exchange={
	['M']="Main mode",
	['A']="Agressive mode",
	['B']="Basic mode",
	},

}

-- ################################################################################
-- LOCAL FUNCTIONS

local function ip_xfrm(mode)
	local cmd_output_result
	local cmd = "/bin/ip xfrm " .. mode .. " 2>/dev/null"
	local f = io.popen( cmd )
	local cmd_output_result = f:read("*a")
	f:close()
	return cmd_output_result
end

local function phase2details(dst)
	local output = {}
	dst = string.match(dst,"^(.*)%.")	-- Removes the portnumber
	table.insert(output, {label="Outgoing", value=ip_xfrm("state list src ".. dst .. " | grep '^src'")})
	table.insert(output, {label="Incoming", value=ip_xfrm("state list dst ".. dst .. " | grep '^src'")})
	return output
end

local function racoonctl_table()
	local output = {}
	local cmd = "/usr/sbin/racoonctl -lll show-sa isakmp 2>/dev/null"
	local f = io.popen( cmd )
	local value = f:read("*a")
	f:close()
 	for k,v in pairs(format.string_to_table(value,"\n")) do
		if not ((string.find(v,"^Source")) or (#v == 0)) then
			output[k]={}
			local variable=format.string_to_table(v,"%s+")
			output[k]['Source']=cfe({
				name="Source",
				label="Source",
				value=variable[1],
				})
			output[k]['Destination']=cfe({
				name="Destination",
				label="Destination",
				value=variable[2],
				})
			output[k]['Cookies']=cfe({
				name="Cookies",
				label="Cookies",
				value=variable[3],
				})
			output[k]['St']=cfe({
				name="St",
				label="State",
				value=variable[4],
				descr=descr.state[variable[4]],
				})
			output[k]['S']=cfe({
				name="S",
				label="Side",
				value=variable[5],
				descr=descr.side[variable[5]],
				})
			output[k]['V']=cfe({
				name="V",
				label="Version",
				value=variable[6],
				})
			output[k]['E']=cfe({
				name="E",
				label="Exchange",
				value=variable[7],
				descr=descr.exchange[variable[7]],
				})
			output[k]['Created']=cfe({
				name="Created",
				label="Created",
				value=(variable[8] or "") .. " " .. (variable[9] or ""),
				})

			local phase2s = phase2details(variable[2])
			output[k]['Phase2']=cfe({
				name="Phase2",
				label="Phase2",
				value=variable[10],
				option=phase2s,
				})

			output[k]['Phase2details']=cfe({
				name="Phase2details",
				label="Phase2details",
				value=tostring(string.gsub(phase2s[1]['value'],"\n","<BR>")) .. tostring(string.gsub(phase2s[2]['value'],"\n","<BR>"))
				})
		end
	end
	return output
end

function process_status_text(procname)
	local t = procps.pidof(procname)
	if (t) and (#t > 0) then
		return "Enabled"
	else
		return "Disabled"
	end
end

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

function startstop_service ( self, action )
	local cmd = action.value
	local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, cmd)
	action.descr=cmdmessage
	action.errtxt=cmderror
	return cmdresult,action
end

function getstatus()
	local status = {}

	local value, errtxt = processinfo.package_version(packagename)
	status.version = cfe({ name = "version",
		label="Program version",
		value=value,
		errtxt=errtxt,
		 })

	status.status = cfe({ name="status",
		label="Program status",
		value=process_status_text(processname),
		})

	local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname)
	status.autostart = cfe({ name="autostart",
		label="Autostart sequence",
		value=autostart_sequense,
		errtxt=autostart_errtxt,
		})

	status.show_isakmp = cfe({ name="show_isakmp",
		label="Tunnels",
		option=racoonctl_table(),
		})

	status.ip_xfrm_policy = cfe({ name="ip_xfrm_policy",
		label="ip xfrm policy",
		value=ip_xfrm("policy"),
		})

	return status
end

function getconfig()
	local config = {}
	return config
end

function get_filedetails(self,num)
	local path
	if (num == "2") then 
		path = configfile2
	else
		path = configfile
	end
	local file = {}
	local filedetails = {}
	local config = {}
	local filenameerrtxt
	if (path) and (fs.is_file(path)) then
		filedetails = fs.stat(path)
		config = getconfig(path)
	else
		config = {}
		config.filename = {}
		config["filename"]["errtxt"]="Config file '".. path .. "' is missing!"
	end

	file["filename" .. (num or "")] = cfe({ 
		name="filename" .. (num or ""),
		label="File name",
		value=path,
		errtxt=filenameerrtxt
		})
	file["filesize" .. (num or "")] = cfe({ 
		name="filesize" .. (num or ""),
		label="File size",
		value=filedetails.size or 0,
		})
	file["mtime" .. (num or "")] = cfe({ 
		name="mtime" .. (num or ""),
		label="File date",
		value=filedetails.mtime or "---",
		})
	file["filecontent" .. (num or "")] = cfe({ 
		type="longtext",
		name="filecontent" .. (num or ""),
		label="File content",
		value=fs.read_file(path),
		})

	-- Sum all errors into one cfe
	local sumerrors = ""
	for k,v in pairs(config) do
		if (config[k]) and (config[k]["errtxt"]) and (config[k]["errtxt"] ~= "") then	
			sumerrors = sumerrors .. config[k]["errtxt"] .. "\n"
		end
	end
	if (sumerrors ~= "") then
		file["sumerrors" .. (num or "")] = cfe ({ 
			name="sumerrors" .. (num or ""),
			label = "Configuration errors",
			errtxt = string.match(sumerrors, "(.-)\n$"),
			})
	end

	return file
end

function update_filecontent (self, modifications)
	local path = configfile
	local file_result,err = fs.write_file(path, format.dostounix(modifications))
	return file_result
end
function update_filecontent2 (self, modifications)
	local path = configfile2
	local file_result,err = fs.write_file(path, format.dostounix(modifications))
	return file_result
end
function get_logfile ()
	local file = {}
	local cmdtxt = "grep " .. processname .. " /var/log/messages"
	local cmd, error = io.popen(cmdtxt ,r)
	local cmdoutput = cmd:read("*a")
	cmd:close()

	file["filename"] = cfe({ 
		name="filename",
		label="File name",
		value=cmdtxt,
		})

	file["filecontent"] = cfe({ 
		type="longtext",
		name="filecontent",
		label="File content",
		value=cmdoutput,
		})

	return file
end