d72da53347a4ef478fe019696d4628c946f2ddb2
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 utf8;
25 use MIME::Base64;
28 BEGIN {}
30 END {}
32 ### Start ######################################################################
34 my $ldap_uri;
35 my $ldap_base;
36 my $ldap_admin_dn;
37 my $ldap_admin_password;
39 my %cfg_defaults = (
40 "server" => {
41 "ldap-uri" => [\$ldap_uri, ""],
42 "ldap-base" => [\$ldap_base, ""],
43 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
44 "ldap-admin-password" => [\$ldap_admin_password, ""],
45 },
46 );
47 &read_configfile($main::cfg_file, %cfg_defaults);
50 sub get_events {
51 return \@events;
52 }
55 sub read_configfile {
56 my ($cfg_file, %cfg_defaults) = @_;
57 my $cfg;
59 if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
60 if( -r $cfg_file ) {
61 $cfg = Config::IniFiles->new( -file => $cfg_file );
62 } else {
63 &main::daemon_log("ERROR: clMessages.pm couldn't read config file!", 1);
64 }
65 } else {
66 $cfg = Config::IniFiles->new() ;
67 }
68 foreach my $section (keys %cfg_defaults) {
69 foreach my $param (keys %{$cfg_defaults{ $section }}) {
70 my $pinfo = $cfg_defaults{ $section }{ $param };
71 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
72 }
73 }
74 }
77 sub save_fai_log {
78 my ($msg, $msg_hash, $session_id) = @_;
79 my $header = @{$msg_hash->{'header'}}[0];
80 my $source = @{$msg_hash->{'source'}}[0];
81 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
82 my $all_logs = @{$msg_hash->{$header}}[0];
84 # if there is nothing to log
85 if( ref($all_logs) eq "HASH" ) { return; }
87 my $client_fai_log_dir = $main::client_fai_log_dir;
88 if (not -d $client_fai_log_dir) {
89 mkdir($client_fai_log_dir, 0755)
90 }
92 $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, $macaddress );
93 if (not -d $client_fai_log_dir) {
94 mkdir($client_fai_log_dir, 0755)
95 }
97 my $time = &get_time;
98 $time = substr($time, 0, 8)."_".substr($time, 8, 6);
99 $client_fai_log_dir = File::Spec->catfile( $client_fai_log_dir, "install-$time" );
100 mkdir($client_fai_log_dir, 0755);
102 my @all_logs = split(/log_file:/, $all_logs);
103 foreach my $log (@all_logs) {
104 if (length $log == 0) { next; };
105 my ($log_file, $log_string) = split("\n", $log, 2);
106 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);
110 }
111 return;
112 }
115 sub LOGIN {
116 my ($msg, $msg_hash, $session_id) = @_;
117 my $header = @{$msg_hash->{'header'}}[0];
118 my $source = @{$msg_hash->{'source'}}[0];
119 my $login = @{$msg_hash->{$header}}[0];
121 my %add_hash = ( table=>$main::login_users_tn,
122 primkey=> ['client', 'user'],
123 client=>$source,
124 user=>$login,
125 timestamp=>&get_time,
126 );
127 my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
128 if ($res != 0) {
129 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
130 return;
131 }
133 return;
134 }
136 # TODO umstellen wie bei LOGIN
137 sub LOGOUT {
138 my ($msg, $msg_hash, $session_id) = @_;
139 my $header = @{$msg_hash->{'header'}}[0];
140 my $source = @{$msg_hash->{'source'}}[0];
141 my $login = @{$msg_hash->{$header}}[0];
143 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
144 my $res = $main::known_clients_db->select_dbentry($sql_statement);
145 if( 1 != keys(%$res) ) {
146 &main::daemon_log("DEBUG: clMessages.pm: LOGOUT: no or more hits found in known_clients_db for host '$source'");
147 return;
148 }
150 my $act_login = $res->{'1'}->{'login'};
151 $act_login =~ s/$login,?//gi;
153 if( $act_login eq "" ){ $act_login = "nobody"; }
155 $sql_statement = "UPDATE known_clients SET login='$act_login' WHERE hostname='$source'";
156 $res = $main::known_clients_db->update_dbentry($sql_statement);
158 return;
159 }
162 sub CURRENTLY_LOGGED_IN {
163 my ($msg, $msg_hash, $session_id) = @_;
164 my ($sql_statement, $db_res);
165 my $header = @{$msg_hash->{'header'}}[0];
166 my $source = @{$msg_hash->{'source'}}[0];
167 my $login = @{$msg_hash->{$header}}[0];
169 $sql_statement = "SELECT * FROM $main::login_users_tn WHERE client='$source'";
170 $db_res = $main::login_users_db->select_dbentry($sql_statement);
171 my %currently_logged_in_user = ();
172 while( my($hit_id, $hit) = each(%{$db_res}) ) {
173 $currently_logged_in_user{$hit->{'user'}} = 1;
174 }
175 &main::daemon_log("$session_id DEBUG: logged in users from login_user_db: ".join(", ", keys(%currently_logged_in_user)), 7);
177 my @logged_in_user = split(/\s+/, $login);
178 &main::daemon_log("$session_id DEBUG: logged in users reported from client: ".join(", ", @logged_in_user), 7);
179 foreach my $user (@logged_in_user) {
180 my %add_hash = ( table=>$main::login_users_tn,
181 primkey=> ['client', 'user'],
182 client=>$source,
183 user=>$user,
184 timestamp=>&get_time,
185 );
186 my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
187 if ($res != 0) {
188 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $error_str");
189 return;
190 }
192 delete $currently_logged_in_user{$user};
193 }
195 # if there is still a user in %currently_logged_in_user
196 # although he is not reported by client
197 # then delete it from $login_user_db
198 foreach my $obsolete_user (keys(%currently_logged_in_user)) {
199 &main::daemon_log("$session_id WARNING: user '$obsolete_user' is currently not logged ".
200 "in at client '$source' but still found at login_user_db", 3);
201 my $sql_statement = "DELETE FROM $main::login_users_tn WHERE client='$source' AND user='$obsolete_user'";
202 my $res = $main::login_users_db->del_dbentry($sql_statement);
203 &main::daemon_log("$session_id WARNING: delete user '$obsolete_user' at client '$source' from login_user_db", 3);
204 }
206 return;
207 }
210 sub GOTOACTIVATION {
211 my ($msg, $msg_hash, $session_id) = @_;
212 my $header = @{$msg_hash->{'header'}}[0];
213 my $source = @{$msg_hash->{'target'}}[0];
214 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
216 # test whether content is an empty hash or a string which is required
217 my $content = @{$msg_hash->{$header}}[0];
218 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
219 if( $@ ) { $content = "$content"; }
221 # clean up header
222 $header =~ s/CLMSG_//g;
224 my $sql_statement = "UPDATE $main::job_queue_tn ".
225 "SET status='processing', result='$header"."$content' ".
226 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
227 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
228 my $res = $main::job_db->update_dbentry($sql_statement);
229 &main::daemon_log("$session_id INFO: $header at '$macaddress'", 5);
230 return;
231 }
234 sub PROGRESS {
235 my ($msg, $msg_hash, $session_id) = @_;
236 my $header = @{$msg_hash->{'header'}}[0];
237 my $source = @{$msg_hash->{'target'}}[0];
238 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
240 # test whether content is an empty hash or a string which is required
241 my $content;
242 my $cont = @{$msg_hash->{$header}}[0];
243 eval{ if( 0 == keys(%$cont) ) { $content = ""; } };
244 if( $@ ) { $content = "$cont"; }
246 # clean up header
247 $header =~ s/CLMSG_//g;
249 my $sql_statement = "UPDATE $main::job_queue_tn ".
250 "SET progress='$content' ".
251 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
252 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
253 my $res = $main::job_db->update_dbentry($sql_statement);
254 &main::daemon_log("$session_id INFO: $header at '$macaddress' - $content%", 5);
256 return;
257 }
260 sub FAIREBOOT {
261 my ($msg, $msg_hash, $session_id) = @_;
262 my $header = @{$msg_hash->{'header'}}[0];
263 my $source = @{$msg_hash->{'target'}}[0];
264 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
266 # test whether content is an empty hash or a string which is required
267 my $content = @{$msg_hash->{$header}}[0];
268 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
269 if( $@ ) { $content = "$content"; }
271 # clean up header
272 $header =~ s/CLMSG_//g;
274 my $sql_statement = "UPDATE $main::job_queue_tn ".
275 "SET status='processing', result='$header "."$content' ".
276 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
277 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
278 my $res = $main::job_db->update_dbentry($sql_statement);
279 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
281 return;
282 }
285 sub TASKSKIP {
286 my ($msg, $msg_hash, $session_id) = @_;
287 my $header = @{$msg_hash->{'header'}}[0];
288 my $source = @{$msg_hash->{'target'}}[0];
289 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
291 # test whether content is an empty hash or a string which is required
292 my $content = @{$msg_hash->{$header}}[0];
293 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
294 if( $@ ) { $content = "$content"; }
296 # clean up header
297 $header =~ s/CLMSG_//g;
299 my $sql_statement = "UPDATE $main::job_queue_tn ".
300 "SET status='processing', result='$header "."$content' ".
301 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
302 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
303 my $res = $main::job_db->update_dbentry($sql_statement);
304 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
306 return;
307 }
310 sub TASKBEGIN {
311 my ($msg, $msg_hash, $session_id) = @_;
312 my $header = @{$msg_hash->{'header'}}[0];
313 my $source = @{$msg_hash->{'target'}}[0];
314 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
315 my $content = @{$msg_hash->{$header}}[0];
317 # test whether content is an empty hash or a string which is required
318 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
319 if( $@ ) { $content = "$content"; }
321 # clean up header
322 $header =~ s/CLMSG_//g;
324 # check if installation finished
325 if (($content eq 'finish') || ($content eq 'faiend')){
326 my $sql_statement = "UPDATE $main::job_queue_tn ".
327 "SET status='done', result='$header "."$content' ".
328 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
329 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
330 my $res = $main::job_db->update_dbentry($sql_statement);
331 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
333 # set fai_state to localboot
334 &main::change_fai_state('localboot', \@{$msg_hash->{target}}, $session_id);
336 } else {
337 my $sql_statement = "UPDATE $main::job_queue_tn ".
338 "SET status='processing', result='$header "."$content' ".
339 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
340 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
341 my $res = $main::job_db->update_dbentry($sql_statement);
342 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
345 # -----------------------> Update hier
346 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
347 # <header>CLMSG_TASKBEGIN</header>
348 # macaddress auslesen, Client im LDAP lokalisieren
349 # FAIstate auf "localboot" setzen, wenn FAIstate "install" oder "softupdate" war
350 }
352 return;
353 }
356 sub TASKEND {
357 my ($msg, $msg_hash, $session_id) = @_;
358 my $header = @{$msg_hash->{'header'}}[0];
359 my $source = @{$msg_hash->{'target'}}[0];
360 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
362 # test whether content is an empty hash or a string which is required
363 my $content = @{$msg_hash->{$header}}[0];
364 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
365 if( $@ ) { $content = "$content"; }
367 # clean up header
368 $header =~ s/CLMSG_//g;
370 my $sql_statement = "UPDATE $main::job_queue_tn ".
371 "SET status='processing', result='$header "."$content' ".
372 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
373 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
374 my $res = $main::job_db->update_dbentry($sql_statement);
375 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
377 # -----------------------> Update hier
378 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
379 # <header>CLMSG_TASKBEGIN</header>
380 # macaddress auslesen, Client im LDAP lokalisieren
381 # FAIstate auf "error" setzen
383 return;
384 }
387 sub TASKERROR {
388 my ($msg, $msg_hash, $session_id) = @_;
389 my $header = @{$msg_hash->{'header'}}[0];
390 my $source = @{$msg_hash->{'target'}}[0];
391 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
393 # clean up header
394 $header =~ s/CLMSG_//g;
396 # test whether content is an empty hash or a string which is required
397 my $content = @{$msg_hash->{$header}}[0];
398 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
399 if( $@ ) { $content = "$content"; }
401 my $sql_statement = "UPDATE $main::job_queue_tn ".
402 "SET status='processing', result='$header "."$content' ".
403 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
404 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
405 my $res = $main::job_db->update_dbentry($sql_statement);
406 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
408 # -----------------------> Update hier
409 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
410 # <header>CLMSG_TASKBEGIN</header>
411 # macaddress auslesen, Client im LDAP lokalisieren
412 # FAIstate auf "error" setzen
414 return;
415 }
418 sub HOOK {
419 my ($msg, $msg_hash, $session_id) = @_;
420 my $header = @{$msg_hash->{'header'}}[0];
421 my $source = @{$msg_hash->{'target'}}[0];
422 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
424 # clean up header
425 $header =~ s/CLMSG_//g;
427 # test whether content is an empty hash or a string which is required
428 my $content = @{$msg_hash->{$header}}[0];
429 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
430 if( $@ ) { $content = "$content"; }
432 my $sql_statement = "UPDATE $main::job_queue_tn ".
433 "SET status='processing', result='$header "."$content' ".
434 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
435 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
436 my $res = $main::job_db->update_dbentry($sql_statement);
437 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
439 return;
440 }
443 1;