使用PHP连接到Active Directory时是否需要ldap.conf?

时间:2012-05-10 13:11:49

标签: php apache ldap

我正在尝试使用PHP(5.3.2)/ Apache(2.0.59)/ Windows(2003)对Active Directory进行身份验证。

但是,我收到以下错误:

ldap_start_tls()[function.ldap-start-tls]:无法启动TLS:第15行的E:\ my_page.php中的连接错误

这是我目前的剧本:

putenv('LDAPTLS_REQCERT=never');

$resource = ldap_connect("xxx") 
    or die("Failed to connect to LDAP server."); 

echo "Connected to LDAP server.<br />"; 

//these options may not be necessary in all environments 
ldap_set_option($resource, LDAP_OPT_PROTOCOL_VERSION, 3);    
ldap_set_option($resource, LDAP_OPT_REFERRALS, 0); 

$result = ldap_start_tls($resource) or die("Failed to start TLS.<br />"); 

echo "Started TLS.<br />"; 

$result = ldap_sasl_bind($resource, NULL, '', 'GSSAPI', 'xxx', '', '') 
    or die("Failed to GSSAPI bind.<br />"); 

echo "GSSAPI bound."; 

我看过this question for help但是,我一直看到对ldap.conf的引用。

这是否适用于OpenLDAP,您连接的LDAP服务器?如果是,我可以忽略它,因为使用现有的企业Active Directory?

或者这是连接到LDAP服务器的PHP库(即ldap_connect())吗?

编辑#1

Wireshark的屏幕截图...

Screenshot of Wireshark

我在那里看到,未知的CA ......我将如何解决这个问题(在网上看ATM)。

编辑#2

更新,我现在收到一个不同的错误。我在c:\和c:\ openldap \ sysconf

上创建了ldap.conf

ldap.conf的内容:

#
# LDAP Defaults
#

TLS_REQCERT never

现在,它停留在ldap_sasl_bind方法,这是正常的 - 它没有安装。

编辑#3

最终产品:

function isAuthenticated($user, $pass){
    //init
    $ldap_server = "";
    $ldap_user = "";
    $ldap_pass = "";
    $ldap_dn = "";
    $ldap_filter_fields = array("dn","cn","samaccountname");
    //establish connection
    $ldap_conn = ldap_connect($ldap_server) 
        or die("Failed to connect to LDAP server."); 

    //these options may not be necessary in all environments 
    ldap_set_option($resource, LDAP_OPT_PROTOCOL_VERSION, 3);    
    ldap_set_option($resource, LDAP_OPT_REFERRALS, 0); 

    //Magic happens here, encrypted tunnel starts!
    $result = ldap_start_tls($ldap_conn) or die("Failed to start TLS.<br />"); 

    $out = 0;
    //connect using our known user
    if($bind = ldap_bind($ldap_conn, $ldap_user, $ldap_pass)){  
        //search for the user
        $ldap_search_results = ldap_search($ldap_conn, $ldap_dn, "samaccountname=".$user, $ldap_filter_fields) or die ("Failed to search LDAP");
        //get entry
        $ldap_record = ldap_get_entries($ldap_conn, $ldap_search_results);
        debug($ldap_record);
        if($ldap_record["count"] > 0){
            //try to authenticate user here
            if($bind2 = @ldap_bind($ldap_conn, $ldap_record[0]["dn"], $pass))
                $out = 1;
            else
                //wrong password
                $out = 0;
        }
        else
            //user wasn't found
            $out = 3;       
    }
    else
        //something happened when connecting with our ldap_user
        $out = 2;

    return $out;    
}

2 个答案:

答案 0 :(得分:1)

您与未知的CA一起走在正确的轨道上。我在连接到AD的CentOS上遇到了类似的PHP问题。我必须从AD服务器导出CA证书并将其配置为在CentOS系统上受信任,这涉及将证书复制到/etc/openldap/cacerts并运行OpenSSL的c_rehash。不幸的是,我不知道如何告诉你在Windows下使用相同的设置。

答案 1 :(得分:0)

是的,如果您尝试连接尚未在运行Apache的主机上建立信任的AD,则需要更改ldap.conf文件并将TLS_REQCERT的值更改为demand。如果您拥有来自受信任CA的证书,即已经安装在计算机操作系统密钥库中的证书,那么它可以在TLS_REQCERT设置为的情况下运行,否则您将不得不显式提供证书并更改变量TLS_REQCERT。

相关问题