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 LDAPMessage = require('./message')
|
2019-08-27 21:11:49 +00:00
|
|
|
// var LDAPResult = require('./result')
|
2020-10-31 21:07:32 +00:00
|
|
|
const dn = require('../dn')
|
|
|
|
const filters = require('../filters')
|
|
|
|
const Protocol = require('../protocol')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const Ber = asn1.Ber
|
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 SearchRequest (options) {
|
|
|
|
options = options || {}
|
|
|
|
assert.object(options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
options.protocolOp = Protocol.LDAP_REQ_SEARCH
|
|
|
|
LDAPMessage.call(this, options)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2015-04-26 01:53:50 +00:00
|
|
|
if (options.baseObject !== undefined) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.baseObject = options.baseObject
|
2015-04-26 01:53:50 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.baseObject = dn.parse('')
|
2015-04-26 01:53:50 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
this.scope = options.scope || 'base'
|
|
|
|
this.derefAliases = options.derefAliases || Protocol.NEVER_DEREF_ALIASES
|
|
|
|
this.sizeLimit = options.sizeLimit || 0
|
|
|
|
this.timeLimit = options.timeLimit || 0
|
|
|
|
this.typesOnly = options.typesOnly || false
|
|
|
|
this.filter = options.filter || null
|
|
|
|
this.attributes = options.attributes ? options.attributes.slice(0) : []
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(SearchRequest, LDAPMessage)
|
2015-10-31 17:28:25 +00:00
|
|
|
Object.defineProperties(SearchRequest.prototype, {
|
|
|
|
type: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getType () { return 'SearchRequest' },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
_dn: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getDN () { return this.baseObject },
|
2015-10-31 17:28:25 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
scope: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getScope () {
|
2015-10-31 17:28:25 +00:00
|
|
|
switch (this._scope) {
|
2019-08-27 21:11:49 +00:00
|
|
|
case Protocol.SCOPE_BASE_OBJECT: return 'base'
|
|
|
|
case Protocol.SCOPE_ONE_LEVEL: return 'one'
|
|
|
|
case Protocol.SCOPE_SUBTREE: return 'sub'
|
|
|
|
default:
|
|
|
|
throw new Error(this._scope + ' is an invalid search scope')
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
},
|
2019-08-27 21:11:49 +00:00
|
|
|
set: function setScope (val) {
|
2015-10-31 17:28:25 +00:00
|
|
|
if (typeof (val) === 'string') {
|
|
|
|
switch (val) {
|
2019-08-27 21:11:49 +00:00
|
|
|
case 'base':
|
|
|
|
this._scope = Protocol.SCOPE_BASE_OBJECT
|
|
|
|
break
|
|
|
|
case 'one':
|
|
|
|
this._scope = Protocol.SCOPE_ONE_LEVEL
|
|
|
|
break
|
|
|
|
case 'sub':
|
|
|
|
this._scope = Protocol.SCOPE_SUBTREE
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error(val + ' is an invalid search scope')
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._scope = val
|
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
|
|
|
SearchRequest.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.baseObject = ber.readString()
|
|
|
|
this.scope = ber.readEnumeration()
|
|
|
|
this.derefAliases = ber.readEnumeration()
|
|
|
|
this.sizeLimit = ber.readInt()
|
|
|
|
this.timeLimit = ber.readInt()
|
|
|
|
this.typesOnly = ber.readBoolean()
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.filter = filters.parse(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
// look for attributes
|
2011-08-10 21:46:04 +00:00
|
|
|
if (ber.peek() === 0x30) {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.readSequence()
|
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.attributes.push(ber.readString().toLowerCase()) }
|
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
|
|
|
SearchRequest.prototype._toBer = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-05-13 15:01:00 +00:00
|
|
|
// Format only with commas, since that is what RFC 4514 mandates.
|
|
|
|
// There's a gotcha here: even though it's called baseObject,
|
|
|
|
// it can be a string or a DN object.
|
2020-10-31 21:07:32 +00:00
|
|
|
const formattedDN = dn.DN.isDN(this.baseObject)
|
2020-05-13 15:01:00 +00:00
|
|
|
? this.baseObject.format({ skipSpace: true })
|
|
|
|
: this.baseObject.toString()
|
|
|
|
ber.writeString(formattedDN)
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeEnumeration(this._scope)
|
|
|
|
ber.writeEnumeration(this.derefAliases)
|
|
|
|
ber.writeInt(this.sizeLimit)
|
|
|
|
ber.writeInt(this.timeLimit)
|
|
|
|
ber.writeBoolean(this.typesOnly)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const f = this.filter || new filters.PresenceFilter({ attribute: 'objectclass' })
|
2019-08-27 21:11:49 +00:00
|
|
|
ber = f.toBer(ber)
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +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) {
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeString(a)
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
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
|
|
|
SearchRequest.prototype._json = function (j) {
|
2019-08-27 21:11:49 +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
|
|
|
|
}
|
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 = SearchRequest
|