#!/usr/bin/perl

# monFTP  (c) Copyright 1998 Mark Black 

# NOTE:  If you modify this code, you MUST change the version to
#        something over 100 to prevent it being replaced by future
#        versions of this script. 
$version = 2 ;

# monFTP - This is a little bit of perl network code to test the 
#          operation of the FTP daemon.
#          OUTPUT:
#                   1:      - Success
#                   0:info  - Failure
#	   Input args:
#                   -v      - Print script version number
#		    -i      - Instance.  This tells MATd if there is
#		              an instance of the parameter it wishes
#                             to monitor.  Code returns number of 
#                             instances.
#
#         Note:  Exit status of script should always be  0, anything
#	         else indicates an error in the monitoring script.

$inp = "" ;
foreach $i (0 .. $#ARGV) {
   $inp .= "@ARGV[$i] " ;
}
chop($inp) ;

# Output the version if requested
if($inp eq "-v") {
    print "$version\n" ;
    exit(0) ;
}

if($inp eq "-i") {
     # Just ignore it.  Output is the same
}

use Socket ;
use Sys::Hostname ;

# FTP port = 21
$port = 21 ;

$SIG{'INT'} = 'dokill' ;
$SIG{'TERM'} = 'dokill' ;

sub dokill {
    kill 9,$child if $child ;
}

$sockaddr = 'S n a4 x8' ;

chop($hostname = `hostname`) ;
$them = $hostname ;

($name,$aliases,$proto) = getprotobyname('tcp') ;
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ /^\d+$/;;
($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname) ;
($name,$aliases,$type,$len,$thataddr) = gethostbyname($hostname) ;

$this = pack($sockaddr, &AF_INET, 0, $thisaddr) ;
$that = pack($sockaddr, &AF_INET, $port, $thataddr) ;

#print "port = $port, thisaddr = $thisaddr,  thataddr = $thataddr \n" ;

# Make the socket filehandle
$sck = socket(S, &PF_INET, &SOCK_STREAM, $proto) ;
if(!$sck) {
    print "0:Can't open socket.  sck = $sck . $!\n" if !$inst ;
    exit 0 ;
}

# Give the socket an address
if(!bind(S, $this)) {
    print "0:Can't bind to port\n" ;
    exit 0 ;
}

# Call up the server
if(!connect(S,$that)) {
    # Code fails here if the service is not available
    print "0:Service not running\n" ;
    exit 0 ;
}

# Set socket to be command buffered
select(S); $| = 1; select(STDOUT) ;

# Open pipe for parent child communications
pipe(Rhandle, Whandle) ;

# Avoid deadlock by forking
if($child = fork) {
    close(Whandle) ;
    select(Rhandle) ; $| = 1 ; select(STDOUT) ;
    $flag = 1 ; 
    while($flag) {
	$data = <Rhandle> ;
	if( $data ne "") {
	    $flag = 0 ;
	}
    }
    # Send the quit signal
    print S "quit\n" ;
    $data = <Rhandle> ;
    # Is the correct response
    if(grep(/Goodbye/, $data)) {
	print "1:Service running\n" ;
    } else {
	print "0:Service failure\n" ;
    }
    exit 0 ;
} else {
    close(Rhandle) ;
    select(Whandle) ; $| = 1 ;
    while(<S>) {
	# The child receives data from the remote server through S and sends it back through the pipe
	print Whandle $_ ;
    }
} 










