Current setup:
I've setup a server connection to my Active Directory system using TLS. Using the latest dev version of the LDAP module I am able to login to my Drupal site using Active Directory credentials.

Desired functionality:
The LDAP module seems to support that I can "Provide option on admin/people/create to create corresponding LDAP Entry." I've setup the attributes under "PROVISIONING FROM DRUPAL TO LDAP MAPPINGS:" correctly. Right now everything works about this except for saving the password. If I try to create a user in Drupal I get 53 - "Server is unwilling to perform". This is because I am trying to pass the Drupal password into the "unicodePwd" AD field. Something seems to be going wrong there. If I remove the password element from the provisioning scheme, then the user is created in Drupal and the AD just fine. But, obviously this isn't functional because that user in the AD doesn't have a password assigned.

From some research into this problem I think I need to do something like this to the password before it's sent to ldap_add as an attribute:

// create the unicode password 
$len = strlen($newPassword); 
$newPass = '"'; 
for ($i = 0; $i < $len; $i++) 
{ 
    $newPass .= "{$newPassword{$i}}\000"; 
} 
$newPass .= '"'; 

I've done a bit of research into the module, but can't exactly figure out where the "Pwd: User or Random" parsing happens. It seems like I could write a conditional that wraps the password string in the above code if the server type is Active Directory. Can anyone give me some pointers on where I should look at adding that code?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

johnbarclay’s picture

Status: Active » Closed (duplicate)

This is a duplicate of another current issue.

bryan kennedy’s picture

Could you let me know which issue this is a duplicate of? I did a bit of searching and even posted on another thread and was encouraged to open a new issue. Sorry for cluttering the queue, I just need some specific direction.

mallezie’s picture

Status: Closed (duplicate) » Active

I'm gonna reopen this issue. I encountered the same problem.
It's a very specific Active Directory 'change password' problem.
When mapping the password to the unicodePwd attribute. To make it possible to change your AD-password from Drupal, there are specific requirements. See http://stackoverflow.com/questions/10763070/ldap-mod-replace-function-ld...

Main reasons are

  1. you need to use an SSL connection (ldaps://)
  2. the password needs to be enclosed in quotes
  3. the (quoted) password needs to be encoded in 16-bit unicode (UTF-16LE)

This code above solves the last two issues.

i managed to get this working, by hacking this code in LdapServer.class.php more specific just before the ldap_modify call on line 564.
(This probably only works for account update, not account creation).
LdapServer.class.php line 564

if (count($attributes) > 0) {
      if(isset($attributes['unicodePwd'])){
        $newpassword = $attributes['unicodePwd'];
        $newpassword = "\"" . $newpassword . "\"";
        $len = strlen($newpassword);
        $newpass = '';
        for ($i = 0; $i < $len; $i++) {
          $newpass .= "{$newpassword{$i}}\000";
        }
        $attributes['unicodePwd'] = $newpass;
      }
      $result = @ldap_modify($this->connection, $dn, $attributes);
      if (!$result) {
        $error = "LDAP Server ldap_modify(%dn) in LdapServer::modifyLdapEntry() Error Server ID = %sid, LDAP Err No: %ldap_errno LDAP Err Message: %ldap_err2str ";
        $tokens = array('%dn' => $dn, '%sid' => $this->sid, '%ldap_errno' => ldap_errno($this->connection), '%ldap_err2str' => ldap_err2str(ldap_errno($this->connection)));
        watchdog('ldap_server', $error, $tokens, WATCHDOG_ERROR);
        return FALSE;
      }
    }

I assume this better belongs some way in the LdapTypeActiveDirectory.class.php class, but i have no idea, how to isolate this code there.

saford91’s picture

Issue tags: -unicode, -password
FileSize
1.62 KB

This solved my problem. Here are those changes made into a patch.

johnbarclay’s picture

The patch looks good. But there should be an option in the UI to specify this type of behavior or in the token system.

kenorb’s picture

Status: Active » Needs review
nullkernel’s picture

I have been working with the ldap module for the past week, and have made a patch to contribute back to the project. There are two problems which caused me lots of debugging, and I have both bugfixes in this patch. One being unicodePwd, the other being a bug I haven't seen reported ("Pwd: User Only" not being set with my site's registration form).

I can appreciate why johnbarclay suggests to have an option in the UI. However the code I wrote is very particular - it will only change the unicodePwd field, and only if the LDAP server type is set to Active Directory (which is a UI option). I can think of no use case in which this change would have an adverse impact.

The other change I made is adding a hook so that the function ldap_user_grab_password_validate($form, &$form_state) will be called if the FORM_ID is user-pass-reset (in addition to the previously defined hooks which call the function).

Also, I have noticed that there is a similar function that appears to be intended for Active Directory, but it is implemented in a different way and doesn't work for me. I am thinking it is defunct in general (and works for nobody). It is called "ldap_password_modify". I have left it alone, but I am thinking it probably should be deleted.

sin’s picture

#8 works. Thanks!

jean jack’s picture

#8 works good for AD 2012
Thanks

nullkernel’s picture

Thanks for the feedback guys. Although the patch is 2 years old, it still applies fine to my local clone of branch 7.x-2.x . Should the issue's status be changed to RTBC so that the patch may be committed?

mallezie’s picture

Johnbarclay (the maintainer) already replied, this should be configurable behaviour. (Which i fully agree). So if anyone could take care of that, i'm sure this could be merged. But it can't be committed as is, since this would break other backends.

nullkernel’s picture

But it can't be committed as is, since this would break other backends.

I think the patch I proposed would not break other backends. The patch restricts the behavior to Active Directory servers. From what I gather, Active Directory always needs the string to be in this format (and no other backend does). So I don't see any technical reason to add a configuration option. Or am I missing something?

grahl’s picture

Status: Needs review » Needs work
Issue tags: -active directory
grahl’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: ldap-activedirectory_unicodePwd-1954744-8.patch, failed testing.

nullkernel’s picture

Status: Needs work » Needs review

Updating to "Needs review" - I don't see any current testbot failures. Earlier testbot failures were caused by a composer require failure which is unrelated to this patch.

grahl’s picture

Version: 7.x-2.x-dev » 8.x-3.x-dev
Status: Needs review » Needs work

Looks good, committed.

I agree with nullkernel, this is type-specific and should not break other backends.

Moving this issue back to needs work for porting it to 8.x. Patches are welcome!

  • grahl committed 515fc0a on 7.x-2.x authored by nullkernel
    Issue #1954744 by nullkernel, saford91: LDAP User: Can't provision from...
caiobianchi’s picture

Any updates on the 8.x fix?
I'm having the same issue. :\

grahl’s picture

rawbubble’s picture

grahl,

thanks for this password patch. couldn't have been better timed for a project i'm working on.

only hiccup: any idea why the 8.x patch would fail?

Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/activedirectory-unicodepw-1954744-21.patch
grahl’s picture

@caiobianchi: Patch working?

  • grahl committed 689c266 on 8.x-3.x authored by mh.marouan
    Issue #1954744 by grahl, nullkernel, saford91, mh.marouan: LDAP User:...
grahl’s picture

Status: Needs review » Fixed

Thanks for the feedback, committed.

Status: Fixed » Closed (fixed)

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