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; }
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;
113 }
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;
135 }
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);
159 return;
160 }
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 }
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;
215 }
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 my $content = @{$msg_hash->{$header}}[0];
226 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
227 if( $@ ) { $content = "$content"; }
229 # clean up header
230 $header =~ s/CLMSG_//g;
232 my $sql_statement = "UPDATE $main::job_queue_tn ".
233 "SET status='processing', result='$header"."$content' ".
234 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
235 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
236 my $res = $main::job_db->update_dbentry($sql_statement);
237 &main::daemon_log("$session_id INFO: $header at '$macaddress'", 5);
238 return;
239 }
242 sub PROGRESS {
243 my ($msg, $msg_hash, $session_id) = @_;
244 my $header = @{$msg_hash->{'header'}}[0];
245 my $source = @{$msg_hash->{'target'}}[0];
246 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
248 # test whether content is an empty hash or a string which is required
249 my $content;
250 my $cont = @{$msg_hash->{$header}}[0];
251 eval{ if( 0 == keys(%$cont) ) { $content = ""; } };
252 if( $@ ) { $content = "$cont"; }
254 # clean up header
255 $header =~ s/CLMSG_//g;
257 my $sql_statement = "UPDATE $main::job_queue_tn ".
258 "SET progress='$content' ".
259 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
260 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
261 my $res = $main::job_db->update_dbentry($sql_statement);
262 &main::daemon_log("$session_id INFO: $header at '$macaddress' - $content%", 5);
264 return;
265 }
268 sub FAIREBOOT {
269 my ($msg, $msg_hash, $session_id) = @_;
270 my $header = @{$msg_hash->{'header'}}[0];
271 my $source = @{$msg_hash->{'target'}}[0];
272 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
274 # test whether content is an empty hash or a string which is required
275 my $content = @{$msg_hash->{$header}}[0];
276 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
277 if( $@ ) { $content = "$content"; }
279 # clean up header
280 $header =~ s/CLMSG_//g;
282 my $sql_statement = "UPDATE $main::job_queue_tn ".
283 "SET status='processing', result='$header "."$content' ".
284 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
285 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
286 my $res = $main::job_db->update_dbentry($sql_statement);
287 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
289 return;
290 }
293 sub TASKSKIP {
294 my ($msg, $msg_hash, $session_id) = @_;
295 my $header = @{$msg_hash->{'header'}}[0];
296 my $source = @{$msg_hash->{'target'}}[0];
297 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
299 # test whether content is an empty hash or a string which is required
300 my $content = @{$msg_hash->{$header}}[0];
301 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
302 if( $@ ) { $content = "$content"; }
304 # clean up header
305 $header =~ s/CLMSG_//g;
307 my $sql_statement = "UPDATE $main::job_queue_tn ".
308 "SET status='processing', result='$header "."$content' ".
309 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
310 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
311 my $res = $main::job_db->update_dbentry($sql_statement);
312 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
314 return;
315 }
318 sub TASKBEGIN {
319 my ($msg, $msg_hash, $session_id) = @_;
320 my $header = @{$msg_hash->{'header'}}[0];
321 my $source = @{$msg_hash->{'target'}}[0];
322 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
323 my $content = @{$msg_hash->{$header}}[0];
325 # test whether content is an empty hash or a string which is required
326 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
327 if( $@ ) { $content = "$content"; }
329 # clean up header
330 $header =~ s/CLMSG_//g;
332 # check if installation finished
333 if (($content eq 'finish') || ($content eq 'faiend')){
334 my $sql_statement = "UPDATE $main::job_queue_tn ".
335 "SET status='done', result='$header "."$content' ".
336 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
337 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
338 my $res = $main::job_db->update_dbentry($sql_statement);
339 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
341 # set fai_state to localboot
342 &main::change_fai_state('localboot', \@{$msg_hash->{'macaddress'}}, $session_id);
344 } else {
345 my $sql_statement = "UPDATE $main::job_queue_tn ".
346 "SET status='processing', result='$header "."$content' ".
347 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
348 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
349 my $res = $main::job_db->update_dbentry($sql_statement);
350 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
353 # -----------------------> Update hier
354 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
355 # <header>CLMSG_TASKBEGIN</header>
356 # macaddress auslesen, Client im LDAP lokalisieren
357 # FAIstate auf "localboot" setzen, wenn FAIstate "install" oder "softupdate" war
358 }
360 return;
361 }
364 sub TASKEND {
365 my ($msg, $msg_hash, $session_id) = @_;
366 my $header = @{$msg_hash->{'header'}}[0];
367 my $source = @{$msg_hash->{'target'}}[0];
368 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
370 # test whether content is an empty hash or a string which is required
371 my $content = @{$msg_hash->{$header}}[0];
372 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
373 if( $@ ) { $content = "$content"; }
375 # clean up header
376 $header =~ s/CLMSG_//g;
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);
385 # -----------------------> Update hier
386 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
387 # <header>CLMSG_TASKBEGIN</header>
388 # macaddress auslesen, Client im LDAP lokalisieren
389 # FAIstate auf "error" setzen
391 return;
392 }
395 sub TASKERROR {
396 my ($msg, $msg_hash, $session_id) = @_;
397 my $header = @{$msg_hash->{'header'}}[0];
398 my $source = @{$msg_hash->{'target'}}[0];
399 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
401 # clean up header
402 $header =~ s/CLMSG_//g;
404 # test whether content is an empty hash or a string which is required
405 my $content = @{$msg_hash->{$header}}[0];
406 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
407 if( $@ ) { $content = "$content"; }
409 my $sql_statement = "UPDATE $main::job_queue_tn ".
410 "SET status='processing', result='$header "."$content' ".
411 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
412 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
413 my $res = $main::job_db->update_dbentry($sql_statement);
414 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
416 # -----------------------> Update hier
417 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
418 # <header>CLMSG_TASKBEGIN</header>
419 # macaddress auslesen, Client im LDAP lokalisieren
420 # FAIstate auf "error" setzen
422 return;
423 }
426 sub HOOK {
427 my ($msg, $msg_hash, $session_id) = @_;
428 my $header = @{$msg_hash->{'header'}}[0];
429 my $source = @{$msg_hash->{'target'}}[0];
430 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
432 # clean up header
433 $header =~ s/CLMSG_//g;
435 # test whether content is an empty hash or a string which is required
436 my $content = @{$msg_hash->{$header}}[0];
437 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
438 if( $@ ) { $content = "$content"; }
440 my $sql_statement = "UPDATE $main::job_queue_tn ".
441 "SET status='processing', result='$header "."$content' ".
442 "WHERE status='processing' AND macaddress LIKE '$macaddress'";
443 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
444 my $res = $main::job_db->update_dbentry($sql_statement);
445 &main::daemon_log("$session_id INFO: $header at '$macaddress' - '$content'", 5);
447 return;
448 }
451 1;