From 3f00a5e0ed7883a4e41ab9452ae9b05887e19188 Mon Sep 17 00:00:00 2001 From: janw Date: Fri, 1 Feb 2008 15:25:45 +0000 Subject: [PATCH] Added missing attributes from hardware detection. New Host will get a hardware configuration of another host if the checksum is equal. git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8732 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/gosa-si-client | 76 ++++++++-------- gosa-si/modules/SIPackages.pm | 166 ++++++++++++++++++++++++++++------ 2 files changed, 177 insertions(+), 65 deletions(-) diff --git a/gosa-si/gosa-si-client b/gosa-si/gosa-si-client index 2076b0979..2d9ce6063 100755 --- a/gosa-si/gosa-si-client +++ b/gosa-si/gosa-si-client @@ -58,6 +58,7 @@ $foreground = 0 ; "client" => {"client_port" => [\$client_port, "20083"], "client_ip" => [\$client_ip, "0.0.0.0"], + "client_mac_address" => [\$client_mac_address, "00:00:00:00:00:00"], "ldap" => [\$ldap_enabled, 1], "ldap_config" => [\$ldap_config, "/etc/ldap/ldap.conf"], "pam_config" => [\$pam_config, "/etc/pam_ldap.conf"], @@ -271,38 +272,39 @@ sub get_interfaces { # 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") { + my $ifreq= shift; + my $result; + + if ($ifreq && length($ifreq) > 0) { + if($ifreq eq "all") { if(defined($server_ip)) { $result = &get_local_mac_for_remote_ip($server_ip); + } elsif ($client_mac_address && length($client_mac_address) > 0) { + $result = $client_mac_address; } else { $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 ($client_mac_address and length($client_mac_address) > 0) { - $result= $client_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; + } else { + my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list + + socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip') + or die "socket: $!"; + + # A configured MAC Address should always override a guessed value + if ($client_mac_address and length($client_mac_address) > 0) { + $result = $client_mac_address; + } elsif(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 ================================================================ @@ -1285,6 +1287,8 @@ sub detect_hardware { } my $result= { + macAddress => $client_mac_address, + gotoXMonitor => "", gotoXDriver => "", gotoXMouseType => "", gotoXMouseport => "", @@ -1314,8 +1318,8 @@ sub detect_hardware { my $monitor= `$hwinfo --monitor`; my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m; if(defined($primary_monitor)) { - ($result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3) if - $monitor =~ /$primary_monitor:\s.*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s; + ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if + $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s; } if(length($result->{gotoXHsync}) == 0) { @@ -1369,16 +1373,12 @@ sub detect_hardware { $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false"; - #TODO Ide detection -#$result->{ghIdeDev} = $@ if `` -#opendir(IDE, "/proc/ide"); -#for my $model(grep /ide\d\/hd\w\/model/, readdir(DIR)) { -# print "$model\n"; -#} -#close(IDE); + foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) { + push @{$result->{ghIdeDev}}, $device; + } - while ( `cat /proc/scsi/scsi` =~ /^.*?Vendor:\s(.*?)\s+Model:\s(.*?)\s+.*$/mg ) { - push (@{$result->{ghScsiDev}}, "$1 $2"); + foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) { + push @{$result->{ghScsiDev}}, $device; } &main::daemon_log("Hardware detection done!", 4); diff --git a/gosa-si/modules/SIPackages.pm b/gosa-si/modules/SIPackages.pm index 81ae0671b..1d3213a91 100644 --- a/gosa-si/modules/SIPackages.pm +++ b/gosa-si/modules/SIPackages.pm @@ -791,19 +791,8 @@ sub new_ldap_config { sub process_detected_hardware { my $msg_hash = shift; + my $address = $msg_hash->{source}[0]; - - return; -} -#=== FUNCTION ================================================================ -# NAME: hardware_config -# PARAMETERS: address - string - ip address and port of a host -# RETURNS: -# DESCRIPTION: -#=============================================================================== -sub hardware_config { - my ($address, $gotoHardwareChecksum) = @_ ; - my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'"; my $res = $main::known_clients_db->select_dbentry( $sql_statement ); @@ -820,7 +809,6 @@ sub hardware_config { &main::daemon_log("ERROR: no mac address found for client $address", 1); return; } - # Build LDAP connection my $ldap = Net::LDAP->new($ldap_uri); if( not defined $ldap ) { @@ -837,7 +825,84 @@ sub hardware_config { scope => 'sub', filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))" ); - + + if($mesg->count == 1) { + my $entry= $mesg->entry(0); + $entry->changetype("modify"); + foreach my $attribute ( + "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel", + "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync", + "gotoXDriver", "gotoXVsync", "gotoXMonitor") { + if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) { + if(defined($entry->get_value($attribute))) { + $entry->delete($attribute); + } + &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1); + $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute}); + } + } + foreach my $attribute ( + "gotoModules", "ghScsiDev", "ghIdeDev") { + if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) { + if(defined($entry->get_value($attribute))) { + $entry->delete($attribute); + } + foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) { + $entry->add($attribute => $array_entry); + } + } + + } + if($entry->update($ldap)) { + &main::daemon_log("Added Hardware configuration to LDAP", 4); + } + + } + return; +} +#=== FUNCTION ================================================================ +# NAME: hardware_config +# PARAMETERS: address - string - ip address and port of a host +# RETURNS: +# DESCRIPTION: +#=============================================================================== +sub hardware_config { + my ($address, $gotoHardwareChecksum) = @_ ; + + my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'"; + my $res = $main::known_clients_db->select_dbentry( $sql_statement ); + + # check hit + my $hit_counter = keys %{$res}; + if( not $hit_counter == 1 ) { + &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1); + } + + my $macaddress = $res->{1}->{macaddress}; + my $hostkey = $res->{1}->{hostkey}; + + if (not defined $macaddress) { + &main::daemon_log("ERROR: no mac address found for client $address", 1); + return; + } + + # Build LDAP connection + my $ldap = Net::LDAP->new($ldap_uri); + if( not defined $ldap ) { + &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1); + return; + } + + # Bind to a directory with dn and password + my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password); + + # Perform search + $mesg = $ldap->search( + base => $ldap_base, + scope => 'sub', + filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))" + ); + if($mesg->count() == 0) { &main::daemon_log("Host was not found in LDAP!", 1); return; @@ -846,11 +911,21 @@ sub hardware_config { my $entry= $mesg->entry(0); my $dn= $entry->dn; if(defined($entry->get_value("gotoHardwareChecksum"))) { - return; + if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) { + $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum); + if($entry->update($ldap)) { + &main::daemon_log("Hardware changed! Detection triggered.", 4); + } + } else { + # Nothing to do + return; + } } else { # need to fill it to LDAP $entry->add(gotoHardwareChecksum => $gotoHardwareChecksum); - &main::daemon_log(Dumper($entry->update($ldap)),1); + if($entry->update($ldap)) { + &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4); + } # Look if there another host with this checksum to use the hardware config $mesg = $ldap->search( @@ -858,23 +933,60 @@ sub hardware_config { scope => 'sub', filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))" ); + + if($mesg->count>1) { + my $clone_entry= $mesg->entry(0); + $entry->changetype("modify"); + foreach my $attribute ( + "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel", + "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync", + "gotoXDriver", "gotoXVsync", "gotoXMonitor") { + my $value= $clone_entry->get_value($attribute); + if(defined($value)) { + if(defined($entry->get_value($attribute))) { + $entry->delete($attribute); + } + &main::daemon_log("Adding attribute $attribute with value $value",1); + $entry->add($attribute => $value); + } + } + foreach my $attribute ( + "gotoModules", "ghScsiDev", "ghIdeDev") { + my $array= $clone_entry->get_value($attribute, 'as_ref' => 1); + if(defined($array)) { + if(defined($entry->get_value($attribute))) { + $entry->delete($attribute); + } + foreach my $array_entry (@{$array}) { + $entry->add($attribute => $array_entry); + } + } + + } + if($entry->update($ldap)) { + &main::daemon_log("Added Hardware configuration to LDAP", 4); + } + + } + } - # Assemble data package - my %data = (); + # Assemble data package + my %data = (); - # Need to append GOto settings? - if (defined $goto_admin and defined $goto_secret){ - $data{'goto_admin'}= $goto_admin; - $data{'goto_secret'}= $goto_secret; - } + # Need to append GOto settings? + if (defined $goto_admin and defined $goto_secret){ + $data{'goto_admin'}= $goto_admin; + $data{'goto_secret'}= $goto_secret; + } - # Unbind - $mesg = $ldap->unbind; + # Unbind + $mesg = $ldap->unbind; &main::daemon_log("Send detect_hardware message to $address", 4); - # Send information - return send_msg("detect_hardware", $server_address, $address, \%data); + + # Send information + return send_msg("detect_hardware", $server_address, $address, \%data); } -- 2.30.2