summaryrefslogtreecommitdiffstats
path: root/iptables-model.lua
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2013-01-01 18:14:01 +0000
committerTed Trask <ttrask01@yahoo.com>2013-01-01 18:14:01 +0000
commit38f5d1fb5817aaf7a609662907472a967f1253df (patch)
tree4c8c2c9b9a0eb72dfe37bfb06a11ffc7d113496b /iptables-model.lua
parent1ef22307ce6d7ac34033f2d0a85eb003c37b98c1 (diff)
downloadacf-iptables-38f5d1fb5817aaf7a609662907472a967f1253df.tar.bz2
acf-iptables-38f5d1fb5817aaf7a609662907472a967f1253df.tar.xz
Replace io.popen with modelfunctions.run_executable
Diffstat (limited to 'iptables-model.lua')
-rw-r--r--iptables-model.lua69
1 files changed, 17 insertions, 52 deletions
diff --git a/iptables-model.lua b/iptables-model.lua
index 537baea..8b567c3 100644
--- a/iptables-model.lua
+++ b/iptables-model.lua
@@ -11,7 +11,6 @@ local packagename = "iptables"
local servicename = "iptables"
local configfile = "/etc/conf.d/iptables"
-local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
local tables = {"filter", "nat", "mangle"}
local details
@@ -21,12 +20,11 @@ local details
local getdetails = function()
if not details then
details = {}
- local cmd = path .. "iptables -t filter -n -L -v"
+ local cmd = {"iptables", "-t", "filter", "-n", "-L", "-v"}
for i,tab in ipairs(tables) do
- local f = io.popen( (string.gsub(cmd, "filter", tab)) )
details[tab] = {table=tab}
- local file = f:read("*a")
- f:close()
+ cmd[3] = tab
+ local file = modelfunctions.run_executable(cmd)
for line in string.gmatch(file, "([^\n]+)") do
if string.match(line, "^Chain") then
local name = string.match(line, "^%w+%s+(%S+)")
@@ -59,9 +57,7 @@ local find_chain = function(tab, chain)
end
local save = function()
- local cmd = path .. "/etc/init.d/"..servicename.." save 2>&1"
- local f = io.popen( cmd )
- f:close()
+ modelfunctions.run_executable({"/etc/init.d/"..servicename, "save"}, true)
details = nil
end
@@ -256,13 +252,7 @@ function update_chain(self, chain)
if success then
if chain.value.policy then
- local cmd = path .. "iptables -t "..format.escapespecialcharacters(chain.value.table.value).." -P "..format.escapespecialcharacters(chain.value.chain.value).." "..format.escapespecialcharacters(chain.value.policy.value).." 2>&1"
- local f = io.popen(cmd)
- local errtxt = f:read("*a")
- f:close()
- if errtxt ~= "" then
- chain.errtxt = errtxt
- end
+ chain.descr, chain.errtxt = modelfunctions.run_executable({"iptables", "-t", chain.value.table.value, "-P", chain.value.chain.value, chain.value.policy.value}, true)
save()
end
else
@@ -288,13 +278,7 @@ function create_chain(self, chain)
end
if success then
- local cmd = path .. "iptables -t "..format.escapespecialcharacters(chain.value.table.value).." -N "..format.escapespecialcharacters(chain.value.chain.value).." 2>&1"
- local f = io.popen(cmd)
- local errtxt = f:read("*a")
- if errtxt ~= "" then
- chain.errtxt = errtxt
- end
- f:close()
+ chain.descr, chain.errtxt = modelfunctions.run_executable({"iptables", "-t", chain.value.table.value, "-N", chain.value.chain.value}, true)
save()
else
chain.errtxt = "Failed to create chain"
@@ -319,12 +303,7 @@ function delete_chain(self, chain)
elseif chn.references and tonumber(chn.references) > 0 then
chain.errtxt = "Cannot delete chain with references"
else
- local cmd = path .. "iptables -t "..format.escapespecialcharacters(chain.value.table.value).." -X "..format.escapespecialcharacters(chain.value.chain.value).." 2>&1"
- local f = io.popen(cmd)
- local errtxt = f:read("*a")
- if errtxt ~= "" then
- chain.errtxt = errtxt
- end
+ chain.descr, chain.errtxt = modelfunctions.run_executable({"iptables", "-t", chain.value.table.value, "-X", chain.value.chain.value}, true)
save()
end
@@ -490,19 +469,17 @@ function create_rule(self, rule)
if success then
local spec = generate_rule_specification(rule)
- local cmd = path .. "iptables -t " .. format.escapespecialcharacters(rule.value.table.value)
+ local cmd = {"iptables", "-t", rule.value.table.value}
if tonumber(rule.value.position.value) then
- cmd = cmd .. " -I " .. format.escapespecialcharacters(rule.value.chain.value) .. " " .. format.escapespecialcharacters(rule.value.position.value)
+ cmd[#cmd+1] = "-I"
+ cmd[#cmd+1] = rule.value.chain.value
+ cmd[#cmd+1] = rule.value.position.value
else
- cmd = cmd .. " -A " .. format.escapespecialcharacters(rule.value.chain.value)
- end
- cmd = cmd .. " " .. spec .. " 2>&1"
- local f = io.popen(cmd)
- rule.errtxt = f:read("*a")
- f:close()
- if string.match(rule.errtxt, "^%s*$") then
- rule.errtxt = nil
+ cmd[#cmd+1] = "-A"
+ cmd[#cmd+1] = rule.value.chain.value
end
+ cmd[#cmd+1] = spec
+ rule.descr, rule.errtxt = modelfunctions.run_executable(cmd, true)
else
rule.errtxt = "Failed to create rule"
end
@@ -519,13 +496,7 @@ function update_rule(self, rule)
if success then
local spec = generate_rule_specification(rule)
- local cmd = path .. "iptables -t " .. format.escapespecialcharacters(rule.value.table.value) .. " -R " .. format.escapespecialcharacters(rule.value.chain.value) .. " " .. format.escapespecialcharacters(rule.value.position.value) .. " " .. spec .. " 2>&1"
- local f = io.popen(cmd)
- rule.errtxt = f:read("*a")
- f:close()
- if string.match(rule.errtxt, "^%s*$") then
- rule.errtxt = nil
- end
+ rule.descr, rule.errtxt = modelfunctions.run_executable({"iptables", "-t", rule.value.table.value, "-R", rule.value.chain.value, rule.value.position.value, spec}, true)
else
rule.errtxt = "Failed to update rule"
end
@@ -546,13 +517,7 @@ function delete_rule(self, rule)
if "" == rule.value.table.value or "" == rule.value.chain.value or "" == rule.value.position.value then
rule.errtxt = "Incomplete specification - must define table, chain, and position"
else
- local cmd = path .. "iptables -t " .. format.escapespecialcharacters(rule.value.table.value) .. " -D " .. format.escapespecialcharacters(rule.value.chain.value) .. " " .. format.escapespecialcharacters(rule.value.position.value)
- local f = io.popen(cmd)
- rule.errtxt = f:read("*a")
- f:close()
- if string.match(rule.errtxt, "^%s*$") then
- rule.errtxt = nil
- end
+ rule.descr, rule.errtxt = modelfunctions.run_executable({"iptables", "-t", rule.value.table.value, "-D", rule.value.chain.value, rule.value.position.value}, true)
end
return rule
end