#!/usr/local/bin/perl
#
# This script will search an LDAP server for objects that match the -filter
# option, starting at the DN given by the -basedn option. Each DN found must
# contain the attribute given by the -attribute option and the attribute's
# value must match the value given by the -value option.  Servers are given on
# the command line. At least one server must be specified.

# This script use the Net::LDAP, which uses some LDAP libraries like those
# from UMich, Netscape, or ISODE.
#
# Porting to LDAP (from LDAPapi) by Thomas Quinot <thomas@cuivre.fr.eu.org>,
# 1999-09-20.

# Copyright (C) 1998, David Eckelkamp <davide@tradewave.com>
#
#    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 2 of the License, or
#    (at your option) any later version.
#
#    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.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# $Id: ldap.plugin,v 1.1.1.1.2.1 2002/07/22 15:21:49 paul-cs Exp $
#
# Modified by Paul van Tilburg. Speedup needed for Checkservice...
# (removed multiple host support and changed default values)

use Net::LDAP;
use Getopt::Long;

# Here are the default values for the things you can specify via options
$LDAPPort = 389;
$BaseDN   = "o=Linvision, c=NL";
$Filter   = "objectClass=organizationalunit";
$Scope    = 2;
$Attribute = "ou";
$Value    = "Users";
$verbose = 0;

@errs = ();

%OptVars = ("port"   => \$LDAPPort,
	    "basedn" => \$BaseDN,
	    "filter" => \$Filter,
	    "attribute" => \$Attribute,
	    "value"  => \$Value,
	    "verbose" => \$verbose,
	    "host" => \$LDAPHost);

if (!GetOptions(\%OptVars,
		"port=i", "basedn=s", "filter=s",
		"attribute=s", "value=s", "scope=i", "verbose", "host=s")) { exit 1; }

# Open the connection to the server and do a simple, anonymous bind
unless ($ldap = Net::LDAP->new($LDAPHost, port => $LDAPPort)) { exit 1;}

unless ($ldap->bind) { exit 1;}

unless ($mesg = $ldap->search(base => $BaseDN, filter => $Filter, scope => $Scope)) { exit 1; }

$nentries = 0;
foreach $entry ($mesg->entries) {
  my $dn = $entry->dn;
  $nentries++;
  foreach $attr ($entry->attributes) {
    $record{$dn}->{$attr} = [$entry->get ($attr)];
  }
}

$ldap->unbind;

if ($nentries == 0) { exit 1; }

# Analyze results.

# Step 1 is to loop through all DNs returned from the search.
print "Looking for $Attribute=$Value\n" if $verbose;
foreach $dn (sort keys %record) {
  print "checking object $dn\n" if $verbose;
  # Loop through the attributes for this DN
  $attrFound = 0;
  $goodVal = 0;
  foreach $attr (keys %{$record{$dn}}) {
    print "  checking attr=$attr\n" if $verbose;
    next unless ($attr eq $Attribute);
    $attrFound++;
    print "  found correct attribute\n" if $verbose;
    # Each value could be/is an array so search the array
    foreach $val (@{$record{$dn}{$attr}}) {
      print "    checking val = $val\n" if $verbose;
      next unless ($val eq $Value);
      $goodVal++;
      print "    found correct value\n" if $verbose;
      last;
    }
    last if ($goodVal);
  }
}

if (!$attrFound || !$goodVal) { exit 1; };

exit 0;


syntax highlighted by Code2HTML, v. 0.9.1