空密码字段启用成功的LDAP绑定

时间:2018-06-01 21:29:09

标签: php binding ldap bind

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

以下是我正在使用的代码:

//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>";    
}

2 个答案:

答案 0 :(得分:3)

没有密码,ldap匿名绑定。

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

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

的ldap_bind文档中

答案 1 :(得分:1)

您应该避免从应用程序匿名连接到LDAP服务器,因为您不能依赖服务器配置。但是,如果您有权访问服务器,则可以通过限制访问来控制对条目的访问。在 slapd.conf

中添加这些行
  

通过匿名身份验证访问*

要详细了解访问控制:https://www.openldap.org/doc/admin24/access-control.html