Commit 385bd779 authored by Kaarle Ritvanen's avatar Kaarle Ritvanen

eliminate explicit use of string module

parent 2656abb8
...@@ -107,12 +107,12 @@ if not util.contains({'translate', 'activate', 'fallback', 'flush', ...@@ -107,12 +107,12 @@ if not util.contains({'translate', 'activate', 'fallback', 'flush',
pol_paths = {} pol_paths = {}
for i, cls in ipairs{'mandatory', 'optional', 'private'} do for i, cls in ipairs{'mandatory', 'optional', 'private'} do
path = os.getenv('AWALL_PATH_'..string.upper(cls)) path = os.getenv('AWALL_PATH_'..cls:upper())
if path then pol_paths[cls] = util.split(path, ':') end if path then pol_paths[cls] = util.split(path, ':') end
end end
if stringy.endswith(arg[0], '/awall-cli') then if stringy.endswith(arg[0], '/awall-cli') then
basedir = string.sub(arg[0], 1, -11) basedir = arg[0]:sub(1, -11)
if not pol_paths.mandatory then if not pol_paths.mandatory then
pol_paths.mandatory = {'/etc/awall'} pol_paths.mandatory = {'/etc/awall'}
end end
...@@ -185,7 +185,7 @@ if not uerror.call( ...@@ -185,7 +185,7 @@ if not uerror.call(
expinput = input:expand() expinput = input:expand()
function capitalize(cls) function capitalize(cls)
return string.upper(string.sub(cls, 1, 1))..string.sub(cls, 2, -1) return cls:sub(1, 1):upper()..cls:sub(2, -1)
end end
for cls, objs in pairs(input.data) do for cls, objs in pairs(input.data) do
......
--[[ --[[
Host address resolver for Alpine Wall Host address resolver for Alpine Wall
Copyright (C) 2012 Kaarle Ritvanen Copyright (C) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details See LICENSE file for license details
]]-- ]]--
...@@ -13,7 +13,7 @@ local familypatterns = {inet='%d[%.%d/]+', ...@@ -13,7 +13,7 @@ local familypatterns = {inet='%d[%.%d/]+',
local function getfamily(addr, context) local function getfamily(addr, context)
for k, v in pairs(familypatterns) do for k, v in pairs(familypatterns) do
if string.match(addr, '^'..v..'$') then return k end if addr:match('^'..v..'$') then return k end
end end
context:error('Malformed host specification: '..addr) context:error('Malformed host specification: '..addr)
end end
...@@ -28,9 +28,11 @@ function resolve(host, context) ...@@ -28,9 +28,11 @@ function resolve(host, context)
dnscache[host] = {} dnscache[host] = {}
for rec in io.popen('dig -t ANY '..host):lines() do for rec in io.popen('dig -t ANY '..host):lines() do
local name, rtype, addr = local name, rtype, addr =
string.match(rec, '^('..familypatterns.domain..')%s+%d+%s+IN%s+(A+)%s+(.+)') rec:match(
'^('..familypatterns.domain..')%s+%d+%s+IN%s+(A+)%s+(.+)'
)
if name and string.sub(name, 1, string.len(host) + 1) == host..'.' then if name and name:sub(1, host:len() + 1) == host..'.' then
if rtype == 'A' then family = 'inet' if rtype == 'A' then family = 'inet'
elseif rtype == 'AAAA' then family = 'inet6' elseif rtype == 'AAAA' then family = 'inet6'
else family = nil end else family = nil end
......
--[[ --[[
Alpine Wall main module Alpine Wall main module
Copyright (C) 2012-2013 Kaarle Ritvanen Copyright (C) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details See LICENSE file for license details
]]-- ]]--
...@@ -49,7 +49,7 @@ function loadmodules(path) ...@@ -49,7 +49,7 @@ function loadmodules(path)
local modules = {} local modules = {}
for modfile in lfs.dir((path or '/usr/share/lua/5.1')..'/awall/modules') do for modfile in lfs.dir((path or '/usr/share/lua/5.1')..'/awall/modules') do
if stringy.endswith(modfile, '.lua') then if stringy.endswith(modfile, '.lua') then
table.insert(modules, 'awall.modules.'..string.sub(modfile, 1, -5)) table.insert(modules, 'awall.modules.'..modfile:sub(1, -5))
end end
end end
table.sort(modules) table.sort(modules)
...@@ -67,7 +67,7 @@ function loadmodules(path) ...@@ -67,7 +67,7 @@ function loadmodules(path)
end end
function loadclass(path) function loadclass(path)
assert(string.sub(path, 1, 1) ~= '%') assert(path:sub(1, 1) ~= '%')
return events[path] and events[path].class return events[path] and events[path].class
end end
...@@ -103,7 +103,7 @@ function Config:init(policyconfig) ...@@ -103,7 +103,7 @@ function Config:init(policyconfig)
end end
for i, path in ipairs(procorder) do for i, path in ipairs(procorder) do
if string.sub(path, 1, 1) ~= '%' then if path:sub(1, 1) ~= '%' then
local objs = self.objects[path] local objs = self.objects[path]
if objs then if objs then
for k, v in pairs(objs) do for k, v in pairs(objs) do
...@@ -118,7 +118,7 @@ function Config:init(policyconfig) ...@@ -118,7 +118,7 @@ function Config:init(policyconfig)
end end
for i, event in ipairs(procorder) do for i, event in ipairs(procorder) do
if string.sub(event, 1, 1) == '%' then if event:sub(1, 1) == '%' then
local r = events[event].rules local r = events[event].rules
if r then if r then
if type(r) == 'function' then r = r(self.objects) end if type(r) == 'function' then r = r(self.objects) end
......
...@@ -173,7 +173,7 @@ function Rule:zoneoptfrags() ...@@ -173,7 +173,7 @@ function Rule:zoneoptfrags()
if zin == zout then return {} end if zin == zout then return {} end
local dir, z = 'in', zin local dir, z = 'in', zin
if zin == fwzone then dir, z = 'out', zout end if zin == fwzone then dir, z = 'out', zout end
chain = string.upper(dir)..'PUT' chain = dir:upper()..'PUT'
ofrags = zofs(z, dir) ofrags = zofs(z, dir)
elseif not zin or not zout then elseif not zin or not zout then
...@@ -243,7 +243,7 @@ function Rule:servoptfrags() ...@@ -243,7 +243,7 @@ function Rule:servoptfrags()
ports[sdef.proto], ports[sdef.proto],
util.maplist( util.maplist(
sdef.port, sdef.port,
function(p) return string.gsub(p, '-', ':') end function(p) return tostring(p):gsub('-', ':') end
) )
) )
else ports[sdef.proto] = {} end else ports[sdef.proto] = {} end
...@@ -310,7 +310,7 @@ function Rule:servoptfrags() ...@@ -310,7 +310,7 @@ function Rule:servoptfrags()
local sep = pc == 0 and '' or ',' local sep = pc == 0 and '' or ','
local port = ports[1] local port = ports[1]
pc = pc + (string.find(port, ':') and 2 or 1) pc = pc + (port:find(':') and 2 or 1)
if pc > 15 then break end if pc > 15 then break end
opts = opts..sep..port opts = opts..sep..port
......
...@@ -49,7 +49,7 @@ function Filter:init(...) ...@@ -49,7 +49,7 @@ function Filter:init(...)
-- alpine v2.4 compatibility -- alpine v2.4 compatibility
if util.contains({'logdrop', 'logreject'}, self.action) then if util.contains({'logdrop', 'logreject'}, self.action) then
self:warning('Deprecated action: '..self.action) self:warning('Deprecated action: '..self.action)
self.action = string.sub(self.action, 4, -1) self.action = self.action:sub(4, -1)
end end
local log = require('awall').loadclass('log').get local log = require('awall').loadclass('log').get
...@@ -100,7 +100,7 @@ function Filter:trules() ...@@ -100,7 +100,7 @@ function Filter:trules()
if not self.dest then if not self.dest then
self:error('Destination address must be specified with DNAT') self:error('Destination address must be specified with DNAT')
end end
if string.find(self.dnat, '/') then if self.dnat:find('/') then
self:error('DNAT target cannot be a network address') self:error('DNAT target cannot be a network address')
end end
for i, attr in ipairs({'ipsec', 'ipset'}) do for i, attr in ipairs({'ipsec', 'ipset'}) do
...@@ -177,7 +177,7 @@ end ...@@ -177,7 +177,7 @@ end
function Filter:actiontarget() function Filter:actiontarget()
if self.action == 'tarpit' then return 'tarpit' end if self.action == 'tarpit' then return 'tarpit' end
if util.contains({'accept', 'drop', 'reject'}, self.action) then if util.contains({'accept', 'drop', 'reject'}, self.action) then
return string.upper(self.action) return self.action:upper()
end end
self:error('Invalid filter action: '..self.action) self:error('Invalid filter action: '..self.action)
end end
...@@ -268,11 +268,7 @@ function stateful(config) ...@@ -268,11 +268,7 @@ function stateful(config)
) )
for i, chain in ipairs({'INPUT', 'OUTPUT'}) do for i, chain in ipairs({'INPUT', 'OUTPUT'}) do
table.insert( table.insert(
er, er, {chain=chain, opts='-'..chain:sub(1, 1):lower()..' lo'}
{
chain=chain,
opts='-'..string.lower(string.sub(chain, 1, 1))..' lo'
}
) )
end end
extend( extend(
......
...@@ -59,7 +59,7 @@ function Log:target() ...@@ -59,7 +59,7 @@ function Log:target()
local mode = self.mode or 'log' local mode = self.mode or 'log'
if not optmap[mode] then self:error('Invalid logging mode: '..mode) end if not optmap[mode] then self:error('Invalid logging mode: '..mode) end
local res = string.upper(mode) local res = mode:upper()
for s, t in pairs(optmap[mode]) do for s, t in pairs(optmap[mode]) do
if self[s] then res = res..' --'..mode..'-'..t..' '..self[s] end if self[s] then res = res..' --'..mode..'-'..t..' '..self[s] end
end end
......
...@@ -34,8 +34,9 @@ function PolicyConfig:expand() ...@@ -34,8 +34,9 @@ function PolicyConfig:expand()
local visited = {} local visited = {}
local pattern = '%$(%a[%w_]*)' local pattern = '%$(%a[%w_]*)'
while type(value) == 'string' and string.find(value, pattern) do while type(value) == 'string' do
local si, ei, name = string.find(value, pattern) local si, ei, name = value:find(pattern)
if not si then break end
if contains(visited, name) then if contains(visited, name) then
raise('Circular variable definition: '..name) raise('Circular variable definition: '..name)
...@@ -45,9 +46,9 @@ function PolicyConfig:expand() ...@@ -45,9 +46,9 @@ function PolicyConfig:expand()
local var = self.data.variable[name] local var = self.data.variable[name]
if var == nil then raise('Invalid variable reference: '..name) end if var == nil then raise('Invalid variable reference: '..name) end
if si == 1 and ei == string.len(value) then value = var if si == 1 and ei == value:len() then value = var
elseif contains({'number', 'string'}, type(var)) then elseif contains({'number', 'string'}, type(var)) then
value = string.sub(value, 1, si - 1)..var..string.sub(value, ei + 1, -1) value = value:sub(1, si - 1)..var..value:sub(ei + 1, -1)
else else
raise('Attempted to concatenate complex variable: '..name) raise('Attempted to concatenate complex variable: '..name)
end end
...@@ -110,12 +111,12 @@ function PolicySet:init(dirs) ...@@ -110,12 +111,12 @@ function PolicySet:init(dirs)
for i, cls in ipairs{'private', 'optional', 'mandatory'} do for i, cls in ipairs{'private', 'optional', 'mandatory'} do
for i, dir in ipairs(dirs[cls] or defdirs[cls]) do for i, dir in ipairs(dirs[cls] or defdirs[cls]) do
for fname in lfs.dir(dir) do for fname in lfs.dir(dir) do
local si, ei, name = string.find(fname, '^([%w-]+)%.json$') local si, ei, name = fname:find('^([%w-]+)%.json$')
if name then if name then
local pol = self.policies[name] local pol = self.policies[name]
local path = dir..'/'..fname local path = dir..'/'..fname
if string.sub(path, 1, 1) ~= '/' then if path:sub(1, 1) ~= '/' then
path = lfs.currentdir()..'/'..path path = lfs.currentdir()..'/'..path
end end
...@@ -173,7 +174,7 @@ function PolicySet:load() ...@@ -173,7 +174,7 @@ function PolicySet:load()
end end
for i, name in listpairs(data.import) do for i, name in listpairs(data.import) do
if string.sub(name, 1, 1) ~= '%' then if name:sub(1, 1) ~= '%' then
local pol = self.policies[name] local pol = self.policies[name]
if not pol then if not pol then
raise('Invalid policy reference from '..policy.name..': '..name) raise('Invalid policy reference from '..policy.name..': '..name)
......
...@@ -15,8 +15,8 @@ function call(f, ...) ...@@ -15,8 +15,8 @@ function call(f, ...)
return xpcall( return xpcall(
function() f(unpack(arg)) end, function() f(unpack(arg)) end,
function(msg) function(msg)
local si, ei = string.find(msg, prefix, 1, true) local si, ei = msg:find(prefix, 1, true)
if si then msg = 'awall: '..string.sub(msg, ei + 1, -1) end if si then msg = 'awall: '..msg:sub(ei + 1, -1) end
io.stderr:write(msg..'\n') io.stderr:write(msg..'\n')
if not si then io.stderr:write(debug.traceback()..'\n') end if not si then io.stderr:write(debug.traceback()..'\n') end
end end
......
--[[ --[[
Utility module for Alpine Wall Utility module for Alpine Wall
Copyright (C) 2012-2013 Kaarle Ritvanen Copyright (C) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details See LICENSE file for license details
]]-- ]]--
...@@ -11,13 +11,13 @@ function split(s, sep) ...@@ -11,13 +11,13 @@ function split(s, sep)
if s == '' then return {} end if s == '' then return {} end
local res = {} local res = {}
while true do while true do
local si, ei = string.find(s, sep, 1, true) local si, ei = s:find(sep, 1, true)
if not si then if not si then
table.insert(res, s) table.insert(res, s)
return res return res
end end
table.insert(res, string.sub(s, 1, si - 1)) table.insert(res, s:sub(1, si - 1))
s = string.sub(s, ei + 1, -1) s = s:sub(ei + 1, -1)
end end
end end
...@@ -104,7 +104,7 @@ function printtabulars(tables) ...@@ -104,7 +104,7 @@ function printtabulars(tables)
for i, tbl in ipairs(tables) do for i, tbl in ipairs(tables) do
for j, row in ipairs(tbl) do for j, row in ipairs(tbl) do
for k, col in ipairs(row) do for k, col in ipairs(row) do
colwidth[k] = math.max(colwidth[k] or 0, string.len(col)) colwidth[k] = math.max(colwidth[k] or 0, col:len())
end end
end end
end end
...@@ -114,7 +114,7 @@ function printtabulars(tables) ...@@ -114,7 +114,7 @@ function printtabulars(tables)
if k > 1 then io.write(' ') end if k > 1 then io.write(' ') end
io.write(row[k]) io.write(row[k])
if k < #row then if k < #row then
for l = 1,colwidth[k] - string.len(row[k]) do io.write(' ') end for l = 1,colwidth[k] - row[k]:len() do io.write(' ') end
end end
end end
io.write('\n') io.write('\n')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment