Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * PHP-LDAP CONNECTION TEST
- * This is test script to check if LDAP authentication connection is up.
- * The algio here just to show how-to, in production these best in the class setup.
- *
- * Minimum:
- * - PHP 5.5
- * - PHP LDAP Lib
- *
- * Author: [email protected]
- *
- * Futher Readings;
- * - [LDAP Port|https://technet.microsoft.com/en-us/library/dd772723(v=ws.10).aspx]
- */
- // show all error
- error_reporting(E_ALL);
- // just a method to print status & terminate this script
- function logHere($type, $msg = FALSE) {
- $fatal = FALSE;
- if ($fatal = ($msg === FALSE)) {
- $msg = $type;
- $type = 'FATAL';
- }
- echo "{$type}:{$msg}" . PHP_EOL;
- if ($fatal) {
- exit;
- }
- }
- /**
- * CHECK PHP
- * - Do these once only; to check if enviroment have those lib.
- */
- if (
- (!extension_loaded('ldap')) ||
- (!function_exists('ldap_connect')) ||
- (!function_exists('ldap_set_option')) ||
- (!function_exists('ldap_search')) ||
- (!function_exists('ldap_get_entries')) ||
- (!defined('LDAP_OPT_PROTOCOL_VERSION')) ||
- (!defined('LDAP_OPT_REFERRALS')) ||
- (!defined('LDAP_OPT_NETWORK_TIMEOUT'))
- ) {
- // logHere & exit
- logHere('ldap.php-failed-ldap');
- }
- /**
- * CHECK HOST LOCATION
- * - Do this once only; to check if IP & port welcome Authentication API.
- */
- $host = 'xxx.xxx.xxx.xxx'; // <-- CONFIG HERE
- // port - these are basic selections suggested by Microsoft TechNet - dd772723.
- // - however each organization might have different setup for security reason.
- // $port = 389; // normal - 3268 GC
- // $port = 636; // secure - 3269 GC
- $port = 88; // Kerberos - User / Comp Autehntication
- // $port = 53; // DNS - User / Comp Autehntication
- $wait = 5;
- $eCode = $eStr = '';
- if (!($fp = @fsockopen($host, $port, $eCode, $eStr, $wait))) {
- // logHere & exit
- logHere('error', 'ldap.error-code:' . $eCode);
- logHere('error', 'ldap.error-msg:' . $eStr);
- logHere('ldap.hello-failed');
- }
- fclose($fp);
- logHere('info', 'ldap-fsockopen-success');
- /**
- * CHECK LDAP CONNECTION
- * - Check if Host can be connected with PHP Driver.
- */
- $con = ldap_connect($host);
- if (empty($con)) {
- // logHere & exit
- logHere('ldap.connection-failed');
- }
- logHere('info', 'ldap-connection-ok');
- /**
- * SETUP LDAP CONTROLS
- * - Basic PHP-LDAP options; as needed.
- */
- ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);
- ldap_set_option($con, LDAP_OPT_REFERRALS, 0);
- ldap_set_option($con, LDAP_OPT_NETWORK_TIMEOUT, 10);
- /**
- * BINDING USER
- * - Authenticate User / Computer via LDAP
- */
- $domain = 'XXXX'; // <-- CONFIG HERE
- $username = 'XXX.XXX.XXX'; // <-- CONFIG HERE
- $password = 'xXxXxXxXxX'; // <-- CONFIG HERE
- $ldaprdn = "{$domain}\\{$username}";
- $bind = @ldap_bind($con, $ldaprdn, $password);
- if (empty($bind)) {
- // get more error details
- $extended_error = ': Unknown issue.';
- $diagMsg = 0x0032; // LDAP_OPT_ERROR_STRING - if version issue (octal)
- ldap_get_option($con, $diagMsg, $extended_error);
- // logHere & exit
- logHere('error', $extended_error);
- logHere('ldap.binding-failed');
- }
- logHere('info', 'ldap-binding-ok');
- logHere('info', 'ldap-authentication-ok');
- // all ok
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement