Question

  Asked: Mar 12 2008   11:42 AM GMT
  Asked by: Constance.kan


Assigning new user default password with php


Default password, userPassword, unicodePwd, PHP, Active Directory

I've written some php codes (see codes below) to add a new user in our Active Directory.
I've attached the php codes below. It does actually add a new user and put in all the settings in attributes correctly.
However, even though I've set the userpassword (I can see the attribute set in ldap), the password is still blank when you first login.
If I set the unicodepwd attribute instead of userpassword, it generates error.
Any idea? Your advise will be much appreciated :)


// ****************
// Add new AD user
// ****************
$ldapconn = ldap_start();
$user["givenname"> = "Peter Alan";
$user["sn"> = "Pan";
$user["displayname"> = "Peter Pan";
$user["distinguishedname"> = "CN=PPan,OU=CKTest,DC=CK,DC=lan";
$user["homedirectory"> = '\\\sal\home$\%username%';
$user["homedrive"> = "h:";
$user["samaccountname"> = "PPan";
$user["profilepath"> = '\\\sal\profiles$\mandatory';
$user["objectcategory"> =
"CN=Person,CN=Schema,CN=Configuration,DC=CK,DC=lan";
$user['cn'] = "PPan";
$user["userprincipalname"> = $user["samaccountname">."@DOMAIN";
$user['objectclass'][0] = "top";
$user['objectclass'][1] = "person";
$user['objectclass'][2] = "organizationalPerson";
$user['objectclass'][3] = "user";
$user['mail'] = "ppan@DOMAIN";

user['userPassword'] ='pwd';
//user['userPassword'] ='{MD5}'.base64_encode(pack('H*',md5('password')));
// $user["unicodepwd"> = "{md5}".base64_encode(pack("H*",md5("password")));

$user["userAccountControl"> = "544";

print_r($user);

$dn = "CN=PPan,OU=CKTest,DC=CK,DC=lan";
$result = ldap_add($ldapconn, $dn, $user);

//assign user to AllStudents group
$group_name = "CN=AllStudents,OU=Groups,OU=Students,DC=CK,DC=lan";
$group_info['member'] = $dn; // User's DN is added to group's 'member'array

ldap_mod_add($ldapconn,$group_name,$group_info);

if ($result)
{
echo "User added!";
}
else
{ echo "There was a problem!";}
ldap_end($ldapconn);
}

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



I've not done this with PHP, but I have done this with VBSCRIPT and had a similar problem initially.

I see some pieces of code are in functions not provided (though I don't know they are needed), are you calling the .setinfo method after youe set the password?

Here is a Microsoft page that gives a short example of what I'm talking about. This was the solution to the problem when I had it, it revolves around basically having to create the account first --- then setting the password. Each time you midify something you need to setinfo.
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Microsoft Windows, Security and Development.

Looking for relevant Microsoft Windows Whitepapers? Visit the SearchWinIT.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register

Jlees  |   Mar 12 2008  1:43PM GMT

Be sure and check out my blog here on ITKE for more Network administration and VBSCRIPT tips.
The VBScript Network and Systems Administrator’s Cafe

 

Lelyea  |   Apr 4 2008  1:18PM GMT

I have 30+ hours in over two days attempting to add a user to AD (Server 2003 R2) using PHP and Openldap on a Red Hat 5 server. Your post showed me what I was doing wrong! I have it working now.

Sorry - I don’t know the answer to your password problem - haven’t gotten there yet. I do have a question though:
I would like all users added to AD with this script to only be members of the ‘Guest’ group, and not a member of the ‘Domain Users’ group. Can you tell me how to go about this??

Help from anyone would be greatly appreciated!

<a href="mailto:lelyea@yahoo.com">lelyea@yahoo.com</a>

Len