2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert')
|
|
|
|
const EventEmitter = require('events').EventEmitter
|
|
|
|
const net = require('net')
|
|
|
|
const tls = require('tls')
|
|
|
|
const util = require('util')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2022-03-27 14:13:16 +00:00
|
|
|
// var asn1 = require('@ldapjs/asn1')
|
2020-10-31 21:07:32 +00:00
|
|
|
const VError = require('verror').VError
|
|
|
|
|
|
|
|
const dn = require('./dn')
|
|
|
|
const dtrace = require('./dtrace')
|
|
|
|
const errors = require('./errors')
|
2022-06-06 02:01:40 +00:00
|
|
|
const Protocol = require('@ldapjs/protocol')
|
2020-10-31 21:07:32 +00:00
|
|
|
|
|
|
|
const Parser = require('./messages').Parser
|
|
|
|
const AbandonResponse = require('./messages/abandon_response')
|
|
|
|
const AddResponse = require('./messages/add_response')
|
|
|
|
const BindResponse = require('./messages/bind_response')
|
|
|
|
const CompareResponse = require('./messages/compare_response')
|
|
|
|
const DeleteResponse = require('./messages/del_response')
|
|
|
|
const ExtendedResponse = require('./messages/ext_response')
|
2019-08-27 21:11:49 +00:00
|
|
|
// var LDAPResult = require('./messages/result')
|
2020-10-31 21:07:32 +00:00
|
|
|
const ModifyResponse = require('./messages/modify_response')
|
|
|
|
const ModifyDNResponse = require('./messages/moddn_response')
|
|
|
|
const SearchRequest = require('./messages/search_request')
|
|
|
|
const SearchResponse = require('./messages/search_response')
|
|
|
|
const UnbindResponse = require('./messages/unbind_response')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var Ber = asn1.Ber
|
|
|
|
// var BerReader = asn1.BerReader
|
2020-10-31 21:07:32 +00:00
|
|
|
const DN = dn.DN
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var sprintf = util.format
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Helpers
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function mergeFunctionArgs (argv, start, end) {
|
|
|
|
assert.ok(argv)
|
2012-02-18 08:15:52 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!start) { start = 0 }
|
|
|
|
if (!end) { end = argv.length }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const handlers = []
|
2011-08-06 20:44:26 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
for (let i = start; i < end; i++) {
|
2011-08-06 20:44:26 +00:00
|
|
|
if (argv[i] instanceof Array) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const arr = argv[i]
|
|
|
|
for (let j = 0; j < arr.length; j++) {
|
2011-08-06 20:44:26 +00:00
|
|
|
if (!(arr[j] instanceof Function)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
throw new TypeError('Invalid argument type: ' + typeof (arr[j]))
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
handlers.push(arr[j])
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
|
|
|
} else if (argv[i] instanceof Function) {
|
2019-08-27 21:11:49 +00:00
|
|
|
handlers.push(argv[i])
|
2011-08-06 20:44:26 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +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
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return handlers
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function getResponse (req) {
|
|
|
|
assert.ok(req)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let Response
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
switch (req.protocolOp) {
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_BIND:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = BindResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_ABANDON:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = AbandonResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_ADD:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = AddResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_COMPARE:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = CompareResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_DELETE:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = DeleteResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_EXTENSION:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = ExtendedResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_MODIFY:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = ModifyResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_MODRDN:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = ModifyDNResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_SEARCH:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = SearchResponse
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_UNBIND:
|
2019-08-27 21:11:49 +00:00
|
|
|
Response = UnbindResponse
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return null
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(Response)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const res = new Response({
|
2011-08-04 20:32:01 +00:00
|
|
|
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)
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
res.connection = req.connection
|
|
|
|
res.logId = req.logId
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return res
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function defaultHandler (req, res, next) {
|
|
|
|
assert.ok(req)
|
|
|
|
assert.ok(res)
|
|
|
|
assert.ok(next)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.matchedDN = req.dn.toString()
|
|
|
|
res.errorMessage = 'Server method not implemented'
|
|
|
|
res.end(errors.LDAP_OTHER)
|
|
|
|
return next()
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function defaultNoOpHandler (req, res, next) {
|
|
|
|
assert.ok(req)
|
|
|
|
assert.ok(res)
|
|
|
|
assert.ok(next)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.end()
|
|
|
|
return next()
|
2011-08-24 19:38:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function noSuffixHandler (req, res, next) {
|
|
|
|
assert.ok(req)
|
|
|
|
assert.ok(res)
|
|
|
|
assert.ok(next)
|
2011-08-24 19:38:23 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.errorMessage = 'No tree found for: ' + req.dn.toString()
|
|
|
|
res.end(errors.LDAP_NO_SUCH_OBJECT)
|
|
|
|
return next()
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function noExOpHandler (req, res, next) {
|
|
|
|
assert.ok(req)
|
|
|
|
assert.ok(res)
|
|
|
|
assert.ok(next)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.errorMessage = req.requestName + ' not supported'
|
|
|
|
res.end(errors.LDAP_PROTOCOL_ERROR)
|
|
|
|
return next()
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function fireDTraceProbe (req, res) {
|
|
|
|
assert.ok(req)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
req._dtraceId = res._dtraceId = dtrace._nextId()
|
2020-10-31 21:07:32 +00:00
|
|
|
const probeArgs = [
|
2011-11-19 00:39:37 +00:00
|
|
|
req._dtraceId,
|
|
|
|
req.connection.remoteAddress || 'localhost',
|
|
|
|
req.connection.ldap.bindDN.toString(),
|
2011-11-22 21:29:19 +00:00
|
|
|
req.dn.toString()
|
2019-08-27 21:11:49 +00:00
|
|
|
]
|
2011-09-09 20:40:25 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let op
|
2011-11-19 00:39:37 +00:00
|
|
|
switch (req.protocolOp) {
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_ABANDON:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'abandon'
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_ADD:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'add'
|
|
|
|
probeArgs.push(req.attributes.length)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_BIND:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'bind'
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_COMPARE:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'compare'
|
|
|
|
probeArgs.push(req.attribute)
|
|
|
|
probeArgs.push(req.value)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_DELETE:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'delete'
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_EXTENSION:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'exop'
|
|
|
|
probeArgs.push(req.name)
|
|
|
|
probeArgs.push(req.value)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_MODIFY:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'modify'
|
|
|
|
probeArgs.push(req.changes.length)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_MODRDN:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'modifydn'
|
|
|
|
probeArgs.push(req.newRdn.toString())
|
|
|
|
probeArgs.push((req.newSuperior ? req.newSuperior.toString() : ''))
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_SEARCH:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'search'
|
|
|
|
probeArgs.push(req.scope)
|
|
|
|
probeArgs.push(req.filter.toString())
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_UNBIND:
|
2019-08-27 21:11:49 +00:00
|
|
|
op = 'unbind'
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
2011-11-19 00:39:37 +00:00
|
|
|
}
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res._dtraceOp = op
|
2012-02-18 08:15:52 +00:00
|
|
|
dtrace.fire('server-' + op + '-start', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
return probeArgs
|
|
|
|
})
|
2011-09-09 20:40:25 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +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.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
function Server (options) {
|
2011-08-06 20:44:26 +00:00
|
|
|
if (options) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (options) !== 'object') { throw new TypeError('options (object) required') }
|
|
|
|
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 ' +
|
2019-08-27 21:11:49 +00:00
|
|
|
'(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 {
|
2019-08-27 21:11:49 +00:00
|
|
|
options = {}
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
EventEmitter.call(this, options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this._chain = []
|
|
|
|
this.log = options.log
|
|
|
|
this.strictDN = (options.strictDN !== undefined) ? options.strictDN : true
|
2011-09-30 17:48:56 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const log = this.log
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +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') {
|
2019-08-27 21:11:49 +00:00
|
|
|
c.remoteAddress = self.server.path
|
|
|
|
c.remotePort = c.fd
|
2014-09-07 19:42:31 +00:00
|
|
|
} else if (c.socket) {
|
|
|
|
// TLS
|
2019-08-27 21:11:49 +00:00
|
|
|
c.remoteAddress = c.socket.remoteAddress
|
|
|
|
c.remotePort = c.socket.remotePort
|
2011-08-12 23:37:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const rdn = new dn.RDN({ cn: 'anonymous' })
|
2011-08-30 18:12:34 +00:00
|
|
|
|
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])
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('timeout', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
log.trace('%s timed out', c.ldap.id)
|
|
|
|
c.destroy()
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('end', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
log.trace('%s shutdown', c.ldap.id)
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
c.addListener('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
log.warn('%s unexpected connection error', c.ldap.id, err)
|
|
|
|
self.emit('clientError', err)
|
|
|
|
c.destroy()
|
|
|
|
})
|
|
|
|
c.addListener('close', function (closeError) {
|
|
|
|
log.trace('%s close; had_err=%j', c.ldap.id, closeError)
|
|
|
|
c.end()
|
|
|
|
})
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
c.ldap.__defineGetter__('bindDN', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
return c.ldap._bindDN
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
c.ldap.__defineSetter__('bindDN', function (val) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!(val instanceof DN)) { throw new TypeError('DN required') }
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
c.ldap._bindDN = val
|
|
|
|
return val
|
|
|
|
})
|
|
|
|
return c
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2021-05-19 18:36:51 +00:00
|
|
|
self.newConnection = function (conn) {
|
2021-05-21 12:04:41 +00:00
|
|
|
// TODO: make `newConnection` available on the `Server` prototype
|
|
|
|
// https://github.com/ldapjs/node-ldapjs/pull/727/files#r636572294
|
2020-12-05 23:58:13 +00:00
|
|
|
setupConnection(conn)
|
|
|
|
log.trace('new connection from %s', conn.ldap.id)
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
dtrace.fire('server-connection', function () {
|
2020-12-05 23:58:13 +00:00
|
|
|
return [conn.remoteAddress]
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
conn.parser = new Parser({
|
2012-02-18 08:54:22 +00:00
|
|
|
log: options.log
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2020-12-05 23:58:13 +00:00
|
|
|
conn.parser.on('message', function (req) {
|
|
|
|
req.connection = conn
|
|
|
|
req.logId = conn.ldap.id + '::' + req.messageID
|
2019-08-27 21:11:49 +00:00
|
|
|
req.startTime = new Date().getTime()
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
log.debug('%s: message received: req=%j', conn.ldap.id, req.json)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const res = getResponse(req)
|
2011-08-06 20:44:26 +00:00
|
|
|
if (!res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
log.warn('Unimplemented server method: %s', req.type)
|
2020-12-05 23:58:13 +00:00
|
|
|
conn.destroy()
|
2019-08-27 21:11:49 +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
|
2015-10-31 14:01:50 +00:00
|
|
|
try {
|
|
|
|
switch (req.protocolOp) {
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_BIND:
|
2019-08-27 21:11:49 +00:00
|
|
|
req.name = dn.parse(req.name)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_ADD:
|
|
|
|
case Protocol.operations.LDAP_REQ_COMPARE:
|
|
|
|
case Protocol.operations.LDAP_REQ_DELETE:
|
2019-08-27 21:11:49 +00:00
|
|
|
req.entry = dn.parse(req.entry)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_MODIFY:
|
2019-08-27 21:11:49 +00:00
|
|
|
req.object = dn.parse(req.object)
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_MODRDN:
|
2019-08-27 21:11:49 +00:00
|
|
|
req.entry = dn.parse(req.entry)
|
|
|
|
// TODO: handle newRdn/Superior
|
|
|
|
break
|
2022-06-06 02:01:40 +00:00
|
|
|
case Protocol.operations.LDAP_REQ_SEARCH:
|
2019-08-27 21:11:49 +00:00
|
|
|
req.baseObject = dn.parse(req.baseObject)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
2015-10-31 14:01:50 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (self.strictDN) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return res.end(errors.LDAP_INVALID_DN_SYNTAX)
|
2015-10-31 14:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 23:39:19 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
res.connection = conn
|
2019-08-27 21:11:49 +00:00
|
|
|
res.logId = req.logId
|
|
|
|
res.requestDN = req.dn
|
2011-08-30 04:48:05 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const chain = self._getHandlerChain(req, res)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let i = 0
|
2019-08-27 21:11:49 +00:00
|
|
|
return (function messageIIFE (err) {
|
2020-12-05 23:58:13 +00:00
|
|
|
function sendError (sendErr) {
|
|
|
|
res.status = sendErr.code || errors.LDAP_OPERATIONS_ERROR
|
2019-08-27 21:11:49 +00:00
|
|
|
res.matchedDN = req.suffix ? req.suffix.toString() : ''
|
2020-12-05 23:58:13 +00:00
|
|
|
res.errorMessage = sendErr.message || ''
|
2019-08-27 21:11:49 +00:00
|
|
|
return res.end()
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function after () {
|
|
|
|
if (!self._postChain || !self._postChain.length) { return }
|
2011-09-13 21:49:21 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function next () {} // stub out next for the post chain
|
2011-09-13 21:49:21 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
self._postChain.forEach(function (cb) {
|
|
|
|
cb.call(self, req, res, next)
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-09-13 21:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
log.trace('%s sending error: %s', req.logId, err.stack || err)
|
|
|
|
self.emit('clientError', err)
|
|
|
|
sendError(err)
|
|
|
|
return after()
|
2011-09-13 21:49:21 +00:00
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
|
|
|
|
try {
|
2020-10-31 21:07:32 +00:00
|
|
|
const next = messageIIFE
|
2019-08-27 21:11:49 +00:00
|
|
|
if (chain.handlers[i]) { return chain.handlers[i++].call(chain.backend, req, res, next) }
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2022-06-06 02:01:40 +00:00
|
|
|
if (req.protocolOp === Protocol.operations.LDAP_REQ_BIND && res.status === 0) {
|
2021-07-12 15:02:12 +00:00
|
|
|
// 0 length == anonymous bind
|
2021-07-12 15:32:06 +00:00
|
|
|
if (req.dn.length === 0 && req.credentials === '') {
|
2021-07-12 15:02:12 +00:00
|
|
|
conn.ldap.bindDN = new DN([new dn.RDN({ cn: 'anonymous' })])
|
|
|
|
} else {
|
|
|
|
conn.ldap.bindDN = req.dn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unbind clear bindDN for safety
|
|
|
|
// conn should terminate on unbind (RFC4511 4.3)
|
2022-06-06 02:01:40 +00:00
|
|
|
if (req.protocolOp === Protocol.operations.LDAP_REQ_UNBIND && res.status === 0) {
|
2021-07-12 15:02:12 +00:00
|
|
|
conn.ldap.bindDN = new DN([new dn.RDN({ cn: 'anonymous' })])
|
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return after()
|
2011-08-10 17:57:58 +00:00
|
|
|
} catch (e) {
|
2019-08-27 21:11:49 +00:00
|
|
|
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-10 17:57:58 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}())
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
conn.parser.on('error', function (err, message) {
|
|
|
|
self.emit('error', new VError(err, 'Parser error for %s', conn.ldap.id))
|
2011-11-11 18:08:48 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
if (!message) { return conn.destroy() }
|
2011-11-11 18:08:48 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const res = getResponse(message)
|
2020-12-05 23:58:13 +00:00
|
|
|
if (!res) { return conn.destroy() }
|
2011-11-11 18:08:48 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
res.status = 0x02 // protocol error
|
|
|
|
res.errorMessage = err.toString()
|
2020-12-05 23:58:13 +00:00
|
|
|
return conn.end(res.toBer())
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-09-15 21:49:00 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
conn.on('data', function (data) {
|
|
|
|
log.trace('data on %s: %s', conn.ldap.id, util.inspect(data))
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-12-05 23:58:13 +00:00
|
|
|
conn.parser.write(data)
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
} // end newConnection
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.routes = {}
|
2011-10-18 17:15:14 +00:00
|
|
|
if ((options.cert || options.certificate) && options.key) {
|
2019-08-27 21:11:49 +00:00
|
|
|
options.cert = options.cert || options.certificate
|
2021-05-20 14:35:13 +00:00
|
|
|
this.server = tls.createServer(options, options.connectionRouter ? options.connectionRouter : self.newConnection)
|
2011-08-06 20:44:26 +00:00
|
|
|
} else {
|
2021-05-20 14:35:13 +00:00
|
|
|
this.server = net.createServer(options.connectionRouter ? options.connectionRouter : self.newConnection)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
this.server.log = options.log
|
2011-08-06 20:44:26 +00:00
|
|
|
this.server.ldap = {
|
|
|
|
config: options
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
this.server.on('close', function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.emit('close')
|
|
|
|
})
|
2012-02-18 08:15:52 +00:00
|
|
|
this.server.on('error', function (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.emit('error', err)
|
|
|
|
})
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(Server, EventEmitter)
|
2015-10-31 13:34:51 +00:00
|
|
|
Object.defineProperties(Server.prototype, {
|
|
|
|
maxConnections: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getMaxConnections () {
|
|
|
|
return this.server.maxConnections
|
2015-10-31 13:34:51 +00:00
|
|
|
},
|
2019-08-27 21:11:49 +00:00
|
|
|
set: function setMaxConnections (val) {
|
|
|
|
this.server.maxConnections = val
|
2015-10-31 13:34:51 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
connections: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getConnections () {
|
|
|
|
return this.server.connections
|
2015-10-31 13:34:51 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
name: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getName () {
|
|
|
|
return 'LDAPServer'
|
2015-10-31 13:34:51 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
url: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getURL () {
|
2020-10-31 21:07:32 +00:00
|
|
|
let str
|
|
|
|
const addr = this.server.address()
|
2015-10-31 13:34:51 +00:00
|
|
|
if (!addr) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return null
|
2015-10-31 13:34:51 +00:00
|
|
|
}
|
|
|
|
if (!addr.family) {
|
2019-08-27 21:11:49 +00:00
|
|
|
str = 'ldapi://'
|
2020-10-31 21:07:32 +00:00
|
|
|
str += this.host.replace(/\//g, '%2f')
|
2019-08-27 21:11:49 +00:00
|
|
|
return str
|
2015-10-31 13:34:51 +00:00
|
|
|
}
|
|
|
|
if (this.server instanceof tls.Server) {
|
2019-08-27 21:11:49 +00:00
|
|
|
str = 'ldaps://'
|
2015-10-31 13:34:51 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
str = 'ldap://'
|
2015-10-31 13:34:51 +00:00
|
|
|
}
|
2022-06-08 00:59:33 +00:00
|
|
|
|
|
|
|
let host = this.host
|
|
|
|
// Node 18 switched family from returning a string to returning a number
|
|
|
|
// https://nodejs.org/api/net.html#serveraddress
|
|
|
|
if (addr.family === 'IPv6' || addr.family === 6) {
|
|
|
|
host = '[' + this.host + ']'
|
|
|
|
}
|
|
|
|
|
|
|
|
str += host + ':' + this.port
|
2019-08-27 21:11:49 +00:00
|
|
|
return str
|
2015-10-31 13:34:51 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
module.exports = Server
|
2011-08-06 20:44:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_ADD, name, args)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_BIND, name, args)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_COMPARE, name, args)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_DELETE, name, args)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_EXTENSION, name, args, true)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_MODIFY, name, args)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_MODRDN, name, args)
|
2019-08-27 21:11:49 +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) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_SEARCH, name, args)
|
2019-08-27 21:11:49 +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 () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments, 0)
|
2022-06-06 02:01:40 +00:00
|
|
|
return this._mount(Protocol.operations.LDAP_REQ_UNBIND, 'unbind', args, true)
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Server.prototype.use = function use () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const args = Array.prototype.slice.call(arguments)
|
|
|
|
const chain = mergeFunctionArgs(args, 0, args.length)
|
|
|
|
const self = this
|
2012-02-18 08:15:52 +00:00
|
|
|
chain.forEach(function (c) {
|
2019-08-27 21:11:49 +00:00
|
|
|
self._chain.push(c)
|
|
|
|
})
|
|
|
|
}
|
2012-01-06 23:10:46 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.after = function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!this._postChain) { this._postChain = [] }
|
2011-09-13 21:49:21 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2012-02-18 08:15:52 +00:00
|
|
|
mergeFunctionArgs(arguments).forEach(function (h) {
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +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') {
|
2019-08-27 21:11:49 +00:00
|
|
|
callback = host
|
|
|
|
host = '0.0.0.0'
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
2014-06-06 17:21:05 +00:00
|
|
|
if (typeof (port) === 'string' && /^[0-9]+$/.test(port)) {
|
|
|
|
// Disambiguate between string ports and file paths
|
2019-08-27 21:11:49 +00:00
|
|
|
port = parseInt(port, 10)
|
2014-06-06 17:21:05 +00:00
|
|
|
}
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function cbListen () {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (port) === 'number') {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.host = self.address().address
|
|
|
|
self.port = self.address().port
|
2011-08-10 17:57:58 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.host = port
|
|
|
|
self.port = self.server.fd
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (callback) === 'function') { callback() }
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (port) === 'number') {
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.server.listen(port, host, cbListen)
|
2011-08-10 17:57:58 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.server.listen(port, cbListen)
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.listenFD = function (fd) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.host = 'unix-domain-socket'
|
|
|
|
this.port = fd
|
|
|
|
return this.server.listenFD(fd)
|
|
|
|
}
|
2019-08-27 11:57:29 +00:00
|
|
|
Server.prototype.close = function (callback) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.server.close(callback)
|
|
|
|
}
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype.address = function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.server.address()
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-02-15 16:22:00 +00:00
|
|
|
Server.prototype.getConnections = function (callback) {
|
|
|
|
return this.server.getConnections(callback)
|
|
|
|
}
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Server.prototype._getRoute = function (_dn, backend) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(dn)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!backend) { backend = this }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let name
|
2011-08-06 20:44:26 +00:00
|
|
|
if (_dn instanceof dn.DN) {
|
2019-08-27 21:11:49 +00:00
|
|
|
name = _dn.toString()
|
2011-08-06 20:44:26 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
name = _dn
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-06 20:44:26 +00:00
|
|
|
if (!this.routes[name]) {
|
2019-08-27 21:11:49 +00:00
|
|
|
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
|
2019-08-27 21:11:49 +00:00
|
|
|
this._routeKeyCache = null
|
2011-08-06 20:44:26 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.routes[name]
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Server.prototype._sortedRouteKeys = function _sortedRouteKeys () {
|
2014-06-05 21:47:09 +00:00
|
|
|
// The filtered/sorted route keys are cached to prevent needlessly
|
|
|
|
// regenerating the list for every incoming request.
|
|
|
|
if (!this._routeKeyCache) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
|
|
|
const reversedRDNsToKeys = {}
|
2014-06-05 21:47:09 +00:00
|
|
|
// Generate mapping of reversedRDNs(DN) -> routeKey
|
|
|
|
Object.keys(this.routes).forEach(function (key) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const _dn = self.routes[key].dn
|
2014-06-05 21:47:09 +00:00
|
|
|
// Ignore non-DN routes such as exop or unbind
|
|
|
|
if (_dn instanceof dn.DN) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const reversed = _dn.clone()
|
2019-08-27 21:11:49 +00:00
|
|
|
reversed.rdns.reverse()
|
|
|
|
reversedRDNsToKeys[reversed.format()] = key
|
2014-06-05 21:47:09 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2020-10-31 21:07:32 +00:00
|
|
|
const output = []
|
2014-06-05 21:47:09 +00:00
|
|
|
// 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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
output.push(reversedRDNsToKeys[_dn])
|
|
|
|
})
|
|
|
|
this._routeKeyCache = output
|
2014-06-05 21:47:09 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return this._routeKeyCache
|
|
|
|
}
|
2014-06-05 21:47:09 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
Server.prototype._getHandlerChain = function _getHandlerChain (req, res) {
|
|
|
|
assert.ok(req)
|
2014-06-05 21:47:09 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
fireDTraceProbe(req, res)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
|
|
|
const routes = this.routes
|
|
|
|
let route
|
2011-11-19 00:39:37 +00:00
|
|
|
|
2011-08-24 19:38:23 +00:00
|
|
|
// check anonymous bind
|
2022-06-06 02:01:40 +00:00
|
|
|
if (req.protocolOp === Protocol.operations.LDAP_REQ_BIND &&
|
2011-08-24 19:38:23 +00:00
|
|
|
req.dn.toString() === '' &&
|
|
|
|
req.credentials === '') {
|
|
|
|
return {
|
|
|
|
backend: self,
|
2011-11-19 00:39:37 +00:00
|
|
|
handlers: [defaultNoOpHandler]
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2011-08-24 19:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const op = '0x' + req.protocolOp.toString(16)
|
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.
|
2022-06-06 02:01:40 +00:00
|
|
|
if (req.protocolOp === Protocol.operations.LDAP_REQ_EXTENSION) {
|
2019-08-27 21:11:49 +00:00
|
|
|
route = routes[req.requestName]
|
2014-06-05 21:47:09 +00:00
|
|
|
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])
|
2019-08-27 21:11:49 +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]
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2014-06-05 21:47:09 +00:00
|
|
|
}
|
2022-06-06 02:01:40 +00:00
|
|
|
} else if (req.protocolOp === Protocol.operations.LDAP_REQ_UNBIND) {
|
2019-08-27 21:11:49 +00:00
|
|
|
route = routes.unbind
|
2014-06-06 19:05:08 +00:00
|
|
|
if (route) {
|
|
|
|
return {
|
|
|
|
backend: route.backend,
|
|
|
|
handlers: route[op]
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2014-06-06 19:05:08 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
backend: self,
|
|
|
|
handlers: [defaultNoOpHandler]
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2014-06-06 19:05:08 +00:00
|
|
|
}
|
2022-06-06 02:01:40 +00:00
|
|
|
} else if (req.protocolOp === Protocol.operations.LDAP_REQ_ABANDON) {
|
2014-06-05 21:47:09 +00:00
|
|
|
return {
|
|
|
|
backend: self,
|
|
|
|
handlers: [defaultNoOpHandler]
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2014-06-05 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, match via DN rules
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(req.dn)
|
2020-10-31 21:07:32 +00:00
|
|
|
const keys = this._sortedRouteKeys()
|
|
|
|
let fallbackHandler = [noSuffixHandler]
|
2014-09-30 23:39:19 +00:00
|
|
|
// invalid DNs in non-strict mode are routed to the default handler
|
2020-10-31 21:07:32 +00:00
|
|
|
const testDN = (typeof (req.dn) === 'string') ? '' : req.dn
|
2014-09-30 23:39:19 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const suffix = keys[i]
|
2019-08-27 21:11:49 +00:00
|
|
|
route = routes[suffix]
|
|
|
|
assert.ok(route.dn)
|
2014-06-05 21:47:09 +00:00
|
|
|
// 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.
|
2019-08-27 21:11:49 +00:00
|
|
|
req.suffix = route.dn
|
2012-02-18 08:15:52 +00:00
|
|
|
return {
|
|
|
|
backend: route.backend,
|
2014-06-05 21:47:09 +00:00
|
|
|
handlers: route[op]
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2014-06-05 21:47:09 +00:00
|
|
|
} else {
|
2015-04-26 01:53:50 +00:00
|
|
|
if (suffix === '') {
|
2019-08-27 21:11:49 +00:00
|
|
|
break
|
2015-04-26 01:53:50 +00:00
|
|
|
} else {
|
|
|
|
// We found a valid suffix but not a valid operation.
|
|
|
|
// There might be a more generic suffix with a legitimate operation.
|
2019-08-27 21:11:49 +00:00
|
|
|
fallbackHandler = [defaultHandler]
|
2015-04-26 01:53:50 +00:00
|
|
|
}
|
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
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(op)
|
|
|
|
assert.ok(name !== undefined)
|
|
|
|
assert.ok(argv)
|
2011-08-15 16:44:31 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (name) !== 'string') { throw new TypeError('name (string) required') }
|
|
|
|
if (!argv.length) { throw new Error('at least one handler required') }
|
2011-08-15 16:44:31 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let backend = this
|
|
|
|
let 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])) {
|
2019-08-27 21:11:49 +00:00
|
|
|
backend = argv[0]
|
|
|
|
index = 1
|
2011-08-15 16:44:31 +00:00
|
|
|
}
|
2020-10-31 21:07:32 +00:00
|
|
|
const route = this._getRoute(notDN ? name : dn.parse(name), backend)
|
2012-01-06 23:10:46 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const chain = this._chain.slice()
|
2012-02-18 08:15:52 +00:00
|
|
|
argv.slice(index).forEach(function (a) {
|
2019-08-27 21:11:49 +00:00
|
|
|
chain.push(a)
|
|
|
|
})
|
|
|
|
route['0x' + op.toString(16)] = mergeFunctionArgs(chain)
|
2011-08-15 16:44:31 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return this
|
|
|
|
}
|