diff --git a/include/class_ldap.inc b/include/class_ldap.inc
index 74020d3c9e3f455f334a1ad320cbdfb5efc316d9..96ff38e203b08e796d2a663a796ef53dd8e9d190 100644 (file)
--- a/include/class_ldap.inc
+++ b/include/class_ldap.inc
define("ALREADY_EXISTING_ENTRY",-10001);
define("UNKNOWN_TOKEN_IN_LDIF_FILE",-10002);
define("NO_FILE_UPLOADED",10003);
-
-
define("INSERT_OK",10000);
-
-
+define("COLON_OVERRIDE", TRUE);
class LDAP{
{
$this->follow_referral= $follow_referral;
$this->tls=$tls;
- $this->binddn=$binddn;
+ $this->binddn=$this->convert($binddn);
+
$this->bindpw=$bindpw;
$this->hostname=$hostname;
$this->connect();
}
+
+ function convert($dn)
+ {
+ if (COLON_OVERRIDE == TRUE){
+ $res= preg_replace("/\\\\,/", '###GOSAREPLACED###', $dn);
+ $res= preg_replace("/\\\\2C/", '###GOSAREPLACED###', $res);
+ #if ($dn != $res){
+ # echo "Conversation from '$dn' to '$res'<br>";
+ #}
+ return ($res);
+ } else {
+ return ($dn);
+ }
+ }
+
+
+ function fix($dn)
+ {
+ if (COLON_OVERRIDE == TRUE){
+ $res= preg_replace("/###GOSAREPLACED###/", '\,', $dn);
+ #if ($dn != $res){
+ # echo "Fix from '$dn' to '$res'<br>";
+ #}
+ return ($res);
+ } else {
+ return ($dn);
+ }
+ }
+
+
function connect()
{
$this->hascon=false;
}
$this->error = "No Error";
- if ($bid = @ldap_bind($this->cid, $this->binddn, $this->bindpw)) {
+ if ($bid = @ldap_bind($this->cid, $this->fix($this->binddn), $this->bindpw)) {
$this->error = "Success";
$this->hascon=true;
} else {
function rebind($ldap, $referral)
{
$credentials= $this->get_credentials($referral);
- if (@ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
+ if (@ldap_bind($ldap, $this->fix($credentials['ADMIN']), $credentials['PASSWORD'])) {
$this->error = "Success";
$this->hascon=true;
$this->reconnect= true;
return (0);
} else {
- $this->error = "Could not bind to " . $binddn;
+ $this->error = "Could not bind to " . $credentials['ADMIN'];
return NULL;
}
}
if ($dir == "..")
$this->basedn = $this->getParentDir();
else
- $this->basedn = $dir;
+ $this->basedn = $this->convert($dir);
}
function getParentDir($basedn = "")
{
if ($basedn=="")
$basedn = $this->basedn;
+ else
+ $basedn = $this->convert($this->basedn);
return(ereg_replace("[^,]*[,]*[ ]*(.*)", "\\1", $basedn));
}
function search($filter, $attrs= array())
{
-
if($this->hascon){
if ($this->reconnect) $this->connect();
$this->clearResult();
- $this->sr = @ldap_search($this->cid, $this->basedn, $filter, $attrs);
+ $this->sr = @ldap_search($this->cid, $this->fix($this->basedn), $filter, $attrs);
$this->error = @ldap_error($this->cid);
$this->resetResult();
$this->hasres=true;
+
return($this->sr);
}else{
$this->error = "Could not connect to LDAP server";
}
}
- function ls($filter = "(objectclass=*)", $basedn = "")
+ function ls($filter = "(objectclass=*)", $basedn = "",$attrs = array("*"))
{
if($this->hascon){
if ($this->reconnect) $this->connect();
$this->clearResult();
if ($basedn == "")
$basedn = $this->basedn;
- $this->sr = @ldap_list($this->cid, $basedn, $filter);
+ else
+ $basedn= $this->convert($basedn);
+ $this->sr = @ldap_list($this->cid, $this->fix($basedn), $filter,$attrs);
$this->error = @ldap_error($this->cid);
$this->resetResult();
$this->hasres=true;
}
}
- function cat($dn)
+ function cat($dn,$attrs= array("*"))
{
if($this->hascon){
if ($this->reconnect) $this->connect();
$this->clearResult();
$filter = "(objectclass=*)";
- $this->sr = @ldap_read($this->cid, $dn, $filter);
+ $this->sr = @ldap_read($this->cid, $this->fix($dn), $filter,$attrs);
$this->error = @ldap_error($this->cid);
$this->resetResult();
$this->hasres=true;
if ($this->re)
{
$att= @ldap_get_attributes($this->cid, $this->re);
- $att['dn']= @ldap_get_dn($this->cid, $this->re);
+ $att['dn']= $this->convert(@ldap_get_dn($this->cid, $this->re));
}
$this->error = @ldap_error($this->cid);
if (!isset($att)){
$rv = @ldap_get_dn($this->cid, $this->re);
$this->error = @ldap_error($this->cid);
- $rv= preg_replace("/[ ]*,[ ]*/", ",", $rv);
- return($rv);
+ return($this->convert($rv));
}
}else{
$this->error = "Perform a Fetch with no Search";
if ($dn == "")
$dn = $this->basedn;
- $r = @ldap_mod_del($this->cid, $dn, $attrs);
+ $r = @ldap_mod_del($this->cid, $this->fix($dn), $attrs);
$this->error = @ldap_error($this->cid);
return($r);
}else{
if ($dn == "")
$dn = $this->basedn;
- $r = @ldap_mod_replace($this->cid, $dn, $attrs);
+ $r = @ldap_mod_replace($this->cid, $this->fix($dn), $attrs);
$this->error = @ldap_error($this->cid);
return($r);
}else{
{
if($this->hascon){
if ($this->reconnect) $this->connect();
- $r = @ldap_delete($this->cid, $deletedn);
+ $r = @ldap_delete($this->cid, $this->fix($deletedn));
$this->error = @ldap_error($this->cid);
return($r ? $r : 0);
}else{
}
}
+ /**
+ * Function rmdir_recursive
+ *
+ * Description: Based in recursive_remove, adding two thing: full subtree remove, and delete own node.
+ * Parameters: The dn to delete
+ * GiveBack: True on sucessfull , 0 in error, and "" when we don't get a ldap conection
+ *
+ */
+
+ function rmdir_recursive($deletedn)
+ {
+ if($this->hascon){
+ if ($this->reconnect) $this->connect();
+ $delarray= array();
+
+ /* Get sorted list of dn's to delete */
+ $this->ls ("(objectClass=*)",$deletedn);
+ while ($this->fetch()){
+ $deldn= $this->getDN();
+ $delarray[$deldn]= strlen($deldn);
+ }
+ arsort ($delarray);
+ reset ($delarray);
+
+ /* Really Delete ALL dn's in subtree */
+ foreach ($delarray as $key => $value){
+ $this->rmdir_recursive($key);
+ }
+
+ /* Finally Delete own Node */
+ $r = @ldap_delete($this->cid, $this->fix($deletedn));
+ $this->error = @ldap_error($this->cid);
+ return($r ? $r : 0);
+ }else{
+ $this->error = "Could not connect to LDAP server";
+ return("");
+ }
+ }
+
+ /* Copy given attributes and sub-dns with attributes to destination dn
+ */
+ function copy_FAI_resource_recursive($sourcedn,$destinationdn,$type="branch",$is_first = true,$depth=0)
+ {
+ error_reporting(E_ALL);
+
+ if($is_first){
+ echo "<h2>".sprintf(_("Creating copy of %s"),"<i>".$sourcedn."</i>")."</h2>";
+ }else{
+ if(preg_match("/^ou=/",$sourcedn)){
+ echo "<h3>"._("Processing")." <i>$destinationdn</i></h3>";
+ }else{
+ $tmp = split(",",$sourcedn);
+
+ echo " <b>"._("Object").":</b> ";
+
+ $deststr = $destinationdn;
+ if(strlen($deststr) > 96){
+ $deststr = substr($deststr,0,96)."...";
+ }
+
+ echo $deststr."<br>";
+ }
+ }
+
+ flush();
+
+ if($this->hascon){
+ if ($this->reconnect) $this->connect();
+
+ /* Save base dn */
+ $basedn= $this->basedn;
+ $delarray= array();
+
+ /* Check if destination entry already exists */
+ $this->cat($destinationdn);
+
+ if($this->count()){
+ return;
+ }else{
+
+ $this->clearResult();
+
+ /* Get source entry */
+ $this->cd($basedn);
+ $this->cat($sourcedn);
+ $attr = $this->fetch();
+
+ /* Error while fetching object / attribute abort*/
+ if((!$attr) || (count($attr)) ==0) {
+ echo _("Error while fetching source dn - aborted!");
+ return;
+ }
+
+ /* check if this is a department */
+ if(in_array("organizationalUnit",$attr['objectClass'])){
+ $attr['dn'] = $this->convert($destinationdn);
+ $this->cd($basedn);
+ $this->create_missing_trees($destinationdn);
+ $this->cd($destinationdn);
+
+ /* If is first entry, append FAIbranch to department entry */
+ if($is_first){
+ $this->cat($destinationdn);
+ $attr= $this->fetch();
+
+ /* Filter unneeded informations */
+ foreach($attr as $key => $value){
+ if(is_numeric($key)) unset($attr[$key]);
+ if(isset($attr[$key]['count'])){
+ if(is_array($attr[$key])){
+ unset($attr[$key]['count']);
+ }
+ }
+ }
+
+ unset($attr['count']);
+ unset($attr['dn']);
+
+ /* Add marking attribute */
+ $attr['objectClass'][] = "FAIbranch";
+
+ /* Add this entry */
+ $this->modify($attr);
+ }
+ }else{
+
+ /* If this is no department */
+ foreach($attr as $key => $value){
+ if(in_array($key ,array("FAItemplateFile","FAIscript", "gotoLogonScript", "gosaApplicationIcon"))){
+ $sr= ldap_read($this->cid, $this->fix($sourcedn), "$key=*", array($key));
+ $ei= ldap_first_entry($this->cid, $sr);
+ if ($tmp= @ldap_get_values_len($this->cid, $ei,$key)){
+ $attr[$key] = $tmp;
+ }
+ }
+
+ if(is_numeric($key)) unset($attr[$key]);
+ if(isset($attr[$key]['count'])){
+ if(is_array($attr[$key])){
+ unset($attr[$key]['count']);
+ }
+ }
+ }
+ unset($attr['count']);
+ unset($attr['dn']);
+
+ if(!in_array("gosaApplication" , $attr['objectClass'])){
+ if($type=="branch"){
+ $attr['FAIstate'] ="branch";
+ }elseif($type=="freeze"){
+ $attr['FAIstate'] ="freeze";
+ }else{
+ print_red(_("Unknown FAIstate %s"),$type);
+ }
+ }
+
+ /* Add entry */
+ $this->cd($destinationdn);
+ $this->cat($destinationdn);
+ $a = $this->fetch();
+ if(!count($a)){
+ $this->add($attr);
+ }
+
+ if($this->error != "Success"){
+ /* Some error occured */
+ print "---------------------------------------------";
+ print $this->get_error()."<br>";
+ print $sourcedn."<br>";
+ print $destinationdn."<br>";
+ print_a( $attr);
+ exit();
+ }
+ }
+ }
+
+ $this->ls ("(objectClass=*)",$sourcedn);
+ while ($this->fetch()){
+ $deldn= $this->getDN();
+ $delarray[$deldn]= strlen($deldn);
+ }
+ asort ($delarray);
+ reset ($delarray);
+
+ $depth ++;
+ foreach($delarray as $dn => $bla){
+ if($dn != $destinationdn){
+ $this->cd($basedn);
+ $item = $this->fetch($this->cat($dn));
+ if(!in_array("FAIbranch",$item['objectClass'])){
+ $this->copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$type,false,$depth);
+ }
+ }
+ }
+ }
+ if($is_first){
+ echo "<p class='seperator'> </p>";
+ }
+
+ }
+
function modify($attrs)
{
+ if(count($attrs) == 0){
+ return (0);
+ }
if($this->hascon){
if ($this->reconnect) $this->connect();
- $r = @ldap_modify($this->cid, $this->basedn, $attrs);
+ $r = @ldap_modify($this->cid, $this->fix($this->basedn), $attrs);
$this->error = @ldap_error($this->cid);
return($r ? $r : 0);
}else{
{
if($this->hascon){
if ($this->reconnect) $this->connect();
- $r = @ldap_add($this->cid, $this->basedn, $attrs);
+ $r = @ldap_add($this->cid, $this->fix($this->basedn), $attrs);
$this->error = @ldap_error($this->cid);
return($r ? $r : 0);
}else{
function create_missing_trees($target)
{
-
/* Ignore create_missing trees if the base equals target */
if ($target == $this->basedn){
return;
break;
default:
print_red(sprintf(_("Autocreation of type '%s' is currently not supported. Please report to the GOsa team."), $type));
+ echo $_SESSION['errors'];
exit;
}
$this->cd($cdn);
}
}
- function get_attribute($dn, $name)
+ function get_attribute($dn, $name,$r_array=0)
{
$data= "";
if ($this->reconnect) $this->connect();
- $sr= @ldap_read($this->cid, $dn, "objectClass=*", array("$name"));
+ $sr= @ldap_read($this->cid, $this->fix($dn), "objectClass=*", array("$name"));
/* fill data from LDAP */
if ($sr) {
if ($info= @ldap_get_values_len($this->cid, $ei, "$name")){
$data= $info[0];
}
+
}
}
-
+ if($r_array==0)
return ($data);
+ else
+ return ($info);
+
+
}
+
+
function get_additional_error()
{
if (isset($referrals[$server])){
return ($referrals[$server]);
} else {
- $ret['ADMIN']= $this->binddn;
+ $ret['ADMIN']= $this->fix($this->binddn);
$ret['PASSWORD']= $this->bindpw;
}
return ($display);
}
+function gen_xls ($dn, $filter= "(objectClass=*)", $attributes= array('*'), $recursive= TRUE,$r_array=0)
+ {
+ $display= "";
+
+ $this->cd($dn);
+ $this->search("$filter");
+
+ $i=0;
+ while ($attrs= $this->fetch()){
+ $j=0;
+
+ foreach ($attributes as $at){
+ $display[$i][$j]= $this->get_attribute($attrs['dn'], $at,$r_array);
+ $j++;
+ }
+
+ $i++;
+ }
+
+ return ($display);
+ }
+
function gen_one_entry($dn, $filter= "(objectClass=*)" , $name= array("*"))
{
}
/* Searching Ldap Tree */
- $sr= @ldap_read($this->cid, $dn, $filter, $name);
+ $sr= @ldap_read($this->cid, $this->fix($dn), $filter, $name);
- /* Get the first entry */
+ /* Get the first entry */
$entry= @ldap_first_entry($this->cid, $sr);
/* Get all attributes related to that Objekt */
/* Reset index */
$i = 1 ;
-
+ $identifier = array();
$attribute= @ldap_first_attribute($this->cid,$entry,$identifier);
while ($attribute) {
$i++;
function dn_exists($dn)
{
- return @ldap_list($this->cid, $dn, "(objectClass=*)", array("objectClass"));
+ return @ldap_list($this->cid, $this->fix($dn), "(objectClass=*)", array("objectClass"));
}
/* Every block must begin with a dn */
if($dn != "dn") {
- $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"),
- $line, $current_line);
+ $error= sprintf(_("This is not a valid DN: '%s'. A block for import should begin with 'dn: ...' in line %s"), $line, $current_line);
return -2;
}
-
- // Should we use Modify instead of Add
- $usemodify = false;
-
- // Delete before insert
- $usermdir = false;
-
+ /* Should we use Modify instead of Add */
+ $usemodify= false;
+ /* Delete before insert */
+ $usermdir= false;
/* The dn address already exists! */
if (($this->dn_exists($value))&&((!$overwrite)&&(!$cleanup))) {
+
$error= sprintf(_("The dn: '%s' (from line %s) already exists in the LDAP database."), $line, $current_line);
return ALREADY_EXISTING_ENTRY;
- }
- elseif(($this->dn_exists($value))&&($cleanup)){
- ;//Delete first, then add
+
+ } elseif(($this->dn_exists($value))&&($cleanup)){
+
+ /* Delete first, then add */
$usermdir = true;
- }
- elseif(($this->dn_exists($value))&&($overwrite)) {
- ;//Modify instead of Add
+
+ } elseif(($this->dn_exists($value))&&($overwrite)) {
+
+ /* Modify instead of Add */
$usemodify = true;
}
-
/* If we can't Import, return with a file error */
if(!$this->import_single_entry($single,$usemodify,$usermdir) ) {
$error= sprintf(_("Error while importing dn: '%s', please check your LDIF from line %s on!"), $line,
$current_line);
return UNKNOWN_TOKEN_IN_LDIF_FILE; }
-
}
+
return (INSERT_OK);
}
+
/* Imports a single entry */
function import_single_entry($str_attr,$modify,$delete)
{
-
-
-
if($this->reconnect) $this->connect();
$ret = false;
+ $rows= split("\n",$str_attr);
+ $data= false;
- $rows = split("\n",$str_attr);
- $data = false;
foreach($rows as $row) {
/* Check if we use Linenumbers (when import_complete_ldif is called we use
Linenumbers) Linenumbers are use like this 123#attribute : value */
if(!empty($row)) {
if((strpos($row,"#")!=FALSE)&&(strpos($row,"#")<strpos($row,":"))) {
+
/* We are using line numbers
Because there is a # before a : */
- $tmp1 = split("#",$row);
- $current_line = $tmp1[0];
- $row = $tmp1[1];
+ $tmp1= split("#",$row);
+ $current_line= $tmp1[0];
+ $row= $tmp1[1];
}
/* Split the line into attribute and value */
- $attr = split(":", $row);
- $attr[0] = trim($attr[0]); /* attribute */
- $attr[1] = trim($attr[1]); /* value */
+ $attr = split(":", $row);
+ $attr[0]= trim($attr[0]); /* attribute */
+ $attr[1]= trim($attr[1]); /* value */
/* Check for attributes that are used more than once */
if(!isset($data[$attr[0]])) {
$this->cd($data['dn']);
/* Delete existing entry */
- if($delete)
- $this->rmdir($data['dn']);
-
+ if($delete){
+ $this->rmdir($data['dn']);
+ }
/* Create missing trees */
$this->create_missing_trees($data['dn']);
unset($data['dn']);
/* If entry exists use modify */
- if(!$modify)
+ if(!$modify){
$ret = $this->add($data);
- else
+ } else {
$ret = $this->modify($data);
-
-
-
+ }
}
return($ret);
foreach($lines as $line)
{
/* continue if theres a comment */
- if(substr(trim($line),0,1)=="#")
- continue;
+ if(substr(trim($line),0,1)=="#"){
+ continue;
+ }
$line= str_replace ("\t\t","\t",$line);
$line= str_replace ("\t" ,"," ,$line);
- print $line;
-
-
+ echo $line;
$cells = split(",",$line ) ;
- #print_a($cells);
$linet= str_replace ("\t\t",",",$line);
$cells = split("\t",$line);
$count = count($cells);