Code

wrong if-clause
[gosa.git] / gosa-si / server / events / clMessages.pm
index dfac6725555c7010e51e5952778697f663a9ff64..7425bfbac4690a6e232317d12598f3c1f3e17e65 100644 (file)
@@ -2,6 +2,7 @@ package clMessages;
 use Exporter;
 @ISA = qw(Exporter);
 my @events = (
+    "confirm_usr_msg",
     "PROGRESS",
     "FAIREBOOT",
     "TASKSKIP",
@@ -14,7 +15,6 @@ my @events = (
     "LOGOUT",
     "CURRENTLY_LOGGED_IN",
     "save_fai_log",
-       "goto_activation_start",
     );
 @EXPORT = @events;
 
@@ -52,11 +52,28 @@ sub get_events {
 }
 
 
+sub confirm_usr_msg {
+    my ($msg, $msg_hash, $session_id) = @_;
+    my $message = @{$msg_hash->{'message'}}[0];
+    my $subject = @{$msg_hash->{'subject'}}[0];
+    my $usr = @{$msg_hash->{'usr'}}[0];
+
+    # set update for this message
+    my $sql = "UPDATE $main::messaging_tn SET flag='s' WHERE (message='$message' AND subject='$subject' AND message_to='$usr')"; 
+    &main::daemon_log("$session_id DEBUG: $sql", 7);
+    my $res = $main::messaging_db->exec_statement($sql); 
+
+
+    return;
+}
+
+
+
 sub read_configfile {
     my ($cfg_file, %cfg_defaults) = @_;
     my $cfg;
 
-    if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
+    if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
         if( -r $cfg_file ) {
             $cfg = Config::IniFiles->new( -file => $cfg_file );
         } else {
@@ -74,21 +91,6 @@ sub read_configfile {
 }
 
 
-sub goto_activation_start {
-       my ($msg, $msg_hash, $session_id) = @_;
-       my $header = @{$msg_hash->{'header'}}[0];
-       my $macaddress = @{$msg_hash->{'macaddress'}}[0];
-       
-    my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', progress='goto-activation' ".
-            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
-    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
-    my $res = $main::job_db->update_dbentry($sql_statement);
-    &main::daemon_log("$session_id INFO: '$header' at '$macaddress'", 5); 
-       
-}
-
-
 sub save_fai_log {
     my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
@@ -111,7 +113,7 @@ sub save_fai_log {
 
     my $time = &get_time;
     $time = substr($time, 0, 8)."_".substr($time, 8, 6);
-    $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, "install-$time" );
+    $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, "install_$time" );
     mkdir($client_fai_log_dir, 0755);
 
     my @all_logs = split(/log_file:/, $all_logs); 
@@ -150,27 +152,16 @@ sub LOGIN {
     return;   
 }
 
-# TODO umstellen wie bei LOGIN
+
 sub LOGOUT {
     my ($msg, $msg_hash, $session_id) = @_;
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'source'}}[0];
     my $login = @{$msg_hash->{$header}}[0];
-
-    my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
-    my $res = $main::known_clients_db->select_dbentry($sql_statement);
-    if( 1 != keys(%$res) ) {
-        &main::daemon_log("DEBUG: clMessages.pm: LOGOUT: no or more hits found in known_clients_db for host '$source'");
-        return;
-    }
-
-    my $act_login = $res->{'1'}->{'login'};
-    $act_login =~ s/$login,?//gi;
-
-    if( $act_login eq "" ){ $act_login = "nobody"; }
-
-    $sql_statement = "UPDATE known_clients SET login='$act_login' WHERE hostname='$source'";
-    $res = $main::known_clients_db->update_dbentry($sql_statement);
+    
+    my $sql_statement = "DELETE FROM $main::login_users_tn WHERE (client='$source' AND user='$login')"; 
+    my $res =  $main::login_users_db->del_dbentry($sql_statement);
+    &main::daemon_log("$session_id INFO: delete user '$login' at client '$source' from login_user_db", 5); 
     
     return;
 }
@@ -238,20 +229,14 @@ sub GOTOACTIVATION {
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
     # test whether content is an empty hash or a string which is required
-#########
-# testing 
     my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-########
 
     # clean up header
     $header =~ s/CLMSG_//g;
 
     my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header"."$content' ".
+            "SET status='processing', progress='goto-activation' ".
             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
     my $res = $main::job_db->update_dbentry($sql_statement);
@@ -267,15 +252,8 @@ sub PROGRESS {
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
-       my $content = @{$msg_hash->{$header}}[0];
+    my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-########
 
     # clean up header
     $header =~ s/CLMSG_//g;
@@ -298,15 +276,8 @@ sub FAIREBOOT {
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
        my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-######### 
 
     # clean up header
     $header =~ s/CLMSG_//g;
@@ -329,15 +300,8 @@ sub TASKSKIP {
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
        my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-#########
 
     # clean up header
     $header =~ s/CLMSG_//g;
@@ -358,23 +322,15 @@ sub TASKBEGIN {
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'target'}}[0];
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
-    my $content = @{$msg_hash->{$header}}[0];
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
        my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-#########
 
     # clean up header
     $header =~ s/CLMSG_//g;
 
-    # check if installation finished
+    # TASKBEGIN eq finish or faiend 
     if (($content eq 'finish') || ($content eq 'faiend')){
         my $sql_statement = "UPDATE $main::job_queue_tn ".
             "SET status='done', result='$header "."$content' ".
@@ -386,14 +342,80 @@ sub TASKBEGIN {
         # set fai_state to localboot
         &main::change_fai_state('localboot', \@{$msg_hash->{'macaddress'}}, $session_id);
 
+       # other TASKBEGIN msgs
     } else {
-        my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content' ".
-            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
-        &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
-        my $res = $main::job_db->update_dbentry($sql_statement);
-        &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
-
+               # select processing jobs for host
+               my $sql_statement = "SELECT * FROM $main::job_queue_tn WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+               &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
+               my $res = $main::job_db->select_dbentry($sql_statement);
+
+               # there is exactly one job entry in queue for this host
+               if (keys(%$res) == 1) {
+                       &main::daemon_log("$session_id DEBUG: there is already one processing job in queue for host '$macaddress', run an update for this entry", 7);
+                       my $sql_statement = "UPDATE $main::job_queue_tn SET result='$header $content' WHERE status='processing' AND macaddress LIKE '$macaddress'";
+                       my $err = $main::job_db->update_dbentry($sql_statement);
+                       if (not defined  $err) {
+                               &main::daemon_log("$session_id ERROR: cannot update job_db entry: ".Dumper($err), 1);
+                       }
+                       
+               # there is no entry or more than one enties
+               } else {
+                       # in case of more than one running jobs in queue, delete all jobs
+                       if (keys(%$res) > 1) {
+                               &main::daemon_log("$session_id DEBUG: there are more than one processing job in queue for host '$macaddress', ".
+                                                               "delete entries", 7); 
+
+                               my $sql_statement = "DELETE FROM $main::job_queue_tn WHERE status='processing' AND macaddress LIKE '$macaddress'";
+                               my ($err) = $main::job_db->del_dbentry($sql_statement);
+                               if (not defined $err) {
+                                       &main::daemon_log("$session_id ERROR: can not delete multiple processing queue entries for host '$macaddress': ".Dumper($err), 1); 
+                               }
+                       }
+               
+                       # in case of no and more than one running jobs in queue, add one single job
+                       # resolve plain name for host $macaddress
+                       my $plain_name;
+                       my $ldap_handle = &main::get_ldap_handle($session_id);
+                       if( not defined $ldap_handle ) {
+                               &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
+                               $plain_name = "none";
+
+                       # try to fetch a 'real name'
+                       } else {
+                               my $mesg = $ldap_handle->search(
+                                               base => $main::ldap_base,
+                                               scope => 'sub',
+                                               attrs => ['cn'],
+                                               filter => "(macAddress=$macaddress)");
+                               if($mesg->code) {
+                                       &main::daemon_log($mesg->error, 1);
+                                       $plain_name = "none";
+                               } else {
+                                       my $entry= $mesg->entry(0);
+                                       $plain_name = $entry->get_value("cn");
+                               }
+                       }
+
+
+                       &main::daemon_log("$session_id DEBUG: add job to queue for host '$macaddress'", 7); 
+                       my $func_dic = {table=>$main::job_queue_tn,
+                                       primkey=>[],
+                                       timestamp=>&get_time,
+                                       status=>'processing',
+                                       result=>"$header $content",
+                                       progress=>'none',
+                                       headertag=>'trigger_action_reinstall',
+                                       targettag=>$source,
+                                       xmlmessage=>'none',
+                                       macaddress=>$macaddress,
+                                       plainname=>$plain_name,
+                       };
+                       my ($err, $error_str) = $main::job_db->add_dbentry($func_dic);
+                       if ($err != 0)  {
+                                       &main::daemon_log("$session_id ERROR: cannot add entry to job_db: $error_str", 1);
+                       }
+
+               }
 
 # -----------------------> Update hier
 #  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
@@ -413,26 +435,28 @@ sub TASKEND {
     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
-       my $content = @{$msg_hash->{$header}}[0];
+    my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-#########
 
     # clean up header
     $header =~ s/CLMSG_//g;
 
-    my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content' ".
-            "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
-    &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
-    my $res = $main::job_db->update_dbentry($sql_statement);
-    &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
+       if ($content eq "savelog 0") {
+               &main::daemon_log("$session_id DEBUG: got savelog from host '$source' - jub done", 7);
+               my $sql_statement = "DELETE FROM $main::job_queue_tn WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+               &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
+               my $res = $main::job_db->del_dbentry($sql_statement);
+
+       } else {
+
+                       my $sql_statement = "UPDATE $main::job_queue_tn ".
+                                       "SET status='processing', result='$header "."$content' ".
+                                       "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
+                       &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
+                       my $res = $main::job_db->update_dbentry($sql_statement);
+                       &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
 
+       }
 # -----------------------> Update hier
 #  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
 #  <header>CLMSG_TASKBEGIN</header>
@@ -453,15 +477,8 @@ sub TASKERROR {
     $header =~ s/CLMSG_//g;
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
-       my $content = @{$msg_hash->{$header}}[0];
-    if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-#########
+    my $content = @{$msg_hash->{$header}}[0];
+    if(ref($content) eq "HASH") { $content = ""; } 
 
        # set fai_state to localboot
        &main::change_fai_state('error', \@{$msg_hash->{'macaddress'}}, $session_id);
@@ -493,15 +510,8 @@ sub HOOK {
     $header =~ s/CLMSG_//g;
 
     # test whether content is an empty hash or a string which is required
-# TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
-#########
-# testing 
        my $content = @{$msg_hash->{$header}}[0];
     if(ref($content) eq "HASH") { $content = ""; }
-    #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
-    #if( $@ ) { $content = "$content"; }
-# testing
-#########
 
     my $sql_statement = "UPDATE $main::job_queue_tn ".
             "SET status='processing', result='$header "."$content' ".