Advertisement
Guest User

Ruperto

a guest
Jan 12th, 2009
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.66 KB | None | 0 0
  1. #!/usr/bin/perl                                                            
  2.  
  3. use Text::Shellwords;
  4. use Getopt::Long;    
  5. use Net::LDAP;      
  6.  
  7. my %opts = ();
  8.  
  9. $|=1;
  10.  
  11. GetOptions(
  12.         "H:s"=>\$opts{H},
  13.         "T:i"=>\$opts{T},
  14.         "debug"=>\$opts{debug},
  15.         "D:s"=>\$opts{D},      
  16.         "B=s"=>\$opts{B},      
  17.         "w:s"=>\$opts{w},      
  18.         "M:s"=>\$opts{M}      
  19. );                            
  20.  
  21. if (!$opts{T}){
  22.     $opts{T} = 30*60;
  23. }                    
  24.  
  25. my $timeOut = $opts{T};
  26.  
  27. if (!$opts{H}) {
  28.    $opts{H} = '127.0.0.1';
  29. }                        
  30.  
  31. if (!$opts{M}){
  32.     $opts{M} = $opts{H};
  33. }                      
  34.  
  35. if ($opts{debug}){
  36.         open (DEBUG, ">&STDERR");
  37.         print DEBUG "DEBUG: Start\n";
  38.         print DEBUG "DEBUG: timeOut: [$timeOut]\n";
  39. }                                                  
  40.  
  41. my $ldap;
  42. $ldap = Net::LDAP->new( $opts{H} ) or die "$@";
  43.  
  44. print DEBUG "DEBUG: LDAP Connected " . $opts{H} . "\n"   if ($opts{debug});
  45. my $mesg;                                                                  
  46.  
  47. if (defined($opts{D})){
  48.   $mesg = $ldap->bind( $opts{D},
  49.                       password => $opts{w}
  50.                     );                    
  51. }                                        
  52. else{                                    
  53.    $mesg = $ldap->bind;                  
  54. }                                        
  55.  
  56. while (<>) {
  57.  
  58.     ($ip,$name) = &shellwords($_);
  59.     print DEBUG "DEBUG: ip: [$ip] name: [$name]\n" if $opts{debug};
  60.     if (&valid($ip,$name)) {                                      
  61.         print "OK\n";                                              
  62.     } else {                                                      
  63.         print "ERR\n";                                            
  64.     }                                                              
  65. }                                                                  
  66.  
  67. $mesg = $ldap->unbind;
  68. $ldap->disconnect();  
  69. close DEBUG;          
  70.  
  71. sub valid {
  72.     my $ip = shift;
  73.     my $name = shift;
  74.  
  75.     $mesg = $ldap->search(base => $opts{B},
  76.                                 scope => "sub",
  77.                                 filter => "cn=" . $name,
  78.                                 attrs => ['cn','ipHostNumber','l']);
  79.  
  80.     my $found = 0;
  81.     my $ee;      
  82.     foreach my $entry ($mesg->entries) {
  83.             my $e = $entry->get_value('cn');
  84.             if ($opts{debug}){              
  85.                     #$entry->dump if $opts{debug};
  86.                     print DEBUG "DEBUG: [$e]\n";  
  87.             }                                    
  88.             if (lc($e) eq lc($name)){            
  89.               $found |= 1;                        
  90.               $ee = $entry;                      
  91.               print DEBUG "DEBUG: Found!\n" if $opts{debug};
  92.             }                                              
  93.     }                                                      
  94.  
  95.     my $uip; my $uname; my $udate;
  96.  
  97.     if ($found){
  98.         $udate = $ee->get_value('l');
  99.         $uip  = $ee->get_value('ipHostNumber');
  100.         $uname = $ee->get_value('cn');        
  101.  
  102.         print DEBUG "udate: [$udate]\n" if $opts{debug};
  103.         print DEBUG "uip: [$uip]\n" if $opts{debug};    
  104.         print DEBUG "uname: [$uname]\n" if $opts{debug};
  105.  
  106.         print DEBUG time() . "- $udate = " . (time() - $udate) . "\n" if $opts{debug};
  107.         if (((time() - $udate)  <= $timeOut) and  ($ip ne $uip)){                    
  108.             return 0;                                                                
  109.         }                                                                            
  110.  
  111.          # Actualiza
  112.          &update ($name,$ip,time());
  113.          return 1;                  
  114.     }                              
  115.  
  116.     return 0;
  117. }            
  118.  
  119.  
  120. sub update {
  121.   my $n = shift;
  122.   my $i = shift;
  123.   my $d = shift;
  124.   my $mesg2;
  125.  
  126.   my $ldap2;
  127.   $ldap2 = Net::LDAP->new( $opts{M} ) or return 0;
  128.   print DEBUG "Connected to Master " . $opts{M} . "\n" if $opts{debug};
  129.   if ($opts{D}){
  130.     $mesg2 = $ldap2->bind( $opts{D},
  131.                         password => $opts{w}
  132.                       );
  133.   }
  134.   else{
  135.     $mesg2 = $ldap2->bind;
  136.   }
  137.   print DEBUG "Binded\n" if $opts{debug};
  138.   $mesg2 = $ldap2->modify ("cn=$n," . $opts{B},
  139.                             changes => [
  140.                                         replace => [ 'ipHostNumber' => $i,
  141.                                                     'l' => $d]
  142.                                       ]
  143.                           );
  144.   print DEBUG "Modified ipHostNumber => $i,  l => $d\n" if $opts{debug};
  145.   $mesg2 = $ldap2->unbind;
  146.   $ldap2->disconnect();
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement