UNPKG

2.93 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3const assert = require('assert-plus')
4const util = require('util')
5
6// var asn1 = require('asn1')
7
8const dtrace = require('../dtrace')
9const LDAPMessage = require('./message')
10const Protocol = require('../protocol')
11
12/// --- Globals
13
14// var Ber = asn1.Ber
15// var BerWriter = asn1.BerWriter
16
17/// --- API
18
19function LDAPResult (options) {
20 options = options || {}
21 assert.object(options)
22 assert.optionalNumber(options.status)
23 assert.optionalString(options.matchedDN)
24 assert.optionalString(options.errorMessage)
25 assert.optionalArrayOfString(options.referrals)
26
27 LDAPMessage.call(this, options)
28
29 this.status = options.status || 0 // LDAP SUCCESS
30 this.matchedDN = options.matchedDN || ''
31 this.errorMessage = options.errorMessage || ''
32 this.referrals = options.referrals || []
33
34 this.connection = options.connection || null
35}
36util.inherits(LDAPResult, LDAPMessage)
37Object.defineProperties(LDAPResult.prototype, {
38 type: {
39 get: function getType () { return 'LDAPResult' },
40 configurable: false
41 }
42})
43
44LDAPResult.prototype.end = function (status) {
45 assert.ok(this.connection)
46
47 if (typeof (status) === 'number') { this.status = status }
48
49 const ber = this.toBer()
50 this.log.debug('%s: sending: %j', this.connection.ldap.id, this.json)
51
52 try {
53 const self = this
54 this.connection.write(ber)
55
56 if (self._dtraceOp && self._dtraceId) {
57 dtrace.fire('server-' + self._dtraceOp + '-done', function () {
58 const c = self.connection || { ldap: {} }
59 return [
60 self._dtraceId || 0,
61 (c.remoteAddress || ''),
62 c.ldap.bindDN ? c.ldap.bindDN.toString() : '',
63 (self.requestDN ? self.requestDN.toString() : ''),
64 status || self.status,
65 self.errorMessage
66 ]
67 })
68 }
69 } catch (e) {
70 this.log.warn(e, '%s failure to write message %j',
71 this.connection.ldap.id, this.json)
72 }
73}
74
75LDAPResult.prototype._parse = function (ber) {
76 assert.ok(ber)
77
78 this.status = ber.readEnumeration()
79 this.matchedDN = ber.readString()
80 this.errorMessage = ber.readString()
81
82 const t = ber.peek()
83
84 if (t === Protocol.LDAP_REP_REFERRAL) {
85 const end = ber.offset + ber.length
86 while (ber.offset < end) { this.referrals.push(ber.readString()) }
87 }
88
89 return true
90}
91
92LDAPResult.prototype._toBer = function (ber) {
93 assert.ok(ber)
94
95 ber.writeEnumeration(this.status)
96 ber.writeString(this.matchedDN || '')
97 ber.writeString(this.errorMessage || '')
98
99 if (this.referrals.length) {
100 ber.startSequence(Protocol.LDAP_REP_REFERRAL)
101 ber.writeStringArray(this.referrals)
102 ber.endSequence()
103 }
104
105 return ber
106}
107
108LDAPResult.prototype._json = function (j) {
109 assert.ok(j)
110
111 j.status = this.status
112 j.matchedDN = this.matchedDN
113 j.errorMessage = this.errorMessage
114 j.referrals = this.referrals
115
116 return j
117}
118
119/// --- Exports
120
121module.exports = LDAPResult