samaniego
samaniego

Reputation: 419

add users in microsoft active directory via php ldap_add

I have tried adding users via php ldap Active-Directory for Microsoft Server 2008 R2 datacenter, but I can't. I always get this error :

An error occurred. Error number 64: Naming violation

The code is:

<?php 
$ldaprdn = '[email protected]'; 
$ldappass = 'dir378prob@'; 
$ds = 'correo.mx'; 
$dn = 'ou=usuarios,dc=correo,dc=mx'; 
$puertoldap = 389; 
$ldapconn = ldap_connect($ds,$puertoldap)or die("ERROR: I Don'n connect to LDAP."); 

if ($ldapconn) 
{ 
  ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION,3); 
  ldap_set_option($ldapconn, LDAP_OPT_REFERRALS,0); 
  $con = ldap_bind($ldapconn, $ldaprdn, $ldappass); 
  if ($con) 
  {                 

    $info["cn"] = $_POST['cn'];
    $info["sn"] = $_POST['sn'];
    $info["mail"] = $_POST['mail'];
    $info["objectclass"] = "inetorgperson";

    // prepare DN for new entry
    $dn_aux = "mail=" . $_POST['mail'] . ",ou=usuarios,dc=correo,dc=mx";
    $result = ldap_add($ldapconn, $dn_aux, $info);

    if($result) 
    {
      echo "New entry with DN " . $dn . " added to LDAP directory."; 
    } 
    // else display error   
    else 
    {
      echo "An error occurred. Error number " . ldap_errno($conn) . ": " .
      ldap_err2str(ldap_errno($conn)); 
    }
  } 
  else 
  { 
    echo "LDAP bind error..."; 
  } 
} 
ldap_close($ldapconn); 
?> 

I'm taking my first steps in this ldap, so please could you explain in detail.

Upvotes: 1

Views: 3568

Answers (1)

JPBlanc
JPBlanc

Reputation: 72612

Not sure of the correct PHP syntax but the following line :

$dn_aux = "mail=" . $_POST['mail'] . ",ou=usuarios,dc=correo,dc=mx"; 

is not correct concerning an Active-Directory. The explanation is that in such a Directory your are not able to choose the attribute you use for naming an object. For example an 'InetOrgPerson' object MUST use the CN attribute to name it. For more details read carefuly naming attributes in object naming from Microsoft documentation.

try :

dn_aux = "CN=" . $_POST['cn'] . ",ou=usuarios,dc=correo,dc=mx"; 

Upvotes: 1

Related Questions