Find us on facebook

Jan 31, 2018

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...";
 }
}


?>

No comments:

Post a Comment