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.

parser.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var EventEmitter = require('events').EventEmitter;
  3. var util = require('util');
  4. var assert = require('assert-plus');
  5. var asn1 = require('asn1');
  6. var VError = require('verror').VError;
  7. var AbandonRequest = require('./abandon_request');
  8. var AddRequest = require('./add_request');
  9. var AddResponse = require('./add_response');
  10. var BindRequest = require('./bind_request');
  11. var BindResponse = require('./bind_response');
  12. var CompareRequest = require('./compare_request');
  13. var CompareResponse = require('./compare_response');
  14. var DeleteRequest = require('./del_request');
  15. var DeleteResponse = require('./del_response');
  16. var ExtendedRequest = require('./ext_request');
  17. var ExtendedResponse = require('./ext_response');
  18. var ModifyRequest = require('./modify_request');
  19. var ModifyResponse = require('./modify_response');
  20. var ModifyDNRequest = require('./moddn_request');
  21. var ModifyDNResponse = require('./moddn_response');
  22. var SearchRequest = require('./search_request');
  23. var SearchEntry = require('./search_entry');
  24. var SearchReference = require('./search_reference');
  25. var SearchResponse = require('./search_response');
  26. var UnbindRequest = require('./unbind_request');
  27. var UnbindResponse = require('./unbind_response');
  28. var LDAPResult = require('./result');
  29. var Message = require('./message');
  30. var Protocol = require('../protocol');
  31. ///--- Globals
  32. var Ber = asn1.Ber;
  33. var BerReader = asn1.BerReader;
  34. ///--- API
  35. function Parser(options) {
  36. assert.object(options);
  37. assert.object(options.log);
  38. EventEmitter.call(this);
  39. this.buffer = null;
  40. this.log = options.log;
  41. }
  42. util.inherits(Parser, EventEmitter);
  43. Parser.prototype.write = function (data) {
  44. if (!data || !Buffer.isBuffer(data))
  45. throw new TypeError('data (buffer) required');
  46. var nextMessage = null;
  47. var self = this;
  48. function end() {
  49. if (nextMessage)
  50. return self.write(nextMessage);
  51. return true;
  52. }
  53. self.buffer = (self.buffer ? Buffer.concat([self.buffer, data]) : data);
  54. var ber = new BerReader(self.buffer);
  55. var foundSeq = false;
  56. try {
  57. foundSeq = ber.readSequence();
  58. } catch (e) {
  59. this.emit('error', e);
  60. }
  61. if (!foundSeq || ber.remain < ber.length) {
  62. // ENOTENOUGH
  63. return false;
  64. } else if (ber.remain > ber.length) {
  65. // ETOOMUCH
  66. // This is sort of ugly, but allows us to make miminal copies
  67. nextMessage = self.buffer.slice(ber.offset + ber.length);
  68. ber._size = ber.offset + ber.length;
  69. assert.equal(ber.remain, ber.length);
  70. }
  71. // If we're here, ber holds the message, and nextMessage is temporarily
  72. // pointing at the next sequence of data (if it exists)
  73. self.buffer = null;
  74. var message;
  75. try {
  76. // Bail here if peer isn't speaking protocol at all
  77. message = this.getMessage(ber);
  78. if (!message) {
  79. return end();
  80. }
  81. message.parse(ber);
  82. } catch (e) {
  83. this.emit('error', e, message);
  84. return false;
  85. }
  86. this.emit('message', message);
  87. return end();
  88. };
  89. Parser.prototype.getMessage = function (ber) {
  90. assert.ok(ber);
  91. var self = this;
  92. var messageID = ber.readInt();
  93. var type = ber.readSequence();
  94. var Message;
  95. switch (type) {
  96. case Protocol.LDAP_REQ_ABANDON:
  97. Message = AbandonRequest;
  98. break;
  99. case Protocol.LDAP_REQ_ADD:
  100. Message = AddRequest;
  101. break;
  102. case Protocol.LDAP_REP_ADD:
  103. Message = AddResponse;
  104. break;
  105. case Protocol.LDAP_REQ_BIND:
  106. Message = BindRequest;
  107. break;
  108. case Protocol.LDAP_REP_BIND:
  109. Message = BindResponse;
  110. break;
  111. case Protocol.LDAP_REQ_COMPARE:
  112. Message = CompareRequest;
  113. break;
  114. case Protocol.LDAP_REP_COMPARE:
  115. Message = CompareResponse;
  116. break;
  117. case Protocol.LDAP_REQ_DELETE:
  118. Message = DeleteRequest;
  119. break;
  120. case Protocol.LDAP_REP_DELETE:
  121. Message = DeleteResponse;
  122. break;
  123. case Protocol.LDAP_REQ_EXTENSION:
  124. Message = ExtendedRequest;
  125. break;
  126. case Protocol.LDAP_REP_EXTENSION:
  127. Message = ExtendedResponse;
  128. break;
  129. case Protocol.LDAP_REQ_MODIFY:
  130. Message = ModifyRequest;
  131. break;
  132. case Protocol.LDAP_REP_MODIFY:
  133. Message = ModifyResponse;
  134. break;
  135. case Protocol.LDAP_REQ_MODRDN:
  136. Message = ModifyDNRequest;
  137. break;
  138. case Protocol.LDAP_REP_MODRDN:
  139. Message = ModifyDNResponse;
  140. break;
  141. case Protocol.LDAP_REQ_SEARCH:
  142. Message = SearchRequest;
  143. break;
  144. case Protocol.LDAP_REP_SEARCH_ENTRY:
  145. Message = SearchEntry;
  146. break;
  147. case Protocol.LDAP_REP_SEARCH_REF:
  148. Message = SearchReference;
  149. break;
  150. case Protocol.LDAP_REP_SEARCH:
  151. Message = SearchResponse;
  152. break;
  153. case Protocol.LDAP_REQ_UNBIND:
  154. Message = UnbindRequest;
  155. break;
  156. default:
  157. this.emit('error',
  158. new Error('Op 0x' + (type ? type.toString(16) : '??') +
  159. ' not supported'),
  160. new LDAPResult({
  161. messageID: messageID,
  162. protocolOp: type || Protocol.LDAP_REP_EXTENSION
  163. }));
  164. return false;
  165. }
  166. return new Message({
  167. messageID: messageID,
  168. log: self.log
  169. });
  170. };
  171. ///--- Exports
  172. module.exports = Parser;