Code

Fix LHM #8687
[gosa.git] / trunk / gosa-si / server / events / gosaTriggered.pm
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];
94     
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;
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 );
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 );
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;
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;
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;
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 );
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 );
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 );
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;
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;
435    
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;
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;
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;
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;
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     }
546                                              
547     my @out_msg_l;
548     return @out_msg_l;
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     }
564                                              
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;
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;
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;
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;
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;
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);
648     &main::change_goto_state('active', \@{$msg_hash->{macaddress}}, $session_id);
649     my @out_msg_l = &set_activated_for_installation($msg, $msg_hash, $session_id);
651     my %data = ( 'macaddress'  => \@{$msg_hash->{macaddress}} );
652     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
653     # invoke trigger wake for this gosa-si-server
654     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
656     push(@out_msg_l, $wake_msg);
657     push(@out_msg_l, $msg);
658     return @out_msg_l;
662 sub trigger_action_update {
663     my ($msg, $msg_hash, $session_id) = @_;
664     $msg =~ s/<header>gosa_trigger_action_update<\/header>/<header>trigger_action_update<\/header>/;
666     &main::change_fai_state('update', \@{$msg_hash->{macaddress}}, $session_id);
668     my %data = ( 'macaddress'  => \@{$msg_hash->{macaddress}} );
669     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
670     # invoke trigger wake for this gosa-si-server
671     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
673     my @out_msg_l = ($wake_msg, $msg);  
674     return @out_msg_l;
678 sub trigger_action_instant_update {
679     my ($msg, $msg_hash, $session_id) = @_;
680     $msg =~ s/<header>gosa_trigger_action_instant_update<\/header>/<header>trigger_action_instant_update<\/header>/;
682     &main::change_fai_state('update', \@{$msg_hash->{macaddress}}, $session_id);
684     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
685     if( defined $jobdb_id) {
686         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
687         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
688         my $res = $main::job_db->exec_statement($sql_statement);
689     }
692     my %data = ( 'macaddress'  => \@{$msg_hash->{macaddress}} );
693     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
694     # invoke trigger wake for this gosa-si-server
695     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
697     my @out_msg_l = ($wake_msg, $msg);  
698     return @out_msg_l;
702 sub trigger_action_sysinfo {
703     my ($msg, $msg_hash, $session_id) = @_;
704     $msg =~ s/<header>gosa_trigger_action_sysinfo<\/header>/<header>trigger_action_sysinfo<\/header>/;
706     &main::change_fai_state('sysinfo', \@{$msg_hash->{macaddress}}, $session_id);
707     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
708     if( defined $jobdb_id) {
709         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
710         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
711         my $res = $main::job_db->exec_statement($sql_statement);
712     }
714     my @out_msg_l = ($msg);  
715     return @out_msg_l;
719 sub new_key_for_client {
720     my ($msg, $msg_hash, $session_id) = @_;
722     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
723     if( defined $jobdb_id) {
724         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
725         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
726         my $res = $main::job_db->exec_statement($sql_statement);
727     }
728     
729     $msg =~ s/<header>gosa_new_key_for_client<\/header>/<header>new_key<\/header>/;
730     my @out_msg_l = ($msg);  
731     return @out_msg_l;
735 sub trigger_action_rescan {
736     my ($msg, $msg_hash, $session_id) = @_;
738     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
739     if( defined $jobdb_id) {
740         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
741         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
742         my $res = $main::job_db->exec_statement($sql_statement);
743     }
746     $msg =~ s/<header>gosa_trigger_action_rescan<\/header>/<header>detect_hardware<header>/;
747     my @out_msg_l = ($msg);  
748     return @out_msg_l;
752 sub trigger_action_wake {
753     my ($msg, $msg_hash, $session_id) = @_;
754     
755     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
756     if( defined $jobdb_id) {
757         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
758         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
759         my $res = $main::job_db->exec_statement($sql_statement);
760     }
762     # build out message
763     my $out_hash = &create_xml_hash("trigger_wake", "GOSA", "KNOWN_SERVER");
764     foreach (@{$msg_hash->{'macaddress'}}) {
765         &add_content2xml_hash($out_hash, 'macaddress', $_);
766     }
767     if (defined $jobdb_id){
768         &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
769     }
770     my $out_msg = &create_xml_string($out_hash);
771     
772     # invoke trigger wake for this gosa-si-server
773     &main::server_server_com::trigger_wake($out_msg, $out_hash, $session_id);
775     # send trigger wake to all other gosa-si-server
776     my @out_msg_l = ($out_msg);  
777     return @out_msg_l;
781 sub get_available_kernel {
782   my ($msg, $msg_hash, $session_id) = @_;
784   my $source = @{$msg_hash->{'source'}}[0];
785   my $target = @{$msg_hash->{'target'}}[0];
786   my $fai_release= @{$msg_hash->{'fai_release'}}[0];
788   my @kernel;
789   # Get Kernel packages for release
790   my $sql_statement = "SELECT * FROM $main::packages_list_tn WHERE distribution='$fai_release' AND package LIKE 'linux\-image\-%'";
791   my $res_hash = $main::packages_list_db->select_dbentry($sql_statement);
792   my %data;
793   my $i=1;
795   foreach my $package (keys %{$res_hash}) {
796     $data{"answer".$i++}= $data{"answer".$i++}= ${$res_hash}{$package}->{'package'};
797   }
798   $data{"answer".$i++}= "default";
800   my $out_msg = &build_msg("get_available_kernel", $target, $source, \%data);
801   my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
802   if (defined $forward_to_gosa) {
803     $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
804   }
806   return ( $out_msg );
809 sub trigger_activate_new {
810         my ($msg, $msg_hash, $session_id) = @_;
812         my $source = @{$msg_hash->{'source'}}[0];
813         my $target = @{$msg_hash->{'target'}}[0];
814         my $header= @{$msg_hash->{'header'}}[0];
815         my $mac= (defined($msg_hash->{'mac'}))?@{$msg_hash->{'mac'}}[0]:undef;
816         my $ogroup= (defined($msg_hash->{'ogroup'}))?@{$msg_hash->{'ogroup'}}[0]:undef;
817         my $timestamp= (defined($msg_hash->{'timestamp'}))?@{$msg_hash->{'timestamp'}}[0]:undef;
818         my $base= (defined($msg_hash->{'base'}))?@{$msg_hash->{'base'}}[0]:undef;
819         my $hostname= (defined($msg_hash->{'fqdn'}))?@{$msg_hash->{'fqdn'}}[0]:undef;
820         my $ip_address= (defined($msg_hash->{'ip'}))?@{$msg_hash->{'ip'}}[0]:undef;
821         my $dhcp_statement= (defined($msg_hash->{'dhcp'}))?@{$msg_hash->{'dhcp'}}[0]:undef;
822         my $jobdb_id= (defined($msg_hash->{'jobdb_id'}))?@{$msg_hash->{'jobdb_id'}}[0]:undef;
824     # Sanity check for base
825     if (ref($base) eq "HASH") {
826         # Incoming msg has a xml tag 'base' but no content
827         $base = undef;
828     }
830     # In case that the client is sleeping, wake it up
831     my %data = ( 'macaddress'  => $mac );
832     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
833     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
834     my $sql_statement= "SELECT * FROM $main::known_server_tn";
835     my $query_res = $main::known_server_db->select_dbentry( $sql_statement ); 
836     while( my ($hit_num, $hit) = each %{ $query_res } ) {    
837         my $host_name = $hit->{hostname};
838         my $host_key = $hit->{hostkey};
839         $wake_msg =~ s/<target>\S+<\/target>/<target>$host_name<\/target>/g;
840         my $error = &main::send_msg_to_target($wake_msg, $host_name, $host_key, $header, $session_id);
841     }
843         my $ldap_handle = &main::get_ldap_handle();
844         my $ldap_entry;
845         my $ogroup_entry;
846         my $changed_attributes_counter = 0;
848     my $activate_client = 0;
849         
850     if(defined($ogroup)) {
851       my $ldap_mesg= $ldap_handle->search(
852         base => $main::ldap_base,
853         scope => 'sub',
854         filter => "(&(objectClass=gosaGroupOfnames)(cn=$ogroup))",
855       );
856       if($ldap_mesg->count == 1) {
857         $ogroup_entry= $ldap_mesg->pop_entry();
858         &main::daemon_log("$session_id DEBUG: A GosaGroupOfNames with cn '$ogroup' was found in base '".$main::ldap_base."'!", 5);
859       } elsif ($ldap_mesg->count == 0) {
860         &main::daemon_log("$session_id ERROR: A GosaGroupOfNames with cn '$ogroup' was not found in base '".$main::ldap_base."'!", 1);
861         $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
862         return undef;
863       } else {
864         &main::daemon_log("$session_id ERROR: More than one ObjectGroups with cn '$ogroup' was found in base '".$main::ldap_base."'!", 1);
865         $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
866         return undef;
867       }
869       # build the base, use optional base parameter or take it from ogroup
870       if(!(defined($base) && (length($base) > 0))) {
871         # Subtract the ObjectGroup cn
872         $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
873         &main::daemon_log("$session_id DEBUG: New base for system with mac address '$mac' is '$base'", 5);
874       }
875     }
877     # prepend ou=systems (configurable through config)
878     $base = $main::new_systems_ou.",".$base;
880     # Search for an existing entry (should be in ou=incoming)
881     my $ldap_mesg= $ldap_handle->search(
882       base => $main::ldap_base,
883       scope => 'sub',
884       filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
885     );
887     # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
888     if($ldap_mesg->count == 1) {
889       &main::daemon_log("$session_id DEBUG: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 5);
890       # Get the entry from LDAP
891       $ldap_entry= $ldap_mesg->pop_entry();
893       if(!($ldap_entry->dn() eq "cn=".$ldap_entry->get_value('cn').",$base")) {
894         # Move the entry to the new ou
895         $ldap_entry->changetype('moddn');
896         $ldap_entry->add(
897           newrdn => "cn=".$ldap_entry->get_value('cn'),
898           deleteoldrdn => 1,
899           newsuperior => $base,
900         );
901         # To prevent replication problems just re-queue the job with 10 seconds in the future
902         my $moddn_result = $ldap_entry->update($ldap_handle);
903         if ($moddn_result->code() != 0) {
904             my $error_string = "Moving the system with mac address '$mac' to new base '$base' failed (code '".$moddn_result->code()."') with '".$moddn_result->{'errorMessage'}."'!";
905             &main::daemon_log("$session_id ERROR: $error_string", 1);
906             my $sql = "UPDATE $main::job_queue_tn SET status='error', result='$error_string' WHERE id=$jobdb_id";
907             return undef;
908         } else {
909           &main::daemon_log("$session_id INFO: System with mac address '$mac' was moved to base '".$main::ldap_base."'! Re-queuing job.", 4);
910           $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 10))."' WHERE id = $jobdb_id");
911           return undef;
912         }
913       }
915     } elsif ($ldap_mesg->count == 0) {
916       &main::daemon_log("$session_id WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
917       my $sql_statement = "UPDATE ".$main::job_queue_tn.
918               " SET status='waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' ".
919               " WHERE id = $jobdb_id";
920       $main::job_db->exec_statement($sql_statement);
921       return undef;
922     }
924     $ldap_mesg= $ldap_handle->search(
925       base => $main::ldap_base,
926       scope => 'sub',
927       filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
928     );
930     # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
931     if($ldap_mesg->count == 1) {
932       $ldap_entry= $ldap_mesg->pop_entry();
933       # Check for needed objectClasses
934       my $oclasses = $ldap_entry->get_value('objectClass', asref => 1);
935       foreach my $oclass ("FAIobject", "GOhard", "gotoWorkstation") {
936         if(!(scalar grep $_ eq $oclass, map {$_ => 1} @$oclasses)) {
937           &main::daemon_log("$session_id INFO: Adding objectClass '$oclass' to system entry with mac adress '$mac'", 1);
938           $ldap_entry->add(
939             objectClass => $oclass,
940           );
941           my $oclass_result = $ldap_entry->update($ldap_handle);
942           if ($oclass_result->code() != 0) {
943             &main::daemon_log("$session_id ERROR: Adding the ObjectClass '$oclass' failed (code '".$oclass_result->code()."') with '".$oclass_result->{'errorMessage'}."'!", 1);
944           } else {
945             &main::daemon_log("$session_id DEBUG: Adding the ObjectClass '$oclass' to '".($ldap_entry->dn())."' succeeded!", 5);
946           }
947         }
948       }
950       # Set FAIstate
951       if(defined($ldap_entry->get_value('FAIstate'))) {
952         if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
953           $ldap_entry->replace(
954             'FAIstate' => 'install'
955           );
956           my $replace_result = $ldap_entry->update($ldap_handle);
957           if ($replace_result->code() != 0) {
958             &main::daemon_log("$session_id ERROR: Setting the FAIstate to install failed with code '".$replace_result->code()."') and message '".$replace_result->{'errorMessage'}."'!", 1);
959           } else {
960             &main::daemon_log("$session_id DEBUG: Setting the FAIstate to install for '".($ldap_entry->dn())."' succeeded!", 5);
961           }
962         }
963       } else {
964         $ldap_entry->add(
965           'FAIstate' => 'install'
966         );
967         my $add_result = $ldap_entry->update($ldap_handle);
968         if ($add_result->code() != 0) {
969           &main::daemon_log("$session_id ERROR: Setting the FAIstate to install failed with code '".$add_result->code()."') and message '".$add_result->{'errorMessage'}."'!", 1);
970         } else {
971           &main::daemon_log("$session_id DEBUG: Setting the FAIstate to install for '".($ldap_entry->dn())."' succeeded!", 5);
972         }
973       }
976     } elsif ($ldap_mesg->count == 0) {
977       # TODO: Create a new entry
978       # $ldap_entry = Net::LDAP::Entry->new();
979       # $ldap_entry->dn("cn=$mac,$base");
980       &main::daemon_log("$session_id WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
981       $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
982       return undef;
983     } else {
984       &main::daemon_log("$session_id ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
985     }
987     # Add to ObjectGroup
988     my $ogroup_member = $ogroup_entry->get_value('member', asref => 1);
989     if( (!defined($ogroup_member)) ||
990         (!defined($ldap_entry)) ||
991         (!defined($ldap_entry->dn)) ||
992         (!(scalar grep $_ eq $ldap_entry->dn, @{$ogroup_member}))) {
993       $ogroup_entry->add (
994         'member' => $ldap_entry->dn(),
995       );
996       my $ogroup_result = $ogroup_entry->update($ldap_handle);
997       if ($ogroup_result->code() != 0) {
998         &main::daemon_log("$session_id ERROR: Updating the ObjectGroup '$ogroup' failed (code '".$ogroup_result->code()."') with '".$ogroup_result->{'errorMessage'}."'!", 1);
999       } else {
1000         &main::daemon_log("$session_id DEBUG: Updating the ObjectGroup '$ogroup' for member '".($ldap_entry->dn())."' succeeded!", 5);
1001       }
1002     } else {
1003       &main::daemon_log("$session_id DEBUG: System with mac address '$mac' is already a member of ObjectGroup '$ogroup'.", 5);
1004     }
1006     # Finally set gotoMode to active
1007     if(defined($ldap_entry->get_value('gotoMode'))) {
1008       if(!($ldap_entry->get_value('gotoMode') eq 'active')) {
1009         $ldap_entry->replace(
1010           'gotoMode' => 'active'
1011         );
1012         my $activate_result = $ldap_entry->update($ldap_handle);
1013         if ($activate_result->code() != 0) {
1014           &main::daemon_log("$session_id ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
1015         } else {
1016           &main::daemon_log("$session_id DEBUG: Activating system '".$ldap_entry->dn()."' succeeded!", 5);
1017           $activate_client = 1;
1018         }
1019       } else {
1020           $activate_client = 1;
1021       }
1022     } else {
1023       $ldap_entry->add(
1024         'gotoMode' => 'active'
1025       );
1026       my $activate_result = $ldap_entry->update($ldap_handle);
1027       if ($activate_result->code() != 0) {
1028         &main::daemon_log("$session_id ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
1029       } else {
1030         &main::daemon_log("$session_id DEBUG: Activating system '".$ldap_entry->dn()."' succeeded!", 5);
1031         $activate_client = 1;
1032       }
1033     }
1035     if($activate_client == 1) {
1036         &main::daemon_log("$session_id DEBIG: Activating system with mac address '$mac'!", 5);
1038         # Create delivery list
1039         my @out_msg_l;
1041         # Set job to done
1042         $main::job_db->exec_statement("UPDATE jobs SET status = 'done' WHERE id = $jobdb_id");
1043     
1044         # If a client gets a 'set_activated_for_installation' msg, always deliver a fresh 'new_ldap_config'
1045         # just for backup and robustness purposes
1046         my $ldap_out_msg = &ClientPackages::new_ldap_config($mac, $session_id);
1047         push(@out_msg_l, $ldap_out_msg);
1049         # create set_activated_for_installation message for delivery
1050         my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
1051         my $out_msg = &create_xml_string($out_hash);
1052         push(@out_msg_l, $out_msg);
1054         # Return delivery list of messages
1055         return @out_msg_l;
1057     } else {
1058       &main::daemon_log("$session_id WARNING: Activating system with mac address '$mac' failed! Re-queuing job.", 4);
1059       $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting',  timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
1060     }
1061     return undef;
1065 #sub get_dak_keyring {
1066 #    my ($msg, $msg_hash) = @_;
1067 #    my $source = @{$msg_hash->{'source'}}[0];
1068 #    my $target = @{$msg_hash->{'target'}}[0];
1069 #    my $header= @{$msg_hash->{'header'}}[0];
1070 #    my $session_id = @{$msg_hash->{'session_id'}}[0];
1072 #    # build return message with twisted target and source
1073 #    my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1074 #    &add_content2xml_hash($out_hash, "session_id", $session_id);
1076 #    my @keys;
1077 #    my %data;
1079 #    my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1081 #    my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1082 #    my $gpg     = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
1084 #    # Check if the keyrings are in place and readable
1085 #    if(
1086 #        &run_as($main::dak_user, "test -r $keyring")->{'resultCode'} != 0
1087 #    ) {
1088 #        &add_content2xml_hash($out_hash, "error", "DAK Keyring is not readable");
1089 #    } else {
1090 #        my $command = "$gpg --list-keys";
1091 #        my $output = &run_as($main::dak_user, $command);
1092 #        &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1094 #        my $i=0;
1095 #        foreach (@{$output->{'output'}}) {
1096 #            if ($_ =~ m/^pub\s.*$/) {
1097 #                ($keys[$i]->{'pub'}->{'length'}, $keys[$i]->{'pub'}->{'uid'}, $keys[$i]->{'pub'}->{'created'}) = ($1, $2, $3)
1098 #                if $_ =~ m/^pub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
1099 #                $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})\]/;
1100 #                $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})\]/;
1101 #            } elsif ($_ =~ m/^sub\s.*$/) {
1102 #                ($keys[$i]->{'sub'}->{'length'}, $keys[$i]->{'sub'}->{'uid'}, $keys[$i]->{'sub'}->{'created'}) = ($1, $2, $3)
1103 #                if $_ =~ m/^sub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
1104 #                $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})\]/;
1105 #                $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})\]/;
1106 #            } elsif ($_ =~ m/^uid\s.*$/) {
1107 #                push @{$keys[$i]->{'uid'}}, $1 if $_ =~ m/^uid\s*?([^\s].*?)$/;
1108 #            } elsif ($_ =~ m/^$/) {
1109 #                $i++;
1110 #            }
1111 #        }
1112 #    }
1114 #    my $i=0;
1115 #    foreach my $key (@keys) {
1116 #        #    &main::daemon_log(Dumper($key));
1117 #        &add_content2xml_hash($out_hash, "answer".$i++, $key);
1118 #    }
1119 #    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1120 #    if (defined $forward_to_gosa) {
1121 #        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1122 #    }
1123 #    return &create_xml_string($out_hash);
1124 #}
1127 #sub import_dak_key {
1128 #    my ($msg, $msg_hash) = @_;
1129 #    my $source = @{$msg_hash->{'source'}}[0];
1130 #    my $target = @{$msg_hash->{'target'}}[0];
1131 #    my $header= @{$msg_hash->{'header'}}[0];
1132 #    my $session_id = @{$msg_hash->{'session_id'}}[0];
1133 #    my $key = &decode_base64(@{$msg_hash->{'key'}}[0]);
1135 #    # build return message with twisted target and source
1136 #    my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1137 #    &add_content2xml_hash($out_hash, "session_id", $session_id);
1139 #    my %data;
1141 #    my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1143 #    my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1144 #    my $gpg     = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
1146 #    # Check if the keyrings are in place and writable
1147 #    if(
1148 #        &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
1149 #    ) {
1150 #        &add_content2xml_hash($out_hash, "error", "DAK Keyring is not writable");
1151 #    } else {
1152 #        my $keyfile;
1153 #        open($keyfile, ">/tmp/gosa_si_tmp_dak_key");
1154 #        print $keyfile $key;
1155 #        close($keyfile);
1156 #        my $command = "$gpg --import /tmp/gosa_si_tmp_dak_key";
1157 #        my $output = &run_as($main::dak_user, $command);
1158 #        &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1159 #        unlink("/tmp/gosa_si_tmp_dak_key");
1161 #        if($output->{'resultCode'} != 0) {
1162 #            &add_content2xml_hash($out_hash, "error", "Import of DAK key failed! Output was '".$output->{'output'}."'");
1163 #        } else {
1164 #            &add_content2xml_hash($out_hash, "answer", "Import of DAK key successfull! Output was '".$output->{'output'}."'");
1165 #        }
1166 #    }
1168 #    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1169 #    if (defined $forward_to_gosa) {
1170 #        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1171 #    }
1172 #    return &create_xml_string($out_hash);
1173 #}
1176 #sub remove_dak_key {
1177 #    my ($msg, $msg_hash) = @_;
1178 #    my $source = @{$msg_hash->{'source'}}[0];
1179 #    my $target = @{$msg_hash->{'target'}}[0];
1180 #    my $header= @{$msg_hash->{'header'}}[0];
1181 #    my $session_id = @{$msg_hash->{'session_id'}}[0];
1182 #    my $key = @{$msg_hash->{'keyid'}}[0];
1183 #    # build return message with twisted target and source
1184 #    my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1185 #    &add_content2xml_hash($out_hash, "session_id", $session_id);
1187 #    my %data;
1189 #    my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1191 #    my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1192 #    my $gpg     = "$gpg_cmd --no-default-keyring --no-random-seed --homedir ".$main::dak_signing_keys_directory." --keyring $keyring";
1194 #    # Check if the keyrings are in place and writable
1195 #    if(
1196 #        &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
1197 #    ) {
1198 #        &add_content2xml_hash($out_hash, "error", "DAK keyring is not writable");
1199 #    } else {
1200 #        # Check if the key is present in the keyring
1201 #        if(&run_as($main::dak_user, "$gpg --list-keys $key")->{'resultCode'} == 0) {
1202 #            my $command = "$gpg --batch --yes --delete-key $key";
1203 #            my $output = &run_as($main::dak_user, $command);
1204 #            &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1205 #        } else {
1206 #            &add_content2xml_hash($out_hash, "error", "DAK key with id '$key' was not found in keyring");
1207 #        }
1208 #    }
1210 #    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1211 #    if (defined $forward_to_gosa) {
1212 #        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1213 #    }
1214 #    return &create_xml_string($out_hash);
1215 #}
1218 #sub get_dak_queue {
1219 #    my ($msg, $msg_hash, $session_id) = @_;
1220 #    my %data;
1221 #    my $source = @{$msg_hash->{'source'}}[0];
1222 #    my $target = @{$msg_hash->{'target'}}[0];
1223 #    my $header= @{$msg_hash->{'header'}}[0];
1225 #    my %data;
1227 #    foreach my $dir ("unchecked", "new", "accepted") {
1228 #        foreach my $file(<"$main::dak_queue_directory/$dir/*.changes">) {
1229 #        }
1230 #    }
1232 #    my $out_msg = &build_msg("get_dak_queue", $target, $source, \%data);
1233 #    my @out_msg_l = ($out_msg);
1234 #    return @out_msg_l;
1235 #}
1237 ## @method get_hosts_with_module
1238 # Reports all GOsa-si-server providing the given module. 
1239 # @param msg - STRING - xml message with tag get_hosts_with_module
1240 # @param msg_hash - HASHREF - message information parsed into a hash
1241 # @param session_id - INTEGER - POE session id of the processing of this message
1242 # @return out_msg - STRING - feedback to GOsa in success and error case
1243 sub get_hosts_with_module {
1244     my ($msg, $msg_hash, $session_id) = @_;
1245     my $source = @{$msg_hash->{'source'}}[0];
1246     my $target = @{$msg_hash->{'target'}}[0];
1247     my $header= @{$msg_hash->{'header'}}[0];
1248     my $module_name = @{$msg_hash->{'module_name'}}[0];
1249     my $out_hash = &create_xml_hash($header, $target, $source);
1251     # Sanity check of module_name
1252     if ((not exists $msg_hash->{'module_name'}) || (@{$msg_hash->{'module_name'}} != 1))  {
1253         &add_content2xml_hash($out_hash, "error_string", "no module_name specified or module_name tag invalid");
1254         &add_content2xml_hash($out_hash, "error", "module_name");
1255         &main::daemon_log("$session_id ERROR: no module_name specified or module_name tag invalid: $msg", 1); 
1256         return (&create_xml_string($out_hash));
1257     }
1259     my $out_msg = &create_xml_string($out_hash);
1261     # Check localhost for module_name
1262     if (exists @{$main::known_modules->{'GosaPackages'}}[2]->{$module_name}) {
1263         my ($local_ip, $local_port) = split(/:/, $target);
1264         my $network_interface= &get_interface_for_ip($local_ip);
1265         my $local_mac = &get_mac_for_interface($network_interface);
1266         $out_msg =~ s/<\/xml>/<result>host0<\/result> <\/xml>/;
1267         my $host_infos = "<ip>$local_ip</ip>";
1268         $host_infos .= " <mac>$local_mac</mac>"; 
1269         $out_msg =~  s/<\/xml>/\n<answer0> $host_infos <\/answer0> \n <\/xml>/;
1270     }
1272     # Search for opsi hosts in server_db
1273     my $sql = "SELECT * FROM $main::known_server_tn WHERE loaded_modules LIKE '%$module_name%'"; 
1274     my $res = $main::known_server_db->select_dbentry($sql);
1275     while (my ($hit_id, $hit_hash) = each %$res) {
1276         $out_msg =~ s/<\/xml>/<result>host$hit_id<\/result> <\/xml>/;
1277         my $host_infos = "<ip>".$hit_hash->{'hostname'}."</ip>";
1278         $host_infos .= " <mac>".$hit_hash->{'macaddress'}."</mac>"; 
1279         $out_msg =~  s/<\/xml>/\n<answer$hit_id> $host_infos <\/answer$hit_id> \n <\/xml>/;
1280     }
1282     return $out_msg;
1285 # vim:ts=4:shiftwidth:expandtab
1286 1;