Code

42f2189fcc24e71f71a13cd0cfcbe02c6e82c5da
[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     $main::fai_server_db->create_table("new_fai_server", \@main::fai_server_col_names);
166     &main::create_fai_server_db("new_fai_server",undef,"dont", $session_id);
167     $main::fai_server_db->move_table("new_fai_server", $main::fai_server_tn);
168     
169     my @out_msg_l = ( $out_msg );
170     return @out_msg_l;
174 sub recreate_fai_release_db {
175     my ($msg, $msg_hash, $session_id) = @_ ;
176     my $out_msg;
178     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
179     if( defined $jobdb_id) {
180         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
181         &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
182         my $res = $main::job_db->exec_statement($sql_statement);
183     }
185     $main::fai_release_db->create_table("new_fai_release", \@main::fai_release_col_names);
186     &main::create_fai_release_db("new_fai_release", $session_id);
187     $main::fai_release_db->move_table("new_fai_release", $main::fai_release_tn);
189     my @out_msg_l = ( $out_msg );
190     return @out_msg_l;
194 sub recreate_packages_list_db {
195         my ($msg, $msg_hash, $session_id) = @_ ;
196         my $out_msg;
198         my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
199         if( defined $jobdb_id) {
200                 my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
201                 &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
202                 my $res = $main::job_db->exec_statement($sql_statement);
203         }
205         &main::create_packages_list_db(undef, undef, $session_id);
207         my @out_msg_l = ( $out_msg );
208         return @out_msg_l;
212 sub get_login_usr_for_client {
213     my ($msg, $msg_hash, $session_id) = @_ ;
214     my $header = @{$msg_hash->{'header'}}[0];
215     my $source = @{$msg_hash->{'source'}}[0];
216     my $target = @{$msg_hash->{'target'}}[0];
217     my $client = @{$msg_hash->{'client'}}[0];
219     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
220     if( defined $jobdb_id) {
221         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
222         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
223         my $res = $main::job_db->exec_statement($sql_statement);
224     }
226     $header =~ s/^gosa_//;
228     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$client' OR macaddress LIKE '$client'";
229     my $res = $main::known_clients_db->select_dbentry($sql_statement);
231     my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
232     $out_msg .= &db_res2xml($res);
233     $out_msg .= "</xml>";
235     my @out_msg_l = ( $out_msg );
236     return @out_msg_l;
240 sub get_client_for_login_usr {
241     my ($msg, $msg_hash, $session_id) = @_ ;
242     my $header = @{$msg_hash->{'header'}}[0];
243     my $source = @{$msg_hash->{'source'}}[0];
244     my $target = @{$msg_hash->{'target'}}[0];
246     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
247     if( defined $jobdb_id) {
248         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
249         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
250         my $res = $main::job_db->exec_statement($sql_statement);
251     }
253     my $usr = @{$msg_hash->{'usr'}}[0];
254     $header =~ s/^gosa_//;
256     my $sql_statement = "SELECT * FROM known_clients WHERE login LIKE '%$usr%'";
257     my $res = $main::known_clients_db->select_dbentry($sql_statement);
259     my $out_msg = "<xml><header>$header</header><source>$target</source><target>$source</target>";
260     $out_msg .= &db_res2xml($res);
261     $out_msg .= "</xml>";
262     my @out_msg_l = ( $out_msg );
263     return @out_msg_l;
268 sub ping {
269     my ($msg, $msg_hash, $session_id) = @_ ;
270     my $header = @{$msg_hash->{header}}[0];
271     my $target = @{$msg_hash->{target}}[0];
272     my $source = @{$msg_hash->{source}}[0];
273     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
274     my $error = 0;
275     my $answer_msg;
276     my ($sql, $res);
278     if( defined $jobdb_id) {
279         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
280         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
281         my $res = $main::job_db->exec_statement($sql_statement);
282     }
284     # send message
285     $sql = "SELECT * FROM $main::known_clients_tn WHERE ((hostname='$target') || (macaddress LIKE '$target'))"; 
286     $res = $main::known_clients_db->exec_statement($sql);
288     # sanity check of db result
289     my ($host_name, $host_key);
290     if ((defined $res) && (@$res > 0) && @{@$res[0]} > 0) {
291         $host_name = @{@$res[0]}[0];
292         $host_key = @{@$res[0]}[2];
293     } else {
294         &main::daemon_log("$session_id ERROR: cannot determine host_name and host_key from known_clients_db at function ping\n$msg", 1);
295         $error = 1;
296     }
298     if (not $error) {
299         my $client_hash = &create_xml_hash("ping", $main::server_address, $host_name);
300         &add_content2xml_hash($client_hash, 'session_id', $session_id); 
301         my $client_msg = &create_xml_string($client_hash);
302         &main::send_msg_to_target($client_msg, $host_name, $host_key, $header, $session_id);
304         my $message_id;
305         my $i = 0;
306         while (1) {
307             $i++;
308             $sql = "SELECT * FROM $main::incoming_tn WHERE headertag='answer_$session_id'";
309             $res = $main::incoming_db->exec_statement($sql);
310             if (ref @$res[0] eq "ARRAY") { 
311                 $message_id = @{@$res[0]}[0];
312                 last;
313             }
315             # do not run into a endless loop
316             if ($i > 100) { last; }
317             usleep(100000);
318         }
320         # if an answer to the question exists
321         if (defined $message_id) {
322             my $answer_xml = @{@$res[0]}[3];
323             my %data = ( 'answer_xml'  => 'bin noch da' );
324             $answer_msg = &build_msg("got_ping", $target, $source, \%data);
325             my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
326             if (defined $forward_to_gosa){
327                 $answer_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
328             }
329             $sql = "DELETE FROM $main::incoming_tn WHERE id=$message_id"; 
330             $res = $main::incoming_db->exec_statement($sql);
331         }
333     }
335     return ( $answer_msg );
340 sub gen_smb_hash {
341      my ($msg, $msg_hash, $session_id) = @_ ;
342      my $source = @{$msg_hash->{source}}[0];
343      my $target = @{$msg_hash->{target}}[0];
344      my $password = @{$msg_hash->{password}}[0];
346      my %data= ('hash' => join(q[:], ntlmgen $password));
347      my $out_msg = &build_msg("gen_smb_hash", $target, $source, \%data );
348      my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
349      if (defined $forward_to_gosa) {
350          $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
351      }
353      return ( $out_msg );
357 sub network_completition {
358      my ($msg, $msg_hash, $session_id) = @_ ;
359      my $source = @{$msg_hash->{source}}[0];
360      my $target = @{$msg_hash->{target}}[0];
361      my $name = @{$msg_hash->{hostname}}[0];
363      # Can we resolv the name?
364      my %data;
365      if (inet_aton($name)){
366              my $address = inet_ntoa(inet_aton($name));
367              my $p = Net::Ping->new('tcp');
368              my $mac= "";
369              if ($p->ping($address, 1)){
370                $mac = Net::ARP::arp_lookup("", $address);
371              }
373              %data= ('ip' => $address, 'mac' => $mac);
374      } else {
375              %data= ('ip' => '', 'mac' => '');
376      }
378      my $out_msg = &build_msg("network_completition", $target, $source, \%data );
379      my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
380      if (defined $forward_to_gosa) {
381          $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
382      }
384      return ( $out_msg );
388 sub detect_hardware {
389     my ($msg, $msg_hash, $session_id) = @_ ;
390     # just forward msg to client, but dont forget to split off 'gosa_' in header
391     my $source = @{$msg_hash->{source}}[0];
392     my $target = @{$msg_hash->{target}}[0];
393     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
394     if( defined $jobdb_id) {
395         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
396         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
397         my $res = $main::job_db->exec_statement($sql_statement);
398     }
400     my $out_hash = &create_xml_hash("detect_hardware", $source, $target);
401     if( defined $jobdb_id ) { 
402         &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id); 
403     }
404     my $out_msg = &create_xml_string($out_hash);
406     my @out_msg_l = ( $out_msg );
407     return @out_msg_l;
411 sub trigger_reload_syslog_config {
412     my ($msg, $msg_hash, $session_id) = @_ ;
414     # Sanity check of macaddress
415     # TODO
417     my $macaddress = @{$msg_hash->{macaddress}}[0];
419     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
420     if( defined $jobdb_id) {
421         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
422         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
423         my $res = $main::job_db->exec_statement($sql_statement);
424     }
426         my $out_msg = &ClientPackages::new_syslog_config($macaddress, $session_id);
427         my @out_msg_l = ( $out_msg );
429     return @out_msg_l;
431    
434 sub trigger_reload_ntp_config {
435     my ($msg, $msg_hash, $session_id) = @_ ;
437     # Sanity check of macaddress
438     # TODO
440     my $macaddress = @{$msg_hash->{macaddress}}[0];
442     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
443     if( defined $jobdb_id) {
444         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
445         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
446         my $res = $main::job_db->exec_statement($sql_statement);
447     }
449         my $out_msg = &ClientPackages::new_ntp_config($macaddress, $session_id);
450         my @out_msg_l = ( $out_msg );
452     return @out_msg_l;
456 sub trigger_reload_ldap_config {
457     my ($msg, $msg_hash, $session_id) = @_ ;
458     my $target = @{$msg_hash->{target}}[0];
460     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
461     if( defined $jobdb_id) {
462         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
463         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
464         my $res = $main::job_db->exec_statement($sql_statement);
465     }
467         my $out_msg = &ClientPackages::new_ldap_config($target, $session_id);
468         my @out_msg_l = ( $out_msg );
470     return @out_msg_l;
474 sub set_activated_for_installation {
475     my ($msg, $msg_hash, $session_id) = @_;
476     my $header = @{$msg_hash->{header}}[0];
477     my $source = @{$msg_hash->{source}}[0];
478     my $target = @{$msg_hash->{target}}[0];
479         my $mac= (defined($msg_hash->{'macaddress'}))?@{$msg_hash->{'macaddress'}}[0]:undef;
480         my @out_msg_l;
482         # update status of job 
483     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
484     if( defined $jobdb_id) {
485         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
486         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
487         my $res = $main::job_db->exec_statement($sql_statement);
488     }
490     # If a client gets a 'set_activated_for_installation' msg, always deliver a fresh 'new_ldap_config'
491     # just for backup and robustness purposes
492     my $ldap_out_msg = &ClientPackages::new_ldap_config($mac, $session_id);
493     push(@out_msg_l, $ldap_out_msg);
495         # create set_activated_for_installation message for delivery
496     my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
497     if( defined $jobdb_id ) { 
498         &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id); 
499     }
500     my $out_msg = &create_xml_string($out_hash);
501         push(@out_msg_l, $out_msg); 
503     return @out_msg_l;
507 sub trigger_action_faireboot {
508     my ($msg, $msg_hash, $session_id) = @_;
509     my $macaddress = @{$msg_hash->{macaddress}}[0];
510     my $source = @{$msg_hash->{source}}[0];
512     my @out_msg_l;
513     $msg =~ s/<header>gosa_trigger_action_faireboot<\/header>/<header>trigger_action_faireboot<\/header>/;
514     push(@out_msg_l, $msg);
516     &main::change_goto_state('locked', \@{$msg_hash->{macaddress}}, $session_id);
517         &main::change_fai_state('install', \@{$msg_hash->{macaddress}}, $session_id); 
519     # set job to status 'done', job will be deleted automatically
520     my $sql_statement = "UPDATE $main::job_queue_tn ".
521         "SET status='done', modified='1'".
522         "WHERE (macaddress='$macaddress' AND status='processing')";
523     &main::daemon_log("$session_id DEBUG: $sql_statement", 7);
524     my $res = $main::job_db->update_dbentry( $sql_statement );
526     return @out_msg_l;
530 sub trigger_action_lock {
531     my ($msg, $msg_hash, $session_id) = @_;
532     my $macaddress = @{$msg_hash->{macaddress}}[0];
533     my $source = @{$msg_hash->{source}}[0];
535     &main::change_goto_state('locked', \@{$msg_hash->{macaddress}}, $session_id);
536     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
537     if( defined $jobdb_id) {
538         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
539         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
540         my $res = $main::job_db->exec_statement($sql_statement);
541     }
542                                              
543     my @out_msg_l;
544     return @out_msg_l;
548 sub trigger_action_activate {
549     my ($msg, $msg_hash, $session_id) = @_;
550     my $macaddress = @{$msg_hash->{macaddress}}[0];
551     my $source = @{$msg_hash->{source}}[0];
553     &main::change_goto_state('active', \@{$msg_hash->{macaddress}}, $session_id);
554     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
555     if( defined $jobdb_id) {
556         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
557         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
558         my $res = $main::job_db->exec_statement($sql_statement);
559     }
560                                              
561     my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $macaddress);
562     if( exists $msg_hash->{'jobdb_id'} ) { 
563         &add_content2xml_hash($out_hash, 'jobdb_id', @{$msg_hash->{'jobdb_id'}}[0]); 
564     }
565     my $out_msg = &create_xml_string($out_hash);
567     my @out_msg_l = ($out_msg);  
568     return @out_msg_l;
573 sub trigger_action_localboot {
574     my ($msg, $msg_hash, $session_id) = @_;
575     $msg =~ s/<header>gosa_trigger_action_localboot<\/header>/<header>trigger_action_localboot<\/header>/;
576     &main::change_fai_state('localboot', \@{$msg_hash->{macaddress}}, $session_id);
577     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
578     if( defined $jobdb_id) {
579         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
580         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
581         my $res = $main::job_db->exec_statement($sql_statement);
582     }
584     my @out_msg_l = ($msg);  
585     return @out_msg_l;
589 sub trigger_action_halt {
590     my ($msg, $msg_hash, $session_id) = @_;
591     $msg =~ s/<header>gosa_trigger_action_halt<\/header>/<header>trigger_action_halt<\/header>/;
593     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
594     if( defined $jobdb_id) {
595         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
596         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
597         my $res = $main::job_db->exec_statement($sql_statement);
598     }
600     my @out_msg_l = ($msg);  
601     return @out_msg_l;
605 sub trigger_action_reboot {
606     my ($msg, $msg_hash, $session_id) = @_;
607     $msg =~ s/<header>gosa_trigger_action_reboot<\/header>/<header>trigger_action_reboot<\/header>/;
609     &main::change_fai_state('reboot', \@{$msg_hash->{macaddress}}, $session_id);
610     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
611     if( defined $jobdb_id) {
612         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
613         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
614         my $res = $main::job_db->exec_statement($sql_statement);
615     }
617     my @out_msg_l = ($msg);  
618     return @out_msg_l;
622 sub trigger_action_memcheck {
623     my ($msg, $msg_hash, $session_id) = @_ ;
624     $msg =~ s/<header>gosa_trigger_action_memcheck<\/header>/<header>trigger_action_memcheck<\/header>/;
626     &main::change_fai_state('memcheck', \@{$msg_hash->{macaddress}}, $session_id);
627     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
628     if( defined $jobdb_id) {
629         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
630         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
631         my $res = $main::job_db->exec_statement($sql_statement);
632     }
634     my @out_msg_l = ($msg);  
635     return @out_msg_l;
639 sub trigger_action_reinstall {
640     my ($msg, $msg_hash, $session_id) = @_;
641     $msg =~ s/<header>gosa_trigger_action_reinstall<\/header>/<header>trigger_action_reinstall<\/header>/;
643     &main::change_fai_state('reinstall', \@{$msg_hash->{macaddress}}, $session_id);
645     my %data = ( 'macaddress'  => \@{$msg_hash->{macaddress}} );
646     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
647     # invoke trigger wake for this gosa-si-server
648     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
650     my @out_msg_l = ($wake_msg, $msg);  
651     return @out_msg_l;
655 sub trigger_action_update {
656     my ($msg, $msg_hash, $session_id) = @_;
657     $msg =~ s/<header>gosa_trigger_action_update<\/header>/<header>trigger_action_update<\/header>/;
659     &main::change_fai_state('update', \@{$msg_hash->{macaddress}}, $session_id);
661     my %data = ( 'macaddress'  => \@{$msg_hash->{macaddress}} );
662     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
663     # invoke trigger wake for this gosa-si-server
664     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
666     my @out_msg_l = ($wake_msg, $msg);  
667     return @out_msg_l;
671 sub trigger_action_instant_update {
672     my ($msg, $msg_hash, $session_id) = @_;
673     $msg =~ s/<header>gosa_trigger_action_instant_update<\/header>/<header>trigger_action_instant_update<\/header>/;
675     &main::change_fai_state('update', \@{$msg_hash->{macaddress}}, $session_id);
677     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
678     if( defined $jobdb_id) {
679         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
680         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
681         my $res = $main::job_db->exec_statement($sql_statement);
682     }
685     my %data = ( 'macaddress'  => \@{$msg_hash->{macaddress}} );
686     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
687     # invoke trigger wake for this gosa-si-server
688     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
690     my @out_msg_l = ($wake_msg, $msg);  
691     return @out_msg_l;
695 sub trigger_action_sysinfo {
696     my ($msg, $msg_hash, $session_id) = @_;
697     $msg =~ s/<header>gosa_trigger_action_sysinfo<\/header>/<header>trigger_action_sysinfo<\/header>/;
699     &main::change_fai_state('sysinfo', \@{$msg_hash->{macaddress}}, $session_id);
700     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
701     if( defined $jobdb_id) {
702         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
703         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
704         my $res = $main::job_db->exec_statement($sql_statement);
705     }
707     my @out_msg_l = ($msg);  
708     return @out_msg_l;
712 sub new_key_for_client {
713     my ($msg, $msg_hash, $session_id) = @_;
715     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
716     if( defined $jobdb_id) {
717         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
718         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
719         my $res = $main::job_db->exec_statement($sql_statement);
720     }
721     
722     $msg =~ s/<header>gosa_new_key_for_client<\/header>/<header>new_key<\/header>/;
723     my @out_msg_l = ($msg);  
724     return @out_msg_l;
728 sub trigger_action_rescan {
729     my ($msg, $msg_hash, $session_id) = @_;
731     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
732     if( defined $jobdb_id) {
733         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
734         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
735         my $res = $main::job_db->exec_statement($sql_statement);
736     }
739     $msg =~ s/<header>gosa_trigger_action_rescan<\/header>/<header>detect_hardware<header>/;
740     my @out_msg_l = ($msg);  
741     return @out_msg_l;
745 sub trigger_action_wake {
746     my ($msg, $msg_hash, $session_id) = @_;
747     
748     my $jobdb_id = @{$msg_hash->{'jobdb_id'}}[0];
749     if( defined $jobdb_id) {
750         my $sql_statement = "UPDATE $main::job_queue_tn SET status='processed' WHERE id=$jobdb_id";
751         &main::daemon_log("$session_id DEBUG: $sql_statement", 7); 
752         my $res = $main::job_db->exec_statement($sql_statement);
753     }
755     # build out message
756     my $out_hash = &create_xml_hash("trigger_wake", "GOSA", "KNOWN_SERVER");
757     foreach (@{$msg_hash->{'macaddress'}}) {
758         &add_content2xml_hash($out_hash, 'macaddress', $_);
759     }
760     if (defined $jobdb_id){
761         &add_content2xml_hash($out_hash, 'jobdb_id', $jobdb_id);
762     }
763     my $out_msg = &create_xml_string($out_hash);
764     
765     # invoke trigger wake for this gosa-si-server
766     &main::server_server_com::trigger_wake($out_msg, $out_hash, $session_id);
768     # send trigger wake to all other gosa-si-server
769     my @out_msg_l = ($out_msg);  
770     return @out_msg_l;
774 sub get_available_kernel {
775   my ($msg, $msg_hash, $session_id) = @_;
777   my $source = @{$msg_hash->{'source'}}[0];
778   my $target = @{$msg_hash->{'target'}}[0];
779   my $fai_release= @{$msg_hash->{'fai_release'}}[0];
781   my @kernel;
782   # Get Kernel packages for release
783   my $sql_statement = "SELECT * FROM $main::packages_list_tn WHERE distribution='$fai_release' AND package LIKE 'linux\-image\-%'";
784   my $res_hash = $main::packages_list_db->select_dbentry($sql_statement);
785   my %data;
786   my $i=1;
788   foreach my $package (keys %{$res_hash}) {
789     $data{"answer".$i++}= $data{"answer".$i++}= ${$res_hash}{$package}->{'package'};
790   }
791   $data{"answer".$i++}= "default";
793   my $out_msg = &build_msg("get_available_kernel", $target, $source, \%data);
794   my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
795   if (defined $forward_to_gosa) {
796     $out_msg =~s/<\/xml>/<forward_to_gosa>$forward_to_gosa<\/forward_to_gosa><\/xml>/;
797   }
799   return ( $out_msg );
802 sub trigger_activate_new {
803         my ($msg, $msg_hash, $session_id) = @_;
805         my $source = @{$msg_hash->{'source'}}[0];
806         my $target = @{$msg_hash->{'target'}}[0];
807         my $header= @{$msg_hash->{'header'}}[0];
808         my $mac= (defined($msg_hash->{'mac'}))?@{$msg_hash->{'mac'}}[0]:undef;
809         my $ogroup= (defined($msg_hash->{'ogroup'}))?@{$msg_hash->{'ogroup'}}[0]:undef;
810         my $timestamp= (defined($msg_hash->{'timestamp'}))?@{$msg_hash->{'timestamp'}}[0]:undef;
811         my $base= (defined($msg_hash->{'base'}))?@{$msg_hash->{'base'}}[0]:undef;
812         my $hostname= (defined($msg_hash->{'fqdn'}))?@{$msg_hash->{'fqdn'}}[0]:undef;
813         my $ip_address= (defined($msg_hash->{'ip'}))?@{$msg_hash->{'ip'}}[0]:undef;
814         my $dhcp_statement= (defined($msg_hash->{'dhcp'}))?@{$msg_hash->{'dhcp'}}[0]:undef;
815         my $jobdb_id= (defined($msg_hash->{'jobdb_id'}))?@{$msg_hash->{'jobdb_id'}}[0]:undef;
817     # Sanity check for base
818     if (ref($base) eq "HASH") {
819         # Incoming msg has a xml tag 'base' but no content
820         $base = undef;
821     }
823     # In case that the client is sleeping, wake it up
824     my %data = ( 'macaddress'  => $mac );
825     my $wake_msg = &build_msg("trigger_wake", "GOSA", "KNOWN_SERVER", \%data);
826     &main::server_server_com::trigger_wake($msg, $msg_hash, $session_id);
827     my $sql_statement= "SELECT * FROM $main::known_server_tn";
828     my $query_res = $main::known_server_db->select_dbentry( $sql_statement ); 
829     while( my ($hit_num, $hit) = each %{ $query_res } ) {    
830         my $host_name = $hit->{hostname};
831         my $host_key = $hit->{hostkey};
832         $wake_msg =~ s/<target>\S+<\/target>/<target>$host_name<\/target>/g;
833         my $error = &main::send_msg_to_target($wake_msg, $host_name, $host_key, $header, $session_id);
834     }
836         my $ldap_handle = &main::get_ldap_handle();
837         my $ldap_entry;
838         my $ogroup_entry;
839         my $changed_attributes_counter = 0;
841     my $activate_client = 0;
842         
843     if(defined($ogroup)) {
844       my $ldap_mesg= $ldap_handle->search(
845         base => $main::ldap_base,
846         scope => 'sub',
847         filter => "(&(objectClass=gosaGroupOfnames)(cn=$ogroup))",
848       );
849       if($ldap_mesg->count == 1) {
850         $ogroup_entry= $ldap_mesg->pop_entry();
851         &main::daemon_log("$session_id DEBUG: A GosaGroupOfNames with cn '$ogroup' was found in base '".$main::ldap_base."'!", 5);
852       } elsif ($ldap_mesg->count == 0) {
853         &main::daemon_log("$session_id ERROR: A GosaGroupOfNames with cn '$ogroup' was not found in base '".$main::ldap_base."'!", 1);
854         $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
855         return undef;
856       } else {
857         &main::daemon_log("$session_id ERROR: More than one ObjectGroups with cn '$ogroup' was found in base '".$main::ldap_base."'!", 1);
858         $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
859         return undef;
860       }
862       # build the base, use optional base parameter or take it from ogroup
863       if(!(defined($base) && (length($base) > 0))) {
864         # Subtract the ObjectGroup cn
865         $base = $1 if $ogroup_entry->dn =~ /cn=$ogroup,ou=groups,(.*)$/;
866         &main::daemon_log("$session_id DEBUG: New base for system with mac address '$mac' is '$base'", 5);
867       }
868     }
870     # prepend ou=systems (configurable through config)
871     $base = $main::new_systems_ou.",".$base;
873     # Search for an existing entry (should be in ou=incoming)
874     my $ldap_mesg= $ldap_handle->search(
875       base => $main::ldap_base,
876       scope => 'sub',
877       filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
878     );
880     # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
881     if($ldap_mesg->count == 1) {
882       &main::daemon_log("$session_id DEBUG: One system with mac address '$mac' was found in base '".$main::ldap_base."'!", 5);
883       # Get the entry from LDAP
884       $ldap_entry= $ldap_mesg->pop_entry();
886       if(!($ldap_entry->dn() eq "cn=".$ldap_entry->get_value('cn').",$base")) {
887         # Move the entry to the new ou
888         $ldap_entry->changetype('moddn');
889         $ldap_entry->add(
890           newrdn => "cn=".$ldap_entry->get_value('cn'),
891           deleteoldrdn => 1,
892           newsuperior => $base,
893         );
894         # To prevent replication problems just re-queue the job with 10 seconds in the future
895         my $moddn_result = $ldap_entry->update($ldap_handle);
896         if ($moddn_result->code() != 0) {
897             my $error_string = "Moving the system with mac address '$mac' to new base '$base' failed (code '".$moddn_result->code()."') with '".$moddn_result->{'errorMessage'}."'!";
898             &main::daemon_log("$session_id ERROR: $error_string", 1);
899             my $sql = "UPDATE $main::job_queue_tn SET status='error', result='$error_string' WHERE id=$jobdb_id";
900             return undef;
901         } else {
902           &main::daemon_log("$session_id INFO: System with mac address '$mac' was moved to base '".$main::ldap_base."'! Re-queuing job.", 4);
903           $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 10))."' WHERE id = $jobdb_id");
904           return undef;
905         }
906       }
908     } elsif ($ldap_mesg->count == 0) {
909       &main::daemon_log("$session_id WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
910       my $sql_statement = "UPDATE ".$main::job_queue_tn.
911               " SET status='waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' ".
912               " WHERE id = $jobdb_id";
913       $main::job_db->exec_statement($sql_statement);
914       return undef;
915     }
917     $ldap_mesg= $ldap_handle->search(
918       base => $main::ldap_base,
919       scope => 'sub',
920       filter => "(&(objectClass=GOhard)(|(macAddress=$mac)(dhcpHWaddress=$mac)))",
921     );
923     # TODO: Find a way to guess an ip address for hosts with no ldap entry (MAC->ARP->IP)
924     if($ldap_mesg->count == 1) {
925       $ldap_entry= $ldap_mesg->pop_entry();
926       # Check for needed objectClasses
927       my $oclasses = $ldap_entry->get_value('objectClass', asref => 1);
928       foreach my $oclass ("FAIobject", "GOhard", "gotoWorkstation") {
929         if(!(scalar grep $_ eq $oclass, map {$_ => 1} @$oclasses)) {
930           &main::daemon_log("$session_id INFO: Adding objectClass '$oclass' to system entry with mac adress '$mac'", 1);
931           $ldap_entry->add(
932             objectClass => $oclass,
933           );
934           my $oclass_result = $ldap_entry->update($ldap_handle);
935           if ($oclass_result->code() != 0) {
936             &main::daemon_log("$session_id ERROR: Adding the ObjectClass '$oclass' failed (code '".$oclass_result->code()."') with '".$oclass_result->{'errorMessage'}."'!", 1);
937           } else {
938             &main::daemon_log("$session_id DEBUG: Adding the ObjectClass '$oclass' to '".($ldap_entry->dn())."' succeeded!", 5);
939           }
940         }
941       }
943       # Set FAIstate
944       if(defined($ldap_entry->get_value('FAIstate'))) {
945         if(!($ldap_entry->get_value('FAIstate') eq 'install')) {
946           $ldap_entry->replace(
947             'FAIstate' => 'install'
948           );
949           my $replace_result = $ldap_entry->update($ldap_handle);
950           if ($replace_result->code() != 0) {
951             &main::daemon_log("$session_id ERROR: Setting the FAIstate to install failed with code '".$replace_result->code()."') and message '".$replace_result->{'errorMessage'}."'!", 1);
952           } else {
953             &main::daemon_log("$session_id DEBUG: Setting the FAIstate to install for '".($ldap_entry->dn())."' succeeded!", 5);
954           }
955         }
956       } else {
957         $ldap_entry->add(
958           'FAIstate' => 'install'
959         );
960         my $add_result = $ldap_entry->update($ldap_handle);
961         if ($add_result->code() != 0) {
962           &main::daemon_log("$session_id ERROR: Setting the FAIstate to install failed with code '".$add_result->code()."') and message '".$add_result->{'errorMessage'}."'!", 1);
963         } else {
964           &main::daemon_log("$session_id DEBUG: Setting the FAIstate to install for '".($ldap_entry->dn())."' succeeded!", 5);
965         }
966       }
969     } elsif ($ldap_mesg->count == 0) {
970       # TODO: Create a new entry
971       # $ldap_entry = Net::LDAP::Entry->new();
972       # $ldap_entry->dn("cn=$mac,$base");
973       &main::daemon_log("$session_id WARNING: No System with mac address '$mac' was found in base '".$main::ldap_base."'! Re-queuing job.", 4);
974       $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting', timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
975       return undef;
976     } else {
977       &main::daemon_log("$session_id ERROR: More than one system with mac address '$mac' was found in base '".$main::ldap_base."'!", 1);
978     }
980     # Add to ObjectGroup
981     my $ogroup_member = $ogroup_entry->get_value('member', asref => 1);
982     if( (!defined($ogroup_member)) ||
983         (!defined($ldap_entry)) ||
984         (!defined($ldap_entry->dn)) ||
985         (!(scalar grep $_ eq $ldap_entry->dn, @{$ogroup_member}))) {
986       $ogroup_entry->add (
987         'member' => $ldap_entry->dn(),
988       );
989       my $ogroup_result = $ogroup_entry->update($ldap_handle);
990       if ($ogroup_result->code() != 0) {
991         &main::daemon_log("$session_id ERROR: Updating the ObjectGroup '$ogroup' failed (code '".$ogroup_result->code()."') with '".$ogroup_result->{'errorMessage'}."'!", 1);
992       } else {
993         &main::daemon_log("$session_id DEBUG: Updating the ObjectGroup '$ogroup' for member '".($ldap_entry->dn())."' succeeded!", 5);
994       }
995     } else {
996       &main::daemon_log("$session_id DEBUG: System with mac address '$mac' is already a member of ObjectGroup '$ogroup'.", 5);
997     }
999     # Finally set gotoMode to active
1000     if(defined($ldap_entry->get_value('gotoMode'))) {
1001       if(!($ldap_entry->get_value('gotoMode') eq 'active')) {
1002         $ldap_entry->replace(
1003           'gotoMode' => 'active'
1004         );
1005         my $activate_result = $ldap_entry->update($ldap_handle);
1006         if ($activate_result->code() != 0) {
1007           &main::daemon_log("$session_id ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
1008         } else {
1009           &main::daemon_log("$session_id DEBUG: Activating system '".$ldap_entry->dn()."' succeeded!", 5);
1010           $activate_client = 1;
1011         }
1012       } else {
1013           $activate_client = 1;
1014       }
1015     } else {
1016       $ldap_entry->add(
1017         'gotoMode' => 'active'
1018       );
1019       my $activate_result = $ldap_entry->update($ldap_handle);
1020       if ($activate_result->code() != 0) {
1021         &main::daemon_log("$session_id ERROR: Activating system '".$ldap_entry->dn()."' failed (code '".$activate_result->code()."') with '".$activate_result->{'errorMessage'}."'!", 1);
1022       } else {
1023         &main::daemon_log("$session_id DEBUG: Activating system '".$ldap_entry->dn()."' succeeded!", 5);
1024         $activate_client = 1;
1025       }
1026     }
1028     if($activate_client == 1) {
1029         &main::daemon_log("$session_id DEBIG: Activating system with mac address '$mac'!", 5);
1031         # Create delivery list
1032         my @out_msg_l;
1034         # Set job to done
1035         $main::job_db->exec_statement("UPDATE jobs SET status = 'done' WHERE id = $jobdb_id");
1036     
1037         # If a client gets a 'set_activated_for_installation' msg, always deliver a fresh 'new_ldap_config'
1038         # just for backup and robustness purposes
1039         my $ldap_out_msg = &ClientPackages::new_ldap_config($mac, $session_id);
1040         push(@out_msg_l, $ldap_out_msg);
1042         # create set_activated_for_installation message for delivery
1043         my $out_hash = &create_xml_hash("set_activated_for_installation", $source, $target);
1044         my $out_msg = &create_xml_string($out_hash);
1045         push(@out_msg_l, $out_msg);
1047         # Return delivery list of messages
1048         return @out_msg_l;
1050     } else {
1051       &main::daemon_log("$session_id WARNING: Activating system with mac address '$mac' failed! Re-queuing job.", 4);
1052       $main::job_db->exec_statement("UPDATE ".$main::job_queue_tn." SET status = 'waiting',  timestamp = '".(&calc_timestamp(&get_time(), 'plus', 60))."' WHERE id = $jobdb_id");
1053     }
1054     return undef;
1058 #sub get_dak_keyring {
1059 #    my ($msg, $msg_hash) = @_;
1060 #    my $source = @{$msg_hash->{'source'}}[0];
1061 #    my $target = @{$msg_hash->{'target'}}[0];
1062 #    my $header= @{$msg_hash->{'header'}}[0];
1063 #    my $session_id = @{$msg_hash->{'session_id'}}[0];
1065 #    # build return message with twisted target and source
1066 #    my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1067 #    &add_content2xml_hash($out_hash, "session_id", $session_id);
1069 #    my @keys;
1070 #    my %data;
1072 #    my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1074 #    my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1075 #    my $gpg     = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
1077 #    # Check if the keyrings are in place and readable
1078 #    if(
1079 #        &run_as($main::dak_user, "test -r $keyring")->{'resultCode'} != 0
1080 #    ) {
1081 #        &add_content2xml_hash($out_hash, "error", "DAK Keyring is not readable");
1082 #    } else {
1083 #        my $command = "$gpg --list-keys";
1084 #        my $output = &run_as($main::dak_user, $command);
1085 #        &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1087 #        my $i=0;
1088 #        foreach (@{$output->{'output'}}) {
1089 #            if ($_ =~ m/^pub\s.*$/) {
1090 #                ($keys[$i]->{'pub'}->{'length'}, $keys[$i]->{'pub'}->{'uid'}, $keys[$i]->{'pub'}->{'created'}) = ($1, $2, $3)
1091 #                if $_ =~ m/^pub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
1092 #                $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})\]/;
1093 #                $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})\]/;
1094 #            } elsif ($_ =~ m/^sub\s.*$/) {
1095 #                ($keys[$i]->{'sub'}->{'length'}, $keys[$i]->{'sub'}->{'uid'}, $keys[$i]->{'sub'}->{'created'}) = ($1, $2, $3)
1096 #                if $_ =~ m/^sub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
1097 #                $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})\]/;
1098 #                $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})\]/;
1099 #            } elsif ($_ =~ m/^uid\s.*$/) {
1100 #                push @{$keys[$i]->{'uid'}}, $1 if $_ =~ m/^uid\s*?([^\s].*?)$/;
1101 #            } elsif ($_ =~ m/^$/) {
1102 #                $i++;
1103 #            }
1104 #        }
1105 #    }
1107 #    my $i=0;
1108 #    foreach my $key (@keys) {
1109 #        #    &main::daemon_log(Dumper($key));
1110 #        &add_content2xml_hash($out_hash, "answer".$i++, $key);
1111 #    }
1112 #    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1113 #    if (defined $forward_to_gosa) {
1114 #        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1115 #    }
1116 #    return &create_xml_string($out_hash);
1117 #}
1120 #sub import_dak_key {
1121 #    my ($msg, $msg_hash) = @_;
1122 #    my $source = @{$msg_hash->{'source'}}[0];
1123 #    my $target = @{$msg_hash->{'target'}}[0];
1124 #    my $header= @{$msg_hash->{'header'}}[0];
1125 #    my $session_id = @{$msg_hash->{'session_id'}}[0];
1126 #    my $key = &decode_base64(@{$msg_hash->{'key'}}[0]);
1128 #    # build return message with twisted target and source
1129 #    my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1130 #    &add_content2xml_hash($out_hash, "session_id", $session_id);
1132 #    my %data;
1134 #    my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1136 #    my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1137 #    my $gpg     = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
1139 #    # Check if the keyrings are in place and writable
1140 #    if(
1141 #        &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
1142 #    ) {
1143 #        &add_content2xml_hash($out_hash, "error", "DAK Keyring is not writable");
1144 #    } else {
1145 #        my $keyfile;
1146 #        open($keyfile, ">/tmp/gosa_si_tmp_dak_key");
1147 #        print $keyfile $key;
1148 #        close($keyfile);
1149 #        my $command = "$gpg --import /tmp/gosa_si_tmp_dak_key";
1150 #        my $output = &run_as($main::dak_user, $command);
1151 #        &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1152 #        unlink("/tmp/gosa_si_tmp_dak_key");
1154 #        if($output->{'resultCode'} != 0) {
1155 #            &add_content2xml_hash($out_hash, "error", "Import of DAK key failed! Output was '".$output->{'output'}."'");
1156 #        } else {
1157 #            &add_content2xml_hash($out_hash, "answer", "Import of DAK key successfull! Output was '".$output->{'output'}."'");
1158 #        }
1159 #    }
1161 #    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1162 #    if (defined $forward_to_gosa) {
1163 #        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1164 #    }
1165 #    return &create_xml_string($out_hash);
1166 #}
1169 #sub remove_dak_key {
1170 #    my ($msg, $msg_hash) = @_;
1171 #    my $source = @{$msg_hash->{'source'}}[0];
1172 #    my $target = @{$msg_hash->{'target'}}[0];
1173 #    my $header= @{$msg_hash->{'header'}}[0];
1174 #    my $session_id = @{$msg_hash->{'session_id'}}[0];
1175 #    my $key = @{$msg_hash->{'keyid'}}[0];
1176 #    # build return message with twisted target and source
1177 #    my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
1178 #    &add_content2xml_hash($out_hash, "session_id", $session_id);
1180 #    my %data;
1182 #    my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
1184 #    my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
1185 #    my $gpg     = "$gpg_cmd --no-default-keyring --no-random-seed --homedir ".$main::dak_signing_keys_directory." --keyring $keyring";
1187 #    # Check if the keyrings are in place and writable
1188 #    if(
1189 #        &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
1190 #    ) {
1191 #        &add_content2xml_hash($out_hash, "error", "DAK keyring is not writable");
1192 #    } else {
1193 #        # Check if the key is present in the keyring
1194 #        if(&run_as($main::dak_user, "$gpg --list-keys $key")->{'resultCode'} == 0) {
1195 #            my $command = "$gpg --batch --yes --delete-key $key";
1196 #            my $output = &run_as($main::dak_user, $command);
1197 #            &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
1198 #        } else {
1199 #            &add_content2xml_hash($out_hash, "error", "DAK key with id '$key' was not found in keyring");
1200 #        }
1201 #    }
1203 #    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
1204 #    if (defined $forward_to_gosa) {
1205 #        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
1206 #    }
1207 #    return &create_xml_string($out_hash);
1208 #}
1211 #sub get_dak_queue {
1212 #    my ($msg, $msg_hash, $session_id) = @_;
1213 #    my %data;
1214 #    my $source = @{$msg_hash->{'source'}}[0];
1215 #    my $target = @{$msg_hash->{'target'}}[0];
1216 #    my $header= @{$msg_hash->{'header'}}[0];
1218 #    my %data;
1220 #    foreach my $dir ("unchecked", "new", "accepted") {
1221 #        foreach my $file(<"$main::dak_queue_directory/$dir/*.changes">) {
1222 #        }
1223 #    }
1225 #    my $out_msg = &build_msg("get_dak_queue", $target, $source, \%data);
1226 #    my @out_msg_l = ($out_msg);
1227 #    return @out_msg_l;
1228 #}
1230 ## @method get_hosts_with_module
1231 # Reports all GOsa-si-server providing the given module. 
1232 # @param msg - STRING - xml message with tag get_hosts_with_module
1233 # @param msg_hash - HASHREF - message information parsed into a hash
1234 # @param session_id - INTEGER - POE session id of the processing of this message
1235 # @return out_msg - STRING - feedback to GOsa in success and error case
1236 sub get_hosts_with_module {
1237     my ($msg, $msg_hash, $session_id) = @_;
1238     my $source = @{$msg_hash->{'source'}}[0];
1239     my $target = @{$msg_hash->{'target'}}[0];
1240     my $header= @{$msg_hash->{'header'}}[0];
1241     my $module_name = @{$msg_hash->{'module_name'}}[0];
1242     my $out_hash = &create_xml_hash($header, $target, $source);
1244     # Sanity check of module_name
1245     if ((not exists $msg_hash->{'module_name'}) || (@{$msg_hash->{'module_name'}} != 1))  {
1246         &add_content2xml_hash($out_hash, "error_string", "no module_name specified or module_name tag invalid");
1247         &add_content2xml_hash($out_hash, "error", "module_name");
1248         &main::daemon_log("$session_id ERROR: no module_name specified or module_name tag invalid: $msg", 1); 
1249         return (&create_xml_string($out_hash));
1250     }
1252     my $out_msg = &create_xml_string($out_hash);
1254     # Check localhost for module_name
1255     if (exists @{$main::known_modules->{'GosaPackages'}}[2]->{$module_name}) {
1256         my ($local_ip, $local_port) = split(/:/, $target);
1257         my $network_interface= &get_interface_for_ip($local_ip);
1258         my $local_mac = &get_mac_for_interface($network_interface);
1259         $out_msg =~ s/<\/xml>/<result>host0<\/result> <\/xml>/;
1260         my $host_infos = "<ip>$local_ip</ip>";
1261         $host_infos .= " <mac>$local_mac</mac>"; 
1262         $out_msg =~  s/<\/xml>/\n<answer0> $host_infos <\/answer0> \n <\/xml>/;
1263     }
1265     # Search for opsi hosts in server_db
1266     my $sql = "SELECT * FROM $main::known_server_tn WHERE loaded_modules LIKE '%$module_name%'"; 
1267     my $res = $main::known_server_db->select_dbentry($sql);
1268     while (my ($hit_id, $hit_hash) = each %$res) {
1269         $out_msg =~ s/<\/xml>/<result>host$hit_id<\/result> <\/xml>/;
1270         my $host_infos = "<ip>".$hit_hash->{'hostname'}."</ip>";
1271         $host_infos .= " <mac>".$hit_hash->{'macaddress'}."</mac>"; 
1272         $out_msg =~  s/<\/xml>/\n<answer$hit_id> $host_infos <\/answer$hit_id> \n <\/xml>/;
1273     }
1275     return $out_msg;
1278 # vim:ts=4:shiftwidth:expandtab
1279 1;