diff --git a/lib/filters/equality_filter.js b/lib/filters/equality_filter.js index 9899dd5..2fae0c6 100644 --- a/lib/filters/equality_filter.js +++ b/lib/filters/equality_filter.js @@ -48,8 +48,17 @@ EqualityFilter.prototype.matches = function(target) { throw new TypeError('target (object) required'); var matches = false; - if (target.hasOwnProperty(this.attribute)) - matches = (this.value === target[this.attribute]); + if (target.hasOwnProperty(this.attribute)) { + if (Array.isArray(target[this.attribute])) { + for (var i = 0; i < target[this.attribute].length; i++) { + matches = (this.value === target[this.attribute][i]); + if (matches) + break; + } + } else { + matches = (this.value === target[this.attribute]); + } + } return matches; }; diff --git a/lib/filters/filter.js b/lib/filters/filter.js index 8556835..75c146b 100644 --- a/lib/filters/filter.js +++ b/lib/filters/filter.js @@ -5,10 +5,16 @@ var assert = require('assert'); var asn1 = require('asn1'); +var Protocol = require('../protocol'); + + + ///--- Globals var BerWriter = asn1.BerWriter; + + ///--- API function Filter(options) { @@ -21,7 +27,21 @@ function Filter(options) { var self = this; this.__defineGetter__('type', function() { - return '0x' + self._type.toString(16); + switch (self._type) { + case Protocol.FILTER_AND: return 'and'; + case Protocol.FILTER_OR: return 'or'; + case Protocol.FILTER_NOT: return 'not'; + case Protocol.FILTER_EQUALITY: return 'equal'; + case Protocol.FILTER_SUBSTRINGS: return 'substring'; + case Protocol.FILTER_GE: return 'ge'; + case Protocol.FILTER_LE: return 'le'; + case Protocol.FILTER_PRESENT: return 'present'; + case Protocol.FILTER_APPROX: return 'approx'; + case Protocol.FILTER_EXT: return 'ext'; + default: + throw new Error('0x' + self._type.toString(16) + + ' is an invalid search filter'); + } }); } module.exports = Filter; diff --git a/lib/filters/ge_filter.js b/lib/filters/ge_filter.js index cbb5a5a..cb2e19b 100644 --- a/lib/filters/ge_filter.js +++ b/lib/filters/ge_filter.js @@ -49,8 +49,17 @@ GreaterThanEqualsFilter.prototype.matches = function(target) { throw new TypeError('target (object) required'); var matches = false; - if (target.hasOwnProperty(this.attribute)) - matches = (target[this.attribute] >= this.value); + if (target.hasOwnProperty(this.attribute)) { + if (Array.isArray(target[this.attribute])) { + for (var i = 0; i < target[this.attribute].length; i++) { + matches = (this.value <= target[this.attribute][i]); + if (matches) + break; + } + } else { + matches = (this.value <= target[this.attribute]); + } + } return matches; }; diff --git a/lib/filters/le_filter.js b/lib/filters/le_filter.js index 86d1fa1..9c72303 100644 --- a/lib/filters/le_filter.js +++ b/lib/filters/le_filter.js @@ -49,8 +49,18 @@ LessThanEqualsFilter.prototype.matches = function(target) { throw new TypeError('target (object) required'); var matches = false; - if (target.hasOwnProperty(this.attribute)) - matches = (target[this.attribute] <= this.value); + if (target.hasOwnProperty(this.attribute)) { + if (Array.isArray(target[this.attribute])) { + for (var i = 0; i < target[this.attribute].length; i++) { + matches = (this.value >= target[this.attribute][i]); + if (matches) + break; + } + } else { + matches = (this.value >= target[this.attribute]); + } + } + return matches; }; diff --git a/lib/filters/presence_filter.js b/lib/filters/presence_filter.js index 8483ded..4ed4ae3 100644 --- a/lib/filters/presence_filter.js +++ b/lib/filters/presence_filter.js @@ -42,7 +42,6 @@ PresenceFilter.prototype.matches = function(target) { if (typeof(target) !== 'object') throw new TypeError('target (object) required'); - console.log(this.attribute); return target.hasOwnProperty(this.attribute); }; @@ -50,7 +49,14 @@ PresenceFilter.prototype.matches = function(target) { PresenceFilter.prototype.parse = function(ber) { assert.ok(ber); - this.attribute = ber.buffer.slice(0, ber.length).toString('utf8'); + this.attribute = + ber + .buffer + .slice(0, ber.length) + .toString('utf8') + .toLowerCase(); + + ber._offset += ber.length; return true; }; diff --git a/lib/messages/message.js b/lib/messages/message.js index 9d861d4..a95e958 100644 --- a/lib/messages/message.js +++ b/lib/messages/message.js @@ -75,7 +75,7 @@ LDAPMessage.prototype.parse = function(data, length) { this._parse(ber, length); // Look for controls - if (ber.peek === Protocol.LDAP_CONTROLS && ber.offset < length) { + if (ber.peek() === 0xa0) { ber.readSequence(); var end = ber.offset + ber.length; while (ber.offset < end) { diff --git a/lib/messages/search_entry.js b/lib/messages/search_entry.js index be40910..2d4af9f 100644 --- a/lib/messages/search_entry.js +++ b/lib/messages/search_entry.js @@ -98,7 +98,7 @@ SearchEntry.prototype._json = function(j) { j.objectName = this.objectName.toString(); j.attributes = []; this.attributes.forEach(function(a) { - j.attributes.push(a.json); + j.attributes.push(a.json || a); }); return j; diff --git a/lib/messages/search_request.js b/lib/messages/search_request.js index a392a58..3300378 100644 --- a/lib/messages/search_request.js +++ b/lib/messages/search_request.js @@ -103,7 +103,8 @@ SearchRequest.prototype._parse = function(ber) { this.filter = filters.parse(ber); // look for attributes - if (ber.readSequence() === (Ber.Sequence | Ber.Constructor)) { + if (ber.peek() === 0x30) { + ber.readSequence(); var end = ber.offset + ber.length; while (ber.offset < end) this.attributes.push(ber.readString().toLowerCase()); diff --git a/lib/server.js b/lib/server.js index b03d50d..af5dd2d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -274,7 +274,7 @@ function Server(options) { return function(err) { function sendError(err) { res.status = err.code || errors.LDAP_OPERATIONS_ERROR; - res.matchedDN = req.dn.toString(); + res.matchedDN = req.suffix.toString(); res.errorMessage = err.message || ''; return res.end(); }