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
|
From 463c4117d2c985b3e6d693508240fdf34272f22d Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Tue, 9 May 2017 14:48:24 +0200
Subject: [PATCH] policy: add support for loading yaml and toml policies
The lua modules for yaml and toml are optional, and will only be
required if there are any *.yaml or *.toml files.
---
awall/policy.lua | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/awall/policy.lua b/awall/policy.lua
index 1509d3f..cb2c93a 100644
--- a/awall/policy.lua
+++ b/awall/policy.lua
@@ -17,7 +17,6 @@ local map = util.map
local printmsg = util.printmsg
-local json = require('cjson')
local posix = require('posix')
@@ -75,7 +74,7 @@ function Policy:load()
local data = file:read('*all')
file:close()
- local success, res = pcall(json.decode, data)
+ local success, res = pcall(self.decode, data)
if success then return res end
raise(res..' while parsing '..self.path)
end
@@ -109,14 +108,21 @@ local PolicySet = class()
function PolicySet:init(dirs)
local confdir = (dirs.mandatory or defdirs.mandatory)[1]
+ local decoder = {
+ json = { mod="cjson", func="decode" },
+ yaml = { mod="lyaml", func="load" },
+ toml = { mod="toml", func="parse" },
+ }
self.policies = {}
for i, cls in ipairs{'private', 'optional', 'mandatory'} do
for i, dir in ipairs(dirs[cls] or defdirs[cls]) do
for _, fname in ipairs(posix.dir(dir)) do
- local si, ei, name = fname:find('^([%w-]+)%.json$')
- if name then
+ local si, ei, name, suff = fname:find('^([%w-]+)%.([jyt][sao][om][nl])$') -- json|yaml|toml
+
+ if name and suff and decoder[suff] then
local pol = self.policies[name]
+ local decmod = require(decoder[suff].mod)
local path = dir..'/'..fname
if path:sub(1, 1) ~= '/' then
@@ -141,7 +147,8 @@ function PolicySet:init(dirs)
path=path,
fname=fname,
loc=loc,
- confdir=confdir
+ confdir=confdir,
+ decode=decmod[decoder[suff].func]
}
end
end
--
2.12.2
|