#!/bin/perl
#############################################################################
#
#              $Source: /share/rcs/ops_dev/ldap-util/bin/search $
#            $Revision: 1.4 $
#                $Date: 2001/08/16 19:08:02 $
#              $Author: edm $
#              $Locker: $
#               $State: Exp $
#
#              Purpose: search ldap database
#
#           Directions: see pod docs (type "perldoc ./search")
#
#     Default Location:
#
#           Invoked by:
#
#       Copyright (C) 2000 iEngineer.com
#
#       This program is free software; you can redistribute it and/or
#       modify it under the terms of version 2 of the GNU General Public
#       License as published by the Free Software Foundation available at
#
#       http://www.gnu.org/copyleft/gpl.html
#
#       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.  See the
#       GNU General Public License for more details.
#
#
#############################################################################
#
#
#       REVISION HISTORY:
#
#       $Log: search $
#       Revision 1.4  2001/08/16 19:08:02  edm
#       updated documentation
#       Revision 1.3  2001/08/16 17:58:01  nickb


use 5.6.0;
use Getopt::Std;
use Net::LDAP;
use strict;
use vars qw($opt_b $opt_h $opt_p $opt_V);

my $VERSION = 1.0;

# organizational attribute
my $org_root = "iengineer.com";

# usage message
my $usage =
q(usage:  search [ -b 'base DN' ] [ -h ldaphost ] [ -p ldapport ] filter
[ 'parameter list' ] [ -V ]) . "\n";

my $version =
q(search version 1.0, Copyright (C) 2000 iEngineer.com

This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more
details) . "\n\n";

die "$usage" unless (@ARGV) >= 1;

getopts('b:h:p:V');
my ($arg, $attr, $base, $entry, $host, @param, $port);

# getopt tests

# version
if (defined($opt_V))
{
    die $version;
}

# assign base
if (defined($opt_b)) {
    if ($opt_b =~ /\w+/) {
	$base = $opt_b;
    } else {
	die "$usage";
    }
} else {
    $base = "o=iengineer.com";
}

# assign host
if (defined($opt_h)) {
    $host = $opt_h;
} else {
    $host = "localhost";
}

# assign port
if (defined($opt_p)) {
    if ($opt_p =~ /^\d+$/) {
	$port = $opt_p;
	print "port is $port\n";
    } else {
	die "$usage";
    }
} else {
    $port = 389;
}

# assign filter
die "$usage" unless defined($ARGV[0]);
my $filter = shift;

# assign parameter list
if (defined($ARGV[0])) {
    foreach $attr (@ARGV) {
	push(@param, $attr);
    }
}

my $paramref = \@param;

my $ldap = Net::LDAP->new($host, port => $port);

# anonymous bind to ldap server
$ldap -> bind;

# search server
my $mesg = $ldap->search (
			  base   => $base,
			  filter => $filter,
			  attrs  => [ @{$paramref} ]
			 ) or die "search failed:  $!\n" ;

# print search results
foreach $entry ($mesg->all_entries) { $entry->dump; }

# end session
$ldap->unbind;

__END__

=head1 NAME

search - search ldap database

=head1 SYNOPSIS

B<search> [ B<-b> C<base DN> ] [ B<-h> ldaphost ] [ B<-p> ldapport ] filter [ C<parameter list> ] [ B<-V> ]

=head1 DESCRIPTION

I<search> prints ldap objects to the screen based on criteria specified on the
command line.

=head1 OPTIONS

=over 4

=item B<-b> base DN

The DN that is the base object entry relative to which the search is to be
performed.  defaults to C<o=iengineer.com> unless specified.  optionally
you may change "$org_root" on line 47 to an appropriate value.

=item B<-h> ldaphost

hostname or IP address of ldap server.

=item B<-p> ldapport

ldap server listen port.  defaults to 389.

=item B<filter>

attribute being searched for in ldap database.  required parameter.

=item B<-V>

print version and exit.

=back

=head1 EXAMPLES

=over 4

=item example 1:  print record pertaining to an attribute

search -b o=iengineer.com -h thames uid=nickb

=item example 2:  print all records with common attribute

search -b o=iengineer.com -h thames 'l=Salt Lake City'

=item example 3: search for records using wildcard

search -b o=iengineer.com -h thames uid=*

=back

=head1 PREREQUISITES

perl v5.6.0
Net::LDAP
Convert::ASN1

=head1 VERSION

search v1.0, Copyright (C) 2000 iEngineer.com

=head1 AUTHOR

Nick Balthaser <nb9001@yahoo.com>

=head1 SCRIPT CATEGORIES

UNIX/System_administration

=head1 README

search ldap database for specified attributes.

=cut
