-
Notifications
You must be signed in to change notification settings - Fork 7
/
ctldap.js
544 lines (510 loc) · 18.2 KB
/
ctldap.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
/**
* ctldap - ChurchTools LDAP-Wrapper 3.0
* This tool requires a node.js-Server and a recent version of ChurchTools 3
* @copyright 2017-2023 Michael Lux
* @copyright 2019-2020 Matthias Huber
* @copyright André Schild
* @licence GNU/GPL v3.0
*/
import fs from "fs";
import ldapjs from "ldapjs";
import { CtldapConfig } from "./ctldap-config.js";
import { patchLdapjsFilters } from "./ldapjs-filter-overrides.js";
const { InsufficientAccessRightsError, InvalidCredentialsError, OtherError, parseDN } = ldapjs;
// Make some ldapjs filters case-insensitive
patchLdapjsFilters();
/**
* Simple integer range as array, inspired by https://developer.mozilla.org
* @param start Start integer, inclusive
* @param end Stop integer, exclusive
* @return {number[]} Array with number sequence
*/
const range = (start, end) => Array.from({ length: end - start }, (_, i) => start + i);
const config = new CtldapConfig();
function getIsoDate() {
return new Date().toISOString();
}
export const logTrace = (site, msg) => {
if (config.trace) {
// For lazy evaluation
if (typeof msg === "function") {
msg = msg()
}
console.log(`${getIsoDate()} [TRACE] ${site.name} - ${msg}`);
}
}
export const logDebug = (site, msg) => {
if (config.debug) {
// For lazy evaluation
if (typeof msg === "function") {
msg = msg()
}
console.log(`${getIsoDate()} [DEBUG] ${site.name} - ${msg}`);
}
}
export const logWarn = (site, msg) => {
console.warn(`${getIsoDate()} [WARN] ${site.name} - ${msg}`);
}
export const logError = (site, msg, error) => {
console.error(`${getIsoDate()} [ERROR] ${site.name} - ${msg}`);
if (error !== undefined) {
console.error(error.stack);
}
}
logDebug({ name: 'root logger' }, "Debug mode enabled, expect lots of output!");
let options = {};
if (config.ldapCertFilename && config.ldapKeyFilename) {
const ldapCert = fs.readFileSync(new URL(`./${config.ldapCertFilename}`, import.meta.url), { encoding: "utf8" }),
ldapKey = fs.readFileSync(new URL(`./${config.ldapKeyFilename}`, import.meta.url), { encoding: "utf8" });
options = { certificate: ldapCert, key: ldapKey };
}
const server = ldapjs.createServer(options);
const USERS_KEY = 'users', GROUPS_KEY = 'groups', RAW_DATA_KEY = 'rawData';
/**
* Retrieves data from cache as a Promise or refreshes the data with the provided (async) factory.
* @param {object} site - The site for which to query the cache
* @param {string} key - The cache key
* @param {function} factory - A function returning a Promise that resolves with the new cache entry or rejects
*/
function getCached(site, key, factory) {
const cache = site.CACHE;
const co = cache[key] || { time: -1, entry: null };
const promise = new Promise((resolve, reject) => {
const time = new Date().getTime();
if (time - config.cacheLifetime < co.time) {
logDebug(site, `Returning cached data for key "${key}".`);
resolve(co.entry);
} else {
if (co.promise) {
logDebug(site, `Returning pending Promise for cache key "${key}".`);
} else {
// Call the factory() function to retrieve the Promise for the fresh entry
// Either resolve with the new entry (plus cache update), or pass on the rejection
co.promise = factory().then((result) => {
logDebug(site, `Store cache entry for cache key "${key}".`)
co.entry = result;
co.time = new Date().getTime();
return result;
}).finally(() => {
delete co.promise;
});
}
// Wait until promise resolves
logDebug(site, `Wait on Promise for cache key "${key}".`)
co.promise.then(resolve, reject);
}
});
cache[key] = co;
return promise;
}
/**
* Fetches all data from a paginated API endpoint.
* Automatically "heals" the wrong behavior of unpatched pagination when requesting with limit of -1
* by transparently fetching missing record(s) with another request.
* @param {object} site The site for which this information is requested.
* @param {string} apiPath The API endpoint to query for all paginated data.
* @param {object} [searchParams] Additional search params (query parameters)
*/
async function fetchAllPaginated(site, apiPath, searchParams= {}) {
// Closure for fetching a single page
const fetchPage = (page) => {
return site.api.get(apiPath, {
searchParams: {
...searchParams,
page
}
});
};
// Get pagination meta cache
const pCache = site.CACHE.pagination;
// Assume the same number of pages as last time, default to 1
const assumedPages = pCache[site] || 1;
// Fetch assumed number of pages
const promises = range(1, assumedPages + 1).map(fetchPage);
// Await first result
const firstResult = await Promise.any(promises);
// Check first result for completeness, and fix up results and pagination cache if necessary
const nPages = firstResult['meta']['pagination']['lastPage'];
if (nPages !== assumedPages) {
logDebug(site, () => `Assumed ${assumedPages} page(s) of data for /api/${apiPath}, but had to load ${nPages}.`);
// Update meta cache
pCache[site] = nPages;
// Fetch remaining pages, if any
if (nPages > assumedPages) {
promises.push(...range(assumedPages + 1, nPages + 1).map(fetchPage));
}
} else {
logDebug(site, () => `Assumed ${assumedPages} page(s) of data for /api/${apiPath}, which was correct.`);
}
// Await all results
const results = await Promise.all(promises);
// Collect all data via flatMap() and return it
return results.flatMap(r => r['data']);
}
/**
* Fetches all mappings of persons and groups.
* @param {object} site The site for which this information is requested.
*/
async function fetchMemberships(site) {
const result = await site.api.get('groups/members', {
searchParams: {"with_deleted": false}
});
logDebug(site, "fetchMemberships done");
return result['data'];
}
/**
* Fetches all persons and computes dn values.
* Persons are filtered by accepted invitations, because uninvited users cannot do logins anyway.
* @param {object} site The site for which this information is requested.
*/
async function fetchPersons(site) {
const data = await fetchAllPaginated(site, 'persons', { limit: 500 });
logDebug(site, "fetchPersons done");
const personMap = {};
data.forEach((p) => {
if (p['invitationStatus'] === "accepted") {
personMap[p['id']] = p;
p.dn = site.compatTransform(site.fnUserDn(p['cmsUserId']));
}
});
return personMap;
}
/**
* Fetches all groups and computes dn values and "special classes" for custom LDAP objectClass attributes.
* @param {object} site The site for which this information is requested.
*/
async function fetchGroups(site) {
const data = await fetchAllPaginated(site, 'groups', { limit: 100 });
logDebug(site, "fetchGroups done");
const groupMap = {};
const sgmKeys = Object.keys(site.specialGroupMappings);
data.forEach((g) => {
// Strip some irrelevant information
delete g['settings'];
delete g['roles'];
// Pre-compute the "distinguished name" of this group for LDAP
g.dn = site.compatTransform(site.fnGroupDn(g['name']));
const info = g['information'];
g.specialClasses = sgmKeys.filter((k) => info[k])
groupMap[g['id']] = g;
});
return groupMap;
}
/**
* Fetches all group types from person master data.
* @param {object} site The site for which this information is requested.
*/
async function fetchGroupTypes(site) {
const result = await site.api.get('person/masterdata');
logDebug(site, "fetchGroupTypes done");
const groupTypes = {};
// noinspection JSUnresolvedFunction
result['data']['groupTypes'].forEach((gt) => groupTypes[gt['id']] = gt['name']);
return groupTypes;
}
/**
* Collects all required group and user information and computes group-to-users and user-to-groups mappings.
* @param {object} site The site for which this information is requested.
*/
async function fetchAll(site) {
return await getCached(site, RAW_DATA_KEY, async () => {
const [personMap, groupMap, memberships, groupTypes] = await Promise.all([
fetchPersons(site), fetchGroups(site), fetchMemberships(site), fetchGroupTypes(site)
]);
// Create membership mappings
const g2p = {}, p2g = {};
memberships.forEach((m) => {
const { personId, groupId } = m;
// Only map persons/groups that have not been filtered
if ((personId in personMap) && (groupId in groupMap)) {
// Entry for group-to-persons-mappings
if (!g2p[groupId]) {
g2p[groupId] = [personId];
} else {
g2p[groupId].push(personId);
}
// Entry for person-to-groups-mappings
if (!p2g[personId]) {
p2g[personId] = [groupId];
} else {
p2g[personId].push(groupId);
}
}
});
return { groupTypes, g2p, p2g, personMap, groupMap };
});
}
/**
* Retrieves the users for the processed request as a Promise.
* @param {object} req - Request object
* @param {object} _res - Response object
* @param {function} next - Next handler function of filter chain
*/
function requestUsers(req, _res, next) {
const site = req.site;
req.usersPromise = getCached(site, USERS_KEY, async () => {
const { p2g, personMap, groupMap } = await fetchAll(site);
let newCache = Object.entries(personMap).map(([id, p]) => {
const cn = p['cmsUserId'];
const email = site.compatTransformEmail(p['email']);
return {
dn: p.dn,
attributes: {
cn,
displayName: `${p['firstName']} ${p['lastName']}`,
id,
uid: cn,
nsUniqueId: `u${id}`,
givenName: p['firstName'],
street: p['street'],
telephoneMobile: p['mobile'],
telephoneHome: p['phonePrivate'],
postalCode: p['zip'],
l: p['city'],
sn: p['lastName'],
email,
mail: email,
objectClass: [
'person',
'CTPerson',
// Map special CT field names of associated groups to the LDAP objectClass names defined in configuration.
...(p2g[id] || [])
.flatMap((gid) => groupMap[gid].specialClasses)
.map((key) => site.specialGroupMappings[key]['personClass'])
],
memberOf: (p2g[id] || []).map((gid) => groupMap[gid].dn)
}
};
});
newCache = site.uniqueEmails(newCache);
// Virtual admin user
if (site.ldapPassword !== undefined) {
const cn = site.ldapUser;
newCache.push({
dn: site.compatTransform(site.fnUserDn(cn)),
attributes: {
cn,
displayname: "LDAP Administrator",
id: 0,
uid: cn,
nsUniqueId: "u0",
givenName: "LDAP Administrator",
objectClass: ['person'],
}
});
}
logDebug(site, () => `Updated users: ${newCache.length}`);
return newCache;
});
return next();
}
/**
* Retrieves the groups for the processed request as a Promise.
* @param {object} req - Request object
* @param {object} _res - Response object
* @param {function} next - Next handler function of filter chain
*/
function requestGroups(req, _res, next) {
const site = req.site;
req.groupsPromise = getCached(site, GROUPS_KEY, async () => {
const { groupTypes, g2p, personMap, groupMap } = await fetchAll(site);
const newCache = Object.entries(groupMap).map(([id, g]) => {
const cn = g['name'];
const info = g['information'];
const groupType = groupTypes[info['groupTypeId']];
const objectClasses = ["group", "CTGroup" + groupType.charAt(0).toUpperCase() + groupType.slice(1),
// Map observed special CT field names to the LDAP objectClass names defined in configuration.
...g.specialClasses.map((key) => site.specialGroupMappings[key]['groupClass'])];
return {
dn: g.dn,
attributes: {
cn,
displayname: g['name'],
id,
nsUniqueId: `g${id}`,
objectClass: objectClasses,
uniqueMember: (g2p[id] || []).map((pid) => personMap[pid].dn)
}
};
});
logDebug(site, () => `Updated groups: ${newCache.length}`);
return newCache;
});
return next();
}
/**
* Validates root user authentication by comparing the bind DN with the configured admin DN.
* @param {object} req - Request object
* @param {object} _res - Response object
* @param {function} next - Next handler function of filter chain
*/
function authorize(req, _res, next) {
if (!req.connection.ldap.bindDN.equals(req.site.adminDn)) {
logWarn(req.site, "Rejected search without proper binding!");
return next(new InsufficientAccessRightsError());
}
return next();
}
/**
* Performs debug logging if debug mode is enabled.
* @param {object} req - Request object
* @param {object} _res - Response object
* @param {function} next - Next handler function of filter chain
*/
function searchLogging(req, _res, next) {
logDebug(req.site, () => `SEARCH base object: ${req.dn.toString()} scope: ${req.scopeName}`);
logDebug(req.site, () => `Filter: ${req.filter.toString()}`);
return next();
}
/**
* Evaluates req.usersPromise and sends matching elements to the client.
* @param {object} req - Request object
* @param {object} res - Response object
* @param {function} next - Next handler function of filter chain
*/
function sendUsers(req, res, next) {
req.usersPromise.then((users) => {
users.forEach((u) => {
if ((req.checkAll || req.dn.equals(u.dn)) && req.filter.matches(u.attributes, false)) {
logTrace(req.site, () => `MatchUser: ${u.dn.toString()}`);
res.send(u);
}
});
return next();
}, (error) => {
logError(req.site, "Error whilst retrieving users: ", error);
return next();
});
}
/**
* Evaluates req.groupsPromise and sends matching elements to the client.
* @param {object} req - Request object
* @param {object} res - Response object
* @param {function} next - Next handler function of filter chain
*/
function sendGroups(req, res, next) {
req.groupsPromise.then((groups) => {
groups.forEach((g) => {
if ((req.checkAll || req.dn.equals(g.dn)) && req.filter.matches(g.attributes, false)) {
logTrace(req.site, () => `MatchGroup: ${g.dn}`);
res.send(g);
}
});
return next();
}, (error) => {
logError(req.site, "Error whilst retrieving groups: ", error);
return next();
});
}
/**
* Calls the res.end() function to finalize successful chain processing.
* @param {object} _req - Request object
* @param {object} res - Response object
* @param {function} next - Next handler function of filter chain
*/
function endSuccess(_req, res, next) {
res.end();
return next();
}
/**
* Checks the given credentials against the credentials in the config file or against the ChurchTools API.
* @param {object} req - Request object
* @param {object} _res - Response object
* @param {function} next - Next handler function of filter chain
*/
async function authenticate(req, _res, next) {
const site = req.site;
if (req.dn === site.adminDn) {
logDebug(site, () => `Admin bind with DN "${req.dn}"`);
// If ldapPassword is undefined, try a default ChurchTools authentication with this user
if (site.ldapPassword) {
try {
await site.authenticateAdmin(req.credentials);
logDebug(site, "Admin bind successful");
return next();
} catch (error) {
logError(site, "Invalid password for admin bind or auth error: ", error);
return next(new InvalidCredentialsError());
}
} else {
logDebug("ldapPassword is undefined, trying ChurchTools authentication...")
}
} else {
logDebug(site, () => `Bind user with DN "${req.dn}"`);
}
const username = parseDN(req.dn).rdnAt(0).getValue("cn");
try {
await site.api.post('login', {
json: {
"username": username,
"password": req.credentials
}
});
logDebug(site, `Authentication successful for "${username}"`);
return next();
} catch (error) {
if (error.response?.statusCode === 400) {
logWarn(site, `Authentication error (CT API HTTP 400) occurred for "${username}" (probably wrong password): ${error}`);
return next(new InvalidCredentialsError());
} else {
logError(site, `Authentication error for "${username}": ${error}`);
return next(new OtherError());
}
}
}
config.sites.forEach((site) => {
// Login bind for user
server.bind(`ou=users,o=${site.name}`, (req, _res, next) => {
req.site = site;
next();
}, authenticate, endSuccess);
// Search implementation for user search
server.search(`ou=users,o=${site.name}`, (req, _res, next) => {
req.site = site;
next();
}, searchLogging, authorize, (req, _res, next) => {
logDebug(site, "Search for users");
req.checkAll = req.scopeName !== "base" && req.dn.length === 2;
return next();
}, requestUsers, sendUsers, endSuccess);
// Search implementation for group search
server.search(`ou=groups,o=${site.name}`, (req, _res, next) => {
req.site = site;
next();
}, searchLogging, authorize, (req, _res, next) => {
logDebug(site, "Search for groups");
req.checkAll = req.scopeName !== "base" && req.dn.length === 2;
return next();
}, requestGroups, sendGroups, endSuccess);
// Search implementation for user and group search
server.search(`o=${site.name}`, (req, _res, next) => {
req.site = site;
next();
}, searchLogging, authorize, (req, _res, next) => {
logDebug(site, "Search for users and groups combined");
req.checkAll = req.scopeName === "subtree";
return next();
}, requestUsers, requestGroups, sendUsers, sendGroups, endSuccess);
});
// Search implementation for basic search for Directory Information Tree and the LDAP Root DSE
server.search('', (req, res) => {
// noinspection JSUnresolvedVariable
logDebug({ name: req.dn.o }, "Empty request, return directory information");
// noinspection JSUnresolvedVariable
const obj = {
"attributes": {
"objectClass": ["top", "OpenLDAProotDSE"],
"subschemaSubentry": ["cn=subschema"],
"namingContexts": `o=${req.dn.o}`,
},
"dn": "",
};
if (req.filter.matches(obj.attributes, false)) {
res.send(obj);
}
res.end();
}, endSuccess);
// Start LDAP server
server.listen(parseInt(config.ldapPort), config.ldapIp, () => {
logDebug({ name: 'root logger' }, `ChurchTools-LDAP-Wrapper listening @ ${server.url}`);
});