Code

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