This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rcpt_to.js
78 lines (74 loc) · 2.57 KB
/
rcpt_to.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
'use strict';
var util = require('util');
var constants = require('haraka-constants');
exports._verify_existence = function (address, callback, connection) {
var plugin = this;
var pool = connection.server.notes.ldappool;
var onError = function(err) {
connection.logerror('Could not verify address ' + util.inspect(address) + ': ' + util.inspect(err));
callback(err, false);
};
if (!pool) {
return onError('LDAP Pool not found!');
}
var search = function (err, client) {
if (err) {
return onError(err);
}
else {
var config = plugin._get_search_conf(address, connection);
connection.logdebug('Verifying existence: ' + util.inspect(config));
try {
client.search(config.basedn, config, function(search_error, res) {
if (search_error) { onError(search_error); }
var entries = 0;
res.on('searchEntry', function(entry) {
entries++;
});
res.on('error', onError);
res.on('end', function() {
callback(null, entries > 0);
});
});
}
catch (e) {
return onError(e);
}
}
};
pool.get(search);
};
exports._get_search_conf = function(address, connection) {
var pool = connection.server.notes.ldappool;
var filter = pool.config.rcpt_to.searchfilter || '(&(objectclass=*)(mail=%a))';
filter = filter.replace(/%a/g, address);
var config = {
basedn: pool.config.rcpt_to.basedn || pool.config.basedn,
filter: filter,
scope: pool.config.rcpt_to.scope || pool.config.scope,
attributes: [ 'dn' ]
};
return config;
};
exports.check_rcpt = function(next, connection, params) {
var plugin = this;
if (!params || !params[0] || !params[0].address) {
connection.logerror('Ignoring invalid call. Given connection.transaction: ' +
util.inspect(connection.transaction));
return next();
}
var rcpt = params[0].address();
var callback = function(err, result) {
if (err) {
connection.logerror('Could not use LDAP for address check: ' + util.inspect(err));
next(constants.denysoft);
}
else if (!result) {
next(constants.deny);
}
else {
next(constants.ok);
}
};
plugin._verify_existence(rcpt, callback, connection);
};