使用 PHP 创建 LDAP 服务器


Creating an LDAP Server With PHP

我希望在PHP中创建一个基于Web的应用程序,该应用程序接收LDAP请求并发送LDAP响应,但实际上并不使用LDAP服务器。具体来说,我想让 MySQL 数据库中的联系人表作为 LDAP 地址簿提供给 Thunderbird。

两个问题:

  1. 是否有用于使用 PHP 实现 LDAP 服务器的现有库?(PHP_LDAP包用于创建 LDAP 客户端,PHP 应用程序在其中连接到现有的 LDAP 服务器。

  2. LDAP 数据如何实际从客户端进入我的脚本?LDAP是否通过HTTP传输?请求将显示在以下位置:

    $HTTP_RAW_POST_DATA
    

或类似?Apache 可以处理 LDAP 请求并将它们传递到我的脚本中,还是需要不同的"侦听器"应用程序来处理的完全不同的协议?

可以使用

此库创建一个纯 PHP LDAP 服务器(我最初编写它是为了 LDAP 客户端目的):

https://github.com/FreeDSx/LDAP

它基于客户端请求的请求处理程序(只是一个接口)工作。基本上,您扩展了一个将处理客户端请求并发送回响应的类(无论如何在搜索的情况下)。一个基本的例子:

    创建一个请求处理程序,
  1. 以扩展库中的通用请求处理程序:
namespace Foo;
use FreeDSx'Ldap'Server'RequestHandler'GenericRequestHandler;
class LdapRequestHandler extends GenericRequestHandler
{
    /**
     * @var array
     */
    protected $users = [
        'user' => '12345',
    ];
    /**
     * Validates the username/password of a simple bind request
     *
     * @param string $username
     * @param string $password
     * @return bool
     */
    public function bind(string $username, string $password): bool
    {
        return isset($this->users[$username]) && $this->users[$username] === $password;
    }
    /**
     * Override the search request. This must send back an entries object.
     *
     * @param RequestContext $context
     * @param SearchRequest $search
     * @return Entries
     */
    public function search(RequestContext $context, SearchRequest $search): Entries
    {
        // Do your logic here with the search request, return entries...
        return new Entries(
            Entry::create('cn=Foo,dc=FreeDSx,dc=local', [
                'cn' => 'Foo',
                'sn' => 'Bar',
                'givenName' => 'Foo',
            ]),
            Entry::create('cn=Chad,dc=FreeDSx,dc=local', [
                'cn' => 'Chad',
                'sn' => 'Sikorra',
                'givenName' => 'Chad',
            ])
        );
    }
}
  1. 使用请求处理程序,创建一个在端口 389 上侦听客户端的 LDAP 服务器进程:
use FreeDSx'Ldap'LdapServer;
use Foo'LdapRequestHandler;
$server = new LdapServer([ 'request_handler' => LdapRequestHandler::class ]);
$server->run();

这里有更多关于库服务器组件的文档:

https://github.com/FreeDSx/LDAP/tree/master/docs/Server

对此有一些警告:

  • 目前没有对服务器的分页/vlv 支持
  • 目前无法将请求处理程序中的控件返回到客户端。
LDAP 协议

不是由 Apache 原生处理的,我还没有看到任何处理该协议的 Apache 模块。我不相信你能通过Apache使用PHP来做到这一点。您也许能够实现纯 PHP 服务器(请参阅 http://php.net/manual/en/function.stream-socket-server.php),然后在 PHP 中实现 LDAP 协议数据包解析器。我不相信有用于PHP的本机ASN1解析器,但是您可以在C中找到一个并以某种方式集成它。

不久前,我与一位非常聪明的开发人员合作,他说他在PHP中实现了有效的LDAP客户端/服务器。他在麻省理工学院的许可下在这里发表了它:https://code.google.com/p/ldap-php/。

我不知道这是什么状态。