简体   繁体   English

PHP - 带有 SSL 的 LDAP 无法绑定

[英]PHP - LDAP with SSL fail to bind

I have PHP 7.0 on CentOS 7. And I've installed php-ldap module as well.我在 CentOS 7 上有 PHP 7.0。我也安装了php-ldap模块。

# yum install -y php php-ldap
...
# php -m
...
ldap
...

Now the following PHP codes works:现在以下 PHP 代码可以工作:

<?php
$ldapconn = ldap_connect("dc.example.com", 389) or die("Could not connect to LDAP server.");
    
if ($ldapconn) {
    $ldaprdn  = 'username';
    $ldappass = 'password';

    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
    
    if ($ldapbind) {
        echo "LDAP bind successful...";
    } else {
        echo "LDAP bind failed...";
    }
}

$Result = ldap_search($ldapconn, "DC=example,DC=com", "(sAMAccountName=johndoe)");
$data = ldap_get_entries($ldapconn, $Result);

print_r($data);
?>

That works!这样可行! I can connect, bind, and then even search for username johndoe and view his entire AD profile successfully.我可以连接、绑定,甚至搜索用户名johndoe并成功查看他的整个 AD 配置文件。

Problem问题

But then I tried with SSL via port 636 :但后来我尝试通过端口636使用 SSL:

<?php
putenv('LDAPTLS_REQCERT=require');
putenv('LDAPTLS_CACERT=/var/www/html/servercert.der'); #I know, but this is just temporary location
$ldapconn = ldap_connect("dc.example.com", 636) or die("Could not connect to LDAP server.");

ldap_set_option($ldapconn, LDAP_OPT_DEBUG_LEVEL, 7);
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

if ($ldapconn) {
    $ldaprdn  = 'username';
    $ldappass = 'password';

    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
    
    if ($ldapbind) {
        echo "LDAP bind successful...";
    } else {
        echo "LDAP bind failed...";
    }
}

$Result = ldap_search($ldapconn, "DC=example,DC=com", "(sAMAccountName=johndoe)");
$data = ldap_get_entries($ldapconn, $Result);

print_r($data);
?>

I got this error:我收到了这个错误:

Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server in /var/www/html/index.php on line 14
LDAP bind failed...
Warning: ldap_search(): Search: Can't contact LDAP server in......

What am I missing please?请问我错过了什么?

Note:笔记:

  1. We have port 636 opened on Windows AD Server and it is reachable from this PHP web server.我们在 Windows AD 服务器上打开了 636 端口,可以从这个 PHP Web 服务器访问它。
  2. Server certificate is valid.服务器证书有效。

I figured out the ldap_connect should be as below:我发现ldap_connect应该如下所示:

ldap_connect("ldaps://dc.example.com:636")

And then all of sudden it worked!然后突然之间它起作用了!

Note: If it is on Apache, it is worth restarting it after changing to above code.注意:如果它在 Apache 上,则值得在更改为上述代码后重新启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM