Just spent a good 2 hours trying to get this all worked out... Figured I would post it just in case anyone else out there ever runs into the same issue.
Part of my app involves authenticating users via LDAP binding. Our organization requires that the LDAP connection be made over SSL (ldaps). I have a class that I wrote awhile back to do this that has always worked, but when I was trying to run my script it kept failing with the error that it couldnt connect to the ldap server. The same script works perfectly on the production server, and I could connect to the ldap server using my LDAP browser (Softerra) so I knew it had to be something with the php settings being used.
First thing that I noticed was that the default php.ini install didnt have either the LDAP or the ssl extensions loaded. So I had to change the file. Still no luck. I have
WinSSL Wrap installed and have used it for some other SSL-related issues. So I set up an SSL tunnel redirecting from 127.0.0.1 to our LDAP server. Success!!!!
So, I then knew it had something to do with the local php not knowing where a cert was to use in the communications. After some searching, I came across this post which does an excellent job of explaining on what to do in order to get it working :
http://greg.cathell.net/php_ldap_ssl.html.
Unless you are also running your own LDAP server, you can skip steps 4 and 5. You will need a certificate for step 6, so either use one you already have, or you can make your own using
openssl. If you download WinSSL wrap, it should have a batch file in it for walking you through the steps of creating your own pem cert. Otherwise, you will need to convert your cert to pem format, of which, you can use openssl to do the conversion.
Just in case the link to the instructions goes down, I will copy the main instructions here.
Italics indicates a change from the original document.
Quote: |
Here are the steps you will need to take with details following:
1. Configure PHP to load the LDAP and SSL modules (may require recompilation on UNIX/LINUX)
2. Copy DLL files to Windows system32 directory
3. Place ldap.conf file in C:\openldap\sysconf
4. Enable SSL over LDAP on Windows Domain Controller
5. Obtain certificate for AD server (.cer file)
6. Convert cert from .cer to .pem format
7. Install the certificate by referencing it in the ldap.conf file
- Configure PHP
In the php.ini file you need to have the LDAP and SSL modules loaded. Find the Windows Extensions section of the file and make sure you uncomment the following lines:
extension=php_ldap.dll
extension=php_openssl.dll
- Copy DLLs to System
Several DLL files need to be copied from the PHP installation to the Windows SYSTEM directory (C:\WINDOWS\SYSTEM32)
For PHP <= 4.2.0: Copy libsasl.dll to your SYSTEM directory
For PHP >= 4.3.0: Copy libeay32.dll and ssleay32.dll to your SYSTEM directory
- LDAP Config File
If it does not already exist you will need to create an LDAP configuration file where PHP will look for it. The file needs to reside in C:\openldap\sysconf\ldap.conf. As far as I know this is a hard coded location and is not changeable. Make sure the following directive line is in this file:
TLS_REQCERT never
I won't get into the specifics about what this does. You can find a reference on the directives of the ldap.conf file at: http://manpages.debian.net/cgi-bin/display_man.cgi?id=1764c345c61a6f62aac16e27fa3769f0&format=html
- Skipped
- Obtain your own cert
- Convert Certificate Format
To convert the certificate from .cer to .pem format I used OpenSSL. (You can obtain this software from here: http://gnuwin32.sourceforge.net/packages/openssl.htm if you don't already have it.)
Copy the certificate file you generated in the previous step to the machine on which PHP is running. Run the following command:
\openssl x509 -in -out
For example:
C:\openssl\openssl x509 -in myserver.cer -out myserver.pem
This creates the certificate file in a form that OpenLDAP can use.
- Install the Certificate
Place the .pem file generated in the previous step in a directory of your choosing (C:\openldap\sysconf may be a good choice since that directory already exists.)
Add the following line to your ldap.conf file:
TLS_CACERT
For example:
TLS_CACERT C:\openldap\sysconf\myserver.pem
This directive tells OpenLDAP where the certificate is so it can access it when needed.
That is pretty much it. At this point you should be able to change your code to access your LDAP server over SSL.
If you were doing something like this before:
<?php $conn = ldap_connect('myserver.mywebserver'); ?> |
then all you have to do now is use:
<?php $conn = ldap_connect('ldaps://myserver.mywebserver/'); ?> |
to connect securely over SSL. |