<?php
include "ldap_lib.inc";
/*
* Authentification
*/
function auth($login,$passwd) {
$uid="uid=".$login.",ou=users,dc=univ-lehavre,dc=fr";
return connectLdap("127.0.0.1", $uid, $passwd);
}
?>
<?php
function connectLdap($host,$userDn,$userPasswd) {
$ds=ldap_connect($host);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$r=@ldap_bind($ds,$userDn,$userPasswd);
return $r ;
}
function list_of_people($host, $path, $userDn, $userPasswd) {
$ds=ldap_connect($host);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$r=@ldap_bind($ds,$userDn,$userPasswd);
if($ds) {
$sr=ldap_search($ds, $path, "objectclass=*");
$info = ldap_get_entries($ds, $sr);
// Affiche toutes les données
foreach($info as $k=>$v) {
if(is_array($v)) {
foreach($v as $k1=>$v1) {
if(is_array($v1)) {
foreach($v1 as $k2=>$v2) {
echo $k1."=".$v2."<br/>";
}
}
}
}
}
//affiche une partie des données
for ($i=0;$i<$info["count"];$i++) {
$res[$i]["uid"]=$info[$i]["uid"][0];
$res[$i]["cn"]=$info[$i]["cn"][0];
}
return $res ;
}
else {
return "Erreur de connexion" ;
}
}
function ldapAddUser($login, $clearpass, $loginshell, $gidnumber, $uidnumber, $homedirectory) {
$host="localhost";
$rootdn="cn=admin,dc=univ-lehavre,dc=fr";
$rootpasswd="azerty";
$ds = ldap_connect($host);
if($ds) {
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$r=ldap_bind($ds,$rootdn,$rootpasswd);
$info["uid"] = $login ;
$info["cn"] = $login ;
$info["objectclass"][0] = "account" ;
$info["objectclass"][1] = "posixAccount" ;
$info["userpassword"] = "{crypt}".constructPass($clearpass);
$info["uidnumber"] = $uidnumber ;
$info["gidnumber"] = $gidnumber ;
$info["homedirectory"] = $homedirectory ;
$dn = "uid=".$login.",ou=users,dc=univ-lehavre,dc=fr";
$res = ldap_add($ds, $dn, $info);
$res = ldap_err2str( ldap_errno($ds) );
ldap_close($ds);
return $res ;
}
}
/**
* Genere un salt aléatoire de 8 caracteres
*/
function generateSalt($length) {
return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
function constructPass($text) {
return crypt($text,'$1$'.generateSalt('8'));
}
//Delete
function ldapDelUser($dn) {
$host="localhost";
$rootdn="cn=admin,dc=univ-lehavre,dc=fr";
$rootpasswd="azerty";
$ds = ldap_connect($host);
if($ds) {
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$r=ldap_bind($ds,$rootdn,$rootpasswd);
$res=ldap_delete($ds,$dn);
ldap_close($ds);
}
return $res ;
}
function ldapModPassUser($login, $newpass) {
$host="localhost";
$rootdn="cn=admin,dc=univ-lehavre,dc=fr";
$rootpasswd="azerty";
$ds = ldap_connect($host);
$dn = "uid=".$login.",ou=users,dc=univ-lehavre,dc=fr";
$info["userpassword"]="{crypt}".constructPass($newpass);
if($ds) {
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$r=ldap_bind($ds,$rootdn,$rootpasswd);
$res=ldap_modify($ds,$dn,$info);
ldap_close($ds);
}
return $res;
}
?>