Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function startLdapServer(callback) {
const server = ldap.createServer();
const searchConf = Helper.config.ldap.searchDN;
const userDN = primaryKey + "=" + user + "," + baseDN;
// Two users are authorized: john doe and the root user in case of
// advanced auth (the user that does the search for john's actual
// bindDN)
const authorizedUsers = {};
authorizedUsers[normalizeDN(searchConf.rootDN)] = searchConf.rootPassword;
authorizedUsers[normalizeDN(userDN)] = correctPassword;
function authorize(req, res, next) {
const bindDN = req.connection.ldap.bindDN;
if (bindDN in authorizedUsers) {
return next();
"namingcontexts": [ SUFFIX, "CN=Configuration," + SUFFIX, "CN=Schema,CN=Configuration," + SUFFIX],
"defaultnamingcontext": SUFFIX,
"schemanamingcontext": "CN=Schema,CN=Configuration," + SUFFIX,
"configurationnamingcontext": "CN=Configuration," + SUFFIX,
"rootdomainnamingcontext": SUFFIX,
"supportedcontrol": ["1.3.6.1.4.1.4203.1.11.3"],
"supportedldapversion":["3", "2"],
"supportedldappolicies":[],
"supportedsaslmechanisms":["GSSAPI", "GSS-SPNEGO", "EXTERNAL", "DIGEST-MD5"],
"dnshostname":"example.com",
"ldapservicename":"example.com$@EXAMPLE.COM",
"servername":"CN=EXAMPLE,CN=Servers,CN=NN,CN=Sites,CN=Configuration," + SUFFIX,
"supportedcapabilities":[]
}
};
var server = ldap.createServer();
server.bind('cn=admin, dc=example, dc=com', function(req, res, next) {
if (req.dn.toString() !== 'cn=admin,dc=example,dc=com' || req.credentials !== 'test')
return next(new ldap.InvalidCredentialsError());
res.end();
return next();
});
server.add(SUFFIX, authorize, function(req, res, next) {
try{
var dn = req.dn.toString().replaceSpaces();
if(!dn.endsWith(SUFFIX.replaceSpaces())){
dn+=","+SUFFIX.replaceSpaces();
}
function createServer(usetls) {
if (usetls) {
var tls = require('./tls.json')[0];
return ldap.createServer({
certificate: tls.certs[0].cert,
key: tls['private-key'] });
}
return ldap.createServer();
}
(async () => {
const server = ldap.createServer();
server.bind(db.root.dn.toString(), function(req, res, next) {
if (!req.dn.equals(db.root.dn) || req.credentials !== db.root.credentials) {
return next(new ldap.InvalidCredentialsError());
}
res.end();
return next();
});
server.bind(suffix, authorize, function(req, res, next) {
for (const cn in db) {
const user = db[cn];
if (req.dn.equals(user.dn)) {
if (req.credentials !== user.credentials) {
return next(new ldap.InvalidCredentialsError());
}
function createServer(usetls) {
if (usetls) {
var tls = require('./tls.json')[0];
return ldap.createServer({
certificate: tls.certs[0].cert,
key: tls['private-key'] });
}
return ldap.createServer();
}
exports.start = function (port, password) {
if (server) {
return Promise.resolve();
}
server = ldap.createServer();
server.bind('cn=root, dc=example, dc=org', function(req, res, next) {
if (req.dn.toString() !== 'cn=root, dc=example, dc=org' || req.credentials !== password) {
return next(new ldap.InvalidCredentialsError());
}
res.end();
return next();
});
server.bind(SUFFIX, authorize, function(req, res, next) {
let dn = req.dn.toString();
if (dn !== 'cn=alice, dc=example, dc=org' || req.credentials !== password) {
return next(new ldap.InvalidCredentialsError());
}
res.end();
return next();
const LDAP = require('ldapjs');
const Server = LDAP.createServer();
const Mock = require('./helper');
let init = false;
module.exports = {
init: () => {
if (!init) {
Server.listen(10388, '0.0.0.0');
Server.bind('uid=admin,ou=system', authenticateAdmin);
Server.search('ou=users,ou=system', findUser);
Server.search('ou=groups,ou=system', findGroups);
}
init = true;
}
};
cn: 'anotherLdapUser2',
mail: 'anotherLdapUser2@example.com'
}
}, {
dn: 'cn=inactiveUser1,dc=example,dc=com',
password: 'ldapPwd',
attributes: {
objectclass: ['organization', 'top'],
userAccountControl: 514,
o: 'example',
cn: 'inactiveUser1',
mail: 'inactiveUser1@example.com'
}
}];
let server = ldap.createServer();
function authorize(req, res, next) {
if (!req.connection.ldap.bindDN.equals('cn=ldapUser1,dc=example,dc=com'))
return next(new ldap.InsufficientAccessRightsError());
return next();
}
server.search('dc=example,dc=com', authorize, function(req, res, next) {
let matches = _.filter(users, user => req.filter.matches(user.attributes));
_.each(matches, match => res.send(match));
res.end();
return next();
});
///--- Shared handlers
function authorize(req, res, next) {
if (!req.connection.ldap.bindDN.equals('cn=root')) {
return next(new ldap.InsufficientAccessRightsError());
}
return next();
}
///--- Globals
var SUFFIX = 'dc=samplestack,dc=org';
var db = {};
var server = ldap.createServer();
var stop = function () {
try {
console.log('Stopping LDAP worker...');
server.close();
}
catch (e) {}
};
process.on('exit', stop);
server.bind('cn=root', function (req, res, next) {
if (req.dn.toString() !== 'cn=root' ||
req.credentials !== options.middleTier.ldap.adminPassword
) {
return next(new ldap.InvalidCredentialsError());
}
function newServer(port, dn, provokeSearchError = false) {
const server = ldapjs.createServer();
server.bind('o=example', function(req, res, next) {
if (req.dn.toString() !== dn || req.credentials !== 'secret')
return next(new ldapjs.InvalidCredentialsError());
res.end();
return next();
});
server.search('o=example', function(req, res, next) {
if (provokeSearchError) {
res.end(ldapjs.LDAP_SIZE_LIMIT_EXCEEDED);
return next();
}
const obj = {
dn: req.dn.toString(),
attributes: {