summaryrefslogtreecommitdiffstats
path: root/lualib/lua_cfg_transform.lua
blob: ae05eed592b74e6982645a51defd6fe34cf33f61 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
--[[
Copyright (c) 2017, 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.
]]--

local logger = require "rspamd_logger"
local lua_util = require "lua_util"
local rspamd_util = require "rspamd_util"

local function is_implicit(t)
  local mt = getmetatable(t)

  return mt and mt.class and mt.class == 'ucl.type.impl_array'
end

local function metric_pairs(t)
  -- collect the keys
  local keys = {}
  local implicit_array = is_implicit(t)

  local function gen_keys(tbl)
    if implicit_array then
      for _,v in ipairs(tbl) do
        if v.name then
          table.insert(keys, {v.name, v})
          v.name = nil
        else
          -- Very tricky to distinguish:
          -- group {name = "foo" ... } + group "blah" { ... }
          for gr_name,gr in pairs(v) do
            if type(gr_name) ~= 'number' then
              -- We can also have implicit arrays here
              local gr_implicit = is_implicit(gr)

              if gr_implicit then
                for _,gr_elt in ipairs(gr) do
                  table.insert(keys, {gr_name, gr_elt})
                end
              else
                table.insert(keys, {gr_name, gr})
              end
            end
          end
        end
      end
    else
      if tbl.name then
        table.insert(keys, {tbl.name, tbl})
        tbl.name = nil
      else
        for k,v in pairs(tbl) do
          if type(k) ~= 'number' then
            -- We can also have implicit arrays here
            local sym_implicit = is_implicit(v)

            if sym_implicit then
              for _,elt in ipairs(v) do
                table.insert(keys, {k, elt})
              end
            else
              table.insert(keys, {k, v})
            end
          end
        end
      end
    end
  end

  gen_keys(t)

  -- return the iterator function
  local i = 0
  return function()
    i = i + 1
    if keys[i] then
      return keys[i][1], keys[i][2]
    end
  end
end

local function group_transform(cfg, k, v)
  if v.name then k = v.name end

  local new_group = {
    symbols = {}
  }

  if v.enabled then new_group.enabled = v.enabled end
  if v.disabled then new_group.disabled = v.disabled end
  if v.max_score then new_group.max_score = v.max_score end

  if v.symbol then
    for sk,sv in metric_pairs(v.symbol) do
      if sv.name then
        sk = sv.name
        sv.name = nil -- Remove field
      end

      new_group.symbols[sk] = sv
    end
  end

  if not cfg.group then cfg.group = {} end

  if cfg.group[k] then
    cfg.group[k] = lua_util.override_defaults(cfg.group[k], new_group)
  else
    cfg.group[k] = new_group
  end

  logger.infox("overriding group %s from the legacy metric settings", k)
end

local function symbol_transform(cfg, k, v)
  -- first try to find any group where there is a definition of this symbol
  for gr_n, gr in pairs(cfg.group) do
    if gr.symbols and gr.symbols[k] then
      -- We override group symbol with ungrouped symbol
      logger.infox("overriding group symbol %s in the group %s", k, gr_n)
      gr.symbols[k] = lua_util.override_defaults(gr.symbols[k], v)
      return
    end
  end
  -- Now check what Rspamd knows about this symbol
  local sym = rspamd_config:get_metric_symbol(k)

  if not sym or not sym.group then
    -- Otherwise we just use group 'ungrouped'
    if not cfg.group.ungrouped then
      cfg.group.ungrouped = {
        symbols = {}
      }
    end

    cfg.group.ungrouped.symbols[k] = v
    logger.debugx("adding symbol %s to the group 'ungrouped'", k)
  end
end

local function test_groups(groups)
  for gr_name, gr in pairs(groups) do
    if not gr.symbols then
      local cnt = 0
      for _,_ in pairs(gr) do cnt = cnt + 1 end

      if cnt == 0 then
        logger.debugx('group %s is empty', gr_name)
      else
        logger.infox('group %s has no symbols', gr_name)
      end
    end
  end
end

local function convert_metric(cfg, metric)
  if metric.actions then
    cfg.actions = lua_util.override_defaults(cfg.actions, metric.actions)
    logger.infox("overriding actions from the legacy metric settings")
  end
  if metric.unknown_weight then
    cfg.actions.unknown_weight = metric.unknown_weight
  end

  if metric.subject then
    logger.infox("overriding subject from the legacy metric settings")
    cfg.actions.subject = metric.subject
  end

  if metric.group then
    for k, v in metric_pairs(metric.group) do
      group_transform(cfg, k, v)
    end
  else
    if not cfg.group then
      cfg.group = {
        ungrouped = {
          symbols = {}
        }
      }
    end
  end

  if metric.symbol then
    for k, v in metric_pairs(metric.symbol) do
      symbol_transform(cfg, k, v)
    end
  end

  return cfg
end

-- Converts a table of groups indexed by number (implicit array) to a
-- merged group definition
local function merge_groups(groups)
  local ret = {}
  for k,gr in pairs(groups) do
    if type(k) == 'number' then
      for key,sec in pairs(gr) do
        ret[key] = sec
      end
    else
      ret[k] = gr
    end
  end

  return ret
end

-- Checks configuration files for statistics
local function check_statistics_sanity()
  local local_conf = rspamd_paths['LOCAL_CONFDIR']
  local local_stat = string.format('%s/local.d/%s', local_conf,
      'statistic.conf')
  local local_bayes = string.format('%s/local.d/%s', local_conf,
      'classifier-bayes.conf')

  if rspamd_util.file_exists(local_stat) and
      rspamd_util.file_exists(local_bayes) then
    logger.warnx(rspamd_config, 'conflicting files %s and %s are found: '..
        'Rspamd classifier configuration might be broken!', local_stat, local_bayes)
  end
end

return function(cfg)
  local ret = false

  if cfg['metric'] then
    for _, v in metric_pairs(cfg.metric) do
      cfg = convert_metric(cfg, v)
    end
    ret = true
  end

  if cfg.symbols then
    for k, v in metric_pairs(cfg.symbols) do
      symbol_transform(cfg, k, v)
    end
  end

  check_statistics_sanity()

  if not cfg.actions then
    logger.errx('no actions defined')
  else
    -- Perform sanity check for actions
    local actions_defs = {'no action', 'no_action', -- In case if that's added
                          'greylist', 'add header', 'add_header',
                          'rewrite subject', 'rewrite_subject', 'reject'}

    if not cfg.actions['no action'] and not cfg.actions['no_action'] and
            not cfg.actions['accept'] then
      for _,d in ipairs(actions_defs) do
        if cfg.actions[d] and type(cfg.actions[d]) == 'number' then
          if cfg.actions[d] < 0 then
            cfg.actions['no action'] = cfg.actions[d] - 0.001
            logger.infox('set no action score to: %s, as action %s has negative score',
                    cfg.actions['no action'], d)
            break
          end
        end
      end
    end

    local actions_set = {}
    for _,d in ipairs(actions_defs) do
      actions_set[d] = true
    end

    -- Now check actions section for garbadge
    actions_set['unknown_weight'] = true
    actions_set['grow_factor'] = true
    actions_set['subject'] = true

    for k,_ in pairs(cfg.actions) do
      if not actions_set[k] then
        logger.warnx('unknown element in actions section: %s', k)
      end
    end
  end

  if not cfg.group then
    logger.errx('no symbol groups defined')
  else
    if cfg.group[1] then
      -- We need to merge groups
      cfg.group = merge_groups(cfg.group)
      ret = true
    end
    test_groups(cfg.group)
  end

  -- Deal with dkim settings
  if not cfg.dkim then cfg.dkim = {} end

  if not cfg.dkim.sign_headers then
    local sec = cfg.dkim_signing
    if sec and sec[1] then sec = cfg.dkim_signing[1] end

    if sec and sec.sign_headers then
      cfg.dkim.sign_headers = sec.sign_headers
    end
  end

  if cfg.dkim and cfg.dkim.sign_headers and type(cfg.dkim.sign_headers) == 'table' then
    -- Flatten
    cfg.dkim.sign_headers = table.concat(cfg.dkim.sign_headers, ':')
  end

  -- Try to find some obvious issues with configuration
  for k,v in pairs(cfg) do
    if type(v) == 'table' and v[k] and type (v[k]) == 'table' then
      logger.errx('nested section: %s { %s { ... } }, it is likely a configuration error',
              k, k)
    end
  end

  return ret, cfg
end