LDAP Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesLDAP Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 9th, 2006, 10:24 AM
mayag mayag is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 9 mayag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 44 sec
Reputation Power: 0
Parsing ldif file

Hi all,

I have to parse ldif file in order to perform "full sync" of AD with DB. If you ever did parsing of ldif file in C/C++, I would appreciate any help from you.

Thank you in advance

Reply With Quote
  #2  
Old October 9th, 2006, 03:09 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,845 Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 20 h 16 m 27 sec
Reputation Power: 634
Instead of answering to both threads (your other thread here I'll answer both here.

You can do a partial dump of the AD server, when you run ldapsearch (to preform the LDIF dump) you can specifiy which attributes you want dumped, and the other ones are omited. This way you have less data to parse through.

To parse an LDIF is quite easy, it's just a text file. What part of parsing it do you need help with?

You say you have to use C/C++ any chance you can use Perl? Perl is great at parsing text so you won't have to create anything new. Perl normally comes with most lunix servers/workstations.

Reply With Quote
  #3  
Old October 19th, 2006, 11:03 AM
mayag mayag is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 9 mayag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 44 sec
Reputation Power: 0
thank you for your response,
the problem is that my ldif file contains a huge number of objects.
I will be very grateful for any example of parsing ldif file

thanks in advance

Reply With Quote
  #4  
Old October 19th, 2006, 02:38 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,845 Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 20 h 16 m 27 sec
Reputation Power: 634
Can you give an example (2 or 3 entries) they don't have to have valid data in them, but I need to know what we're working with.

Reply With Quote
  #5  
Old October 22nd, 2006, 05:04 AM
mayag mayag is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 9 mayag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 44 sec
Reputation Power: 0
# extended LDIF
#
# LDAPv3
# base <OU=xxx,OU=xxx,OU=xxx,DC=xxxx,DC=xx,DC=xx> with scope sub
# filter: (objectclass=person)
# requesting: objectguid displayname dn cn objectclass rdn proxyaddresses rfc822mailbox mailnickname loginname displayname memberof email member samaccountname givenName sn mail
# with pagedResults control: size=100
#

# xxxxxxxxxxxxx, xxx, consolidatedmessenger, Hosting, xxxxx
dn: CN=xxxxx,OU=xxx,OU=consolidatedmessenger,OU=Hosting,DC=xxx,DC=xx,DC=xx
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: xxxxxxxxx
sn:: xxxxx
givenName:: xxxxxx
displayName:: xxxxxxxx
proxyAddresses: smtp:xxxxxxx
proxyAddresses: smtp:xxxxxxxxxxxxxxxxx
proxyAddresses: SMTP:xxxxxxxxxxxxxxxxx
mailNickname: xxxxxxxxx
objectGUID:: xxxxxxxxxxxxxxxxxxxx
sAMAccountName: xxxxxxxx
mail: xxxxxxxxxxxx

# xxxxxxxxxxxx, xxxx, consolidatedmessenger, Hosting, xxxxx
dn: CN=xxxxx,OU=xxx,OU=consolidatedmessenger,OU=Hosting,DC=xxxx,DC=xx,DC=xx
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: xxxxxxxxxxxxx
sn:: xxxxxxxxx
givenName:: xxxxxxxxxxxxxx
displayName:: xxxxxxxxxxxxxxx
proxyAddresses: smtp:xxxxx
proxyAddresses: smtp:xxxxx
proxyAddresses: SMTP:xxxxxxxxxxxx
mailNickname: xxxx
objectGUID:: xxxxxx
sAMAccountName: xxxxxx
mail: xxxxxxxxxxxx

# search result
search: 7
result: 0 Success
control: 1.2.840.113556.1.4.319 false MIQAAAFhAgEABIQAAAFYAQAAAFgBAAD/////sKoL
1XsvIlh1xSTRSCtLqw6SpMmhPlROocTYT1fet2cAAAAAAQAAAAAAAAABAwsAJQAAAAUAAAACAAAAA
AAAAAQAAAAFAAEAGjAAAPQvAACzBQAAAAAAADbnOhYdlxhAuAEwoCfjfywAAAAABAAAAAEAAAAAAA
AALMFAAAJwy8AAPQvAAAJGjAAAAEDCwAIf4ALAwEAAAAAAAD/////AAAAAAAAAAD/////DwAAACUA
H8CAAAAsQUAAAmyBQAAswUAAAnDLwAA9C8AAAkaMAAA////////AAA=
# extended LDIF
#
# LDAPv3
# base <OU=xxxx,OU=consolidatedmessenger,OU=Hosting,DC=xxx,DC=xx,DC=xx> with scope sub
# filter: (objectclass=person)
# requesting: objectguid displayname dn cn objectclass rdn proxyaddresses rfc822mailbox mailnickname loginname displayname memberof email member samaccountname givenName sn mail
# with pagedResults control: size=100
#

# xxxxxxxxxxxxx, xxxx, consolidatedmessenger, Hosting, xxxxx
dn: CN=xxxxx,OU=xxx,OU=consolidatedmessenger,OU=Hosting,DC=xxx,DC=xx,DC=xx
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: xxxxx
givenName: xxxxx
displayName: xxxxx
proxyAddresses: smtp:xxxxxx
proxyAddresses: smtp:xxxxxx
proxyAddresses: SMTP:xxxxxxxxxxxxxxx
mailNickname: xxxxxxxxx
objectGUID:: xxxxxxxxx
sAMAccountName: xxxxxxx
mail: xxxxxxxx

Reply With Quote
  #6  
Old October 24th, 2006, 10:45 AM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Psycho Canadian
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jan 2001
Location: Canada
Posts: 4,845 Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level)Viper_SB User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 20 h 16 m 27 sec
Reputation Power: 634
Ok what do you want parsed out of it? I'd personally use regular expressesions and it'd be quite easy, but I think you are looking for something very specific.

Reply With Quote
  #7  
Old March 17th, 2010, 04:34 PM
BillyPaul BillyPaul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2010
Posts: 1 BillyPaul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 47 sec
Reputation Power: 0
Perl LDIF parser

I just finished writing a one-off LDIF parser in Perl. Here is the code in case anyone needs it...

Code:
# This routine takes in a path to an LDIF and returns an associative array of associative arrays.
sub getLDAPEntriesFromLDIF($)
{
	# Get the input path.
	my ($inputLDAPPath) = @_;
	
	# Open the input file.
	open(INPUTFILE, $inputLDAPPath);

	# Get all the lines of the INPUTFILE as an array.
	my @lines;
	@lines = <INPUTFILE>;
	
	# Create an associative array to hold all the ldap entries.
	my %ldapEntries;
	
	# Create a counter.
	my $i = 0;
	
	# Start a loop that iterates through all the lines of the LDIF.
	GET_NEXT_ENTRY: for($i; $i < @lines; $i++)
	{
		# Create an associative array to hold an individual entry.
		my %ldapEntry;
		
		# Start iterating through the entry.
		GET_NEXT_LINE: for($i; $i < @lines; $i++)
		{
			# If the line is a line continuation, restart the GET_NEXT_LINE loop on the next line.
			if($lines[$i] =~ /^(\s+\S+)\n$/)
			{
				$i++;
				redo GET_NEXT_LINE;
			}

			# If the line is empty, it signifies the end of an entry.  In this case, cache the individual entry
			# to ldapEntries and restart the GET_NEXT_ENTRY loop on the next line.
			if($lines[$i] =~ /^(\s*)\n$/)
			{
				if($ldapEntry{"dn"})
				{
					$ldapEntries{$ldapEntry{"dn"}[0]} = [%ldapEntry];
				}
				else
				{
					print "-------------- ERROR - No \"dn\" for entry at line ".($i+1)." --------------\n";
				}
				$i++;
				redo GET_NEXT_ENTRY;
			}
			
			# Create a temp variable to hold the current line of the ldif
			my $tempLine = $lines[$i];
			
			# Create a variable and populate it with the split line.
			my @splitLine;
			if($tempLine =~ /::/)
			{
				@splitLine = split(/::/, $tempLine);
				print "-------------- WARN value for attribute \"".$splitLine[0]."\" at line ".($i+1)." is encrypted --------------\n";
			}
			elsif($tempLine =~ /:/)
			{
				@splitLine = split(/:/, $tempLine);
			}
			
			# If the next line is a continuation of the current line, go ahead and add its text to the end of the current line.
			if($lines[$i+1] =~ /^(\s+)/)
			{
				if($lines[$i+1] =~ /(\S+)$/)
				{
					chomp($splitLine[1]);
					$splitLine[1] = $splitLine[1].$&;
				}
			}

			# Remove the line break from the end of the value portion of the line.
			chomp($splitLine[1]);
			
			# Remove the space from the front of the value portion of the line.
			$splitLine[1] = substr($splitLine[1], 1);
			
			# If everything went okay, go ahead and cache the attribute and value to the ldap entry.
			if($splitLine[1])
			{
				if(!($ldapEntry{$splitLine[0]}))
				{
					my @tempArray = ($splitLine[1]);
					$ldapEntry{$splitLine[0]} = [@tempArray];
				}
				else
				{
					if($splitLine[1])
					{
						push(@{$ldapEntry{$splitLine[0]}}, $splitLine[1])
					}
					else
					{
						print "ERROR!";
					}
				}
			}
		}
	}
	close(INPUTFILE);
	return %ldapEntries;
}


As you can see, it returns an associative array (keyed on the dn of the entries) of associative arrays (keyed on the attribute names in each entry).

To access an attribute (and it's values) on an entry use:

Code:
my %entry = @{$ldapEntries{$key}}; # Where $key = the entry's 'dn' (e.g. cn=blah,ou=Awesome,dc=code,dc=com).
my @values = @{$entry{$attr}}; # Where $attr = the attributes name (e.g. "uid").


I hope this helps someone out.

Billy

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesLDAP Programming > Parsing ldif file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap