Code

Updated list image
[gosa.git] / gosa-si / modules / GosaPackages.pm
index 8c05daf395394b85763d7f5c73adce232245d010..835eaa520108a5015b9238290436f1cf3b3df8e4 100644 (file)
@@ -11,7 +11,6 @@ use Socket;
 use XML::Simple;
 use File::Spec;
 use Data::Dumper;
-use GOSA::DBsqlite;
 use MIME::Base64;
 
 my $event_dir = "/usr/lib/gosa-si/server/GosaPackages";
@@ -20,149 +19,51 @@ use lib "/usr/lib/gosa-si/server/GosaPackages";
 BEGIN{}
 END{}
 
-my ($server_ip, $server_port, $server_passwd, $max_clients);
-my ($gosa_ip, $gosa_mac_address, $gosa_port, $gosa_passwd, $network_interface);
-my ($job_queue_timeout, $job_queue_file_name);
-
-my %cfg_defaults = (
-"server" => {
-    "ip" => [\$server_ip, "0.0.0.0"],
-    "port" => [\$server_port, "20081"],
-    "key" => [\$server_passwd, ""],
-    "max-clients" => [\$max_clients, 100],
-    },
-"GOsaPackages" => {
-    "ip" => [\$gosa_ip, "0.0.0.0"],
-    "port" => [\$gosa_port, "20082"],
-    "key" => [\$gosa_passwd, "none"],
-    "job-queue" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'],
-    },
-);
+my $network_interface;
+my $gosa_mac_address;
 
 ## START ##########################
 
-# read configfile and import variables
-&read_configfile();
-$network_interface= &get_interface_for_ip($server_ip);
+$network_interface= &get_interface_for_ip($main::server_ip);
 $gosa_mac_address= &get_mac($network_interface);
 
 # complete addresses
-if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); } 
-our $server_address = "$server_ip:$server_port";
-if( inet_aton($gosa_ip) ){ $gosa_ip = inet_ntoa(inet_aton($gosa_ip)); }
-$main::gosa_address = "$gosa_ip:$gosa_port";
+if( inet_aton($main::server_ip) ){ $main::server_ip = inet_ntoa(inet_aton($main::server_ip)); } 
+$main::server_address = $main::server_ip.":".$main::server_port;
+
 
-# create general settings for this module
-#y $gosa_cipher = &create_ciphering($gosa_passwd);
-my $xml = new XML::Simple();
 
 # import local events
 my ($error, $result, $event_hash) = &import_events($event_dir);
-if ($error == 0) {
-    foreach my $log_line (@$result) {
-        &main::daemon_log("0 DEBUG: GosaPackages - $log_line", 7);
-    }
-} else {
-    foreach my $log_line (@$result) {
+foreach my $log_line (@$result) {
+    if ($log_line =~ / succeed: /) {
+        &main::daemon_log("0 INFO: GosaPackages - $log_line", 5);
+    } else {
         &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
     }
 }
 
+# build vice versa event_hash, event_name => module
+my $event2module_hash = {};
+while (my ($module, $mod_events) = each %$event_hash) {
+    while (my ($event_name, $nothing) = each %$mod_events) {
+        $event2module_hash->{$event_name} = $module;
+    }
+
+}
+
 
 ## FUNCTIONS #################################################################
 
 sub get_module_info {
     my @info = ($main::gosa_address,
-                $gosa_passwd,
+                $main::gosa_passwd,
+                $event_hash,
                 );
     return \@info;
 }
 
 
-#===  FUNCTION  ================================================================
-#         NAME:  read_configfile
-#   PARAMETERS:  cfg_file - string -
-#      RETURNS:  nothing
-#  DESCRIPTION:  read cfg_file and set variables
-#===============================================================================
-sub read_configfile {
-    my $cfg;
-    if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
-        if( -r $main::cfg_file ) {
-            $cfg = Config::IniFiles->new( -file => $main::cfg_file );
-        } else {
-            print STDERR "Couldn't read config file!";
-        }
-    } else {
-        $cfg = Config::IniFiles->new() ;
-    }
-    foreach my $section (keys %cfg_defaults) {
-        foreach my $param (keys %{$cfg_defaults{ $section }}) {
-            my $pinfo = $cfg_defaults{ $section }{ $param };
-            ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
-        }
-    }
-}
-
-# moved to GosaSupportDaemon: 03-06-2008: rettenbe
-#===  FUNCTION  ================================================================
-#         NAME:  get_interface_for_ip
-#   PARAMETERS:  ip address (i.e. 192.168.0.1)
-#      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
-#  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
-#===============================================================================
-#sub get_interface_for_ip {
-#    my $result;
-#    my $ip= shift;
-#    if ($ip && length($ip) > 0) {
-#        my @ifs= &get_interfaces();
-#        if($ip eq "0.0.0.0") {
-#            $result = "all";
-#        } else {
-#            foreach (@ifs) {
-#                my $if=$_;
-#                if(get_ip($if) eq $ip) {
-#                    $result = $if;
-#                }
-#            }       
-#        }
-#    }       
-#    return $result;
-#}
-
-# moved to GosaSupportDaemon: 03-06-2008: rettenbe
-#===  FUNCTION  ================================================================
-#         NAME:  get_interfaces 
-#   PARAMETERS:  none
-#      RETURNS:  (list of interfaces) 
-#  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
-#===============================================================================
-#sub get_interfaces {
-#        my @result;
-#        my $PROC_NET_DEV= ('/proc/net/dev');
-#
-#        open(PROC_NET_DEV, "<$PROC_NET_DEV")
-#                or die "Could not open $PROC_NET_DEV";
-#
-#        my @ifs = <PROC_NET_DEV>;
-#
-#        close(PROC_NET_DEV);
-#
-#        # Eat first two line
-#        shift @ifs;
-#        shift @ifs;
-#
-#        chomp @ifs;
-#        foreach my $line(@ifs) {
-#                my $if= (split /:/, $line)[0];
-#                $if =~ s/^\s+//;
-#                push @result, $if;
-#        }
-#
-#        return @result;
-#}
-
 #===  FUNCTION  ================================================================
 #         NAME:  get_mac 
 #   PARAMETERS:  interface name (i.e. eth0)
@@ -200,35 +101,6 @@ sub get_mac {
     return $result;
 }
 
-# moved to GosaSupportDaemon: 03-06-2008: rettenbe
-#===  FUNCTION  ================================================================
-#         NAME:  get_ip 
-#   PARAMETERS:  interface name (i.e. eth0)
-#      RETURNS:  (ip address) 
-#  DESCRIPTION:  Uses ioctl to get ip address directly from system.
-#===============================================================================
-#sub get_ip {
-#        my $ifreq= shift;
-#        my $result= "";
-#        my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
-#        my $proto= getprotobyname('ip');
-#
-#        socket SOCKET, PF_INET, SOCK_DGRAM, $proto
-#                or die "socket: $!";
-#
-#        if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
-#                my ($if, $sin)    = unpack 'a16 a16', $ifreq;
-#                my ($port, $addr) = sockaddr_in $sin;
-#                my $ip            = inet_ntoa $addr;
-#
-#                if ($ip && length($ip) > 0) {
-#                        $result = $ip;
-#                }
-#        }
-#
-#        return $result;
-#}
-
 
 #===  FUNCTION  ================================================================
 #         NAME:  process_incoming_msg
@@ -242,7 +114,7 @@ sub process_incoming_msg {
     my @msg_l;
     my @out_msg_l;
 
-    &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
+    &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 26);
     
     if ($header =~ /^job_/) {
         @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
@@ -255,13 +127,24 @@ sub process_incoming_msg {
     }
 
     foreach my $out_msg ( @msg_l ) {
-        # substitute in all outgoing msg <source>GOSA</source> of <source>$server_address</source>
-        $out_msg =~ s/<source>GOSA<\/source>/<source>$server_address<\/source>/g;
+        # determine the correct outgoing source address to the corresponding target address
+        $out_msg =~ /<target>(\S*)<\/target>/;
+        my $act_target = $1;
+        $act_target =~ s/GOSA/$main::server_address/;
+        my $act_server_ip = &main::get_local_ip_for_remote_ip(sprintf("%s", $act_target =~ /^([0-9\.]*?):.*$/));
+
+        # Patch the correct outgoing source address
+        if ($out_msg =~ /<source>GOSA<\/source>/ ) {
+            $out_msg =~ s/<source>GOSA<\/source>/<source>$act_server_ip:$main::server_port<\/source>/g;
+        }
+
+        # Add to each outgoing message the current POE session id
         $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
+
+
         if (defined $out_msg){
             push(@out_msg_l, $out_msg);
         }
-
     }
 
     return \@out_msg_l;
@@ -274,16 +157,19 @@ sub process_gosa_msg {
     my @out_msg_l = ('nohandler');
     my $sql_events;
 
+    # strip gosa_ prefix from header, it is not used any more
+    @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
+    $msg =~ s/<header>gosa_/<header>/;
+
     my $header = @{$msg_hash->{'header'}}[0];
     my $target = @{$msg_hash->{'target'}}[0];
-    $header =~ s/gosa_//;
 
     # check local installed events
-    if( exists $event_hash->{$header} ) {
+    if( exists $event2module_hash->{$header} ) {
         # a event exists with the header as name
-        &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
+        &main::daemon_log("$session_id DEBUG: found event '$header' at event-module '".$event2module_hash->{$header}."'", 26);
         no strict 'refs';
-        @out_msg_l = &{$event_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
+        @out_msg_l = &{$event2module_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
 
     # check client registered events
     } else {
@@ -294,14 +180,13 @@ sub process_gosa_msg {
         # set error if no or more than 1 hits are found for sql query
         if ( $l != 1) {
             @out_msg_l = ('knownclienterror');
-        
         # found exact 1 hit in db
         } else {
             my $client_events = $res->{'1'}->{'events'};
 
             # client is registered for this event, deliver this message to client
-            if ($client_events =~ /,$header,/) {
-                $msg =~ s/<header>gosa_/<header>/;
+            if (($client_events =~ /^$header,/) || ($client_events =~ /,$header,/) || ($client_events =~ /,$header$/)) {
+                &main::daemon_log("$session_id DEBUg: client '$target' is registerd for event '$header', forward message to client.", 26);
                 @out_msg_l = ( $msg );
 
             # client is not registered for this event, set error
@@ -318,11 +203,16 @@ sub process_gosa_msg {
         &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
         @out_msg_l = ();
     } elsif ($out_msg_l[0] eq 'knownclienterror') {
-        &main::daemon_log("$session_id ERROR: no event handler found for '$header', check client registraion events!", 1);
-        &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
-        &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
+        if ($header eq "ping") {
+            &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);
+        } else {
+            &main::daemon_log("$session_id ERROR: no general event handler found for '$header', check client registration events!", 1);
+            &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
+            &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
+        }
         @out_msg_l = ();
     } elsif ($out_msg_l[0] eq 'noeventerror') {
+        &main::daemon_log("$session_id ERROR: no general event handler found for '$header', check client registration events!", 1);
         &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1); 
         @out_msg_l = ();
     }
@@ -340,13 +230,13 @@ sub process_job_msg {
     $header =~ s/job_//;
        my $target = @{$msg_hash->{'target'}}[0];
     
-    # if no timestamp is specified, use 19700101000000
+    # If no timestamp is specified, use 19700101000000
     my $timestamp = "19700101000000";
     if( exists $msg_hash->{'timestamp'} ) {
         $timestamp = @{$msg_hash->{'timestamp'}}[0];
     }
 
-    #if no macaddress is specified, raise error 
+    # If no macaddress is specified, raise error 
     my $macaddress;
     if( exists $msg_hash->{'macaddress'} ) {
         $macaddress = @{$msg_hash->{'macaddress'}}[0];
@@ -356,38 +246,63 @@ sub process_job_msg {
         $error ++;
         $out_msg = "<xml>".
             "<header>answer</header>".
-            "<source>$server_address</source>".
+            "<source>$main::server_address</source>".
             "<target>GOSA</target>".
             "<answer1>1</answer1>".
             "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
             "</xml>";
+
+        return ($out_msg);
     }
     
-       # if mac address is already known in ldap, set plain_name to 'cn'
+    # Determine plain_name for host
     my $plain_name;
-       my $ldap_handle = &main::get_ldap_handle($session_id); 
-       if( not defined $ldap_handle ) {
-               &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
-               $plain_name = "none"; 
-               
-       # try to fetch a 'real name'            
-       } else {
-               my $mesg = $ldap_handle->search(
-                                               base => $main::ldap_base,
-                                               scope => 'sub',
-                                               attrs => ['cn'],
-                                               filter => "(macAddress=$macaddress)");
-               if($mesg->code) {
-                       &main::daemon_log($mesg->error, 1);
-                       $plain_name = "none";
-               } else {
-                       my $entry= $mesg->entry(0);
-                       $plain_name = $entry->get_value("cn");
-               }
-       }
+    if ($header eq "opsi_install_client") {   # Opsi installing clients use hostId as plain_name
+        if (not exists $msg_hash->{'hostId'}) {
+            $error++;
+            &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1);
+        } else {
+            $plain_name = $msg_hash->{'hostId'}[0];
+            $header = "trigger_action_reinstall"
+        }
+
+    } else {   # Try to determine plain_name via ldap search
+        my $ldap_handle=&main::get_ldap_handle();
+        my $mesg = $ldap_handle->search(
+                base => $main::ldap_base,
+                scope => 'sub',
+                attrs => ['cn'],
+                filter => "(macAddress=$macaddress)");
+        if($mesg->code || ($mesg->count!=1)) {
+            &main::daemon_log($mesg->error, 1);
+            $plain_name = "none";
+        } else {
+            my $entry= $mesg->entry(0);
+            $plain_name = $entry->get_value("cn");
+        }
+        &main::release_ldap_handle($ldap_handle);
+    }
+       
+    # Check if it is a periodical job
+    my $periodic = 'none';
+    if (exists $msg_hash->{periodic})
+    {
+        $periodic = $msg_hash->{periodic}[0];
+        if ($periodic ne 'none' and not $periodic =~ /[0-9]+_(hours|minutes|days|weeks|months)/)    # Periodic tag is not valid
+        {
+            &main::daemon_log("$session_id ERROR: Message contains invalid periodic-tag '$periodic'.".
+                    " Please use the following pattern for the tag: 'INTEGER_[minutes|hours|days|weeks|months]'".
+                    " Aborted message: $msg", 1);
+            $out_msg = "<xml>".
+                "<header>answer</header><source>$main::server_address</source><target>GOSA</target>".
+                "<answer1>1</answer1><error_string>Message contains invalid periodic-tag '$periodic'</error_string>".
+                "</xml>";
+            return ($out_msg);
+        }
+    }
 
+    # Add job to job queue
     if( $error == 0 ) {
-        # add job to job queue
         my $func_dic = {table=>$main::job_queue_tn, 
             primkey=>['macaddress', 'headertag'],
             timestamp=>$timestamp,
@@ -399,6 +314,9 @@ sub process_job_msg {
             xmlmessage=>$msg,
             macaddress=>$macaddress,
                        plainname=>$plain_name,
+            siserver=>"localhost",
+            modified=>"1",
+            periodic=>$periodic,
         };
         my $res = $main::job_db->add_dbentry($func_dic);
         if (not $res == 0) {
@@ -406,7 +324,7 @@ sub process_job_msg {
         } else {
             &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
         }
-        $out_msg = "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
+        $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
     }
     
     my @out_msg_l = ( $out_msg );