Andy Baugh 3 年 前
コミット
9fad6a8c49
3 ファイル変更65 行追加7 行削除
  1. 7 0
      README.md
  2. 25 7
      ldapscanner
  3. 33 0
      ldapscanner.cfg.dist

+ 7 - 0
README.md

@@ -0,0 +1,7 @@
+ldapscanner
+===========
+
+Reads a config file in the same dir (ldapscanner.cfg)
+and then scans for changes since last time it was ran.
+
+Base your config on ldapscanner.cfg.dist

+ 25 - 7
ldapscanner

@@ -9,23 +9,41 @@ use Net::LDAP     ();
 
 my $cfg     = Config::Tiny->read('ldapscanner.cfg');
 my @servers = split( ',', $cfg->{'server'}{'hosts'} );
+my $proto   = $cfg->{'server'}{'ldaps'} ? 'ldaps' : 'ldap';
 my $port    = $cfg->{'server'}{'port'};
 
 foreach my $server (@servers) {
-    log( "debug", "Querying $server" );
-    my $conn = Net::LDAP->new("ldaps://$server:$port");
-    my $result = $conn->bind;
+    _log( "debug", "Connecting to $proto://$server:$port..." );
+    my $conn = Net::LDAP->new(
+        "$proto://$server:$port",
+        'debug'   => $cfg->{'prefs'}{'loglevel'} eq 'debug',
+        'onerror' => 'die',
+        'async'   => 0,
+    ) or die $@;
+    _log( "debug", "Binding anonymously..." );
+    my $result = $conn->bind( 'anonymous' => 1 );
     $result->code and die $result->error;
+
+    # Login if you wanna do that
+    if( scalar( grep { $cfg->{'creds'}{$_} } qw{id_field id_value pass} ) eq 3 ) {
+        #$conn->unbind;
+        _log( "debug", "Now binding as $cfg->{'creds'}{'id_value'}..." );
+        #$bind_args = "dn=$result->dn";
+        #$result = $conn->bind($bind_args);
+    }
+
     $result = $conn->search(
-        base   => "ou=people,dc=cpanel,dc=net",
-        filter => "(&(objectClass=inetOrgPerson))",
+        base   => $cfg->{'search'}{'base'},
+        filter => $cfg->{'search'}{'filter'},
     );
     $result->code and die $result->error;
-    my $result;
+    foreach my $entry ($result->entries) {
+        $entry->dump;
+    }
     last if $result;
 }
 
-sub log {
+sub _log {
     my ( $level, $msg ) = @_;
     print "$msg\n" if $level eq $cfg->{'prefs'}{'loglevel'};
     return;

+ 33 - 0
ldapscanner.cfg.dist

@@ -0,0 +1,33 @@
+[server]
+# What's needed to connect?
+# Hosts can be many, separate them with commas
+hosts=ldap.host,another.host
+port=636
+ldaps=1
+
+[search]
+# Put in what records you wanna find (and diff over time) here
+base=ou=superheroes,dc=marvel,dc=comics
+filter=(&(objectClass=capedAvenger))
+
+[creds]
+# If you need to auth with the server in order to get the
+# records you need, then enter the details below.
+# If not, don't.
+# id_field is what is distinct about your LDAP record
+# given the search filter, id_value is the value of the field
+# If the base & filter are the same, just enter it in
+# again here.
+base=ou=superheroes,dc=marvel,dc=comics
+filter=(&(objectClass=capedAvenger))
+id_field=codename
+id_value=Lobo
+pass=S4NT4DR00LZ
+
+[prefs]
+# Loglevels:
+#  info:  only the diff is printed
+#  error: die on errors
+#  warn:  warn on errors
+#  debug: die on errors, also print debug info
+loglevel=info