#!/usr/bin/perl
use strict;
use warnings;
use RFC::Main;
use SOAP::Lite;
use Net::Ping;
use IO::Socket::SSL;

if (-e '/home/celltest/allow_selfsigned_certs')
{
    IO::Socket::SSL::set_defaults(SSL_verify_mode => 0);
    $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
}
### End self signed cert hack

my $chainfile = get_cv('SSL','chainfile');
if($chainfile)
{
    $ENV{HTTPS_CA_FILE}   = $chainfile;
}
my $soap = undef;
my $service  = &get_cv('passwds','passwd_server');
my $wsdl = &get_cv('passwds','wsdl');
$wsdl = 'https://'.$service.'/wsdl/session.wsdl' unless ($wsdl);
if ($wsdl)
{    
    my $remote = $wsdl;
    $remote =~ /\w+\:\/+([\w\.\-]+)/;
    $remote = $1;
    ## Check if server is reachable ###
    my $p = Net::Ping->new("tcp", 2);
    $p->port_number(scalar(getservbyname("https", "tcp")));
    my $is_alive = $p->ping($remote);
    $p->close();
    die "No conection to password server $remote\n" unless ($is_alive);
    $soap = SOAP::Lite->on_fault(\&soapGetBad)->service($wsdl) if ($is_alive);
    print $soap->ping(),"\n";
}
else
{
    die "No wsdl configuration found for the password server\n";
}
