You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_settings.lua 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. --[[
  2. Copyright (c) 2019, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. --[[[
  14. -- @module lua_settings
  15. -- This module contains internal helpers for the settings infrastructure in Rspamd
  16. -- More details at https://rspamd.com/doc/configuration/settings.html
  17. --]]
  18. local exports = {}
  19. local known_ids = {}
  20. local post_init_added = false
  21. local function register_settings_cb()
  22. for _,set in pairs(known_ids) do
  23. local s = set.settings.apply
  24. local enabled_symbols = {}
  25. local seen_enabled = false
  26. local disabled_symbols = {}
  27. local seen_disabled = false
  28. -- Enabled map
  29. if s.symbols_enabled then
  30. for _,sym in ipairs(s.symbols_enabled) do
  31. enabled_symbols[sym] = true
  32. seen_enabled = true
  33. end
  34. end
  35. if s.groups_enabled then
  36. for _,gr in ipairs(s.groups_enabled) do
  37. local syms = rspamd_config:get_group_symbols(gr)
  38. if syms then
  39. for _,sym in ipairs(syms) do
  40. enabled_symbols[sym] = true
  41. seen_enabled = true
  42. end
  43. end
  44. end
  45. end
  46. -- Disabled map
  47. if s.symbols_disabled then
  48. for _,sym in ipairs(s.symbols_disabled) do
  49. disabled_symbols[sym] = true
  50. seen_disabled = true
  51. end
  52. end
  53. if s.groups_disabled then
  54. for _,gr in ipairs(s.groups_disabled) do
  55. local syms = rspamd_config:get_group_symbols(gr)
  56. if syms then
  57. for _,sym in ipairs(syms) do
  58. disabled_symbols[sym] = true
  59. seen_disabled = true
  60. end
  61. end
  62. end
  63. end
  64. -- Deal with complexity to avoid mess in C
  65. if not seen_enabled then enabled_symbols = nil end
  66. if not seen_disabled then disabled_symbols = nil end
  67. rspamd_config:register_settings_id(set.name, enabled_symbols, disabled_symbols)
  68. -- Remove to avoid clash
  69. s.symbols_disabled = nil
  70. s.symbols_enabled = nil
  71. s.groups_enabled = nil
  72. s.groups_disabled = nil
  73. end
  74. end
  75. -- Returns numeric representation of the settings id
  76. local function numeric_settings_id(str)
  77. local cr = require "rspamd_cryptobox_hash"
  78. local util = require "rspamd_util"
  79. local ret = util.unpack("I4",
  80. cr.create_specific('xxh64'):update(str):bin())
  81. return ret
  82. end
  83. local function register_settings_id(str, settings)
  84. local numeric_id = numeric_settings_id(str)
  85. if known_ids[numeric_id] then
  86. -- Might be either rewrite or a collision
  87. if known_ids[numeric_id].name ~= str then
  88. local logger = require "rspamd_logger"
  89. logger.errx(rspamd_config, 'settings ID clash! id %s maps to %s and conflicts with %s',
  90. numeric_id, known_ids[numeric_id].name, str)
  91. return nil
  92. end
  93. else
  94. known_ids[numeric_id] = {
  95. name = str,
  96. settings = settings
  97. }
  98. end
  99. if not post_init_added then
  100. rspamd_config:add_post_init(register_settings_cb)
  101. post_init_added = true
  102. end
  103. return numeric_id
  104. end
  105. exports.register_settings_id = register_settings_id
  106. local function settings_by_id(id)
  107. return known_ids[id]
  108. end
  109. exports.settings_by_id = settings_by_id
  110. return exports