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-modify 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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: 'Attribute to modify',
  15. helpArg: 'ATTR'
  16. },
  17. {
  18. names: ['value', 'v'],
  19. type: 'arrayOfString',
  20. help: 'Desired value',
  21. helpArg: 'VAL'
  22. },
  23. {
  24. names: ['type', 't'],
  25. type: 'string',
  26. help: 'Attribute type',
  27. helpArg: 'TYPE'
  28. },
  29. { group: 'General options' },
  30. {
  31. names: ['help', 'h'],
  32. type: 'bool',
  33. help: 'Print this help and exit.'
  34. },
  35. {
  36. names: ['debug', 'd'],
  37. type: 'integer',
  38. help: 'Set debug level <0-2>',
  39. helpArg: 'LEVEL'
  40. },
  41. { group: 'Connection Options' },
  42. {
  43. names: ['url', 'u'],
  44. type: 'string',
  45. help: 'LDAP server URL',
  46. helpArg: 'URL',
  47. default: 'ldap://127.0.0.1:389'
  48. },
  49. {
  50. names: ['binddn', 'D'],
  51. type: 'string',
  52. help: 'Bind DN',
  53. helpArg: 'BIND_DN',
  54. default: ''
  55. },
  56. {
  57. names: ['password', 'w'],
  58. type: 'string',
  59. help: 'Bind password',
  60. helpArg: 'PASSWD',
  61. default: ''
  62. },
  63. {
  64. names: ['insecure', 'i'],
  65. type: 'bool',
  66. env: 'LDAPJS_TLS_INSECURE',
  67. help: 'Disable SSL certificate verification',
  68. default: false
  69. }
  70. ];
  71. var parser = dashdash.createParser({options: opts});
  72. ///--- Helpers
  73. function usage(code, message) {
  74. var msg = (message ? message + '\n' : '') +
  75. 'Usage: ' + path.basename(process.argv[1]) + ' [OPTIONS] DN\n\n' +
  76. parser.help({includeEnv: true});
  77. process.stderr.write(msg + '\n');
  78. process.exit(code);
  79. }
  80. function perror(err) {
  81. if (parsed.debug) {
  82. process.stderr.write(err.stack + '\n');
  83. } else {
  84. process.stderr.write(err.message + '\n');
  85. }
  86. process.exit(1);
  87. }
  88. ///--- Mainline
  89. var logLevel = 'info';
  90. var parsed;
  91. try {
  92. parsed = parser.parse(process.argv);
  93. } catch (e) {
  94. usage(1, e.toString());
  95. }
  96. if (parsed.help)
  97. usage(0);
  98. if (parsed._args.length < 1)
  99. usage(1, 'DN required');
  100. try {
  101. parsed._args.forEach(function (dn) {
  102. ldap.parseDN(dn);
  103. });
  104. } catch (e) {
  105. usage(1, e.toString());
  106. }
  107. if (!parsed.type)
  108. parsed.type = 'replace';
  109. if (!parsed.attribute || !Array.isArray(parsed.value))
  110. usage(1, 'attribute and value required');
  111. if (parsed.debug)
  112. logLevel = (parsed.debug > 1 ? 'trace' : 'debug');
  113. var log = new Logger({
  114. name: 'ldapjs',
  115. component: 'client',
  116. stream: process.stderr,
  117. level: logLevel
  118. });
  119. var client = ldap.createClient({
  120. url: parsed.url,
  121. log: log,
  122. strictDN: false,
  123. tlsOptions: {
  124. rejectUnauthorized: !parsed.insecure
  125. }
  126. });
  127. client.on('error', function (err) {
  128. perror(err);
  129. });
  130. client.bind(parsed.binddn, parsed.password, function (err, res) {
  131. if (err)
  132. perror(err);
  133. var finished = 0;
  134. var mod = {};
  135. mod[parsed.attribute] = [];
  136. parsed.value.forEach(function (v) {
  137. mod[parsed.attribute].push(v);
  138. });
  139. var change = new ldap.Change({
  140. type: parsed.type,
  141. modification: mod
  142. });
  143. function callback(err) {
  144. if (err)
  145. perror(err);
  146. if (++finished === parsed._args.length) {
  147. client.unbind(function () {
  148. return;
  149. });
  150. }
  151. }
  152. parsed._args.forEach(function (dn) {
  153. client.modify(dn, change, callback);
  154. });
  155. });