首页 > 解决方案 > 空密码字段启用成功的 LDAP 绑定

问题描述

我目前正在编写我的第一个带有 2 个绑定的 PHP LDAP 脚本。
第一个绑定似乎工作正常,因为在成功绑定到服务器以进行查询后,我得到了结果的返回。但是,第二个绑定使用了初始绑定的 distinctName 结果,这给我带来了一些问题。当尝试进行第二次绑定时,它将允许空密码字段与 distinctName 绑定。
但是,当输入错误的密码时,将无法正确绑定。
在这种情况下,为什么空条目绑定?感谢任何线索。

这是我正在使用的代码:

//begin set parameters
$host = "*****";
$port = "3268";
$rdnUsername = "*****";//for accessing server
$rdnPassword = "*****";//for accessing server


$_connect = ldap_connect($host);
if (! $_connect) {
    die ('no connection');
}

if (isset($rdnUsername) && isset($rdnPassword)) {
    $args [] = $_connect;
    $args [] = $rdnUsername;
    $args [] = $rdnPassword;
}
if ( ! call_user_func_array ( 'ldap_bind', $args ) ) {
    die (
        sprintf('Could not bind to server %s. Returned Error was: [%s] %s',$host,ldap_errno($_connect),ldap_error($_connect))
    );
}

if (! isset($filter)) {
    $filter = "(userPrincipalName=".$_POST['username']."@school.edu)";
}

$trimmerdUsername = trim(preg_replace('/[^a-zA-Z0-9\-\_@\.]/', '', $_POST['username']));

$filter = str_replace($_POST['username'], $trimmerdUsername, $filter);

$attributes = array("name", "telephonenumber", "mail", "userprincipalname");
$ldap_dn = "dc=tcw,dc=net,dc=tceo,dc=edu";

$_ldapresults = ldap_search($_connect, $ldap_dn, $filter, $attributes, 0, 0, 10 ) or exit("Unable to search");

if (! $_ldapresults) {
    die ('No user with that information found');
}
if (1 > ldap_count_entries($_connect, $_ldapresults)) {
    die ('No user with that information found');
}

if (1 < ldap_count_entries($_connect, $_ldapresults )) {
    die ('More than one user found with that information');
}

$_results = ldap_get_entries($_connect, $_ldapresults);
if (false === $_results) {
    die ('no result set found');
}

ldap_free_result ( $_ldapresults );
$distinguishedName = $_results[0]['dn'];

$userPrincipalName = $_results[0]["userprincipalname"][0];
print "<pre>";
print_r ($_results);
print "</pre>";
echo "<br>userPrincipalName is: ".$userPrincipalName."<br>";
echo "<br>distinguishedName is: ".$distinguishedName."<br>";


$password = $_POST['password'];
$link_id = @ldap_bind($_connect, $distinguishedName, $password);

//if (false === $link_id) {
if ($link_id === false) {   
    die ('BIND failed');
}else{
    echo "<br>success!<br>";    
}

标签: phpbindingldapbind

解决方案


没有密码 ldap 匿名绑定。

这是在协议中定义的,应用程序要求它不允许与空密码绑定。

这也记录在 PHP 的 ldap_bind 文档(http://php.net/ldap_bind


推荐阅读