2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var net = require('net');
|
|
|
|
var tls = require('tls');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var asn1 = require('asn1');
|
2014-09-07 19:42:31 +00:00
|
|
|
var VError = require('verror').VError;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
var dn = require('./dn');
|
2011-08-30 00:24:50 +00:00
|
|
|
var dtrace = require('./dtrace');
|
2011-08-04 20:32:01 +00:00
|
|
|
var errors = require('./errors');
|
|
|
|
var Protocol = require('./protocol');
|
|
|
|
|
|
|
|
var Parser = require('./messages').Parser;
|
2011-11-07 22:13:53 +00:00
|
|
|
var AbandonResponse = require('./messages/abandon_response');
|
2011-08-04 20:32:01 +00:00
|
|
|
var AddResponse = require('./messages/add_response');
|
|
|
|
var BindResponse = require('./messages/bind_response');
|
|
|
|
var CompareResponse = require('./messages/compare_response');
|
|
|
|
var DeleteResponse = require('./messages/del_response');
|
|
|
|
var ExtendedResponse = require('./messages/ext_response');
|
2011-09-15 21:49:00 +00:00
|
|
|
var LDAPResult = require('./messages/result');
|
2011-08-04 20:32:01 +00:00
|
|
|
var ModifyResponse = require('./messages/modify_response');
|
|
|
|
var ModifyDNResponse = require('./messages/moddn_response');
|
2011-08-12 23:37:47 +00:00
|
|
|
var SearchRequest = require('./messages/search_request');
|
2011-08-04 20:32:01 +00:00
|
|
|
var SearchResponse = require('./messages/search_response');
|
|
|
|
var UnbindResponse = require('./messages/unbind_response');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var Ber = asn1.Ber;
|
|
|
|
var BerReader = asn1.BerReader;
|
2011-08-10 17:57:58 +00:00
|
|
|
var DN = dn.DN;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
var sprintf = util.format;
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
///--- Helpers
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
function mergeFunctionArgs(argv, start, end) {
|
|
|
|
assert.ok(argv);
|
|
|
|
|
|
|
|
if (!start)
|
|
|
|
start = 0;
|
|
|
|
if (!end)
|
|
|
|
end = argv.length;
|
|
|
|
|
|
|
|
var handlers = [];
|
|
|
|
|
|
|
|
for (var i = start; i < end; i++) {
|
|
|
|
if (argv[i] instanceof Array) {
|
|
|
|
var arr = argv[i];
|
|
|
|
for (var j = 0; j < arr.length; j++) {
|
|
|
|
if (!(arr[j] instanceof Function)) {
|
2012-02-18 08:15:52 +00:00
|
|
|
throw new TypeError('Invalid argument type: ' + typeof (arr[j]));
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
|
|
|
handlers.push(arr[j]);
|
|
|
|
}
|
|
|
|
} else if (argv[i] instanceof Function) {
|
|
|
|
handlers.push(argv[i]);
|
|
|
|
} else {
|
2012-02-18 08:15:52 +00:00
|
|
|
throw new TypeError('Invalid argument type: ' + typeof (argv[i]));
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
return handlers;
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getResponse(req) {
|
|
|
|
assert.ok(req);
|
|
|
|
|
|
|
|
var Response;
|
|
|
|
|
|
|
|
switch (req.protocolOp) {
|
|
|
|
case Protocol.LDAP_REQ_BIND:
|
|
|
|
Response = BindResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_ABANDON:
|
2011-11-07 22:13:53 +00:00
|
|
|
Response = AbandonResponse;
|
|
|
|
break;
|
2011-08-04 20:32:01 +00:00
|
|
|
case Protocol.LDAP_REQ_ADD:
|
|
|
|
Response = AddResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_COMPARE:
|
|
|
|
Response = CompareResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_DELETE:
|
|
|
|
Response = DeleteResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_EXTENSION:
|
|
|
|
Response = ExtendedResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_MODIFY:
|
|
|
|
Response = ModifyResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_MODRDN:
|
|
|
|
Response = ModifyDNResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_SEARCH:
|
|
|
|
Response = SearchResponse;
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_UNBIND:
|
|
|
|
Response = UnbindResponse;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
assert.ok(Response);
|
|
|
|
|
|
|
|
var res = new Response({
|
|
|
|
messageID: req.messageID,
|
2012-02-18 08:54:22 +00:00
|
|
|
log: req.log,
|
2011-08-12 23:37:47 +00:00
|
|
|
attributes: ((req instanceof SearchRequest) ? req.attributes : undefined)
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
res.connection = req.connection;
|
|
|
|
res.logId = req.logId;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
function decodeDN(req, strict) {
|
|
|
|
assert.ok(req);
|
|
|
|
var parse;
|
|
|
|
if (strict) {
|
|
|
|
parse = dn.parse;
|
|
|
|
} else {
|
|
|
|
parse = function (input) {
|
|
|
|
try {
|
|
|
|
return dn.parse(input);
|
|
|
|
} catch (e) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
switch (req.protocolOp) {
|
|
|
|
case Protocol.LDAP_REQ_BIND:
|
|
|
|
req.name = parse(req.name);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_ADD:
|
|
|
|
case Protocol.LDAP_REQ_COMPARE:
|
|
|
|
case Protocol.LDAP_REQ_DELETE:
|
|
|
|
req.entry = parse(req.entry);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_MODIFY:
|
|
|
|
req.object = parse(req.object);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_MODRDN:
|
|
|
|
req.entry = parse(req.entry);
|
|
|
|
// TODO: handle newRdn/Superior
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_SEARCH:
|
|
|
|
req.baseObject = parse(req.baseObject);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
function defaultHandler(req, res, next) {
|
|
|
|
assert.ok(req);
|
|
|
|
assert.ok(res);
|
|
|
|
assert.ok(next);
|
|
|
|
|
|
|
|
res.matchedDN = req.dn.toString();
|
|
|
|
res.errorMessage = 'Server method not implemented';
|
|
|
|
res.end(errors.LDAP_OTHER);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
function defaultNoOpHandler(req, res, next) {
|
2011-08-24 19:38:23 +00:00
|
|
|
assert.ok(req);
|
|
|
|
assert.ok(res);
|
|
|
|
assert.ok(next);
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
function noSuffixHandler(req, res, next) {
|
|
|
|
assert.ok(req);
|
|
|
|
assert.ok(res);
|
|
|
|
assert.ok(next);
|
|
|
|
|
|
|
|
res.errorMessage = 'No tree found for: ' + req.dn.toString();
|
|
|
|
res.end(errors.LDAP_NO_SUCH_OBJECT);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function noExOpHandler(req, res, next) {
|
|
|
|
assert.ok(req);
|
|
|
|
assert.ok(res);
|
|
|
|
assert.ok(next);
|
|
|
|
|
|
|
|
res.errorMessage = req.requestName + ' not supported';
|
|
|
|
res.end(errors.LDAP_PROTOCOL_ERROR);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
function fireDTraceProbe(req, res) {
|
|
|
|
assert.ok(req);
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
req._dtraceId = res._dtraceId = dtrace._nextId();
|
|
|
|
var probeArgs = [
|
|
|
|
req._dtraceId,
|
|
|
|
req.connection.remoteAddress || 'localhost',
|
|
|
|
req.connection.ldap.bindDN.toString(),
|
2011-11-22 21:29:19 +00:00
|
|
|
req.dn.toString()
|
2011-11-19 00:39:37 +00:00
|
|
|
];
|
2011-09-09 20:40:25 +00:00
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
var op;
|
|
|
|
switch (req.protocolOp) {
|
|
|
|
case Protocol.LDAP_REQ_ABANDON:
|
|
|
|
op = 'abandon';
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_ADD:
|
|
|
|
op = 'add';
|
|
|
|
probeArgs.push(req.attributes.length);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_BIND:
|
|
|
|
op = 'bind';
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_COMPARE:
|
|
|
|
op = 'compare';
|
|
|
|
probeArgs.push(req.attribute);
|
|
|
|
probeArgs.push(req.value);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_DELETE:
|
|
|
|
op = 'delete';
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_EXTENSION:
|
|
|
|
op = 'exop';
|
|
|
|
probeArgs.push(req.name);
|
|
|
|
probeArgs.push(req.value);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_MODIFY:
|
|
|
|
op = 'modify';
|
|
|
|
probeArgs.push(req.changes.length);
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_MODRDN:
|
|
|
|
op = 'modifydn';
|
|
|
|
probeArgs.push(req.newRdn.toString());
|
|
|
|
probeArgs.push((req.newSuperior ? req.newSuperior.toString() : ''));
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_SEARCH:
|
|
|
|
op = 'search';
|
|
|
|
probeArgs.push(req.scope);
|
|
|
|
probeArgs.push(req.filter.toString());
|
|
|
|
break;
|
|
|
|
case Protocol.LDAP_REQ_UNBIND:
|
|
|
|
op = 'unbind';
|
|
|
|
break;
|
2012-02-18 08:15:52 +00:00
|
|
|
default:
|
|
|
|
break;
|
2011-11-19 00:39:37 +00:00
|
|
|
}
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
res._dtraceOp = op;
|
2012-02-18 08:15:52 +00:00
|
|
|
dtrace.fire('server-' + op + '-start', function () {
|
2011-11-19 00:39:37 +00:00
|
|
|
return probeArgs;
|
|
|
|
});
|
2011-09-09 20:40:25 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
///--- API
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new server that you can call .listen() on, in the various
|
|
|
|
* forms node supports. You need to first assign some handlers to the various
|
|
|
|
* LDAP operations however.
|
|
|
|
*
|
|
|
|
* The options object currently only takes a certificate/private key, and a
|
2012-02-18 08:54:22 +00:00
|
|
|
* bunyan logger handle.
|
2011-08-06 20:44:26 +00:00
|
|
|
*
|
|
|
|
* This object exposes the following events:
|
|
|
|
* - 'error'
|
|
|
|
* - 'close'
|
|
|
|
*
|
|
|
|
* @param {Object} options (optional) parameterization object.
|
|
|
|
* @throws {TypeError} on bad input.
|
|
|
|
*/
|
|
|
|
function Server(options) {
|
|
|
|
if (options) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (options) !== 'object')
|
2011-08-06 20:44:26 +00:00
|
|
|
throw new TypeError('options (object) required');
|
2012-02-18 08:54:22 +00:00
|
|
|
if (typeof (options.log) !== 'object')
|
|
|
|
throw new TypeError('options.log must be an object');
|
2011-08-06 20:44:26 +00:00
|
|
|
|
|
|
|
if (options.certificate || options.key) {
|
|
|
|
if (!(options.certificate && options.key) ||
|
2014-05-06 05:36:18 +00:00
|
|
|
(typeof (options.certificate) !== 'string' &&
|
2014-05-06 12:33:43 +00:00
|
|
|
!Buffer.isBuffer(options.certificate)) ||
|
2014-05-06 05:36:18 +00:00
|
|
|
(typeof (options.key) !== 'string' &&
|
|
|
|
!Buffer.isBuffer(options.key))) {
|
|
|
|
throw new TypeError('options.certificate and options.key ' +
|
|
|
|
'(string or buffer) are both required for TLS');
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
var self = this;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
EventEmitter.call(this, options);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-01-06 23:10:46 +00:00
|
|
|
this._chain = [];
|
2012-02-18 08:54:22 +00:00
|
|
|
this.log = options.log;
|
2014-09-30 23:39:19 +00:00
|
|
|
this.strictDN = (options.strictDN !== undefined) ? options.strictDN : true;
|
2011-09-30 17:48:56 +00:00
|
|
|
|
2011-08-12 23:37:47 +00:00
|
|
|
var log = this.log;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
function setupConnection(c) {
|
|
|
|
assert.ok(c);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-12 23:37:47 +00:00
|
|
|
if (c.type === 'unix') {
|
|
|
|
c.remoteAddress = self.server.path;
|
|
|
|
c.remotePort = c.fd;
|
2014-09-07 19:42:31 +00:00
|
|
|
} else if (c.socket) {
|
|
|
|
// TLS
|
2011-11-07 20:05:45 +00:00
|
|
|
c.remoteAddress = c.socket.remoteAddress;
|
|
|
|
c.remotePort = c.socket.remotePort;
|
2011-08-12 23:37:47 +00:00
|
|
|
}
|
|
|
|
|
2011-11-07 20:05:45 +00:00
|
|
|
|
2011-08-30 18:12:34 +00:00
|
|
|
var rdn = new dn.RDN({cn: 'anonymous'});
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
c.ldap = {
|
|
|
|
id: c.remoteAddress + ':' + c.remotePort,
|
2011-08-30 04:48:05 +00:00
|
|
|
config: options,
|
|
|
|
_bindDN: new DN([rdn])
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('timeout', function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
log.trace('%s timed out', c.ldap.id);
|
|
|
|
c.destroy();
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('end', function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
log.trace('%s shutdown', c.ldap.id);
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('error', function (err) {
|
2012-09-11 09:49:37 +00:00
|
|
|
log.warn('%s unexpected connection error', c.ldap.id, err);
|
|
|
|
self.emit('clientError', err);
|
2011-08-06 20:44:26 +00:00
|
|
|
c.destroy();
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('close', function (had_err) {
|
2011-08-06 20:44:26 +00:00
|
|
|
log.trace('%s close; had_err=%j', c.ldap.id, had_err);
|
|
|
|
c.end();
|
|
|
|
});
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
c.ldap.__defineGetter__('bindDN', function () {
|
2011-08-30 04:48:05 +00:00
|
|
|
return c.ldap._bindDN;
|
2011-08-10 17:57:58 +00:00
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
c.ldap.__defineSetter__('bindDN', function (val) {
|
2011-08-10 17:57:58 +00:00
|
|
|
if (!(val instanceof DN))
|
|
|
|
throw new TypeError('DN required');
|
|
|
|
|
|
|
|
c.ldap._bindDN = val;
|
|
|
|
return val;
|
|
|
|
});
|
2011-08-06 20:44:26 +00:00
|
|
|
return c;
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
function newConnection(c) {
|
|
|
|
setupConnection(c);
|
2012-02-18 08:54:22 +00:00
|
|
|
log.trace('new connection from %s', c.ldap.id);
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
dtrace.fire('server-connection', function () {
|
2011-11-15 18:49:23 +00:00
|
|
|
return [c.remoteAddress];
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
c.parser = new Parser({
|
2012-02-18 08:54:22 +00:00
|
|
|
log: options.log
|
2011-08-06 20:44:26 +00:00
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
c.parser.on('message', function (req) {
|
2011-08-06 20:44:26 +00:00
|
|
|
req.connection = c;
|
2011-08-12 23:37:47 +00:00
|
|
|
req.logId = c.ldap.id + '::' + req.messageID;
|
2011-09-13 21:49:21 +00:00
|
|
|
req.startTime = new Date().getTime();
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:54:22 +00:00
|
|
|
if (log.debug())
|
2011-08-06 20:44:26 +00:00
|
|
|
log.debug('%s: message received: req=%j', c.ldap.id, req.json);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
var res = getResponse(req);
|
|
|
|
if (!res) {
|
|
|
|
log.warn('Unimplemented server method: %s', req.type);
|
|
|
|
c.destroy();
|
2012-01-24 17:43:46 +00:00
|
|
|
return false;
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
// parse string DNs for routing/etc
|
|
|
|
decodeDN(req, this.strictDN);
|
|
|
|
|
2011-08-30 04:48:05 +00:00
|
|
|
res.connection = c;
|
2011-08-30 18:12:34 +00:00
|
|
|
res.logId = req.logId;
|
2011-08-30 04:48:05 +00:00
|
|
|
res.requestDN = req.dn;
|
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
var chain = self._getHandlerChain(req, res);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
var i = 0;
|
2012-02-18 08:15:52 +00:00
|
|
|
return function (err) {
|
2011-08-10 17:57:58 +00:00
|
|
|
function sendError(err) {
|
2011-08-06 20:44:26 +00:00
|
|
|
res.status = err.code || errors.LDAP_OPERATIONS_ERROR;
|
2011-09-13 21:49:21 +00:00
|
|
|
res.matchedDN = req.suffix ? req.suffix.toString() : '';
|
2011-08-06 20:44:26 +00:00
|
|
|
res.errorMessage = err.message || '';
|
|
|
|
return res.end();
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-09-13 21:49:21 +00:00
|
|
|
function after() {
|
|
|
|
if (!self._postChain || !self._postChain.length)
|
|
|
|
return;
|
|
|
|
|
|
|
|
function next() {} // stub out next for the post chain
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
self._postChain.forEach(function (c) {
|
2011-09-13 21:49:21 +00:00
|
|
|
c.call(self, req, res, next);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2012-09-11 09:49:37 +00:00
|
|
|
log.trace('%s sending error: %s', req.logId, err.stack || err);
|
|
|
|
self.emit('clientError', err);
|
2011-09-13 21:49:21 +00:00
|
|
|
sendError(err);
|
|
|
|
return after();
|
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
var next = arguments.callee;
|
|
|
|
if (chain.handlers[i])
|
|
|
|
return chain.handlers[i++].call(chain.backend, req, res, next);
|
|
|
|
|
|
|
|
if (req.protocolOp === Protocol.LDAP_REQ_BIND && res.status === 0)
|
|
|
|
c.ldap.bindDN = req.dn;
|
|
|
|
|
2011-09-13 21:49:21 +00:00
|
|
|
return after();
|
2011-08-10 17:57:58 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (!e.stack)
|
|
|
|
e.stack = e.toString();
|
|
|
|
log.error('%s uncaught exception: %s', req.logId, e.stack);
|
|
|
|
return sendError(new errors.OperationsError(e.message));
|
|
|
|
}
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
}();
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
c.parser.on('error', function (err, message) {
|
2014-09-07 19:42:31 +00:00
|
|
|
self.emit('error', new VError(err, 'Parser error for %s', c.ldap.id));
|
2011-11-11 18:08:48 +00:00
|
|
|
|
|
|
|
if (!message)
|
|
|
|
return c.destroy();
|
|
|
|
|
2011-09-15 21:49:00 +00:00
|
|
|
var res = getResponse(message);
|
2011-11-11 18:08:48 +00:00
|
|
|
if (!res)
|
|
|
|
return c.destroy();
|
|
|
|
|
|
|
|
res.status = 0x02; // protocol error
|
|
|
|
res.errorMessage = err.toString();
|
2012-01-24 17:43:46 +00:00
|
|
|
return c.end(res.toBer());
|
2011-08-06 20:44:26 +00:00
|
|
|
});
|
2011-09-15 21:49:00 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
c.on('data', function (data) {
|
2012-02-18 08:54:22 +00:00
|
|
|
if (log.trace())
|
2011-08-06 20:44:26 +00:00
|
|
|
log.trace('data on %s: %s', c.ldap.id, util.inspect(data));
|
2012-01-09 16:24:14 +00:00
|
|
|
|
2014-09-07 19:42:31 +00:00
|
|
|
c.parser.write(data);
|
2011-08-06 20:44:26 +00:00
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
} // end newConnection
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
this.routes = {};
|
2011-10-18 17:15:14 +00:00
|
|
|
if ((options.cert || options.certificate) && options.key) {
|
|
|
|
options.cert = options.cert || options.certificate;
|
2011-08-06 20:44:26 +00:00
|
|
|
this.server = tls.createServer(options, newConnection);
|
|
|
|
} else {
|
|
|
|
this.server = net.createServer(newConnection);
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2012-02-18 08:54:22 +00:00
|
|
|
this.server.log = options.log;
|
2011-08-06 20:44:26 +00:00
|
|
|
this.server.ldap = {
|
|
|
|
config: options
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
this.server.on('close', function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
self.emit('close');
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
this.server.on('error', function (err) {
|
2011-08-06 20:44:26 +00:00
|
|
|
self.emit('error', err);
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('maxConnections', function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
return self.server.maxConnections;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineSetter__('maxConnections', function (val) {
|
2011-08-06 20:44:26 +00:00
|
|
|
self.server.maxConnections = val;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('connections', function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
return self.server.connections;
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('name', function () {
|
2011-08-10 17:57:58 +00:00
|
|
|
return 'LDAPServer';
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('url', function () {
|
2011-08-10 17:57:58 +00:00
|
|
|
var str;
|
2013-10-30 04:46:48 +00:00
|
|
|
if (!this.server.address().family) {
|
|
|
|
str = 'ldapi://';
|
|
|
|
str += this.host.replace(new RegExp('/', 'g'), '%2f');
|
|
|
|
return str;
|
|
|
|
}
|
2011-11-08 01:12:59 +00:00
|
|
|
if (this.server instanceof tls.Server) {
|
2011-08-10 17:57:58 +00:00
|
|
|
str = 'ldaps://';
|
|
|
|
} else {
|
|
|
|
str = 'ldap://';
|
|
|
|
}
|
|
|
|
str += self.host || 'localhost';
|
|
|
|
str += ':';
|
|
|
|
str += self.port || 389;
|
|
|
|
return str;
|
|
|
|
});
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
|
|
|
util.inherits(Server, EventEmitter);
|
|
|
|
module.exports = Server;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP add method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.add = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 00:24:50 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_ADD, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP bind method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.bind = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 00:24:50 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_BIND, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP compare method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.compare = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_COMPARE, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP delete method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.del = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_DELETE, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP exop method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name OID to assign this handler chain to.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input.
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.exop = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_EXTENSION, name, args, true);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP modify method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.modify = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_MODIFY, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP modifyDN method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.modifyDN = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_MODRDN, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP search method.
|
|
|
|
*
|
|
|
|
* Note that this is of the form f(name, [function]) where the second...N
|
|
|
|
* arguments can all either be functions or arrays of functions.
|
|
|
|
*
|
|
|
|
* @param {String} name the DN to mount this handler chain at.
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.search = function (name) {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_SEARCH, name, args);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
/**
|
|
|
|
* Adds a handler (chain) for the LDAP unbind method.
|
|
|
|
*
|
|
|
|
* This method is different than the others and takes no mount point, as unbind
|
|
|
|
* is a connection-wide operation, not constrianed to part of the DIT.
|
|
|
|
*
|
|
|
|
* @return {Server} this so you can chain calls.
|
|
|
|
* @throws {TypeError} on bad input
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.unbind = function () {
|
2011-11-19 00:39:37 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 0);
|
2011-08-30 04:48:05 +00:00
|
|
|
return this._mount(Protocol.LDAP_REQ_UNBIND, 'unbind', args, true);
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2012-01-06 23:10:46 +00:00
|
|
|
Server.prototype.use = function use() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var chain = mergeFunctionArgs(args, 0, args.length);
|
|
|
|
var self = this;
|
2012-02-18 08:15:52 +00:00
|
|
|
chain.forEach(function (c) {
|
2012-01-06 23:10:46 +00:00
|
|
|
self._chain.push(c);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.after = function () {
|
2011-09-13 21:49:21 +00:00
|
|
|
if (!this._postChain)
|
|
|
|
this._postChain = [];
|
|
|
|
|
|
|
|
var self = this;
|
2012-02-18 08:15:52 +00:00
|
|
|
mergeFunctionArgs(arguments).forEach(function (h) {
|
2011-09-13 21:49:21 +00:00
|
|
|
self._postChain.push(h);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-11-07 22:13:53 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
// All these just reexpose the requisite net.Server APIs
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.listen = function (port, host, callback) {
|
2013-10-30 04:29:21 +00:00
|
|
|
if (typeof (port) !== 'number' && typeof (port) !== 'string')
|
|
|
|
throw new TypeError('port (number or path) required');
|
2011-09-09 20:40:25 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (host) === 'function') {
|
2011-08-06 20:44:26 +00:00
|
|
|
callback = host;
|
2011-08-10 17:57:58 +00:00
|
|
|
host = '0.0.0.0';
|
|
|
|
}
|
2014-06-06 17:21:05 +00:00
|
|
|
if (typeof (port) === 'string' && /^[0-9]+$/.test(port)) {
|
|
|
|
// Disambiguate between string ports and file paths
|
|
|
|
port = parseInt(port, 10);
|
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
function _callback() {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (port) === 'number') {
|
2013-05-27 20:49:44 +00:00
|
|
|
self.host = self.address().address;
|
|
|
|
self.port = self.address().port;
|
2011-08-10 17:57:58 +00:00
|
|
|
} else {
|
|
|
|
self.host = port;
|
|
|
|
self.port = self.server.fd;
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (callback) === 'function')
|
2012-01-24 17:43:46 +00:00
|
|
|
callback();
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (port) === 'number') {
|
2011-08-10 17:57:58 +00:00
|
|
|
return this.server.listen(port, host, _callback);
|
|
|
|
} else {
|
|
|
|
return this.server.listen(port, _callback);
|
|
|
|
}
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.listenFD = function (fd) {
|
2012-01-24 17:43:46 +00:00
|
|
|
this.host = 'unix-domain-socket';
|
|
|
|
this.port = fd;
|
2011-08-06 20:44:26 +00:00
|
|
|
return this.server.listenFD(fd);
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.close = function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
return this.server.close();
|
|
|
|
};
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.address = function () {
|
2011-08-06 20:44:26 +00:00
|
|
|
return this.server.address();
|
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype._getRoute = function (_dn, backend) {
|
2011-08-06 20:44:26 +00:00
|
|
|
assert.ok(dn);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
if (!backend)
|
|
|
|
backend = this;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
var name;
|
|
|
|
if (_dn instanceof dn.DN) {
|
|
|
|
name = _dn.toString();
|
|
|
|
} else {
|
|
|
|
name = _dn;
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
if (!this.routes[name]) {
|
|
|
|
this.routes[name] = {};
|
|
|
|
this.routes[name].backend = backend;
|
|
|
|
this.routes[name].dn = _dn;
|
2014-06-05 21:47:09 +00:00
|
|
|
// Force regeneration of the route key cache on next request
|
|
|
|
this._routeKeyCache = null;
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
return this.routes[name];
|
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
2014-06-05 21:47:09 +00:00
|
|
|
Server.prototype._sortedRouteKeys = function _sortedRouteKeys() {
|
|
|
|
// The filtered/sorted route keys are cached to prevent needlessly
|
|
|
|
// regenerating the list for every incoming request.
|
|
|
|
if (!this._routeKeyCache) {
|
|
|
|
var self = this;
|
|
|
|
var reversedRDNsToKeys = {};
|
|
|
|
// Generate mapping of reversedRDNs(DN) -> routeKey
|
|
|
|
Object.keys(this.routes).forEach(function (key) {
|
|
|
|
var _dn = self.routes[key].dn;
|
|
|
|
// Ignore non-DN routes such as exop or unbind
|
|
|
|
if (_dn instanceof dn.DN) {
|
|
|
|
var reversed = _dn.clone();
|
|
|
|
reversed.rdns.reverse();
|
|
|
|
reversedRDNsToKeys[reversed.spaced(true).toString()] = key;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var output = [];
|
|
|
|
// Reverse-sort on reversedRDS(DN) in order to output routeKey list.
|
|
|
|
// This will place more specific DNs in front of their parents:
|
|
|
|
// 1. dc=test, dc=domain, dc=sub
|
|
|
|
// 2. dc=test, dc=domain
|
|
|
|
// 3. dc=other, dc=foobar
|
|
|
|
Object.keys(reversedRDNsToKeys).sort().reverse().forEach(function (_dn) {
|
|
|
|
output.push(reversedRDNsToKeys[_dn]);
|
|
|
|
});
|
|
|
|
this._routeKeyCache = output;
|
|
|
|
}
|
|
|
|
return this._routeKeyCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype._getHandlerChain = function _getHandlerChain(req, res) {
|
2011-08-06 20:44:26 +00:00
|
|
|
assert.ok(req);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-11-19 00:39:37 +00:00
|
|
|
fireDTraceProbe(req, res);
|
|
|
|
|
2011-08-24 19:38:23 +00:00
|
|
|
// check anonymous bind
|
|
|
|
if (req.protocolOp === Protocol.LDAP_REQ_BIND &&
|
|
|
|
req.dn.toString() === '' &&
|
|
|
|
req.credentials === '') {
|
|
|
|
return {
|
|
|
|
backend: self,
|
2011-11-19 00:39:37 +00:00
|
|
|
handlers: [defaultNoOpHandler]
|
2011-08-24 19:38:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
var op = '0x' + req.protocolOp.toString(16);
|
|
|
|
var self = this;
|
|
|
|
var routes = this.routes;
|
2014-06-05 21:47:09 +00:00
|
|
|
var route;
|
2011-11-08 01:12:59 +00:00
|
|
|
|
2014-06-05 21:47:09 +00:00
|
|
|
// Special cases are exops, unbinds and abandons. Handle those first.
|
|
|
|
if (req.protocolOp === Protocol.LDAP_REQ_EXTENSION) {
|
|
|
|
route = routes[req.requestName];
|
|
|
|
if (route) {
|
2011-08-06 20:44:26 +00:00
|
|
|
return {
|
2014-06-05 21:47:09 +00:00
|
|
|
backend: route.backend,
|
|
|
|
handlers: (route[op] ? route[op] : [noExOpHandler])
|
2011-11-08 01:12:59 +00:00
|
|
|
};
|
2014-06-05 21:47:09 +00:00
|
|
|
} else {
|
2011-11-08 01:12:59 +00:00
|
|
|
return {
|
|
|
|
backend: self,
|
2014-06-05 21:47:09 +00:00
|
|
|
handlers: [noExOpHandler]
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2014-06-05 21:47:09 +00:00
|
|
|
}
|
|
|
|
} else if (req.protocolOp === Protocol.LDAP_REQ_UNBIND) {
|
|
|
|
route = routes['unbind'];
|
2014-06-06 19:05:08 +00:00
|
|
|
if (route) {
|
|
|
|
return {
|
|
|
|
backend: route.backend,
|
|
|
|
handlers: route[op]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
backend: self,
|
|
|
|
handlers: [defaultNoOpHandler]
|
|
|
|
};
|
|
|
|
}
|
2014-06-05 21:47:09 +00:00
|
|
|
} else if (req.protocolOp === Protocol.LDAP_REQ_ABANDON) {
|
|
|
|
return {
|
|
|
|
backend: self,
|
|
|
|
handlers: [defaultNoOpHandler]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, match via DN rules
|
|
|
|
assert.ok(req.dn);
|
|
|
|
var keys = this._sortedRouteKeys();
|
|
|
|
var fallbackHandler = [noSuffixHandler];
|
2014-09-30 23:39:19 +00:00
|
|
|
// invalid DNs in non-strict mode are routed to the default handler
|
|
|
|
var testDN = (typeof (req.dn) === 'string') ? '' : req.dn;
|
|
|
|
|
2014-06-05 21:47:09 +00:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
var suffix = keys[i];
|
|
|
|
route = routes[suffix];
|
|
|
|
assert.ok(route.dn);
|
|
|
|
// Match a valid route or the route wildcard ('')
|
2014-09-30 23:39:19 +00:00
|
|
|
if (route.dn.equals(testDN) || route.dn.parentOf(testDN) || suffix === '') {
|
2014-06-05 21:47:09 +00:00
|
|
|
if (route[op]) {
|
2012-02-18 08:15:52 +00:00
|
|
|
// We should be good to go.
|
|
|
|
req.suffix = route.dn;
|
|
|
|
return {
|
|
|
|
backend: route.backend,
|
2014-06-05 21:47:09 +00:00
|
|
|
handlers: route[op]
|
2012-02-18 08:15:52 +00:00
|
|
|
};
|
2014-06-05 21:47:09 +00:00
|
|
|
} else {
|
|
|
|
// We found a valid suffix but not a valid operation.
|
|
|
|
// There might be a more generic suffix with a legitimate operation.
|
|
|
|
fallbackHandler = [defaultHandler];
|
2012-02-18 08:15:52 +00:00
|
|
|
}
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
backend: self,
|
2014-06-05 21:47:09 +00:00
|
|
|
handlers: fallbackHandler
|
2011-08-06 20:44:26 +00:00
|
|
|
};
|
2011-11-11 18:08:48 +00:00
|
|
|
};
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-15 16:44:31 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype._mount = function (op, name, argv, notDN) {
|
2011-08-15 16:44:31 +00:00
|
|
|
assert.ok(op);
|
2011-09-29 16:35:53 +00:00
|
|
|
assert.ok(name !== undefined);
|
2011-08-15 16:44:31 +00:00
|
|
|
assert.ok(argv);
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (name) !== 'string')
|
2011-08-15 16:44:31 +00:00
|
|
|
throw new TypeError('name (string) required');
|
2011-11-19 00:39:37 +00:00
|
|
|
if (!argv.length)
|
2011-08-15 16:44:31 +00:00
|
|
|
throw new Error('at least one handler required');
|
|
|
|
|
|
|
|
var backend = this;
|
|
|
|
var index = 0;
|
2011-09-09 20:40:25 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (argv[0]) === 'object' && !Array.isArray(argv[0])) {
|
2011-08-15 16:44:31 +00:00
|
|
|
backend = argv[0];
|
|
|
|
index = 1;
|
|
|
|
}
|
|
|
|
var route = this._getRoute(notDN ? name : dn.parse(name), backend);
|
2012-01-06 23:10:46 +00:00
|
|
|
|
|
|
|
var chain = this._chain.slice();
|
2012-02-18 08:15:52 +00:00
|
|
|
argv.slice(index).forEach(function (a) {
|
2012-01-06 23:10:46 +00:00
|
|
|
chain.push(a);
|
|
|
|
});
|
|
|
|
route['0x' + op.toString(16)] = mergeFunctionArgs(chain);
|
2011-08-15 16:44:31 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|