I'm looking to create a web based application in PHP that receives LDAP requests and sends LDAP responses back, but doesn't actually use an LDAP server. Specifically I'd like to make the contacts table in a MySQL database available to Thunderbird as an LDAP address book.
Two Questions:
Is there an existing library for implementing an LDAP server with PHP? (The PHP_LDAP package is for creating an LDAP client, where the PHP application connects to an existing LDAP server.)
How does LDAP data actually get from the client into my script? Does LDAP travel over HTTP? Where the request would show up in:
$HTTP_RAW_POST_DATA
or similar? Can Apache handle LDAP requests and pass them into my script or is it a completely different protocol that requires a different "listener" application to handle?
Prerequisites: PHP LDAP extension, Working knowledge of PHP Many times in enterprise environments you already have an active directory server and all the users you would ever want to access something have an account there.
Add the database with the top level for the database People. Create a file named domain2_db.ldif When creating a new LDAP connection for the field Hostname use the IP address of your virtual machine. Define also any name for the connection and keep the default values for the other parameters In the wizard select to start from scratch.
Install and configure the LDAP account manager on Ubuntu. Step 1: Install the OpenLDAP server. First, you have to install and run the LDAP server. Step 2: Install Apache Webserver & PHP. You can install the PHP and Apache server by running the commands below.
// This is it. Be careful with types. PHP switches automatically between strings and numbers, but LDAP doesn't, and PHP will send whatever is most convenient for PHP, not LDAP, unless you specify a type. If you inadvertently send a number as a string, you will get an error: "ldap_add (): Add: Invalid syntax in [filename] on line LINENUM."
It's possible to create a pure PHP LDAP server with this library (I wrote it initially for LDAP client purposes):
https://github.com/FreeDSx/LDAP
It works on the basis of a request handler (just an interface) for client requests. Basically you extend a class that will handle client requests and send a response back (in the case of a search anyway). A basic example:
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',
])
);
}
}
use FreeDSx\Ldap\LdapServer;
use Foo\LdapRequestHandler;
$server = new LdapServer([ 'request_handler' => LdapRequestHandler::class ]);
$server->run();
There are more docs on the server component of the library here:
https://github.com/FreeDSx/LDAP/tree/master/docs/Server
A few caveats to this:
The LDAP protocol is not natively handled by Apache, and I've not seen any Apache modules that handle that protocol. I don't believe you'll be able to do it using PHP through Apache. You might be able to implement a pure PHP server (see http://php.net/manual/en/function.stream-socket-server.php) and then implement the LDAP protocol packet parser in PHP as well. I don't believe there is a native ASN1 parser for PHP, but you might be able to find one in C and somehow integrate it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With