Zetetic.Ldap - Bringing LDAP & LDIF tools to .NET

Zetetic.Ldap is a . NET library for . NET 2 and above, which makes it easier to work with directory servers (like Active Directory, ADAM , Red Hat Directory Server, and others).

Zetetc.Ldap on Github

What’s it for?

We built the Zetetic.Ldap library to make directory projects and programming faster and easier, and release it here in the hopes that others will find it useful too. As far as we know, at this particular moment, this is the only .NET library that really understands the LDIF specification.

Features

  • LDIF Parsing and Generation

    SRead and write the file format used for moving data around between directory systems.

  • LDAP Entry-oriented API with Change Tracking

    Create and modify directory objects in a more natural way.

  • LDAP Schema Interrogation

    Quick programmatic access to the kinds of objects and fields your directory server understands. Learn if an attribute is a string, a number, a date, etc., without lots of manual research and re-parsing.

  • LDIF Pivoter

    Turn an LDIF file into a (comma or tab-delimited) flat file for analysis or loading into systems that don’t speak LDIF.

Examples

Zetetic.Ldap.LdifEntryReader

using (LdifEntryReader ldif = new LdifEntryReader(@"c:\temp\stuff.ldif"))
{
  for (Entry entry = ldif.ReadEntry(); entry != null; entry = ldif.ReadEntry())
    Console.WriteLine("Found: {0}", entry.DistinguishedName); 
}

Zetetic.Ldap.LdifWriter

using (LdifWriter ldif = new LdifWriter(@"c:\temp\stuff.ldif"))
{
  ldif.BeginEntry("cn=joe cool,o=zetetic"); 
  ldif.WriteAttr("givenName", "joe"); 
  ldif.WriteAttr("jpegPhoto", File.ReadAllBytes(@"c:\temp\joe-photo.jpeg"); 
  ldif.WriteAttr("dateCreated", DateTime.Now); 
  ldif.Close();
}

Zetetic.Ldap.Schema

ISchemaInfo target = new AdsSchemaInfo();
using (LdapConnection conn = new LdapConnection("localhost:20389"))
{
  conn.Bind(System.Net.CredentialCache.DefaultNetworkCredentials);
  target.Initialize(conn);
}

foreach (ObjectClassSchema o in target.ObjectClasses)
{
  System.Console.WriteLine("oc: {0}", o);
  foreach (AttributeSchema a in o.MustHave)
    System.Console.WriteLine("  must: {0} as {1}", a, a.LangType);

  foreach (AttributeSchema a in o.MayHave)
    System.Console.WriteLine("  may : {0} as {1}", a, a.LangType);
}

The LDIF Pivoter

The pivoter transforms LDIF files into delimited files, so you can load them into a database (or elsewhere) for analysis and data quality checking. Let’s suppose you start out with a file like this, pulled from Active Directory with ldifde, or from any other directory server with ldapsearch:

dn: cn=molly,ou=users,dc=dublin,dc=net
objectClass: top
objectClass: person
objectClass: organizationalPerson
uid: mollybloom
cn: molly
givenName: Marion
sn: Bloom

dn: cn=leo,ou=users,dc=dublin,dc=net
objectClass: top
objectClass: person
objectClass: organizationalPerson
uid: leobloom
cn: leo
givenName: Leopold
sn: Bloom

dn: cn=stephen,ou=users,dc=dublin,dc=net
objectClass: top
objectClass: person
objectClass: organizationalPerson
uid: sdaedalus
cn: stephen
givenName: Stephen
sn: Daedalus
jpegPhoto:: JkfeJFLE32njkFKJEFHJEWLFHjlhjlfhulf7yYOfh3nfhjLHj
 fjlewfhJKLFEJ390KJFHJLEhjfejlwfh893hjhjlHKLhjkHJgvhwghjq
 83hjkHFJKFGILUhjvlhjkdvguil3hu3289vfhjlfew7ylvhjlecewhjl

dn: cn=characters,ou=groups,dc=dublin,dc=net
objectClass: top
objectClass: groupOfUniqueNames
uid: characters
uniqueMember: cn=molly,ou=users,dc=dublin,dc=net
uniqueMember: cn=stephen,ou=users,dc=dublin,dc=net
uniqueMember: cn=leo,ou=users,dc=dublin,dc=net

LDIF is a readable, flexible format, but it can be tricky to parse, particularly with folded lines on long values, binary syntax, etc. The pivoter is here to help.

The first step is to decide what values you want to draw out of the LDIF file. If you don’t tell the pivoter what columns to take, it will grab DN, RDN, parent DN, sAMAccountName, objectClass, sn, givenName, and mail. But if you want—let’s say—uid, sn, and objectClass only, point the pivoter to a little XML file like so:

<ArrayOfPivotColumn>
  <PivotColumn>
    <Source>uid</Source>
    <Destination>uid</Destination>
    <Index>0</Index>
  </PivotColumn>
  <PivotColumn>
    <Source>sn</Source>
    <Destination>sn</Destination>
    <Index>0</Index>
  </PivotColumn>
  <PivotColumn>
    <Source>objectClass</Source>
    <Destination>objectClass</Destination>
    <Index>-1</Index>
  </PivotColumn>
</ArrayOfPivotColumn>

Now, launch the pivoter with the “-c” option to tell it where to load the XML configuration, and “-f” to specify the LDIF file:

Zetetic.Ldap.Pivoter.exe -c myconfig.xml -f dublin.ldif

You’ll get output like this — ready to rock in Excel or an RDBMS:

uid          sn        objectclass
mollybloom   Bloom     organizationalPerson
leobloom     Bloom     organizationalPerson
sdaedalus    Daedalus  organizationalPerson
characters             groupOfUniqueNames