简体   繁体   中英

PHP LDAP unable to search for user

I'm able to successfully connect and bind to LDAP and I can query users from one domain level but not users from the next level down.

the base dn I can query:

$ldap_base_dn = 'DC=a_level,DC=company,DC=org';

the base dn I would like to query:

$ldap_base_dn = 'DC=b_level,DC=a_level,DC=company,DC=org';

I am binding to LDAP using an admin account.

<?php

/**
 * Get a list of users from Active Directory.
 */
$ldap_password = 'PASSWORD';
$ldap_username = 'ADMIN';
$ldap_connection = ldap_connect('ldap://ldap.company.org/');
if (false === $ldap_connection) {
    // Uh-oh, something is wrong...
    print "CONNECT ERROR<br />";
}

// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.

if (true === ldap_bind($ldap_connection, $ldap_username, $ldap_password)) {
    print "ldap bind<br />";
    $ldap_base_dn = 'DC=b_level,DC=a_level,DC=company,DC=org';
    $search_filter = '(&(objectCategory=person)(samaccountname=*))';
    $attributes = array();
    $attributes[] = 'givenname';
    $attributes[] = 'mail';
    $attributes[] = 'samaccountname';
    $attributes[] = 'sn';
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
    if (false !== $result) {
        print "ldap search<br />";
        $entries = ldap_get_entries($ldap_connection, $result);
        for ($x=0; $x<$entries['count']; $x++) {
            if (!empty($entries[$x]['givenname'][0]) &&
                 !empty($entries[$x]['mail'][0]) &&
                 !empty($entries[$x]['samaccountname'][0]) &&
                 !empty($entries[$x]['sn'][0]) &&
                 'Shop' !== $entries[$x]['sn'][0] &&
                 'Account' !== $entries[$x]['sn'][0]) {
                $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));
            }
        }
    }
    ldap_unbind($ldap_connection); // Clean up after ourselves.
}

$message .= "Retrieved ". count($ad_users) ." Active Directory users\n";

print $message;

echo '<pre>';
print_r($entries);
echo '</pre>';

using the program Apache Directory studio I am able to run a search for a user in b_level and the search base dn so i don't understand why the php version doesn't work.

EDIT:

Made changes to display error output.

<?php

/**
 * Get a list of users from Active Directory.
 */
$ldap_password = 'PASSWORD';
$ldap_username = 'ADMIN';
$ldap_connection = ldap_connect('ldap://ldap.company.org/');
if (false === $ldap_connection) {
    // Uh-oh, something is wrong...
    print "CONNECT ERROR<br />";
}

print "Connect Success...<br />";

// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.

if (true === ldap_bind($ldap_connection, $ldap_username, $ldap_password)) {
    print "Bind Success...<br />";
    $ldap_base_dn = 'DC=b_level,DC=a_level,DC=company,DC=org';
    $search_filter = '(&(objectCategory=person)(samaccountname=*))';
    $attributes = array();
    $attributes[] = 'givenname';
    $attributes[] = 'mail';
    $attributes[] = 'samaccountname';
    $attributes[] = 'sn';
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
    print "ldap_search error: ".ldap_error($ldap_connection) . '<br />';
    if (false !== $result) {
        print "LDAP Search...<br />";
        $entries = ldap_get_entries($ldap_connection, $result);
        for ($x=0; $x<$entries['count']; $x++) {
            if (!empty($entries[$x]['givenname'][0]) &&
                 !empty($entries[$x]['mail'][0]) &&
                 !empty($entries[$x]['samaccountname'][0]) &&
                 !empty($entries[$x]['sn'][0]) &&
                 'Shop' !== $entries[$x]['sn'][0] &&
                 'Account' !== $entries[$x]['sn'][0]) {
                $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));
            }
        }
    }
    ldap_unbind($ldap_connection); // Clean up after ourselves.
}

$message .= "Retrieved ". count($ad_users) ." Active Directory users\n";

print $message;

echo '<pre>';
print_r($entries);
echo '</pre>';

output is:

Connect Success...
Bind Success...
ldap_search error: Referral
LDAP Search...
Retrieved 0 Active Directory users
Array
(
    [count] => 0
)

It seems to me that the base_dn ( level_b ) is not stored on the directory you are requesting but on another directory configured as the referral.

You need to :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM