#!/usr/bin/perl -w
#
# Module for TWiki Enterprise Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2007-2021 Peter Thoeny & TWiki Contributors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version. For
# more details read LICENSE in the root of this distribution.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

use Net::LDAP;

# usage: ldaptest 'ldap-query'

## PLEASE MODIFIY #############################################

# server name or IP address
my $server = 'localhost';

# dn from where searches are performed recursively
my $baseDN = 'dc=nodomain';

# username and credentials used to connect to the LDAP server;
# if unset an anonymous connect will be used
# CAUTION: when typing in a password, this is plain text. 
# please make sure that this file is only readable for authorized personell
my $bindUser = ''; 
my $bindPassword = '';

# maximum number of records returned
my $sizeLimit = 10;

###############################################################

my $filter = $ARGV[0] || '(objectClass=*)';

my $ldap = Net::LDAP->new($server) || die "$@";

my $mesg;
if ($bindUser) {
  $mesg = $ldap->bind($bindUser, password=>$bindPassword);
} else {
  $mesg = $ldap->bind();
}
my $code = $mesg->code;
$code && die "Bind error, code $code, message: ".$mesg->error;
$mesg = $ldap->search(
  filter=>$filter,
  base=>$baseDN,
  sizelimit=>$sizeLimit,
);
$code = $mesg->code;
if ($code && $code != 4) {
  die "Search error, code: $code, message: ".$mesg->error;
}

my @entries = $mesg->entries;
foreach $entry (@entries) {
  $entry->dump;
}
