2011-10-18 20:25:56 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
// -*- mode: js -*-
|
|
|
|
// Copyright 2011 Mark Cavage. All rights reserved.
|
|
|
|
|
|
|
|
var path = require('path');
|
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
var dashdash = require('dashdash');
|
2011-10-18 20:25:56 +00:00
|
|
|
|
|
|
|
var ldap = require('../lib/index');
|
2013-04-24 18:45:19 +00:00
|
|
|
var Logger = require('bunyan');
|
2011-10-18 20:25:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
dashdash.addOptionType({
|
|
|
|
name: 'ldap.DN',
|
|
|
|
takesArg: true,
|
|
|
|
helpArg: 'LDAP_DN',
|
|
|
|
parseArg: function (option, optstr, arg) {
|
|
|
|
return ldap.parseDN(arg);
|
2011-10-18 20:25:56 +00:00
|
|
|
}
|
2014-06-18 22:56:13 +00:00
|
|
|
});
|
2011-10-18 20:25:56 +00:00
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
dashdash.addOptionType({
|
|
|
|
name: 'ldap.Filter',
|
|
|
|
takesArg: true,
|
|
|
|
helpArg: 'LDAP_FILTER',
|
|
|
|
parseArg: function (option, optstr, arg) {
|
|
|
|
return ldap.parseFilter(arg);
|
2011-10-18 20:25:56 +00:00
|
|
|
}
|
2014-06-18 22:56:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
dashdash.addOptionType({
|
|
|
|
name: 'ldap.scope',
|
|
|
|
takesArg: true,
|
|
|
|
helpArg: 'SCOPE',
|
|
|
|
parseArg: function (option, optstr, arg) {
|
|
|
|
if (!/^base|one|sub$/.test(arg)) {
|
|
|
|
throw new TypeError('Scope must be <base|one|sub>');
|
|
|
|
}
|
2014-07-10 14:58:25 +00:00
|
|
|
return arg;
|
2014-06-18 22:56:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var opts = [
|
|
|
|
{
|
|
|
|
names: ['base', 'b'],
|
|
|
|
type: 'ldap.DN',
|
|
|
|
help: 'Base DN of search',
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['scope', 's'],
|
|
|
|
type: 'ldap.scope',
|
|
|
|
help: 'Search scope <base|sub|one>',
|
|
|
|
helpArg: 'SCOPE',
|
|
|
|
default: 'sub'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['timeout', 't'],
|
|
|
|
type: 'integer',
|
|
|
|
help: 'Search timeout',
|
|
|
|
helpArg: 'SECS'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['persistent', 'p'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Enable persistent search control',
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['paged', 'g'],
|
|
|
|
type: 'number',
|
|
|
|
help: 'Enable paged search result control',
|
|
|
|
helpArg: 'PAGE_SIZE'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['control', 'c'],
|
|
|
|
type: 'arrayOfString',
|
|
|
|
help: 'Send addition control OID',
|
|
|
|
helpArg: 'OID',
|
|
|
|
default: []
|
|
|
|
},
|
2014-06-20 00:19:36 +00:00
|
|
|
{ group: 'General Options' },
|
2014-06-18 22:56:13 +00:00
|
|
|
{
|
2014-06-20 00:19:36 +00:00
|
|
|
names: ['help', 'h'],
|
2014-06-18 22:56:13 +00:00
|
|
|
type: 'bool',
|
2014-06-20 00:19:36 +00:00
|
|
|
help: 'Print this help and exit.'
|
2014-06-18 22:56:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['debug', 'd'],
|
|
|
|
type: 'integer',
|
|
|
|
help: 'Set debug level <0-2>',
|
|
|
|
helpArg: 'LEVEL'
|
|
|
|
},
|
2014-06-20 00:19:36 +00:00
|
|
|
{ group: 'Connection Options' },
|
2014-06-18 22:56:13 +00:00
|
|
|
{
|
2014-06-20 00:19:36 +00:00
|
|
|
names: ['url', 'u'],
|
|
|
|
type: 'string',
|
|
|
|
help: 'LDAP server URL',
|
|
|
|
helpArg: 'URL',
|
|
|
|
default: 'ldap://127.0.0.1:389'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['binddn', 'D'],
|
|
|
|
type: 'ldap.DN',
|
|
|
|
help: 'Bind DN',
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['password', 'w'],
|
|
|
|
type: 'string',
|
|
|
|
help: 'Bind password',
|
|
|
|
helpArg: 'PASSWD',
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['insecure', 'i'],
|
2014-06-18 22:56:13 +00:00
|
|
|
type: 'bool',
|
2014-06-20 00:19:36 +00:00
|
|
|
env: 'LDAPJS_TLS_INSECURE',
|
|
|
|
help: 'Disable SSL certificate verification',
|
|
|
|
default: false
|
2014-06-18 22:56:13 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
var parser = dashdash.createParser({options: opts});
|
2011-10-18 20:25:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///--- Helpers
|
|
|
|
|
|
|
|
function usage(code, message) {
|
|
|
|
var msg = (message ? message + '\n' : '') +
|
2014-06-20 00:19:36 +00:00
|
|
|
'Usage: ' + path.basename(process.argv[1]) +
|
|
|
|
' [OPTIONS] FILTER [ATTRIBUTES...]\n\n' +
|
|
|
|
parser.help({includeEnv: true});
|
2011-10-18 20:25:56 +00:00
|
|
|
|
|
|
|
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 parsed;
|
|
|
|
|
|
|
|
try {
|
2014-06-18 22:56:13 +00:00
|
|
|
parsed = parser.parse(process.argv);
|
2011-10-18 20:25:56 +00:00
|
|
|
} catch (e) {
|
|
|
|
usage(1, e.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed.help)
|
|
|
|
usage(0);
|
2014-06-18 22:56:13 +00:00
|
|
|
if (parsed._args.length < 1)
|
2011-10-18 20:25:56 +00:00
|
|
|
usage(1, 'filter required');
|
|
|
|
|
|
|
|
try {
|
2014-06-18 22:56:13 +00:00
|
|
|
ldap.parseFilter(parsed._args[0]);
|
2011-10-18 20:25:56 +00:00
|
|
|
} catch (e) {
|
|
|
|
usage(1, e.message);
|
|
|
|
}
|
|
|
|
|
2013-04-24 18:45:19 +00:00
|
|
|
var logLevel = 'info';
|
2011-10-18 20:25:56 +00:00
|
|
|
|
|
|
|
if (parsed.debug)
|
2013-04-24 18:45:19 +00:00
|
|
|
logLevel = (parsed.debug > 1 ? 'trace' : 'debug');
|
2011-10-18 20:25:56 +00:00
|
|
|
|
2013-04-24 18:45:19 +00:00
|
|
|
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,
|
2013-04-24 18:45:19 +00:00
|
|
|
log: log,
|
2014-04-23 00:46:14 +00:00
|
|
|
timeout: parsed.timeout || false,
|
|
|
|
tlsOptions: {
|
|
|
|
rejectUnauthorized: !parsed.insecure
|
|
|
|
}
|
2011-10-18 20:25:56 +00:00
|
|
|
});
|
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
client.on('error', function (err) {
|
2011-10-18 20:25:56 +00:00
|
|
|
perror(err);
|
|
|
|
});
|
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
client.on('timeout', function (req) {
|
2012-01-20 02:02:10 +00:00
|
|
|
process.stderr.write('Timeout reached\n');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
client.bind(parsed.binddn, parsed.password, function (err, res) {
|
2011-10-18 20:25:56 +00:00
|
|
|
if (err)
|
|
|
|
perror(err);
|
|
|
|
|
2011-10-18 23:26:38 +00:00
|
|
|
var controls = [];
|
2014-06-18 22:56:13 +00:00
|
|
|
parsed.control.forEach(function (c) {
|
2011-10-18 23:26:38 +00:00
|
|
|
controls.push(new ldap.Control({
|
|
|
|
type: c,
|
|
|
|
criticality: true
|
|
|
|
}));
|
|
|
|
});
|
2011-12-20 23:04:59 +00:00
|
|
|
if (parsed.persistent) {
|
|
|
|
var pCtrl = new ldap.PersistentSearchControl({
|
|
|
|
type: '2.16.840.1.113730.3.4.3',
|
|
|
|
value: {
|
|
|
|
changeTypes: 15,
|
|
|
|
changesOnly: false,
|
|
|
|
returnECs: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
controls.push(pCtrl);
|
|
|
|
}
|
2012-09-19 20:39:31 +00:00
|
|
|
if (parsed.paged) {
|
|
|
|
var ctrl = new ldap.PagedResultsControl({ value: { size: parsed.paged } });
|
|
|
|
controls.push(ctrl);
|
|
|
|
}
|
2011-10-18 20:25:56 +00:00
|
|
|
var req = {
|
|
|
|
scope: parsed.scope || 'sub',
|
2014-06-18 22:56:13 +00:00
|
|
|
filter: parsed._args[0],
|
|
|
|
attributes: parsed._args.length > 1 ? parsed._args.slice(1) : []
|
2011-10-18 20:25:56 +00:00
|
|
|
};
|
2014-06-18 22:56:13 +00:00
|
|
|
client.search(parsed.base, req, controls, function (err, res) {
|
2011-10-18 20:25:56 +00:00
|
|
|
if (err)
|
|
|
|
perror(err);
|
|
|
|
|
2014-06-18 22:56:13 +00:00
|
|
|
res.on('searchEntry', function (entry) {
|
2011-12-20 23:04:59 +00:00
|
|
|
process.stdout.write(JSON.stringify(entry.object, null, 2) + '\n');
|
2011-10-18 20:25:56 +00:00
|
|
|
});
|
2014-06-18 22:56:13 +00:00
|
|
|
res.on('error', function (err) {
|
2011-10-18 20:25:56 +00:00
|
|
|
perror(err);
|
|
|
|
});
|
2014-06-18 22:56:13 +00:00
|
|
|
res.on('end', function (res) {
|
2011-10-18 20:25:56 +00:00
|
|
|
if (res.status !== 0)
|
|
|
|
process.stderr.write(ldap.getMessage(res.status) + '\n');
|
2014-06-18 22:56:13 +00:00
|
|
|
client.unbind(function () {
|
2011-10-18 20:25:56 +00:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|