81137df2dcc5fec9c7b042f205e1d016e8c34020
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_halt",
21 "trigger_action_update",
22 "trigger_action_reinstall",
23 "trigger_action_memcheck",
24 "trigger_action_sysinfo",
25 "trigger_action_instant_update",
26 "trigger_action_rescan",
27 "trigger_action_wake",
28 "recreate_fai_server_db",
29 "send_user_msg",
30 );
31 @EXPORT = @events;
33 use strict;
34 use warnings;
35 use GOSA::GosaSupportDaemon;
36 use Data::Dumper;
37 use Crypt::SmbHash;
38 use Net::ARP;
39 use Net::Ping;
40 use Socket;
41 use utf8;
43 BEGIN {}
45 END {}
47 ### Start ######################################################################
49 #&main::read_configfile($main::cfg_file, %cfg_defaults);
51 sub get_events {
52 return \@events;
53 }
55 sub send_user_msg {
56 my ($msg, $msg_hash, $session_id) = @_ ;
57 my @out_msg_l;
58 my @user_list;
59 my @group_list;
61 my $header = @{$msg_hash->{'header'}}[0];
62 my $source = @{$msg_hash->{'source'}}[0];
63 my $target = @{$msg_hash->{'target'}}[0];
64 my $message = @{$msg_hash->{'message'}}[0];
65 if( exists $msg_hash->{'user'} ) { @user_list = @{$msg_hash->{'user'}}; }
66 if( exists $msg_hash->{'group'} ) { @group_list = @{$msg_hash->{'group'}}; }
68 # error handling
69 if( not @user_list && not @group_list ) {
70 &main::daemon_log("WARNING: no user-tag or a group-tag specified in 'send_user_msg'", 3);
71 return ("<xml><header>$header</header><source>GOSA</source><target>GOSA</target>".
72 "<error_string>no user-tag or a group-tag specified in 'send_user_msg'</error_string></xml>");
73 }
74 if( not defined $message ) {
75 &main::daemon_log("WARNING: no message-tag specified in 'send_user_msg'", 3);
76 return ("<xml><header>$header</header><source>GOSA</source><target>GOSA</target>".
77 "<error_string>no message-tag specified in 'send_user_msg'</error_string></xml>");
79 }
81 # resolve groups to users
82 if( @group_list ) {
83 # build ldap connection
84 &main::refresh_ldap_handle();
85 if( not defined $main::ldap_handle ) {
86 &main::daemon_log("ERROR: cannot connect to ldap", 1);
87 return ();
88 }
89 foreach my $group (@group_list) {
90 # Perform search
91 my $mesg = $main::ldap_handle->search(
92 base => $main::ldap_base,
93 scope => 'sub',
94 attrs => ['memberUid'],
95 filter => "(&(objectClass=posixGroup)(cn=$group)(memberUid=*))");
96 if($mesg->code) {
97 &main::daemon_log($mesg->error, 1);
98 return ();
99 }
100 my $entry= $mesg->entry(0);
101 my @users= $entry->get_value("memberUid");
102 foreach my $user (@users) { push(@user_list, $user); }
103 }
104 }
106 # drop multiple users in @user_list
107 my %seen = ();
108 foreach my $user (@user_list) {
109 $seen{$user}++;
110 }
111 @user_list = keys %seen;
113 # build xml messages sended to client where user is logged in
114 foreach my $user (@user_list) {
115 my $sql_statement = "SELECT * FROM $main::login_users_tn WHERE user='$user'";
116 my $db_res = $main::login_users_db->select_dbentry($sql_statement);
118 if(0 == keys(%{$db_res})) {
120 } else {
121 while( my($hit, $content) = each %{$db_res} ) {
122 my $out_hash = &create_xml_hash('send_user_msg', $main::server_address, $content->{'client'});
123 &add_content2xml_hash($out_hash, 'message', $message);
124 &add_content2xml_hash($out_hash, 'user', $user);
125 if( exists $msg_hash->{'jobdb_id'} ) {
126 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
127 }
128 my $out_msg = &create_xml_string($out_hash);
129 push(@out_msg_l, $out_msg);
130 }
131 }
132 }
134 return @out_msg_l;
135 }
138 sub recreate_fai_server_db {
139 my ($msg, $msg_hash, $session_id) = @_ ;
140 my $out_msg;
142 $main::fai_server_db->create_table("new_fai_server", \@main::fai_server_col_names);
143 &main::create_fai_server_db("new_fai_server");
144 $main::fai_server_db->move_table("new_fai_server", $main::fai_server_tn);
146 my @out_msg_l = ( $out_msg );
147 return @out_msg_l;
148 }
151 sub get_login_usr_for_client {
152 my ($msg, $msg_hash, $session_id) = @_ ;
153 my $header = @{$msg_hash->{'header'}}[0];
154 my $source = @{$msg_hash->{'source'}}[0];
155 my $target = @{$msg_hash->{'target'}}[0];
156 my $client = @{$msg_hash->{'client'}}[0];
158 $header =~ s/^gosa_//;
160 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$client' OR macaddress='$client'";
161 my $res = $main::known_clients_db->select_dbentry($sql_statement);
163 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
164 $out_msg .= &db_res2xml($res);
165 $out_msg .= "</xml>";
167 my @out_msg_l = ( $out_msg );
168 return @out_msg_l;
169 }
172 sub get_client_for_login_usr {
173 my ($msg, $msg_hash, $session_id) = @_ ;
174 my $header = @{$msg_hash->{'header'}}[0];
175 my $source = @{$msg_hash->{'source'}}[0];
176 my $target = @{$msg_hash->{'target'}}[0];
177 my $usr = @{$msg_hash->{'usr'}}[0];
178 $header =~ s/^gosa_//;
180 my $sql_statement = "SELECT * FROM known_clients WHERE login LIKE '%$usr%'";
181 my $res = $main::known_clients_db->select_dbentry($sql_statement);
183 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
184 $out_msg .= &db_res2xml($res);
185 $out_msg .= "</xml>";
186 my @out_msg_l = ( $out_msg );
187 return @out_msg_l;
189 }
192 sub ping {
193 my ($msg, $msg_hash, $session_id) = @_ ;
194 my $out_msg = $msg;
195 $out_msg =~ s/<header>gosa_/<header>/;
196 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
198 my @out_msg_l = ( $out_msg );
199 return @out_msg_l;
200 }
202 sub gen_smb_hash {
203 my ($msg, $msg_hash, $session_id) = @_ ;
204 my $source = @{$msg_hash->{source}}[0];
205 my $target = @{$msg_hash->{target}}[0];
206 my $password = @{$msg_hash->{password}}[0];
208 my %data= ('hash' => join(q[:], ntlmgen $password));
209 my $out_msg = &build_msg("gen_smb_hash", $target, 'GOSA', \%data );
210 return ( $out_msg );
211 }
214 sub network_completition {
215 my ($msg, $msg_hash, $session_id) = @_ ;
216 my $source = @{$msg_hash->{source}}[0];
217 my $target = @{$msg_hash->{target}}[0];
218 my $name = @{$msg_hash->{hostname}}[0];
220 # Can we resolv the name?
221 my %data;
222 if (inet_aton($name)){
223 my $address = inet_ntoa(inet_aton($name));
224 my $p = Net::Ping->new('tcp');
225 my $mac= "";
226 if ($p->ping($address, 1)){
227 $mac = Net::ARP::arp_lookup("", $address);
228 }
230 %data= ('ip' => $address, 'mac' => $mac);
231 } else {
232 %data= ('ip' => '', 'mac' => '');
233 }
235 my $out_msg = &build_msg("network_completition", $target, 'GOSA', \%data );
237 return ( $out_msg );
238 }
241 sub detect_hardware {
242 my ($msg, $msg_hash) = @_ ;
243 # just forward msg to client, but dont forget to split off 'gosa_' in header
244 my $source = @{$msg_hash->{source}}[0];
245 my $target = @{$msg_hash->{target}}[0];
247 my $out_hash = &create_xml_hash("detect_hardware", $source, $target);
248 if( exists $msg_hash->{'jobdb_id'} ) {
249 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
250 }
251 my $out_msg = &create_xml_string($out_hash);
253 my @out_msg_l = ( $out_msg );
254 return @out_msg_l;
256 }
259 sub trigger_reload_ldap_config {
260 my ($msg, $msg_hash) = @_ ;
261 my $target = @{$msg_hash->{target}}[0];
263 my $out_hash = &create_xml_hash("reload_ldap_config", $main::server_address, $main::server_address, $target);
264 if( exists $msg_hash->{'jobdb_id'} ) {
265 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
266 }
267 my $out_msg = &create_xml_string($out_hash);
268 my @out_msg_l;
269 push(@out_msg_l, $out_msg);
270 return @out_msg_l;
271 }
274 sub set_activated_for_installation {
275 my ($msg, $msg_hash) = @_;
277 my $header = @{$msg_hash->{header}}[0];
278 my $source = @{$msg_hash->{source}}[0];
279 my $target = @{$msg_hash->{target}}[0];
281 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
282 if( exists $msg_hash->{'jobdb_id'} ) {
283 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
284 }
285 my $out_msg = &create_xml_string($out_hash);
286 my @out_msg_l = ( $out_msg );
287 return @out_msg_l;
288 }
291 sub trigger_action_faireboot {
292 my ($msg, $msg_hash) = @_;
293 my $macaddress = @{$msg_hash->{target}}[0];
294 my $source = @{$msg_hash->{source}}[0];
296 my @out_msg_l;
297 $msg =~ s/<header>gosa_trigger_action_faireboot<\/header>/<header>trigger_action_faireboot<\/header>/;
298 push(@out_msg_l, $msg);
300 # delete all jobs from jobqueue which correspond to fai
301 my $sql_statement = "DELETE FROM $main::job_queue_tn WHERE (macaddress='$macaddress' AND ".
302 "status='processing' AND headertag='trigger_action_install')";
303 $main::job_db->del_dbentry($sql_statement );
305 return @out_msg_l;
306 }
310 sub trigger_action_localboot {
311 my ($msg, $msg_hash) = @_;
312 $msg =~ s/<header>gosa_trigger_action_localboot<\/header>/<header>trigger_action_localboot<\/header>/;
314 change_fai_state('localboot', \@{$msg_hash->{target}});
316 my @out_msg_l = ($msg);
317 return @out_msg_l;
318 }
321 sub trigger_action_halt {
322 my ($msg, $msg_hash) = @_;
323 $msg =~ s/<header>gosa_trigger_action_halt<\/header>/<header>trigger_action_halt<\/header>/;
325 change_fai_state('halt', \@{$msg_hash->{target}});
327 my @out_msg_l = ($msg);
328 return @out_msg_l;
329 }
332 sub trigger_action_reboot {
333 my ($msg, $msg_hash) = @_;
334 $msg =~ s/<header>gosa_trigger_action_reboot<\/header>/<header>trigger_action_reboot<\/header>/;
336 change_fai_state('reboot', \@{$msg_hash->{target}});
338 my @out_msg_l = ($msg);
339 return @out_msg_l;
340 }
343 sub trigger_action_memcheck {
344 my ($msg, $msg_hash) = @_ ;
345 $msg =~ s/<header>gosa_trigger_action_memcheck<\/header>/<header>trigger_action_memcheck<\/header>/;
347 change_fai_state('memcheck', \@{$msg_hash->{target}});
349 my @out_msg_l = ($msg);
350 return @out_msg_l;
351 }
354 sub trigger_action_reinstall {
355 my ($msg, $msg_hash) = @_;
356 $msg =~ s/<header>gosa_trigger_action_reinstall<\/header>/<header>trigger_action_reinstall<\/header>/;
358 change_fai_state('reinstall', \@{$msg_hash->{target}});
360 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
361 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
362 my @out_msg_l = ($wake_msg, $msg);
363 return @out_msg_l;
364 }
367 sub trigger_action_update {
368 my ($msg, $msg_hash) = @_;
369 $msg =~ s/<header>gosa_trigger_action_update<\/header>/<header>trigger_action_update<\/header>/;
371 change_fai_state('update', \@{$msg_hash->{target}});
373 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
374 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
375 my @out_msg_l = ($wake_msg, $msg);
376 return @out_msg_l;
377 }
380 sub trigger_action_instant_update {
381 my ($msg, $msg_hash) = @_;
382 $msg =~ s/<header>gosa_trigger_action_instant_update<\/header>/<header>trigger_action_instant_update<\/header>/;
384 change_fai_state('update', \@{$msg_hash->{target}});
386 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
387 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
388 my @out_msg_l = ($wake_msg, $msg);
389 return @out_msg_l;
390 }
393 sub trigger_action_sysinfo {
394 my ($msg, $msg_hash) = @_;
395 $msg =~ s/<header>gosa_trigger_action_sysinfo<\/header>/<header>trigger_action_sysinfo<\/header>/;
397 change_fai_state('sysinfo', \@{$msg_hash->{target}});
399 my @out_msg_l = ($msg);
400 return @out_msg_l;
401 }
404 sub new_key_for_client {
405 my ($msg, $msg_hash) = @_;
406 $msg =~ s/<header>gosa_new_key_for_client<\/header>/<header>new_key<\/header>/;
407 my @out_msg_l = ($msg);
408 return @out_msg_l;
409 }
412 sub trigger_action_rescan {
413 my ($msg, $msg_hash) = @_;
414 $msg =~ s/<header>gosa_trigger_action_rescan<\/header>/<header>trigger_action_rescan<\/header>/;
415 my @out_msg_l = ($msg);
416 return @out_msg_l;
417 }
420 sub trigger_action_wake {
421 my ($msg, $msg_hash) = @_;
422 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
423 my $out_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
424 my @out_msg_l = ($out_msg);
425 return @out_msg_l;
426 }
429 sub change_fai_state {
430 my ($st, $targets) = @_;
432 # Set FAI state to localboot
433 my %mapActions= (
434 reboot => '',
435 update => 'softupdate',
436 localboot => 'localboot',
437 reinstall => 'install',
438 rescan => '',
439 wake => '',
440 memcheck => 'memcheck',
441 sysinfo => 'sysinfo',
442 );
444 # Return if this is unknown
445 if (!exists $mapActions{ $st }){
446 return;
447 }
449 my $state= $mapActions{ $st };
451 &main::refresh_ldap_handle();
452 if( defined($main::ldap_handle) ) {
454 # Build search filter for hosts
455 my $search= "(&(objectClass=GOhard)";
456 foreach (@{$targets}){
457 $search.= "(macAddress=$_)";
458 }
459 $search.= ")";
461 # If there's any host inside of the search string, procress them
462 if ($search =~ /macAddress/){
463 # Perform search for Unit Tag
464 my $mesg = $main::ldap_handle->search(
465 base => $main::ldap_base,
466 scope => 'sub',
467 attrs => ['dn', 'FAIstate', 'objectClass'],
468 filter => "$search"
469 );
471 if ($mesg->count) {
472 my @entries = $mesg->entries;
473 foreach my $entry (@entries) {
475 # Only modify entry if it is not set to 'localboot'
476 if ($entry->get_value("FAIstate") ne 'localboot'){
478 &main::daemon_log("INFO: Setting FAIstate to 'localboot' for ".$entry->dn, 5);
479 my $result;
480 my %tmp = map { $_ => 1 } $entry->get_value("objectClass");
481 if (exists $tmp{'FAIobject'}){
482 if ($state eq ''){
483 $result= $main::ldap_handle->modify($entry->dn, changes => [
484 delete => [ FAIstate => [] ] ]);
485 } else {
486 $result= $main::ldap_handle->modify($entry->dn, changes => [
487 replace => [ FAIstate => $state ] ]);
488 }
489 } elsif ($state ne ''){
490 $result= $main::ldap_handle->modify($entry->dn, changes => [
491 add => [ objectClass => 'FAIobject' ],
492 add => [ FAIstate => $state ] ]);
493 }
495 # Errors?
496 if ($result->code){
497 &main::daemon_log("Error: Setting FAIstate to 'localboot' for ".$entry->dn. "failed: ".$result->error, 1);
498 }
500 }
501 }
502 }
504 }
506 }
507 }
509 1;