From: rettenbe Date: Tue, 2 Sep 2008 11:34:37 +0000 (+0000) Subject: update: mac address is shared among si-servers X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=65d9ec1db8caf26dc31bcf234c2395a40089c056;p=gosa.git update: mac address is shared among si-servers git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@12342 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index 7b7a61d22..0155f08b8 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -150,7 +150,7 @@ my @job_queue_col_names = ("id INTEGER PRIMARY KEY", our $known_server_db; our $known_server_tn = "known_server"; my $known_server_file_name; -my @known_server_col_names = ("hostname", "status", "hostkey", "loaded_modules", "timestamp"); +my @known_server_col_names = ("hostname", "macaddress", "status", "hostkey", "loaded_modules", "timestamp"); # holds all registrated clients our $known_clients_db; @@ -833,46 +833,46 @@ sub open_socket { } -sub get_local_ip_for_remote_ip { - my $remote_ip= shift; - my $result="0.0.0.0"; - - if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) { - if($remote_ip eq "127.0.0.1") { - $result = "127.0.0.1"; - } else { - my $PROC_NET_ROUTE= ('/proc/net/route'); - - open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE") - or die "Could not open $PROC_NET_ROUTE"; - - my @ifs = ; - - close(PROC_NET_ROUTE); - - # Eat header line - shift @ifs; - chomp @ifs; - foreach my $line(@ifs) { - my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line); - my $destination; - my $mask; - my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination); - $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d)); - ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask); - $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d)); - if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) { - # destination matches route, save mac and exit - $result= &get_ip($Iface); - last; - } - } - } - } else { - daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1); - } - return $result; -} +#sub get_local_ip_for_remote_ip { +# my $remote_ip= shift; +# my $result="0.0.0.0"; +# +# if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) { +# if($remote_ip eq "127.0.0.1") { +# $result = "127.0.0.1"; +# } else { +# my $PROC_NET_ROUTE= ('/proc/net/route'); +# +# open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE") +# or die "Could not open $PROC_NET_ROUTE"; +# +# my @ifs = ; +# +# close(PROC_NET_ROUTE); +# +# # Eat header line +# shift @ifs; +# chomp @ifs; +# foreach my $line(@ifs) { +# my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line); +# my $destination; +# my $mask; +# my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination); +# $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d)); +# ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask); +# $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d)); +# if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) { +# # destination matches route, save mac and exit +# $result= &get_ip($Iface); +# last; +# } +# } +# } +# } else { +# daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1); +# } +# return $result; +#} sub send_msg_to_target { @@ -2983,6 +2983,13 @@ sub register_at_foreign_servers { } } map(&add_content2xml_hash($myhash, "loaded_modules", $_), keys(%$loaded_modules)); + + # add macaddress to registration message + my ($host_ip, $host_port) = split(/:/, $hostname); + my $local_ip = &get_local_ip_for_remote_ip($host_ip); + my $network_interface= &get_interface_for_ip($local_ip); + my $host_mac = &get_mac_for_interface($network_interface); + &add_content2xml_hash($myhash, 'macaddress', $host_mac); # build registration message and send it my $foreign_server_msg = &create_xml_string($myhash); @@ -3158,9 +3165,10 @@ foreach my $foreign_server (@foreign_server_list) { my $res = $known_server_db->add_dbentry( {table=>$known_server_tn, primkey=>['hostname'], hostname=>$foreign_server, + macaddress=>"", status=>'not_jet_registered', hostkey=>"none", - loaded_modules => "", + loaded_modules => "none", timestamp=>$act_timestamp, } ); } diff --git a/gosa-si/modules/GosaSupportDaemon.pm b/gosa-si/modules/GosaSupportDaemon.pm index f90c149cc..f71d8df84 100644 --- a/gosa-si/modules/GosaSupportDaemon.pm +++ b/gosa-si/modules/GosaSupportDaemon.pm @@ -26,6 +26,8 @@ my @functions = ( "get_ip", "get_interface_for_ip", "get_interfaces", + "get_mac_for_interface", + "get_local_ip_for_remote_ip", "is_local", "run_as", "inform_all_other_si_server", @@ -629,6 +631,79 @@ sub get_interfaces { return @result; } +sub get_local_ip_for_remote_ip { + my $remote_ip= shift; + my $result="0.0.0.0"; + + if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) { + if($remote_ip eq "127.0.0.1") { + $result = "127.0.0.1"; + } else { + my $PROC_NET_ROUTE= ('/proc/net/route'); + + open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE") + or die "Could not open $PROC_NET_ROUTE"; + + my @ifs = ; + + close(PROC_NET_ROUTE); + + # Eat header line + shift @ifs; + chomp @ifs; + foreach my $line(@ifs) { + my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line); + my $destination; + my $mask; + my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination); + $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d)); + ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask); + $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d)); + if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) { + # destination matches route, save mac and exit + $result= &get_ip($Iface); + last; + } + } + } + } else { + daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1); + } + return $result; +} + + +sub get_mac_for_interface { + 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 ($main::server_mac_address and length($main::server_mac_address) > 0) { + $result= $main::server_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 $result; +} + #=== FUNCTION ================================================================ # NAME: is_local diff --git a/gosa-si/server/events/gosaTriggered.pm b/gosa-si/server/events/gosaTriggered.pm index d4d7ddd9f..a524713b5 100644 --- a/gosa-si/server/events/gosaTriggered.pm +++ b/gosa-si/server/events/gosaTriggered.pm @@ -43,6 +43,7 @@ my @events = ( # "import_dak_key", # "remove_dak_key", # "get_dak_queue", + "get_opsi_hosts", ); @EXPORT = @events; @@ -1115,5 +1116,10 @@ sub trigger_activate_new { #} +sub get_opsi_hosts { + +} + + # vim:ts=4:shiftwidth:expandtab 1; diff --git a/gosa-si/server/events/server_server_com.pm b/gosa-si/server/events/server_server_com.pm index b14c90029..171a4eaed 100644 --- a/gosa-si/server/events/server_server_com.pm +++ b/gosa-si/server/events/server_server_com.pm @@ -77,6 +77,7 @@ sub new_server { my $source = @{$msg_hash->{'source'}}[0]; my $target = @{$msg_hash->{'target'}}[0]; my $key = @{$msg_hash->{'key'}}[0]; + my $mac = exists $msg_hash->{'macaddress'} ? @{$msg_hash->{'macaddress'}}[0] : "" ; my @clients = exists $msg_hash->{'client'} ? @{$msg_hash->{'client'}} : qw(); my @loaded_modules = exists $msg_hash->{'loaded_modules'} ? @{$msg_hash->{'loaded_modules'}} : qw(); @@ -89,6 +90,7 @@ sub new_server { my $func_dic = {table=>$main::known_server_tn, primkey=>['hostname'], hostname => $source, + macaddress => $mac, status => "new_server", hostkey => $key, loaded_modules => join(',', @loaded_modules), @@ -147,6 +149,13 @@ sub new_server { } map(&add_content2xml_hash($myhash, "loaded_modules", $_), keys(%$loaded_modules)); + # add macaddress to registration message + my ($host_ip, $host_port) = split(/:/, $source); + my $local_ip = &get_local_ip_for_remote_ip($host_ip); + my $network_interface= &get_interface_for_ip($local_ip); + my $host_mac = &get_mac_for_interface($network_interface); + &add_content2xml_hash($myhash, 'macaddress', $host_mac); + # build registration message and send it my $out_msg = &create_xml_string($myhash); my $error = &main::send_msg_to_target($out_msg, $source, $main::ServerPackages_key, 'confirm_new_server', $session_id); @@ -160,10 +169,13 @@ sub confirm_new_server { my $header = @{$msg_hash->{'header'}}[0]; my $source = @{$msg_hash->{'source'}}[0]; my $key = @{$msg_hash->{'key'}}[0]; + my $mac = exists $msg_hash->{'macaddress'} ? @{$msg_hash->{'macaddress'}}[0] : "" ; my @clients = exists $msg_hash->{'client'} ? @{$msg_hash->{'client'}} : qw(); my @loaded_modules = exists $msg_hash->{'loaded_modules'} ? @{$msg_hash->{'loaded_modules'}} : qw(); - my $sql = "UPDATE $main::known_server_tn SET status='$header', hostkey='$key', loaded_modules='".join(",",@loaded_modules)."' WHERE hostname='$source'"; + my $sql = "UPDATE $main::known_server_tn". + " SET status='$header', hostkey='$key', loaded_modules='".join(",",@loaded_modules)."', macaddress='$mac'". + " WHERE hostname='$source'"; my $res = $main::known_server_db->update_dbentry($sql); # add clients of foreign server to known_foreign_clients_db