【发布时间】:2017-08-28 20:46:48
【问题描述】:
我能够成功连接并绑定到 LDAP,我可以查询一个域级别的用户,但不能查询下一级的用户。
我可以查询的基本 dn:
$ldap_base_dn = 'DC=a_level,DC=company,DC=org';
我要查询的基本 dn:
$ldap_base_dn = 'DC=b_level,DC=a_level,DC=company,DC=org';
我正在使用管理员帐户绑定到 LDAP。
<?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>';
使用程序 Apache Directory studio 我可以在 b_level 和搜索库 dn 中搜索用户,所以我不明白为什么 php 版本不起作用。
编辑:
进行了更改以显示错误输出。
<?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>';
输出是:
Connect Success...
Bind Success...
ldap_search error: Referral
LDAP Search...
Retrieved 0 Active Directory users
Array
(
[count] => 0
)
【问题讨论】:
-
ldap_error() 输出什么? (例如)由于搜索确实适用于一个 Base-DN 的机会很高,这是一个配置问题,它似乎不适用于不同的 Base-DN。但这与您的特定设置高度相关。因此,在不知道这一点的情况下,很难说为什么“它不起作用”
-
一个原因 f.e.可能是您不允许以下推荐,但您请求的基本 DN 可能是一个...
-
我在ldap_search下添加了以下
$result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes); print ldap_error($ldap_connection) . '<br />';它返回Referral。 -
“返回推荐”是什么意思?您可以将更改添加到您的代码示例并添加输出吗?
标签: php ldap ldap-query