Code

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