From 24527b3630e618f177c79a9785d63706f8348fe7 Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Tue, 18 Oct 2011 13:49:48 -0700 Subject: [PATCH] ldapjs-compare --- bin/ldapjs-compare | 146 +++++++++++++++++++++++++++++++++++++++++++++ bin/ldapjs-modify | 2 +- 2 files changed, 147 insertions(+), 1 deletion(-) create mode 100755 bin/ldapjs-compare diff --git a/bin/ldapjs-compare b/bin/ldapjs-compare new file mode 100755 index 0000000..767f87a --- /dev/null +++ b/bin/ldapjs-compare @@ -0,0 +1,146 @@ +#!/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 = { + 'attribute': String, + 'debug': Number, + 'binddn': ldap.DN, + 'file': String, + 'password': String, + 'url': url, + 'value': String, +}; + +var shortOpts = { + 'a': ['--attribute'], + 'd': ['--debug'], + 'D': ['--binddn'], + 'f': ['--file'], + 'w': ['--password'], + 'u': ['--url'], + 'v': ['--value'] +}; + + + +///--- 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.attribute || typeof(parsed.value) !== 'string') + usage(1, 'attribute and value required'); + +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; + parsed.argv.remain.forEach(function(dn) { + client.compare(dn, parsed.attribute, parsed.value, function(err, match) { + if (err) + perror(err); + + process.stdout.write(match + '\n'); + + if (++finished === parsed.argv.remain.length) { + client.unbind(function() { + return; + }); + } + }); + }); +}); diff --git a/bin/ldapjs-modify b/bin/ldapjs-modify index 00aae3c..2e7ddea 100755 --- a/bin/ldapjs-modify +++ b/bin/ldapjs-modify @@ -143,7 +143,7 @@ client.bind(parsed.binddn, parsed.password, function(err, res) { modification: mod }); - function callback() { + function callback(err) { if (err) perror(err);