Code

Move information to 90_gosa.conf
[gosa.git] / gosa-si / server / events / clMessages.pm
index 6e26295b598807aa1da4e7ce656d35ea1544c74c..6c1e0546003c00552f7cacaeac071b4fece9f073 100644 (file)
@@ -75,7 +75,10 @@ sub save_fai_log {
     my $all_logs = @{$msg_hash->{$header}}[0];
 
     # if there is nothing to log
-    if( ref($all_logs) eq "HASH" ) { return; }
+    if( ref($all_logs) eq "HASH" ) { 
+        &main::daemon_log("$session_id INFO: There is nothing to log!", 5);
+        return; 
+    }
         
     my $client_fai_log_dir = $main::client_fai_log_dir;
     if (not -d $client_fai_log_dir) {
@@ -98,9 +101,11 @@ sub save_fai_log {
         my ($log_file, $log_string) = split(":", $log);
         my $client_fai_log_file = File::Spec->catfile( $client_fai_log_dir, $log_file);
 
-       open(my $LOG_FILE, ">$client_fai_log_file"); 
-        print $LOG_FILE &decode_base64($log_string);
-        close($LOG_FILE);
+               open(my $LOG_FILE, ">$client_fai_log_file"); 
+               print $LOG_FILE &decode_base64($log_string);
+               close($LOG_FILE);
+               chown($main::root_uid, $main::adm_gid, $client_fai_log_file);
+               chmod(0440, $client_fai_log_file);
 
     }
     return;
@@ -116,20 +121,31 @@ sub LOGIN {
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'source'}}[0];
     my $login = @{$msg_hash->{$header}}[0];
+    my $res;
+    my $error_str;
+
+    # Invoke set_last_system; message sets ldap attributes 'gotoLastSystemLogin' and 'gotoLastSystem'
+       $res = &set_last_system($msg, $msg_hash, $session_id);
 
+    # Add user to login_users_db
     my %add_hash = ( table=>$main::login_users_tn, 
         primkey=> ['client', 'user'],
         client=>$source,
         user=>$login,
         timestamp=>&get_time,
+        regserver=>'localhost',
         ); 
-    my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
+    ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
     if ($res != 0)  {
         &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
         return;
     }
 
-    return;   
+    # Share login information with all other si-server
+    my %data = ( 'new_user'  => "$source;$login" );
+    my $info_msg = &build_msg("information_sharing", $main::server_address, "KNOWN_SERVER", \%data);
+
+    return ($info_msg);  
 }
 
 
@@ -169,7 +185,7 @@ sub CURRENTLY_LOGGED_IN {
         return;     
     }
 
-    # Invoke set_last_system
+    # Invoke set_last_system; message sets ldap attributes 'gotoLastSystemLogin' and 'gotoLastSystem'
        my $res = &set_last_system($msg, $msg_hash, $session_id);
     
     # fetch all user currently assigned to the client at login_users_db
@@ -190,6 +206,7 @@ sub CURRENTLY_LOGGED_IN {
                 client=>$source,
                 user=>$user,
                 timestamp=>&get_time,
+                regserver=>'localhost',
                 ); 
         my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
         if ($res != 0)  {
@@ -225,12 +242,21 @@ sub CURRENTLY_LOGGED_IN {
         &main::daemon_log("$session_id INFO: delete user '".$hit->{'user'}."' at client '".$hit->{'client'}."' from login_user_db", 5); 
     }
 
-    return;
+    # Inform all other server which users are logged in at clients registered at local server
+    my $info_sql = "SELECT * FROM $main::login_users_tn WHERE regserver='localhost'";
+    my $info_res = $main::login_users_db->select_dbentry($info_sql);
+    my $info_msg_hash = &create_xml_hash("information_sharing", $main::server_address, "KNOWN_SERVER");
+    while (my ($hit_id, $hit) = each(%$info_res)) {
+        &add_content2xml_hash($info_msg_hash, 'user_db', $hit->{'client'}.";".$hit->{'user'});
+    }
+    my $info_msg = &create_xml_string($info_msg_hash);
+
+    return ($info_msg);  
 }
 
 
 ## @method set_last_system()
-# @details Message set ldap attributes 'gosaLastSystemLogin' and 'gosaLastSystem'
+# @details Message sets ldap attributes 'gotoLastSystemLogin' and 'gotoLastSystem'
 # @param msg - STRING - xml message with tag 'last_system_login' and 'last_system'
 # @param msg_hash - HASHREF - message information parsed into a hash
 # @param session_id - INTEGER - POE session id of the processing of this message
@@ -243,25 +269,25 @@ sub set_last_system {
        # Sanity check of needed parameter
        if (not exists $msg_hash->{'timestamp'}){
                &main::daemon_log("$session_id ERROR: message does not contain needed xml tag 'timestamp', ".
-                                               "setting of 'gosaLastSystem' and 'gosaLastSystemLogin' stopped!", 1);
+                                               "setting of 'gotoLastSystem' and 'gotoLastSystemLogin' stopped!", 1);
                &main::daemon_log($msg, 1);
                return;
        }
        if (@{$msg_hash->{'timestamp'}} != 1)  {
                &main::daemon_log("$session_id ERROR: xml tag 'timestamp' has no content or exists more than one time, ".
-                                               "setting of 'gosaLastSystem' and 'gosaLastSystemLogin' stopped!", 1);
+                                               "setting of 'gotoLastSystem' and 'gotoLastSystemLogin' stopped!", 1);
                &ymain::daemon_log($msg, 1);
                return;
        }
        if (not exists $msg_hash->{'macaddress'}){
                &main::daemon_log("$session_id ERROR: message does not contain needed xml tag 'mac_address', ".
-                                               "setting of 'gosaLastSystem' and 'gosaLastSystemLogin' stopped!", 1);
+                                               "setting of 'gotoLastSystem' and 'gotoLastSystemLogin' stopped!", 1);
                &main::daemon_log($msg, 1);
                return;
        }
        if (@{$msg_hash->{'macaddress'}} != 1)  {
                &main::daemon_log("$session_id ERROR: xml tag 'macaddress' has no content or exists more than one time, ".
-                                               "setting of 'gosaLastSystem' and 'gosaLastSystemLogin' stopped!", 1);
+                                               "setting of 'gotoLastSystem' and 'gotoLastSystemLogin' stopped!", 1);
                &ymain::daemon_log($msg, 1);
                return;
        }
@@ -280,10 +306,8 @@ sub set_last_system {
                return;
        }
 
-       # Fetch ldap handle
-       my $ldap_handle = &main::get_ldap_handle();
-
        # Get system info
+       my $ldap_handle=&main::get_ldap_handle($session_id);
        my $ldap_mesg= $ldap_handle->search(
                                        base => $main::ldap_base,
                                        scope => 'sub',
@@ -291,51 +315,75 @@ sub set_last_system {
                                        );
        if ($ldap_mesg->count == 0) {
                &main::daemon_log("$session_id ERROR: no system with mac address='$mac' was found in base '".
-                                               $main::ldap_base."', setting of 'gosaLastSystem' and 'gosaLastSystemLogin' stopped!", 1);
+                                               $main::ldap_base."', setting of 'gotoLastSystem' and 'gotoLastSystemLogin' stopped!", 1);
+               &main::release_ldap_handle($ldap_handle);
                return;
        }
+
        my $ldap_entry = $ldap_mesg->pop_entry();
-       my $system_dn = $ldap_entry->get_value('dn');
+       my $system_dn = $ldap_entry->dn();
        
-       # For each logged in user set gosaLastSystem and gosaLastSystemLogin
+       # For each logged in user set gotoLastSystem and gotoLastSystemLogin
        foreach my $user (@login_list) {
                # Search user
                my $ldap_mesg= $ldap_handle->search(
                                                base => $main::ldap_base,
                                                scope => 'sub',
-                                               filter => "uid=$user",
+                                               filter => "(&(objectClass=gosaAccount)(uid=$user))",
                                                );
                # Sanity check of user search
                if ($ldap_mesg->count == 0) {
                        &main::daemon_log("$session_id ERROR: no user with uid='$user' was found in base '".
-                                                       $main::ldap_base."', setting of 'gosaLastSystem' and 'gosaLastSystemLogin' stopped!", 1);
+                                                       $main::ldap_base."', setting of 'gotoLastSystem' and 'gotoLastSystemLogin' stopped!", 1);
 
-               # Set gosaLastSystem and gosaLastSystemLogin
+               # Set gotoLastSystem and gotoLastSystemLogin
                } else {
                        my $ldap_entry= $ldap_mesg->pop_entry();
-                       if (defined($ldap_entry->get_value('gosaLastSystem'))) {
-                                       $ldap_entry->replace ( 'gosaLastSystem' => $system_dn );
-                       } else {
-                                       $ldap_entry->add( 'gosaLastSystem' => $system_dn );
-                    &main::daemon_log("$session_id INFO: ldap entry 'uid=$user' do not know attribute 'gosaLastSystem', add attribute!");
-                       }
-                       if (defined($ldap_entry->get_value('gosaLastSystemLogin'))) {
-                                       $ldap_entry->replace ( 'gosaLastSystemLogin' => $timestamp );
-                       } else {
-                                       $ldap_entry->add( 'gosaLastSystemLogin' => $timestamp );
-                    &main::daemon_log("$session_id INFO: ldap entry 'uid=$user' do not know attribute 'gosaLastSystemLogin', add attribute!");
-                       }
-                       my $result = $ldap_entry->update($ldap_handle);
-                       if ($result->code() != 0) {
-                                       &main::daemon_log("$session_id ERROR: setting 'gosaLastSystem' and 'gosaLastSystemLogin' at user '$user' failed: ".
-                                                                       $result->{'errorMessage'}."\n".
+            my $do_update = 0;
+
+            # Set gotoLastSystem information
+            my $last_system_dn = $ldap_entry->get_value('gotoLastSystem');
+            if ((defined $last_system_dn) && ($last_system_dn eq $system_dn)) {
+                &main::daemon_log("$session_id INFO: no new 'gotoLastSystem' information for ldap entry 'uid=$user', do nothing!", 5);
+            } elsif ((defined $last_system_dn) && ($last_system_dn ne $system_dn)) {
+                $ldap_entry->replace ( 'gotoLastSystem' => $system_dn );
+                &main::daemon_log("$session_id INFO: update attribute 'gotoLastSystem'='$system_dn' at ldap entry 'uid=$user'!",5);
+                $do_update++;
+            } else {
+                $ldap_entry->add( 'gotoLastSystem' => $system_dn );
+                &main::daemon_log("$session_id INFO: add attribute 'gotoLastSystem'='$system_dn' at ldap entry 'uid=$user'!", 5);
+                $do_update++;
+            }
+
+            # Set gotoLastSystemLogin information
+            # Attention: only write information if last_system_dn and system_dn differs
+            my $last_system_login = $ldap_entry->get_value('gotoLastSystemLogin');
+            if ((defined $last_system_login) && ($last_system_dn eq $system_dn)) {
+                &main::daemon_log("$session_id INFO: no new 'gotoLastSystemLogin' information for ldap entry 'uid=$user', do nothing!", 5);
+            } elsif ((defined $last_system_login) && ($last_system_dn ne $system_dn)) {
+                $ldap_entry->replace ( 'gotoLastSystemLogin' => $timestamp );
+                &main::daemon_log("$session_id INFO: update attribute 'gotoLastSystemLogin'='$timestamp' at ldap entry 'uid=$user'!", 5);
+                $do_update++;
+            } else {
+                $ldap_entry->add( 'gotoLastSystemLogin' => $timestamp );
+                &main::daemon_log("$session_id INFO: add attribute 'gotoLastSystemLogin'='$timestamp' at ldap entry 'uid=$user'!",5);
+                $do_update++;
+            }
+
+            if ($do_update) {
+                my $result = $ldap_entry->update($ldap_handle);
+                if ($result->code() != 0) {
+                    &main::daemon_log("$session_id ERROR: setting 'gotoLastSystem' and 'gotoLastSystemLogin' at user '$user' failed: ".
+                            $result->{'errorMessage'}."\n".
                             "\tbase: $main::ldap_base\n".
                             "\tscope: 'sub'\n".
                             "\tfilter: 'uid=$user'\n".
                             "\tmessage: $msg", 1); 
-                       }
+                }
+            }
                }
        }
+       &main::release_ldap_handle($ldap_handle);
 
        return;
 }
@@ -360,7 +408,7 @@ sub GOTOACTIVATION {
     $header =~ s/CLMSG_//g;
 
     my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', progress='goto-activation', modified='1' ".
+            "SET progress='goto-activation', modified='1' ".
             "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);
@@ -417,7 +465,7 @@ sub FAIREBOOT {
     $header =~ s/CLMSG_//g;
 
     my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content', modified='1' ".
+            "SET result='$header "."$content', modified='1' ".
             "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);
@@ -446,7 +494,7 @@ sub TASKSKIP {
     $header =~ s/CLMSG_//g;
 
     my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content', modified='1' ".
+            "SET result='$header "."$content', modified='1' ".
             "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);
@@ -500,7 +548,14 @@ sub TASKBEGIN {
 
        # other TASKBEGIN msgs
     } else {
-               # select processing jobs for host
+               # TASKBEGIN msgs do only occour during a softupdate or a reinstallation
+               # of a host. Set all waiting update- or reinstall-jobs for host to
+               # processing so they can be handled correctly by the rest of the function.
+               my $waiting_sql = "UPDATE $main::job_queue_tn SET status='processing' WHERE status='waiting' AND macaddress LIKE '$macaddress' AND (headertag='trigger_action_update' OR headertag='trigger_action_reinstall')"; 
+               &main::daemon_log("$session_id DEBUB: $waiting_sql", 7);
+               my $waiting_res = $main::job_db->update_dbentry($waiting_sql);
+
+               # 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);
@@ -508,17 +563,15 @@ sub TASKBEGIN {
                # 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', modified='1', siserver='localhost' ".
-                "WHERE status='processing' AND macaddress LIKE '$macaddress'";
+                       my $sql_statement = "UPDATE $main::job_queue_tn SET result='$header $content', modified='1', siserver='localhost' 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
+               # There is no entry in queue or more than one entries in queue for this host
                } else {
-                       # in case of more than one running jobs in queue, delete all jobs
+                       # 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); 
@@ -532,29 +585,24 @@ sub TASKBEGIN {
 
                        }
                
-                       # 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";
+                       # In case of no and more than one running jobs in queue, add one single job
 
-                       # try to fetch a 'real name'
+                       # Resolve plain name for host $macaddress
+                       my $plain_name;
+                       my $ldap_handle=&main::get_ldap_handle($session_id);
+                       my $mesg = $ldap_handle->search(
+                                       base => $main::ldap_base,
+                                       scope => 'sub',
+                                       attrs => ['cn'],
+                                       filter => "(macAddress=$macaddress)");
+                       if(not $mesg->code) {
+                               my $entry= $mesg->entry(0);
+                               $plain_name = $entry->get_value("cn");
                        } 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($mesg->error, 1);
+                               $plain_name = "none";
                        }
+                       &main::release_ldap_handle($ldap_handle);
 
             # In any case add a new job to job queue
                        &main::daemon_log("$session_id DEBUG: add job to queue for host '$macaddress'", 7); 
@@ -614,7 +662,7 @@ sub TASKEND {
 
        } else {
         my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content', modified='1' ".
+            "SET result='$header "."$content', modified='1' ".
             "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);
@@ -647,7 +695,7 @@ sub TASKERROR {
        &main::change_fai_state('error', \@{$msg_hash->{'macaddress'}}, $session_id);
                
     my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content', modified='1' ".
+            "SET result='$header "."$content', modified='1' ".
             "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);
@@ -676,7 +724,7 @@ sub HOOK {
     if(not ref($content) eq "STRING") { $content = ""; }
 
     my $sql_statement = "UPDATE $main::job_queue_tn ".
-            "SET status='processing', result='$header "."$content', modified='1' ".
+            "SET result='$header "."$content', modified='1' ".
             "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);