#!/usr/bin/perl -w

use strict;
use IO::Handle;
use Socket;
use Sys::Hostname;
use Getopt::Std;

STDERR->autoflush(1);
STDOUT->autoflush(1);

# i: NetID; t: timeout

use vars qw($opt_d $opt_i $opt_h $opt_H $opt_p $opt_t);
getopts("dhH:p:i:t:");

our $Usage = <<EOUSAGE;
Usage:  $0 -H <hostname> [-p <port>] [-t <timeout>] -i NetID
EOUSAGE
(print $Usage and exit 3) if $opt_h;

if (!defined($opt_H) or !defined($opt_i)) {
	print $Usage;
	exit 3;
}

our $debug = $opt_d;

our $NetID = $opt_i;
our $ThatHost = $opt_H;
our $ThatPort = defined($opt_p) ? $opt_p : 80;
our $TIMEOUT = defined($opt_t) ? $opt_t : 10;

our @Result;

our $QRY = <<End_Of_Query;
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<IsNetIDAvailable xmlns="http://webservices.princeton.edu/NetID">
<NetID>$NetID</NetID>
</IsNetIDAvailable>
</soap:Body>
</soap:Envelope>
End_Of_Query

our $LengthOfQRY = length($QRY);

our $ThisHost = hostname();
our ($AfInet, $SockStream, $SockAddr) = (AF_INET, SOCK_STREAM, 'S n a4 x8');
our ($Name, $Aliases, $Proto) = getprotobyname('tcp');
our ($Type, $Length) = (undef, undef);
our @ThisAddr = ();
our @ThatAddr = ();
($Name, $Aliases, $Type, $Length, @ThisAddr) = gethostbyname($ThisHost);
($Name, $Aliases, $Type, $Length, @ThatAddr) = gethostbyname($ThatHost);
our $This = pack ($SockAddr, $AfInet,         0, $ThisAddr[0]);
our $That = pack ($SockAddr, $AfInet, $ThatPort, $ThatAddr[0]);
if (socket(SOCKET, $AfInet, $SockStream, $Proto)) {
	print STDOUT "TCP socket created\n" if $debug;
} else {
	print "CRITICAL: Cannot connect to $ThatHost:$ThatPort\n";
	exit 2;
}

our $Error = '';
$SIG{ALRM} = sub { print "CRITICAL: Timeout connecting to $ThatHost:$ThatPort\n"; exit 2; };
alarm($TIMEOUT) if $TIMEOUT > 0;
if (connect(SOCKET, $That)) {
	printf STDOUT "Connected to port %u of %s\n", $ThatPort, $ThatHost if $debug;
} else {
	print "CRITICAL: Cannot connect to $ThatHost:$ThatPort\n";
	exit 2;
}

alarm(0);

$SIG{ALRM} = 'DEFAULT';
SOCKET->autoflush(1);

our $HTTPandQRY = <<EONETID;
POST http://winadmin.princeton.edu/webservices/NetIDServices/NetID.asmx HTTP/1.1
Host: winadmin.Princeton.EDU
Content-Type: text/xml; charset=utf-8
Content-Length: $LengthOfQRY
SOAPAction: "http://webservices.princeton.edu/NetID/IsNetIDAvailable"

$QRY
EONETID

select SOCKET; $|=1; select STDOUT;
$SIG{ALRM} = sub { print "CRITICAL: Timeout receiving response from $ThatHost:$ThatPort\n"; exit 2; };
alarm($TIMEOUT) if $TIMEOUT > 0;
print STDOUT $HTTPandQRY if $debug;
print SOCKET $HTTPandQRY;

my $clength = 0;

while (<SOCKET>) {
	print STDOUT if $debug;
	chop;chop;
	push @Result, $_;
	if (m/Content-Length: (\d+)/) {
		$clength = $1;
		print "content length $clength\n" if $debug;
	}
	last if $_ eq '';
}

print "Got header\n" if $debug;

if ($Result[0] !~ /HTTP\/.*200 OK/) {
	print "CRITICAL: NETID query returned $Result[0]\n";
	exit 2;
}

my $buf;

read(SOCKET, $buf, $clength);
print "$buf\n" if $debug;

alarm(0);
$SIG{ALRM} = 'DEFAULT';
close SOCKET;

if ($buf =~ /<IsNetIDAvailableResult>(true|false)<\/IsNetIDAvailableResult>/) {
	print "OK: NetID query succeeded ($NetID: $1)\n";
	exit 0;
}

print "CRITICAL: NetID query failed\n";
exit 2;

