Search code examples
phpopenldap

openldap auththentication php5 - comparing the password


I'm trying to authenticate againest an ldap server I've created. The user password is stored within ldap as sha hash. However when I try accessing the userPassword attribute it doesn't exist. I have to login as the manager. Am assuming this isn't a very good idea on a production server. is there a way around this?


Solution

  • It does not make sense to return the password field. You may simply authenticate against the ldap server with given credentials.

    see this example (reference here: http://php.net/manual/en/ref.ldap.php)

    <?php
    
    $user = 'bob';
    $password = 'zhlob';
    $host = 'myldap';
    $domain = 'mydomain.ex';
    $basedn = 'dc=mydomain,dc=ex';
    $group = 'SomeGroup';
    
    $ad = ldap_connect("ldap://{$host}.{$domain}") or die('Could not connect to LDAP server.');
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
    @ldap_bind($ad, "{$user}@{$domain}", $password) or die('Could not bind to AD.');
    $userdn = getDN($ad, $user, $basedn);
    if (checkGroupEx($ad, $userdn, getDN($ad, $group, $basedn))) {
    //if (checkGroup($ad, $userdn, getDN($ad, $group, $basedn))) {
        echo "You're authorized as ".getCN($userdn);
    } else {
        echo 'Authorization failed';
    }
    ldap_unbind($ad);
    ?>