Code

Updated some sieve templates
[gosa.git] / gosa-si / modules / GosaPackages.pm
1 package GosaPackages;
3 use strict;
4 use warnings;
6 use Exporter;
7 use GOSA::GosaSupportDaemon;
8 use IO::Socket::INET;
9 use Socket;
10 use XML::Simple;
11 use File::Spec;
12 use Data::Dumper;
13 use MIME::Base64;
14 use Net::ARP;
16 our @ISA = ("Exporter");
18 my $event_dir = "/usr/lib/gosa-si/server/GosaPackages";
19 use lib "/usr/lib/gosa-si/server/GosaPackages";
21 BEGIN{}
22 END{}
24 my $network_interface;
25 my $gosa_mac_address;
27 ## START ##########################
29 $network_interface= &get_interface_for_ip($main::server_ip);
30 $gosa_mac_address= &get_mac($network_interface);
32 # complete addresses
33 if( inet_aton($main::server_ip) ){ $main::server_ip = inet_ntoa(inet_aton($main::server_ip)); } 
34 $main::server_address = $main::server_ip.":".$main::server_port;
38 # import local events
39 my ($error, $result, $event_hash) = &import_events($event_dir);
40 foreach my $log_line (@$result) {
41     if ($log_line =~ / succeed: /) {
42         &main::daemon_log("0 INFO: GosaPackages - $log_line", 5);
43     } else {
44         &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
45     }
46 }
48 # build vice versa event_hash, event_name => module
49 my $event2module_hash = {};
50 while (my ($module, $mod_events) = each %$event_hash) {
51     while (my ($event_name, $nothing) = each %$mod_events) {
52         $event2module_hash->{$event_name} = $module;
53     }
55 }
58 ## FUNCTIONS #################################################################
60 sub get_module_info {
61     my @info = ($main::gosa_address,
62                 $main::gosa_passwd,
63                 $event_hash,
64                 );
65     return \@info;
66 }
69 #===  FUNCTION  ================================================================
70 #         NAME:  get_mac 
71 #   PARAMETERS:  interface name (i.e. eth0)
72 #      RETURNS:  (mac address) 
73 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
74 #===============================================================================
75 sub get_mac {
76     my $ifreq= shift;
77     my $result;
78     if ($ifreq && length($ifreq) > 0) {
79         if($ifreq eq "all") {
80             $result = "00:00:00:00:00:00";
81         } else {
82                 # A configured MAC Address should always override a guessed value
83                 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
84                     $result= $gosa_mac_address;
85                 }
87           $result = Net::ARP::get_mac($ifreq);
88         }
89     }
90     return $result;
91 }
94 #===  FUNCTION  ================================================================
95 #         NAME:  process_incoming_msg
96 #   PARAMETERS:  crypted_msg - string - incoming crypted message
97 #      RETURNS:  nothing
98 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
99 #===============================================================================
100 sub process_incoming_msg {
101     my ($msg, $msg_hash, $session_id) = @_ ;
102     my $header = @{$msg_hash->{header}}[0];
103     my @msg_l;
104     my @out_msg_l;
106     &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 26);
107     
108     if ($header =~ /^job_/) {
109         @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
110     } 
111     elsif ($header =~ /^gosa_/) {
112         @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
113     } 
114     else {
115         &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
116     }
118     foreach my $out_msg ( @msg_l ) {
119         # determine the correct outgoing source address to the corresponding target address
120         $out_msg =~ /<target>(\S*)<\/target>/;
121         my $act_target = $1;
122         $act_target =~ s/GOSA/$main::server_address/;
123         my $act_server_ip = &main::get_local_ip_for_remote_ip(sprintf("%s", $act_target =~ /^([0-9\.]*?):.*$/));
125         # Patch the correct outgoing source address
126         if ($out_msg =~ /<source>GOSA<\/source>/ ) {
127             $out_msg =~ s/<source>GOSA<\/source>/<source>$act_server_ip:$main::server_port<\/source>/g;
128         }
130         # Add to each outgoing message the current POE session id
131         $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
134         if (defined $out_msg){
135             push(@out_msg_l, $out_msg);
136         }
137     }
139     return \@out_msg_l;
143 sub process_gosa_msg {
144     my ($msg, $msg_hash, $session_id) = @_ ;
145     my $out_msg;
146     my @out_msg_l = ('nohandler');
147     my $sql_events;
149     # strip gosa_ prefix from header, it is not used any more
150     @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
151     $msg =~ s/<header>gosa_/<header>/;
153     my $header = @{$msg_hash->{'header'}}[0];
154     my $target = @{$msg_hash->{'target'}}[0];
156     # check local installed events
157     if( exists $event2module_hash->{$header} ) {
158         # a event exists with the header as name
159         &main::daemon_log("$session_id DEBUG: found event '$header' at event-module '".$event2module_hash->{$header}."'", 26);
160         no strict 'refs';
161         @out_msg_l = &{$event2module_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
163     # check client registered events
164     } else {
165         $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )"; 
166         my $res = $main::known_clients_db->select_dbentry( $sql_events );
167         my $l = keys(%$res);
168         
169         # set error if no or more than 1 hits are found for sql query
170         if ( $l != 1) {
171             @out_msg_l = ('knownclienterror');
172         # found exact 1 hit in db
173         } else {
174             my $client_events = $res->{'1'}->{'events'};
176             # client is registered for this event, deliver this message to client
177             if (($client_events =~ /^$header,/) || ($client_events =~ /,$header,/) || ($client_events =~ /,$header$/)) {
178                 &main::daemon_log("$session_id DEBUg: client '$target' is registerd for event '$header', forward message to client.", 26);
179                 @out_msg_l = ( $msg );
181             # client is not registered for this event, set error
182             } else {
183                 @out_msg_l = ('noeventerror');
184             }
185         }
186     }
188     # if delivery not possible raise error and return 
189     if (not defined $out_msg_l[0]) {
190         @out_msg_l = ();
191     } elsif ($out_msg_l[0] eq 'nohandler') {
192         &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
193         @out_msg_l = ();
194     } elsif ($out_msg_l[0] eq 'knownclienterror') {
195         if ($header eq "ping") {
196             &main::daemon_log("$session_id WARNING: Cannot send '$header' to '$target'. GOsa-si do not know client. Maybe client is offline or gosa-si-client process is not running.", 3);
197         } else {
198             &main::daemon_log("$session_id ERROR: no general event handler found for '$header', check client registration events!", 1);
199             &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
200             &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
201         }
202         @out_msg_l = ();
203     } elsif ($out_msg_l[0] eq 'noeventerror') {
204         &main::daemon_log("$session_id ERROR: no general event handler found for '$header', check client registration events!", 1);
205         &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1); 
206         @out_msg_l = ();
207     }
209     return @out_msg_l;
213 sub process_job_msg {
214     my ($msg, $msg_hash, $session_id)= @_ ;    
215     my $out_msg;
216     my $error = 0;
218     my $header = @{$msg_hash->{'header'}}[0];
219     $header =~ s/job_//;
220         my $target = @{$msg_hash->{'target'}}[0];
221     
222     # If no timestamp is specified, use 19700101000000
223     my $timestamp = "19700101000000";
224     if( exists $msg_hash->{'timestamp'} ) {
225         $timestamp = @{$msg_hash->{'timestamp'}}[0];
226     }
228     # If no macaddress is specified, raise error 
229     my $macaddress;
230     if( exists $msg_hash->{'macaddress'} ) {
231         $macaddress = @{$msg_hash->{'macaddress'}}[0];
232     } elsif (@{$msg_hash->{'target'}}[0] =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ) {
233         $macaddress = $1;
234     } else {
235         $error ++;
236         $out_msg = "<xml>".
237             "<header>answer</header>".
238             "<source>$main::server_address</source>".
239             "<target>GOSA</target>".
240             "<answer1>1</answer1>".
241             "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
242             "</xml>";
244         return ($out_msg);
245     }
246     
247     # Determine plain_name for host
248     my $plain_name;
249     if ($header eq "opsi_install_client") {   # Opsi installing clients use hostId as plain_name
250         if (not exists $msg_hash->{'hostId'}) {
251             $error++;
252             &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1);
253         } else {
254             $plain_name = $msg_hash->{'hostId'}[0];
255             $header = "trigger_action_reinstall"
256         }
258     } else {   # Try to determine plain_name via ldap search
259         my $ldap_handle=&main::get_ldap_handle();
260         my $mesg = $ldap_handle->search(
261                 base => $main::ldap_base,
262                 scope => 'sub',
263                 attrs => ['cn'],
264                 filter => "(macAddress=$macaddress)");
265         if($mesg->code || ($mesg->count!=1)) {
266             &main::daemon_log($mesg->error, 1);
267             $plain_name = "none";
268         } else {
269             my $entry= $mesg->entry(0);
270             $plain_name = $entry->get_value("cn");
271         }
272         &main::release_ldap_handle($ldap_handle);
273     }
274         
275     # Check if it is a periodical job
276     my $periodic = 'none';
277     if (exists $msg_hash->{periodic})
278     {
279         $periodic = $msg_hash->{periodic}[0];
280         if ($periodic ne 'none' and not $periodic =~ /[0-9]+_(hours|minutes|days|weeks|months)/)    # Periodic tag is not valid
281         {
282             &main::daemon_log("$session_id ERROR: Message contains invalid periodic-tag '$periodic'.".
283                     " Please use the following pattern for the tag: 'INTEGER_[minutes|hours|days|weeks|months]'".
284                     " Aborted message: $msg", 1);
285             $out_msg = "<xml>".
286                 "<header>answer</header><source>$main::server_address</source><target>GOSA</target>".
287                 "<answer1>1</answer1><error_string>Message contains invalid periodic-tag '$periodic'</error_string>".
288                 "</xml>";
289             return ($out_msg);
290         }
291     }
293     # Add job to job queue
294     if( $error == 0 ) {
295         my $func_dic = {table=>$main::job_queue_tn, 
296             primkey=>['macaddress', 'headertag'],
297             timestamp=>$timestamp,
298             status=>'waiting', 
299             result=>'none',
300             progress=>'none',
301             headertag=>$header, 
302             targettag=>$target,
303             xmlmessage=>$msg,
304             macaddress=>$macaddress,
305                         plainname=>$plain_name,
306             siserver=>"localhost",
307             modified=>"1",
308             periodic=>$periodic,
309         };
310         my $res = $main::job_db->add_dbentry($func_dic);
311         if (not $res == 0) {
312             &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
313         } else {
314             &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
315         }
316         $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
317     }
318     
319     my @out_msg_l = ( $out_msg );
320     return @out_msg_l;
323 # vim:ts=4:shiftwidth:expandtab
324 1;