# -*- perl -*-
#
# $Revision: 1.1 $
package ftpd;

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.1 $';
    $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 $ftp;
    my $status = "CRITICAL";
    my $output = "";

    use Net::FTP;

    $ftp = Net::FTP->new($host, Debug=> 0);

    if ($ftp == undef) {
      # Can't connect to an FTP daemon at all...this is bad
      $status = "CRITICAL";
    } else {
      my $result = $ftp->login("anonymous",'nagios\@cs.wisc.edu');
      if ($result == undef) {
	# Can't login as anonymous, although ftpd DID answer...
	$status = "WARNING";
      } else {
	$status = "OK";
      }
      $ftp->quit;
    }

    if ($status eq "OK") {
      print "$status: anonymous ftpd ok\n";
    } elsif ($status eq "WARNING") {
      print "$status: ftpd running, not allowing anonymous login\n";
    } else {
      print "$status: ftpd not responding\n";
    }

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

    return $ERRORS{$status};
}

