added search_domains() function

This commit is contained in:
Roland Gruber 2003-07-26 12:37:31 +00:00
parent fc071a1407
commit 1512734ac1
1 changed files with 120 additions and 59 deletions

View File

@ -25,6 +25,7 @@ $Id$
include_once("config.inc"); include_once("config.inc");
// manages connection to LDAP and several helper functions
class Ldap{ class Ldap{
// object of Config to access preferences // object of Config to access preferences
@ -105,76 +106,103 @@ class Ldap{
} }
} }
// closes connection to server // closes connection to server
function close() { function close() {
ldap_close($this->server); ldap_close($this->server);
} }
// searches LDAP for a specific user name // searches LDAP for a specific user name
// and returns its DN entry // and returns its DN entry
function search_username($name) { function search_username($name) {
if ($this->conf->get_samba3() == "yes") { if ($this->conf->get_samba3() == "yes") {
// users have the attribute "posixAccount" or "sambaSamAccount" and uid $name // users have the attribute "posixAccount" or "sambaSamAccount" and uid $name
$filter = "(&(objectClass=sambaSamAccount) (uid=$name))"; $filter = "(&(objectClass=sambaSamAccount) (uid=$name))";
}
else {
// users have the attribute "posixAccount" or "sambaAccount" and uid $name
$filter = "(&(objectClass=sambaAccount) (uid=$name))";
}
$attrs = array();
$sr = @ldap_search($this->server, $this->conf->get_UserSuffix(), $filter, $attrs);
if ($sr) {
$info = ldap_get_entries($this->server, $sr);
// return only first DN entry
$ret = $info[0]["dn"];
ldap_free_result($sr);
return $ret;
}
} }
else {
// users have the attribute "posixAccount" or "sambaAccount" and uid $name
$filter = "(&(objectClass=sambaAccount) (uid=$name))";
}
$attrs = array();
$sr = @ldap_search($this->server, $this->conf->get_UserSuffix(), $filter, $attrs);
if ($sr) {
$info = ldap_get_entries($this->server, $sr);
// return only first DN entry
$ret = $info[0]["dn"];
ldap_free_result($sr);
return $ret;
}
}
// returns an array with all organizational units under the given suffix // returns an array with all organizational units under the given suffix
function search_units($suffix) { function search_units($suffix) {
$ret = array(); $ret = array();
$sr = @ldap_search($_SESSION["ldap"]->server(), $suffix, "objectClass=organizationalunit", array("DN")); $sr = @ldap_search($_SESSION["ldap"]->server(), $suffix, "objectClass=organizationalunit", array("DN"));
if ($sr) { if ($sr) {
$units = ldap_get_entries($_SESSION["ldap"]->server, $sr); $units = ldap_get_entries($_SESSION["ldap"]->server, $sr);
// extract Dns // extract Dns
for ($i = 0; $i < sizeof($units); $i++) { for ($i = 0; $i < sizeof($units); $i++) {
if ($units[$i]['dn']) $ret[] = $units[$i]['dn']; if ($units[$i]['dn']) $ret[] = $units[$i]['dn'];
}
} }
} // add root suffix if needed
// add root suffix if needed $found == false;
$found == false; for ($i = 0; $i < sizeof($ret); $i++) { // search suffix case-intensitive
for ($i = 0; $i < sizeof($ret); $i++) { // search suffix case-intensitive if (strtolower($suffix) == strtolower($ret[$i])) {
if (strtolower($suffix) == strtolower($ret[$i])) { $found = true;
$found = true; break;
break; }
} }
} if (!$found) {
if (!$found) { $ret[] = $suffix;
$ret[] = $suffix; }
} usort($ret, array($this,"cmp_array"));
usort($ret, array($this,"cmp_array")); return $ret;
return $ret;
} }
// returns the LDAP connection handle // returns an array with all Samba 3 domain entries under the given suffix
function server() { function search_domains($suffix) {
return $this->server; $ret = array();
} $attr = array("DN", "sambaDomainName", "sambaSID", "sambaNextRid", "sambaNextGroupRid",
"sambaNextUserRid", "sambaAlgorithmicRidBase");
$sr = @ldap_search($_SESSION["ldap"]->server(), $suffix, "sambaDomainName=*", $attr);
if ($sr) {
$units = ldap_get_entries($_SESSION["ldap"]->server, $sr);
// delete count entry
array_shift($units);
// extract attributes
for ($i = 0; $i < sizeof($units); $i++) {
$ret[$i] = new samba3domain();
$ret[$i]->dn = $units[$i]['dn'];
$ret[$i]->name = $units[$i]['sambadomainname'][0];
$ret[$i]->SID = $units[$i]['sambasid'][0];
$ret[$i]->nextRID = $units[$i]['sambanextrid'][0];
$ret[$i]->nextGroupRID = $units[$i]['sambanextgrouprid'][0];
$ret[$i]->nextUserRID = $units[$i]['sambanextuserrid'][0];
if (isset($units[$i]['sambaalgorithmicridbase'][0])) $ret[$i]->RIDbase = $units[$i]['sambaalgorithmicridbase'][0];
}
// sort array by domain name
usort($ret, array($this,"cmp_domain"));
}
return $ret;
}
// closes connection to LDAP server before serialization // returns the LDAP connection handle
function __sleep() { function server() {
$this->close(); return $this->server;
// define which attributes to save }
return array("conf", "username", "password", "ldapUserAttributes", "ldapGroupAttributes", "ldapHostAttributes");
}
// reconnects to LDAP server when deserialized // closes connection to LDAP server before serialization
function __wakeup() { function __sleep() {
$data = $this->decrypt(); $this->close();
$this->connect($data[0], $data[1]); // define which attributes to save
} return array("conf", "username", "password", "ldapUserAttributes", "ldapGroupAttributes", "ldapHostAttributes");
}
// reconnects to LDAP server when deserialized
function __wakeup() {
$data = $this->decrypt();
$this->connect($data[0], $data[1]);
}
// encrypts username and password // encrypts username and password
function encrypt($username, $password) { function encrypt($username, $password) {
@ -249,7 +277,40 @@ class Ldap{
else return -1; else return -1;
} }
} }
// helper function to sort the domains
function cmp_domain($a, $b) {
if ($a->name == $b->name) return 0;
elseif ($a->name == max($a->name, $b->name)) return 1;
else return -1;
}
} }
// represents a Samba 3 domain entry
class samba3domain {
// DN
var $dn;
// domain name
var $name;
// domain SID
var $SID;
// next RID
var $nextRID;
// next user RID
var $nextUserRID;
// next group RID
var $nextGroupRID;
// RID base to calculate RIDs, default 1000
var $RIDbase=1000;
}
?> ?>