【问题标题】:LDAP bind with Win Server 2008 R2 Standard AD failsLDAP 与 Win Server 2008 R2 标准 AD 绑定失败
【发布时间】:2013-07-22 17:30:34
【问题描述】:

环境是运行 Win Server 2008 R2 Standard 和 SP1 以及 Apache 2.2 和 PHP 5.3.19 的单台服务器。

我有一个 PHP 脚本,它尝试将 LDAP 绑定到 Win Server 2008 R2 Active Directory,但无论我尝试什么变体都失败了。我放了很多回声来查看每一步的结果,我发现 ldap_connect 总是连接。我可以在 URL 中放入全部垃圾并且它永远不会失败。我的 PHP 脚本的最新迭代执行匿名绑定,然后执行 ldap_search,但失败了。

这是脚本(实际域、用户和 pswd 已更改):

<?php

    define(LDAP_OPT_DIAGNOSTIC_MESSAGE, 0x0032);

    ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
    echo "<br>LDAP set debug level: " . ldap_errno($ad) . ' ' . ldap_error($ad);

    $LDAPhost = 'ldap://myDomain.com';
    $LDAPport = '3268';
    $ad = ldap_connect($LDAPhost, $LDAPport);
    echo "<br>LDAP connect to $LDAPhost: " . ldap_errno($ad) . ' ' . ldap_error($ad);

    if ($ad) {

        ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
        echo "<br>LDAP set protocol to version 3: " . ldap_errno($ad) . ' ' . ldap_error($ad);

        ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
        echo "<br>LDAP set referrals to 0: " . ldap_errno($ad) . ' ' . ldap_error($ad);

        $bind = ldap_bind($ad);
        echo "<br>LDAP bind: " . ldap_errno($ad) . ' ' . ldap_error($ad);

        $baseDN = 'CN=Users,DC=myDomain,DC=com';
        $filter = '(objectClass=*)';
        $search = ldap_search($ad, $baseDN, $filter);
        echo "<br>LDAP search $baseDN $filter: " . ldap_errno($ad) . ' ' . ldap_error($ad);

        if ($rec = ldap_get_entries($ad, $search)) {
            echo "<br>LDAP get entries: " . ldap_errno($ad) . ' ' . ldap_error($ad);

            for ($i = 0; $i < $rec["count"]; $i++) {

                echo "<br>$rec[0]['uid'][0]";
                echo "<br>$rec[0]['givenname'][0]";

            }

        } else {
            echo '<br>Record not found.';
        }

        ldap_close($ad);
    } else {
        echo '<br><br>Could not connect.';
    }
?>

显示的结果是:

LDAP set debug level:
LDAP connect to ldap://myDomain.com: 0 Success
LDAP set protocol to version 3: 0 Success
LDAP set referrals to 0: 0 Success
LDAP bind: 0 Success
LDAP search CN=Users,DC=myDomain,DC=com (objectClass=*): 1 Operations error
Record not found.

使用 Windows LDP.exe 实用程序,我可以绑定测试中使用的相同用户/pswd 凭据。

我很想知道将什么作为基本 DN,即 ldap_bind 将这些值与什么进行比较?活动目录中的东西?注册表中的东西?还有什么?

搜索这些东西会找到很多例子,但没有一个对我有用。有什么想法吗?

【问题讨论】:

    标签: ldap bind


    【解决方案1】:

    您需要的基本 DN 是您 AD 中的有效 DN。如何找到基本 DN?那么您需要查看rootDSE,以了解namingContexts 属性的含义。

    如果你有一个带有 ldapsearch 的 Unix 机器,你可以做一个 ldapsearch -x -h mydomain.com -p3268 | less 看看 namingContexts 的值是什么。

    我记得默认情况下,AD 通常有 4 个namingContexts 值。 (没有可以快速测试的 AD 框)。许多 GUI LDAP 工具可以让您连接并查看 rootDSE,或者通过 GUI 方式查看它。

    【讨论】:

    • 好的,到目前为止一切顺利。我在 Windows 机器上,所以我使用了 ldp.exe,已连接,它显示(除其他外):
    • (抱歉,被打断了):namingContexts (5): DC=myDomain,DC=com; CN=配置,DC=myDomain,DC=com; CN=架构,CN=配置,DC=myDomain,DC=com; DC=DomainDnsZones,DC=myDomain,DC=com; DC=ForestDnsZones,DC=myDomain,DC=com;我的基本 DN 使用什么来查看用户?
    • @P.Dow 所以你的基本 DN 是:DC=myDomain,DC=com
    【解决方案2】:

    需要注意的一点是,如果基本 DN 似乎不起作用并且您确定一切,请尝试使用 2003 格式的用户名:user@domain 而不是 DN。从 2003 控制器迁移到 2008 控制器的域似乎存在使用 PHP LDAP 函数绑定的问题。我没有完整的细节,因为我仍在探索它,但 2003 格式适用于 2008 R2 域控制器。下面是一个用于测试的示例基本函数。

    # $ldapconn = a valid connection
    # $username = a username in the format 'username@domain'
    # #password = the password for the above user
    
    function ad_auth($ldapconn,$username,$password){
        ldap_set_option ($ldapconn, LDAP_OPT_REFERRALS, 0);
        ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
        try {
            $ldap_bind = ldap_bind($ldapconn, $username, $password);
        } catch (Exception $e){
            # Your error code
        }
        return $ldap_bind;
    }
    

    用法:$rs = ad_auth($ldapconn,$username,$password);

    如果我找出原因,我会发布它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多