# -*- perl -*-
#
# $Revision: 1.2 $
package slapd;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;

sub print_usage ();
sub print_version ();
sub service_test (%);

sub print_usage () {

    print "
Usage:  Check ftpd service.
Options:
    -H <hostname> [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.2 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "ftpd.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $host = $options{"opt_H"};

    if ((!$host) || ($host eq "")) {
        print "\nHost name not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my $ldap;
    my $ldaps;
    my $mesg;
    my $basedn = "dc=cs,dc=wisc,dc=edu";
    my $status = "OK";
    my $output = "";

    use Net::LDAP;

    $ldap = Net::LDAP->new("ldap://$host", timeout=>30);
    if ($ldap == undef) {
      # Can't connect to an LDAP daemon at all...this is bad
      $status = "CRITICAL";
      $output = "LDAP (port 389) connection failed! ";
    } else {
      # do a user search; use strivia account as test
      $mesg = $ldap->search (base=>$basedn, filter=>'(uid=strivia)');
      if ($mesg == undef) {
	# no search results!
      	$status = "CRITICAL";
	$output = $output . " Search for user on LDAP port 389 failed! ";
      }
      # do an alias search; use strivia account as test
      $mesg = $ldap->search (base=>$basedn, filter=>'(SendmailMTAKey=strivia)');
      if ($mesg == undef) {
	# no search results!
      	$status = "CRITICAL";
	$output = $output . " Search for alias on LDAP port 389 failed! ";
      }
    }


    $ldaps = Net::LDAP->new("ldaps://$host", timeout=>30);
    if ($ldaps == undef) {
      # Can't connect to an LDAP daemon at all on secure port...this is bad
      $status = "CRITICAL";
      $output = $output . "LDAP (port 636) connection failed! ";
    } else {
      # do a search; use strivia account as test
      $mesg = $ldaps->search (base=>$basedn, filter=>'(uid=strivia)');
      if ($mesg == undef) {
	# no search results!
      	$status = "CRITICAL";
	$output = $output . " Search on LDAP port 636 failed! ";
      }
      # do an alias search; use strivia account as test
      $mesg = $ldap->search (base=>$basedn, filter=>'(SendmailMTAKey=strivia)');
      if ($mesg == undef) {
	# no search results!
      	$status = "CRITICAL";
	$output = $output . " Search for alias on LDAP port 636 failed! ";
      }
    } 

    if ($status eq "OK") {
      print "$status: LDAP services ok\n";
#    } elsif ($status eq "WARNING") {
#      print "$status: some warning error condition\n";
    } else {
      print "$status: $output\n";
    }

    print "Exit status: $status\n" if $options{"verbose"};

    return $ERRORS{$status};
}

