#!/usr/bin/env perl

use strict;

# ten second timeout

$SIG{'ALRM'} = sub {
  print "no bueno - connection timeout\n";
  exit 2; # CRITICAL
};
alarm(10);

# connect to policyd

use IO::Socket::INET;

my $sock = IO::Socket::INET->new('127.0.0.1:10031');

if (! $sock) {
  print "no bueno - could not connect to localhost socket\n";
  exit 2; # CRITICAL
}

# send a bogus request

print $sock <<EOD;
request=smtpd_access_policy
protocol_state=RCPT
protocol_name=SMTP
helo_name=some.domain.tld
queue_id=8045F2AB23
sender=foo\@bar.tld
recipient=bar\@foo.tld
recipient_count=0
client_address=1.2.3.4
client_name=another.domain.tld
reverse_client_name=another.domain.tld
instance=123.456.7

EOD

# check for an appropriate response

$_ = readline $sock;
if (/^action=/) {
  print "bueno\n";
  exit 0; # OK
}
else {
  print "no bueno - failure in policyd response\n";
  exit 2; # CRITICAL
}
