1 |
|
2 |
|
3 | const assert = require('assert-plus')
|
4 | const util = require('util')
|
5 |
|
6 | const asn1 = require('asn1')
|
7 |
|
8 | const LDAPMessage = require('./message')
|
9 |
|
10 | const dn = require('../dn')
|
11 | const filters = require('../filters')
|
12 | const Protocol = require('../protocol')
|
13 |
|
14 |
|
15 |
|
16 | const Ber = asn1.Ber
|
17 |
|
18 |
|
19 |
|
20 | function SearchRequest (options) {
|
21 | options = options || {}
|
22 | assert.object(options)
|
23 |
|
24 | options.protocolOp = Protocol.LDAP_REQ_SEARCH
|
25 | LDAPMessage.call(this, options)
|
26 |
|
27 | if (options.baseObject !== undefined) {
|
28 | this.baseObject = options.baseObject
|
29 | } else {
|
30 | this.baseObject = dn.parse('')
|
31 | }
|
32 | this.scope = options.scope || 'base'
|
33 | this.derefAliases = options.derefAliases || Protocol.NEVER_DEREF_ALIASES
|
34 | this.sizeLimit = options.sizeLimit || 0
|
35 | this.timeLimit = options.timeLimit || 0
|
36 | this.typesOnly = options.typesOnly || false
|
37 | this.filter = options.filter || null
|
38 | this.attributes = options.attributes ? options.attributes.slice(0) : []
|
39 | }
|
40 | util.inherits(SearchRequest, LDAPMessage)
|
41 | Object.defineProperties(SearchRequest.prototype, {
|
42 | type: {
|
43 | get: function getType () { return 'SearchRequest' },
|
44 | configurable: false
|
45 | },
|
46 | _dn: {
|
47 | get: function getDN () { return this.baseObject },
|
48 | configurable: false
|
49 | },
|
50 | scope: {
|
51 | get: function getScope () {
|
52 | switch (this._scope) {
|
53 | case Protocol.SCOPE_BASE_OBJECT: return 'base'
|
54 | case Protocol.SCOPE_ONE_LEVEL: return 'one'
|
55 | case Protocol.SCOPE_SUBTREE: return 'sub'
|
56 | default:
|
57 | throw new Error(this._scope + ' is an invalid search scope')
|
58 | }
|
59 | },
|
60 | set: function setScope (val) {
|
61 | if (typeof (val) === 'string') {
|
62 | switch (val) {
|
63 | case 'base':
|
64 | this._scope = Protocol.SCOPE_BASE_OBJECT
|
65 | break
|
66 | case 'one':
|
67 | this._scope = Protocol.SCOPE_ONE_LEVEL
|
68 | break
|
69 | case 'sub':
|
70 | this._scope = Protocol.SCOPE_SUBTREE
|
71 | break
|
72 | default:
|
73 | throw new Error(val + ' is an invalid search scope')
|
74 | }
|
75 | } else {
|
76 | this._scope = val
|
77 | }
|
78 | },
|
79 | configurable: false
|
80 | }
|
81 | })
|
82 |
|
83 | SearchRequest.prototype._parse = function (ber) {
|
84 | assert.ok(ber)
|
85 |
|
86 | this.baseObject = ber.readString()
|
87 | this.scope = ber.readEnumeration()
|
88 | this.derefAliases = ber.readEnumeration()
|
89 | this.sizeLimit = ber.readInt()
|
90 | this.timeLimit = ber.readInt()
|
91 | this.typesOnly = ber.readBoolean()
|
92 |
|
93 | this.filter = filters.parse(ber)
|
94 |
|
95 |
|
96 | if (ber.peek() === 0x30) {
|
97 | ber.readSequence()
|
98 | const end = ber.offset + ber.length
|
99 | while (ber.offset < end) { this.attributes.push(ber.readString().toLowerCase()) }
|
100 | }
|
101 |
|
102 | return true
|
103 | }
|
104 |
|
105 | SearchRequest.prototype._toBer = function (ber) {
|
106 | assert.ok(ber)
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | const formattedDN = dn.DN.isDN(this.baseObject)
|
112 | ? this.baseObject.format({ skipSpace: true })
|
113 | : this.baseObject.toString()
|
114 | ber.writeString(formattedDN)
|
115 | ber.writeEnumeration(this._scope)
|
116 | ber.writeEnumeration(this.derefAliases)
|
117 | ber.writeInt(this.sizeLimit)
|
118 | ber.writeInt(this.timeLimit)
|
119 | ber.writeBoolean(this.typesOnly)
|
120 |
|
121 | const f = this.filter || new filters.PresenceFilter({ attribute: 'objectclass' })
|
122 | ber = f.toBer(ber)
|
123 |
|
124 | ber.startSequence(Ber.Sequence | Ber.Constructor)
|
125 | if (this.attributes && this.attributes.length) {
|
126 | this.attributes.forEach(function (a) {
|
127 | ber.writeString(a)
|
128 | })
|
129 | }
|
130 | ber.endSequence()
|
131 |
|
132 | return ber
|
133 | }
|
134 |
|
135 | SearchRequest.prototype._json = function (j) {
|
136 | assert.ok(j)
|
137 |
|
138 | j.baseObject = this.baseObject
|
139 | j.scope = this.scope
|
140 | j.derefAliases = this.derefAliases
|
141 | j.sizeLimit = this.sizeLimit
|
142 | j.timeLimit = this.timeLimit
|
143 | j.typesOnly = this.typesOnly
|
144 | j.filter = this.filter.toString()
|
145 | j.attributes = this.attributes
|
146 |
|
147 | return j
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 | module.exports = SearchRequest
|