无法使用PHP绑定LDAP

时间:2015-03-09 03:23:04

标签: php ldap

我尝试使用PHP绑定LDAP并收到此错误

    Warning: ldap_bind() [function.ldap-bind]: Unable to bind to server: Can't contact LDAP server on line 21

第21行的脚本就是这个..

    $bind_status = ldap_bind($conn_status, $app_user, $app_pass);

这是在LDAP中连接的脚本:

   $conn_status = ldap_connect('ldaps://ldap.domain.com/', 389);
    if ($conn_status === FALSE) {
        die("Couldn't connect to LDAP service");
    } else {
        echo "Successful! <br/>";
    }

这是Bind to LDAP的脚本:

    $app_user = 'cn=user, dc=domain, dc=com';
    $app_pass = 'password';

    $username = 'user'; //same as cn
    $password = 'password'; //same as $app_pass

    $bind_status = ldap_bind($conn_status, $app_user, $app_pass);
    if ($bind_status === FALSE) {
        die("Couldn't bind to LDAP as application user");
    } else {
        echo "Bind to LDAP successfully <br/>";
    }

我更新的LDAP绑定脚本

    $bind_status = ldap_bind($conn_status, $username, $password);
    if ($bind_status === FALSE) {
        //die("Couldn't bind to LDAP <br/>");
        echo "LDAP-Errno: " . ldap_errno($ds) . "<br />";
    } else {
        echo "Bind to LDAP successfully <br/>";
    }

现在我收到了这个错误:

    Warning: ldap_bind() [function.ldap-bind]: Unable to bind to server: Operations error on line 21

第21行是这样的:

    $bind_status = ldap_bind($conn_status, $username, $password);

当我使用

    var_dump (@ldap_bind($conn_status, "cn=Username, ou=domain, ou=com"));

结果是

    bool(false)

请帮我解决这个问题。谢谢

2 个答案:

答案 0 :(得分:1)

通常ldaps侦听端口636 / tcp,ldap监听端口389 / tcp starttls

$ldap_URI = "ldap://ldap.example.com/" ;
$ldap_bind_dn = "cn=myapplication,ou=service accounts,dc=example,dc=com" ;
$ldap_bind_dn_password = "hopefully something long and complicated" ;
$ldap_connection = ldap_connect($ldap_URI) ;
if(ldap_start_tls($ldap_connection)){
    if(!ldap_bind($ldap_connection,$ldap_bind_dn,$ldap_bind_dn_password)) ;
    //TODO: return/throw some error/exception here to be handled by caller, regarding invalid credentials
}else{
    ldap_close($ldap_connection);
    //TODO: return/throw some error/exception here to be handled by caller, regarding starttls failure
}
  • 通常检查全局ldap配置的TLS设置 /etc/openldap/ldap.conf/etc/ldap/ldap.conf
  • 如果您使用SELinux,请检查httpd_can_connect_ldap,即$ getsebool httpd_can_connect_ldap

此外:

  

当使用OpenLDAP 2.x.x时,ldap_connect()将始终返回资源,因为它实际上没有连接,只是初始化连接参数。实际连接发生在下一次调用ldap_ * funcs时,通常使用ldap_bind()。 - php manual

答案 1 :(得分:0)

在ldap_connect方法中,您指定了一个安全的ldap连接ldaps,但使用了389的标准端口。如果您尝试建立安全连接,请删除端口号,ldap_connect将找出正确的端口或使用端口636.否则,请使用端口号为389的ldap进行不安全连接。

要么

$conn_status = ldap_connect('ldap://ldap.domain.com/');

$conn_status = ldap_connect('ldap://ldap.domain.com/', 389);

OR

$conn_status = ldap_connect('ldaps://ldap.domain.com/');

$conn_status = ldap_connect('ldaps://ldap.domain.com/', 636);