164c4913e100a5c6d908b9103a09c94f68150497
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 "new_ping",
12 "network_completition",
13 "set_activated_for_installation",
14 "new_key_for_client",
15 "detect_hardware",
16 "get_login_usr",
17 "get_login_client",
18 "trigger_action_localboot",
19 "trigger_action_faireboot",
20 "trigger_action_reboot",
21 "trigger_action_activate",
22 "trigger_action_lock",
23 "trigger_action_halt",
24 "trigger_action_update",
25 "trigger_action_reinstall",
26 "trigger_action_memcheck",
27 "trigger_action_sysinfo",
28 "trigger_action_instant_update",
29 "trigger_action_rescan",
30 "trigger_action_wake",
31 "recreate_fai_server_db",
32 "recreate_fai_release_db",
33 "recreate_packages_list_db",
34 "send_user_msg",
35 "get_available_kernel",
36 "trigger_activate_new",
37 );
38 @EXPORT = @events;
40 use strict;
41 use warnings;
42 use GOSA::GosaSupportDaemon;
43 use Data::Dumper;
44 use Crypt::SmbHash;
45 use Net::ARP;
46 use Net::Ping;
47 use Socket;
48 use Time::HiRes qw( usleep);
50 BEGIN {}
52 END {}
54 ### Start ######################################################################
56 #&main::read_configfile($main::cfg_file, %cfg_defaults);
58 sub get_events {
59 return \@events;
60 }
62 sub send_user_msg {
64 # msg from gosa
65 # <xml><header>gosa_send_user_msg</header><source>GOSA</source><target>GOSA</target>
66 # <timestamp>20080429151605</timestamp>
67 # <users>andreas.rettenberger</users>
68 # <subject>hallo</subject>
69 # <message>test</message>
70 # <macaddress>GOSA</macaddress>
71 # </xml>
73 my ($msg, $msg_hash, $session_id) = @_ ;
74 my $header = @{$msg_hash->{'header'}}[0];
75 my $source = @{$msg_hash->{'source'}}[0];
76 my $target = @{$msg_hash->{'target'}}[0];
78 #my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
79 my $subject = @{$msg_hash->{'subject'}}[0];
80 my $from = @{$msg_hash->{'from'}}[0];
81 my @users = @{$msg_hash->{'users'}};
82 my @groups = @{$msg_hash->{'groups'}}[0];
83 my $delivery_time = @{$msg_hash->{'delivery_time'}}[0];
84 #my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
85 my $message = @{$msg_hash->{'message'}}[0];
87 # keep job queue uptodate if necessary
88 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
89 if( defined $jobdb_id) {
90 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
91 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
92 my $res = $main::job_db->exec_statement($sql_statement);
93 }
95 # error handling
96 if (not $delivery_time =~ /^\d{14}$/) {
97 my $error_string = "delivery_time '$delivery_time' is not a valid timestamp, please use format 'yyyymmddhhmmss'";
98 &main::daemon_log("$session_id ERROR: $error_string", 1);
99 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
100 }
102 # add incoming message to messaging_db
103 my $new_msg_id = 1;
104 my $new_msg_id_sql = "SELECT MAX(CAST(id AS INTEGER)) FROM $main::messaging_tn";
105 my $new_msg_id_res = $main::messaging_db->exec_statement($new_msg_id_sql);
106 if (defined @{@{$new_msg_id_res}[0]}[0] ) {
107 $new_msg_id = int(@{@{$new_msg_id_res}[0]}[0]);
108 $new_msg_id += 1;
109 }
111 # highlight user name and group name
112 my @receiver_l;
113 @users = map(push(@receiver_l, "u_$_"), @users);
114 #@groups = map(push(@receiver_l, "g_$_"), @groups);
115 # TODO
116 # handling, was passiert wenn in einer liste nix drin steht
117 # handling von groups hinzufügen
120 my $func_dic = {table=>$main::messaging_tn,
121 primkey=>[],
122 id=>$new_msg_id,
123 subject=>$subject,
124 message_from=>$from,
125 message_to=>join(",", @receiver_l),
126 flag=>"n",
127 direction=>"in",
128 delivery_time=>$delivery_time,
129 message=>$message,
130 timestamp=>&get_time(),
131 };
132 my $res = $main::messaging_db->add_dbentry($func_dic);
133 if (not $res == 0) {
134 &main::daemon_log("$session_id ERROR: gosaTriggered.pm: cannot add message to message_db: $res", 1);
135 } else {
136 &main::daemon_log("$session_id INFO: gosaTriggered.pm: message with subject '$subject' successfully added to message_db", 5);
137 }
139 return;
140 }
142 sub send_user_msg_OLD {
143 my ($msg, $msg_hash, $session_id) = @_ ;
144 my @out_msg_l;
145 my @user_list;
146 my @group_list;
148 my $header = @{$msg_hash->{'header'}}[0];
149 my $source = @{$msg_hash->{'source'}}[0];
150 my $target = @{$msg_hash->{'target'}}[0];
151 my $message = @{$msg_hash->{'message'}}[0];
152 if( exists $msg_hash->{'user'} ) { @user_list = @{$msg_hash->{'user'}}; }
153 if( exists $msg_hash->{'group'} ) { @group_list = @{$msg_hash->{'group'}}; }
155 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
156 if( defined $jobdb_id) {
157 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
158 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
159 my $res = $main::job_db->exec_statement($sql_statement);
160 }
162 # error handling
163 if( not @user_list && not @group_list ) {
164 &main::daemon_log("$session_id WARNING: no user-tag or a group-tag specified in 'send_user_msg'", 3);
165 return ("<xml><header>$header</header><source>GOSA</source><target>GOSA</target>".
166 "<error_string>no user-tag or a group-tag specified in 'send_user_msg'</error_string></xml>");
167 }
168 if( not defined $message ) {
169 &main::daemon_log("$session_id WARNING: no message-tag specified in 'send_user_msg'", 3);
170 return ("<xml><header>$header</header><source>GOSA</source><target>GOSA</target>".
171 "<error_string>no message-tag specified in 'send_user_msg'</error_string></xml>");
173 }
175 # resolve groups to users
176 my $ldap_handle = &main::get_ldap_handle($session_id);
177 if( @group_list ) {
178 if( not defined $ldap_handle ) {
179 &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
180 return ();
181 }
182 foreach my $group (@group_list) { # Perform search
183 my $mesg = $ldap_handle->search(
184 base => $main::ldap_base,
185 scope => 'sub',
186 attrs => ['memberUid'],
187 filter => "(&(objectClass=posixGroup)(cn=$group)(memberUid=*))");
188 if($mesg->code) {
189 &main::daemon_log($mesg->error, 1);
190 return ();
191 }
192 my $entry= $mesg->entry(0);
193 my @users= $entry->get_value("memberUid");
194 foreach my $user (@users) { push(@user_list, $user); }
195 }
196 }
198 # drop multiple users in @user_list
199 my %seen = ();
200 foreach my $user (@user_list) {
201 $seen{$user}++;
202 }
203 @user_list = keys %seen;
205 # build xml messages sended to client where user is logged in
206 foreach my $user (@user_list) {
207 my $sql_statement = "SELECT * FROM $main::login_users_tn WHERE user='$user'";
208 my $db_res = $main::login_users_db->select_dbentry($sql_statement);
210 if(0 == keys(%{$db_res})) {
212 } else {
213 while( my($hit, $content) = each %{$db_res} ) {
214 my $out_hash = &create_xml_hash('send_user_msg', $main::server_address, $content->{'client'});
215 &add_content2xml_hash($out_hash, 'message', $message);
216 &add_content2xml_hash($out_hash, 'user', $user);
217 if( exists $msg_hash->{'jobdb_id'} ) {
218 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
219 }
220 my $out_msg = &create_xml_string($out_hash);
221 push(@out_msg_l, $out_msg);
222 }
223 }
224 }
226 return @out_msg_l;
227 }
230 sub recreate_fai_server_db {
231 my ($msg, $msg_hash, $session_id) = @_ ;
232 my $out_msg;
234 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
235 if( defined $jobdb_id) {
236 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
237 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
238 my $res = $main::job_db->exec_statement($sql_statement);
239 }
241 $main::fai_server_db->create_table("new_fai_server", \@main::fai_server_col_names);
242 &main::create_fai_server_db("new_fai_server",undef,"dont", $session_id);
243 $main::fai_server_db->move_table("new_fai_server", $main::fai_server_tn);
245 my @out_msg_l = ( $out_msg );
246 return @out_msg_l;
247 }
250 sub recreate_fai_release_db {
251 my ($msg, $msg_hash, $session_id) = @_ ;
252 my $out_msg;
254 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
255 if( defined $jobdb_id) {
256 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
257 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
258 my $res = $main::job_db->exec_statement($sql_statement);
259 }
261 $main::fai_release_db->create_table("new_fai_release", \@main::fai_release_col_names);
262 &main::create_fai_release_db("new_fai_release", $session_id);
263 $main::fai_release_db->move_table("new_fai_release", $main::fai_release_tn);
265 my @out_msg_l = ( $out_msg );
266 return @out_msg_l;
267 }
270 sub recreate_packages_list_db {
271 my ($msg, $msg_hash, $session_id) = @_ ;
272 my $out_msg;
274 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
275 if( defined $jobdb_id) {
276 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
277 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
278 my $res = $main::job_db->exec_statement($sql_statement);
279 }
281 &main::create_packages_list_db;
283 my @out_msg_l = ( $out_msg );
284 return @out_msg_l;
285 }
288 sub get_login_usr_for_client {
289 my ($msg, $msg_hash, $session_id) = @_ ;
290 my $header = @{$msg_hash->{'header'}}[0];
291 my $source = @{$msg_hash->{'source'}}[0];
292 my $target = @{$msg_hash->{'target'}}[0];
293 my $client = @{$msg_hash->{'client'}}[0];
295 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
296 if( defined $jobdb_id) {
297 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
298 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
299 my $res = $main::job_db->exec_statement($sql_statement);
300 }
302 $header =~ s/^gosa_//;
304 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$client' OR macaddress LIKE '$client'";
305 my $res = $main::known_clients_db->select_dbentry($sql_statement);
307 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
308 $out_msg .= &db_res2xml($res);
309 $out_msg .= "</xml>";
311 my @out_msg_l = ( $out_msg );
312 return @out_msg_l;
313 }
316 sub get_client_for_login_usr {
317 my ($msg, $msg_hash, $session_id) = @_ ;
318 my $header = @{$msg_hash->{'header'}}[0];
319 my $source = @{$msg_hash->{'source'}}[0];
320 my $target = @{$msg_hash->{'target'}}[0];
322 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
323 if( defined $jobdb_id) {
324 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
325 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
326 my $res = $main::job_db->exec_statement($sql_statement);
327 }
329 my $usr = @{$msg_hash->{'usr'}}[0];
330 $header =~ s/^gosa_//;
332 my $sql_statement = "SELECT * FROM known_clients WHERE login LIKE '%$usr%'";
333 my $res = $main::known_clients_db->select_dbentry($sql_statement);
335 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
336 $out_msg .= &db_res2xml($res);
337 $out_msg .= "</xml>";
338 my @out_msg_l = ( $out_msg );
339 return @out_msg_l;
341 }
344 sub ping {
345 my ($msg, $msg_hash, $session_id) = @_ ;
346 my $out_msg = $msg;
347 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
348 if( defined $jobdb_id) {
349 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
350 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
351 my $res = $main::job_db->exec_statement($sql_statement);
352 }
354 $out_msg =~ s/<header>gosa_/<header>/;
356 my @out_msg_l = ( $out_msg );
357 return @out_msg_l;
358 }
359 sub new_ping {
360 my ($msg, $msg_hash, $session_id) = @_ ;
361 my ($sql, $res);
362 my $out_msg = $msg;
363 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
364 if( defined $jobdb_id) {
365 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
366 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
367 my $res = $main::job_db->exec_statement($sql_statement);
368 }
370 $out_msg =~ s/<header>gosa_/<header>/;
372 # send message
373 my $header = @{$msg_hash->{header}}[0];
374 my $target = @{$msg_hash->{target}}[0];
376 $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))";
377 $res = $main::known_clients_db->exec_statement($sql);
378 my $host_name = @{@$res[0]}[0];
379 $out_msg =~ s/<target>\S+<\/target>/<target>$host_name<\/target>/;
380 $out_msg =~ s/<source>\S+<\/source>/<source>$main::server_address<\/source>/;
381 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
382 my $host_key = @{@$res[0]}[2];
384 my $error = &main::send_msg_to_target($out_msg, $host_name, $host_key, $header, $session_id);
385 #if ($error != 0) {}
387 my $message_id;
388 while (1) {
389 $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
390 $res = $main::incoming_db->exec_statement($sql);
391 if (ref @$res[0] eq "ARRAY") {
392 $message_id = @{@$res[0]}[0];
393 last;
394 }
395 usleep(100000);
396 }
397 my $answer_xml = @{@$res[0]}[3];
398 my %data = ( 'answer_xml' => 'bin noch da' );
399 my $answer_msg = &build_msg("got_new_ping", "$main::server_address", "GOSA", \%data);
401 $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id";
402 $res = $main::incoming_db->exec_statement($sql);
405 my @answer_msg_l = ( $answer_msg );
406 return @answer_msg_l;
407 }
411 sub gen_smb_hash {
412 my ($msg, $msg_hash, $session_id) = @_ ;
413 my $source = @{$msg_hash->{source}}[0];
414 my $target = @{$msg_hash->{target}}[0];
415 my $password = @{$msg_hash->{password}}[0];
417 my %data= ('hash' => join(q[:], ntlmgen $password));
418 my $out_msg = &build_msg("gen_smb_hash", $target, 'GOSA', \%data );
419 return ( $out_msg );
420 }
423 sub network_completition {
424 my ($msg, $msg_hash, $session_id) = @_ ;
425 my $source = @{$msg_hash->{source}}[0];
426 my $target = @{$msg_hash->{target}}[0];
427 my $name = @{$msg_hash->{hostname}}[0];
429 # Can we resolv the name?
430 my %data;
431 if (inet_aton($name)){
432 my $address = inet_ntoa(inet_aton($name));
433 my $p = Net::Ping->new('tcp');
434 my $mac= "";
435 if ($p->ping($address, 1)){
436 $mac = Net::ARP::arp_lookup("", $address);
437 }
439 %data= ('ip' => $address, 'mac' => $mac);
440 } else {
441 %data= ('ip' => '', 'mac' => '');
442 }
444 my $out_msg = &build_msg("network_completition", $target, 'GOSA', \%data );
446 return ( $out_msg );
447 }
450 sub detect_hardware {
451 my ($msg, $msg_hash, $session_id) = @_ ;
452 # just forward msg to client, but dont forget to split off 'gosa_' in header
453 my $source = @{$msg_hash->{source}}[0];
454 my $target = @{$msg_hash->{target}}[0];
455 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
456 if( defined $jobdb_id) {
457 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
458 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
459 my $res = $main::job_db->exec_statement($sql_statement);
460 }
462 my $out_hash = &create_xml_hash("detect_hardware", $source, $target);
463 if( defined $jobdb_id ) {
464 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
465 }
466 my $out_msg = &create_xml_string($out_hash);
468 my @out_msg_l = ( $out_msg );
469 return @out_msg_l;
471 }
474 sub trigger_reload_ldap_config {
475 my ($msg, $msg_hash, $session_id) = @_ ;
476 my $target = @{$msg_hash->{target}}[0];
478 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
479 if( defined $jobdb_id) {
480 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
481 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
482 my $res = $main::job_db->exec_statement($sql_statement);
483 }
485 my $out_msg = &ClientPackages::new_ldap_config($target, $session_id);
486 my @out_msg_l = ( $out_msg );
488 return @out_msg_l;
489 }
492 sub set_activated_for_installation {
493 my ($msg, $msg_hash, $session_id) = @_;
494 my $header = @{$msg_hash->{header}}[0];
495 my $source = @{$msg_hash->{source}}[0];
496 my $target = @{$msg_hash->{target}}[0];
497 my @out_msg_l;
499 # update status of job
500 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
501 if( defined $jobdb_id) {
502 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
503 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
504 my $res = $main::job_db->exec_statement($sql_statement);
505 }
507 # create set_activated_for_installation message for delivery
508 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
509 if( defined $jobdb_id ) {
510 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
511 }
512 my $out_msg = &create_xml_string($out_hash);
513 push(@out_msg_l, $out_msg);
515 return @out_msg_l;
516 }
519 sub trigger_action_faireboot {
520 my ($msg, $msg_hash, $session_id) = @_;
521 my $macaddress = @{$msg_hash->{target}}[0];
522 my $source = @{$msg_hash->{source}}[0];
524 my @out_msg_l;
525 $msg =~ s/<header>gosa_trigger_action_faireboot<\/header>/<header>trigger_action_faireboot<\/header>/;
526 push(@out_msg_l, $msg);
528 &main::change_goto_state('locked', \@{$msg_hash->{target}}, $session_id);
529 &main::change_fai_state('install', \@{$msg_hash->{target}}, $session_id);
531 # delete all jobs from jobqueue which correspond to fai
532 my $sql_statement = "DELETE FROM $main::job_queue_tn WHERE (macaddress='$macaddress' AND ".
533 "status='processing')";
534 $main::job_db->del_dbentry($sql_statement );
536 return @out_msg_l;
537 }
540 sub trigger_action_lock {
541 my ($msg, $msg_hash, $session_id) = @_;
542 my $macaddress = @{$msg_hash->{target}}[0];
543 my $source = @{$msg_hash->{source}}[0];
545 &main::change_goto_state('locked', \@{$msg_hash->{target}}, $session_id);
546 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
547 if( defined $jobdb_id) {
548 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
549 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
550 my $res = $main::job_db->exec_statement($sql_statement);
551 }
553 my @out_msg_l;
554 return @out_msg_l;
555 }
558 sub trigger_action_activate {
559 my ($msg, $msg_hash, $session_id) = @_;
560 my $macaddress = @{$msg_hash->{target}}[0];
561 my $source = @{$msg_hash->{source}}[0];
563 &main::change_goto_state('active', \@{$msg_hash->{target}}, $session_id);
564 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
565 if( defined $jobdb_id) {
566 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
567 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
568 my $res = $main::job_db->exec_statement($sql_statement);
569 }
571 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $macaddress);
572 if( exists $msg_hash->{'jobdb_id'} ) {
573 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
574 }
575 my $out_msg = &create_xml_string($out_hash);
577 return ( $out_msg );
578 }
581 sub trigger_action_localboot {
582 my ($msg, $msg_hash, $session_id) = @_;
583 $msg =~ s/<header>gosa_trigger_action_localboot<\/header>/<header>trigger_action_localboot<\/header>/;
584 &main::change_fai_state('localboot', \@{$msg_hash->{target}}, $session_id);
585 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
586 if( defined $jobdb_id) {
587 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
588 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
589 my $res = $main::job_db->exec_statement($sql_statement);
590 }
592 my @out_msg_l = ($msg);
593 return @out_msg_l;
594 }
597 sub trigger_action_halt {
598 my ($msg, $msg_hash, $session_id) = @_;
599 $msg =~ s/<header>gosa_trigger_action_halt<\/header>/<header>trigger_action_halt<\/header>/;
601 &main::change_fai_state('halt', \@{$msg_hash->{target}}, $session_id);
602 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
603 if( defined $jobdb_id) {
604 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
605 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
606 my $res = $main::job_db->exec_statement($sql_statement);
607 }
609 my @out_msg_l = ($msg);
610 return @out_msg_l;
611 }
614 sub trigger_action_reboot {
615 my ($msg, $msg_hash, $session_id) = @_;
616 $msg =~ s/<header>gosa_trigger_action_reboot<\/header>/<header>trigger_action_reboot<\/header>/;
618 &main::change_fai_state('reboot', \@{$msg_hash->{target}}, $session_id);
619 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
620 if( defined $jobdb_id) {
621 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
622 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
623 my $res = $main::job_db->exec_statement($sql_statement);
624 }
626 my @out_msg_l = ($msg);
627 return @out_msg_l;
628 }
631 sub trigger_action_memcheck {
632 my ($msg, $msg_hash, $session_id) = @_ ;
633 $msg =~ s/<header>gosa_trigger_action_memcheck<\/header>/<header>trigger_action_memcheck<\/header>/;
635 &main::change_fai_state('memcheck', \@{$msg_hash->{target}}, $session_id);
636 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
637 if( defined $jobdb_id) {
638 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
639 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
640 my $res = $main::job_db->exec_statement($sql_statement);
641 }
643 my @out_msg_l = ($msg);
644 return @out_msg_l;
645 }
648 sub trigger_action_reinstall {
649 my ($msg, $msg_hash, $session_id) = @_;
650 $msg =~ s/<header>gosa_trigger_action_reinstall<\/header>/<header>trigger_action_reinstall<\/header>/;
652 &main::change_fai_state('reinstall', \@{$msg_hash->{target}}, $session_id);
654 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
655 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
656 my @out_msg_l = ($wake_msg, $msg);
657 return @out_msg_l;
658 }
661 sub trigger_action_update {
662 my ($msg, $msg_hash, $session_id) = @_;
663 $msg =~ s/<header>gosa_trigger_action_update<\/header>/<header>trigger_action_update<\/header>/;
665 &main::change_fai_state('update', \@{$msg_hash->{target}}, $session_id);
667 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
668 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
669 my @out_msg_l = ($wake_msg, $msg);
670 return @out_msg_l;
671 }
674 sub trigger_action_instant_update {
675 my ($msg, $msg_hash, $session_id) = @_;
676 $msg =~ s/<header>gosa_trigger_action_instant_update<\/header>/<header>trigger_action_instant_update<\/header>/;
678 &main::change_fai_state('update', \@{$msg_hash->{target}}, $session_id);
680 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
681 if( defined $jobdb_id) {
682 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
683 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
684 my $res = $main::job_db->exec_statement($sql_statement);
685 }
687 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
688 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
689 my @out_msg_l = ($wake_msg, $msg);
690 return @out_msg_l;
691 }
694 sub trigger_action_sysinfo {
695 my ($msg, $msg_hash, $session_id) = @_;
696 $msg =~ s/<header>gosa_trigger_action_sysinfo<\/header>/<header>trigger_action_sysinfo<\/header>/;
698 &main::change_fai_state('sysinfo', \@{$msg_hash->{target}}, $session_id);
699 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
700 if( defined $jobdb_id) {
701 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
702 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
703 my $res = $main::job_db->exec_statement($sql_statement);
704 }
706 my @out_msg_l = ($msg);
707 return @out_msg_l;
708 }
711 sub new_key_for_client {
712 my ($msg, $msg_hash, $session_id) = @_;
714 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
715 if( defined $jobdb_id) {
716 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
717 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
718 my $res = $main::job_db->exec_statement($sql_statement);
719 }
721 $msg =~ s/<header>gosa_new_key_for_client<\/header>/<header>new_key<\/header>/;
722 my @out_msg_l = ($msg);
723 return @out_msg_l;
724 }
727 sub trigger_action_rescan {
728 my ($msg, $msg_hash, $session_id) = @_;
730 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
731 if( defined $jobdb_id) {
732 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
733 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
734 my $res = $main::job_db->exec_statement($sql_statement);
735 }
738 $msg =~ s/<header>gosa_trigger_action_rescan<\/header>/<header>trigger_action_rescan<\/header>/;
739 my @out_msg_l = ($msg);
740 return @out_msg_l;
741 }
744 sub trigger_action_wake {
745 my ($msg, $msg_hash, $session_id) = @_;
747 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
748 if( defined $jobdb_id) {
749 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=jobdb_id";
750 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
751 my $res = $main::job_db->exec_statement($sql_statement);
752 }
755 my %data = ( 'macAddress' => \@{$msg_hash->{target}} );
756 my $out_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
757 my @out_msg_l = ($out_msg);
758 return @out_msg_l;
759 }
762 sub get_available_kernel {
763 my ($msg, $msg_hash, $session_id) = @_;
765 my $source = @{$msg_hash->{'source'}}[0];
766 my $target = @{$msg_hash->{'target'}}[0];
767 my $release= @{$msg_hash->{'release'}}[0];
769 my @kernel;
770 # Get Kernel packages for release
771 my $sql_statement = "SELECT * FROM $main::packages_list_tn WHERE distribution='$release' AND package LIKE 'linux\-image\-%'";
772 my $res_hash = $main::packages_list_db->select_dbentry($sql_statement);
773 my %data;
774 my $i=1;
776 foreach my $package (keys %{$res_hash}) {
777 $data{"answer".$i++}= $data{"answer".$i++}= ${$res_hash}{$package}->{'package'};
778 }
779 $data{"answer".$i++}= "default";
781 my $out_msg = &build_msg("get_available_kernel", $target, "GOSA", \%data);
782 return ( $out_msg );
783 }
786 sub trigger_activate_new {
787 my ($msg, $msg_hash, $session_id) = @_;
789 my $source = @{$msg_hash->{'source'}}[0];
790 my $target = @{$msg_hash->{'target'}}[0];
791 my $header= @{$msg_hash->{'header'}}[0];
792 &main::daemon_log(Dumper($msg_hash));
793 my $mac= (defined($msg_hash->{'mac'}))?@{$msg_hash->{'mac'}}[0]:undef;
794 my $ogroup= (defined($msg_hash->{'ogroup'}))?@{$msg_hash->{'ogroup'}}[0]:undef;
795 my $timestamp= (defined($msg_hash->{'timestamp'}))?@{$msg_hash->{'timestamp'}}[0]:undef;
796 my $base= (defined($msg_hash->{'base'}))?@{$msg_hash->{'base'}}[0]:undef;
797 my $hostname= (defined($msg_hash->{'fqdn'}))?@{$msg_hash->{'fqdn'}}[0]:undef;
798 my $ip_address= (defined($msg_hash->{'ip'}))?@{$msg_hash->{'ip'}}[0]:undef;
799 my $dhcp_statement= (defined($msg_hash->{'dhcp'}))?@{$msg_hash->{'dhcp'}}[0]:undef;
800 my $jobdb_id= (defined($msg_hash->{'jobdb_id'}))?@{$msg_hash->{'jobdb_id'}}[0]:undef;
802 my $ldap_handle = &main::get_ldap_handle();
803 my $ldap_entry;
804 my $ogroup_entry;
806 # build the base, use optional base parameter or take it from ogroup
807 if(!(defined($base) && (length($base) > 0))) {
808 my $ldap_mesg= $ldap_handle->search(
809 base => $main::ldap_base,
810 scope => 'sub',
811 filter => "(&(objectClass=gosaGroupOfnames)(cn=$ogroup))",
812 );
813 if($ldap_mesg->count == 1) {
814 $ogroup_entry= $ldap_mesg->pop_entry();
816 # Subtract the ObjectGroup cn
817 $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
818 } elsif ($ldap_mesg->count == 0) {
819 &main::daemon_log("ERROR: A GosaGroupOfNames with cn '$ogroup' was not found in base '".$main::ldap_base."'!", 1);
820 } else {
821 &main::daemon_log("ERROR: More than one ObjectGroups with cn '$ogroup' was found in base '".$main::ldap_base."'!", 1);
822 }
823 }
825 # prepend ou=systems
826 $base = "ou=systems,".$base;
828 # Search for an existing entry (should be in ou=incoming)
829 my $ldap_mesg= $ldap_handle->search(
830 base => $main::ldap_base,
831 scope => 'sub',
832 filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
833 );
835 # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
837 if($ldap_mesg->count == 1) {
838 &main::daemon_log("NOTICE: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
839 # Get the entry from LDAP
840 $ldap_entry= $ldap_mesg->pop_entry();
842 if(!($ldap_entry->dn() eq "cn=".$ldap_entry->get_value('cn').",$base")) {
843 # Move the entry to the new ou
844 $ldap_entry->changetype('moddn');
845 $ldap_entry->add(
846 'newrdn' => "cn=".$ldap_entry->get_value('cn'),
847 'deleteoldrdn' => "1",
848 'newsuperior' => $base,
849 );
850 my $mesg = $ldap_entry->update($ldap_handle);
851 if($mesg->code() != 0) {
852 &main::daemon_log("ERROR: Updating the dn for system with mac address '$mac' failed (code ".$mesg->code().") with '".$mesg->{'errorMessage'}."'!", 1);
853 }
854 }
855 # Check for needed objectClasses
856 my $oclasses = $ldap_entry->get_value('objectClass', asref => 1);
857 foreach my $oclass ("FAIobject", "GOhard") {
858 if(!(scalar grep $_ eq $oclass, map {$_ => 1} @$oclasses)) {
859 $ldap_entry->add(
860 objectClass => $oclass,
861 );
862 }
863 }
865 # Set FAIstate
866 if(defined($ldap_entry->get_value('FAIstate'))) {
867 if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
868 $ldap_entry->replace(
869 'FAIstate' => 'install'
870 );
871 }
872 } else {
873 $ldap_entry->add(
874 'FAIstate' => 'install'
875 );
876 }
878 my $faistate_mesg = $ldap_entry->update($ldap_handle);
879 if ($faistate_mesg->code() != 0) {
880 &main::daemon_log("ERROR: Updating the FAIstate for '".$ldap_entry->dn()."' failed (code '".$faistate_mesg->code()."') with '$@'!", 1);
881 }
883 } elsif ($ldap_mesg->count == 0) {
884 # TODO: Create a new entry
885 # $ldap_entry = Net::LDAP::Entry->new();
886 # $ldap_entry->dn("cn=$mac,$base");
887 &main::daemon_log("WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
888 $main::job_db->select_dbentry("UPDATE jobs SET state = 'waiting', timestamp = '".&get_time()."' WHERE header = 'trigger_activate_new' AND mac_address LIKE '$mac'");
889 } else {
890 &main::daemon_log("ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
891 }
893 my $update_mesg = $ldap_entry->update($ldap_handle);
894 if ($update_mesg->code() != 0) {
895 &main::daemon_log("ERROR: Updating attributes for '".$ldap_entry->dn()."' failed (code '".$update_mesg->code()."') with '".$update_mesg->{'errorMessage'}."'!", 1);
896 }
898 # Add to ObjectGroup
899 if(!(scalar grep $_, map {$_ => 1} $ogroup_entry->get_value('member', asref => 1))) {
900 $ogroup_entry->add (
901 'member' => $ldap_entry->dn(),
902 );
903 my $ogroup_result = $ogroup_entry->update($ldap_handle);
904 if ($ogroup_result->code() != 0) {
905 &main::daemon_log("ERROR: Updating the ObjectGroup '$ogroup' failed (code '".$ogroup_result->code()."') with '".$ogroup_result->{'errorMessage'}."'!", 1);
906 &main::daemon_log(Dumper($ogroup_result));
907 }
908 }
910 # Finally set gotoMode to active
911 if(defined($ldap_entry->get_value('gotoMode'))) {
912 if(!($ldap_entry->get_value('gotoMode') eq 'active')) {
913 $ldap_entry->replace(
914 'gotoMode' => 'active'
915 );
916 }
917 } else {
918 $ldap_entry->add(
919 'gotoMode' => 'active'
920 );
921 }
922 my $activate_result = $ldap_entry->update($ldap_handle);
923 if ($activate_result->code() != 0) {
924 &main::daemon_log("ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
925 }
928 my %data;
929 my $out_msg = &build_msg("activate_new", $target, "GOSA", \%data);
930 return ( $out_msg );
931 }
934 1;