0ec31d5c177fe4ab75446118aaa8bf5abdf190d5
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 );
17 @EXPORT = @events;
19 use strict;
20 use warnings;
21 use Data::Dumper;
22 use GOSA::GosaSupportDaemon;
23 use utf8;
26 BEGIN {}
28 END {}
30 ### Start ######################################################################
32 my $ldap_uri;
33 my $ldap_base;
34 my $ldap_admin_dn;
35 my $ldap_admin_password;
37 my %cfg_defaults = (
38 "server" => {
39 "ldap-uri" => [\$ldap_uri, ""],
40 "ldap-base" => [\$ldap_base, ""],
41 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
42 "ldap-admin-password" => [\$ldap_admin_password, ""],
43 },
44 );
45 &read_configfile($main::cfg_file, %cfg_defaults);
48 sub get_events {
49 return \@events;
50 }
53 sub read_configfile {
54 my ($cfg_file, %cfg_defaults) = @_;
55 my $cfg;
57 if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
58 if( -r $cfg_file ) {
59 $cfg = Config::IniFiles->new( -file => $cfg_file );
60 } else {
61 &main::daemon_log("ERROR: clMessages.pm couldn't read config file!", 1);
62 }
63 } else {
64 $cfg = Config::IniFiles->new() ;
65 }
66 foreach my $section (keys %cfg_defaults) {
67 foreach my $param (keys %{$cfg_defaults{ $section }}) {
68 my $pinfo = $cfg_defaults{ $section }{ $param };
69 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
70 }
71 }
72 }
75 sub LOGIN {
76 my ($msg, $msg_hash, $session_id) = @_;
77 my $header = @{$msg_hash->{'header'}}[0];
78 my $source = @{$msg_hash->{'source'}}[0];
79 my $login = @{$msg_hash->{$header}}[0];
81 my %add_hash = ( table=>$main::login_users_tn,
82 primkey=> ['client', 'user'],
83 client=>$source,
84 user=>$login,
85 timestamp=>&get_time,
86 );
87 my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
88 if ($res != 0) {
89 &main::daemon_log("ERROR: cannot add entry to known_clients: $error_str");
90 return;
91 }
93 return;
94 }
96 # TODO umstellen wie bei LOGIN
97 sub LOGOUT {
98 my ($msg, $msg_hash, $session_id) = @_;
99 my $header = @{$msg_hash->{'header'}}[0];
100 my $source = @{$msg_hash->{'source'}}[0];
101 my $login = @{$msg_hash->{$header}}[0];
103 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
104 my $res = $main::known_clients_db->select_dbentry($sql_statement);
105 if( 1 != keys(%$res) ) {
106 &main::daemon_log("DEBUG: clMessages.pm: LOGOUT: no or more hits found in known_clients_db for host '$source'");
107 return;
108 }
110 my $act_login = $res->{'1'}->{'login'};
111 $act_login =~ s/$login,?//gi;
113 if( $act_login eq "" ){ $act_login = "nobody"; }
115 $sql_statement = "UPDATE known_clients SET login='$act_login' WHERE hostname='$source'";
116 $res = $main::known_clients_db->update_dbentry($sql_statement);
118 return;
119 }
122 sub CURRENTLY_LOGGED_IN {
123 my ($msg, $msg_hash, $session_id) = @_;
124 my ($sql_statement, $db_res);
125 my $header = @{$msg_hash->{'header'}}[0];
126 my $source = @{$msg_hash->{'source'}}[0];
127 my $login = @{$msg_hash->{$header}}[0];
129 $sql_statement = "SELECT * FROM $main::login_users_tn WHERE client='$source'";
130 $db_res = $main::login_users_db->select_dbentry($sql_statement);
131 my %currently_logged_in_user = ();
132 while( my($hit_id, $hit) = each(%{$db_res}) ) {
133 $currently_logged_in_user{$hit->{'user'}} = 1;
134 }
135 &main::daemon_log("DEBUG: logged in users from login_user_db: ".join(", ", keys(%currently_logged_in_user)), 7);
137 my @logged_in_user = split(/\s+/, $login);
138 &main::daemon_log("DEBUG: logged in users reported from client: ".join(", ", @logged_in_user), 7);
139 foreach my $user (@logged_in_user) {
140 my %add_hash = ( table=>$main::login_users_tn,
141 primkey=> ['client', 'user'],
142 client=>$source,
143 user=>$user,
144 timestamp=>&get_time,
145 );
146 my ($res, $error_str) = $main::login_users_db->add_dbentry( \%add_hash );
147 if ($res != 0) {
148 &main::daemon_log("ERROR: cannot add entry to known_clients: $error_str");
149 return;
150 }
152 delete $currently_logged_in_user{$user};
153 }
155 # if there is still a user in %currently_logged_in_user
156 # although he is not reported by client
157 # then delete it from $login_user_db
158 foreach my $obsolete_user (keys(%currently_logged_in_user)) {
159 &main::daemon_log("WARNING: user '$obsolete_user' is currently not logged ".
160 "in at client '$source' but still found at login_user_db", 3);
161 my $sql_statement = "DELETE FROM $main::login_users_tn WHERE client='$source' AND user='$obsolete_user'";
162 my $res = $main::login_users_db->del_dbentry($sql_statement);
163 &main::daemon_log("WARNING: delete user '$obsolete_user' at client '$source' from login_user_db", 3);
164 }
166 return;
167 }
170 sub GOTOACTIVATION {
171 my ($msg, $msg_hash, $session_id) = @_;
172 my $header = @{$msg_hash->{'header'}}[0];
173 my $source = @{$msg_hash->{'target'}}[0];
174 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
176 # test whether content is an empty hash or a string which is required
177 my $content = @{$msg_hash->{$header}}[0];
178 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
179 if( $@ ) { $content = "$content"; }
181 # clean up header
182 $header =~ s/CLMSG_//g;
184 my $sql_statement = "UPDATE $main::job_queue_tn ".
185 "SET status='processing', result='$header"."$content' ".
186 "WHERE status='processing' AND macaddress='$macaddress'";
187 &main::daemon_log("DEBUG: $sql_statement", 7);
188 my $res = $main::job_db->update_dbentry($sql_statement);
189 &main::daemon_log("INFO: $header at '$macaddress'", 5);
190 return;
191 }
194 sub PROGRESS {
195 my ($msg, $msg_hash, $session_id) = @_;
196 my $header = @{$msg_hash->{'header'}}[0];
197 my $source = @{$msg_hash->{'target'}}[0];
198 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
200 # test whether content is an empty hash or a string which is required
201 my $content;
202 my $cont = @{$msg_hash->{$header}}[0];
203 eval{ if( 0 == keys(%$cont) ) { $content = ""; } };
204 if( $@ ) { $content = "$cont"; }
206 # clean up header
207 $header =~ s/CLMSG_//g;
209 my $sql_statement = "UPDATE $main::job_queue_tn ".
210 "SET progress='$content' ".
211 "WHERE status='processing' AND macaddress='$macaddress'";
212 &main::daemon_log("DEBUG: $sql_statement", 7);
213 my $res = $main::job_db->update_dbentry($sql_statement);
214 &main::daemon_log("INFO: $header at '$macaddress' - $content%", 5);
216 return;
217 }
220 sub FAIREBOOT {
221 my ($msg, $msg_hash, $session_id) = @_;
222 my $header = @{$msg_hash->{'header'}}[0];
223 my $source = @{$msg_hash->{'target'}}[0];
224 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
226 # test whether content is an empty hash or a string which is required
227 my $content = @{$msg_hash->{$header}}[0];
228 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
229 if( $@ ) { $content = "$content"; }
231 # clean up header
232 $header =~ s/CLMSG_//g;
234 my $sql_statement = "UPDATE $main::job_queue_tn ".
235 "SET status='processing', result='$header "."$content' ".
236 "WHERE status='processing' AND macaddress='$macaddress'";
237 &main::daemon_log("DEBUG: $sql_statement", 7);
238 my $res = $main::job_db->update_dbentry($sql_statement);
239 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
241 return;
242 }
245 sub TASKSKIP {
246 my ($msg, $msg_hash, $session_id) = @_;
247 my $header = @{$msg_hash->{'header'}}[0];
248 my $source = @{$msg_hash->{'target'}}[0];
249 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
251 # test whether content is an empty hash or a string which is required
252 my $content = @{$msg_hash->{$header}}[0];
253 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
254 if( $@ ) { $content = "$content"; }
256 # clean up header
257 $header =~ s/CLMSG_//g;
259 my $sql_statement = "UPDATE $main::job_queue_tn ".
260 "SET status='processing', result='$header "."$content' ".
261 "WHERE status='processing' AND macaddress='$macaddress'";
262 &main::daemon_log("DEBUG: $sql_statement", 7);
263 my $res = $main::job_db->update_dbentry($sql_statement);
264 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
266 return;
267 }
271 sub TASKBEGIN {
272 my ($msg, $msg_hash, $session_id) = @_;
273 my $header = @{$msg_hash->{'header'}}[0];
274 my $source = @{$msg_hash->{'target'}}[0];
275 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
276 my $content = @{$msg_hash->{$header}}[0];
278 # test whether content is an empty hash or a string which is required
279 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
280 if( $@ ) { $content = "$content"; }
282 # clean up header
283 $header =~ s/CLMSG_//g;
285 # check if installation finished
286 if (($content eq 'finish') || ($content eq 'faiend')){
287 my $sql_statement = "UPDATE $main::job_queue_tn ".
288 "SET status='done', result='$header "."$content' ".
289 "WHERE status='processing' AND macaddress='$macaddress'";
290 &main::daemon_log("DEBUG: $sql_statement", 7);
291 my $res = $main::job_db->update_dbentry($sql_statement);
292 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
294 # set fai_state to localboot
295 &main::change_fai_state('localboot', \@{$msg_hash->{target}});
297 } else {
298 my $sql_statement = "UPDATE $main::job_queue_tn ".
299 "SET status='processing', result='$header "."$content' ".
300 "WHERE status='processing' AND macaddress='$macaddress'";
301 &main::daemon_log("DEBUG: $sql_statement", 7);
302 my $res = $main::job_db->update_dbentry($sql_statement);
303 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
306 # -----------------------> Update hier
307 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
308 # <header>CLMSG_TASKBEGIN</header>
309 # macaddress auslesen, Client im LDAP lokalisieren
310 # FAIstate auf "localboot" setzen, wenn FAIstate "install" oder "softupdate" war
311 }
313 return;
314 }
317 sub TASKEND {
318 my ($msg, $msg_hash, $session_id) = @_;
319 my $header = @{$msg_hash->{'header'}}[0];
320 my $source = @{$msg_hash->{'target'}}[0];
321 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
323 # test whether content is an empty hash or a string which is required
324 my $content = @{$msg_hash->{$header}}[0];
325 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
326 if( $@ ) { $content = "$content"; }
328 # clean up header
329 $header =~ s/CLMSG_//g;
331 my $sql_statement = "UPDATE $main::job_queue_tn ".
332 "SET status='processing', result='$header "."$content' ".
333 "WHERE status='processing' AND macaddress='$macaddress'";
334 &main::daemon_log("DEBUG: $sql_statement", 7);
335 my $res = $main::job_db->update_dbentry($sql_statement);
336 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
338 # -----------------------> Update hier
339 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
340 # <header>CLMSG_TASKBEGIN</header>
341 # macaddress auslesen, Client im LDAP lokalisieren
342 # FAIstate auf "error" setzen
344 return;
345 }
348 sub TASKERROR {
349 my ($msg, $msg_hash, $session_id) = @_;
350 my $header = @{$msg_hash->{'header'}}[0];
351 my $source = @{$msg_hash->{'target'}}[0];
352 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
354 # clean up header
355 $header =~ s/CLMSG_//g;
357 # test whether content is an empty hash or a string which is required
358 my $content = @{$msg_hash->{$header}}[0];
359 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
360 if( $@ ) { $content = "$content"; }
362 my $sql_statement = "UPDATE $main::job_queue_tn ".
363 "SET status='processing', result='$header "."$content' ".
364 "WHERE status='processing' AND macaddress='$macaddress'";
365 &main::daemon_log("DEBUG: $sql_statement", 7);
366 my $res = $main::job_db->update_dbentry($sql_statement);
367 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
369 # -----------------------> Update hier
370 # <CLMSG_TASKBEGIN>finish</CLMSG_TASKBEGIN>
371 # <header>CLMSG_TASKBEGIN</header>
372 # macaddress auslesen, Client im LDAP lokalisieren
373 # FAIstate auf "error" setzen
375 return;
376 }
379 sub HOOK {
380 my ($msg, $msg_hash, $session_id) = @_;
381 my $header = @{$msg_hash->{'header'}}[0];
382 my $source = @{$msg_hash->{'target'}}[0];
383 my $macaddress = @{$msg_hash->{'macaddress'}}[0];
385 # clean up header
386 $header =~ s/CLMSG_//g;
388 # test whether content is an empty hash or a string which is required
389 my $content = @{$msg_hash->{$header}}[0];
390 eval{ if( 0 == keys(%$content) ) { $content = ""; } };
391 if( $@ ) { $content = "$content"; }
393 my $sql_statement = "UPDATE $main::job_queue_tn ".
394 "SET status='processing', result='$header "."$content' ".
395 "WHERE status='processing' AND macaddress='$macaddress'";
396 &main::daemon_log("DEBUG: $sql_statement", 7);
397 my $res = $main::job_db->update_dbentry($sql_statement);
398 &main::daemon_log("INFO: $header at '$macaddress' - '$content'", 5);
400 return;
401 }
404 1;