node-ldapjs/bin/ldapjs-add

170 lines
3.2 KiB
Plaintext
Raw Normal View History

2011-10-18 20:25:56 +00:00
#!/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 nopt = require('nopt');
var ldap = require('../lib/index');
var Logger = require('bunyan');
2011-10-18 20:25:56 +00:00
///--- 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,
'insecure': Boolean,
2011-10-18 20:25:56 +00:00
'password': String,
'url': url
};
var shortOpts = {
'd': ['--debug'],
'D': ['--binddn'],
'f': ['--file'],
'i': ['--insecure'],
2011-10-18 20:25:56 +00:00
'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
var logLevel = 'info';
2011-10-18 20:25:56 +00:00
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.insecure === undefined &&
process.env.LDAPJS_TLS_INSECURE !== undefined) {
if (process.env.LDAPJS_TLS_INSECURE === '0') {
parsed.insecure = false;
} else {
parsed.insecure = true;
}
}
2011-10-18 20:25:56 +00:00
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)
logLevel = (parsed.debug > 1 ? 'trace' : 'debug');
2011-10-18 20:25:56 +00:00
if (!parsed.url)
parsed.url = 'ldap://127.0.0.1:389';
if (!parsed.binddn)
parsed.binddn = '';
if (!parsed.password)
parsed.password = '';
var log = new Logger({
name: 'ldapjs',
component: 'client',
stream: process.stderr,
level: logLevel
});
2011-10-18 20:25:56 +00:00
var client = ldap.createClient({
url: parsed.url,
log: log,
tlsOptions: {
rejectUnauthorized: !parsed.insecure
}
2011-10-18 20:25:56 +00:00
});
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);
});
});