#!/usr/bin/perl

#---------------------------------------------------------------
# See ../Config/ldap_mozilla for documentation.
#
# Manuel de Vega Barreiro
# Madrid,Spain
# mbarreiro@red.madritel.es
#
#---------------------------------------------------------------

package Monitor::ldap;

require 5.002;
require Exporter;

use strict;

use Mozilla::LDAP::Conn;


@Monitor::ldap::ISA = qw(Exporter Monitor::Monitor);

sub new
{
    my ($this) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};
	$self->{htable} = [];

    bless($self, $class);
    return($self);
}

sub add {
	my $self = shift;
# parametros pendientes de determinar, dependera mucho de si
# se admiten consultas anonimas o no.
#
# pendiente
	my $host = shift;	# ldap server
	my $port = shift;	# ldap port
	my $dn   = shift;	# bind
	my $pass = shift;	# password
	my $item = shift;	# item to report (host.test)
	my $time = shift;	# number of seconds to wait before reporting timeout

	# set port to default ldap port unless specified
	$port = $port ? $port : 389;

	# complain complain complain
	if (! $host) { print "ldap_add(): ENOHOST; no hostname specified\n"; exit(0); }
	if (! $dn) { print "ldap_add(): ENOUSER; no username specified\n"; exit(0); }
	if (! $pass) { print "ldap_add(): ENOPASS; no passname specified\n"; exit(0); }

	print "ldap::add( $host, $port, $dn, $pass, $item, $time)\n" if ($BigSister::common::dl > 3);


	# store the information for this test in @htable
	push(@{ $self->{htable} },[$host,$port,$dn,$pass,$item,$time]);

	return(1);
}

sub check {
 	my $self = shift;
	my $check;
	# step through each host and perform a user authentication request
	foreach $check (@{ $self->{htable} }) {

# pendiente
		my ($host,$port,$dn,$pass,$item,$time) = @{ $check }; 
		my $result;
		my $mesg;
		my $errstr;
		print "ldap::bind( $item, $dn, $pass) -> " if( $BigSister::common::dl>3 );

		# query the ldap server
	# create new ldap object for this host
		my $ldap = new Mozilla::LDAP::Conn($host,$port,$dn,$pass);
		if ($ldap){          
                    $ldap->close();
		    $result = $ldap->getErrorCode();
		    $errstr = $ldap->getErrorString;
		}else{
		    $result=10;
		    $errstr = "Cannot connect to Ldap Server";
		}

		if ($result != 0) {
			# an error occured
			print "$result: $errstr\n" if( $BigSister::common::dl > 3 );
			# report RED
 			$self->report( $item, $BigSister::common::status_codes{"red"}, "$result: $errstr" );
		} else {
			# report GREEN
			print "ok\n" if ( $BigSister::common::dl > 3 );
 			$self->report( $item, $BigSister::common::status_codes{"green"}, "codigo: $result, OK");
		}
	}
}

sub late_check {
	# NOOP #
	return(1);
}

1;







