【问题标题】:PHP - LDAP search working sporadically over TLS/SSLPHP - LDAP 搜索偶尔通过 TLS/SSL 工作
【发布时间】:2018-12-05 14:05:17
【问题描述】:

下面的代码在我的 LDAP 服务器中执行所有 OrganizationalUnits 的查找,但是在 40% 的情况下它无法执行 LDAP 搜索。

我唯一的线索是下面的两个 Apache 日志条目。我不是 PHP 向导,但假设第二条错误消息是由于连接失败导致空的 $sr 变量引起的。

我在连接期间运行了tcpdump,PHP 正在连接到服务器,但在失败期间进行的通信非常少——只有大约 1/2 的数据包传输反对成功的连接。

这似乎只发生在 TLS/SSL 上(因此是 putenv last-ditch-effort)。如果我使用明文,搜索每次都能完美运行。什么会导致这“有时”不起作用?有什么方法可以了解更多信息吗?

更新:我刚刚注意到这段代码在不使用 LDAPS/TLS 的情况下 100% 有效,因此在某种程度上肯定与 SSL/TLS 相关。

<?php
   putenv('TLS_REQCERT=never');
   print "<html><head><title>ldap test</title></head><body>";

   $ldapconn = ldap_connect("ldaps://my.ldap.com") or die("Could not connect to LDAP server.");
   ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);

   if ($ldapconn)
   {
      $basedn = "dc=my,dc=ldap,dc=com";
      $attributes = array("ou","cn");
      $sr = ldap_search ($ldapconn, $basedn, "(ObjectClass=OrganizationalUnit)", $attributes);
      $info = ldap_get_entries($ldapconn, $sr);
   }

   if ($info["count"] > 0)
   {
      for ($i=0; $i < $info["count"]; $i++)
      {
         $ou = $info[$i]["ou"][0];
         print "<br><input type='radio' name='ldap_ou' value='$ou'>$ou<br>";
      }
   }

   print "</body></html>";

?>

Apache 错误:

PHP Warning:  ldap_search(): Search: Can't contact LDAP server in /var/www/test.php on line 14
PHP Warning:  ldap_get_entries() expects parameter 2 to be resource, boolean given in /var/www/test.php on line 15

更新 2(调试失败信息):

...
ldap_prepare_socket: 18
ldap_connect_to_host: Trying 10.14.13.92:636
ldap_pvt_connect: fd: 18 tm: -1 async: 0
TLS: peer cert untrusted or revoked (0x42)
TLS: can't connect: (unknown error code).
ldap_err2string
....

【问题讨论】:

  • 在 ldap_search 之后尝试过 ldap_error 吗? php.net/manual/de/function.ldap-error.php
  • ldap 在 php 中工作正常吗?确保,可能会帮助serverfault.com/a/452940/322998
  • @FatFreddy - 是的,马上就试过了,它说的是Success 在ldap_connect 之后(即使它失败了)和Can't contact LDAP server 在搜索之后。
  • @DiogoAlves - 是的,ldapsearch -x -ZZ 每次都从命令行工作
  • 我会在手动运行的代码副本上做一些事情——你可以启用调试标志,使错误报告更加详细 (php.net/manual/en/function.ldap-set-option.php) ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);从命令行(php filename.php)运行脚本,你会看到很多输出。

标签: php linux apache ldap


【解决方案1】:

我也有同样的问题。 LDAP 服务器需要 TLS,所以我们必须使用 ldaps 连接到服务器。 ldap_connect 和 ldap_bind 每次都有效。但以下 ldap_search 偶尔会因 Can't contact LDAP Server (-1) 错误而失败。

在 LDAP 端,我可以在我的 ldap 服务器上看到一个有效的绑定请求,但仅此而已 -.-

我正在使用 CentOS 和 php7.2,有没有人遇到同样的问题或任何解决方案?


好的,我改用http://php.net/manual/en/function.ldap-start-tls.php 解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多