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
|
--[[
Copyright (c) 2020, Vsevolod Stakhov <vsevolod@highsecure.ru>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
-- Controller maps plugin
local maps_cache
local maps_aliases
local lua_util = require "lua_util"
local function maybe_fill_maps_cache()
if not maps_cache then
maps_cache = {}
maps_aliases = {}
local maps = rspamd_config:get_maps()
for _,m in ipairs(maps) do
-- We get the first url here and that's it
local url = m:get_uri()
if url ~= 'static' then
if not maps_cache[url] then
local alias = url:match('/([^/]+)$')
maps_cache[url] = m
if not maps_aliases[alias] then
maps_aliases[alias] = url
end
else
-- Do not override, as we don't care about duplicate maps that come from different
-- sources.
-- In theory, that should be cached but there are some exceptions even so far...
end
end
end
end
end
local function check_specific_map(value, uri, m, results)
local value = m:get_key(value)
if value then
local result = {
map = uri,
alias = uri:match('/([^/]+)$'),
value = value
}
table.insert(results, result)
end
end
local function handle_query_map(_, conn, req_params)
maybe_fill_maps_cache()
if req_params.value and req_params.value ~= '' then
local results = {}
for uri,m in pairs(maps_cache) do
check_specific_map(req_params.value, uri, m, results)
end
conn:send_ucl{
success = (#results > 0),
results = results
}
else
conn:send_error(404, 'missing value')
end
end
local function handle_query_specific_map(_, conn, req_params)
maybe_fill_maps_cache()
if req_params.value and req_params.value ~= '' then
local maps_to_check = maps_cache
if req_params.maps then
local map_names = lua_util.str_split(req_params.maps, ',')
maps_to_check = {}
for _,mn in ipairs(map_names) do
if maps_cache[mn] then
maps_to_check[mn] = maps_cache[mn]
else
local alias = maps_aliases[mn]
if alias then
maps_to_check[alias] = maps_cache[alias]
else
conn:send_error(404, 'no such map: ' .. mn)
end
end
end
end
local results = {}
for uri,m in pairs(maps_to_check) do
check_specific_map(req_params.value, uri, m, results)
end
conn:send_ucl{
success = (#results > 0),
results = results
}
else
conn:send_error(404, 'missing value')
end
end
local function handle_list_maps(_, conn, _)
maybe_fill_maps_cache()
conn:send_ucl{
maps = lua_util.keys(maps_cache),
aliases = maps_aliases
}
end
return {
query = {
handler = handle_query_map,
enable = false,
},
query_specific = {
handler = handle_query_specific_map,
enable = false,
},
list = {
handler = handle_list_maps,
enable = false,
},
}
|