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.

ldapjs-compare 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env node
  2. // -*- mode: js -*-
  3. // Copyright 2011 Mark Cavage. All rights reserved.
  4. var fs = require('fs');
  5. var path = require('path');
  6. var dashdash = require('dashdash');
  7. var ldap = require('../lib/index');
  8. var Logger = require('bunyan');
  9. ///--- Globals
  10. var opts = [
  11. {
  12. names: ['attribute', 'a'],
  13. type: 'string',
  14. help: 'Comparison attribute',
  15. helpArg: 'ATTR'
  16. },
  17. {
  18. names: ['value', 'v'],
  19. type: 'string',
  20. help: 'Comparison value',
  21. helpArg: 'VAL'
  22. },
  23. { group: 'General Options' },
  24. {
  25. names: ['help', 'h'],
  26. type: 'bool',
  27. help: 'Print this help and exit.'
  28. },
  29. {
  30. names: ['debug', 'd'],
  31. type: 'integer',
  32. help: 'Set debug level <0-2>',
  33. helpArg: 'LEVEL'
  34. },
  35. { group: 'Connection Options' },
  36. {
  37. names: ['url', 'u'],
  38. type: 'string',
  39. help: 'LDAP server URL',
  40. helpArg: 'URL',
  41. default: 'ldap://127.0.0.1:389'
  42. },
  43. {
  44. names: ['binddn', 'D'],
  45. type: 'string',
  46. help: 'Bind DN',
  47. helpArg: 'BIND_DN',
  48. default: ''
  49. },
  50. {
  51. names: ['password', 'w'],
  52. type: 'string',
  53. help: 'Bind password',
  54. helpArg: 'PASSWD',
  55. default: ''
  56. },
  57. {
  58. names: ['insecure', 'i'],
  59. type: 'bool',
  60. env: 'LDAPJS_TLS_INSECURE',
  61. help: 'Disable SSL certificate verification',
  62. default: false
  63. }
  64. ];
  65. var parser = dashdash.createParser({options: opts});
  66. ///--- Helpers
  67. function usage(code, message) {
  68. var msg = (message ? message + '\n' : '') +
  69. 'Usage: ' + path.basename(process.argv[1]) + ' [OPTIONS] DN\n\n' +
  70. parser.help({includeEnv: true});
  71. process.stderr.write(msg + '\n');
  72. process.exit(code);
  73. }
  74. function perror(err) {
  75. if (parsed.debug) {
  76. process.stderr.write(err.stack + '\n');
  77. } else {
  78. process.stderr.write(err.message + '\n');
  79. }
  80. process.exit(1);
  81. }
  82. ///--- Mainline
  83. var logLevel = 'info';
  84. var parsed;
  85. try {
  86. parsed = parser.parse(process.argv);
  87. } catch (e) {
  88. usage(1, e.toString());
  89. }
  90. if (parsed.help)
  91. usage(0);
  92. if (parsed._args.length < 1)
  93. usage(1, 'DN required');
  94. try {
  95. parsed._args.forEach(function (dn) {
  96. ldap.parseDN(dn);
  97. });
  98. } catch (e) {
  99. usage(1, e.toString());
  100. }
  101. if (!parsed.attribute || typeof (parsed.value) !== 'string')
  102. usage(1, 'attribute and value required');
  103. if (parsed.debug)
  104. logLevel = (parsed.debug > 1 ? 'trace' : 'debug');
  105. var log = new Logger({
  106. name: 'ldapjs',
  107. component: 'client',
  108. stream: process.stderr,
  109. level: logLevel
  110. });
  111. var client = ldap.createClient({
  112. url: parsed.url,
  113. log: log,
  114. strictDN: false,
  115. tlsOptions: {
  116. rejectUnauthorized: !parsed.insecure
  117. }
  118. });
  119. client.on('error', function (err) {
  120. perror(err);
  121. });
  122. client.bind(parsed.binddn, parsed.password, function (err, res) {
  123. if (err)
  124. perror(err);
  125. var finished = 0;
  126. parsed._args.forEach(function (dn) {
  127. client.compare(dn, parsed.attribute, parsed.value, function (err, match) {
  128. if (err)
  129. perror(err);
  130. process.stdout.write(match + '\n');
  131. if (++finished === parsed._args.length) {
  132. client.unbind(function () {
  133. return;
  134. });
  135. }
  136. });
  137. });
  138. });