【问题标题】:PHP Connection to LDAP fails randomly until Apache restartPHP 与 LDAP 的连接随机失败,直到 Apache 重新启动
【发布时间】:2014-05-14 14:27:42
【问题描述】:

我有一个 PHP 应用程序,它通过 ldap 对 Active Directory 用户进行身份验证,但是周期性地,ldap 连接显然会失败,直到 Apache 重新启动。一旦处于这种“失败状态”,在重新启动之前,任何人都无法登录。

这种情况有时会在一天内发生 3 或 4 次,有时会持续数天或数周而不会出现问题。我无法确定触发问题的任何具体情况。

我尝试使用主机名和 IP 地址进行连接,但两者都会出现这种情况。

错误号返回110,是连接超时。

Apache/2.4.6 (Unix)

PHP/5.4.21

OpenLDAP 版本 20437

ldap.max_links 无限制

<?php
$username= "myuser";
$host    = "192.168.0.38";
$user_dn = "DOMAIN\\$username";
$base_dn = "OU=Organization,DC=corp,DC=domain,DC=ch";
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); //to get debug info
if ($ldap_handle = ldap_connect($host)) {
    $bind_result = ldap_bind($ldap_handle, $user_dn, $password);
    if ($bind_result) { 
        $filter    = "(samaccountname=$username)";
        $justthese = array("givenname","sn","displayname","mail","st","samaccountname","telephonenumber");
            $sr        = ldap_search($ldap_handle, $base_dn, $filter,$justthese);
            $info      = ldap_get_entries($ldap_handle, $sr);
            ldap_close(); //edit as per comment below
            return $info;
        } else {
            $error=ldap_error($ldap_handle);
            ldap_close(); //edit as per comment below
            return array("error"=>$error);
        }
    } else {
        $error=ldap_error($ldap_handle);
        ldap_close(); //edit as per comment below
        return array("error"=>$error);
    }
?>

Apache 日志包含:

** ld 0x7f82f81d92e0 未完成的请求: * msgid 2, origid 2, 状态 InProgress 未完成推荐 0,父计数 0 ld 0x7f82f81d92e0 请求计数 1(放弃 0) ** ld 0x7f82f81d92e0 响应队列: * msgid 2,输入 100 ld 0x7f82f81d92e0 响应计数 1 ldap_chkResponseList ld 0x7f82f81d92e0 msgid 2 全部 1 ldap_chkResponseList 返回 ld 0x7f82f81d92e0 NULL ldap_int_select read1msg: ld 0x7f82f81d92e0 msgid 2 全部 1 read1msg: ld 0x7f82f81d92e0 msgid 2 消息类型搜索结果 read1msg: ld 0x7f82f81d92e0 0 新推荐 read1msg:标记请求完成,ld 0x7f82f81d92e0 msgid 2 请求完成:ld 0x7f82f81d92e0 msgid 2 res_errno: 0, res_error: , res_matched: ldap_free_request (origid 2, msgid 2) 添加响应 ld 0x7f82f81d92e0 msgid 2 类型 101: ldap_parse_result ldap_first_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_values_len ldap_next_attribute ldap_get_dn ldap_free_connection 1 1 ldap_send_unbind ldap_free_connection:实际释放 ldap_msgfree ldap_create ldap_bind_s ldap_simple_bind_s ldap_sasl_bind_s ldap_sasl_bind ldap_send_initial_request ldap_new_connection 1 1 0 ldap_int_open_connection ldap_connect_to_host:TCP 192.168.0.38:389 ldap_new_socket:52 ldap_prepare_socket:52 ldap_connect_to_host:正在尝试 192.168.0.38:389 ldap_pvt_connect:fd:52 tm:-1 异步:0 尝试连接: ldap_create ldap_bind_s ldap_simple_bind_s ldap_sasl_bind_s ldap_sasl_bind ldap_send_initial_request ldap_new_connection 1 1 0 ldap_int_open_connection ldap_connect_to_host:TCP 192.168.0.38:389 ldap_new_socket:56 ldap_prepare_socket:56 ldap_connect_to_host:正在尝试 192.168.0.38:389 ldap_pvt_connect:fd:56 tm:-1 异步:0 尝试连接: ldap_create ldap_bind_s ldap_simple_bind_s ldap_sasl_bind_s ldap_sasl_bind ldap_send_initial_request ldap_new_connection 1 1 0 ldap_int_open_connection ldap_connect_to_host:TCP 192.168.0.38:389 ldap_new_socket:54 ldap_prepare_socket:54 ldap_connect_to_host:正在尝试 192.168.0.38:389 ldap_pvt_connect:fd:54 tm:-1 异步:0 尝试连接: 连接错误号:110 ldap_close_socket:52 ldap_err2string ldap_err2string ldap_create ldap_bind_s ldap_simple_bind_s ldap_sasl_bind_s ldap_sasl_bind ldap_send_initial_request ldap_new_connection 1 1 0 ldap_int_open_connection ldap_connect_to_host:TCP 192.168.0.38:389 ldap_new_socket:58 ldap_prepare_socket:58 ldap_connect_to_host:正在尝试 192.168.0.38:389 ldap_pvt_connect:fd:58 tm:-1 异步:0 尝试连接: 连接错误号:110 ldap_close_socket:56 ldap_err2string ldap_err2string ldap_create ldap_bind_s ldap_simple_bind_s ldap_sasl_bind_s ldap_sasl_bind ldap_send_initial_request ldap_new_connection 1 1 0 ldap_int_open_connection ldap_connect_to_host:TCP 192.168.0.38:389 ldap_new_socket:33 ldap_prepare_socket:33 ldap_connect_to_host:正在尝试 192.168.0.38:389 ldap_pvt_connect:fd:33 tm:-1 异步:0 尝试连接: 连接错误号:110 ldap_close_socket:54 ldap_err2string ldap_err2string ldap_create ldap_bind_s ldap_simple_bind_s ldap_sasl_bind_s ldap_sasl_bind ldap_send_initial_request ldap_new_connection 1 1 0 ldap_int_open_connection ldap_connect_to_host:TCP 192.168.0.38:389 ldap_new_socket:52 ldap_prepare_socket:52 ldap_connect_to_host:正在尝试 192.168.0.38:389 ldap_pvt_connect:fd:52 tm:-1 异步:0 尝试连接: 连接错误号:110 ldap_close_socket:58 ldap_err2string ldap_err2string 连接成功 ldap_open_defconn:成功 ldap_send_server_request ldap_result ld 0x7f82f8164f00 msgid 1 wait4msg ld 0x7f82f8164f00 msgid 1(无限超时) wait4msg 继续 ld 0x7f82f8164f00 msgid 1 全部 1 ** ld 0x7f82f8164f00 连接: * 主机:192.168.0.38 端口:389(默认) refcnt:2 状态:已连接 最后使用时间:2014 年 5 月 14 日星期三 15:50:27

** ld 0x7f82f8164f00 未完成的请求: * msgid 1, origid 1, 状态 InProgress 未完成推荐 0,父计数 0 ld 0x7f82f8164f00 请求计数 1(放弃 0) ** ld 0x7f82f8164f00 响应队列: 空的 ld 0x7f82f8164f00 响应计数 0 ldap_chkResponseList ld 0x7f82f8164f00 msgid 1 全部 1 ldap_chkResponseList 返回 ld 0x7f82f8164f00 NULL ldap_int_select read1msg: ld 0x7f82f8164f00 msgid 1 全部 1 read1msg: ld 0x7f82f8164f00 msgid 1 消息类型绑定 read1msg: ld 0x7f82f8164f00 0 新推荐 read1msg:标记请求完成,ld 0x7f82f8164f00 msgid 1 请求完成:ld 0x7f82f8164f00 msgid 1 res_errno: 0, res_error: , res_matched: ldap_free_request (origid 1, msgid 1) ldap_parse_result ldap_msgfree ldap_search put_filter: "(samaccountname=stfra)" put_filter:简单 put_simple_filter: "samaccountname=stfra" ldap_build_search_req ATTRS: givenname sn displayname mail st samaccountname telephonenumber l c st postalcode physicaldeliveryofficename streetaddress title facsimiletelephonenumber ldap_send_initial_request ldap_send_server_request ldap_result ld 0x7f82f8164f00 msgid 2 wait4msg ld 0x7f82f8164f00 msgid 2(无限超时) wait4msg 继续 ld 0x7f82f8164f00 msgid 2 全部 1 ** ld 0x7f82f8164f00 连接: * 主机:192.168.0.38 端口:389(默认) refcnt:2 状态:已连接 最后使用时间:2014 年 5 月 14 日星期三 15:50:27

【问题讨论】:

  • 您要关闭所有脚本的连接吗?
  • 我同意@kwolfe,请尝试致电ldap_close();,这样您就不会留下孤立的开放连接。
  • @ThomasEllis 我试过这个(上面更新了代码)但没有用。

标签: php apache ldap openldap


【解决方案1】:

听起来你没有关闭连接。 使用延迟初始化将 ldap_connect 包装到类 并将连接关闭命令放入析构函数

【讨论】:

  • 我会试试看是否有帮助。看起来很有可能。
  • 我在上面的所有“return”语句之前添加了 ldap_close() 但是我仍然遇到这个问题。
猜你喜欢
  • 2016-04-02
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
相关资源
最近更新 更多