# -*- perl -*-

# taken from:
# check_by_ssl.pl Verify a server is running on SSL.
# requires Net::SSLeay
#
# Copyright (C) 2001 Subhendu Ghosh
#
# 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.
#
#
# Report bugs to: s.ghosh@acm.org

#
# $Id: csl_ssl.pm,v 1.1 2006/05/18 17:42:44 craft Exp $
#
# $Revision: 1.1 $
package ssl;

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;

use CSL::SSL096;
use Net::SSLeay qw(sslcat);

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

sub print_usage () {

    print "
Usage:  Check ssl services.
Options:
    -H <hostname> -P <portnumber> -r <pop3,imap>... [--verbose]
    --help
    --version
    --usage

";
    return $ERRORS{'OK'};
}

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

    print "ssl.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'};
    }

    ## Currently supported definitions include POP3, IMAP
    my %test = (
			    'pop3' 	=> {
			 	    'message' 	=> "QUIT\n",
				    'match'		=> "OK",
				    'port'		=> "995",
				    },
			    'imap'	=> {
				    'message'	=> "a1 LOGOUT\n",
				    'match'		=> "OK",
				    'port'		=> "993",
				    }
			    );

    my $port = $options{"opt_P"};
    my $protocol = $options{"opt_r"};

    if ((!($protocol)) || (!($test{$protocol}{'message'}))) {
        print "Invalid protocol!\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    if (defined $port) {
        $test{$protocol}{'port'} = $port;
    }


    my $answer = "UNKNOWN: no response\n";
    my $state = "UNKNOWN";

    if ($options{"verbose"}) {
        print "port - " . $test{$protocol}{'port'} . " protocol - $protocol\n"; 
        print "message - " . $test{$protocol}{'message'} . "\n";
    }

    my $res1 = sslcat($host, $test{$protocol}{'port'}, $test{$protocol}{'message'} );


    if ($res1 =~/$test{$protocol}{'match'}/ && $res1 =~ /$protocol/i ) {
	    $state = 'OK';
	    $answer = "$state: $res1 \n";
    } elsif($res1 eq '0') {
	    $state = 'CRITICAL';
	    $answer = "CRITICAL: No answer on $host:$port ($res1)\n";
    } else {
	    $state = 'UNKNOWN';
	    $answer = "UNKNOWN: $protocol response did not match: ($res1) \n";
    }

    $answer =~ s/\n/;/g;
    print ("$answer\n");

    exit $ERRORS{$state};
}

