Find us on facebook

Showing posts with label LDAP. Show all posts
Showing posts with label LDAP. Show all posts

Jan 31, 2018

LDAP Simple bind in PHP

<?php
$ldaphost = "ldap.xxx.yyy"; 
$ldapport = 389; 

$ds = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost"); 
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
if ($ds) { 
 $username = "test"; 
 $upasswd = "password"; 
 $binddn = "cn=admin,dc=xxx,dc=yyy"; 
 $binddnt = "ou=mathematicians,dc=xxx,dc=yyy"; 
 $ldapbind = ldap_bind($ds,$binddn, $upasswd); 

 //check if ldap was sucessfull 
 if ($ldapbind) {
    $filter = "(uniqueMember=*)";
    $result = ldap_search($ds, $binddnt, $filter) or exit("Unable to search LDAP server");
 $entries = ldap_get_entries($ds, $result);
 var_dump($entries);
 } else {
    echo "LDAP bind failed...";
 }
}


?>

ldap anonymous bind with PHP - example

<?php
$ldaphost = "ldap.yyy.xx";
$ldapport = 389;

$ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
if ($ldapconn) {
 echo "LDAP Connect success...";
 $username = "aaaa";
 $password = "aaa@aa";
 $basedn = "dc=yyy,dc=xx";
 $ldapbind = ldap_bind($ldapconn);

 //check if ldap was sucessfull
 if ($ldapbind) {
    // Search for user
    //$result = ldap_search($ldapconn, $basedn, "uid=$username");
    //$entries = ldap_get_entries($ldapconn, $result);
    //var_dump($entries);exit;
    if(($res_id = ldap_search($ldapconn, $basedn, "uid=$username"))===false){
        var_dump('LDAP Auth: User '.$username.' not found in search');
    }
    if(ldap_count_entries($ldapconn, $res_id)!==1){
        var_dump('LDAP Auth: Failure, username '.$username.'found more than once');
    }
    if(($entry_id = ldap_first_entry($ldapconn, $res_id))===false){
        var_dump('LDAP Auth: Failure, entry of search result could not be fetched');
    }
    if(($user_dn = ldap_get_dn($ldapconn, $entry_id))===false){
        var_dump('LDAP Auth: Failure, user-dn could not be fetched');
    }
    if(($link_id = ldap_bind($ldapconn, $user_dn, $password))===false){
        var_dump('LDAP Auth: Failure, username/password did not match: ' . $user_dn);
    }
    var_dump('LDAP Auth: Success '.$user_dn.' authenticated successfully');
    ldap_close($ldapconn);
   
    exit;

 } else {
    echo "LDAP bind failed...";
 }
}


?>