Code

5cc1a4ea002c4c145d60120bc1ee71b2fdc95aa7
[gosa.git] / gosa-si / server / events / clMessages.pm
1 package clMessages;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5     "PROGRESS",
6     "FAIREBOOT",
7     "TASKSKIP",
8     "TASKBEGIN",
9     "TASKEND",
10     "TASKERROR",
11     "HOOK",
12     "GOTOACTIVATION",
13     "LOGIN",
14     "LOGOUT",
15     "CURRENTLY_LOGGED_IN",
16     "save_fai_log",
17     );
18 @EXPORT = @events;
20 use strict;
21 use warnings;
22 use Data::Dumper;
23 use GOSA::GosaSupportDaemon;
24 use MIME::Base64;
27 BEGIN {}
29 END {}
31 ### Start ######################################################################
33 my $ldap_uri;
34 my $ldap_base;
35 my $ldap_admin_dn;
36 my $ldap_admin_password;
38 my %cfg_defaults = (
39 "server" => {
40    "ldap-uri" => [\$ldap_uri, ""],
41    "ldap-base" => [\$ldap_base, ""],
42    "ldap-admin-dn" => [\$ldap_admin_dn, ""],
43    "ldap-admin-password" => [\$ldap_admin_password, ""],
44    },
45 );
46 &read_configfile($main::cfg_file, %cfg_defaults);
49 sub get_events {
50     return \@events;
51 }
54 sub read_configfile {
55     my ($cfg_file, %cfg_defaults) = @_;
56     my $cfg;
58     if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
59         if( -r $cfg_file ) {
60             $cfg = Config::IniFiles->new( -file => $cfg_file );
61         } else {
62             &main::daemon_log("ERROR: clMessages.pm couldn't read config file!", 1);
63         }
64     } else {
65         $cfg = Config::IniFiles->new() ;
66     }
67     foreach my $section (keys %cfg_defaults) {
68         foreach my $param (keys %{$cfg_defaults{ $section }}) {
69             my $pinfo = $cfg_defaults{ $section }{ $param };
70             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
71         }
72     }
73 }
76 sub save_fai_log {
77     my ($msg, $msg_hash, $session_id) = @_;
78     my $header = @{$msg_hash->{'header'}}[0];
79     my $source = @{$msg_hash->{'source'}}[0];
80     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
81     my $all_logs = @{$msg_hash->{$header}}[0];
83     # if there is nothing to log
84     if( ref($all_logs) eq "HASH" ) { return; }
85         
86     my $client_fai_log_dir = $main::client_fai_log_dir;
87     if (not -d $client_fai_log_dir) {
88         mkdir($client_fai_log_dir, 0755)
89     }
91     $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, $macaddress );
92     if (not -d $client_fai_log_dir) {
93         mkdir($client_fai_log_dir, 0755)
94     }
96     my $time = &get_time;
97     $time = substr($time, 0, 8)."_".substr($time, 8, 6);
98     $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, "install-$time" );
99     mkdir($client_fai_log_dir, 0755);
101     my @all_logs = split(/log_file:/, $all_logs); 
102     foreach my $log (@all_logs) {
103         if (length $log == 0) { next; };
104         my ($log_file, $log_string) = split(":", $log);
105         my $client_fai_log_file = File::Spec->catfile( $client_fai_log_dir, $log_file);
107         open(my $LOG_FILE, ">$client_fai_log_file"); 
108         print $LOG_FILE &decode_base64($log_string);
109         close($LOG_FILE);
111     }
112     return;
116 sub LOGIN {
117     my ($msg, $msg_hash, $session_id) = @_;
118     my $header = @{$msg_hash->{'header'}}[0];
119     my $source = @{$msg_hash->{'source'}}[0];
120     my $login = @{$msg_hash->{$header}}[0];
122     my %add_hash = ( table=>$main::login_users_tn, 
123         primkey=> ['client', 'user'],
124         client=>$source,
125         user=>$login,
126         timestamp=>&get_time,
127         ); 
128     my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
129     if ($res != 0)  {
130         &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
131         return;
132     }
134     return;   
137 # TODO umstellen wie bei LOGIN
138 sub LOGOUT {
139     my ($msg, $msg_hash, $session_id) = @_;
140     my $header = @{$msg_hash->{'header'}}[0];
141     my $source = @{$msg_hash->{'source'}}[0];
142     my $login = @{$msg_hash->{$header}}[0];
144     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
145     my $res = $main::known_clients_db->select_dbentry($sql_statement);
146     if( 1 != keys(%$res) ) {
147         &main::daemon_log("DEBUG: clMessages.pm: LOGOUT: no or more hits found in known_clients_db for host '$source'");
148         return;
149     }
151     my $act_login = $res->{'1'}->{'login'};
152     $act_login =~ s/$login,?//gi;
154     if( $act_login eq "" ){ $act_login = "nobody"; }
156     $sql_statement = "UPDATE known_clients SET login='$act_login' WHERE hostname='$source'";
157     $res = $main::known_clients_db->update_dbentry($sql_statement);
158     
159     return;
163 sub CURRENTLY_LOGGED_IN {
164     my ($msg, $msg_hash, $session_id) = @_;
165     my ($sql_statement, $db_res);
166     my $header = @{$msg_hash->{'header'}}[0];
167     my $source = @{$msg_hash->{'source'}}[0];
168     my $login = @{$msg_hash->{$header}}[0];
170     if(ref $login eq "HASH") { 
171         &main::daemon_log("$session_id INFO: no logged in users reported from host '$source'", 5); 
172         return;     
173     }
174     
175     # fetch all user currently assigned to the client at login_users_db
176     my %currently_logged_in_user = (); 
177     $sql_statement = "SELECT * FROM $main::login_users_tn WHERE client='$source'"; 
178     $db_res = $main::login_users_db->select_dbentry($sql_statement);
179     while( my($hit_id, $hit) = each(%{$db_res}) ) {
180         $currently_logged_in_user{$hit->{'user'}} = 1;
181     }
182     &main::daemon_log("$session_id DEBUG: logged in users from login_user_db: ".join(", ", keys(%currently_logged_in_user)), 7); 
184     # update all reported users in login_user_db
185     my @logged_in_user = split(/\s+/, $login);
186     &main::daemon_log("$session_id DEBUG: logged in users reported from client: ".join(", ", @logged_in_user), 7); 
187     foreach my $user (@logged_in_user) {
188         my %add_hash = ( table=>$main::login_users_tn, 
189                 primkey=> ['client', 'user'],
190                 client=>$source,
191                 user=>$user,
192                 timestamp=>&get_time,
193                 ); 
194         my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
195         if ($res != 0)  {
196             &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
197             return;
198         }
200         delete $currently_logged_in_user{$user};
201     }
203     # if there is still a user in %currently_logged_in_user 
204     # although he is not reported by client 
205     # then delete it from $login_user_db
206     foreach my $obsolete_user (keys(%currently_logged_in_user)) {
207         &main::daemon_log("$session_id WARNING: user '$obsolete_user' is currently not logged ".
208                 "in at client '$source' but still found at login_user_db", 3); 
209         my $sql_statement = "DELETE FROM $main::login_users_tn WHERE client='$source' AND user='$obsolete_user'"; 
210         my $res =  $main::login_users_db->del_dbentry($sql_statement);
211         &main::daemon_log("$session_id WARNING: delete user '$obsolete_user' at client '$source' from login_user_db", 3); 
212     }
214     return;
218 sub GOTOACTIVATION {
219     my ($msg, $msg_hash, $session_id) = @_;
220     my $header = @{$msg_hash->{'header'}}[0];
221     my $source = @{$msg_hash->{'target'}}[0];
222     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
224     # test whether content is an empty hash or a string which is required
225 #########
226 # testing 
227         my $content = @{$msg_hash->{$header}}[0];
228     if(ref($content) eq "HASH") { $content = ""; }
229     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
230     #if( $@ ) { $content = "$content"; }
231 # testing
232 ########
234     # clean up header
235     $header =~ s/CLMSG_//g;
237     my $sql_statement = "UPDATE $main::job_queue_tn ".
238             "SET status='processing', result='$header"."$content' ".
239             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
240     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
241     my $res = $main::job_db->update_dbentry($sql_statement);
242     &main::daemon_log("$session_id INFO: $header at '$macaddress'", 5); 
243     return; 
247 sub PROGRESS {
248     my ($msg, $msg_hash, $session_id) = @_;
249     my $header = @{$msg_hash->{'header'}}[0];
250     my $source = @{$msg_hash->{'target'}}[0];
251     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
253     # test whether content is an empty hash or a string which is required
254     my $content;
255     my $cont = @{$msg_hash->{$header}}[0];
256 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
257 #########
258 # testing 
259         my $content = @{$msg_hash->{$header}}[0];
260     if(ref($content) eq "HASH") { $content = ""; }
261     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
262     #if( $@ ) { $content = "$content"; }
263 # testing
264 ########
266     # clean up header
267     $header =~ s/CLMSG_//g;
269     my $sql_statement = "UPDATE $main::job_queue_tn ".
270         "SET progress='$content' ".
271         "WHERE status='processing' AND macaddress LIKE '$macaddress'";
272     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
273     my $res = $main::job_db->update_dbentry($sql_statement);
274     &main::daemon_log("$session_id INFO: $header at '$macaddress' - $content%", 5); 
276     return;
280 sub FAIREBOOT {
281     my ($msg, $msg_hash, $session_id) = @_;
282     my $header = @{$msg_hash->{'header'}}[0];
283     my $source = @{$msg_hash->{'target'}}[0];
284     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
286     # test whether content is an empty hash or a string which is required
287     my $content = @{$msg_hash->{$header}}[0];
288 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
289 #########
290 # testing 
291         my $content = @{$msg_hash->{$header}}[0];
292     if(ref($content) eq "HASH") { $content = ""; }
293     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
294     #if( $@ ) { $content = "$content"; }
295 # testing
296 ######### 
298     # clean up header
299     $header =~ s/CLMSG_//g;
301     my $sql_statement = "UPDATE $main::job_queue_tn ".
302             "SET status='processing', result='$header "."$content' ".
303             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
304     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
305     my $res = $main::job_db->update_dbentry($sql_statement);
306     &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
308     return; 
312 sub TASKSKIP {
313     my ($msg, $msg_hash, $session_id) = @_;
314     my $header = @{$msg_hash->{'header'}}[0];
315     my $source = @{$msg_hash->{'target'}}[0];
316     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
318     # test whether content is an empty hash or a string which is required
319     my $content = @{$msg_hash->{$header}}[0];
320 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
321 #########
322 # testing 
323         my $content = @{$msg_hash->{$header}}[0];
324     if(ref($content) eq "HASH") { $content = ""; }
325     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
326     #if( $@ ) { $content = "$content"; }
327 # testing
328 #########
330     # clean up header
331     $header =~ s/CLMSG_//g;
333     my $sql_statement = "UPDATE $main::job_queue_tn ".
334             "SET status='processing', result='$header "."$content' ".
335             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
336     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
337     my $res = $main::job_db->update_dbentry($sql_statement);
338     &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
340     return; 
344 sub TASKBEGIN {
345     my ($msg, $msg_hash, $session_id) = @_;
346     my $header = @{$msg_hash->{'header'}}[0];
347     my $source = @{$msg_hash->{'target'}}[0];
348     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
349     my $content = @{$msg_hash->{$header}}[0];
351     # test whether content is an empty hash or a string which is required
352 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
353 #########
354 # testing 
355         my $content = @{$msg_hash->{$header}}[0];
356     if(ref($content) eq "HASH") { $content = ""; }
357     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
358     #if( $@ ) { $content = "$content"; }
359 # testing
360 #########
362     # clean up header
363     $header =~ s/CLMSG_//g;
365     # check if installation finished
366     if (($content eq 'finish') || ($content eq 'faiend')){
367         my $sql_statement = "UPDATE $main::job_queue_tn ".
368             "SET status='done', result='$header "."$content' ".
369             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
370         &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
371         my $res = $main::job_db->update_dbentry($sql_statement);
372         &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
373         
374         # set fai_state to localboot
375         &main::change_fai_state('localboot', \@{$msg_hash->{'macaddress'}}, $session_id);
377     } else {
378         my $sql_statement = "UPDATE $main::job_queue_tn ".
379             "SET status='processing', result='$header "."$content' ".
380             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
381         &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
382         my $res = $main::job_db->update_dbentry($sql_statement);
383         &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
386 # -----------------------> Update hier
387 #  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
388 #  <header>CLMSG_TASKBEGIN</header>
389 # macaddress auslesen, Client im LDAP lokalisieren
390 # FAIstate auf "localboot" setzen, wenn FAIstate "install" oder "softupdate" war
391     }
393     return; 
397 sub TASKEND {
398     my ($msg, $msg_hash, $session_id) = @_;
399     my $header = @{$msg_hash->{'header'}}[0];
400     my $source = @{$msg_hash->{'target'}}[0];
401     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
403     # test whether content is an empty hash or a string which is required
404     my $content = @{$msg_hash->{$header}}[0];
405 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
406 #########
407 # testing 
408         my $content = @{$msg_hash->{$header}}[0];
409     if(ref($content) eq "HASH") { $content = ""; }
410     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
411     #if( $@ ) { $content = "$content"; }
412 # testing
413 #########
415     # clean up header
416     $header =~ s/CLMSG_//g;
418     my $sql_statement = "UPDATE $main::job_queue_tn ".
419             "SET status='processing', result='$header "."$content' ".
420             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
421     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
422     my $res = $main::job_db->update_dbentry($sql_statement);
423     &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
425 # -----------------------> Update hier
426 #  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
427 #  <header>CLMSG_TASKBEGIN</header>
428 # macaddress auslesen, Client im LDAP lokalisieren
429 # FAIstate auf "error" setzen
431     return; 
435 sub TASKERROR {
436     my ($msg, $msg_hash, $session_id) = @_;
437     my $header = @{$msg_hash->{'header'}}[0];
438     my $source = @{$msg_hash->{'target'}}[0];
439     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
441     # clean up header
442     $header =~ s/CLMSG_//g;
444     # test whether content is an empty hash or a string which is required
445     my $content = @{$msg_hash->{$header}}[0];
446 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
447 #########
448 # testing 
449         my $content = @{$msg_hash->{$header}}[0];
450     if(ref($content) eq "HASH") { $content = ""; }
451     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
452     #if( $@ ) { $content = "$content"; }
453 # testing
454 #########
456         # set fai_state to localboot
457         &main::change_fai_state('error', \@{$msg_hash->{'macaddress'}}, $session_id);
458                 
459     my $sql_statement = "UPDATE $main::job_queue_tn ".
460             "SET status='processing', result='$header "."$content' ".
461             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
462     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
463     my $res = $main::job_db->update_dbentry($sql_statement);
464     &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
466 # -----------------------> Update hier
467 #  <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
468 #  <header>CLMSG_TASKBEGIN</header>
469 # macaddress auslesen, Client im LDAP lokalisieren
470 # FAIstate auf "error" setzen
472     return; 
476 sub HOOK {
477     my ($msg, $msg_hash, $session_id) = @_;
478     my $header = @{$msg_hash->{'header'}}[0];
479     my $source = @{$msg_hash->{'target'}}[0];
480     my $macaddress = @{$msg_hash->{'macaddress'}}[0];
482     # clean up header
483     $header =~ s/CLMSG_//g;
485     # test whether content is an empty hash or a string which is required
486     my $content = @{$msg_hash->{$header}}[0];
488 # TODO eval ändern auf if(ref($content) eq "HASH") dann ... else, dann...
489 #########
490 # testing 
491         my $content = @{$msg_hash->{$header}}[0];
492     if(ref($content) eq "HASH") { $content = ""; }
493     #eval{ if( 0 == keys(%$content) ) { $content = ""; } };
494     #if( $@ ) { $content = "$content"; }
495 # testing
496 #########
498     my $sql_statement = "UPDATE $main::job_queue_tn ".
499             "SET status='processing', result='$header "."$content' ".
500             "WHERE status='processing' AND macaddress LIKE '$macaddress'"; 
501     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);         
502     my $res = $main::job_db->update_dbentry($sql_statement);
503     &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5); 
505     return;
509 1;