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-delete 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. { group: 'General Options' },
  12. {
  13. names: ['help', 'h'],
  14. type: 'bool',
  15. help: 'Print this help and exit.'
  16. },
  17. {
  18. names: ['debug', 'd'],
  19. type: 'integer',
  20. help: 'Set debug level <0-2>',
  21. helpArg: 'LEVEL'
  22. },
  23. { group: 'Connection Options' },
  24. {
  25. names: ['url', 'u'],
  26. type: 'string',
  27. help: 'LDAP server URL',
  28. helpArg: 'URL',
  29. default: 'ldap://127.0.0.1:389'
  30. },
  31. {
  32. names: ['binddn', 'D'],
  33. type: 'string',
  34. help: 'Bind DN',
  35. helpArg: 'bind_DN',
  36. default: ''
  37. },
  38. {
  39. names: ['password', 'w'],
  40. type: 'string',
  41. help: 'Bind password',
  42. helpArg: 'PASSWD',
  43. default: ''
  44. },
  45. {
  46. names: ['insecure', 'i'],
  47. type: 'bool',
  48. env: 'LDAPJS_TLS_INSECURE',
  49. help: 'Disable SSL certificate verification',
  50. default: false
  51. }
  52. ];
  53. var parser = dashdash.createParser({options: opts});
  54. ///--- Helpers
  55. function usage(code, message) {
  56. var msg = (message ? message + '\n' : '') +
  57. 'Usage: ' + path.basename(process.argv[1]) + ' [OPTIONS] DN\n\n' +
  58. parser.help({includeEnv: true});
  59. process.stderr.write(msg + '\n');
  60. process.exit(code);
  61. }
  62. function perror(err) {
  63. if (parsed.debug) {
  64. process.stderr.write(err.stack + '\n');
  65. } else {
  66. process.stderr.write(err.message + '\n');
  67. }
  68. process.exit(1);
  69. }
  70. ///--- Mainline
  71. var logLevel = 'info';
  72. var parsed;
  73. try {
  74. parsed = parser.parse(process.argv);
  75. } catch (e) {
  76. usage(1, e.toString());
  77. }
  78. if (parsed.help)
  79. usage(0);
  80. if (parsed._args.length < 1)
  81. usage(1, 'DN required');
  82. try {
  83. parsed._args.forEach(function (dn) {
  84. ldap.parseDN(dn);
  85. });
  86. } catch (e) {
  87. usage(1, e.toString());
  88. }
  89. if (parsed.debug)
  90. logLevel = (parsed.debug > 1 ? 'trace' : 'debug');
  91. var log = new Logger({
  92. name: 'ldapjs',
  93. component: 'client',
  94. stream: process.stderr,
  95. level: logLevel
  96. });
  97. var client = ldap.createClient({
  98. url: parsed.url,
  99. log: log,
  100. strictDN: false,
  101. tlsOptions: {
  102. rejectUnauthorized: !parsed.insecure
  103. }
  104. });
  105. client.on('error', function (err) {
  106. perror(err);
  107. });
  108. client.bind(parsed.binddn, parsed.password, function (err, res) {
  109. if (err)
  110. perror(err);
  111. var finished = 0;
  112. function callback(err) {
  113. if (err)
  114. perror(err);
  115. if (++finished === parsed._args.length)
  116. client.unbind(function () { return; });
  117. }
  118. parsed._args.forEach(function (dn) {
  119. client.del(dn, callback);
  120. });
  121. });