Skip to content
Snippets Groups Projects
Forked from 713 / Warden / Warden
501 commits behind the upstream repository.
WardenClientCommon.pm 6.01 KiB
#!/usr/bin/perl -w
#
# WardenClientCommon.pm
#
# Copyright (C) 2011-2013 Cesnet z.s.p.o
#
# Use of this source is governed by a BSD-style license, see LICENSE file.

package WardenClientCommon;

use strict;
use Carp;
use SOAP::Lite;
use IO::Socket::SSL qw(debug1);
use SOAP::Transport::HTTP;
use Sys::Syslog;

our $VERSION = "2.2";

#-------------------------------------------------------------------------------
# errMsg - prints error msg and returns undef or prints warning and returns 1
#-------------------------------------------------------------------------------
sub errMsg
{
  my $msg = shift;
  my $type = shift; 
  defined $type or $type = "err"; # default type is err. Other: warn
  
  # is this error report and is Verbose logging mode enabled?
  if (($type eq "err") && ($WardenClientConf::LOG_VERBOSE)) { # user wants to log debug information
    $msg .= "\nStack info: " . Carp::longmess();
  }

  # log into STDERR?
  if ($WardenClientConf::LOG_STDERR) {
    print STDERR $msg . "\n";
  }

  # log into Syslog?
  if ($WardenClientConf::LOG_SYSLOG) {
    openlog("Warden-client:", "pid", "$WardenClientConf::LOG_SYSLOG_FACILITY");
    syslog("$type|$WardenClientConf::LOG_SYSLOG_FACILITY", $msg . "\n");
    closelog();
  }

  if ($type eq 'warn') { # case of 'warn'
    return 1;
  } else { # case of 'err'
    return;
  }

} # End of errMsg


#-------------------------------------------------------------------------------
# c2s - connect to server, send request and receive response
#-------------------------------------------------------------------------------
sub c2s
{
  my $uri               = shift;
  my $ssl_key_file      = shift;
  my $ssl_cert_file     = shift;
  my $ssl_ca_file       = shift;
  my $method            = shift;
  my $data              = shift;

  my $client;
  my ($server, $port, $service) = $uri =~ /https:\/\/(.+)\:(\d+)\/(.+)/;
  eval {
    $client = SOAP::Transport::HTTP::Client->new();