LDAP: couldn't connect to LDAP server

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
lpro:ldap:code_php [2015/01/07 16:49]
127.0.0.1 external edit
lpro:ldap:code_php [2016/01/13 10:12] (current)
jbaudry
Line 5: Line 5:
 function authenticate($login,​$passwd) { function authenticate($login,​$passwd) {
  $ds=ldap_connect("​localhost"​);​  $ds=ldap_connect("​localhost"​);​
- $dn="​uid="​.$login.",​ou=people,​dc=univ-lehavre,​dc=fr";​+ $dn="​uid="​.$login.",​ou=users,​dc=univ-lehavre,​dc=fr";​
  ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3);  ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3);
  $r=@ldap_bind($ds,​$dn,​$passwd);​  $r=@ldap_bind($ds,​$dn,​$passwd);​
Line 12: Line 12:
 </​code>​ </​code>​
  
 +<code php>
 +$res = authenticate("​garfield","​azerty"​);​
 +echo $res ;
 +</​code>​
  
 <code php> <code php>
Line 23: Line 27:
  
 <code php> <code php>
-//del group +//del user 
-function ​delGroup($dn) { +function ​delUser($dn) { 
- $ds=authenticate("login", "pass");+ //​authentification 
 +        ​$ds=ldap_connect("localhost"​);​ 
 +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); 
 +        $r=@ldap_bind($ds,"​cn=admin,​dc=univ-lehavre,​dc=fr","​azerty");
  $res=ldap_delete($ds,​$dn);​  $res=ldap_delete($ds,​$dn);​
  ldap_close($ds);​  ldap_close($ds);​
Line 34: Line 41:
  
 <code php> <code php>
-function addGroup($dn, $data) {+function addGroup() {
         //​authentification         //​authentification
- $ds=authenticate("login", "passwd");+        ​$ds=ldap_connect("localhost"​);​ 
 +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); 
 +        $r=@ldap_bind($ds,"​cn=admin,​dc=univ-lehavre,​dc=fr","​azerty"); 
 +        ​
         //on indique l'​endroit où ajouter l'​element         //on indique l'​endroit où ajouter l'​element
  $group_path="​ou=groups,​dc=univ-lehavre,​dc=fr";​  $group_path="​ou=groups,​dc=univ-lehavre,​dc=fr";​
         //on stocke les donnees dans un tableau         //on stocke les donnees dans un tableau
- $info["​objectclass"​][0] = "groupOfNames" ; +      
-        ... etc+ $info["​objectclass"​][0] = "posixGroup" ; 
 +        ​$info["​cn"​]="​utilisateurs3"​ ; 
 +        $info["​gidnumber"​]="​2025";​ 
 +         
 +        $dn="​cn="​.$info["​cn"​].","​.$group_path ;
    
  $res = ldap_add($ds,​ $dn, $info);  $res = ldap_add($ds,​ $dn, $info);
  $res = ldap_err2str( ldap_errno($ds) );  $res = ldap_err2str( ldap_errno($ds) );
         ​         ​
 +        //fermeture de la connexion
 + ldap_close($ds);​
 + return $res ;
 +}
 +</​code>​
 +
 +<code php>
 +function addUser() {
 +        //​authentification
 +        $ds=ldap_connect("​localhost"​);​
 +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3);
 +        $r=@ldap_bind($ds,"​cn=admin,​dc=univ-lehavre,​dc=fr","​azerty"​);​
 + ​ var_dump($r);​
 +        //on indique l'​endroit où ajouter l'​element
 + $user_path="​ou=users,​dc=univ-lehavre,​dc=fr";​
 +        //on stocke les donnees dans un tableau
 + 
 +        $info["​cn"​] = "​newuser"​ ;
 +        $info["​objectclass"​][0] = "​account"​ ;
 +        $info["​objectclass"​][1] = "​posixAccount"​ ;
 +        $info["​objectclass"​][2] = "​top"​ ;
 +        $info["​userpassword"​] = "​pass";​
 +        $info["​loginshell"​] = "/​bin/​bash"​ ;
 +        $info["​uidnumber"​] = "​123"​ ;
 +        $info["​gidnumber"​] = "​345"​ ;
 +        $info["​homedirectory"​] = "/​home/​newuser"​ ;
 + 
 +        $dn="​uid=newuser,"​.$user_path ;
 + 
 + $res = ldap_add($ds,​ $dn, $info);
 + $res = ldap_err2str( ldap_errno($ds) );
 + 
         //fermeture de la connexion         //fermeture de la connexion
  ldap_close($ds);​  ldap_close($ds);​
Line 103: Line 149:
  
  * Donnees d'une personne  * Donnees d'une personne
 +
 <code php> <code php>
 function show_a_person2($host,​ $uidPerson, $rootdn, $rootpasswd) { function show_a_person2($host,​ $uidPerson, $rootdn, $rootpasswd) {
- $ds=ldap_connect($host); +        ​$ds=ldap_connect("​localhost"​); 
- ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); +        ldap_set_option($ds,​ LDAP_OPT_PROTOCOL_VERSION,​ 3); 
- $r=@ldap_bind($ds,​$rootdn,$rootpw); +        $r=@ldap_bind($ds,​"​cn=admin,dc=univ-lehavre,​dc=fr","​azerty"​);
- if($ds && $r) {+
  $sr=ldap_search($ds,​ $uidPerson, "​objectclass=*"​);​  $sr=ldap_search($ds,​ $uidPerson, "​objectclass=*"​);​
  $info = ldap_get_entries($ds,​ $sr);  $info = ldap_get_entries($ds,​ $sr);
 + $i=0;
  if(count($info)>​0) {  if(count($info)>​0) {
  $res["​cn"​] = $info[$i]["​cn"​][0];​  $res["​cn"​] = $info[$i]["​cn"​][0];​
- $res["​gecos"​] = $info[$i]["​gecos"​][0];​ 
  $res["​gidnumber"​] = $info[$i]["​gidnumber"​][0];​  $res["​gidnumber"​] = $info[$i]["​gidnumber"​][0];​
  $res["​homedirectory"​] = $info[$i]["​homedirectory"​][0];​  $res["​homedirectory"​] = $info[$i]["​homedirectory"​][0];​
Line 122: Line 168:
  }  }
  return $res ;  return $res ;
- } 
- else { 
- return "​Erreur de connexion"​ ; 
- } 
 } }
 +
 +$profil = show_a_person2("​localhost",​ "​uid=gaetan,​ou=users,​dc=univ-lehavre,​dc=fr",​ "​cn=admin,​dc=univ-lehavre,​dc=fr",​ "​azerty"​) ;
 +
 +echo "<​table border=1>"​ ;
 +foreach($profil as $k=>$v) {
 + echo "<​tr><​td>"​.$k."</​td><​td>"​.$v."</​td></​tr>"​ ;
 +}
 +echo "</​table>";​
 </​code>​ </​code>​
  
   * Ajouter une personne dans le LDAP   * Ajouter une personne dans le LDAP
 +
 <code php> <code php>
 /* /*
Line 172: Line 223:
  
   * Controleur PHP   * Controleur PHP
 +
 <code php> <code php>
 if($_SERVER['​REQUEST_METHOD'​]=='​POST'​) { if($_SERVER['​REQUEST_METHOD'​]=='​POST'​) {