diff --git a/src/warden-server/lib/Warden.pm b/src/warden-server/lib/Warden.pm index 53f31bf882e11216d6d887912c2b7eb9078e17b9..35f928e6ebdfc8f2ebbffffbd8ff16fc748c2e17 100755 --- a/src/warden-server/lib/Warden.pm +++ b/src/warden-server/lib/Warden.pm @@ -38,8 +38,8 @@ our %VALID_STRINGS = undef; # load set variables by user unless (do $conf_file) { - errMsg("Errors in config file '$conf_file': $@") if $@; - errMsg("Can't read config file '$conf_file': $!") unless defined $_; + die("Errors in config file '$conf_file': $@") if $@; + die("Can't read config file '$conf_file': $!") unless defined $_; # if $_ defined, it's retvalue of last statement of conf, for which we don't care } @@ -48,37 +48,13 @@ unless (do $conf_file) { # VARIABLES ################################################################################ -our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1}) || die "Could not connect to database: $DBI::errstr"; +our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1}) || die "Could not connect to database: $DBH->errstr"; ################################################################################ # LOCAL FUNCTIONS ################################################################################ -#------------------------------------------------------------------------------- -# errMsg - print error message and die -#------------------------------------------------------------------------------- -sub errMsg -{ - my $msg = shift; - $msg = trim($msg); - print $msg . "\n"; - exit 1; -} # End of errMsg - - -#------------------------------------------------------------------------------- -# trim - remove whitespace from the start and end of the string -#------------------------------------------------------------------------------- -sub trim -{ - my $string = shift; - $string =~ s/^\s+//; - $string =~ s/\s+$//; - return $string; -} # End of trim - - #------------------------------------------------------------------------------- # write2log - writing message to syslog #------------------------------------------------------------------------------- @@ -139,7 +115,7 @@ sub authorizeClient ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;"); } - if (!defined $sth) { die("Cannot prepare authorization statement in $function_name: $DBI::errstr\n")} + if (!defined $sth) { die("Cannot prepare authorization statement in $function_name: $DBH->errstr\n")} $sth->execute($service_type, $client_type); my ($an, $cidr, $receive_own, $cidr_list); @@ -219,7 +195,7 @@ sub saveNewEvent # if validator is configured, check validity of event attributes - TYPE if (!exists $VALID_STRINGS{'type'} or grep $type eq $_, @{$VALID_STRINGS{'type'}}) { $sth=$DBH->prepare("INSERT INTO events VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"); - if (!defined $sth) { die("Cannot do insert statement in $function_name: $DBI::errstr\n") } + if (!defined $sth) { die("Cannot do insert statement in $function_name: $DBH->errstr\n") } $sth->execute(undef, $client{'dns'}, $service, $detected, $received, $type, $source_type, $source, $target_proto, $target_port, $attack_scale, $note, $priority, $timeout, $valid); return 1; } else { @@ -253,33 +229,38 @@ sub getNewEvents my $max_rcv_events_limit = $data->{'MAX_RCV_EVENTS_LIMIT'}; # client events limit # comparison of client and server limit - which can be used - (defined $max_rcv_events_limit && $max_rcv_events_limit < $MAX_EVENTS_LIMIT) ? my $used_limit = $max_rcv_events_limit : my $used_limit = $MAX_EVENTS_LIMIT; + my $used_limit; + if (defined $max_rcv_events_limit && $max_rcv_events_limit < $MAX_EVENTS_LIMIT) { + $used_limit = $max_rcv_events_limit; + } else { + $used_limit = $MAX_EVENTS_LIMIT; + } my %client = authorizeClient($alt_names, $ip, $requested_type, $client_type, $function_name); if(defined %client) { if ($client{'receive_own'} eq 't') { if ($requested_type eq '_any_') { $sth = $DBH->prepare("SELECT * FROM events WHERE type != 'test' AND id > ? AND valid = 't' ORDER BY id ASC LIMIT ?;"); - if (!defined $sth) {die("Cannot prepare ROE-ANY statement in $function_name: $DBI::errstr\n")} - my $rows = $sth->execute($last_id, $used_limit); + if (!defined $sth) {die("Cannot prepare ROE-ANY statement in $function_name: $DBH->errstr\n")} + $sth->execute($last_id, $used_limit); } else { $sth = $DBH->prepare("SELECT * FROM events WHERE type != 'test' AND id > ? AND type = ? AND valid = 't' ORDER BY id ASC LIMIT ?;"); - if (!defined $sth) {die("Cannot prepare ROE statement in $function_name: $DBI::errstr\n")} - my $rows = $sth->execute($last_id, $requested_type, $used_limit); + if (!defined $sth) {die("Cannot prepare ROE statement in $function_name: $DBH->errstr\n")} + $sth->execute($last_id, $requested_type, $used_limit); } } else { if ($requested_type eq '_any_') { $sth = $DBH->prepare("SELECT * FROM events WHERE type != 'test' AND id > ? AND valid = 't' AND hostname NOT LIKE ? ORDER BY id ASC LIMIT ?;"); - if (!defined $sth) {die("Cannot prepare ANY statement in $function_name: $DBI::errstr\n")} + if (!defined $sth) {die("Cannot prepare ANY statement in $function_name: $DBH->errstr\n")} my ($domain) = $cn =~ /([^\.]+\.[^\.]+)$/; $domain = '\%' . $domain; - my $rows = $sth->execute($last_id, $domain, $used_limit); + $sth->execute($last_id, $domain, $used_limit); } else { $sth = $DBH->prepare("SELECT * FROM events WHERE type != 'test' AND id > ? AND type = ? AND valid = 't' AND hostname NOT LIKE ? ORDER BY id ASC LIMIT ?;"); - if (!defined $sth) {die("Cannot prepare statement in $function_name: $DBI::errstr\n")} + if (!defined $sth) {die("Cannot prepare statement in $function_name: $DBH->errstr\n")} my ($domain) = $cn =~ /([^\.]+\.[^\.]+)$/; $domain = '\%' . $domain; - my $rows = $sth->execute($last_id, $requested_type, $domain, $used_limit); + $sth->execute($last_id, $requested_type, $domain, $used_limit); } } @@ -341,7 +322,7 @@ sub getLastId my $function_name = 'getLastId'; my $sth = $DBH->prepare("SELECT max(id) FROM events;"); - if ( !defined $sth ) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if ( !defined $sth ) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; my $result = $sth->fetchrow(); @@ -383,7 +364,7 @@ sub registerSender # check if sender has been already registered $sth = $DBH->prepare("SELECT registered FROM clients WHERE hostname = ? AND requestor = ? AND service = ? AND client_type = ? AND type = ? AND receive_own_events = ? AND description_tags = ? AND ip_net_client = ? LIMIT 1;"); - if (!defined $sth) {die("Cannot prepare check statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot prepare check statement in '$function_name': $DBH->errstr\n")} $sth->execute($hostname, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client); my $result = $sth->fetchrow(); @@ -393,7 +374,7 @@ sub registerSender die("Error - sender has already been registered at '$result'"); } else { $sth = $DBH->prepare("INSERT INTO clients VALUES (?,?,?,?,?,?,?,?,?,?);"); - if (!defined $sth) {die("Cannot do statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot do statement in '$function_name': $DBH->errstr\n")} $sth->execute(undef, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client); write2log("info", "New sender '$hostname' (service: '$service', cidr: '$ip_net_client') was registered"); return 1; @@ -436,7 +417,7 @@ sub registerReceiver # check if receiver has been already registered $sth = $DBH->prepare("SELECT registered FROM clients WHERE hostname = ? AND requestor = ? AND service = ? AND client_type = ? AND type = ? AND receive_own_events = ? AND description_tags = ? AND ip_net_client = ? LIMIT 1;"); - if (!defined $sth) {die("Cannot prepare check statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot prepare check statement in '$function_name': $DBH->errstr\n")} $sth->execute($hostname, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client); my $result = $sth->fetchrow(); @@ -446,7 +427,7 @@ sub registerReceiver die("Error - receiver has already been registered at '$result'"); } else { $sth = $DBH->prepare("INSERT INTO clients VALUES (?,?,?,?,?,?,?,?,?,?);"); - if (!defined($sth)) {die("Cannot do statement in '$function_name': $DBI::errstr\n")} + if (!defined($sth)) {die("Cannot do statement in '$function_name': $DBH->errstr\n")} $sth->execute(undef, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client); write2log("info", "New receiver '$hostname' (type: '$type', cidr: '$ip_net_client', receive_own_events: '$receive_own_events') was registered"); return 1; @@ -479,7 +460,7 @@ sub unregisterClient # check if receiver has been already registered $sth = $DBH->prepare("SELECT client_id, hostname, service, client_type FROM clients WHERE client_id = ? LIMIT 1;"); - if (!defined $sth) {die("Cannot prepare check statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot prepare check statement in '$function_name': $DBH->errstr\n")} $sth->execute($client_id); my ($id, $hostname, $service, $client_type) = $sth->fetchrow(); @@ -490,18 +471,18 @@ sub unregisterClient } else { if ($client_type eq 's') { $sth = $DBH->prepare("DELETE FROM clients WHERE client_id = ?;"); - if (!defined $sth) {die("Cannot do delete statement of sender in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot do delete statement of sender in '$function_name': $DBH->errstr\n")} $sth->execute($client_id); $sth = $DBH->prepare("UPDATE events SET valid = 'f' where hostname = ? AND service = ?;"); - if (!defined $sth) {die("Cannot do unvalidation statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot do unvalidation statement in '$function_name': $DBH->errstr\n")} $sth->execute($hostname, $service); write2log("info", "Sender '$hostname' (client_id: '$client_id', service: '$service') was deleted and its data were invalidated" ); return 1; } else { $sth = $DBH->prepare("DELETE FROM clients WHERE client_id = ?;"); - if (!defined $sth) {die("Cannot do delete statement of receiver in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot do delete statement of receiver in '$function_name': $DBH->errstr\n")} $sth->execute($client_id); write2log("info", "Receiver '$hostname' (client_id: '$client_id') was deleted" ); @@ -533,7 +514,7 @@ sub getClients my (@clients, $client); my ($client_id, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client); my $sth = $DBH->prepare("SELECT * FROM clients ORDER BY client_id ASC;"); - if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; while ( my @result = $sth->fetchrow() ) { @@ -606,35 +587,35 @@ sub getStatus # sum of records in table events $sth = $DBH->prepare("SELECT count(*) FROM events WHERE valid = 't';"); - if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; my $events_sum = $sth->fetchrow(); if (!defined $events_sum) { $events_sum = "none" } # id of last record in table events $sth = $DBH->prepare("SELECT max(id) FROM events;"); - if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; my $events_last_id = $sth->fetchrow(); if (!defined $events_last_id) { $events_last_id = "none" } # timestamp of first record in table events $sth = $DBH->prepare("SELECT received FROM events WHERE id = (SELECT min(id) FROM events);"); - if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; my $events_first_timestamp = $sth->fetchrow(); if (!defined $events_first_timestamp) { $events_first_timestamp = "none" } # timestamp of last record in table events $sth = $DBH->prepare("SELECT received FROM events WHERE id = (SELECT max(id) FROM events);"); - if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; my $events_last_timestamp = $sth->fetchrow(); if (!defined $events_last_timestamp) { $events_last_timestamp = "none" } # sum of records in table clients $sth = $DBH->prepare("SELECT count(*) FROM clients;"); - if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBI::errstr\n") } + if (!defined $sth) { die("Cannot prepare statement in '$function_name': $DBH->errstr\n") } $sth->execute; my $clients_sum = $sth->fetchrow(); if (!defined $clients_sum) { $clients_sum = "none" } @@ -660,7 +641,7 @@ sub getStatus # statistics of senders if ($clients_sum != 0) { $sth = $DBH->prepare("SELECT client_id, hostname, service FROM clients WHERE client_type = 's' ORDER BY client_id ASC;"); - if (!defined $sth) {die("Cannot prepare statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth) {die("Cannot prepare statement in '$function_name': $DBH->errstr\n")} $sth->execute; my ($client_id, $hostname, $service); my $client_status; @@ -668,13 +649,13 @@ sub getStatus my $sth2; # sum of stored events $sth2 = $DBH->prepare("SELECT count(*) FROM events WHERE hostname = ? AND service = ?;"); - if (!defined $sth2) {die("Cannot prepare statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth2) {die("Cannot prepare statement in '$function_name': $DBH->errstr\n")} $sth2->execute($hostname, $service); my $count = $sth2->fetchrow(); if (!defined $count) {$count = "none"} # timestamp of last stored event $sth2 = $DBH->prepare("SELECT max(received) FROM events WHERE hostname = ? AND service = ?;"); - if (!defined $sth2) {die("Cannot prepare statement in '$function_name': $DBI::errstr\n")} + if (!defined $sth2) {die("Cannot prepare statement in '$function_name': $DBH->errstr\n")} $sth2->execute($hostname, $service); my $timestamp = $sth2->fetchrow(); if (!defined $timestamp) {$timestamp = "none"}