0

I am trying to create a new user in Active Directory using php by the following codes:

$server = "ldaps://172.16.0.2";
$user = 'someuser'.'@domain.com'; //admin user
$psw = "somepassword"; //admin password

echo "<h2>Mag dagdag ako ng bagong impormasyon</h2>";

ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);

$ds =ldap_connect($server,389); 

 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
 ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);

$r=ldap_bind($ds, $user, $psw); 


$info['cn']='trying.hard';
$info['sAMAccountName']='trying.hard';

$info['givenName']='trying';

$info['mail']='trying.hard@domain.com';
$info['sn']='hard';

$dn='OU=CSS,OU=ETC,OU=AdminStaff,OU=MainBldg,OU=CollegeStaff,OU=DoMain,DC=domain,DC=com ';  

$info['objectCategory'] = 'CN=Person,CN=Schema,CN=Configuration,DC=domain,DC=com ';

$info['objectClass'][0]= 'top';
$info['objectClass'][1]= 'person';
$info['objectClass'][2]= 'organizationalPerson';
$info['objectClass'][3]= 'user'; 

$info['useraccountcontrol'] = 66048;

        $newpass = "112345";        
        $newPassword = "\"" . $newpass . "\"";        
        $len = strlen($newPassword);
        $newPassw='';
        for ($i = 0; $i < $len; $i++)
        $newPassw .= "{$newPassword{$i}}\000";        
        $newPassword = $newPassw; 
        $info["unicodepwd"] = $newPassword;    

$a=ldap_add($ds, $dn, $info);

if($a){
    echo "Entry Succesfull!";
}else{
    echo '</br>##-Entry Failed - '. ldap_err2str(ldap_errno($ds)) .' -##';
}
?>

But I am getting the following error

 /*
    Warning: ldap_add(): Add: Server is unwilling to perform in /var/www/ADMS/adduser.php on line 88

    ##-Entry Failed - Server is unwilling to perform -##
    */

Can anyone help me out in this?

user207421
  • 305,947
  • 44
  • 307
  • 483
user7282
  • 5,106
  • 9
  • 41
  • 72

1 Answers1

0

Can you change this code:

 $newpass = "112345";        
        $newPassword = "\"" . $newpass . "\"";        
        $len = strlen($newPassword);
        $newPassw='';
        for ($i = 0; $i < $len; $i++)
        $newPassw .= "{$newPassword{$i}}\000";        
        $newPassword = $newPassw; 
        $info["unicodepwd"] = $newPassword; 

With this one:

$pwdtxt = "112345";
$newPassword = '"' . $pwdtxt . '"';
$newPass = iconv( 'UTF-8', 'UTF-16LE', $newPassword );
$info["unicodepwd"] = $newPassw;
Serhat Akay
  • 536
  • 3
  • 10
  • I am getting the error Warning: ldap_add(): Add: Already exists in /var/www/ADMS/adduser2.php on line 44 ##-Entry Failed - Already exists -## – user7282 Oct 05 '15 at 07:21
  • It seems like you passed the unwilling error. Can you try to add a different user? – Serhat Akay Oct 05 '15 at 10:52