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
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const asn1 = require('asn1')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const logger = require('../logger')
|
2019-08-27 21:11:49 +00:00
|
|
|
// var Control = require('../controls').Control
|
|
|
|
// var Protocol = require('../protocol')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2011-12-08 22:54:40 +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 BerWriter = asn1.BerWriter
|
|
|
|
const getControl = require('../controls').getControl
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2011-12-08 19:22:35 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
/**
|
|
|
|
* LDAPMessage structure.
|
|
|
|
*
|
|
|
|
* @param {Object} options stuff.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
function LDAPMessage (options) {
|
|
|
|
assert.object(options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.messageID = options.messageID || 0
|
|
|
|
this.protocolOp = options.protocolOp || undefined
|
|
|
|
this.controls = options.controls ? options.controls.slice(0) : []
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.log = options.log || logger
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
Object.defineProperties(LDAPMessage.prototype, {
|
|
|
|
id: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getId () { return this.messageID },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
dn: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getDN () { return this._dn || '' },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
type: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getType () { return 'LDAPMessage' },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
json: {
|
|
|
|
get: function () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const out = this._json({
|
2015-10-31 17:28:25 +00:00
|
|
|
messageID: this.messageID,
|
|
|
|
protocolOp: this.type
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
out.controls = this.controls
|
|
|
|
return out
|
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
|
|
|
LDAPMessage.prototype.toString = function () {
|
2019-08-27 21:11:49 +00:00
|
|
|
return JSON.stringify(this.json)
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPMessage.prototype.parse = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-11-12 17:38:56 +00:00
|
|
|
this.log.trace('parse: data=%s', util.inspect(ber.buffer))
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
// Delegate off to the specific type to parse
|
2019-08-27 21:11:49 +00:00
|
|
|
this._parse(ber, ber.length)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
// Look for controls
|
2011-08-10 21:46:04 +00:00
|
|
|
if (ber.peek() === 0xa0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.readSequence()
|
2020-10-31 21:07:32 +00:00
|
|
|
const end = ber.offset + ber.length
|
2011-08-04 20:32:01 +00:00
|
|
|
while (ber.offset < end) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const c = getControl(ber)
|
2019-08-27 21:11:49 +00:00
|
|
|
if (c) { this.controls.push(c) }
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:38:56 +00:00
|
|
|
this.log.trace('Parsing done: %j', this.json)
|
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
|
|
|
LDAPMessage.prototype.toBer = function () {
|
2020-10-31 21:07:32 +00:00
|
|
|
let writer = new BerWriter()
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.startSequence()
|
|
|
|
writer.writeInt(this.messageID)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.startSequence(this.protocolOp)
|
|
|
|
if (this._toBer) { writer = this._toBer(writer) }
|
|
|
|
writer.endSequence()
|
2011-09-16 16:06:07 +00:00
|
|
|
|
|
|
|
if (this.controls && this.controls.length) {
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.startSequence(0xa0)
|
2012-02-18 08:15:52 +00:00
|
|
|
this.controls.forEach(function (c) {
|
2019-08-27 21:11:49 +00:00
|
|
|
c.toBer(writer)
|
|
|
|
})
|
|
|
|
writer.endSequence()
|
2011-09-16 16:06:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.endSequence()
|
|
|
|
return writer.buffer
|
|
|
|
}
|
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 = LDAPMessage
|