d7ef85131a5be55d105bf993c19044a0a3b85ada
1 ## @file
2 # @details A GOsa-SI event module containing all functions common used by GOsa
3 # @brief Implementation of a GOsa-SI event module.
5 package gosaTriggered;
6 use Exporter;
7 @ISA = qw(Exporter);
8 my @events = (
9 "get_events",
10 "get_login_usr_for_client",
11 "get_client_for_login_usr",
12 "gen_smb_hash",
13 "trigger_reload_syslog_config",
14 "trigger_reload_ntp_config",
15 "trigger_reload_ldap_config",
16 "ping",
17 "network_completition",
18 "set_activated_for_installation",
19 "new_key_for_client",
20 "detect_hardware",
21 "get_login_usr",
22 "get_login_client",
23 "trigger_action_localboot",
24 "trigger_action_faireboot",
25 "trigger_action_reboot",
26 "trigger_action_activate",
27 "trigger_action_lock",
28 "trigger_action_halt",
29 "trigger_action_update",
30 "trigger_action_reinstall",
31 "trigger_action_memcheck",
32 "trigger_action_sysinfo",
33 "trigger_action_instant_update",
34 "trigger_action_rescan",
35 "trigger_action_wake",
36 "recreate_fai_server_db",
37 "recreate_fai_release_db",
38 "recreate_packages_list_db",
39 "send_user_msg",
40 "get_available_kernel",
41 "trigger_activate_new",
42 "get_hosts_with_module",
43 # "get_dak_keyring",
44 # "import_dak_key",
45 # "remove_dak_key",
46 # "get_dak_queue",
47 );
48 @EXPORT = @events;
50 use strict;
51 use warnings;
52 use GOSA::GosaSupportDaemon;
53 use Data::Dumper;
54 use Crypt::SmbHash;
55 use Net::ARP;
56 use Net::Ping;
57 use Socket;
58 use Time::HiRes qw( usleep);
59 use MIME::Base64;
61 BEGIN {}
63 END {}
65 ### Start ######################################################################
67 ## @method get_events()
68 # A brief function returning a list of functions which are exported by importing the module.
69 # @return List of all provided functions
70 sub get_events {
71 return \@events;
72 }
74 ## @method send_usr_msg($msg, $msg_hash, $session_id)
75 # This function accepts usr messages from GOsa, split mulitple target messages to mulitiple single target messages and put all messages into messaging_db
76 # @param msg - STRING - xml message
77 # @param msg_hash - HASHREF - message information parsed into a hash
78 # @param session_id - INTEGER - POE session id of the processing of this message
79 # @return (out_msg) - ARRAY - Array containing the answer message from client
80 sub send_user_msg {
81 my ($msg, $msg_hash, $session_id) = @_ ;
82 my $header = @{$msg_hash->{'header'}}[0];
83 my $source = @{$msg_hash->{'source'}}[0];
84 my $target = @{$msg_hash->{'target'}}[0];
86 #my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]); # just for debugging
87 my $subject = @{$msg_hash->{'subject'}}[0];
88 my $from = @{$msg_hash->{'from'}}[0];
89 my @users = exists $msg_hash->{'user'} ? @{$msg_hash->{'user'}} : () ;
90 my @groups = exists $msg_hash->{'group'} ? @{$msg_hash->{'group'}} : ();
91 my $delivery_time = @{$msg_hash->{'delivery_time'}}[0];
92 #my $message = &decode_base64(@{$msg_hash->{'message'}}[0]); # just for debugging
93 my $message = @{$msg_hash->{'message'}}[0];
95 # keep job queue uptodate if necessary
96 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
97 if( defined $jobdb_id) {
98 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
99 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
100 my $res = $main::job_db->exec_statement($sql_statement);
101 }
103 # error handling
104 if (not $delivery_time =~ /^\d{14}$/) {
105 my $error_string = "delivery_time '$delivery_time' is not a valid timestamp, please use format 'yyyymmddhhmmss'";
106 &main::daemon_log("$session_id ERROR: $error_string", 1);
107 return &create_xml_string(&create_xml_hash($header, $target, $source, $error_string));
108 }
110 # determine new message id
111 my $new_msg_id = 1;
112 my $new_msg_id_sql = "SELECT MAX(id) FROM $main::messaging_tn";
113 my $new_msg_id_res = $main::messaging_db->exec_statement($new_msg_id_sql);
114 if (defined @{@{$new_msg_id_res}[0]}[0] ) {
115 $new_msg_id = int(@{@{$new_msg_id_res}[0]}[0]);
116 $new_msg_id += 1;
117 }
119 # highlight user name and group name
120 my @receiver_l;
121 @users = map(push(@receiver_l, "u_$_"), @users);
122 @groups = map(push(@receiver_l, "g_$_"), @groups);
124 # Sanitiy check of receivers list
125 if (@receiver_l == 0) {
126 &main::daemon_log("$session_id ERROR: 'send_usr_msg'-message contains neither a 'usr' nor a 'group' tag. No receiver specified.", 1);
127 return;
128 }
130 # add incoming message to messaging_db
131 my $func_dic = {table=>$main::messaging_tn,
132 primkey=>[],
133 id=>$new_msg_id,
134 subject=>$subject,
135 message_from=>$from,
136 message_to=>join(",", @receiver_l),
137 flag=>"n",
138 direction=>"in",
139 delivery_time=>$delivery_time,
140 message=>$message,
141 timestamp=>&get_time(),
142 };
143 my $res = $main::messaging_db->add_dbentry($func_dic);
144 if (not $res == 0) {
145 &main::daemon_log("$session_id ERROR: gosaTriggered.pm: cannot add message to message_db: $res", 1);
146 } else {
147 &main::daemon_log("$session_id INFO: gosaTriggered.pm: message with subject '".&decode_base64($subject)."' successfully added to message_db", 5);
148 }
150 return;
151 }
154 sub recreate_fai_server_db {
155 my ($msg, $msg_hash, $session_id) = @_ ;
156 my $out_msg;
158 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
159 if( defined $jobdb_id) {
160 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
161 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
162 my $res = $main::job_db->exec_statement($sql_statement);
163 }
165 my $res = $main::fai_server_db->create_table("new_fai_server", \@main::fai_server_col_names);
166 if ($res) {
167 return ( $out_msg );
168 }
169 &main::create_fai_server_db("new_fai_server",undef,"dont", $session_id);
170 $main::fai_server_db->move_table("new_fai_server", $main::fai_server_tn);
172 return ( $out_msg );
173 }
176 sub recreate_fai_release_db {
177 my ($msg, $msg_hash, $session_id) = @_ ;
178 my $out_msg;
180 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
181 if( defined $jobdb_id) {
182 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
183 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
184 my $res = $main::job_db->exec_statement($sql_statement);
185 }
187 my $res = $main::fai_release_db->create_table("new_fai_release", \@main::fai_release_col_names);
188 if ($res) {
189 return ( $out_msg );
190 }
191 &main::create_fai_release_db("new_fai_release", $session_id);
192 $main::fai_release_db->move_table("new_fai_release", $main::fai_release_tn);
194 return ( $out_msg );
195 }
198 sub recreate_packages_list_db {
199 my ($msg, $msg_hash, $session_id) = @_ ;
200 my $out_msg;
202 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
203 if( defined $jobdb_id) {
204 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
205 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
206 my $res = $main::job_db->exec_statement($sql_statement);
207 }
209 &main::create_packages_list_db(undef, undef, $session_id);
211 my @out_msg_l = ( $out_msg );
212 return @out_msg_l;
213 }
216 sub get_login_usr_for_client {
217 my ($msg, $msg_hash, $session_id) = @_ ;
218 my $header = @{$msg_hash->{'header'}}[0];
219 my $source = @{$msg_hash->{'source'}}[0];
220 my $target = @{$msg_hash->{'target'}}[0];
221 my $client = @{$msg_hash->{'client'}}[0];
223 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
224 if( defined $jobdb_id) {
225 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
226 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
227 my $res = $main::job_db->exec_statement($sql_statement);
228 }
230 $header =~ s/^gosa_//;
232 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$client' OR macaddress LIKE '$client'";
233 my $res = $main::known_clients_db->select_dbentry($sql_statement);
235 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
236 $out_msg .= &db_res2xml($res);
237 $out_msg .= "</xml>";
239 my @out_msg_l = ( $out_msg );
240 return @out_msg_l;
241 }
244 sub get_client_for_login_usr {
245 my ($msg, $msg_hash, $session_id) = @_ ;
246 my $header = @{$msg_hash->{'header'}}[0];
247 my $source = @{$msg_hash->{'source'}}[0];
248 my $target = @{$msg_hash->{'target'}}[0];
250 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
251 if( defined $jobdb_id) {
252 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
253 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
254 my $res = $main::job_db->exec_statement($sql_statement);
255 }
257 my $usr = @{$msg_hash->{'usr'}}[0];
258 $header =~ s/^gosa_//;
260 my $sql_statement = "SELECT * FROM known_clients WHERE login LIKE '%$usr%'";
261 my $res = $main::known_clients_db->select_dbentry($sql_statement);
263 my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
264 $out_msg .= &db_res2xml($res);
265 $out_msg .= "</xml>";
266 my @out_msg_l = ( $out_msg );
267 return @out_msg_l;
269 }
272 sub ping {
273 my ($msg, $msg_hash, $session_id) = @_ ;
274 my $header = @{$msg_hash->{header}}[0];
275 my $target = @{$msg_hash->{target}}[0];
276 my $source = @{$msg_hash->{source}}[0];
277 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
278 my $error = 0;
279 my $answer_msg;
280 my ($sql, $res);
282 if( defined $jobdb_id) {
283 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
284 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
285 my $res = $main::job_db->exec_statement($sql_statement);
286 }
288 # send message
289 $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))";
290 $res = $main::known_clients_db->exec_statement($sql);
292 # sanity check of db result
293 my ($host_name, $host_key);
294 if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {
295 $host_name = @{@$res[0]}[0];
296 $host_key = @{@$res[0]}[2];
297 } else {
298 &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db at function ping\n$msg", 1);
299 $error = 1;
300 }
302 if (not $error) {
303 my $client_hash = &create_xml_hash("ping", $main::server_address, $host_name);
304 &add_content2xml_hash($client_hash, 'session_id', $session_id);
305 my $client_msg = &create_xml_string($client_hash);
306 &main::send_msg_to_target($client_msg, $host_name, $host_key, $header, $session_id);
308 my $message_id;
309 my $i = 0;
310 while (1) {
311 $i++;
312 $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
313 $res = $main::incoming_db->exec_statement($sql);
314 if (ref @$res[0] eq "ARRAY") {
315 $message_id = @{@$res[0]}[0];
316 last;
317 }
319 # do not run into a endless loop
320 if ($i > 100) { last; }
321 usleep(100000);
322 }
324 # if an answer to the question exists
325 if (defined $message_id) {
326 my $answer_xml = @{@$res[0]}[3];
327 my %data = ( 'answer_xml' => 'bin noch da' );
328 $answer_msg = &build_msg("got_ping", $target, $source, \%data);
329 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
330 if (defined $forward_to_gosa){
331 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
332 }
333 $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id";
334 $res = $main::incoming_db->exec_statement($sql);
335 }
337 }
339 return ( $answer_msg );
340 }
344 sub gen_smb_hash {
345 my ($msg, $msg_hash, $session_id) = @_ ;
346 my $source = @{$msg_hash->{source}}[0];
347 my $target = @{$msg_hash->{target}}[0];
348 my $password = @{$msg_hash->{password}}[0];
350 my %data= ('hash' => join(q[:], ntlmgen $password));
351 my $out_msg = &build_msg("gen_smb_hash", $target, $source, \%data );
352 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
353 if (defined $forward_to_gosa) {
354 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
355 }
357 return ( $out_msg );
358 }
361 sub network_completition {
362 my ($msg, $msg_hash, $session_id) = @_ ;
363 my $source = @{$msg_hash->{source}}[0];
364 my $target = @{$msg_hash->{target}}[0];
365 my $name = @{$msg_hash->{hostname}}[0];
367 # Can we resolv the name?
368 my %data;
369 if (inet_aton($name)){
370 my $address = inet_ntoa(inet_aton($name));
371 my $p = Net::Ping->new('tcp');
372 my $mac= "";
373 if ($p->ping($address, 1)){
374 $mac = Net::ARP::arp_lookup("", $address);
375 }
377 %data= ('ip' => $address, 'mac' => $mac);
378 } else {
379 %data= ('ip' => '', 'mac' => '');
380 }
382 my $out_msg = &build_msg("network_completition", $target, $source, \%data );
383 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
384 if (defined $forward_to_gosa) {
385 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
386 }
388 return ( $out_msg );
389 }
392 sub detect_hardware {
393 my ($msg, $msg_hash, $session_id) = @_ ;
394 # just forward msg to client, but dont forget to split off 'gosa_' in header
395 my $source = @{$msg_hash->{source}}[0];
396 my $target = @{$msg_hash->{target}}[0];
397 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
398 if( defined $jobdb_id) {
399 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
400 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
401 my $res = $main::job_db->exec_statement($sql_statement);
402 }
404 my $out_hash = &create_xml_hash("detect_hardware", $source, $target);
405 if( defined $jobdb_id ) {
406 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
407 }
408 my $out_msg = &create_xml_string($out_hash);
410 my @out_msg_l = ( $out_msg );
411 return @out_msg_l;
413 }
415 sub trigger_reload_syslog_config {
416 my ($msg, $msg_hash, $session_id) = @_ ;
418 # Sanity check of macaddress
419 # TODO
421 my $macaddress = @{$msg_hash->{macaddress}}[0];
423 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
424 if( defined $jobdb_id) {
425 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
426 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
427 my $res = $main::job_db->exec_statement($sql_statement);
428 }
430 my $out_msg = &ClientPackages::new_syslog_config($macaddress, $session_id);
431 my @out_msg_l = ( $out_msg );
433 return @out_msg_l;
436 }
438 sub trigger_reload_ntp_config {
439 my ($msg, $msg_hash, $session_id) = @_ ;
441 # Sanity check of macaddress
442 # TODO
444 my $macaddress = @{$msg_hash->{macaddress}}[0];
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 = &ClientPackages::new_ntp_config($macaddress, $session_id);
454 my @out_msg_l = ( $out_msg );
456 return @out_msg_l;
458 }
460 sub trigger_reload_ldap_config {
461 my ($msg, $msg_hash, $session_id) = @_ ;
462 my $target = @{$msg_hash->{target}}[0];
464 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
465 if( defined $jobdb_id) {
466 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
467 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
468 my $res = $main::job_db->exec_statement($sql_statement);
469 }
471 my $out_msg = &ClientPackages::new_ldap_config($target, $session_id);
472 my @out_msg_l = ( $out_msg );
474 return @out_msg_l;
475 }
478 sub set_activated_for_installation {
479 my ($msg, $msg_hash, $session_id) = @_;
480 my $header = @{$msg_hash->{header}}[0];
481 my $source = @{$msg_hash->{source}}[0];
482 my $target = @{$msg_hash->{target}}[0];
483 my $mac= (defined($msg_hash->{'macaddress'}))?@{$msg_hash->{'macaddress'}}[0]:undef;
484 my @out_msg_l;
486 # update status of job
487 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
488 if( defined $jobdb_id) {
489 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
490 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
491 my $res = $main::job_db->exec_statement($sql_statement);
492 }
494 # If a client gets a 'set_activated_for_installation' msg, always deliver a fresh 'new_ldap_config'
495 # just for backup and robustness purposes
496 my $ldap_out_msg = &ClientPackages::new_ldap_config($mac, $session_id);
497 push(@out_msg_l, $ldap_out_msg);
499 # create set_activated_for_installation message for delivery
500 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
501 if( defined $jobdb_id ) {
502 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
503 }
504 my $out_msg = &create_xml_string($out_hash);
505 push(@out_msg_l, $out_msg);
507 return @out_msg_l;
508 }
511 sub trigger_action_faireboot {
512 my ($msg, $msg_hash, $session_id) = @_;
513 my $macaddress = @{$msg_hash->{macaddress}}[0];
514 my $source = @{$msg_hash->{source}}[0];
516 my @out_msg_l;
517 $msg =~ s/<header>gosa_trigger_action_faireboot<\/header>/<header>trigger_action_faireboot<\/header>/;
518 push(@out_msg_l, $msg);
520 &main::change_goto_state('locked', \@{$msg_hash->{macaddress}}, $session_id);
521 &main::change_fai_state('install', \@{$msg_hash->{macaddress}}, $session_id);
523 # set job to status 'done', job will be deleted automatically
524 my $sql_statement = "UPDATE $main::job_queue_tn ".
525 "SET status='done', modified='1'".
526 "WHERE (macaddress='$macaddress' AND status='processing')";
527 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
528 my $res = $main::job_db->update_dbentry( $sql_statement );
530 return @out_msg_l;
531 }
534 sub trigger_action_lock {
535 my ($msg, $msg_hash, $session_id) = @_;
536 my $macaddress = @{$msg_hash->{macaddress}}[0];
537 my $source = @{$msg_hash->{source}}[0];
539 &main::change_goto_state('locked', \@{$msg_hash->{macaddress}}, $session_id);
540 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
541 if( defined $jobdb_id) {
542 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
543 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
544 my $res = $main::job_db->exec_statement($sql_statement);
545 }
547 my @out_msg_l;
548 return @out_msg_l;
549 }
552 sub trigger_action_activate {
553 my ($msg, $msg_hash, $session_id) = @_;
554 my $macaddress = @{$msg_hash->{macaddress}}[0];
555 my $source = @{$msg_hash->{source}}[0];
557 &main::change_goto_state('active', \@{$msg_hash->{macaddress}}, $session_id);
558 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
559 if( defined $jobdb_id) {
560 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
561 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
562 my $res = $main::job_db->exec_statement($sql_statement);
563 }
565 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $macaddress);
566 if( exists $msg_hash->{'jobdb_id'} ) {
567 &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]);
568 }
569 my $out_msg = &create_xml_string($out_hash);
571 my @out_msg_l = ($out_msg);
572 return @out_msg_l;
574 }
577 sub trigger_action_localboot {
578 my ($msg, $msg_hash, $session_id) = @_;
579 $msg =~ s/<header>gosa_trigger_action_localboot<\/header>/<header>trigger_action_localboot<\/header>/;
580 &main::change_fai_state('localboot', \@{$msg_hash->{macaddress}}, $session_id);
581 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
582 if( defined $jobdb_id) {
583 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
584 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
585 my $res = $main::job_db->exec_statement($sql_statement);
586 }
588 my @out_msg_l = ($msg);
589 return @out_msg_l;
590 }
593 sub trigger_action_halt {
594 my ($msg, $msg_hash, $session_id) = @_;
595 $msg =~ s/<header>gosa_trigger_action_halt<\/header>/<header>trigger_action_halt<\/header>/;
597 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
598 if( defined $jobdb_id) {
599 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
600 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
601 my $res = $main::job_db->exec_statement($sql_statement);
602 }
604 my @out_msg_l = ($msg);
605 return @out_msg_l;
606 }
609 sub trigger_action_reboot {
610 my ($msg, $msg_hash, $session_id) = @_;
611 $msg =~ s/<header>gosa_trigger_action_reboot<\/header>/<header>trigger_action_reboot<\/header>/;
613 &main::change_fai_state('reboot', \@{$msg_hash->{macaddress}}, $session_id);
614 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
615 if( defined $jobdb_id) {
616 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
617 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
618 my $res = $main::job_db->exec_statement($sql_statement);
619 }
621 my @out_msg_l = ($msg);
622 return @out_msg_l;
623 }
626 sub trigger_action_memcheck {
627 my ($msg, $msg_hash, $session_id) = @_ ;
628 $msg =~ s/<header>gosa_trigger_action_memcheck<\/header>/<header>trigger_action_memcheck<\/header>/;
630 &main::change_fai_state('memcheck', \@{$msg_hash->{macaddress}}, $session_id);
631 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
632 if( defined $jobdb_id) {
633 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
634 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
635 my $res = $main::job_db->exec_statement($sql_statement);
636 }
638 my @out_msg_l = ($msg);
639 return @out_msg_l;
640 }
643 sub trigger_action_reinstall {
644 my ($msg, $msg_hash, $session_id) = @_;
645 $msg =~ s/<header>gosa_trigger_action_reinstall<\/header>/<header>trigger_action_reinstall<\/header>/;
647 &main::change_fai_state('reinstall', \@{$msg_hash->{macaddress}}, $session_id);
649 my %data = ( 'macaddress' => \@{$msg_hash->{macaddress}} );
650 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
651 # invoke trigger wake for this gosa-si-server
652 &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
654 my @out_msg_l = ($wake_msg, $msg);
655 return @out_msg_l;
656 }
659 sub trigger_action_update {
660 my ($msg, $msg_hash, $session_id) = @_;
661 $msg =~ s/<header>gosa_trigger_action_update<\/header>/<header>trigger_action_update<\/header>/;
663 &main::change_fai_state('update', \@{$msg_hash->{macaddress}}, $session_id);
665 my %data = ( 'macaddress' => \@{$msg_hash->{macaddress}} );
666 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
667 # invoke trigger wake for this gosa-si-server
668 &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
670 my @out_msg_l = ($wake_msg, $msg);
671 return @out_msg_l;
672 }
675 sub trigger_action_instant_update {
676 my ($msg, $msg_hash, $session_id) = @_;
677 $msg =~ s/<header>gosa_trigger_action_instant_update<\/header>/<header>trigger_action_instant_update<\/header>/;
679 &main::change_fai_state('update', \@{$msg_hash->{macaddress}}, $session_id);
681 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
682 if( defined $jobdb_id) {
683 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
684 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
685 my $res = $main::job_db->exec_statement($sql_statement);
686 }
689 my %data = ( 'macaddress' => \@{$msg_hash->{macaddress}} );
690 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
691 # invoke trigger wake for this gosa-si-server
692 &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
694 my @out_msg_l = ($wake_msg, $msg);
695 return @out_msg_l;
696 }
699 sub trigger_action_sysinfo {
700 my ($msg, $msg_hash, $session_id) = @_;
701 $msg =~ s/<header>gosa_trigger_action_sysinfo<\/header>/<header>trigger_action_sysinfo<\/header>/;
703 &main::change_fai_state('sysinfo', \@{$msg_hash->{macaddress}}, $session_id);
704 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
705 if( defined $jobdb_id) {
706 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
707 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
708 my $res = $main::job_db->exec_statement($sql_statement);
709 }
711 my @out_msg_l = ($msg);
712 return @out_msg_l;
713 }
716 sub new_key_for_client {
717 my ($msg, $msg_hash, $session_id) = @_;
719 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
720 if( defined $jobdb_id) {
721 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
722 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
723 my $res = $main::job_db->exec_statement($sql_statement);
724 }
726 $msg =~ s/<header>gosa_new_key_for_client<\/header>/<header>new_key<\/header>/;
727 my @out_msg_l = ($msg);
728 return @out_msg_l;
729 }
732 sub trigger_action_rescan {
733 my ($msg, $msg_hash, $session_id) = @_;
735 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
736 if( defined $jobdb_id) {
737 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
738 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
739 my $res = $main::job_db->exec_statement($sql_statement);
740 }
743 $msg =~ s/<header>gosa_trigger_action_rescan<\/header>/<header>detect_hardware<header>/;
744 my @out_msg_l = ($msg);
745 return @out_msg_l;
746 }
749 sub trigger_action_wake {
750 my ($msg, $msg_hash, $session_id) = @_;
752 my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
753 if( defined $jobdb_id) {
754 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
755 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
756 my $res = $main::job_db->exec_statement($sql_statement);
757 }
759 # build out message
760 my $out_hash = &create_xml_hash("trigger_wake", "GOSA", "KNOWN_SERVER");
761 foreach (@{$msg_hash->{'macaddress'}}) {
762 &add_content2xml_hash($out_hash, 'macaddress', $_);
763 }
764 if (defined $jobdb_id){
765 &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
766 }
767 my $out_msg = &create_xml_string($out_hash);
769 # invoke trigger wake for this gosa-si-server
770 &main::server_server_com::trigger_wake($out_msg, $out_hash, $session_id);
772 # send trigger wake to all other gosa-si-server
773 my @out_msg_l = ($out_msg);
774 return @out_msg_l;
775 }
778 sub get_available_kernel {
779 my ($msg, $msg_hash, $session_id) = @_;
781 my $source = @{$msg_hash->{'source'}}[0];
782 my $target = @{$msg_hash->{'target'}}[0];
783 my $fai_release= @{$msg_hash->{'fai_release'}}[0];
785 my @kernel;
786 # Get Kernel packages for release
787 my $sql_statement = "SELECT * FROM $main::packages_list_tn WHERE distribution='$fai_release' AND package LIKE 'linux\-image\-%'";
788 my $res_hash = $main::packages_list_db->select_dbentry($sql_statement);
789 my %data;
790 my $i=1;
792 foreach my $package (keys %{$res_hash}) {
793 $data{"answer".$i++}= $data{"answer".$i++}= ${$res_hash}{$package}->{'package'};
794 }
795 $data{"answer".$i++}= "default";
797 my $out_msg = &build_msg("get_available_kernel", $target, $source, \%data);
798 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
799 if (defined $forward_to_gosa) {
800 $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
801 }
803 return ( $out_msg );
804 }
806 sub trigger_activate_new {
807 my ($msg, $msg_hash, $session_id) = @_;
809 my $source = @{$msg_hash->{'source'}}[0];
810 my $target = @{$msg_hash->{'target'}}[0];
811 my $header= @{$msg_hash->{'header'}}[0];
812 my $mac= (defined($msg_hash->{'mac'}))?@{$msg_hash->{'mac'}}[0]:undef;
813 my $ogroup= (defined($msg_hash->{'ogroup'}))?@{$msg_hash->{'ogroup'}}[0]:undef;
814 my $timestamp= (defined($msg_hash->{'timestamp'}))?@{$msg_hash->{'timestamp'}}[0]:undef;
815 my $base= (defined($msg_hash->{'base'}))?@{$msg_hash->{'base'}}[0]:undef;
816 my $hostname= (defined($msg_hash->{'fqdn'}))?@{$msg_hash->{'fqdn'}}[0]:undef;
817 my $ip_address= (defined($msg_hash->{'ip'}))?@{$msg_hash->{'ip'}}[0]:undef;
818 my $dhcp_statement= (defined($msg_hash->{'dhcp'}))?@{$msg_hash->{'dhcp'}}[0]:undef;
819 my $jobdb_id= (defined($msg_hash->{'jobdb_id'}))?@{$msg_hash->{'jobdb_id'}}[0]:undef;
821 # Sanity check for base
822 if (ref($base) eq "HASH") {
823 # Incoming msg has a xml tag 'base' but no content
824 $base = undef;
825 }
827 # In case that the client is sleeping, wake it up
828 my %data = ( 'macaddress' => $mac );
829 my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
830 &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
831 my $sql_statement= "SELECT * FROM $main::known_server_tn";
832 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
833 while( my ($hit_num, $hit) = each %{ $query_res } ) {
834 my $host_name = $hit->{hostname};
835 my $host_key = $hit->{hostkey};
836 $wake_msg =~ s/<target>\S+<\/target>/<target>$host_name<\/target>/g;
837 my $error = &main::send_msg_to_target($wake_msg, $host_name, $host_key, $header, $session_id);
838 }
840 my $ldap_handle = &main::get_ldap_handle();
841 my $ldap_entry;
842 my $ogroup_entry;
843 my $changed_attributes_counter = 0;
845 my $activate_client = 0;
847 if(defined($ogroup)) {
848 my $ldap_mesg= $ldap_handle->search(
849 base => $main::ldap_base,
850 scope => 'sub',
851 filter => "(&(objectClass=gosaGroupOfnames)(cn=$ogroup))",
852 );
853 if($ldap_mesg->count == 1) {
854 $ogroup_entry= $ldap_mesg->pop_entry();
855 &main::daemon_log("$session_id DEBUG: A GosaGroupOfNames with cn '$ogroup' was found in base '".$main::ldap_base."'!", 5);
856 } elsif ($ldap_mesg->count == 0) {
857 &main::daemon_log("$session_id ERROR: A GosaGroupOfNames with cn '$ogroup' was not found in base '".$main::ldap_base."'!", 1);
858 $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
859 return undef;
860 } else {
861 &main::daemon_log("$session_id ERROR: More than one ObjectGroups with cn '$ogroup' was found in base '".$main::ldap_base."'!", 1);
862 $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
863 return undef;
864 }
866 # build the base, use optional base parameter or take it from ogroup
867 if(!(defined($base) && (length($base) > 0))) {
868 # Subtract the ObjectGroup cn
869 $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
870 &main::daemon_log("$session_id DEBUG: New base for system with mac address '$mac' is '$base'", 5);
871 }
872 }
874 # prepend ou=systems (configurable through config)
875 $base = $main::new_systems_ou.",".$base;
877 # Search for an existing entry (should be in ou=incoming)
878 my $ldap_mesg= $ldap_handle->search(
879 base => $main::ldap_base,
880 scope => 'sub',
881 filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
882 );
884 # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
885 if($ldap_mesg->count == 1) {
886 &main::daemon_log("$session_id DEBUG: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 5);
887 # Get the entry from LDAP
888 $ldap_entry= $ldap_mesg->pop_entry();
890 if(!($ldap_entry->dn() eq "cn=".$ldap_entry->get_value('cn').",$base")) {
891 # Move the entry to the new ou
892 $ldap_entry->changetype('moddn');
893 $ldap_entry->add(
894 newrdn => "cn=".$ldap_entry->get_value('cn'),
895 deleteoldrdn => 1,
896 newsuperior => $base,
897 );
898 # To prevent replication problems just re-queue the job with 10 seconds in the future
899 my $moddn_result = $ldap_entry->update($ldap_handle);
900 if ($moddn_result->code() != 0) {
901 my $error_string = "Moving the system with mac address '$mac' to new base '$base' failed (code '".$moddn_result->code()."') with '".$moddn_result->{'errorMessage'}."'!";
902 &main::daemon_log("$session_id ERROR: $error_string", 1);
903 my $sql = "UPDATE $main::job_queue_tn SET status='error', result='$error_string' WHERE id=$jobdb_id";
904 return undef;
905 } else {
906 &main::daemon_log("$session_id INFO: System with mac address '$mac' was moved to base '".$main::ldap_base."'! Re-queuing job.", 4);
907 $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 10))."' WHERE id = $jobdb_id");
908 return undef;
909 }
910 }
912 } elsif ($ldap_mesg->count == 0) {
913 &main::daemon_log("$session_id WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
914 my $sql_statement = "UPDATE ".$main::job_queue_tn.
915 " SET status='waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' ".
916 " WHERE id = $jobdb_id";
917 $main::job_db->exec_statement($sql_statement);
918 return undef;
919 }
921 $ldap_mesg= $ldap_handle->search(
922 base => $main::ldap_base,
923 scope => 'sub',
924 filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
925 );
927 # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
928 if($ldap_mesg->count == 1) {
929 $ldap_entry= $ldap_mesg->pop_entry();
930 # Check for needed objectClasses
931 my $oclasses = $ldap_entry->get_value('objectClass', asref => 1);
932 foreach my $oclass ("FAIobject", "GOhard", "gotoWorkstation") {
933 if(!(scalar grep $_ eq $oclass, map {$_ => 1} @$oclasses)) {
934 &main::daemon_log("$session_id INFO: Adding objectClass '$oclass' to system entry with mac adress '$mac'", 1);
935 $ldap_entry->add(
936 objectClass => $oclass,
937 );
938 my $oclass_result = $ldap_entry->update($ldap_handle);
939 if ($oclass_result->code() != 0) {
940 &main::daemon_log("$session_id ERROR: Adding the ObjectClass '$oclass' failed (code '".$oclass_result->code()."') with '".$oclass_result->{'errorMessage'}."'!", 1);
941 } else {
942 &main::daemon_log("$session_id DEBUG: Adding the ObjectClass '$oclass' to '".($ldap_entry->dn())."' succeeded!", 5);
943 }
944 }
945 }
947 # Set FAIstate
948 if(defined($ldap_entry->get_value('FAIstate'))) {
949 if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
950 $ldap_entry->replace(
951 'FAIstate' => 'install'
952 );
953 my $replace_result = $ldap_entry->update($ldap_handle);
954 if ($replace_result->code() != 0) {
955 &main::daemon_log("$session_id ERROR: Setting the FAIstate to install failed with code '".$replace_result->code()."') and message '".$replace_result->{'errorMessage'}."'!", 1);
956 } else {
957 &main::daemon_log("$session_id DEBUG: Setting the FAIstate to install for '".($ldap_entry->dn())."' succeeded!", 5);
958 }
959 }
960 } else {
961 $ldap_entry->add(
962 'FAIstate' => 'install'
963 );
964 my $add_result = $ldap_entry->update($ldap_handle);
965 if ($add_result->code() != 0) {
966 &main::daemon_log("$session_id ERROR: Setting the FAIstate to install failed with code '".$add_result->code()."') and message '".$add_result->{'errorMessage'}."'!", 1);
967 } else {
968 &main::daemon_log("$session_id DEBUG: Setting the FAIstate to install for '".($ldap_entry->dn())."' succeeded!", 5);
969 }
970 }
973 } elsif ($ldap_mesg->count == 0) {
974 # TODO: Create a new entry
975 # $ldap_entry = Net::LDAP::Entry->new();
976 # $ldap_entry->dn("cn=$mac,$base");
977 &main::daemon_log("$session_id WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
978 $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
979 return undef;
980 } else {
981 &main::daemon_log("$session_id ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
982 }
984 # Add to ObjectGroup
985 my $ogroup_member = $ogroup_entry->get_value('member', asref => 1);
986 if( (!defined($ogroup_member)) ||
987 (!defined($ldap_entry)) ||
988 (!defined($ldap_entry->dn)) ||
989 (!(scalar grep $_ eq $ldap_entry->dn, @{$ogroup_member}))) {
990 $ogroup_entry->add (
991 'member' => $ldap_entry->dn(),
992 );
993 my $ogroup_result = $ogroup_entry->update($ldap_handle);
994 if ($ogroup_result->code() != 0) {
995 &main::daemon_log("$session_id ERROR: Updating the ObjectGroup '$ogroup' failed (code '".$ogroup_result->code()."') with '".$ogroup_result->{'errorMessage'}."'!", 1);
996 } else {
997 &main::daemon_log("$session_id DEBUG: Updating the ObjectGroup '$ogroup' for member '".($ldap_entry->dn())."' succeeded!", 5);
998 }
999 } else {
1000 &main::daemon_log("$session_id DEBUG: System with mac address '$mac' is already a member of ObjectGroup '$ogroup'.", 5);
1001 }
1003 # Finally set gotoMode to active
1004 if(defined($ldap_entry->get_value('gotoMode'))) {
1005 if(!($ldap_entry->get_value('gotoMode') eq 'active')) {
1006 $ldap_entry->replace(
1007 'gotoMode' => 'active'
1008 );
1009 my $activate_result = $ldap_entry->update($ldap_handle);
1010 if ($activate_result->code() != 0) {
1011 &main::daemon_log("$session_id ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
1012 } else {
1013 &main::daemon_log("$session_id DEBUG: Activating system '".$ldap_entry->dn()."' succeeded!", 5);
1014 $activate_client = 1;
1015 }
1016 } else {
1017 $activate_client = 1;
1018 }
1019 } else {
1020 $ldap_entry->add(
1021 'gotoMode' => 'active'
1022 );
1023 my $activate_result = $ldap_entry->update($ldap_handle);
1024 if ($activate_result->code() != 0) {
1025 &main::daemon_log("$session_id ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
1026 } else {
1027 &main::daemon_log("$session_id DEBUG: Activating system '".$ldap_entry->dn()."' succeeded!", 5);
1028 $activate_client = 1;
1029 }
1030 }
1032 if($activate_client == 1) {
1033 &main::daemon_log("$session_id DEBIG: Activating system with mac address '$mac'!", 5);
1035 # Create delivery list
1036 my @out_msg_l;
1038 # Set job to done
1039 $main::job_db->exec_statement("UPDATE jobs SET status = 'done' WHERE id = $jobdb_id");
1041 # If a client gets a 'set_activated_for_installation' msg, always deliver a fresh 'new_ldap_config'
1042 # just for backup and robustness purposes
1043 my $ldap_out_msg = &ClientPackages::new_ldap_config($mac, $session_id);
1044 push(@out_msg_l, $ldap_out_msg);
1046 # create set_activated_for_installation message for delivery
1047 my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
1048 my $out_msg = &create_xml_string($out_hash);
1049 push(@out_msg_l, $out_msg);
1051 # Return delivery list of messages
1052 return @out_msg_l;
1054 } else {
1055 &main::daemon_log("$session_id WARNING: Activating system with mac address '$mac' failed! Re-queuing job.", 4);
1056 $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
1057 }
1058 return undef;
1059 }
1062 #sub get_dak_keyring {
1063 # my ($msg, $msg_hash) = @_;
1064 # my $source = @{$msg_hash->{'source'}}[0];
1065 # my $target = @{$msg_hash->{'target'}}[0];
1066 # my $header= @{$msg_hash->{'header'}}[0];
1067 # my $session_id = @{$msg_hash->{'session_id'}}[0];
1068 #
1069 # # build return message with twisted target and source
1070 # my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1071 # &add_content2xml_hash($out_hash, "session_id", $session_id);
1072 #
1073 # my @keys;
1074 # my %data;
1075 #
1076 # my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1077 #
1078 # my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1079 # my $gpg = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
1080 #
1081 # # Check if the keyrings are in place and readable
1082 # if(
1083 # &run_as($main::dak_user, "test -r $keyring")->{'resultCode'} != 0
1084 # ) {
1085 # &add_content2xml_hash($out_hash, "error", "DAK Keyring is not readable");
1086 # } else {
1087 # my $command = "$gpg --list-keys";
1088 # my $output = &run_as($main::dak_user, $command);
1089 # &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1090 #
1091 # my $i=0;
1092 # foreach (@{$output->{'output'}}) {
1093 # if ($_ =~ m/^pub\s.*$/) {
1094 # ($keys[$i]->{'pub'}->{'length'}, $keys[$i]->{'pub'}->{'uid'}, $keys[$i]->{'pub'}->{'created'}) = ($1, $2, $3)
1095 # if $_ =~ m/^pub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
1096 # $keys[$i]->{'pub'}->{'expires'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expires:\s(\d{4}-\d{2}-\d{2})\]/;
1097 # $keys[$i]->{'pub'}->{'expired'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expired:\s(\d{4}-\d{2}-\d{2})\]/;
1098 # } elsif ($_ =~ m/^sub\s.*$/) {
1099 # ($keys[$i]->{'sub'}->{'length'}, $keys[$i]->{'sub'}->{'uid'}, $keys[$i]->{'sub'}->{'created'}) = ($1, $2, $3)
1100 # if $_ =~ m/^sub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
1101 # $keys[$i]->{'sub'}->{'expires'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expires:\s(\d{4}-\d{2}-\d{2})\]/;
1102 # $keys[$i]->{'sub'}->{'expired'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expired:\s(\d{4}-\d{2}-\d{2})\]/;
1103 # } elsif ($_ =~ m/^uid\s.*$/) {
1104 # push @{$keys[$i]->{'uid'}}, $1 if $_ =~ m/^uid\s*?([^\s].*?)$/;
1105 # } elsif ($_ =~ m/^$/) {
1106 # $i++;
1107 # }
1108 # }
1109 # }
1110 #
1111 # my $i=0;
1112 # foreach my $key (@keys) {
1113 # # &main::daemon_log(Dumper($key));
1114 # &add_content2xml_hash($out_hash, "answer".$i++, $key);
1115 # }
1116 # my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1117 # if (defined $forward_to_gosa) {
1118 # &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1119 # }
1120 # return &create_xml_string($out_hash);
1121 #}
1122 #
1123 #
1124 #sub import_dak_key {
1125 # my ($msg, $msg_hash) = @_;
1126 # my $source = @{$msg_hash->{'source'}}[0];
1127 # my $target = @{$msg_hash->{'target'}}[0];
1128 # my $header= @{$msg_hash->{'header'}}[0];
1129 # my $session_id = @{$msg_hash->{'session_id'}}[0];
1130 # my $key = &decode_base64(@{$msg_hash->{'key'}}[0]);
1131 #
1132 # # build return message with twisted target and source
1133 # my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1134 # &add_content2xml_hash($out_hash, "session_id", $session_id);
1135 #
1136 # my %data;
1137 #
1138 # my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1139 #
1140 # my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1141 # my $gpg = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
1142 #
1143 # # Check if the keyrings are in place and writable
1144 # if(
1145 # &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
1146 # ) {
1147 # &add_content2xml_hash($out_hash, "error", "DAK Keyring is not writable");
1148 # } else {
1149 # my $keyfile;
1150 # open($keyfile, ">/tmp/gosa_si_tmp_dak_key");
1151 # print $keyfile $key;
1152 # close($keyfile);
1153 # my $command = "$gpg --import /tmp/gosa_si_tmp_dak_key";
1154 # my $output = &run_as($main::dak_user, $command);
1155 # &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1156 # unlink("/tmp/gosa_si_tmp_dak_key");
1157 #
1158 # if($output->{'resultCode'} != 0) {
1159 # &add_content2xml_hash($out_hash, "error", "Import of DAK key failed! Output was '".$output->{'output'}."'");
1160 # } else {
1161 # &add_content2xml_hash($out_hash, "answer", "Import of DAK key successfull! Output was '".$output->{'output'}."'");
1162 # }
1163 # }
1164 #
1165 # my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1166 # if (defined $forward_to_gosa) {
1167 # &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1168 # }
1169 # return &create_xml_string($out_hash);
1170 #}
1171 #
1172 #
1173 #sub remove_dak_key {
1174 # my ($msg, $msg_hash) = @_;
1175 # my $source = @{$msg_hash->{'source'}}[0];
1176 # my $target = @{$msg_hash->{'target'}}[0];
1177 # my $header= @{$msg_hash->{'header'}}[0];
1178 # my $session_id = @{$msg_hash->{'session_id'}}[0];
1179 # my $key = @{$msg_hash->{'keyid'}}[0];
1180 # # build return message with twisted target and source
1181 # my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1182 # &add_content2xml_hash($out_hash, "session_id", $session_id);
1183 #
1184 # my %data;
1185 #
1186 # my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1187 #
1188 # my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1189 # my $gpg = "$gpg_cmd --no-default-keyring --no-random-seed --homedir ".$main::dak_signing_keys_directory." --keyring $keyring";
1190 #
1191 # # Check if the keyrings are in place and writable
1192 # if(
1193 # &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
1194 # ) {
1195 # &add_content2xml_hash($out_hash, "error", "DAK keyring is not writable");
1196 # } else {
1197 # # Check if the key is present in the keyring
1198 # if(&run_as($main::dak_user, "$gpg --list-keys $key")->{'resultCode'} == 0) {
1199 # my $command = "$gpg --batch --yes --delete-key $key";
1200 # my $output = &run_as($main::dak_user, $command);
1201 # &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1202 # } else {
1203 # &add_content2xml_hash($out_hash, "error", "DAK key with id '$key' was not found in keyring");
1204 # }
1205 # }
1206 #
1207 # my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1208 # if (defined $forward_to_gosa) {
1209 # &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1210 # }
1211 # return &create_xml_string($out_hash);
1212 #}
1215 #sub get_dak_queue {
1216 # my ($msg, $msg_hash, $session_id) = @_;
1217 # my %data;
1218 # my $source = @{$msg_hash->{'source'}}[0];
1219 # my $target = @{$msg_hash->{'target'}}[0];
1220 # my $header= @{$msg_hash->{'header'}}[0];
1221 #
1222 # my %data;
1223 #
1224 # foreach my $dir ("unchecked", "new", "accepted") {
1225 # foreach my $file(<"$main::dak_queue_directory/$dir/*.changes">) {
1226 # }
1227 # }
1228 #
1229 # my $out_msg = &build_msg("get_dak_queue", $target, $source, \%data);
1230 # my @out_msg_l = ($out_msg);
1231 # return @out_msg_l;
1232 #}
1234 ## @method get_hosts_with_module
1235 # Reports all GOsa-si-server providing the given module.
1236 # @param msg - STRING - xml message with tag get_hosts_with_module
1237 # @param msg_hash - HASHREF - message information parsed into a hash
1238 # @param session_id - INTEGER - POE session id of the processing of this message
1239 # @return out_msg - STRING - feedback to GOsa in success and error case
1240 sub get_hosts_with_module {
1241 my ($msg, $msg_hash, $session_id) = @_;
1242 my $source = @{$msg_hash->{'source'}}[0];
1243 my $target = @{$msg_hash->{'target'}}[0];
1244 my $header= @{$msg_hash->{'header'}}[0];
1245 my $module_name = @{$msg_hash->{'module_name'}}[0];
1246 my $out_hash = &create_xml_hash($header, $target, $source);
1248 # Sanity check of module_name
1249 if ((not exists $msg_hash->{'module_name'}) || (@{$msg_hash->{'module_name'}} != 1)) {
1250 &add_content2xml_hash($out_hash, "error_string", "no module_name specified or module_name tag invalid");
1251 &add_content2xml_hash($out_hash, "error", "module_name");
1252 &main::daemon_log("$session_id ERROR: no module_name specified or module_name tag invalid: $msg", 1);
1253 return (&create_xml_string($out_hash));
1254 }
1256 my $out_msg = &create_xml_string($out_hash);
1258 # Check localhost for module_name
1259 if (exists @{$main::known_modules->{'GosaPackages'}}[2]->{$module_name}) {
1260 my ($local_ip, $local_port) = split(/:/, $target);
1261 my $network_interface= &get_interface_for_ip($local_ip);
1262 my $local_mac = &get_mac_for_interface($network_interface);
1263 $out_msg =~ s/<\/xml>/<result>host0<\/result> <\/xml>/;
1264 my $host_infos = "<ip>$local_ip</ip>";
1265 $host_infos .= " <mac>$local_mac</mac>";
1266 $out_msg =~ s/<\/xml>/\n<answer0> $host_infos <\/answer0> \n <\/xml>/;
1267 }
1269 # Search for opsi hosts in server_db
1270 my $sql = "SELECT * FROM $main::known_server_tn WHERE loaded_modules LIKE '%$module_name%'";
1271 my $res = $main::known_server_db->select_dbentry($sql);
1272 while (my ($hit_id, $hit_hash) = each %$res) {
1273 $out_msg =~ s/<\/xml>/<result>host$hit_id<\/result> <\/xml>/;
1274 my $host_infos = "<ip>".$hit_hash->{'hostname'}."</ip>";
1275 $host_infos .= " <mac>".$hit_hash->{'macaddress'}."</mac>";
1276 $out_msg =~ s/<\/xml>/\n<answer$hit_id> $host_infos <\/answer$hit_id> \n <\/xml>/;
1277 }
1279 return $out_msg;
1280 }
1282 # vim:ts=4:shiftwidth:expandtab
1283 1;