diff --git a/src/warden-client/lib/WardenClientReceive.pm b/src/warden-client/lib/WardenClientReceive.pm index 059011b71f591b2a4a406c42a42dd3171e143f79..93ee9052c2c17db8cd08d79c22ba2a083c6811eb 100755 --- a/src/warden-client/lib/WardenClientReceive.pm +++ b/src/warden-client/lib/WardenClientReceive.pm @@ -18,7 +18,7 @@ use Sys::Syslog; our $VERSION = "2.0"; -#----- global configuration variables ----------------------------------------- +#----- global configuration variables - default initialization ----------------- our $LOG_STDERR = 1; our $LOG_SYSLOG = 0; @@ -87,9 +87,6 @@ sub c2s # deserialized response from server -> create SOAP envelope and data object my $response; - # test - errMsg("test error in c2s()"); - eval { $response = $soap->deserializer->deserialize($result); } or errMsg($@ . "Received data: " . $result); @@ -121,9 +118,6 @@ sub getNewEvents my ($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file, $max_rcv_events_limit); ($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file, $max_rcv_events_limit, $LOG_STDERR, $LOG_SYSLOG, $LOG_SYSLOG_FACILITY, $LOG_VERBOSE) = WardenClientConf::loadConf($conf_file); - # test - # errMsg("testovaci error\n"); - # set name of ID file for each client aplication my $caller_name = $FindBin::Script; my $id_file = $vardir . $caller_name . ".id"; @@ -141,7 +135,7 @@ sub getNewEvents } else { my $response = c2s($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file, "getLastId"); $last_id = $response->result; - open(ID, "> $id_file") || die ("Cannot open ID file $id_file: $!"); + open(ID, "> $id_file") || errMsg("Cannot open ID file $id_file: $!"); print ID $last_id; close ID; } @@ -163,7 +157,6 @@ sub getNewEvents # parse returned SOAP data object my ($id, $hostname, $service, $detected, $type, $source_type, $source, $target_proto, $target_port, $attack_scale, $note, $priority, $timeout); -# my @events; my @response_list = $response->valueof('/Envelope/Body/getNewEventsResponse/event/'); while (scalar @response_list) { my $response_data = shift(@response_list); @@ -196,7 +189,7 @@ sub getNewEvents # write last return ID if (defined $last_id) { # must be defined for first check ID - open(ID, "> $id_file") || die ("Cannot open ID file $id_file: $!"); + open(ID, "> $id_file") || errMsg("Cannot open ID file $id_file: $!"); print ID $last_id; close ID; } @@ -204,16 +197,13 @@ sub getNewEvents or do { if ($LOG_STDERR) { - print STDERR "(STDERR)Warden-client unexpected end in eval block.\n" . $@ . "\n"; + print STDERR "Warden-client unexpected end in eval block.\n" . $@ . "\n"; } if ($LOG_SYSLOG) { - #TODO: zapis do syslogu, ne STDERR - print STDERR "(SYSLOG)Warden-client unexpected end in eval block.\n" . $@ . "\n"; - - # openlog("Warden:", "pid", "$LOG_SYSLOG_FACILITY"); - # syslog("err|$LOG_SYSLOG_FACILITY", "Warden-client unexpected end in eval block.\n" . $@ . "\n"); - # closelog(); + openlog("Warden:", "pid", "$LOG_SYSLOG_FACILITY"); + syslog("err|$LOG_SYSLOG_FACILITY", "Warden-client unexpected end in eval block.\n" . $@ . "\n"); + closelog(); } return; }; diff --git a/src/warden-client/lib/WardenClientSend.pm b/src/warden-client/lib/WardenClientSend.pm index 429aeb01c078f523087d66ea808d952f2a1b6995..32a9d45a93b98bf38697746e604e4ed806e342ec 100755 --- a/src/warden-client/lib/WardenClientSend.pm +++ b/src/warden-client/lib/WardenClientSend.pm @@ -12,16 +12,31 @@ use strict; use SOAP::Lite; use IO::Socket::SSL qw(debug1); use SOAP::Transport::HTTP; +use Carp; +use Sys::Syslog; -our $VERSION = "2.0"; +our $VERSION = "2.0"; #first iteration after 'port to Apache' +#----- global configuration variables - default initialization ----------------- +our $LOG_STDERR = 1; + +our $LOG_SYSLOG = 0; +our $LOG_SYSLOG_FACILITY; + +our $LOG_VERBOSE = 0; +#----- end of configuration variables ------------------------------------------ #------------------------------------------------------------------------------- # errMsg - print error message and die #------------------------------------------------------------------------------- sub errMsg { - my $msg = shift; + my $msg = "Error message: " . shift; + + if ($LOG_VERBOSE) { # user wants to log debug information + $msg .= "\nStack info: " . Carp::longmess(); + } + die($msg . "\n"); } # End of errMsg @@ -31,12 +46,12 @@ sub errMsg #------------------------------------------------------------------------------- sub c2s { - my $uri = shift; + 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 $ssl_ca_file = shift; + my $method = shift; + my $data = shift; my ($server, $port, $service) = $uri =~ /https:\/\/(.+)\:(\d+)\/(.+)/; @@ -44,7 +59,7 @@ sub c2s if (!($client = SOAP::Transport::HTTP::Client->new())) { errMsg("Sorry, unable to create socket: " . &SOAP::Transport::HTTP::Client::errstr) } - $client->timeout(10); + $client->timeout(60); $client->ssl_opts(verify_hostname => 1, SSL_use_cert => 1, SSL_verify_mode => 0x02, @@ -67,10 +82,7 @@ sub c2s errMsg("Error: server returned empty response." . "\n" . "Problem with used SSL ceritificates or Warden server at $server:$port is down."); } else { # deserialized response from server -> create SOAP envelope and data object - my $response; - eval { - $response = $soap->deserializer->deserialize($result); - } or errMsg($@ . "Received data: " . $result); + my $response = $soap->deserializer->deserialize($result); # check SOAP fault status $response->fault ? errMsg("Server sent error message:: " . $response->faultstring) : return 1; } @@ -83,6 +95,7 @@ sub c2s sub saveNewEvent { my $result; + eval { my $warden_path = shift; my $event_ref = shift; @@ -93,7 +106,8 @@ sub saveNewEvent # read the config file require $libdir . "WardenClientConf.pm"; my $conf_file = $etcdir . "warden-client.conf"; - my ($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file) = WardenClientConf::loadConf($conf_file); + my ($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file, $max_rcv_events_limit); + ($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file, $max_rcv_events_limit, $LOG_STDERR, $LOG_SYSLOG, $LOG_SYSLOG_FACILITY, $LOG_VERBOSE) = WardenClientConf::loadConf($conf_file); # prepare variables of event my @event = @{$event_ref}; @@ -112,31 +126,38 @@ sub saveNewEvent # create SOAP data object my $event = SOAP::Data->name( event => \SOAP::Data->value( - SOAP::Data->name(SERVICE => $service), - SOAP::Data->name(DETECTED => $detected), - SOAP::Data->name(TYPE => $type), - SOAP::Data->name(SOURCE_TYPE => $source_type), - SOAP::Data->name(SOURCE => $source), + SOAP::Data->name(SERVICE => $service), + SOAP::Data->name(DETECTED => $detected), + SOAP::Data->name(TYPE => $type), + SOAP::Data->name(SOURCE_TYPE => $source_type), + SOAP::Data->name(SOURCE => $source), SOAP::Data->name(TARGET_PROTO => $target_proto), SOAP::Data->name(TARGET_PORT => $target_port), SOAP::Data->name(ATTACK_SCALE => $attack_scale), - SOAP::Data->name(NOTE => $note), - SOAP::Data->name(PRIORITY => $priority), - SOAP::Data->name(TIMEOUT => $timeout) + SOAP::Data->name(NOTE => $note), + SOAP::Data->name(PRIORITY => $priority), + SOAP::Data->name(TIMEOUT => $timeout) ) ); $result = c2s($uri, $ssl_key_file, $ssl_cert_file, $ssl_ca_file, "saveNewEvent", $event); - #$result ? return 1 : return 0; - }; #end of eval block - - if ($@) { - print STDERR "Warden-client unexpected end in eval block: " . $@ . "\n"; - return 0; - } - else { - $result ? return 1 : return 0; + } # End of eval block + or do { + + if ($LOG_STDERR) { + print STDERR "Warden-client unexpected end in eval block.\n" . $@ . "\n"; + } + + if ($LOG_SYSLOG) { + openlog("Warden-client:", "pid", "$LOG_SYSLOG_FACILITY"); + syslog("err|$LOG_SYSLOG_FACILITY", "Warden-client unexpected end in eval block.\n" . $@ . "\n"); + closelog(); } + + return 0; + }; + + $result ? return 1 : return 0; } # End of saveNewEvent