From 0e0982f17a111cad64ece36150c7e62730e421ec Mon Sep 17 00:00:00 2001
From: Tomas Plesnik <plesnik@ics.muni.cz>
Date: Wed, 14 Nov 2012 15:44:03 +0100
Subject: [PATCH] pridana nova funkce getClientInfo, pridana autorizace v
 getLastId, autorizacni proces vraci i client_id

---
 src/warden-server/lib/Warden.pm | 124 +++++++++++++++++++++++++++-----
 1 file changed, 106 insertions(+), 18 deletions(-)

diff --git a/src/warden-server/lib/Warden.pm b/src/warden-server/lib/Warden.pm
index 7e3b751..3386996 100755
--- a/src/warden-server/lib/Warden.pm
+++ b/src/warden-server/lib/Warden.pm
@@ -123,26 +123,39 @@ sub authorizeClient
 
   # obtain cidr based on rigth common name and alternate names, service and client_type
   if($function_name eq 'saveNewEvent') {
-    $sth = $DBH->prepare("SELECT hostname, ip_net_client, receive_own_events FROM clients WHERE hostname IN ($alt_names) AND service = ? AND client_type = ? ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;");
+    $sth = $DBH->prepare("SELECT client_id, hostname, ip_net_client, receive_own_events FROM clients WHERE hostname IN ($alt_names) AND service = ? AND client_type = ? ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;");
   } elsif($function_name eq 'getNewEvents') {
-    $sth = $DBH->prepare("SELECT hostname, ip_net_client, receive_own_events FROM clients WHERE hostname IN ($alt_names) AND (type = ? OR type = '_any_') AND client_type = ? ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;");
-  }
-
+    $sth = $DBH->prepare("SELECT client_id, hostname, ip_net_client, receive_own_events FROM clients WHERE hostname IN ($alt_names) AND (type = ? OR type = '_any_') AND client_type = ? ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;");
+  } elsif($function_name eq 'getClientInfo') {
+    $sth = $DBH->prepare("SELECT client_id, hostname, ip_net_client, receive_own_events FROM clients WHERE hostname IN ($alt_names) ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;");
+  } elsif($function_name eq 'getLastId') {
+    $sth = $DBH->prepare("SELECT client_id, hostname, ip_net_client, receive_own_events FROM clients WHERE hostname IN ($alt_names) AND client_type = 'r' ORDER BY SUBSTRING_INDEX(ip_net_client,'/', -1) DESC;");
+ }
+
+  # check db handler
   if (!defined $sth) {
     sendMsg("err",
            "Cannot prepare authorization statement in $function_name: $DBH->errstr",
 	   "Internal 'prepare' server error")
   }
-  $sth->execute($service_type, $client_type);
 
-  my ($an, $cidr, $receive_own, $cidr_list);
+  # execute query for two or none params functions
+  if ($function_name eq 'saveNewEvent' || $function_name eq 'getNewEvents') {
+    $sth->execute($service_type, $client_type);
+  } else {
+    $sth->execute;
+  }
+
+  # obtain registration info about clients
+  my ($client_id, $an, $cidr, $receive_own, $cidr_list);
   my $correct_ip_source = 0;
   my %ret;
-  while(($an, $cidr, $receive_own)  = $sth->fetchrow()) {
+  while(($client_id, $an, $cidr, $receive_own)  = $sth->fetchrow()) {
     my $cidr_list = Net::CIDR::Lite-> new -> add($cidr);
 
-    $ret{'dns'} = $an;
-    $ret{'cidr'} = $cidr;
+    $ret{'client_id'}	= $client_id;
+    $ret{'dns'}		= $an;
+    $ret{'cidr'}	= $cidr;
     $ret{'receive_own'} = $receive_own;
 
     if ($cidr_list->bin_find($ip)) {
@@ -385,18 +398,29 @@ sub getNewEvents
 sub getLastId
 {
   my ($class, $arg) = @_;
+
+  # client network information
+  my $cn	= $ENV{'SSL_CLIENT_S_DN_CN'};
+  my $alt_names	= getAltNames(undef);
+  my $ip	= $ENV{'REMOTE_ADDR'};
+
+  my $service		= undef;
+  my $client_type	= undef;
+
   my $function_name = 'getLastId';
 
-  my $sth = $DBH->prepare("SELECT max(id) FROM events;");
-  if ( !defined $sth ) {
-    sendMsg("err",
-           "Cannot prepare statement in function '$function_name': $DBH->errstr",
-	   "Internal 'prepare' server error");
+  my %client = authorizeClient($alt_names, $ip, $service, $client_type, $function_name);
+  if (defined %client) {
+    my $sth = $DBH->prepare("SELECT max(id) FROM events;");
+    if (!defined $sth) {
+      sendMsg("err",
+              "Cannot prepare statement in function '$function_name': $DBH->errstr",
+              "Internal 'prepare' server error");
+    }
+    $sth->execute;
+    my $result = $sth->fetchrow();
+    return $result;
   }
-  $sth->execute;
-  my $result = $sth->fetchrow();
-
-  return $result;
 } # END of getLastID
 
 
@@ -816,4 +840,68 @@ sub getStatus
   }
 } # END of getStatus
 
+
+#-------------------------------------------------------------------------------
+# getClientInfo
+#-------------------------------------------------------------------------------
+sub getClientInfo
+{
+  my ($class, $data) = @_;
+  my (@clients, $client);
+  my ($client_id, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client);
+
+  # client network information
+  my $cn	= $ENV{'SSL_CLIENT_S_DN_CN'};
+  my $alt_names	= getAltNames(undef);
+  my $ip	= $ENV{'REMOTE_ADDR'};
+
+  my $service		= undef;
+  my $client_type	= undef;
+
+  my $function_name = 'getClientInfo';
+
+  my %client = authorizeClient($alt_names, $ip, $service, $client_type, $function_name);
+  if (defined %client) {
+    my $sth = $DBH->prepare("SELECT * FROM clients ORDER BY client_id ASC;");
+    if (!defined $sth) {
+      sendMsg("err",
+            "Cannot prepare statement in function '$function_name': $DBH->errstr",
+            "Internal 'prepare' server error");
+    }
+    $sth->execute;
+
+    while ( my @result = $sth->fetchrow() ) {
+      $client_id		= $result[0];
+      $hostname			= $result[1];
+      $registered		= $result[2];
+      $requestor		= $result[3];
+      $service			= $result[4];
+      $client_type		= $result[5];
+      $type 			= $result[6];
+      $receive_own_events	= $result[7];
+      $description_tags		= $result[8];
+      $ip_net_client		= $result[9];
+
+      $client = SOAP::Data->name(client => \SOAP::Data->value(
+      SOAP::Data->name(CLIENT_ID		=> $client_id),
+      SOAP::Data->name(HOSTNAME			=> $hostname),
+      SOAP::Data->name(REGISTERED		=> $registered),
+      SOAP::Data->name(REQUESTOR		=> $requestor),
+      SOAP::Data->name(SERVICE			=> $service),
+      SOAP::Data->name(CLIENT_TYPE		=> $client_type),
+      SOAP::Data->name(TYPE			=> $type),
+      SOAP::Data->name(RECEIVE_OWN_EVENTS	=> $receive_own_events),
+      SOAP::Data->name(DESCRIPTION_TAGS		=> $description_tags),
+      SOAP::Data->name(IP_NET_CLIENT		=> $ip_net_client),
+      ));
+      push(@clients, $client);
+    }
+    my $sum = scalar @clients;
+    sendMsg("info",
+            "Sending information about '$sum' registered clients from $ENV{'SERVER_NAME'}",
+            undef);
+    return @clients;
+  }
+} # END of getClientInfo
+
 1;
-- 
GitLab