b95f0d5c955a6693266fbe95938cedb57da61b7e
1 package gosaTriggered;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5 "get_events",
6 "get_login_usr_for_client",
7 "get_client_for_login_usr",
8 "gen_smb_hash",
9 "trigger_reload_ldap_config",
10 "ping",
11 "network_completition",
12 "set_activated_for_installation",
13 "new_key_for_client",
14 "detect_hardware",
15 "get_login_usr",
16 "get_login_client",
17 "trigger_action_localboot",
18 "trigger_action_faireboot",
19 "trigger_action_reboot",
20 "trigger_action_activate",
21 "trigger_action_lock",
22 "trigger_action_halt",
23 "trigger_action_update",
24 "trigger_action_reinstall",
25 "trigger_action_memcheck",
26 "trigger_action_sysinfo",
27 "trigger_action_instant_update",
28 "trigger_action_rescan",
29 "trigger_action_wake",
30 "recreate_fai_server_db",
31 "send_user_msg",
32 );
33 @EXPORT = @events;
35 use strict;
36 use warnings;
37 use GOSA::GosaSupportDaemon;
38 use Data::Dumper;
39 use Crypt::SmbHash;
40 use Net::ARP;
41 use Net::Ping;
42 use Socket;
44 BEGIN {}
46 END {}
48 ### Start ######################################################################
50 #&main::read_configfile($main::cfg_file, %cfg_defaults);
52 sub get_events {
53 return \@events;
54 }
56 sub send_user_msg {
57 my ($msg, $msg_hash, $session_id) = @_ ;
58 my @out_msg_l;
59 my @user_list;
60 my @group_list;
62 my $header = @{$msg_hash->{'header'}}[0];
63 my $source = @{$msg_hash->{'source'}}[0];
64 my $target = @{$msg_hash->{'target'}}[0];
65 my $message = @{$msg_hash->{'message'}}[0];
66 if( exists $msg_hash->{'user'} ) { @user_list = @{$msg_hash->{'user'}}; }
67 if( exists $msg_hash->{'group'} ) { @group_list = @{$msg_hash->{'group'}}; }
69 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
70 if( defined $jobdb_id) {
71 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
72 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
73 my $res = $main::job_db->exec_statement($sql_statement);
74 }
76 # error handling
77 if( not @user_list && not @group_list ) {
78 &main::daemon_log("WARNING: no user-tag or a group-tag specified in 'send_user_msg'", 3);
79 return ("<xml><header>$header</header><source>GOSA</source><target>GOSA</target>".
80 "<error_string>no user-tag or a group-tag specified in 'send_user_msg'</error_string></xml>");
81 }
82 if( not defined $message ) {
83 &main::daemon_log("WARNING: no message-tag specified in 'send_user_msg'", 3);
84 return ("<xml><header>$header</header><source>GOSA</source><target>GOSA</target>".
85 "<error_string>no message-tag specified in 'send_user_msg'</error_string></xml>");
87 }
89 my $ldap_handle = &main::get_ldap_handle($session_id);
90 # resolve groups to users
91 if( @group_list ) {
92 # build ldap connection
93 if( not defined $ldap_handle ) {
94 &main::daemon_log("ERROR: cannot connect to ldap", 1);
95 return ();
96 }
97 foreach my $group (@group_list) {
98 # Perform search
99 my $mesg = $ldap_handle->search(
100 base => $main::ldap_base,
101 scope => 'sub',
102 attrs => ['memberUid'],
103 filter => "(&(objectClass=posixGroup)(cn=$group)(memberUid=*))");
104 if($mesg->code) {
105 &main::daemon_log($mesg->error, 1);
106 return ();
107 }
108 my $entry= $mesg->entry(0);
109 my @users= $entry->get_value("memberUid");
110 foreach my $user (@users) { push(@user_list, $user); }
111 }
112 }
114 # drop multiple users in @user_list
115 my %seen = ();
116 foreach my $user (@user_list) {
117 $seen{$user}++;
118 }
119 @user_list = keys %seen;
121 # build xml messages sended to client where user is logged in
122 foreach my $user (@user_list) {
123 my $sql_statement = "SELECT * FROM $main::login_users_tn WHERE user='$user'";
124 my $db_res = $main::login_users_db->select_dbentry($sql_statement);
126 if(0 == keys(%{$db_res})) {
128 } else {
129 while( my($hit, $content) = each %{$db_res} ) {
130 my $out_hash = &create_xml_hash('send_user_msg', $main::server_address, $content->{'client'});
131 &add_content2xml_hash($out_hash, 'message', $message);
132 &add_content2xml_hash($out_hash, 'user', $user);
133 if( exists $msg_hash->{'jobdb_id'} ) {
134 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
135 }
136 my $out_msg = &create_xml_string($out_hash);
137 push(@out_msg_l, $out_msg);
138 }
139 }
140 }
142 return @out_msg_l;
143 }
146 sub recreate_fai_server_db {
147 my ($msg, $msg_hash, $session_id) = @_ ;
148 my $out_msg;
150 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
151 if( defined $jobdb_id) {
152 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
153 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
154 my $res = $main::job_db->exec_statement($sql_statement);
155 }
157 $main::fai_server_db->create_table("new_fai_server", \@main::fai_server_col_names);
158 &main::create_fai_server_db("new_fai_server");
159 $main::fai_server_db->move_table("new_fai_server", $main::fai_server_tn);
161 my @out_msg_l = ( $out_msg );
162 return @out_msg_l;
163 }
166 sub get_login_usr_for_client {
167 my ($msg, $msg_hash, $session_id) = @_ ;
168 my $header = @{$msg_hash->{'header'}}[0];
169 my $source = @{$msg_hash->{'source'}}[0];
170 my $target = @{$msg_hash->{'target'}}[0];
171 my $client = @{$msg_hash->{'client'}}[0];
173 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
174 if( defined $jobdb_id) {
175 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
176 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
177 my $res = $main::job_db->exec_statement($sql_statement);
178 }
180 $header =~ s/^gosa_//;
182 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$client' OR macaddress LIKE '$client'";
183 my $res = $main::known_clients_db->select_dbentry($sql_statement);
185 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
186 $out_msg .= &db_res2xml($res);
187 $out_msg .= "</xml>";
189 my @out_msg_l = ( $out_msg );
190 return @out_msg_l;
191 }
194 sub get_client_for_login_usr {
195 my ($msg, $msg_hash, $session_id) = @_ ;
196 my $header = @{$msg_hash->{'header'}}[0];
197 my $source = @{$msg_hash->{'source'}}[0];
198 my $target = @{$msg_hash->{'target'}}[0];
200 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
201 if( defined $jobdb_id) {
202 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
203 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
204 my $res = $main::job_db->exec_statement($sql_statement);
205 }
207 my $usr = @{$msg_hash->{'usr'}}[0];
208 $header =~ s/^gosa_//;
210 my $sql_statement = "SELECT * FROM known_clients WHERE login LIKE '%$usr%'";
211 my $res = $main::known_clients_db->select_dbentry($sql_statement);
213 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
214 $out_msg .= &db_res2xml($res);
215 $out_msg .= "</xml>";
216 my @out_msg_l = ( $out_msg );
217 return @out_msg_l;
219 }
222 sub ping {
223 my ($msg, $msg_hash, $session_id) = @_ ;
224 my $out_msg = $msg;
225 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
226 if( defined $jobdb_id) {
227 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
228 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
229 my $res = $main::job_db->exec_statement($sql_statement);
230 }
232 $out_msg =~ s/<header>gosa_/<header>/;
234 my @out_msg_l = ( $out_msg );
235 return @out_msg_l;
236 }
238 sub gen_smb_hash {
239 my ($msg, $msg_hash, $session_id) = @_ ;
240 my $source = @{$msg_hash->{source}}[0];
241 my $target = @{$msg_hash->{target}}[0];
242 my $password = @{$msg_hash->{password}}[0];
244 my %data= ('hash' => join(q[:], ntlmgen $password));
245 my $out_msg = &build_msg("gen_smb_hash", $target, 'GOSA', \%data );
246 return ( $out_msg );
247 }
250 sub network_completition {
251 my ($msg, $msg_hash, $session_id) = @_ ;
252 my $source = @{$msg_hash->{source}}[0];
253 my $target = @{$msg_hash->{target}}[0];
254 my $name = @{$msg_hash->{hostname}}[0];
256 # Can we resolv the name?
257 my %data;
258 if (inet_aton($name)){
259 my $address = inet_ntoa(inet_aton($name));
260 my $p = Net::Ping->new('tcp');
261 my $mac= "";
262 if ($p->ping($address, 1)){
263 $mac = Net::ARP::arp_lookup("", $address);
264 }
266 %data= ('ip' => $address, 'mac' => $mac);
267 } else {
268 %data= ('ip' => '', 'mac' => '');
269 }
271 my $out_msg = &build_msg("network_completition", $target, 'GOSA', \%data );
273 return ( $out_msg );
274 }
277 sub detect_hardware {
278 my ($msg, $msg_hash, $session_id) = @_ ;
279 # just forward msg to client, but dont forget to split off 'gosa_' in header
280 my $source = @{$msg_hash->{source}}[0];
281 my $target = @{$msg_hash->{target}}[0];
282 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
283 if( defined $jobdb_id) {
284 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
285 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
286 my $res = $main::job_db->exec_statement($sql_statement);
287 }
289 my $out_hash = &create_xml_hash("detect_hardware", $source, $target);
290 if( defined $jobdb_id ) {
291 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
292 }
293 my $out_msg = &create_xml_string($out_hash);
295 my @out_msg_l = ( $out_msg );
296 return @out_msg_l;
298 }
301 sub trigger_reload_ldap_config {
302 my ($msg, $msg_hash, $session_id) = @_ ;
303 my $target = @{$msg_hash->{target}}[0];
305 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
306 if( defined $jobdb_id) {
307 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
308 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
309 my $res = $main::job_db->exec_statement($sql_statement);
310 }
312 my $out_hash = &create_xml_hash("reload_ldap_config", $main::server_address, $main::server_address, $target);
313 if( defined ) {
314 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
315 }
316 my $out_msg = &create_xml_string($out_hash);
317 my @out_msg_l;
318 push(@out_msg_l, $out_msg);
319 return @out_msg_l;
320 }
323 sub set_activated_for_installation {
324 my ($msg, $msg_hash, $session_id) = @_;
325 my $header = @{$msg_hash->{header}}[0];
326 my $source = @{$msg_hash->{source}}[0];
327 my $target = @{$msg_hash->{target}}[0];
329 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
330 if( defined $jobdb_id) {
331 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
332 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
333 my $res = $main::job_db->exec_statement($sql_statement);
334 }
336 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
337 if( defined $jobdb_id ) {
338 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
339 }
340 my $out_msg = &create_xml_string($out_hash);
341 my @out_msg_l = ( $out_msg );
342 return @out_msg_l;
343 }
346 sub trigger_action_faireboot {
347 my ($msg, $msg_hash, $session_id) = @_;
348 my $macaddress = @{$msg_hash->{target}}[0];
349 my $source = @{$msg_hash->{source}}[0];
351 my @out_msg_l;
352 $msg =~ s/<header>gosa_trigger_action_faireboot<\/header>/<header>trigger_action_faireboot<\/header>/;
353 push(@out_msg_l, $msg);
355 &main::change_goto_state('locked', \@{$msg_hash->{target}}, $session_id);
356 &main::change_fai_state('install', \@{$msg_hash->{target}}, $session_id);
358 # delete all jobs from jobqueue which correspond to fai
359 my $sql_statement = "DELETE FROM $main::job_queue_tn WHERE (macaddress='$macaddress' AND ".
360 "status='processing')";
361 $main::job_db->del_dbentry($sql_statement );
363 return @out_msg_l;
364 }
367 sub trigger_action_lock {
368 my ($msg, $msg_hash, $session_id) = @_;
369 my $macaddress = @{$msg_hash->{target}}[0];
370 my $source = @{$msg_hash->{source}}[0];
372 &main::change_goto_state('locked', \@{$msg_hash->{target}}, $session_id);
373 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
374 if( defined $jobdb_id) {
375 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
376 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
377 my $res = $main::job_db->exec_statement($sql_statement);
378 }
380 my @out_msg_l;
381 return @out_msg_l;
382 }
385 sub trigger_action_activate {
386 my ($msg, $msg_hash, $session_id) = @_;
387 my $macaddress = @{$msg_hash->{target}}[0];
388 my $source = @{$msg_hash->{source}}[0];
390 &main::change_goto_state('active', \@{$msg_hash->{target}}, $session_id);
391 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
392 if( defined $jobdb_id) {
393 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
394 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
395 my $res = $main::job_db->exec_statement($sql_statement);
396 }
398 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $macaddress);
399 if( exists $msg_hash->{'jobdb_id'} ) {
400 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
401 }
402 my $out_msg = &create_xml_string($out_hash);
404 return ( $out_msg );
405 }
408 sub trigger_action_localboot {
409 my ($msg, $msg_hash, $session_id) = @_;
410 $msg =~ s/<header>gosa_trigger_action_localboot<\/header>/<header>trigger_action_localboot<\/header>/;
411 &main::change_fai_state('localboot', \@{$msg_hash->{target}}, $session_id);
412 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
413 if( defined $jobdb_id) {
414 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
415 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
416 my $res = $main::job_db->exec_statement($sql_statement);
417 }
419 my @out_msg_l = ($msg);
420 return @out_msg_l;
421 }
424 sub trigger_action_halt {
425 my ($msg, $msg_hash, $session_id) = @_;
426 $msg =~ s/<header>gosa_trigger_action_halt<\/header>/<header>trigger_action_halt<\/header>/;
428 &main::change_fai_state('halt', \@{$msg_hash->{target}}, $session_id);
429 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
430 if( defined $jobdb_id) {
431 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
432 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
433 my $res = $main::job_db->exec_statement($sql_statement);
434 }
436 my @out_msg_l = ($msg);
437 return @out_msg_l;
438 }
441 sub trigger_action_reboot {
442 my ($msg, $msg_hash, $session_id) = @_;
443 $msg =~ s/<header>gosa_trigger_action_reboot<\/header>/<header>trigger_action_reboot<\/header>/;
445 &main::change_fai_state('reboot', \@{$msg_hash->{target}}, $session_id);
446 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
447 if( defined $jobdb_id) {
448 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
449 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
450 my $res = $main::job_db->exec_statement($sql_statement);
451 }
453 my @out_msg_l = ($msg);
454 return @out_msg_l;
455 }
458 sub trigger_action_memcheck {
459 my ($msg, $msg_hash, $session_id) = @_ ;
460 $msg =~ s/<header>gosa_trigger_action_memcheck<\/header>/<header>trigger_action_memcheck<\/header>/;
462 &main::change_fai_state('memcheck', \@{$msg_hash->{target}}, $session_id);
463 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
464 if( defined $jobdb_id) {
465 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
466 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
467 my $res = $main::job_db->exec_statement($sql_statement);
468 }
470 my @out_msg_l = ($msg);
471 return @out_msg_l;
472 }
475 sub trigger_action_reinstall {
476 my ($msg, $msg_hash, $session_id) = @_;
477 $msg =~ s/<header>gosa_trigger_action_reinstall<\/header>/<header>trigger_action_reinstall<\/header>/;
479 &main::change_fai_state('reinstall', \@{$msg_hash->{target}}, $session_id);
481 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
482 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
483 my @out_msg_l = ($wake_msg, $msg);
484 return @out_msg_l;
485 }
488 sub trigger_action_update {
489 my ($msg, $msg_hash, $session_id) = @_;
490 $msg =~ s/<header>gosa_trigger_action_update<\/header>/<header>trigger_action_update<\/header>/;
492 &main::change_fai_state('update', \@{$msg_hash->{target}}, $session_id);
494 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
495 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
496 my @out_msg_l = ($wake_msg, $msg);
497 return @out_msg_l;
498 }
501 sub trigger_action_instant_update {
502 my ($msg, $msg_hash, $session_id) = @_;
503 $msg =~ s/<header>gosa_trigger_action_instant_update<\/header>/<header>trigger_action_instant_update<\/header>/;
505 &main::change_fai_state('update', \@{$msg_hash->{target}}, $session_id);
507 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
508 if( defined $jobdb_id) {
509 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
510 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
511 my $res = $main::job_db->exec_statement($sql_statement);
512 }
514 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
515 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
516 my @out_msg_l = ($wake_msg, $msg);
517 return @out_msg_l;
518 }
521 sub trigger_action_sysinfo {
522 my ($msg, $msg_hash, $session_id) = @_;
523 $msg =~ s/<header>gosa_trigger_action_sysinfo<\/header>/<header>trigger_action_sysinfo<\/header>/;
525 &main::change_fai_state('sysinfo', \@{$msg_hash->{target}}, $session_id);
526 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
527 if( defined $jobdb_id) {
528 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
529 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
530 my $res = $main::job_db->exec_statement($sql_statement);
531 }
533 my @out_msg_l = ($msg);
534 return @out_msg_l;
535 }
538 sub new_key_for_client {
539 my ($msg, $msg_hash, $session_id) = @_;
541 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
542 if( defined $jobdb_id) {
543 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
544 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
545 my $res = $main::job_db->exec_statement($sql_statement);
546 }
548 $msg =~ s/<header>gosa_new_key_for_client<\/header>/<header>new_key<\/header>/;
549 my @out_msg_l = ($msg);
550 return @out_msg_l;
551 }
554 sub trigger_action_rescan {
555 my ($msg, $msg_hash, $session_id) = @_;
557 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
558 if( defined $jobdb_id) {
559 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
560 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
561 my $res = $main::job_db->exec_statement($sql_statement);
562 }
565 $msg =~ s/<header>gosa_trigger_action_rescan<\/header>/<header>trigger_action_rescan<\/header>/;
566 my @out_msg_l = ($msg);
567 return @out_msg_l;
568 }
571 sub trigger_action_wake {
572 my ($msg, $msg_hash, $session_id) = @_;
574 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
575 if( defined $jobdb_id) {
576 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id='$jobdb_id'";
577 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
578 my $res = $main::job_db->exec_statement($sql_statement);
579 }
582 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
583 my $out_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
584 my @out_msg_l = ($out_msg);
585 return @out_msg_l;
586 }
589 1;