We have an intranet set up using LDAP authentication against sAMAccountName and it works fine. People enter their (up to) 7-character username (first initial+last name) and password and it lets them in. If they've never logged in before, it creates a Drupal account for them. The problem comes when someone changes his or her name. When ASmith logs in as AJones for the first time, it creates the AJones account as a completely new account. I've created several tools that hinge on the Drupal users.uid value, and obviously this invalidates those relationships so I have go into my database and update the id numbers. Is there some way to force this recognition of one sAMAccountName as the old one? One thing we have done is to add the objectGUID for each AD account to the Drupal profile, so it stores them locally in Drupal. I think I could write something to either manage those UID updates manually or use the relationship of UID to GUID as the pivot point for the tools I made. But I'm wondering if there's something else that already exists that would handle this. I haven't been able to find anything on point and I can't believe this hasn't come up a million times. Or do people just not change sAMAccountNames?

Comments

johnbarclay’s picture

I haven't seen anything to do this. This has come up in both ldap_integration for 6 and ldap for drupal 7.

In the d7 ldap module, we are addressing it by specifying a true unique identifier (such as objGuid) field in the configuration. Then on account creation it is stored. Then when uid, samaccountname, etc. change the user will retain the same account. See #986806: LDAP User: Use Case: ldap attribute for deriving email or username is not unique, unalterable user attribute.

To work this in in your scenario, it would be hard to do without modifying ldap_auth. The way it creates the account is difficult to hook into. But I don't think there is much harm in hacking this module as there don't seem to be any patches or patch reviews coming for it.

cgmonroe’s picture

I have seen this same issue in the "Bakery" SSO code. One "trick" they use is to re-define the "init" field in the User table to be a unique GUID. This is possible because the original purpose of the init fields was to try to use the initial e-mail as a unique id... but since e-mails get abandoned/re-used and this can lead to a security issue, this field has been dropped from use by core.

So, a strategy to deal with this would be to:

Pick an attribute that you can either set to a GUID or one you think will remain constant (e.g. AD's objectGUID) and use this to definitively map the LDAP object to the Drupal user init value. NOTE: Remember that if you reload an LDAP database from LDIF files, the automatic GUIDs WILL CHANGE.. so IMHO it's better to control this via drupal code than depend on LDAP/AD.. but YMMV.

To create the initial LDAP to Drupal GUID pairing, you can use the hook_ldapauth_create hook.

However, the missing piece in ldapauth is that it tries to load the account before authenticating and there is currently no way to modify this after the ldap user is fully authenticated.

I'm thinking the best way to allow this is to put in an 'ldapauth_account_alter' hook in the ldap_authenticate method between the _ldapauth_auth call and the user creation code. This would allow external modules to use an alternative way to match Drupal users to LDAP objects. E.g.:

  // Authenticate LDAP user.
  if (!($dn = _ldapauth_auth($name, $pass)))
    return;

  // Allow external modules to do additional Drupal user to LDAP object mapping (e.g. handle name changes)
  drupal_alter('ldapauth_account', $account, $dn); 

  if (!$account) {
    if (!variable_get('ldapauth_create_users', TRUE)) {
      form_set_error('name', t('Your account is not authorized to access this system.'));
      watchdog('ldapauth', 'The valid LDAP account %name was denied access because there was no matching Drupal account.', array('%name' => $name), WATCHDOG_ERROR);
      return;
    }
    // Register this new user.
    if ($ldap_user = _ldapauth_user_lookup($name)) {

With this in place, the some psuedo code for the external module would be like:

mymodule_ldapauth_account_alter( &$account, $dn) {
    //load the LDAP object using dn
    if ( $account && $account->init == $ldap[$guid_attr] ) {
       return;  // same so just move along
    }
    if ( ! $account ) { 
      $a = // select * from users where init = '$ldap[$guid_attr]
      if ( $a ) { // found a drupal id that matches the ldap_object... name changed
          // update and save user with new info from ldap
          $account  = $a;
     }
}

Ideally, this could be optimized by changing the _ldapauth_auth function to return the full LDAP entry and not just the dn. This way the second lookup would not have to be done.

Thoughts?

cgmonroe’s picture

Status: Active » Fixed

Marking as fixed since the just commited dev version now has support of Persistent Unique IDs which will prevent this. For AD, you can use the objectGUID attribute for your PUID. Just mark it as a binary PUID in the admin screen.

See Comment #10 of #1475272: 6.x-1.0 Release Candidate 1 Status

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.