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-add 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 vasync = require('vasync');
  8. var ldap = require('../lib/index');
  9. var Logger = require('bunyan');
  10. ///--- Globals
  11. var opts = [
  12. {
  13. names: ['file', 'f'],
  14. type: 'string',
  15. help: 'Input file',
  16. helpArg: 'FILE'
  17. },
  18. { group: 'General Options' },
  19. {
  20. names: ['help', 'h'],
  21. type: 'bool',
  22. help: 'Print this help and exit.'
  23. },
  24. {
  25. names: ['debug', 'd'],
  26. type: 'integer',
  27. help: 'Set debug level <0-2>',
  28. helpArg: 'LEVEL'
  29. },
  30. { group: 'Connection Options' },
  31. {
  32. names: ['url', 'u'],
  33. type: 'string',
  34. help: 'LDAP server URL',
  35. helpArg: 'URL',
  36. default: 'ldap://127.0.0.1:389'
  37. },
  38. {
  39. names: ['binddn', 'D'],
  40. type: 'string',
  41. help: 'Bind DN',
  42. helpArg: 'BIND_DN',
  43. default: ''
  44. },
  45. {
  46. names: ['password', 'w'],
  47. type: 'string',
  48. help: 'Bind password',
  49. helpArg: 'PASSWD',
  50. default: ''
  51. },
  52. {
  53. names: ['insecure', 'i'],
  54. type: 'bool',
  55. env: 'LDAPJS_TLS_INSECURE',
  56. help: 'Disable SSL certificate verification',
  57. default: false
  58. }
  59. ];
  60. var parser = dashdash.createParser({options: opts});
  61. ///--- Helpers
  62. function usage(code, message) {
  63. var msg = (message ? message + '\n' : '') +
  64. 'Usage: ' + path.basename(process.argv[1]) + ' [OPTIONS] [JSON]\n\n' +
  65. parser.help({includeEnv: true});
  66. process.stderr.write(msg + '\n');
  67. process.exit(code);
  68. }
  69. function perror(err) {
  70. if (parsed.debug) {
  71. process.stderr.write(err.stack + '\n');
  72. } else {
  73. process.stderr.write(err.message + '\n');
  74. }
  75. process.exit(1);
  76. }
  77. ///--- Mainline
  78. var logLevel = 'info';
  79. var parsed;
  80. try {
  81. parsed = parser.parse(process.argv);
  82. if (parsed.file) {
  83. parsed.file = JSON.parse(fs.readFileSync(parsed.file, 'utf8'));
  84. if (!Array.isArray(parsed.file))
  85. parsed.file = [parsed.file];
  86. }
  87. } catch (e) {
  88. usage(1, e.toString());
  89. }
  90. if (parsed.help)
  91. usage(0);
  92. if (!parsed.file) {
  93. parsed.file = [];
  94. parsed._args.forEach(function (a) {
  95. var o = JSON.parse(a);
  96. if (Array.isArray(o)) {
  97. o.forEach(function (i) {
  98. parsed.file.push(o);
  99. });
  100. return;
  101. }
  102. parsed.file.push(o);
  103. });
  104. if (parsed.file.length === 0)
  105. parsed.file = null;
  106. }
  107. if (!parsed.file)
  108. usage(1, 'either -f or arguments must be used for adding objects');
  109. if (parsed.debug)
  110. logLevel = (parsed.debug > 1 ? 'trace' : 'debug');
  111. var log = new Logger({
  112. name: 'ldapjs',
  113. component: 'client',
  114. stream: process.stderr,
  115. level: logLevel
  116. });
  117. var client = ldap.createClient({
  118. url: parsed.url,
  119. log: log,
  120. strictDN: false,
  121. tlsOptions: {
  122. rejectUnauthorized: !parsed.insecure
  123. }
  124. });
  125. client.on('error', function (err) {
  126. perror(err);
  127. });
  128. client.bind(parsed.binddn, parsed.password, function (err, res) {
  129. if (err) {
  130. perror(err);
  131. }
  132. vasync.forEachPipeline({
  133. inputs: parsed.file,
  134. func: function (entry, cb) {
  135. var dn = entry.dn;
  136. delete entry.dn;
  137. client.add(dn, entry, cb);
  138. }
  139. }, function (err2, res2) {
  140. if (err2) {
  141. perror(err2);
  142. }
  143. client.unbind(function () { return; });
  144. });
  145. });