#!/usr/bin/perl -w
#
# try to connect to a particular
# port on a bunch of hosts. For use with "mon".
#
# Options are
#   -p <port-num>
#   -t <connect-timeout-in-seconds> (default 15)
#   -S <use ssl>
#   -s <string to send upon connecting to provoke some output>
#   -e <Perl regexp to expect in response>
#   -q <string to send before closing after parsing response>
#   -d <string to use as line delimiter for regexp matching>

# without /-s/-e/-q/, just checks that the socket can be opened
# and closed.

# cheap transformations done on send/quit/delim strings - \r and \n are
# converted to CR and LF.  \\ is not supported - no escape possible.

# sample usage:
#
# smtp:    tcpadv.monitor -p 25  -e '^220\b' -q 'QUIT\r\n'
# smtps:   tcpadv.monitor -p 465 -S -e '^220\b' -q 'QUIT\r\n'
# pop3:    tcpadv.monitor -p 110  -e '^.+OK' -q 'QUIT\r\n'
# pop3s:   tcpadv.monitor -p 995 -S -e '^.+OK' -q 'QUIT\r\n'
# imap:    tcpadv.monitor -p 143 -e '.*OK.*IMAP4.*' -q 'A1 LOGOUT\n'
# imaps:   tcpadv.monitor -p 993 -S -e '.*OK.*IMAP4.*' -q 'A1 LOGOUT\n'
# web:     tcpadv.monitor -p 80  -s 'GET / HTTP/1.0\r\n\n' -e '^Server:.*'
# web-ssl: tcpadv.monitor -S -p 443 -s 'GET / HTTP/1.0\r\n\n' -e '^Server:.*' 
# ssh:     tcpadv.monitor -p 22 -s 'SSH-2.0-SSH.Monitor\n\r\n\r\n\r\n\r\n' -e '.*SSH.*'

#
# Jim Trocki, trockij@transmeta.com
# updated August 2000 by Ed Ravin <eravin@panix.com> for send/expect/quit
#
# $Id: tcpch.monitor,v 1.4 2001/09/22 04:45:25 root Exp root $
#
#    Copyright (C) 1998, Jim Trocki
#
#    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
#
#    Modified by Jb007 for use with SSL
#    tcpadv.monitor,v 1.4 Mod 0.1

use Getopt::Std;
use Socket;

my %opt;
getopts ("d:p:t:s:e:q:S", \%opt);
$USAGE= "Usage: tcpch.monitor -p port [-t timeout] [-S] [-s sendstr] [-e regexp] [-q quitstr] [-d line-delim]\n";

my $PORT = $opt{"p"} || undef;
my $TIMEOUT = $opt{"t"} || 15;

my $SSL = $opt{"S"} || undef;

my $SEND=   $opt{"s"} || undef;
my $EXPECT= $opt{"e"} || undef;
my $QUITSTR=$opt{"q"} || undef;
my $DELIM=  $opt{"d"} || "\n";
if ($DELIM)
{
	$DELIM=~ s/\\n/\n/g;
	$DELIM=~ s/\\r/\r/g;
}

if (defined($SSL))
   {
	use Net::SSLeay qw(die_now die_if_ssl_error) ;
	Net::SSLeay::load_error_strings();
	Net::SSLeay::SSLeay_add_ssl_algorithms();
	Net::SSLeay::randomize();
   }

my @failures = ();
my @detail = ();

my $ALARM = 0;

sub checkbuf  # buffer, regexp
{
	my ($buffer, $regexp)= @_;

	return $buffer =~ /$regexp/ if ($DELIM eq '');

	my @lines= split($DELIM, $buffer);

	foreach my $line (@lines)
	{
		if ($line =~ /$regexp/)
		{
			return 1;
		}
	}
	return 0;
}

die $USAGE unless (@ARGV > 0);
die "$0: missing port number\n" unless defined $PORT;

foreach my $host (@ARGV) {
    my $pro = getprotobyname ('tcp');

    if (!defined $pro) {
    	die "(local err) could not getprotobyname\n";
    }

    if (!defined socket (S, PF_INET, SOCK_STREAM, $pro)) {
    	die "(local err) could not create socket: $!\n";
    }

    my $a = inet_aton ($host);
    if (!defined $a) {
    	push @failures, $host;
	push @detail, "(local err) $host could not inet_aton";
	close (S);
	next;
    }

    my $sin = sockaddr_in ($PORT, $a);
    if (!defined $sin) {
	push @failures, $host;
	push @detail, "(local err) $host could not sockaddr_in";
    	close (S);
	next;
    }

    my $r;

    eval {
	local $SIG{"ALRM"} = sub { die "alarm\n" };

	alarm $TIMEOUT;

	$r = connect (S, $sin);

	alarm 0;
    };

    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
			push @detail, "$host timeout on connect";
		} else {
			push @detail, "$host interrupted syscall on connect: $!";
		}

	close (S);
	next;
    }

    if (!defined $r) {
	push @failures, $host;
	push @detail, "$host: could not connect: $!";
	close (S);
	next;
    }

    select S; $|= 1; select STDOUT;

    if (defined($SSL))
        {
	   $ctx = Net::SSLeay::CTX_new() or die_now("Failed to create SSL_CTX $!");
	   my $setctx = Net::SSLeay::CTX_set_options($ctx, &Net::SSLeay::OP_ALL)
		   and die_if_ssl_error("ssl ctx set options");
	   $ssl = Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!");
	   my $setfd = Net::SSLeay::set_fd($ssl, fileno(S));   # Must use fileno

	   my $rssl;
	   
	   eval {
	   	local $SIG{"ALRM"} = sub { die "alarm\n" };
		
		alarm $TIMEOUT;
		
		$rssl = Net::SSLeay::connect($ssl);

		alarm 0;
		};
	
	   if ($@) {
	        push @failures, $host;

		if ($@ eq "alarm\n") {
			push @detail, "$host timeout on ssl-connect";
		    } else {
			push @detail, "$host interrupted syscall on sll-connect: $!";
		    }

		close (S);
		next;
		}
		
	   if ( $rssl != 1 ) {
	        push @failures, $host;
	        push @detail, "$host ssl connect error";
	   	close (S);
		next;
		}
        }

	if (defined($SEND))
	{
		my $rc= undef;

		$SEND=~ s/\\n/\n/g;
		$SEND=~ s/\\r/\r/g;
		eval {
			local $SIG{"ALRM"} = sub { die "alarm\n" };

			alarm $TIMEOUT;
			if (defined($SSL)) {
				    $rc = Net::SSLeay::write($ssl, $SEND);  # Perl knows how long $SEND is
				    die_if_ssl_error("ssl write");
				} else {
				    $rc= send S, $SEND, 0;
				}
			alarm 0;
		    };
	    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
				push @detail, "$host timeout on write";
			} else {
				push @detail, "$host interrupted syscall on write: $!";
			}
		}

		if (! $rc)
		{
			push @failures, $host;
			push @detail, "$host: write failed: $!";
			close (S);
			next;
		}
	}

	if (defined($EXPECT))
	{
		# read and match

		my $rc= undef;
		my $alldata= "";

		eval {
			local $SIG{"ALRM"} = sub { die "alarm\n" };

			alarm $TIMEOUT;
			if (defined($SSL)) {
				    $rxdata = Net::SSLeay::read($ssl);
				} else {
				    $rc= recv S, $rxdata, 1024, 0;
				}
			$alldata= $alldata . $rxdata;

			while ( !checkbuf($alldata,  $EXPECT))
			{
				if (defined($SSL)) {
					    $rxdata = Net::SSLeay::read($ssl);
					} else {
					    $rc= recv S, $rxdata, 1024, 0;
					}
				$alldata= $alldata . $rxdata;
			}
			alarm 0;
		    };
	    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
				push @detail, "$host timeout on read";
			} else {
				push @detail, "$host interrupted syscall on read: $!";
			}
		}
		if ($rc)
		{
			push @failures, $host;
			push @detail, "$host: recv failed : $!";
			close (S);
			next;
		}

		if (! checkbuf($alldata, $EXPECT))
		{
			push @failures, $host;
			push @detail, "$host: did not recv expected response";
			close (S);
			next;
		}
	}

	if (defined($QUITSTR))
	{
		my $rc= undef;

		$QUITSTR=~ s/\\n/\n/g;
		$QUITSTR=~ s/\\r/\r/g;

		eval {
			local $SIG{"ALRM"} = sub { die "alarm\n" };

			alarm $TIMEOUT;
			if (defined($SSL)) {
					$rc = Net::SSLeay::write($ssl, $QUITSTR);
					die_if_ssl_error("ssl write");
				} else {
					$rc= send S, $QUITSTR, 0;
				}
			alarm 0;
		    };
	    if ($@) {
		push @failures, $host;

		if ($@ eq "alarm\n") {
				push @detail, "$host timeout writing quitstr";
			} else {
				push @detail, "$host interrupted syscall writing quitstr: $!";
			}
		}

		if (! $rc)
		{
			push @failures, $host;
			push @detail, "$host: quit write failed: $!";
			close (S);
			next;
		}
	}

    if (defined($SSL))
    	{
	    shutdown S, 1;
	    die_if_ssl_error("ssl quit");
	    Net::SSLeay::free ($ssl);               # Tear down connection
	    Net::SSLeay::CTX_free ($ctx);
	}

    if (!defined close (S)) {
    	push @failures, $host;
	push @detail, "$host: could not close socket: $!";
	next;
    }
}

if (@failures == 0) {
    exit 0;
}

print "@failures\n";
print "\n", join ("\n", @detail), "\n";

exit 1;
