Code

787b7ce00c51bed47a66b6562855a53b7962afbe
[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 GOSA::DBsqlite;
15 use MIME::Base64;
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 DEBUG: GosaPackages - $log_line", 7);
42     } else {
43         &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
44     }
45 }
48 ## FUNCTIONS #################################################################
50 sub get_module_info {
51     my @info = ($main::gosa_address,
52                 $main::gosa_passwd,
53                 );
54     return \@info;
55 }
58 #===  FUNCTION  ================================================================
59 #         NAME:  get_mac 
60 #   PARAMETERS:  interface name (i.e. eth0)
61 #      RETURNS:  (mac address) 
62 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
63 #===============================================================================
64 sub get_mac {
65     my $ifreq= shift;
66     my $result;
67     if ($ifreq && length($ifreq) > 0) { 
68         if($ifreq eq "all") {
69             $result = "00:00:00:00:00:00";
70         } else {
71             my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
73                 # A configured MAC Address should always override a guessed value
74                 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
75                     $result= $gosa_mac_address;
76                 }
78             socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
79                 or die "socket: $!";
81             if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
82                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
84                 if (length($mac) > 0) {
85                     $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
86                     $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
87                     $result = $mac;
88                 }
89             }
90         }
91     }
92     return $result;
93 }
96 #===  FUNCTION  ================================================================
97 #         NAME:  process_incoming_msg
98 #   PARAMETERS:  crypted_msg - string - incoming crypted message
99 #      RETURNS:  nothing
100 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
101 #===============================================================================
102 sub process_incoming_msg {
103     my ($msg, $msg_hash, $session_id) = @_ ;
104     my $header = @{$msg_hash->{header}}[0];
105     my @msg_l;
106     my @out_msg_l;
108     &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
109     
110     if ($header =~ /^job_/) {
111         @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
112     } 
113     elsif ($header =~ /^gosa_/) {
114         @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
115     } 
116     else {
117         &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
118     }
120     foreach my $out_msg ( @msg_l ) {
121         # determine the correct outgoing source address to the corresponding target address
122         $out_msg =~ /<target>(\S*)<\/target>/;
123         my $act_target = $1;
124         $act_target =~ s/GOSA/$main::server_address/;
125         my $act_server_ip = &main::get_local_ip_for_remote_ip(sprintf("%s", $act_target =~ /^([0-9\.]*?):.*$/));
127         # Patch the correct outgoing source address
128         if ($out_msg =~ /<source>GOSA<\/source>/ ) {
129             $out_msg =~ s/<source>GOSA<\/source>/<source>$act_server_ip:$main::server_port<\/source>/g;
130         }
132         # Add to each outgoing message the current POE session id
133         $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
136         if (defined $out_msg){
137             push(@out_msg_l, $out_msg);
138         }
139     }
141     return \@out_msg_l;
145 sub process_gosa_msg {
146     my ($msg, $msg_hash, $session_id) = @_ ;
147     my $out_msg;
148     my @out_msg_l = ('nohandler');
149     my $sql_events;
151     # strip gosa_ prefix from header, it is not used any more
152     @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
153     $msg =~ s/<header>gosa_/<header>/;
155     my $header = @{$msg_hash->{'header'}}[0];
156     my $target = @{$msg_hash->{'target'}}[0];
158     # check local installed events
159     if( exists $event_hash->{$header} ) {
160         # a event exists with the header as name
161         &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
162         no strict 'refs';
163         @out_msg_l = &{$event_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
165     # check client registered events
166     } else {
167         $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )"; 
168         my $res = $main::known_clients_db->select_dbentry( $sql_events );
169         my $l = keys(%$res);
170         
171         # set error if no or more than 1 hits are found for sql query
172         if ( $l != 1) {
173             @out_msg_l = ('knownclienterror');
174         # found exact 1 hit in db
175         } else {
176             my $client_events = $res->{'1'}->{'events'};
178             # client is registered for this event, deliver this message to client
179             if ($client_events =~ /,$header,/) {
180                 &main::daemon_log("$session_id INFO: client '$target' is registerd for event '$header', forward message to client.", 5);
181                 @out_msg_l = ( $msg );
183             # client is not registered for this event, set error
184             } else {
185                 @out_msg_l = ('noeventerror');
186             }
187         }
188     }
190     # if delivery not possible raise error and return 
191     if (not defined $out_msg_l[0]) {
192         @out_msg_l = ();
193     } elsif ($out_msg_l[0] eq 'nohandler') {
194         &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
195         @out_msg_l = ();
196     } elsif ($out_msg_l[0] eq 'knownclienterror') {
197         &main::daemon_log("$session_id ERROR: no 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         @out_msg_l = ();
201     } elsif ($out_msg_l[0] eq 'noeventerror') {
202         &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1); 
203         @out_msg_l = ();
204     }
206     return @out_msg_l;
210 sub process_job_msg {
211     my ($msg, $msg_hash, $session_id)= @_ ;    
212     my $out_msg;
213     my $error = 0;
215     my $header = @{$msg_hash->{'header'}}[0];
216     $header =~ s/job_//;
217         my $target = @{$msg_hash->{'target'}}[0];
218     
219     # If no timestamp is specified, use 19700101000000
220     my $timestamp = "19700101000000";
221     if( exists $msg_hash->{'timestamp'} ) {
222         $timestamp = @{$msg_hash->{'timestamp'}}[0];
223     }
225     # If no macaddress is specified, raise error 
226     my $macaddress;
227     if( exists $msg_hash->{'macaddress'} ) {
228         $macaddress = @{$msg_hash->{'macaddress'}}[0];
229     } 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 ) {
230         $macaddress = $1;
231     } else {
232         $error ++;
233         $out_msg = "<xml>".
234             "<header>answer</header>".
235             "<source>$main::server_address</source>".
236             "<target>GOSA</target>".
237             "<answer1>1</answer1>".
238             "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
239             "</xml>";
240     }
241     
242     # Determine plain_name for host
243     my $plain_name;
244     if ($header eq "opsi_install_client") {   # Opsi installing clients use hostId as plain_name
245         if (not exists $msg_hash->{'hostId'}) {
246             $error++;
247             &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1);
248         } else {
249             $plain_name = $msg_hash->{'hostId'}[0];
250         }
252     } else {   # Try to determine plain_name via ladp search
253         my $ldap_handle = &main::get_ldap_handle($session_id); 
254         if( not defined $ldap_handle ) {
255             &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
256             $plain_name = "none"; 
257         } else {
258             my $mesg = $ldap_handle->search(
259                     base => $main::ldap_base,
260                     scope => 'sub',
261                     attrs => ['cn'],
262                     filter => "(macAddress=$macaddress)");
263             if($mesg->code) {
264                 &main::daemon_log($mesg->error, 1);
265                 $plain_name = "none";
266             } else {
267                 my $entry= $mesg->entry(0);
268                 $plain_name = $entry->get_value("cn");
269             }
270         }
271     }
272         
273     # Add job to job queue
274     if( $error == 0 ) {
275         my $func_dic = {table=>$main::job_queue_tn, 
276             primkey=>['macaddress', 'headertag'],
277             timestamp=>$timestamp,
278             status=>'waiting', 
279             result=>'none',
280             progress=>'none',
281             headertag=>$header, 
282             targettag=>$target,
283             xmlmessage=>$msg,
284             macaddress=>$macaddress,
285                         plainname=>$plain_name,
286             siserver=>"localhost",
287             modified=>"1",
288         };
289         my $res = $main::job_db->add_dbentry($func_dic);
290         if (not $res == 0) {
291             &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
292         } else {
293             &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
294         }
295         $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
296     }
297     
298     my @out_msg_l = ( $out_msg );
299     return @out_msg_l;
302 # vim:ts=4:shiftwidth:expandtab
303 1;