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-plus')
|
|
|
|
const util = require('util')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var asn1 = require('asn1')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const dtrace = require('../dtrace')
|
|
|
|
const LDAPMessage = require('./message')
|
|
|
|
const Protocol = require('../protocol')
|
2011-11-15 18:49:23 +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 BerWriter = asn1.BerWriter
|
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
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function LDAPResult (options) {
|
|
|
|
options = options || {}
|
|
|
|
assert.object(options)
|
|
|
|
assert.optionalNumber(options.status)
|
|
|
|
assert.optionalString(options.matchedDN)
|
|
|
|
assert.optionalString(options.errorMessage)
|
|
|
|
assert.optionalArrayOfString(options.referrals)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
LDAPMessage.call(this, options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.status = options.status || 0 // LDAP SUCCESS
|
|
|
|
this.matchedDN = options.matchedDN || ''
|
|
|
|
this.errorMessage = options.errorMessage || ''
|
|
|
|
this.referrals = options.referrals || []
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.connection = options.connection || null
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(LDAPResult, LDAPMessage)
|
2015-10-31 17:28:25 +00:00
|
|
|
Object.defineProperties(LDAPResult.prototype, {
|
|
|
|
type: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getType () { return 'LDAPResult' },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype.end = function (status) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(this.connection)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (typeof (status) === 'number') { this.status = status }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const ber = this.toBer()
|
2020-11-12 17:38:56 +00:00
|
|
|
this.log.debug('%s: sending: %j', this.connection.ldap.id, this.json)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2011-08-10 17:57:58 +00:00
|
|
|
try {
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2019-08-27 21:11:49 +00:00
|
|
|
this.connection.write(ber)
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2011-11-15 18:49:23 +00:00
|
|
|
if (self._dtraceOp && self._dtraceId) {
|
2012-02-18 08:15:52 +00:00
|
|
|
dtrace.fire('server-' + self._dtraceOp + '-done', function () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const c = self.connection || { ldap: {} }
|
2011-11-15 18:49:23 +00:00
|
|
|
return [
|
|
|
|
self._dtraceId || 0,
|
|
|
|
(c.remoteAddress || ''),
|
|
|
|
c.ldap.bindDN ? c.ldap.bindDN.toString() : '',
|
|
|
|
(self.requestDN ? self.requestDN.toString() : ''),
|
|
|
|
status || self.status,
|
|
|
|
self.errorMessage
|
2019-08-27 21:11:49 +00:00
|
|
|
]
|
|
|
|
})
|
2011-11-15 18:49:23 +00:00
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
} catch (e) {
|
2012-07-09 13:00:51 +00:00
|
|
|
this.log.warn(e, '%s failure to write message %j',
|
2019-08-27 21:11:49 +00:00
|
|
|
this.connection.ldap.id, this.json)
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype._parse = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.status = ber.readEnumeration()
|
|
|
|
this.matchedDN = ber.readString()
|
|
|
|
this.errorMessage = ber.readString()
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const t = ber.peek()
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
if (t === Protocol.LDAP_REP_REFERRAL) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const end = ber.offset + ber.length
|
2019-08-27 21:11:49 +00:00
|
|
|
while (ber.offset < end) { this.referrals.push(ber.readString()) }
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype._toBer = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeEnumeration(this.status)
|
|
|
|
ber.writeString(this.matchedDN || '')
|
|
|
|
ber.writeString(this.errorMessage || '')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
if (this.referrals.length) {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.startSequence(Protocol.LDAP_REP_REFERRAL)
|
|
|
|
ber.writeStringArray(this.referrals)
|
|
|
|
ber.endSequence()
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return ber
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype._json = function (j) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(j)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
j.status = this.status
|
|
|
|
j.matchedDN = this.matchedDN
|
|
|
|
j.errorMessage = this.errorMessage
|
|
|
|
j.referrals = this.referrals
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return j
|
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
module.exports = LDAPResult
|