X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=gosa-si%2Fmodules%2FArpHandler.pm;h=210e95654f4e0231a4f6888c3a3f46d351925232;hb=850ef70d7d016480405e610bd698a4b0ffc8d6c7;hp=4fa4d14a3fc777e0ac6e44f3fc29c5b5dc48a538;hpb=7cc3d107061f1d30ca8f882024f6201b072b72d0;p=gosa.git diff --git a/gosa-si/modules/ArpHandler.pm b/gosa-si/modules/ArpHandler.pm index 4fa4d14a3..210e95654 100644 --- a/gosa-si/modules/ArpHandler.pm +++ b/gosa-si/modules/ArpHandler.pm @@ -14,6 +14,7 @@ use Net::LDAP::Entry; use Net::DNS; use Switch; use Data::Dumper; +use Socket; # Don't start if some of the modules are missing my $start_service=1; @@ -33,32 +34,23 @@ BEGIN{ END{} my ($timeout, $mailto, $mailfrom, $user, $group); -my ($arp_activ, $arp_interface, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password); +my ($arp_enabled, $arp_interface, $arp_update, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password); my $hosts_database={}; -my $resolver=Net::DNS::Resolver->new; my $ldap; -if($lookup_vendor) { - eval("Net::MAC::Vendor::load_cache('file:///usr/lib/gosa-si/modules/oui.txt')"); - if($@) { - &main::daemon_log("Loading OUI cache file failed! MAC Vendor lookup disabled", 1); - $lookup_vendor=0; - } else { - &main::daemon_log("Loading OUI cache file suceeded!", 6); - } -} my %cfg_defaults = ( - "arp" => { - "arp-enabled" => [\$arp_activ, "true"], - "arp-interface" => [\$arp_interface, "all"], - }, - "server" => { - "ldap-uri" => [\$ldap_uri, ""], - "ldap-base" => [\$ldap_base, ""], - "ldap-admin_dn" => [\$ldap_admin_dn, ""], - "ldap-admin_password" => [\$ldap_admin_password, ""], - }, + "ArpHandler" => { + "enabled" => [\$arp_enabled, "true"], + "interface" => [\$arp_interface, "all"], + "update-entries" => [\$arp_update, "false"], + }, + "server" => { + "ldap-uri" => [\$ldap_uri, ""], + "ldap-base" => [\$ldap_base, ""], + "ldap-admin-dn" => [\$ldap_admin_dn, ""], + "ldap-admin-password" => [\$ldap_admin_password, ""], + }, ); #=== FUNCTION ================================================================ @@ -67,9 +59,9 @@ my %cfg_defaults = # RETURNS: nothing # DESCRIPTION: read cfg_file and set variables #=============================================================================== -sub read_configfile { +sub local_read_configfile { my $cfg; - if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) { + if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) { if( -r $main::cfg_file ) { $cfg = Config::IniFiles->new( -file => $main::cfg_file ); } else { @@ -87,18 +79,19 @@ sub read_configfile { } sub get_module_info { - my @info = (undef, - undef, - ); + my @info = (undef, undef); - &read_configfile(); + &local_read_configfile(); # Don't start if some of the modules are missing - if(($arp_activ eq 'true') && $start_service) { - $ldap = Net::LDAP->new($ldap_uri); - if (!$ldap) { - &main::daemon_log("Could not connect to LDAP Server at $ldap_uri!\n$@", 1); - } else { - $ldap->bind($ldap_admin_dn, password => $ldap_admin_password); + if(($arp_enabled eq 'true') && $start_service) { + if($lookup_vendor) { + eval("Net::MAC::Vendor::load_cache('file:///usr/lib/gosa-si/modules/oui.txt')"); + if($@) { + &main::daemon_log("Loading OUI cache file failed! MAC Vendor lookup disabled", 1); + $lookup_vendor=0; + } else { + &main::daemon_log("Loading OUI cache file suceeded!", 6); + } } # When interface is not configured (or 'all'), start arpwatch on all possible interfaces @@ -119,8 +112,6 @@ sub get_module_info { &start(@_,$device); }, _stop => sub { - $ldap->unbind if (defined($ldap)); - $ldap->disconnect if (defined($ldap)); $_[KERNEL]->post( sprintf("arp_watch_$device") => 'shutdown' ) }, got_packet => \&got_packet, @@ -137,8 +128,6 @@ sub get_module_info { &start(@_,$device); }, _stop => sub { - $ldap->unbind if (defined($ldap)); - $ldap->disconnect if (defined($ldap)); $_[KERNEL]->post( sprintf("arp_watch_$device") => 'shutdown' ) }, got_packet => \&got_packet, @@ -179,11 +168,11 @@ sub got_packet { my $capture_device = sprintf "%s", $kernel->alias_list($sender) =~ /^arp_watch_(.*)$/; - if(!exists($hosts_database->{$packet->{source_haddr}})) { - my $dnsresult= $resolver->search($packet->{source_ipaddr}); - my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$packet->{source_ipaddr}; + my $ldap_handle = &main::get_ldap_handle(); + if(!exists($hosts_database->{$packet->{source_haddr}})) { + my $dnsname= gethostbyaddr(inet_aton($packet->{source_ipaddr}), AF_INET) || $packet->{source_ipaddr}; my $ldap_result=&get_host_from_ldap($packet->{source_haddr}); - if(exists($ldap_result->{dn})) { + if(exists($ldap_result->{dn}) and $arp_update eq "true") { $hosts_database->{$packet->{source_haddr}}=$ldap_result; $hosts_database->{$packet->{source_haddr}}->{dnsname}= $dnsname; if(!exists($ldap_result->{ipHostNumber})) { @@ -206,13 +195,13 @@ sub got_packet { cn => (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\./), macVendor => (($lookup_vendor) ? &get_vendor_for_mac($packet->{source_haddr}) : "Unknown Vendor"), }; - &main::daemon_log("Host was not found in LDAP (".($hosts_database->{$packet->{source_haddr}}->{dnsname}).")",6); + &main::daemon_log("A DEBUG: Host was not found in LDAP (".($hosts_database->{$packet->{source_haddr}}->{dnsname}).")",522); &main::daemon_log( - "New Host ".($hosts_database->{$packet->{source_haddr}}->{dnsname}). + "A INFO: New Host ".($hosts_database->{$packet->{source_haddr}}->{dnsname}). ": ".$hosts_database->{$packet->{source_haddr}}->{ipHostNumber}. - "/".$hosts_database->{$packet->{source_haddr}}->{macAddress},4); + "/".$hosts_database->{$packet->{source_haddr}}->{macAddress},5); &add_ldap_entry( - $ldap, + $ldap_handle, $ldap_base, $hosts_database->{$packet->{source_haddr}}->{macAddress}, 'new-system', @@ -222,14 +211,14 @@ sub got_packet { } $hosts_database->{$packet->{source_haddr}}->{device}= $capture_device; } else { - if(!($hosts_database->{$packet->{source_haddr}}->{ipHostNumber} eq $packet->{source_ipaddr})) { + if(($arp_update eq "true") and !($hosts_database->{$packet->{source_haddr}}->{ipHostNumber} eq $packet->{source_ipaddr})) { &main::daemon_log( - "IP Address change of MAC ".$packet->{source_haddr}. + "A INFO: IP Address change of MAC ".$packet->{source_haddr}. ": ".$hosts_database->{$packet->{source_haddr}}->{ipHostNumber}. - "->".$packet->{source_ipaddr}, 4); + "->".$packet->{source_ipaddr}, 5); $hosts_database->{$packet->{source_haddr}}->{ipHostNumber}= $packet->{source_ipaddr}; &change_ldap_entry( - $ldap, + $ldap_handle, $ldap_base, $hosts_database->{$packet->{source_haddr}}->{macAddress}, 'ip-changed', @@ -245,68 +234,41 @@ sub get_host_from_ldap { my $mac=shift; my $result={}; - my $ldap_result= &search_ldap_entry( - $ldap, - $ldap_base, - "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))" - ); - - if(defined($ldap_result) && $ldap_result->count==1) { - if(exists($ldap_result->{entries}[0]) && - exists($ldap_result->{entries}[0]->{asn}->{objectName}) && - exists($ldap_result->{entries}[0]->{asn}->{attributes})) { - - for my $attribute(@{$ldap_result->{entries}[0]->{asn}->{attributes}}) { - if($attribute->{type} eq 'cn') { - $result->{cn} = $attribute->{vals}[0]; - } - if($attribute->{type} eq 'macAddress') { - $result->{macAddress} = $attribute->{vals}[0]; - } - if($attribute->{type} eq 'dhcpHWAddress') { - $result->{dhcpHWAddress} = $attribute->{vals}[0]; - } - if($attribute->{type} eq 'ipHostNumber') { - $result->{ipHostNumber} = $attribute->{vals}[0]; + my $ldap_handle = &main::get_ldap_handle(); + if(defined($ldap_handle)) { + my $ldap_result= &search_ldap_entry( + $ldap_handle, + $ldap_base, + "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))" + ); + + if(defined($ldap_result) && $ldap_result->count==1) { + if(exists($ldap_result->{entries}[0]) && + exists($ldap_result->{entries}[0]->{asn}->{objectName}) && + exists($ldap_result->{entries}[0]->{asn}->{attributes})) { + + for my $attribute(@{$ldap_result->{entries}[0]->{asn}->{attributes}}) { + if($attribute->{type} eq 'cn') { + $result->{cn} = $attribute->{vals}[0]; + } + if($attribute->{type} eq 'macAddress') { + $result->{macAddress} = $attribute->{vals}[0]; + } + if($attribute->{type} eq 'dhcpHWAddress') { + $result->{dhcpHWAddress} = $attribute->{vals}[0]; + } + if($attribute->{type} eq 'ipHostNumber') { + $result->{ipHostNumber} = $attribute->{vals}[0]; + } } } + $result->{dn} = $ldap_result->{entries}[0]->{asn}->{objectName}; } - $result->{dn} = $ldap_result->{entries}[0]->{asn}->{objectName}; } return $result; } -#=== 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 = ; - - 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 @@ -349,7 +311,7 @@ sub get_vendor_for_mac { if(length($vendor) > 0) { $result= @{$vendor}[0]; } - &main::daemon_log("Looking up Vendor for MAC ".$mac.": $result", 4); + &main::daemon_log("A INFO: Looking up Vendor for MAC ".$mac.": $result", 5); } return $result; @@ -366,41 +328,45 @@ sub get_vendor_for_mac { # SEE ALSO: n/a/bin #=============================================================================== sub add_ldap_entry { - my ($ldap_tree, $ldap_base, $mac, $gotoSysStatus, $ip, $interface, $desc) = @_; - my $dn = "cn=".$hosts_database->{$mac}->{cn}.",ou=incoming,$ldap_base"; - my $s_res = &search_ldap_entry($ldap_tree, $ldap_base, "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))"); - my $c_res = (defined($s_res))?$s_res->count:0; - if($c_res == 1) { - &main::daemon_log("WARNING: macAddress $mac already in LDAP", 1); - return; - } elsif($c_res > 0) { - &main::daemon_log("ERROR: macAddress $mac exists $c_res times in LDAP", 1); - return; - } - - # create LDAP entry - my $entry = Net::LDAP::Entry->new( $dn ); - $entry->dn($dn); - $entry->add("objectClass" => "goHard"); - $entry->add("cn" => $hosts_database->{$mac}->{cn}); - $entry->add("macAddress" => $mac); - if(defined $gotoSysStatus) {$entry->add("gotoSysStatus" => $gotoSysStatus)} - if(defined $ip) {$entry->add("ipHostNumber" => $ip) } - #if(defined $interface) { } - if(defined $desc) {$entry->add("description" => $desc) } - - # submit entry to LDAP - my $result = $entry->update ($ldap_tree); - - # for $result->code constants please look at Net::LDAP::Constant - if($result->code == 68) { # entry already exists - &main::daemon_log("WARNING: $dn ".$result->error, 3); - } elsif($result->code == 0) { # everything went fine - &main::daemon_log("add entry $dn to ldap", 1); - } else { # if any other error occur - &main::daemon_log("ERROR: $dn, ".$result->code.", ".$result->error, 1); - } - return; + my ($ldap_tree, $ldap_base, $mac, $gotoSysStatus, $ip, $interface, $desc) = @_; + if(defined($ldap_tree)) { + my $dn = "cn=".$hosts_database->{$mac}->{cn}.",ou=incoming,$ldap_base"; + my $s_res = &search_ldap_entry($ldap_tree, $ldap_base, "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))"); + my $c_res = (defined($s_res))?$s_res->count:0; + if($c_res == 1) { + &main::daemon_log("A WARNING: macAddress $mac already in LDAP", 3); + return; + } elsif($c_res > 0) { + &main::daemon_log("A ERROR: macAddress $mac exists $c_res times in LDAP", 1); + return; + } + + # create LDAP entry + my $entry = Net::LDAP::Entry->new( $dn ); + $entry->dn($dn); + $entry->add("objectClass" => "GOhard"); + $entry->add("cn" => $hosts_database->{$mac}->{cn}); + $entry->add("macAddress" => $mac); + if(defined $gotoSysStatus) {$entry->add("gotoSysStatus" => $gotoSysStatus)} + if(defined $ip) {$entry->add("ipHostNumber" => $ip) } + #if(defined $interface) { } + if(defined $desc) {$entry->add("description" => $desc) } + + # submit entry to LDAP + my $result = $entry->update ($ldap_tree); + + # for $result->code constants please look at Net::LDAP::Constant + if($result->code == 68) { # entry already exists + &main::daemon_log("A WARNING: $dn ".$result->error, 3); + } elsif($result->code == 0) { # everything went fine + &main::daemon_log("A INFO: Add entry $dn to ldap", 5); + } else { # if any other error occur + &main::daemon_log("A ERROR: $dn, ".$result->code.", ".$result->error, 1); + } + } else { + &main::daemon_log("A INFO: Not adding new Entry: LDAP disabled", 5); + } + return; } @@ -415,39 +381,43 @@ sub add_ldap_entry { # SEE ALSO: n/a #=============================================================================== sub change_ldap_entry { - my ($ldap_tree, $ldap_base, $mac, $gotoSysStatus, $ip) = @_; - - # check if ldap_entry exists or not - my $s_res = &search_ldap_entry($ldap_tree, $ldap_base, "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))"); - my $c_res = (defined $s_res)?$s_res->count:0; - if($c_res == 0) { - &main::daemon_log("WARNING: macAddress $mac not in LDAP", 1); - return; - } elsif($c_res > 1) { - &main::daemon_log("ERROR: macAddress $mac exists $c_res times in LDAP", 1); - return; - } - - my $s_res_entry = $s_res->pop_entry(); - my $dn = $s_res_entry->dn(); - my $replace = { - 'gotoSysStatus' => $gotoSysStatus, - }; - if (defined($ip)) { - $replace->{'ipHostNumber'} = $ip; + my ($ldap_tree, $ldap_base, $mac, $gotoSysStatus, $ip) = @_; + + if(defined($ldap_tree)) { + # check if ldap_entry exists or not + my $s_res = &search_ldap_entry($ldap_tree, $ldap_base, "(|(macAddress=$mac)(dhcpHWAddress=ethernet $mac))"); + my $c_res = (defined $s_res)?$s_res->count:0; + if($c_res == 0) { + &main::daemon_log("WARNING: macAddress $mac not in LDAP", 1); + return; + } elsif($c_res > 1) { + &main::daemon_log("ERROR: macAddress $mac exists $c_res times in LDAP", 1); + return; + } + + my $s_res_entry = $s_res->pop_entry(); + my $dn = $s_res_entry->dn(); + my $replace = { + 'gotoSysStatus' => $gotoSysStatus, + }; + if (defined($ip)) { + $replace->{'ipHostNumber'} = $ip; + } + my $result = $ldap_tree->modify( $dn, replace => $replace ); + + # for $result->code constants please look at Net::LDAP::Constant + if($result->code == 32) { # entry doesnt exists + &add_ldap_entry($mac, $gotoSysStatus); + } elsif($result->code == 0) { # everything went fine + &main::daemon_log("entry $dn changed successful", 1); + } else { # if any other error occur + &main::daemon_log("ERROR: $dn, ".$result->code.", ".$result->error, 1); + } + } else { + &main::daemon_log("Not changing Entry: LDAP disabled", 6); } - my $result = $ldap->modify( $dn, replace => $replace ); - - # for $result->code constants please look at Net::LDAP::Constant - if($result->code == 32) { # entry doesnt exists - &add_ldap_entry($mac, $gotoSysStatus); - } elsif($result->code == 0) { # everything went fine - &main::daemon_log("entry $dn changed successful", 1); - } else { # if any other error occur - &main::daemon_log("ERROR: $dn, ".$result->code.", ".$result->error, 1); - } - - return; + + return; } #=== FUNCTION ================================================================ @@ -470,82 +440,9 @@ sub search_ldap_entry { base => $sub_tree, filter => $search_string, ) or &main::daemon_log("cannot perform search at ldap: $@", 1); - #if(defined $msg) { - # print $sub_tree."\t".$search_string."\t"; - # print $msg->count."\n"; - # foreach my $entry ($msg->entries) { $entry->dump; }; - #} } return $msg; } -# $ldap = Net::LDAP->new( "localhost" ) or die "$@"; -# $ldap->bind($bind_phrase, -# password => $password, -# ) ; -# -# switch($arp_sig) { -# case 0 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "ip-changed", -# )} -# case 1 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "mac-not-whitelisted", -# )} -# case 2 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "mac-in-blacklist", -# )} -# case 3 {&add_ldap_entry($ldap, $ldap_base, -# $mac, "new-mac-address", $ip, -# $interface, $desc, -# )} -# case 4 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "unauthorized-arp-request", -# )} -# case 5 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "abusive-number-of-arp-requests", -# )} -# case 6 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "ether-and-arp-mac-differs", -# )} -# case 7 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "flood-detected", -# )} -# case 8 {&add_ldap_entry($ldap, $ldap_base, -# $mac, $ip, "new-system", -# )} -# case 9 {&change_ldap_entry($ldap, $ldap_base, -# $mac, "mac-changed", -# )} -# } -# -# - # ldap search -# my $base_phrase = "dc=gonicus,dc=de"; -# my $filter_phrase = "cn=keinesorge"; -# my $attrs_phrase = "cn macAdress"; -# my $msg_search = $ldap->search( base => $base_phrase, -# filter => $filter_phrase, -# attrs => $attrs_phrase, -# ); -# $msg_search->code && die $msg_search->error; -# -# my @entries = $msg_search->entries; -# my $max = $msg_search->count; -# print "anzahl der entries: $max\n"; -# my $i; -# for ( $i = 0 ; $i < $max ; $i++ ) { -# my $entry = $msg_search->entry ( $i ); -# foreach my $attr ( $entry->attributes ) { -# if( not $attr eq "cn") { -# next; -# } -# print join( "\n ", $attr, $entry->get_value( $attr ) ), "\n\n"; -# } -# } - # - # # ldap add - # - # - # $ldap->unbind; - # exit; +# vim:ts=4:shiftwidth:expandtab 1;