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');
|
|
|
|
|
|
|
|
var LDAPMessage = require('./message');
|
|
|
|
var LDAPResult = require('./result');
|
|
|
|
var dn = require('../dn');
|
|
|
|
var filters = require('../filters');
|
|
|
|
var Protocol = require('../protocol');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
var DN = dn.DN;
|
2011-08-04 20:32:01 +00:00
|
|
|
var Ber = asn1.Ber;
|
|
|
|
|
|
|
|
|
|
|
|
///--- API
|
|
|
|
|
|
|
|
function SearchRequest(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 must be an object');
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
options.protocolOp = Protocol.LDAP_REQ_SEARCH;
|
|
|
|
LDAPMessage.call(this, options);
|
|
|
|
|
|
|
|
var self = this;
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('type', function () { return 'SearchRequest'; });
|
2014-09-30 23:39:19 +00:00
|
|
|
this.__defineGetter__('_dn', function () { return self.baseObject; });
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('scope', function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
switch (self._scope) {
|
|
|
|
case Protocol.SCOPE_BASE_OBJECT: return 'base';
|
|
|
|
case Protocol.SCOPE_ONE_LEVEL: return 'one';
|
|
|
|
case Protocol.SCOPE_SUBTREE: return 'sub';
|
|
|
|
default:
|
|
|
|
throw new Error(self._scope + ' is an invalid search scope');
|
|
|
|
}
|
|
|
|
});
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineSetter__('scope', function (s) {
|
|
|
|
if (typeof (s) === 'string') {
|
2011-08-04 20:32:01 +00:00
|
|
|
switch (s) {
|
|
|
|
case 'base':
|
|
|
|
self._scope = Protocol.SCOPE_BASE_OBJECT;
|
|
|
|
break;
|
|
|
|
case 'one':
|
|
|
|
self._scope = Protocol.SCOPE_ONE_LEVEL;
|
|
|
|
break;
|
|
|
|
case 'sub':
|
|
|
|
self._scope = Protocol.SCOPE_SUBTREE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(s + ' is an invalid search scope');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self._scope = s;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
this.baseObject = options.baseObject || new DN([ {} ]);
|
2011-08-04 20:32:01 +00:00
|
|
|
this.scope = options.scope || 'base';
|
|
|
|
this.derefAliases = options.derefAliases || Protocol.NEVER_DEREF_ALIASES;
|
|
|
|
this.sizeLimit = options.sizeLimit || 0;
|
2011-08-23 23:25:07 +00:00
|
|
|
this.timeLimit = options.timeLimit || 0;
|
2011-08-04 20:32:01 +00:00
|
|
|
this.typesOnly = options.typesOnly || false;
|
|
|
|
this.filter = options.filter || null;
|
|
|
|
this.attributes = options.attributes ? options.attributes.slice(0) : [];
|
|
|
|
}
|
|
|
|
util.inherits(SearchRequest, LDAPMessage);
|
|
|
|
module.exports = SearchRequest;
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchRequest.prototype.newResult = function () {
|
2011-08-04 20:32:01 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new LDAPResult({
|
|
|
|
messageID: self.messageID,
|
|
|
|
protocolOp: Protocol.LDAP_REP_SEARCH
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchRequest.prototype._parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
this.baseObject = ber.readString();
|
2011-08-04 20:32:01 +00:00
|
|
|
this.scope = ber.readEnumeration();
|
|
|
|
this.derefAliases = ber.readEnumeration();
|
|
|
|
this.sizeLimit = ber.readInt();
|
|
|
|
this.timeLimit = ber.readInt();
|
|
|
|
this.typesOnly = ber.readBoolean();
|
|
|
|
|
|
|
|
this.filter = filters.parse(ber);
|
|
|
|
|
|
|
|
// look for attributes
|
2011-08-10 21:46:04 +00:00
|
|
|
if (ber.peek() === 0x30) {
|
|
|
|
ber.readSequence();
|
2011-08-04 20:32:01 +00:00
|
|
|
var end = ber.offset + ber.length;
|
|
|
|
while (ber.offset < end)
|
|
|
|
this.attributes.push(ber.readString().toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchRequest.prototype._toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.writeString(this.baseObject.toString());
|
|
|
|
ber.writeEnumeration(this._scope);
|
|
|
|
ber.writeEnumeration(this.derefAliases);
|
|
|
|
ber.writeInt(this.sizeLimit);
|
|
|
|
ber.writeInt(this.timeLimit);
|
|
|
|
ber.writeBoolean(this.typesOnly);
|
|
|
|
|
|
|
|
var f = this.filter || new filters.PresenceFilter({attribute: 'objectclass'});
|
|
|
|
ber = f.toBer(ber);
|
|
|
|
|
2011-09-02 13:29:33 +00:00
|
|
|
ber.startSequence(Ber.Sequence | Ber.Constructor);
|
2011-08-04 20:32:01 +00:00
|
|
|
if (this.attributes && this.attributes.length) {
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
ber.writeString(a);
|
|
|
|
});
|
|
|
|
}
|
2011-09-02 13:29:33 +00:00
|
|
|
ber.endSequence();
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchRequest.prototype._json = function (j) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(j);
|
|
|
|
|
|
|
|
j.baseObject = this.baseObject;
|
|
|
|
j.scope = this.scope;
|
|
|
|
j.derefAliases = this.derefAliases;
|
|
|
|
j.sizeLimit = this.sizeLimit;
|
|
|
|
j.timeLimit = this.timeLimit;
|
|
|
|
j.typesOnly = this.typesOnly;
|
|
|
|
j.filter = this.filter.toString();
|
|
|
|
j.attributes = this.attributes;
|
|
|
|
|
|
|
|
return j;
|
|
|
|
};
|