|
<?php
|
|
/**
|
|
* test_openldap.php
|
|
* @author Quentin RIBAC for Maarch
|
|
* @since 23 March 2022
|
|
*
|
|
* Example usage:
|
|
*
|
|
* php ./test_openldap.php search '(uid=bbain)'
|
|
* php ./test_openldap.php bind 'uid=bbain,ou=People,dc=qri,dc=dev,dc=local'
|
|
*/
|
|
|
|
// set your connection here
|
|
$ldapURI = 'ldap://qri.dev.local:389';
|
|
$ldapBaseDN = 'dc=qri,dc=dev,dc=local';
|
|
$ldapAdminDN = 'cn=admin,dc=qri,dc=dev,dc=local';
|
|
|
|
//---
|
|
$conn = ldap_connect($ldapURI) or die('Unable to connect to ldap server at ' . $ldapURI . PHP_EOL);
|
|
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
|
|
|
|
echo 'Binding as: ' . $ldapAdminDN . PHP_EOL;
|
|
$adminPassword = trim(exec('env bash -c \'{ read -s -p "Admin password: " ; echo -n $REPLY ; }\''));
|
|
echo PHP_EOL . PHP_EOL;
|
|
ldap_bind($conn, $ldapAdminDN, $adminPassword) or die('Unable to bind to ldap server as ' . $ldapAdminDN . PHP_EOL);
|
|
|
|
$action = $argv[1];
|
|
if ($action === 'search') {
|
|
$query = $argv[2];
|
|
$result = ldap_search($conn, $ldapBaseDN, $query);
|
|
for ($entry = ldap_first_entry($conn, $result); !empty($entry); $entry = ldap_next_entry($conn, $entry)) {
|
|
echo ldap_get_dn($conn, $entry) . PHP_EOL;
|
|
}
|
|
} elseif ($action === 'bind' && !empty($argv[2])) {
|
|
$dn = $argv[2];
|
|
echo 'Binding as: ' . $dn . PHP_EOL;
|
|
$userPassword = trim(exec('env bash -c \'{ read -s -p "User password: " ; echo -n $REPLY ; }\''));
|
|
echo PHP_EOL . PHP_EOL;
|
|
$result = ldap_bind($conn, $dn, $userPassword);
|
|
unset($userPassword);
|
|
echo ($result ? 'Success' : 'Failure') . PHP_EOL;
|
|
}
|