aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/whitelist.lua
blob: 34867bd2f04c66927a621644a4a4f20ee3bfb7af (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
-- Module that add symbols to those hosts or from domains that are contained in whitelist

local symbol_ip = nil
local symbol_from = nil

local r = nil
local h = nil  -- radix tree and hash table

function check_whitelist (task)
	if symbol_ip then
		-- check client's ip
		local ipn = task:get_from_ip_num()
		if ipn then
			local key = r:get_key(ipn)
			if key then
				task:insert_result( symbol_ip, 1)
			end
		end
	end

	if symbol_from then
		-- check client's from domain
		local from = task:get_from()
		if from then
			local _,_,domain = string.find(from, '@(.+)>?$')
			local key = h:get_key(domain)
			if key then
				task:insert_result(symbol_from, 1)
			end
		end
	end

end


-- Configuration
local opts =  rspamd_config:get_all_opt('whitelist')
if opts then
    if opts['symbol_ip'] or opts['symbol_from'] then
        symbol_ip = opts['symbol_ip']
        symbol_from = opts['symbol_from']
		
		if symbol_ip then
			if opts['ip_whitelist'] then
				r = rspamd_config:add_radix_map (opts['ip_whitelist'])
			else
				-- No whitelist defined
				symbol_ip = nil
			end
		end
		if symbol_from then
			if opts['from_whitelist'] then
				h = rspamd_config:add_host_map (opts['from_whitelist'])
			else
				-- No whitelist defined
				symbol_from = nil
			end
		end


		-- Register symbol's callback
		if symbol_ip then
			rspamd_config:register_symbol(symbol_ip, 1.0, 'check_whitelist')
		elseif symbol_from then
			rspamd_config:register_symbol(symbol_from, 1.0, 'check_whitelist')
		end
	end
end