From: Graham B. <gb...@po...> - 2000-08-07 17:03:27
|
On Mon, Aug 07, 2000 at 09:12:12AM -0700, Howard, Michael J wrote: > Hello, I'm new to LDAP. I am trying to use it for authentication. How do I > retrieve the dn using a uid? Here is the code I'm using. I can successfully > bind anonymously and perform a search on the LDAP, however, I am not sure how to > retrieve the DN. Could someone point me in the right direction? > > $ldap = Net::LDAP->new('[LDAP SERVER]') or die "$@"; > > $mesg = $ldap->bind; > > $base = "o=[my company], c=[country]"; > > my $result = $ldap->search( > base => "$base", > scope => "sub", > filter => "uid = $user", > ); > > my $dn = $result->dn(); $result is a container for all results. As there can be more than one entry returned by a search (although you would hope not in your case) So you need to do $entry = $result->entry(0); my $dn = $entry->dn(); Although don't forget to check the ->code for $result to ensure that your search was OK. > $ldap->unbind; Not needed and some servers may not like it. > if ( $result->count != 0 ) { if ( $dn ) { > $mesg = $ldap->bind( > dn => "$dn", > password => "$sent_pw" > ); > > $ldap->unbind; > > if ($mesg->code) { > return "Failed - User Entered Incorrect password"; > } > else { > return 0; > } > } > else { > return "Failed - You do not exist in the LDAP Server"; > } |