diff --git a/bin/ldapjs-add b/bin/ldapjs-add new file mode 100755 index 0000000..82e7eb5 --- /dev/null +++ b/bin/ldapjs-add @@ -0,0 +1,149 @@ +#!/usr/bin/env node +// -*- mode: js -*- +// Copyright 2011 Mark Cavage. All rights reserved. + +var fs = require('fs'); +var path = require('path'); +var url = require('url'); + +var log4js = require('log4js'); +var nopt = require('nopt'); + +var ldap = require('../lib/index'); + + + +///--- Globals + +nopt.typeDefs.DN = { + type: ldap.DN, + validate: function(data, k, val) { + data[k] = ldap.parseDN(val); + } +}; + +var opts = { + 'debug': Number, + 'binddn': ldap.DN, + 'file': String, + 'password': String, + 'url': url +}; + +var shortOpts = { + 'd': ['--debug'], + 'D': ['--binddn'], + 'f': ['--file'], + 'w': ['--password'], + 'u': ['--url'] +}; + + + +///--- Helpers + +function usage(code, message) { + var _opts = ''; + Object.keys(shortOpts).forEach(function(k) { + if (!Array.isArray(shortOpts[k])) + return; + var longOpt = shortOpts[k][0].replace('--', ''); + var type = opts[longOpt].name || 'string'; + if (type && type === 'boolean') type = ''; + type = type.toLowerCase(); + + _opts += ' [--' + longOpt + ' ' + type + ']'; + }); + _opts += ' [JSON]'; + + var msg = (message ? message + '\n' : '') + + 'usage: ' + path.basename(process.argv[1]) + _opts; + + process.stderr.write(msg + '\n'); + process.exit(code); +} + + +function perror(err) { + if (parsed.debug) { + process.stderr.write(err.stack + '\n'); + } else { + process.stderr.write(err.message + '\n'); + } + process.exit(1); +} + + + +///--- Mainline + +log4js.setGlobalLogLevel('INFO'); +var parsed; + +try { + parsed = nopt(opts, shortOpts, process.argv, 2); + if (parsed.file) { + parsed.file = JSON.parse(fs.readFileSync(parsed.file, 'utf8')); + if (!Array.isArray(parsed.file)) + parsed.file = [parsed.file]; + } +} catch (e) { + usage(1, e.toString()); +} + +if (parsed.help) + usage(0); +if (!parsed.file) { + parsed.file = []; + parsed.argv.remain.forEach(function(a) { + var o =JSON.parse(a); + if (Array.isArray(o)) { + o.forEach(function(i) { + parsed.file.push(o); + }); + return; + } + parsed.file.push(o); + }); +} + +if (!parsed.file) + usage(1, 'either -f or stdin must be used for adding objects'); + +if (parsed.debug) + log4js.setGlobalLogLevel(parsed.debug > 1 ? 'TRACE' : 'DEBUG'); +if (!parsed.url) + parsed.url = 'ldap://127.0.0.1:389'; +if (!parsed.binddn) + parsed.binddn = ''; +if (!parsed.password) + parsed.password = ''; + +var client = ldap.createClient({ + url: parsed.url, + log4js: log4js +}); + +client.on('error', function(err) { + perror(err); +}); + +client.bind(parsed.binddn, parsed.password, function(err, res) { + if (err) + perror(err); + + var finished = 0; + function callback(err) { + if (err) + perror(err); + + if (++finished === parsed.file.length) + client.unbind(function () { return; }); + } + + parsed.file.forEach(function(entry) { + var dn = entry.dn; + delete entry.dn; + client.add(dn, entry, callback); + }); +}); diff --git a/bin/ldapjs-delete b/bin/ldapjs-delete new file mode 100755 index 0000000..2ea187e --- /dev/null +++ b/bin/ldapjs-delete @@ -0,0 +1,133 @@ +#!/usr/bin/env node +// -*- mode: js -*- +// Copyright 2011 Mark Cavage. All rights reserved. + +var fs = require('fs'); +var path = require('path'); +var url = require('url'); + +var log4js = require('log4js'); +var nopt = require('nopt'); + +var ldap = require('../lib/index'); + + + +///--- Globals + +nopt.typeDefs.DN = { + type: ldap.DN, + validate: function(data, k, val) { + data[k] = ldap.parseDN(val); + } +}; + +var opts = { + 'debug': Number, + 'binddn': ldap.DN, + 'password': String, + 'url': url +}; + +var shortOpts = { + 'd': ['--debug'], + 'D': ['--binddn'], + 'w': ['--password'], + 'u': ['--url'] +}; + + + +///--- Helpers + +function usage(code, message) { + var _opts = ''; + Object.keys(shortOpts).forEach(function(k) { + if (!Array.isArray(shortOpts[k])) + return; + var longOpt = shortOpts[k][0].replace('--', ''); + var type = opts[longOpt].name || 'string'; + if (type && type === 'boolean') type = ''; + type = type.toLowerCase(); + + _opts += ' [--' + longOpt + ' ' + type + ']'; + }); + _opts += ' DN'; + + var msg = (message ? message + '\n' : '') + + 'usage: ' + path.basename(process.argv[1]) + _opts; + + process.stderr.write(msg + '\n'); + process.exit(code); +} + + +function perror(err) { + if (parsed.debug) { + process.stderr.write(err.stack + '\n'); + } else { + process.stderr.write(err.message + '\n'); + } + process.exit(1); +} + + + +///--- Mainline + +log4js.setGlobalLogLevel('INFO'); +var parsed; + +try { + parsed = nopt(opts, shortOpts, process.argv, 2); +} catch (e) { + usage(1, e.toString()); +} + +if (parsed.help) + usage(0); +if (parsed.argv.remain.length < 1) + usage(1, 'DN required'); +try { + parsed.argv.remain.forEach(function(dn) { + ldap.parseDN(dn); + }); +} catch (e) { + usage(1, e.toString()); +} + +if (parsed.debug) + log4js.setGlobalLogLevel(parsed.debug > 1 ? 'TRACE' : 'DEBUG'); +if (!parsed.url) + parsed.url = 'ldap://127.0.0.1:389'; +if (!parsed.binddn) + parsed.binddn = ''; +if (!parsed.password) + parsed.password = ''; + +var client = ldap.createClient({ + url: parsed.url, + log4js: log4js +}); + +client.on('error', function(err) { + perror(err); +}); + +client.bind(parsed.binddn, parsed.password, function(err, res) { + if (err) + perror(err); + + var finished = 0; + function callback(err) { + if (err) + perror(err); + + if (++finished === parsed.argv.remain.length) + client.unbind(function () { return; }); + } + + parsed.argv.remain.forEach(function(dn) { + client.del(dn, callback); + }); +}); diff --git a/bin/ldapjs-search b/bin/ldapjs-search new file mode 100755 index 0000000..2feac0e --- /dev/null +++ b/bin/ldapjs-search @@ -0,0 +1,159 @@ +#!/usr/bin/env node +// -*- mode: js -*- +// Copyright 2011 Mark Cavage. All rights reserved. + +var path = require('path'); +var url = require('url'); + +var log4js = require('log4js'); +var nopt = require('nopt'); + +var ldap = require('../lib/index'); + + + +///--- Globals + +nopt.typeDefs.DN = { + type: ldap.DN, + validate: function(data, k, val) { + data[k] = ldap.parseDN(val); + } +}; + +nopt.typeDefs.Filter = { + type: ldap.Filter, + validate: function(data, k, val) { + data[k] = ldap.parseFilter(val); + } +}; + + +var opts = { + 'debug': Number, + 'base': ldap.DN, + 'binddn': ldap.DN, + 'password': String, + 'scope': String, + 'url': url +}; + +var shortOpts = { + 'd': ['--debug'], + 'b': ['--base'], + 'D': ['--binddn'], + 'w': ['--password'], + 's': ['--scope'], + 'u': ['--url'] +}; + + + +///--- Helpers + +function usage(code, message) { + var _opts = ''; + Object.keys(shortOpts).forEach(function(k) { + if (!Array.isArray(shortOpts[k])) + return; + var longOpt = shortOpts[k][0].replace('--', ''); + var type = opts[longOpt].name || 'string'; + if (type && type === 'boolean') type = ''; + type = type.toLowerCase(); + + _opts += ' [--' + longOpt + ' ' + type + ']'; + }); + _opts += ' filter [attributes...]'; + + var msg = (message ? message + '\n' : '') + + 'usage: ' + path.basename(process.argv[1]) + _opts; + + process.stderr.write(msg + '\n'); + process.exit(code); +} + + +function perror(err) { + if (parsed.debug) { + process.stderr.write(err.stack + '\n'); + } else { + process.stderr.write(err.message + '\n'); + } + process.exit(1); +} + + + +///--- Mainline + +log4js.setGlobalLogLevel('INFO'); +var parsed; + +try { + parsed = nopt(opts, shortOpts, process.argv, 2); +} catch (e) { + usage(1, e.toString()); +} + +if (parsed.help) + usage(0); +if (parsed.argv.remain.length < 1) + usage(1, 'filter required'); + +try { + ldap.parseFilter(parsed.argv.remain[0]); +} catch (e) { + usage(1, e.message); +} + + +if (parsed.debug) + log4js.setGlobalLogLevel(parsed.debug > 1 ? 'TRACE' : 'DEBUG'); +if (!parsed.url) + parsed.url = 'ldap://127.0.0.1:389'; +if (!parsed.binddn) + parsed.binddn = ''; +if (!parsed.password) + parsed.password = ''; +if (!parsed.base) + parsed.base = ''; + +var client = ldap.createClient({ + url: parsed.url, + log4js: log4js +}); + +client.on('error', function(err) { + perror(err); +}); + +client.bind(parsed.binddn, parsed.password, function(err, res) { + if (err) + perror(err); + + var req = { + scope: parsed.scope || 'sub', + filter: parsed.argv.remain[0], + attributes: parsed.argv.remain.length > 1 ? parsed.argv.remain.slice(1) : [] + }; + client.search(parsed.base, req, function(err, res) { + if (err) + perror(err); + + var entries = []; + res.on('searchEntry', function(entry) { + entries.push(entry.object); + }); + res.on('error', function(err) { + perror(err); + }); + res.on('end', function(res) { + process.stdout.write(JSON.stringify(entries, null, 2) + '\n'); + if (res.status !== 0) + process.stderr.write(ldap.getMessage(res.status) + '\n'); + client.unbind(function() { + return; + }); + }); + }); +});