forked from haraka/haraka-plugin-auth-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (66 loc) · 2.08 KB
/
index.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
// auth/auth_ldap
const ldap = require('ldapjs');
const async = require('async');
exports.hook_capabilities = function (next, connection) {
// Don't offer AUTH capabilities by default unless session is encrypted
if (connection.tls.enabled) {
const methods = [ 'PLAIN', 'LOGIN' ];
connection.capabilities.push('AUTH ' + methods.join(' '));
connection.notes.allowed_auth_methods = methods;
}
next();
}
exports.register = function () {
this.inherits('auth/auth_base');
this.load_auth_ldap_ini();
}
exports.load_auth_ldap_ini = function () {
const plugin = this;
plugin.cfg = plugin.config.get('auth_ldap.ini', {
booleans: [
'core.rejectUnauthorized'
],
},
function () {
plugin.load_auth_ldap_ini();
});
}
exports.check_plain_passwd = function (connection, user, passwd, cb) {
// Get LDAP config
const config = this.cfg;
let ldap_url = 'ldap://127.0.0.1';
if (config.core.server) {
ldap_url = config.core.server;
}
const rejectUnauthorized = (config.core.rejectUnauthorized != undefined) ?
config.core.rejectUnauthorized : true;
const client = ldap.createClient({
url: ldap_url,
timeout: (config.core.timeout != undefined) ? config.core.timeout : 5000,
tlsOptions: {
rejectUnauthorized: rejectUnauthorized
}
});
client.on('error', function (err) {
connection.loginfo('auth_ldap: client error ' + err.message);
cb(false);
});
config.dns = Object.keys(config.dns).map(function (v) {
return config.dns[v];
})
async.detectSeries(config.dns, function (dn, callback) {
dn = dn.replace(/%u/g, user);
client.bind(dn, passwd, function (err) {
if (err) {
connection.loginfo("auth_ldap: (" + dn + ") " + err.message);
return callback(false);
}
else {
client.unbind();
return callback(true);
}
})
}, function (result) {
cb(result);
});
}