2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var asn1 = require('asn1');
|
|
|
|
|
2011-11-15 18:49:23 +00:00
|
|
|
var dtrace = require('../dtrace');
|
2011-08-04 20:32:01 +00:00
|
|
|
var LDAPMessage = require('./message');
|
|
|
|
var Protocol = require('../protocol');
|
2011-11-15 18:49:23 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var Ber = asn1.Ber;
|
|
|
|
var BerWriter = asn1.BerWriter;
|
|
|
|
|
|
|
|
|
2011-08-30 04:48:05 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
///--- API
|
|
|
|
|
|
|
|
function LDAPResult(options) {
|
|
|
|
if (options) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (options) !== 'object')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options (object) required');
|
2012-02-18 08:15:52 +00:00
|
|
|
if (options.status && typeof (options.status) !== 'number')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.status must be a number');
|
2012-02-18 08:15:52 +00:00
|
|
|
if (options.matchedDN && typeof (options.matchedDN) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.matchedDN must be a string');
|
2012-02-18 08:15:52 +00:00
|
|
|
if (options.errorMessage && typeof (options.errorMessage) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.errorMessage must be a string');
|
|
|
|
|
|
|
|
if (options.referrals) {
|
|
|
|
if (!(options.referrals instanceof Array))
|
|
|
|
throw new TypeError('options.referrrals must be an array[string]');
|
2012-02-18 08:15:52 +00:00
|
|
|
options.referrals.forEach(function (r) {
|
|
|
|
if (typeof (r) !== 'string')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options.referrals must be an array[string]');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
LDAPMessage.call(this, options);
|
|
|
|
|
|
|
|
this.status = options.status || 0; // LDAP SUCCESS
|
|
|
|
this.matchedDN = options.matchedDN || '';
|
|
|
|
this.errorMessage = options.errorMessage || '';
|
|
|
|
this.referrals = options.referrals || [];
|
|
|
|
|
2011-09-15 21:49:00 +00:00
|
|
|
this.connection = options.connection || null;
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('type', function () { return 'LDAPResult'; });
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
util.inherits(LDAPResult, LDAPMessage);
|
|
|
|
module.exports = LDAPResult;
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype.end = function (status) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(this.connection);
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (status) === 'number')
|
2011-08-04 20:32:01 +00:00
|
|
|
this.status = status;
|
|
|
|
|
|
|
|
var ber = this.toBer();
|
2012-02-18 08:54:22 +00:00
|
|
|
if (this.log.debug())
|
2011-08-04 20:32:01 +00:00
|
|
|
this.log.debug('%s: sending: %j', this.connection.ldap.id, this.json);
|
|
|
|
|
2011-08-10 17:57:58 +00:00
|
|
|
try {
|
2011-08-30 04:48:05 +00:00
|
|
|
var self = this;
|
2011-08-10 17:57:58 +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 () {
|
2011-11-15 18:49:23 +00:00
|
|
|
var c = self.connection || {ldap: {}};
|
|
|
|
return [
|
|
|
|
self._dtraceId || 0,
|
|
|
|
(c.remoteAddress || ''),
|
|
|
|
c.ldap.bindDN ? c.ldap.bindDN.toString() : '',
|
|
|
|
(self.requestDN ? self.requestDN.toString() : ''),
|
|
|
|
status || self.status,
|
|
|
|
self.errorMessage
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
this.connection.ldap.id, this.json);
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
2011-08-30 04:48:05 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype._parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
this.status = ber.readEnumeration();
|
|
|
|
this.matchedDN = ber.readString();
|
|
|
|
this.errorMessage = ber.readString();
|
|
|
|
|
|
|
|
var t = ber.peek();
|
|
|
|
|
|
|
|
if (t === Protocol.LDAP_REP_REFERRAL) {
|
|
|
|
var end = ber.offset + ber.length;
|
|
|
|
while (ber.offset < end)
|
|
|
|
this.referrals.push(ber.readString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype._toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.writeEnumeration(this.status);
|
|
|
|
ber.writeString(this.matchedDN || '');
|
|
|
|
ber.writeString(this.errorMessage || '');
|
|
|
|
|
|
|
|
if (this.referrals.length) {
|
|
|
|
ber.startSequence(Protocol.LDAP_REP_REFERRAL);
|
|
|
|
ber.writeStringArray(this.referrals);
|
|
|
|
ber.endSequence();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
LDAPResult.prototype._json = function (j) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(j);
|
|
|
|
|
|
|
|
j.status = this.status;
|
|
|
|
j.matchedDN = this.matchedDN;
|
|
|
|
j.errorMessage = this.errorMessage;
|
|
|
|
j.referrals = this.referrals;
|
|
|
|
|
|
|
|
return j;
|
|
|
|
};
|