Skip to content
Snippets Groups Projects
WardenClientCommon.pm 10.98 KiB
#!/usr/bin/perl -w
#
# WardenClientCommon.pm
#
# Copyright (C) 2011-2012 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 Net::SSLeay qw(post_https make_headers make_form
                  load_error_strings
                  set_cert_and_key set_fd 
                  get_peer_certificate ssl_write_all
                  print_errs die_if_ssl_error die_now ssl_read_all);
use XML::Parser;
use Data::Dumper;

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

#-------------------------------------------------------------------------------
# https_send_receive - send $out_message, return (data, http error, headers)
#-------------------------------------------------------------------------------

sub https_send_receive
{
    my ($dest_serv, $port, $out_message, $crt_path, $key_path, $ca_path) = @_;
    my ($ctx, $ssl, $res, $errs);

    ($res, $errs) = Net::SSLeay::open_tcp_connection($dest_serv, $port);
    goto final unless $res;

    # SSLeay init