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

require("fs")
require("procps")
require("getopts")
require("format")
require("daemoncontrol")
require("validator")

local configfile = "/etc/racoon/racoon.conf"
local processname = "racoon"
local pkgname = "ipsec-tools"
local baseurl = "/etc/racoon/"

local function get_version()
	local cmd_output_result, cmd_output_error
	local cmd = "/sbin/apk_version -vs " .. pkgname .." 2>/dev/null"
	local f = io.popen( cmd )
	local cmdresult = f:read("*l")
	if (cmdresult) and (#cmdresult > 0) then
		cmd_output_result = string.match(cmdresult,"^%S*") or "Unknown"
	else
		cmd_output_error = "Program not installed"
	end	
	f:close()
	return cmd_output_result,cmd_output_error
end

local function autostarts()
	local cmd_output_result
	local cmd = "/sbin/rc_status | egrep '^S' | egrep '" .. processname .."' 2>/dev/null"
	local f = io.popen( cmd )
	local cmdresult = f:read("*a")
	if (cmdresult) and (#cmdresult > 0) then
		cmd_output_result = "Process will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')"
	else
		cmd_output_error = "Not programmed to autostart"
	end	
	f:close()
	return cmd_output_result

end

local function racoonctl()
	local cmd_output_result, cmd_output_error
	local cmd = "/usr/sbin/racoonctl -lll show-sa isakmp 2>/dev/null"
	local f = io.popen( cmd )
	local cmd_output_result = f:read("*a")
	f:close()
	return cmd_output_result,cmd_output_error
end

local function racoonctl_table()
	local value = racoonctl()
	local output = {}
 	for k,v in pairs(format.string_to_table(value,"\n")) do
		if not (string.find(v,"^Source")) 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="Variable St",
			value=variable[4],
			})
		output[k]['S']=cfe({
			name="S",
			label="Variable S",
			value=variable[5],
			})
		output[k]['V']=cfe({
			name="V",
			label="Variable V",
			value=variable[6],
			})
		output[k]['E']=cfe({
			name="E",
			label="Variable E",
			value=variable[7],
			})
		output[k]['Created']=cfe({
			name="Created",
			label="Created",
			value=(variable[8] or "") .. " " .. (variable[9] or ""),
			})

		output[k]['Phase2']=cfe({
			name="Phase2",
			label="Phase2",
			value=variable[10],
			})
	end
	end
	return output
end

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

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

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


function getstatus()
	local status = {}
	status.version = cfe({ name = "version",
		label="Program version",
		value=get_version(),
		 })
	status.status = cfe({ name="status",
		label="Program status",
		value=process_status_text(processname),
		})
	local autostart_sequense, autostart_errtxt = autostarts()
	status.autostart = cfe({ name="autostart",
		label="Autostart sequence",
		value=autostart_sequense,
		errtxt=autostart_errtxt,
		})

	status.show_isakmp2 = cfe({ name="show_isakmp2",
		label="Tunnels",
		value=racoonctl_table(),
		})

	status.show_isakmp = cfe({ name="show_isakmp",
		label="racoonctl -lll show-sa isakmp",
		value=racoonctl(),
		})

	status.ip_xfrm_state = cfe({ name="show_esp",
		label="ip xfrm state",
		value=ip_xfrm("state"),
		})

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

	return status
end