Link to home
Start Free TrialLog in
Avatar of alumwell
alumwell

asked on

Getting PHP to authenticate with LDAP correctly?

Hello.

I'm testing connecting to the LDAP using PHP. I have found a script (see code snippet), although when trying to run it, it just keeps asking for a username and password and after 3 tries will just say Authorization Failed. Ive tried entering in different usernames and password but to no avail. We created a user called changepassword to test the script with.

Is there anything missing or anything that needs to be done on the server in order to get this to work?

Webserver - Windows XP using Xampp web server

Main server - Windows Server 2008

Any advice is appreciated.


<?php
 
$ldapconfig['host'] = 'alumwell-s.walsall.sch.uk';
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'DC=Alumwell-s,DC=walsall,DC=sch,DC=uk';
$ldapconfig['authrealm'] = 'alumwell-s.walsall.sch.uk';
 
function ldap_authenticate() {
    global $ldapconfig;
    global $PHP_AUTH_USER;
    global $PHP_AUTH_PW;
    
    if ($PHP_AUTH_USER = "changepassword" && $PHP_AUTH_PW = "changeme") {
      $ds=@ldap_connect($ldapconfig['host']);
       $r = @ldap_search( $ds, $ldapconfig['basedn'], 'uid=' . $PHP_AUTH_USER);
       if ($r) {
          $result = @ldap_get_entries( $ds, $r);
          if ($result[0]) {
              if (@ldap_bind( $ds, $result[0]['dn'], $PHP_AUTH_PW) ) {
                 return $result[0];
          }
         }
       }
    }
    header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
    header('HTTP/1.0 401 Unauthorized');
    return NULL;
}
 
if (($result = ldap_authenticate()) == NULL) {
    echo('Authorization Failed');
    exit(0);
}
echo('Authorization success');
print_r($result);
 
?>

Open in new window

Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

What version of PHP?

Try $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'


<?php
 
$ldapconfig['host'] = 'alumwell-s.walsall.sch.uk';
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'DC=Alumwell-s,DC=walsall,DC=sch,DC=uk';
$ldapconfig['authrealm'] = 'alumwell-s.walsall.sch.uk';
 
function ldap_authenticate() {
    global $ldapconfig;
    
    if ($_SERVER['PHP_AUTH_USER'] = "changepassword" && $_SERVER['PHP_AUTH_PW'] = "changeme") {
      $ds=@ldap_connect($ldapconfig['host']);
       $r = @ldap_search( $ds, $ldapconfig['basedn'], 'uid=' . $_SERVER['PHP_AUTH_USER']);
       if ($r) {
          $result = @ldap_get_entries( $ds, $r);
          if ($result[0]) {
              if (@ldap_bind( $ds, $result[0]['dn'], $_SERVER['PHP_AUTH_PW']) ) {
                 return $result[0];
          }
         }
       }
    }
    header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
    header('HTTP/1.0 401 Unauthorized');
    return NULL;
}
 
if (($result = ldap_authenticate()) == NULL) {
    echo('Authorization Failed');
    exit(0);
}
echo('Authorization success');
print_r($result);
 
?>

Open in new window

Avatar of alumwell
alumwell

ASKER

Thanks for your reply.

I have just tried that code although i am still getting the login box coming up:

-----------
The server localhost at alumwell-s.walsall.sch.uk requires a username and password.

Warning: This server is requesting that your username and password be sent in an insecure manner (basic authentication without a secure connection).
-----------

Any other ideas? Are there any settings that need to be turned on etc? Do i need to be logged on to a certain user? etc etc

Thanks.
Can you remove the @'s. These will hide any errors.

Add at the top of your script ...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
//... Your code now ...

Let's see what else you may get.
Thank you for your reply.

I've put that code in and taken out the '@'s and i now get this error:

---------
Warning: ldap_search() [function.ldap-search]: Search: Operations error in C:\xampp\htdocs\test.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test.php:17) in C:\xampp\htdocs\test.php on line 27

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test.php:17) in C:\xampp\htdocs\test.php on line 28
Authorization Failed
---------

Is this a step forward? Any ideas now? Current code is attached.

Thanks again.
<?php
 
 error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
 
$ldapconfig['host'] = 'alumwell-s.walsall.sch.uk';
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'DC=Alumwell-s,DC=walsall,DC=sch,DC=uk';
$ldapconfig['authrealm'] = 'alumwell-s.walsall.sch.uk';
 
function ldap_authenticate() {
    global $ldapconfig;
    
    if ($_SERVER['PHP_AUTH_USER'] = "changepassword" && $_SERVER['PHP_AUTH_PW'] = "changeme") {
      $ds=ldap_connect($ldapconfig['host']);
       $r = ldap_search( $ds, $ldapconfig['basedn'], 'uid=' . $_SERVER['PHP_AUTH_USER']);
       if ($r) {
          $result = ldap_get_entries( $ds, $r);
          if ($result[0]) {
              if (ldap_bind( $ds, $result[0]['dn'], $_SERVER['PHP_AUTH_PW']) ) {
                 return $result[0];
          }
         }
       }
    }
    header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
    header('HTTP/1.0 401 Unauthorized');
    return NULL;
}
 
if (($result = ldap_authenticate()) == NULL) {
    echo('Authorization Failed');
    exit(0);
}
echo('Authorization success');
print_r($result);
 
?>

Open in new window

You may find your ldap server isn't allowing anonymous connections / searching.

So you need to ldap_bind() right after ldap_connect().

By binding with the supplied credentials, you know if they are valid or not. No need to try and find the user first.

If you can't bind because you don't know the full DN, then you will need to bind using a set of known credentials first (I would recommend creating an account with readonly access to limited parts of the LDAP if that is possible).
Gettin closer. Right, i now have the code that i will attach, i am getting this message:

-------------
Congratulations! changepassword is authenticated.
Warning: ldap_search() [function.ldap-search]: Search: Operations error in C:\xampp\htdocs\test2.php on line 25
Error in search query
------------

So there is something wrong with the search operation. Cannot figure out what though. The only thing i'm not sure about is the $query variable. Any ideas or advice?

Once again, thanks for your help.
<?php
$ldaphost = "alumwell-s.walsall.sch.uk";
$ldapport = 389;
 
$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
 
if ($ds)
{
    $username = "changepassword";
    $upasswd = "changeme";
 
    $ldapbind = ldap_bind($ds, $username, $upasswd);
                              
    if ($ldapbind)
        {print "Congratulations! $username is authenticated.";}
    else
        {print "Nice try, kid. Better luck next time!";}
}
 
// create the search string
$query = "(&(name=" . $_POST['name'] . "))";
 
ldap_search($ds,"DC=Alumwell-s,DC=walsall,DC=sch,DC=uk", $query) or die("Error in search
query");
 
// get entry data as array
$info = ldap_get_entries($ds, $result);
 
// iterate over array and print data for each entry
echo "<ul>";
for ($i=0; $i<$info["count"]; $i++)
{
echo "<li>".$info[$i]["sn"][0]." - ".$info[$i]["mail"][0]." -
".$info[$i]["dn"]."</li>"; } echo "</ul>";
 
// print number of entries found
echo "Number of entries found: " . ldap_count_entries($ds, $result) .
"<p>";
 
// all done? clean up
ldap_close($ds);
 
?>

Open in new window

Can you try ...

$query = "name={$_POST['name']}";
Tried that to no avail. Says the same thing.

Any other suggestions are welcomed. Thanks again.
There is a difference between the login name and the name.

Try ...

$query = 'name=' . $_POST['name']{0} . '*';

This will wild card based upon the first letter of their name.

Also try ...

$query = "samaccountname={$_POST['name']}";

Is this to Active Directory (that's where I get samaccountname from).

For example, my login account name is RichardQ (samaccountname = RichardQ, but my name is Richard A. Quadling


Well going on username i now have what is attached in the code snippet. A number of usernames start in st, so i've tried using the wildcard to get some results. But i still get the same error:

-------
Congratulations! changepassword is authenticated.
Warning: ldap_search() [function.ldap-search]: Search: Operations error in C:\xampp\htdocs\test2.php on line 27
Error in search query
------

line 27 being the 'ldap_search' line.

Any other ideas?

Thanks for your help so far.
<?php
$ldaphost = "alumwell-s.walsall.sch.uk";
$ldapport = 389;
 
$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
 
if ($ds)
{
    $username = "changepassword";
    $upasswd = "changeme";
 
    $ldapbind = ldap_bind($ds, $username, $upasswd);
                              
    if ($ldapbind)
        {print "Congratulations! $username is authenticated.";}
    else
        {print "Nice try, kid. Better luck next time!";}
}
 
 
 
// create the search string
$name = "st";
$query = "samaccountname=" . $name{0} . '*';
 
ldap_search($ds,"DC=Alumwell-s,DC=walsall,DC=sch,DC=uk", $query) or die("Error in search query");
 
// get entry data as array
$info = ldap_get_entries($ds, $result);
 
// iterate over array and print data for each entry
echo "<ul>";
for ($i=0; $i<$info["count"]; $i++)
{
echo "<li>".$info[$i]["sn"][0]." - ".$info[$i]["mail"][0]." -
".$info[$i]["dn"]."</li>"; } echo "</ul>";
 
 
 
 //print number of entries found
echo "Number of entries found: " . ldap_count_entries($ds, $result) .
"<p>";
 
 
 
// all done? clean up
ldap_close($ds);
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I had a look on that link and used what you'd said previously with it and i now have a working piece of code. Now i will have a go with it. Thanks again.
Excellent.