Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_response.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var assert = require('assert-plus');
  3. var util = require('util');
  4. var LDAPResult = require('./result');
  5. var SearchEntry = require('./search_entry');
  6. var SearchReference = require('./search_reference');
  7. var dtrace = require('../dtrace');
  8. var parseDN = require('../dn').parse;
  9. var parseURL = require('../url').parse;
  10. var Protocol = require('../protocol');
  11. ///--- API
  12. function SearchResponse(options) {
  13. options = options || {};
  14. assert.object(options);
  15. options.protocolOp = Protocol.LDAP_REP_SEARCH;
  16. LDAPResult.call(this, options);
  17. this.attributes = options.attributes ? options.attributes.slice() : [];
  18. this.notAttributes = [];
  19. this.sentEntries = 0;
  20. }
  21. util.inherits(SearchResponse, LDAPResult);
  22. /**
  23. * Allows you to send a SearchEntry back to the client.
  24. *
  25. * @param {Object} entry an instance of SearchEntry.
  26. * @param {Boolean} nofiltering skip filtering notAttributes and '_' attributes.
  27. * Defaults to 'false'.
  28. */
  29. SearchResponse.prototype.send = function (entry, nofiltering) {
  30. if (!entry || typeof (entry) !== 'object')
  31. throw new TypeError('entry (SearchEntry) required');
  32. if (nofiltering === undefined)
  33. nofiltering = false;
  34. if (typeof (nofiltering) !== 'boolean')
  35. throw new TypeError('noFiltering must be a boolean');
  36. var self = this;
  37. if (entry instanceof SearchEntry || entry instanceof SearchReference) {
  38. if (!entry.messageID)
  39. entry.messageID = this.messageID;
  40. if (entry.messageID !== this.messageID)
  41. throw new Error('SearchEntry messageID mismatch');
  42. } else {
  43. if (!entry.attributes)
  44. throw new Error('entry.attributes required');
  45. var savedAttrs = {};
  46. var all = (self.attributes.indexOf('*') !== -1);
  47. Object.keys(entry.attributes).forEach(function (a) {
  48. var _a = a.toLowerCase();
  49. if (!nofiltering && _a.length && _a[0] === '_') {
  50. savedAttrs[a] = entry.attributes[a];
  51. delete entry.attributes[a];
  52. } else if (!nofiltering && self.notAttributes.indexOf(_a) !== -1) {
  53. savedAttrs[a] = entry.attributes[a];
  54. delete entry.attributes[a];
  55. } else if (all) {
  56. return;
  57. } else if (self.attributes.length && self.attributes.indexOf(_a) === -1) {
  58. savedAttrs[a] = entry.attributes[a];
  59. delete entry.attributes[a];
  60. }
  61. });
  62. var save = entry;
  63. entry = new SearchEntry({
  64. objectName: typeof (save.dn) === 'string' ? parseDN(save.dn) : save.dn,
  65. messageID: self.messageID,
  66. log: self.log
  67. });
  68. entry.fromObject(save);
  69. }
  70. try {
  71. if (this.log.debug())
  72. this.log.debug('%s: sending: %j', this.connection.ldap.id, entry.json);
  73. this.connection.write(entry.toBer());
  74. this.sentEntries++;
  75. if (self._dtraceOp && self._dtraceId) {
  76. dtrace.fire('server-search-entry', function () {
  77. var c = self.connection || {ldap: {}};
  78. return [
  79. self._dtraceId || 0,
  80. (c.remoteAddress || ''),
  81. c.ldap.bindDN ? c.ldap.bindDN.toString() : '',
  82. (self.requestDN ? self.requestDN.toString() : ''),
  83. entry.objectName.toString(),
  84. entry.attributes.length
  85. ];
  86. });
  87. }
  88. // Restore attributes
  89. Object.keys(savedAttrs || {}).forEach(function (k) {
  90. save.attributes[k] = savedAttrs[k];
  91. });
  92. } catch (e) {
  93. this.log.warn(e, '%s failure to write message %j',
  94. this.connection.ldap.id, this.json);
  95. }
  96. };
  97. SearchResponse.prototype.createSearchEntry = function (object) {
  98. assert.object(object);
  99. var entry = new SearchEntry({
  100. messageID: this.messageID,
  101. log: this.log,
  102. objectName: object.objectName || object.dn
  103. });
  104. entry.fromObject((object.attributes || object));
  105. return entry;
  106. };
  107. SearchResponse.prototype.createSearchReference = function (uris) {
  108. if (!uris)
  109. throw new TypeError('uris ([string]) required');
  110. if (!Array.isArray(uris))
  111. uris = [uris];
  112. for (var i = 0; i < uris.length; i++) {
  113. if (typeof (uris[i]) == 'string')
  114. uris[i] = parseURL(uris[i]);
  115. }
  116. var self = this;
  117. return new SearchReference({
  118. messageID: self.messageID,
  119. log: self.log,
  120. uris: uris
  121. });
  122. };
  123. ///--- Exports
  124. module.exports = SearchResponse;