aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/ip_score.lua
blob: 4c822c8c7b01a14043566831fcf83968fcd2fcbe (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
-- IP score is a module that set ip score of specific ip and 

-- Default settings
local keystorage_host = nil
local keystorage_port = 0
local metric = 'default'
local reject_score = 3
local add_header_score = 1
local no_action_score = -2
local symbol = 'IP_SCORE'
-- This score is used for normalization of scores from keystorage
local normalize_score = 100 
local whitelist = nil
local expire = 240

-- Set score based on metric's action
local ip_score_set = function(task)
	-- Callback generator
	local make_key_cb = function(ip)
		local cb = function(task, err, data)
			if err then
				if string.find(err, 'not found') then 
					-- Try to set this key
					local cb_set = function(task, err, data)
						if err then
							if err ~= 'OK' then
								rspamd_logger.info('got error: ' .. err)
							end
						end
					end
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb_set, 'SET %b %b %b', ip, expire, '0')
				else
					rspamd_logger.info('got error while incrementing: ' .. err)
				end
			end
		end
		return cb
	end
	local action = task:get_metric_action(metric)
	if action then
		-- Check whitelist 
		if whitelist then
			local ipnum = task:get_from_ip_num()
			if ipnum and whitelist:get_key(ipnum) then
				-- Address is whitelisted
				return
			end
		end
		-- Now check action
		if action == 'reject' then
			local ip = task:get_from_ip()
			if ip then
				local cb = make_key_cb(ip)
				if reject_score > 0 then
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'INCRBY %b %b', ip, reject_score)
				else
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'DECRBY %b %b', ip, -reject_score)
				end
			end
		elseif action == 'add header' then
			local ip = task:get_from_ip()
			if ip then
				local cb = make_key_cb(ip)
				if add_header_score > 0 then
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'INCRBY %b %b', ip, add_header_score)
				else
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'DECRBY %b %b', ip, -add_header_score)
				end
			end
		elseif action == 'no action' then
			local ip = task:get_from_ip()
			if ip then
				local cb = make_key_cb(ip)
				if no_action_score > 0 then
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'INCRBY %b %b', ip, no_action_score)
				else
					rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'DECRBY %b %b', ip, -no_action_score)
				end
			end
		end
	end
end

-- Check score for ip in keystorage
local ip_score_check = function(task)
	local cb = function(task, err, data)
		if err then
			-- Key is not found or error occured
			return
		elseif data then
			local score = tonumber(data)
			-- Normalize
			if score > 0 and score > normalize_score then
				score = 1
			elseif score < 0 and score < -normalize_score then
				score = -1
			else
				score = score / normalize_score
			end
			task:insert_result(symbol, score)
		end
	end
	local ip = task:get_from_ip()
	if ip then
		if whitelist then
			local ipnum = task:get_from_ip_num()
			if whitelist:get_key(ipnum) then
				-- Address is whitelisted
				return
			end
		end
		rspamd_redis.make_request(task, keystorage_host, keystorage_port, cb, 'GET %b', ip)
	end
end


-- Configuration options
local configure_ip_score_module = function()
	local opts =  rspamd_config:get_all_opt('ip_score')
	if opts then
		if opts['keystorage_host'] then
			keystorage_host = opts['keystorage_host']
		end
		if opts['keystorage_port'] then
			keystorage_port = opts['keystorage_port']
		end
		if opts['metric'] then
			metric = opts['metric']
		end
		if opts['reject_score'] then
			reject_score = opts['reject_score']
		end
		if opts['add_header_score'] then
			add_header_score = opts['add_header_score']
		end
		if opts['no_action_score'] then
			no_action_score = opts['no_action_score']
		end
		if opts['symbol'] then
			symbol = opts['symbol']
		end
		if opts['normalize_score'] then
			normalize_score = opts['normalize_score']
		end
		if opts['whitelist'] then
			whitelist = rspamd_config:add_radix_map(opts['whitelist'])
		end
		if opts['expire'] then
			expire = opts['expire']
		end
	end
end

-- Registration
if rspamd_config:get_api_version() >= 9 then
	rspamd_config:register_module_option('ip_score', 'keystorage_host', 'string')
	rspamd_config:register_module_option('ip_score', 'keystorage_port', 'uint')
	rspamd_config:register_module_option('ip_score', 'metric', 'string')
	rspamd_config:register_module_option('ip_score', 'reject_score', 'int')
	rspamd_config:register_module_option('ip_score', 'add_header_score', 'int')
	rspamd_config:register_module_option('ip_score', 'no_action_score', 'int')
	rspamd_config:register_module_option('ip_score', 'symbol', 'string')
	rspamd_config:register_module_option('ip_score', 'normalize_score', 'uint')
	rspamd_config:register_module_option('ip_score', 'whitelist', 'map')
	rspamd_config:register_module_option('ip_score', 'expire', 'uint')

	configure_ip_score_module()
	if keystorage_host and keystorage_port and normalize_score > 0 then
		-- Register ip_score module
		rspamd_config:register_symbol(symbol, 1.0, ip_score_check)
		rspamd_config:register_post_filter(ip_score_set)
	end
else
	rspamd_logger.err('cannot register module ip_score as it requres at least 9 version of lua API and rspamd >= 0.4.6')
end