forked from smfreegard/Haraka
/
rcpt_to.access.js
99 lines (79 loc) · 2.63 KB
/
rcpt_to.access.js
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
// rcpt_to.access plugin
exports.register = function() {
var i;
var config = this.config.get('rcpt_to.access.ini');
this.wl = this.config.get('rcpt_to.access.whitelist', 'list');
this.bl = this.config.get('rcpt_to.access.blacklist', 'list');
this.deny_msg = config.general && (config.general['deny_msg'] ||
'Connection rejected.');
var white_regex =
this.config.get('rcpt_to.access.whitelist_regex', 'list');
var black_regex =
this.config.get('rcpt_to.access.blacklist_regex', 'list');
if (white_regex.length) {
this.wlregex = new RegExp('^(?:' + white_regex.join('|') + ')$', 'i');
}
if (black_regex.length) {
this.blregex = new RegExp('^(?:' + black_regex.join('|') + ')$', 'i');
}
this.register_hook('rcpt', 'rcpt_to_access');
}
exports.rcpt_to_access = function(next, connection, params) {
var plugin = this;
var rcpt_to = params[0].address();
// address whitelist checks
if (rcpt_to) {
connection.logdebug(plugin, 'checking ' + rcpt_to +
' against rcpt_to.access.whitelist');
if (_in_whitelist(connection, plugin, rcpt_to)) {
connection.logdebug(plugin, "Allowing " + rcpt_to);
return next();
}
}
// address blacklist checks
if (rcpt_to) {
connection.logdebug(plugin, 'checking ' + rcpt_to +
' against rcpt_to.access.blacklist');
if (_in_blacklist(connection, plugin, rcpt_to)) {
connection.logdebug(plugin, "Rejecting, matched: " + rcpt_to);
return next(DENY, rcpt_to + ' ' + plugin.deny_msg);
}
}
return next();
}
function _in_whitelist(connection, plugin, address) {
var i;
for (i in plugin.wl) {
connection.logdebug(plugin, 'checking ' + address + ' against ' +
plugin.wl[i]);
if (plugin.wl[i] === address) {
return 1;
}
}
if (plugin.wlregex) {
connection.logdebug(plugin, 'checking ' + address + ' against ' +
plugin.wlregex.source);
if (address.match(plugin.wlregex)) {
return 1;
}
}
return 0;
}
function _in_blacklist(connection, plugin, address) {
var i;
for (i in plugin.bl) {
connection.logdebug(plugin, 'checking ' + address + ' against ' +
plugin.bl[i]);
if (plugin.bl[i] === address) {
return 1;
}
}
if (plugin.blregex) {
connection.logdebug(plugin, 'checking ' + address + ' against ' +
plugin.blregex.source);
if (address.match(plugin.blregex)) {
return 1;
}
}
return 0;
}