Code

Events should get the right use statement too.
[gosa.git] / gosa-si / modules / GosaPackages.pm
index eea707322b1140f74b22f5d9ca1cd3b0c4d928cc..ffc99840c0ca5496ac9c4dd00dff843449a42b22 100644 (file)
@@ -16,9 +16,9 @@ use MIME::Base64;
 BEGIN{}
 END{}
 
-my ($server_activ, $server_port, $server_passwd, $max_clients, $server_event_dir);
+my ($server_activ, $server_ip, $server_mac_address, $server_port, $server_passwd, $max_clients, $server_event_dir);
 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
-my ($gosa_activ, $gosa_ip, $gosa_port, $gosa_passwd);
+my ($gosa_activ, $gosa_ip, $gosa_mac_address, $gosa_port, $gosa_passwd, $network_interface);
 my ($job_queue_timeout, $job_queue_file_name);
 
 my $gosa_server;
@@ -29,6 +29,7 @@ my %cfg_defaults =
     },
 "server" =>
     {"server_activ" => [\$server_activ, "on"],
+    "server_ip" => [\$server_ip, "0.0.0.0"],
     "server_port" => [\$server_port, "20081"],
     "server_passwd" => [\$server_passwd, ""],
     "max_clients" => [\$max_clients, 100],
@@ -37,12 +38,12 @@ my %cfg_defaults =
 "bus" =>
     {"bus_activ" => [\$bus_activ, "on"],
     "bus_passwd" => [\$bus_passwd, ""],
-    "bus_ip" => [\$bus_ip, ""],
+    "bus_ip" => [\$bus_ip, "0.0.0.0"],
     "bus_port" => [\$bus_port, "20080"],
     },
 "gosa" =>
     {"gosa_activ" => [\$gosa_activ, "on"],
-    "gosa_ip" => [\$gosa_ip, ""],
+    "gosa_ip" => [\$gosa_ip, "0.0.0.0"],
     "gosa_port" => [\$gosa_port, "20082"],
     "gosa_passwd" => [\$gosa_passwd, "none"],
     },
@@ -53,9 +54,8 @@ my %cfg_defaults =
 
 # read configfile and import variables
 &read_configfile();
-
-# detect own ip and mac address
-my ($server_ip, $server_mac_address) = &get_ip_and_mac(); 
+$network_interface= &get_interface_for_ip($server_ip);
+$gosa_mac_address= &get_mac($network_interface);
 
 # complete addresses
 my $server_address = "$server_ip:$server_port";
@@ -76,6 +76,7 @@ if ($gosa_activ eq "on") {
             );
     if (not defined $gosa_server) {
         &main::daemon_log("cannot start tcp server at $gosa_port for communication to gosa: $@", 1);
+        die;
     } else {
         &main::daemon_log("start server for communication to gosa: $gosa_address", 1);
         
@@ -127,32 +128,126 @@ sub read_configfile {
     }
 }
 
+#===  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;
+}
 
 #===  FUNCTION  ================================================================
-#         NAME:  get_ip_and_mac 
-#   PARAMETERS:  nothing
-#      RETURNS:  (ip, mac) 
-#  DESCRIPTION:  executes /sbin/ifconfig and parses the output, the first occurence 
-#                of a inet address is returned as well as the mac address in the line
-#                above the inet address
+#         NAME:  get_interfaces 
+#   PARAMETERS:  none
+#      RETURNS:  (list of interfaces) 
+#  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
 #===============================================================================
-sub get_ip_and_mac {
-    my $ip = "0.0.0.0.0"; # Defualt-IP
-    my $mac = "00:00:00:00:00:00";  # Default-MAC
-    my @ifconfig = qx(/sbin/ifconfig);
-    foreach(@ifconfig) {
-        if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
-            $mac = "$1:$2:$3:$4:$5:$6";
-            next;
+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;
         }
-        if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
-            $ip = "$1.$2.$3.$4";
-            last;
+
+        return @result;
+}
+
+#===  FUNCTION  ================================================================
+#         NAME:  get_mac 
+#   PARAMETERS:  interface name (i.e. eth0)
+#      RETURNS:  (mac address) 
+#  DESCRIPTION:  Uses ioctl to get mac address directly from system.
+#===============================================================================
+sub get_mac {
+        my $ifreq= shift;
+        my $result;
+        if ($ifreq && length($ifreq) > 0) { 
+                if($ifreq eq "all") {
+                        $result = "00:00:00:00:00:00";
+                } else {
+                        my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
+
+                        # A configured MAC Address should always override a guessed value
+                        if ($gosa_mac_address and length($gosa_mac_address) > 0) {
+                                $result= $gosa_mac_address;
+                        }
+
+                        socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
+                                or die "socket: $!";
+
+                        if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
+                                my ($if, $mac)= unpack 'h36 H12', $ifreq;
+
+                                if (length($mac) > 0) {
+                                        $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])$/;
+                                        $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
+                                        $result = $mac;
+                                }
+                        }
+                }
         }
-    }
-    return ($ip, $mac);
+        return $result;
 }
 
+#===  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:  open_socket
@@ -188,10 +283,10 @@ sub open_socket {
 #===============================================================================
 sub process_incoming_msg {
     my ($crypted_msg) = @_ ;
-    if(not defined $crypted_msg) {
+    if( (not(defined($crypted_msg))) || (length($crypted_msg) <= 0)) {
         &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
+        return;
     }
-    &main::daemon_log("GosaPackages: incoming msg: \n$crypted_msg", 7);
 
     $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
     $crypted_msg = $1;
@@ -221,8 +316,7 @@ sub process_incoming_msg {
 
     my $header = @{$msg_hash->{header}}[0];
     
-    &main::daemon_log("recieve '$header' at GosaPackages from $host", 1);
-    &main::daemon_log("$msg", 7);
+    &main::daemon_log("GosaPackages: receive '$header' from $host", 1);
     
     my $out_msg;
     if ($header =~ /^job_/) {
@@ -239,11 +333,14 @@ sub process_incoming_msg {
 
     if ($out_msg =~ /<jobdb_id>(\d*?)<\/jobdb_id>/) {
         my $job_id = $1;
-        my $sql = "UPDATE '$main::job_queue_table_name' SET status='done', result='$out_msg' WHERE id='$job_id'";
+        my $sql = "BEGIN TRANSATION; UPDATE '".$main::job_queue_table_name.
+            "' SET status='done', result='".$out_msg.
+            "' WHERE id='$job_id'; COMMIT;";
         my $res = $main::job_db->exec_statement($sql);
         return;
 
     } else {
+
         my $out_cipher = &create_ciphering($gosa_passwd);
         $out_msg = &encrypt_msg($out_msg, $out_cipher);
         return $out_msg;
@@ -255,7 +352,6 @@ sub process_gosa_msg {
     my ($msg, $header) = @_ ;
     my $out_msg;
     $header =~ s/gosa_//;
-    &main::daemon_log("GosaPackages: got a gosa msg $header", 5);
 
     # decide wether msg is a core function or a event handler
     if ( $header eq 'query_jobdb') { $out_msg = &query_jobdb }
@@ -303,13 +399,13 @@ sub process_job_msg {
 
     my $header = @{$msg_hash->{header}}[0];
     $header =~ s/job_//;
-    &main::daemon_log("GosaPackages: got a job msg $header", 5);
     
     # check wether mac address is already known in known_daemons or known_clients
-    my $target = 'not known until now';
+    my $target = 'none';
 
     # add job to job queue
     my $func_dic = {table=>$main::job_queue_table_name, 
+                    primkey=>'id',
                     timestamp=>@{$msg_hash->{timestamp}}[0],
                     status=>'waiting', 
                     result=>'none',
@@ -334,19 +430,21 @@ sub db_res_2_xml {
     my $xml = "<xml>";
 
     while ( my ($hit, $hash) = each %{ $db_res } ) {
-        $xml .= "<$hit>";
+        $xml .= "\n<answer$hit>";
 
         while ( my ($column_name, $column_value) = each %{$hash} ) {
             $xml .= "<$column_name>";
-            my $xml_content = $column_value;
-            if( $column_name eq "xml" ) {
+            my $xml_content;
+            if( $column_name eq "xmlmessage" ) {
                 $xml_content = &encode_base64($column_value);
+            } else {
+                $xml_content = $column_value;
             }
             $xml .= $xml_content;
             $xml .= "</$column_name>"; 
         }
 
-        $xml .= "</$hit>";
+        $xml .= "</answer$hit>";
     }
 
     $xml .= "</xml>";
@@ -402,7 +500,7 @@ sub delete_jobdb_entry {
     }
 
     # prepare xml answer
-    my $out_xml = "<xml><1>$res</1></xml>";
+    my $out_xml = "<xml><answer1>$res</answer1></xml>";
     return $out_xml;
 
 }
@@ -424,7 +522,7 @@ sub clear_jobdb {
     }
 
     # prepare xml answer
-    my $out_xml = "<xml><1>$res</1></xml>";
+    my $out_xml = "<xml><answer1>$res</answer1></xml>";
     return $out_xml;
 }
 
@@ -441,10 +539,9 @@ sub update_status_jobdb_entry {
     }
 
     if( not exists $msg_hash->{update}[0]->{status} ) {
-        return "<xml><1>1</1></xml>";
+        return "<xml><answer1>1</answer1></xml>";
     }
     $update_hash->{update} = [ { status=>$msg_hash->{update}[0]->{status} } ];
-    #$update_hash->{update} = $msg_hash->{update};
 
     # execute db query
     my $db_res = $main::job_db->update_dbentry($update_hash);
@@ -458,7 +555,7 @@ sub update_status_jobdb_entry {
     }
 
     # prepare xml answer
-    my $out_xml = "<xml><1>$res</1></xml>";
+    my $out_xml = "<xml><answer1>$res</answer1></xml>";
     return $out_xml;
 }
 
@@ -475,7 +572,7 @@ sub update_timestamp_jobdb_entry {
     }
 
     if( not exists $msg_hash->{update}[0]->{timestamp} ) {
-        return "<xml><1>1</1></xml>";
+        return "<xml><answer1>1</answer1></xml>";
     }
 
     $update_hash->{update} = [ { timestamp=>$msg_hash->{update}[0]->{timestamp} } ];
@@ -492,7 +589,7 @@ sub update_timestamp_jobdb_entry {
     }
 
     # prepare xml answer
-    my $out_xml = "<xml><1>$res</1></xml>";
+    my $out_xml = "<xml><answer1>$res</answer1></xml>";
     return $out_xml;
 
 }