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","
")) .. tostring(string.gsub(phase2s[2]['value'],"\n","
"))
})
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