Jump to content

[SOLVED] Problems with LDAP query


lonewolf217

Recommended Posts

I am trying to create a very simple login page and I am having a little difficulty with authenticating

 

$ldap['user'] = $_POST['username'];
$ldap['pass'] = $_POST['userpass'];
$ldap['host'] = 'dcserver.com';
$ldap['port'] = 389;
$ldap['dn'] = 'DC=domain,DC=com';
$ldap['base'] = 'CN=Users,DC=domain,DC=com';

// connecting to ldap

$ldap['conn'] = ldap_connect( $ldap['host'], $ldap['port'] ) or die( 'Could not connect to server');

// binding to ldap
$ldap['bind'] = ldap_bind( $ldap['conn'], $ldap['dn'], 'password' );

if( !$ldap['bind'] )
{
    echo ldap_error( $ldap['conn'] );
    exit;
}
// search for the user on the ldap server and return all
// the user information
$ldap['result'] = ldap_search( $ldap['conn'],'','(cn='.$ldap['user'].')');

 

This is basically the beginning of the code up to the search.  The problem is that the user will login using the alias, but the only thing that I can successfully search on so far is the distinguished name.

 

i.e. the user is John Doe with a login of jdoe.  I can successfully find the user if i use

 

$ldap['result'] = ldap_search( $ldap['conn'], $ldap['base'],'(CN=John Doe');

 

but I cannot figure out how to map the login "jdoe" to the DN "John Doe".

 

What am i missing ?  :(

Link to comment
https://forums.phpfreaks.com/topic/114282-solved-problems-with-ldap-query/
Share on other sites

for the ldap_bind, you need the person's unique DN, which usually looks something like uid=jdoe,DC=domain,DC=com

 

what are you dealing with for $_POST['username']? is it "John Doe" or jdoe?

 

also, if you run this, what is the output?

<?php
$ldap['host'] = 'dcserver.com';
$ldap['port'] = 389;
$ldap['dn'] = 'DC=domain,DC=com';
$ldap['base'] = 'CN=Users,DC=domain,DC=com';

$ldap['conn'] = ldap_connect( $ldap['host'], $ldap['port'] ) or die( 'Could not connect to server');
$ldap['bind'] = ldap_bind( $ldap['conn'] ) or die(ldap_error( $ldap['conn'] ));
$ldap['result'] = ldap_search( $ldap['conn'],$ldap['base'],'(cn=John Doe)');
print 'DN: '.ldap_get_dn($ldap['conn'],ldap_first_entry($ldap['result']));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.