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

-- Load libraries
require("modelfunctions")
require("fs")
require("format")

-- 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 i,line in pairs(format.string_to_table(value,"\n")) do
		if not ((string.find(line,"^Source")) or (#line == 0)) then
			entry={}
			local variable=format.string_to_table(line,"%s+")
			entry['Source']=cfe({
				label="Source",
				value=variable[1],
				})
			entry['Destination']=cfe({
				label="Destination",
				value=variable[2],
				})
			entry['Cookies']=cfe({
				label="Cookies",
				value=variable[3],
				})
			entry['St']=cfe({
				label="State",
				value=variable[4],
				descr=descr.state[variable[4]],
				})
			entry['S']=cfe({
				label="Side",
				value=variable[5],
				descr=descr.side[variable[5]],
				})
			entry['V']=cfe({
				label="Version",
				value=variable[6],
				})
			entry['E']=cfe({
				label="Exchange",
				value=variable[7],
				descr=descr.exchange[variable[7]],
				})
			entry['Created']=cfe({
				label="Created",
				value=(variable[8] or "") .. " " .. (variable[9] or ""),
				})

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

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

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

function startstop_service(action)
	return modelfunctions.startstop_service(processname, action)
end

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "Racoon Status")
end

function getstatusdetails()
	local status = {}
	status.show_isakmp = cfe({ type="list", value=racoonctl_table(), label="Tunnels" })
	status.ip_xfrm_policy = cfe({ type="longtext", value=ip_xfrm("policy"), label="ip xfrm policy" })

	return cfe({ type="group", value=status, label="Racoon Status Details" })
end

function get_racoonfiledetails()
	return modelfunctions.getfiledetails(configfile)
end

function update_racoonfiledetails(filedetails)
	return modelfunctions.setfiledetails(filedetails, {configfile})
end

function get_ipsecfiledetails()
	return modelfunctions.getfiledetails(configfile2)
end

function update_ipsecfiledetails(filedetails)
	return modelfunctions.setfiledetails(filedetails, {configfile2})
end