fixed bug with identical missing suffixes,

create missing suffixes recursivly
This commit is contained in:
Roland Gruber 2003-11-03 17:48:15 +00:00
parent a032ba1e2c
commit 8ddcc29855
2 changed files with 75 additions and 17 deletions

View File

@ -41,27 +41,85 @@ if ($_POST['add_suff'] || $_POST['cancel']) {
$new_suff = $_POST['new_suff']; $new_suff = $_POST['new_suff'];
$new_suff = str_replace("\\'", "", $new_suff); $new_suff = str_replace("\\'", "", $new_suff);
$new_suff = explode(";", $new_suff); $new_suff = explode(";", $new_suff);
// add entry // add entries
for ($i = 0; $i < sizeof($new_suff); $i++) { for ($i = 0; $i < sizeof($new_suff); $i++) {
// check if entry is already present
$info = @ldap_search($_SESSION['ldap']->server, $new_suff[$i], "", array());
$res = @ldap_get_entries($_SESSION['ldap']->server, $info);
if ($res) continue;
$suff = $new_suff[$i]; $suff = $new_suff[$i];
// generate DN and attributes // generate DN and attributes
$tmp = explode(",", $suff); $tmp = explode(",", $suff);
$name = explode("=", $tmp[0]); $name = explode("=", $tmp[0]);
array_shift($tmp); array_shift($tmp);
$end = implode(",", $tmp); $end = implode(",", $tmp);
if ($name[0] != "ou") { if ($name[0] != "ou") { // add root entry
$attr = array();
$attr[$name[0]] = $name[1];
$attr['objectClass'] = 'organization';
$dn = $suff;
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
$fail[] = $suff; $fail[] = $suff;
continue; continue;
} }
else { }
else { // add organizational unit
$name = $name[1]; $name = $name[1];
$attr = array(); $attr = array();
$attr['objectClass'] = "organizationalunit"; $attr['objectClass'] = "organizationalunit";
$attr['ou'] = $name; $attr['ou'] = $name;
$dn = "ou=" . $name . "," . $end; $dn = $suff;
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
// check if we have to add parent entries
if (ldap_errno($_SESSION['ldap']->server()) == 32) {
$temp = explode(",", $suff);
$subsuffs = array();
// make list of subsuffixes
for ($k = 0; $k < sizeof($temp); $k++) {
$part = explode("=", $temp[$k]);
if ($part[0] == "ou") $subsuffs[] = implode(",", array_slice($temp, $k));
else {
$subsuffs[] = implode(",", array_slice($temp, $k));
break;
}
}
// create missing entries
for ($k = sizeof($subsuffs) - 1; $k >= 0; $k--) {
// check if subsuffix is present
$info = @ldap_search($_SESSION['ldap']->server, $subsuffs[$k], "", array());
$res = @ldap_get_entries($_SESSION['ldap']->server, $info);
if (!$res) {
$suffarray = explode(",", $subsuffs[$k]);
$headarray = explode("=", $suffarray[0]);
if ($headarray[0] == "ou") { // add ou entry
$attr = array();
$attr['objectClass'] = 'organizationalunit';
$attr['ou'] = $headarray[1];
$dn = $subsuffs[$k];
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) { if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
$fail[] = $suff; $fail[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server()); $error[] = ldap_error($_SESSION['ldap']->server());
break;
}
}
else { // add root entry
$attr = array();
$attr['objectClass'] = 'organization';
$attr[$headarray[0]] = $headarray[1];
$dn = $subsuffs[$k];
if (!@ldap_add($_SESSION['ldap']->server(), $dn, $attr)) {
$fail[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server());
break;
}
}
}
}
}
else {
$fail[] = $suff;
$error[] = ldap_error($_SESSION['ldap']->server());
}
} }
} }
} }

View File

@ -29,22 +29,22 @@ $new_suffs = array();
if ($conf->get_UserSuffix() && ($conf->get_UserSuffix() != "")) { if ($conf->get_UserSuffix() && ($conf->get_UserSuffix() != "")) {
$info = @ldap_search($_SESSION['ldap']->server, $conf->get_UserSuffix(), "", array()); $info = @ldap_search($_SESSION['ldap']->server, $conf->get_UserSuffix(), "", array());
$res = @ldap_get_entries($_SESSION['ldap']->server, $info); $res = @ldap_get_entries($_SESSION['ldap']->server, $info);
if (!$res) $new_suffs[] = $conf->get_UserSuffix(); if (!$res && !in_array($conf->get_UserSuffix(), $new_suffs)) $new_suffs[] = $conf->get_UserSuffix();
} }
if ($conf->get_GroupSuffix() && ($conf->get_GroupSuffix() != "")) { if ($conf->get_GroupSuffix() && ($conf->get_GroupSuffix() != "")) {
$info = @ldap_search($_SESSION['ldap']->server, $conf->get_GroupSuffix(), "", array()); $info = @ldap_search($_SESSION['ldap']->server, $conf->get_GroupSuffix(), "", array());
$res = @ldap_get_entries($_SESSION['ldap']->server, $info); $res = @ldap_get_entries($_SESSION['ldap']->server, $info);
if (!$res) $new_suffs[] = $conf->get_GroupSuffix(); if (!$res && !in_array($conf->get_GroupSuffix(), $new_suffs)) $new_suffs[] = $conf->get_GroupSuffix();
} }
if ($conf->get_HostSuffix() && ($conf->get_HostSuffix() != "")) { if ($conf->get_HostSuffix() && ($conf->get_HostSuffix() != "")) {
$info = @ldap_search($_SESSION['ldap']->server, $conf->get_HostSuffix(), "", array()); $info = @ldap_search($_SESSION['ldap']->server, $conf->get_HostSuffix(), "", array());
$res = @ldap_get_entries($_SESSION['ldap']->server, $info); $res = @ldap_get_entries($_SESSION['ldap']->server, $info);
if (!$res) $new_suffs[] = $conf->get_HostSuffix(); if (!$res && !in_array($conf->get_HostSuffix(), $new_suffs)) $new_suffs[] = $conf->get_HostSuffix();
} }
if ($conf->get_DomainSuffix() && ($conf->get_DomainSuffix() != "")) { if ($conf->get_DomainSuffix() && ($conf->get_DomainSuffix() != "")) {
$info = @ldap_search($_SESSION['ldap']->server, $conf->get_DomainSuffix(), "", array()); $info = @ldap_search($_SESSION['ldap']->server, $conf->get_DomainSuffix(), "", array());
$res = @ldap_get_entries($_SESSION['ldap']->server, $info); $res = @ldap_get_entries($_SESSION['ldap']->server, $info);
if (!$res) $new_suffs[] = $conf->get_DomainSuffix(); if (!$res && !in_array($conf->get_DomainSuffix(), $new_suffs)) $new_suffs[] = $conf->get_DomainSuffix();
} }
if ($_SESSION['config']->is_samba3()) { if ($_SESSION['config']->is_samba3()) {