2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2015-10-31 17:28:25 +00:00
|
|
|
var assert = require('assert-plus');
|
2011-08-04 20:32:01 +00:00
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var asn1 = require('asn1');
|
|
|
|
|
2011-12-08 22:54:40 +00:00
|
|
|
var Control = require('../controls').Control;
|
2011-08-04 20:32:01 +00:00
|
|
|
var Protocol = require('../protocol');
|
|
|
|
|
2011-12-08 22:54:40 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var Ber = asn1.Ber;
|
|
|
|
var BerReader = asn1.BerReader;
|
|
|
|
var BerWriter = asn1.BerWriter;
|
2011-12-08 22:54:40 +00:00
|
|
|
var getControl = require('../controls').getControl;
|
|
|
|
|
|
|
|
|
2011-08-04 20:32:01 +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.
|
|
|
|
*/
|
|
|
|
function LDAPMessage(options) {
|
2015-10-31 17:28:25 +00:00
|
|
|
assert.object(options);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
this.messageID = options.messageID || 0;
|
|
|
|
this.protocolOp = options.protocolOp || undefined;
|
|
|
|
this.controls = options.controls ? options.controls.slice(0) : [];
|
|
|
|
|
2012-02-18 08:54:22 +00:00
|
|
|
this.log = options.log;
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
Object.defineProperties(LDAPMessage.prototype, {
|
|
|
|
id: {
|
|
|
|
get: function getId() { return this.messageID; },
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
dn: {
|
|
|
|
get: function getDN() { return this._dn || ''; },
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
type: {
|
|
|
|
get: function getType() { return 'LDAPMessage'; },
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
json: {
|
|
|
|
get: function () {
|
|
|
|
var out = this._json({
|
|
|
|
messageID: this.messageID,
|
|
|
|
protocolOp: this.type
|
|
|
|
});
|
|
|
|
out.controls = this.controls;
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPMessage.prototype.toString = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return JSON.stringify(this.json);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPMessage.prototype.parse = function (ber) {
|
2011-11-11 18:08:48 +00:00
|
|
|
assert.ok(ber);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:54:22 +00:00
|
|
|
if (this.log.trace())
|
2011-11-11 18:08:48 +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
|
2014-07-16 23:53:06 +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) {
|
2011-08-04 20:32:01 +00:00
|
|
|
ber.readSequence();
|
|
|
|
var end = ber.offset + ber.length;
|
|
|
|
while (ber.offset < end) {
|
2011-12-08 01:02:17 +00:00
|
|
|
var c = getControl(ber);
|
2011-12-08 19:22:35 +00:00
|
|
|
if (c)
|
2011-08-04 20:32:01 +00:00
|
|
|
this.controls.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-18 08:54:22 +00:00
|
|
|
if (this.log.trace())
|
2011-08-04 20:32:01 +00:00
|
|
|
this.log.trace('Parsing done: %j', this.json);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPMessage.prototype.toBer = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
var writer = new BerWriter();
|
|
|
|
writer.startSequence();
|
|
|
|
writer.writeInt(this.messageID);
|
|
|
|
|
2011-09-16 16:06:07 +00:00
|
|
|
writer.startSequence(this.protocolOp);
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this._toBer)
|
|
|
|
writer = this._toBer(writer);
|
|
|
|
writer.endSequence();
|
2011-09-16 16:06:07 +00:00
|
|
|
|
|
|
|
if (this.controls && this.controls.length) {
|
|
|
|
writer.startSequence(0xa0);
|
2012-02-18 08:15:52 +00:00
|
|
|
this.controls.forEach(function (c) {
|
2011-09-16 16:06:07 +00:00
|
|
|
c.toBer(writer);
|
|
|
|
});
|
|
|
|
writer.endSequence();
|
|
|
|
}
|
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
writer.endSequence();
|
|
|
|
return writer.buffer;
|
|
|
|
};
|
2015-10-31 17:28:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Exports
|
|
|
|
|
|
|
|
module.exports = LDAPMessage;
|