forked from ahaenggli/AzureAD-LDAP-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
550 lines (433 loc) · 20.1 KB
/
server.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
'use strict';
const helper = require('./helper');
const config = require('./config');
if (!helper.checkEnvVars()) return;
const ldapwrapper = require('./ldapwrapper');
const ldap = require('ldapjs');
const ldap_overwrites = require('./ldapjs_overwrites.js');
ldap_overwrites(ldap);
const graph = require('./graph_azuread');
const nthash = require('smbhash').nthash;
/* build schema */
var schemaDB = {
"objectClass": ["top", "subentry", "subschema", "extensibleObject"],
"cn": "subschema",
"structuralObjectClass": "subentry",
"entryDN": "cn=subschema",
"createTimestamp": "20220301211408Z",
"modifyTimestamp": "20220301211408Z",
"ldapSyntaxes": "",
"matchingRules": "",
"matchingRuleUse": "",
"attributeTypes": "",
"objectClasses": "",
"subschemaSubentry": "cn=subschema"
};
// source: https://www.iana.org/assignments/ldap-parameters/ldap-parameters.xhtml#ldap-parameters-8
// schemaDB["ldapSyntaxes"] = helper.ReadCSVfile('./schema/ldapSyntaxes.csv', function (row) { return '(' + row[0] + ' DESC ' + row[1] + ')'; });
// source: extraced via ./schema/ldap_seacher.ps1
schemaDB["ldapSyntaxes"] = helper.ReadCSVfile('./schema/ldapSyntaxes.csv', function (row) { if (Array.isArray(row)) return row.join(","); else return row; });
schemaDB["matchingRules"] = helper.ReadCSVfile('./schema/matchingRules.csv', function (row) { if (Array.isArray(row)) return row.join(","); else return row; });
schemaDB["matchingRuleUse"] = helper.ReadCSVfile('./schema/matchingRuleUse.csv', function (row) { if (Array.isArray(row)) return row.join(","); else return row; });
schemaDB["attributeTypes"] = helper.ReadCSVfile('./schema/attributeTypes.csv', function (row) { if (Array.isArray(row)) return row.join(","); else return row; });
schemaDB["objectClasses"] = helper.ReadCSVfile('./schema/objectClasses.csv', function (row) { if (Array.isArray(row)) return row.join(","); else return row; });
var db = helper.ReadJSONfile(config.LDAP_DATAFILE);
db["cn=subschema"] = schemaDB;
var lastRefresh = 0;
var tlsOptions = {};
//if(config.LDAPS_CERTIFICATE && config.LDAPS_KEY)
tlsOptions = { certificate: helper.ReadFile(config.LDAPS_CERTIFICATE), key: helper.ReadFile(config.LDAPS_KEY) };
var server = ldap.createServer(tlsOptions);
const interval = config.LDAP_SYNC_TIME /*minutes*/ * 60 * 1000;
async function refreshDB() {
helper.log("server.js", "refreshDB()", "func called");
if (Date.now() > lastRefresh + interval) {
db = await ldapwrapper.do();
db["cn=subschema"] = schemaDB;
lastRefresh = Date.now();
}
if (!db) {
db = helper.ReadJSONfile(config.LDAP_DATAFILE);
db["cn=subschema"] = schemaDB;
}
}
// init data from azure before starting the server
refreshDB();
const interval_func = function () {
helper.forceLog("server.js", "every", config.LDAP_SYNC_TIME, "minutes refreshDB()");
try {
refreshDB();
} catch (error) {
helper.error("server.js", "interval_func", error);
}
};
setInterval(interval_func, interval);
///--- Shared handlers
const SUFFIX = '';
function authorize(req, res, next) {
var bindi = req.connection.ldap.bindDN.toString().replace(/ /g, '');
var username = bindi.toLowerCase().replace(config.LDAP_USERRDN + "=", '').replace("," + config.LDAP_USERSDN, '');
const isSearch = (req instanceof ldap.SearchRequest);
const isAnonymous = req.connection.ldap.bindDN.equals('cn=anonymous');
if (config.LDAP_ANONYMOUSBIND == "none" && isAnonymous) {
helper.error("server.js", "authorize - denied because of env var `LDAP_ANONYMOUSBIND` ", bindi);
return next(new ldap.InsufficientAccessRightsError());
}
/* Any user may search after bind, only cn=root has full power */
var isAdmin = false;
if (config.LDAP_BINDUSER) {
for (var u of config.LDAP_BINDUSER.toString().split("||")) {
u = u.split("|")[0];
if (u === username) isAdmin = true;
}
}
if (!isAdmin && !isSearch) {
helper.error("server.js", "authorize - denied for => ", username, bindi);
return next(new ldap.InsufficientAccessRightsError());
}
return next();
}
function isUserENVBindUser(binduser) {
var allowSensitiveAttributes = false;
if (config.LDAP_BINDUSER) {
for (var u of config.LDAP_BINDUSER.toString().split("||")) {
u = u.split("|")[0];
var username = binduser.toString().toLowerCase().replace(/ /g, '').replace(config.LDAP_USERRDN + "=", '').replace("," + config.LDAP_USERSDN, '');
if (u === username) allowSensitiveAttributes = true;
}
}
return allowSensitiveAttributes;
}
function removeSensitiveAttributes(binduser, dn, attributes) {
if (!attributes) return attributes;
const isEnvBindUser = isUserENVBindUser(binduser);
var allowSensitiveAttributes = (binduser.equals(dn) || isEnvBindUser);
// samba is special, the own user must have access to them
if (attributes.hasOwnProperty("sambaNTPassword")) {
if (config.LDAP_SAMBANTPWD_MAXCACHETIME && attributes["sambaPwdLastSet"] && config.LDAP_SAMBANTPWD_MAXCACHETIME != -1)
// time is up
if ((attributes["sambaPwdLastSet"] + config.LDAP_SAMBANTPWD_MAXCACHETIME * 60) < Math.floor(Date.now() / 1000)) {
attributes["sambaNTPassword"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
attributes["sambaPwdLastSet"] = 0;
}
// user is not allowed to see
if (!allowSensitiveAttributes) {
attributes["sambaNTPassword"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
attributes["sambaPwdLastSet"] = 0;
}
}
// secure attributes - only the respective user and superusers are allowed to see them
if (!allowSensitiveAttributes) {
if (config.LDAP_SENSITIVE_ATTRIBUTES != "") {
const sensitiveAttributes = config.LDAP_SENSITIVE_ATTRIBUTES.split("|");
// remove all sensitive attributes from the env var
let attKeys = Object.keys(attributes);
sensitiveAttributes.forEach(sensAttName => {
let foundAttNames = attKeys.filter((key) => key.match(new RegExp(sensAttName, "gi")));
foundAttNames.forEach(found => {
delete attributes[found];
});
});
}
}
// secure attributes - only superusers are allowed to see them
if (!isEnvBindUser && config.LDAP_SECURE_ATTRIBUTES != "") {
const secureAttributes = config.LDAP_SECURE_ATTRIBUTES.split("|");
// remove all sensitive attributes from the env var
let attKeys = Object.keys(attributes);
secureAttributes.forEach(sensAttName => {
let foundAttNames = attKeys.filter((key) => key.match(new RegExp(sensAttName, "gi")));
foundAttNames.forEach(found => {
delete attributes[found];
});
});
}
return attributes;
}
// Auth via azure for binding
server.bind(SUFFIX, async (req, res, next) => {
try {
var dn = req.dn.toString().replace(/ /g, '');
helper.log("server.js", "server.bind", dn);
// dn bind
var username = helper.unescapeLDAPspecialChars(dn.replace(config.LDAP_USERRDN + "=", '').replace("," + config.LDAP_USERSDN, ''));
var pass = req.credentials;
if (config.LDAP_BINDUSER && config.LDAP_BINDUSER.toString().split("||").indexOf(username + '|' + pass) > -1) {
helper.log("server.js", "server.bind", username, "binduser, you shall pass");
res.end();
return next();
} else {
if (config.LDAP_REMOVEDOMAIN == true && username.indexOf("@") == -1)
username = username + "@" + config.LDAP_DOMAIN;
if (!db.hasOwnProperty(dn)) {
// helper.warn("server.js", "server.bind", "hmpf", dn);
let searchDN = Object.values(db).filter(g => (g.hasOwnProperty("AzureADuserPrincipalName"))).filter(g => g.AzureADuserPrincipalName == username);
if (searchDN.length == 1) dn = searchDN[0]["entryDN"];
}
var userAttributes = db[dn]; // removeSensitiveAttributes(req.dn, dn, db[dn]);//
if (!userAttributes || !userAttributes.hasOwnProperty("sambaNTPassword") || !userAttributes.hasOwnProperty("AzureADuserPrincipalName")) {
helper.log("server.js", "server.bind", username, "Failed login -> mybe not synced yet?");
return next(new ldap.InvalidCredentialsError());
} else {
var check = await graph.loginWithUsernamePassword(userAttributes["AzureADuserPrincipalName"], pass);
helper.log("server.js", "server.bind", "check", check);
var userNtHash = nthash(pass);
if (check === 1) {
helper.log("server.js", "server.bind", username, "check=true: you shall pass");
//helper.log("server.js", userAttributes);
if (userAttributes && userAttributes.hasOwnProperty("sambaNTPassword")) {
if (userAttributes["sambaNTPassword"] != userNtHash) {
helper.log("server.js", "server.bind", username, "Saving NT password hash for user ", dn);
userAttributes["sambaNTPassword"] = userNtHash;
}
helper.log("server.js", "server.bind", username, "Saving PwdLastSet for user ", dn);
userAttributes["sambaPwdLastSet"] = Math.floor(Date.now() / 1000);
// save the data file, except LDAP_SAMBANTPWD_MAXCACHETIME is set to 0
if(config.LDAP_SAMBANTPWD_MAXCACHETIME != 0)
{
db[dn] = userAttributes;
helper.SaveJSONtoFile(db, config.LDAP_DATAFILE);
}
}
res.end();
return next();
}
else if (check === 2 && config.LDAP_ALLOWCACHEDLOGINONFAILURE) {
helper.error("server.js", "server.bind", username, "wrong password, retry against sambaNTPassword");
if (userAttributes && userAttributes.hasOwnProperty("sambaNTPassword")) {
if (userAttributes["sambaNTPassword"] === userNtHash) {
res.end();
return next();
}
}
return next(new ldap.InvalidCredentialsError());
}
else {
helper.error("server.js", "server.bind", username, " -> Failed login");
return next(new ldap.InvalidCredentialsError());
}
}
}
}
catch (error) {
helper.error("server.js", "server.bind", error);
return next(new ldap.InvalidCredentialsError());
}
});
// search
server.search(SUFFIX, authorize, (req, res, next) => {
try {
const isAnonymous = req.connection.ldap.bindDN.equals('cn=anonymous');
var dn = req.dn.toString().toLowerCase().replace(/ /g, '') || config.LDAP_BASEDN;
helper.log("server.js", "server.search", 'Search for => DB: ' + dn + '; Scope: ' + req.scope + '; Filter: ' + req.filter + '; Attributes: ' + req.attributes + ';');
// search for schema/configuration
if (['cn=SubSchema', 'cn=schema,cn=config', 'cn=schema,cn=configuration'].map(v => v.toLowerCase()).indexOf(dn.toLowerCase()) > -1) {
res.send({
dn: dn,
attributes: schemaDB
});
res.end();
return next();
}
let searchableEntries = JSON.parse(JSON.stringify(db));
if (config.LDAP_ANONYMOUSBIND == 'domain' && isAnonymous) {
for (var key of Object.keys(searchableEntries)) {
if (!searchableEntries[key].hasOwnProperty("namingContexts")) {
delete searchableEntries[key];
}
}
helper.log("server.js", "server.search", 'searchableEntries modified for anonymous', Object.keys(searchableEntries).length);
} else {
helper.log("server.js", "server.search", 'searchableEntries NOT modified', Object.keys(searchableEntries).length);
}
if (!searchableEntries[dn])
return next(new ldap.NoSuchObjectError(dn));
let scopeCheck;
let bindDN = req.connection.ldap.bindDN;
switch (req.scope) {
case 'base':
if (req.filter.matches(removeSensitiveAttributes(bindDN, dn, searchableEntries[dn]))) {
res.send({
dn: dn,
attributes: removeSensitiveAttributes(bindDN, dn, db[dn])
});
}
res.end();
return next();
case 'one':
scopeCheck = (k) => {
if (req.dn.equals(k))
return true;
const parent = ldap.parseDN(k).parent();
return (parent ? parent.equals(req.dn) : false);
};
break;
case 'sub':
scopeCheck = (k) => {
return (req.dn.equals(k) || req.dn.parentOf(k));
};
break;
}
const keys = Object.keys(searchableEntries);
for (const key of keys) {
if (!scopeCheck(key))
continue;
if (req.filter.matches(removeSensitiveAttributes(bindDN, key, searchableEntries[key]))) {
res.send({
dn: key,
attributes: removeSensitiveAttributes(bindDN, key, db[key])
});
}
}
res.end();
return next();
}
catch (error) {
helper.error("server.js", "server.search", error);
}
});
/* ldapjs modify entries: START */
// compare entries
server.compare(SUFFIX, authorize, (req, res, next) => {
const dn = req.dn.toString().toLowerCase().replace(/ {2,}/g, ' ').replace(/, /g, ',');
if (!db[dn])
return next(new ldap.NoSuchObjectError(dn));
// case in-sensitive
req.attribute = Object.keys(db[dn]).find(key => key.toLowerCase() === req.attribute.toLowerCase()) || req.attribute;
if (!db[dn][req.attribute])
return next(new ldap.NoSuchAttributeError(req.attribute));
var matches = false;
const vals = db[dn][req.attribute];
for (const value of vals) {
if (value === req.value) {
matches = true;
break;
}
}
res.end(matches);
return next();
});
// add entries
server.add(SUFFIX, authorize, (req, res, next) => {
const dn = req.dn.toString().toLowerCase().replace(/ {2,}/g, ' ').replace(/, /g, ',');
if (db[dn]) {
helper.error("server.js", "add", "EntryAlreadyExistsError", dn);
return next(new ldap.EntryAlreadyExistsError(dn));
}
db[dn] = Object.assign({}, req.toObject().attributes);
for (var key in db[dn]) {
if (['objectclass', 'memberuid', 'member', 'memberof'].indexOf(key.toLowerCase()) === -1 && db[dn][key].length == 1) {
db[dn][key] = db[dn][key][0];
}
}
helper.SaveJSONtoFile(db, config.LDAP_DATAFILE);
res.end();
return next();
});
// delete entries
server.del(SUFFIX, authorize, (req, res, next) => {
const dn = req.dn.toString().toLowerCase().replace(/ {2,}/g, ' ').replace(/, /g, ',');
if (!db[dn]) {
helper.error("server.js", "del", "NoSuchObjectError", dn);
return next(new ldap.NoSuchObjectError(dn));
}
delete db[dn];
helper.SaveJSONtoFile(db, config.LDAP_DATAFILE);
res.end();
return next();
});
server.modifyDN(SUFFIX, authorize, (req, res, next) => {
helper.error("server.js", "modifyDN", "not yet implemented");
return next(new ldap.ProtocolError('not yet implemented'));
// console.log('DN: ' + req.dn.toString());
// console.log('new RDN: ' + req.newRdn.toString());
// console.log('deleteOldRDN: ' + req.deleteOldRdn);
// console.log('new superior: ' +(req.newSuperior ? req.newSuperior.toString() : ''));
// res.end();
});
// edit entries
server.modify(SUFFIX, authorize, (req, res, next) => {
const dn = req.dn.toString().toLowerCase().replace(/ {2,}/g, ' ').replace(/, /g, ',');
if (!req.changes.length) {
helper.error("server.js", "modify", "ProtocolError", req.changes);
return next(new ldap.ProtocolError('changes required'));
}
if (!db[dn]) {
helper.error("server.js", "modify", "NoSuchObjectError", dn);
return next(new ldap.NoSuchObjectError(dn));
}
const entry = db[dn];
helper.log("server.js", "modify", "dn", dn);
helper.log("server.js", "modify", "req.changes", req.changes);
for (const change of req.changes) {
var mod = change.modification;
// search change/add/delete attribute(s) in lowercase, so the first CamelCase variante is kept
var modType = Object.keys(entry).find(key => key.toLowerCase() === mod.type.toLowerCase()) || mod.type;
var modVals = mod.vals;
helper.log("server.js", "modify", "req.changes -> change-details", { operation: change.operation, modType: modType, modVals: modVals });
//helper.error("server.js", "modify", "modVals2", modVals);
switch (change.operation) {
case 'replace':
if (!entry[modType]) {
helper.error("server.js", "modify", "NoSuchAttributeError", modType);
return next(new ldap.NoSuchAttributeError(modType));
}
if (!modVals || !modVals.length) {
delete entry[modType];
} else {
entry[modType] = modVals;
//modifiy array to single entry
if (['objectclass', 'memberuid', 'member', 'memberof'].indexOf(modType.toLowerCase()) === -1 && entry[modType].length == 1) {
entry[modType] = entry[modType][0];
}
}
break;
case 'add':
if (!entry[modType]) {
entry[modType] = modVals;
} else {
if (!Array.isArray(entry[modType])) entry[modType] = [entry[modType]];
for (const v of modVals) {
if (entry[modType].indexOf(v) === -1)
entry[modType].push(v);
}
}
//modifiy array to single entry
if (['objectclass', 'memberuid', 'member', 'memberof'].indexOf(modType.toLowerCase()) === -1 && entry[modType].length == 1) {
entry[modType] = entry[modType][0];
}
break;
case 'delete':
if (!entry[modType]) {
helper.error("server.js", "modify", "NoSuchAttributeError", modType);
return next(new ldap.NoSuchAttributeError(modType));
} else {
if (!Array.isArray(entry[modType])) entry[modType] = [entry[modType]];
for (const v of modVals) {
let idx = entry[modType].indexOf(v);
if (idx > -1)
entry[modType].splice(idx, 1);
}
if (entry[modType].length == 0 || !modVals || modVals.length == 0)
delete entry[modType];
}
break;
}
}
db[dn] = entry;
helper.SaveJSONtoFile(db, config.LDAP_DATAFILE);
res.end();
return next();
});
/* ldapjs modify entries: ENDE */
server.on("error", (error) => {
helper.error("server.js", "!!! error !!!", error);
});
server.on("uncaughtException", (error) => {
helper.error("server.js", "!!! uncaughtException !!!", error);
});
server.listen(config.LDAP_PORT, function () {
console.log("server.js", '----> LDAP server up at: ', server.url);
var packagejson = require('./package.json');
console.log("server.js", '----> ', packagejson.name, 'version:', packagejson.version);
});