2003-03-05 16:05:23 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
This code is part of LDAP Account Manager (http://www.sourceforge.net/projects/lam)
|
|
|
|
Copyright (C) 2003 Roland Gruber
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2003-06-28 08:05:58 +00:00
|
|
|
|
2003-03-05 16:05:23 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2003-06-28 08:05:58 +00:00
|
|
|
|
2003-03-05 16:05:23 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* ldap.inc provides basic functions to connect to the OpenLDAP server.
|
|
|
|
*
|
|
|
|
* @package LDAP
|
|
|
|
* @author Roland Gruber
|
|
|
|
*/
|
2003-04-27 16:29:53 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Access to configuration data */
|
2003-04-23 19:13:55 +00:00
|
|
|
include_once("config.inc");
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Encryption functions */
|
2004-01-10 11:08:10 +00:00
|
|
|
include_once("blowfish.inc");
|
2003-03-05 16:05:23 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Converts a HEX string to a binary value
|
|
|
|
*
|
|
|
|
* @param string $value HEX string
|
|
|
|
* @return binary result binary
|
|
|
|
*/
|
2004-01-03 18:19:21 +00:00
|
|
|
function hex2bin($value) {
|
|
|
|
return pack("H*", $value);
|
|
|
|
}
|
2003-08-08 15:35:59 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Returns the hash value of a plain text password
|
|
|
|
* the hash algorithm depends on the configuration file
|
|
|
|
*
|
|
|
|
* @param string $password the password string
|
|
|
|
* @param boolean $enabled marks the hash as enabled/disabled (e.g. by prefixing "!")
|
|
|
|
* @return string the password hash
|
|
|
|
*/
|
2003-10-04 13:26:37 +00:00
|
|
|
function pwd_hash($password, $enabled=true) {
|
2003-10-05 10:51:01 +00:00
|
|
|
// check for empty password
|
|
|
|
if (! $password || ($password == "")) {
|
2004-04-09 10:21:43 +00:00
|
|
|
return "";
|
2003-10-05 10:51:01 +00:00
|
|
|
}
|
2004-01-03 18:19:21 +00:00
|
|
|
// calculate new random number
|
|
|
|
$_SESSION['ldap']->new_rand();
|
2003-10-04 13:26:37 +00:00
|
|
|
// hash password with algorithm from config file
|
|
|
|
$hash = "";
|
|
|
|
switch ($_SESSION['config']->get_pwdhash()) {
|
|
|
|
case 'CRYPT':
|
2003-10-05 10:51:01 +00:00
|
|
|
$hash = "{CRYPT}" . crypt($password);
|
2003-10-04 13:26:37 +00:00
|
|
|
break;
|
|
|
|
case 'MD5':
|
2004-01-03 18:19:21 +00:00
|
|
|
$hash = "{MD5}" . base64_encode(hex2bin(md5($password)));
|
2003-10-04 13:26:37 +00:00
|
|
|
break;
|
|
|
|
case 'SMD5':
|
2004-01-03 18:19:21 +00:00
|
|
|
$salt0 = substr(pack("h*", md5($_SESSION['ldap']->rand)), 0, 8);
|
|
|
|
$salt = substr(pack("H*", md5($salt0 . $password)), 0, 4);
|
|
|
|
$hash = "{SMD5}" . base64_encode(hex2bin(md5($password . $salt)) . $salt);
|
2003-10-04 13:26:37 +00:00
|
|
|
break;
|
|
|
|
case 'SHA':
|
2004-01-03 18:19:21 +00:00
|
|
|
// PHP 4.3+ can use sha1() function
|
|
|
|
if (function_exists(sha1)) {
|
|
|
|
$hash = "{SHA}" . base64_encode(hex2bin(sha1($password)));
|
|
|
|
}
|
|
|
|
// otherwise use MHash
|
|
|
|
elseif (function_exists(mHash)) {
|
|
|
|
$hash = "{SHA}" . base64_encode(mHash(MHASH_SHA1, $password));
|
|
|
|
}
|
|
|
|
// if SHA1 is not possible use crypt()
|
|
|
|
else {
|
|
|
|
$hash = "{CRYPT}" . crypt($password);
|
|
|
|
}
|
2003-10-04 13:26:37 +00:00
|
|
|
break;
|
|
|
|
case 'SSHA':
|
2004-01-03 18:19:21 +00:00
|
|
|
// PHP 4.3+ can use sha1() function
|
|
|
|
if (function_exists(sha1)) {
|
|
|
|
$salt0 = substr(pack("h*", md5($_SESSION['ldap']->rand)), 0, 8);
|
|
|
|
$salt = substr(pack("H*", sha1($salt0 . $password)), 0, 4);
|
|
|
|
$hash = "{SSHA}" . base64_encode(hex2bin(sha1($password . $salt)) . $salt);
|
|
|
|
}
|
|
|
|
// otherwise use MHash
|
|
|
|
elseif (function_exists(mHash)) {
|
|
|
|
$salt = mhash_keygen_s2k(MHASH_SHA1, $password, substr(pack("h*", md5($_SESSION['ldap']->rand)), 0, 8), 4);
|
|
|
|
$hash = base64_encode(mHash(MHASH_SHA1, $password . $salt) . $salt);
|
|
|
|
$hash = "{SSHA}" . $hash;
|
|
|
|
}
|
|
|
|
// if SSHA is not possible use crypt()
|
|
|
|
else {
|
|
|
|
$hash = "{CRYPT}" . crypt($password);
|
|
|
|
}
|
2003-10-04 13:26:37 +00:00
|
|
|
break;
|
2003-10-05 10:51:01 +00:00
|
|
|
case 'PLAIN':
|
|
|
|
$hash = $password;
|
|
|
|
break;
|
2003-10-04 13:26:37 +00:00
|
|
|
// use SSHA if the setting is invalid
|
|
|
|
default:
|
2004-01-03 18:19:21 +00:00
|
|
|
// PHP 4.3+ can use sha1() function
|
|
|
|
if (function_exists(sha1)) {
|
|
|
|
$salt0 = substr(pack("h*", md5($_SESSION['ldap']->rand)), 0, 8);
|
|
|
|
$salt = substr(pack("H*", sha1($salt0 . $password)), 0, 4);
|
|
|
|
$hash = "{SSHA}" . base64_encode(hex2bin(sha1($password . $salt)) . $salt);
|
|
|
|
}
|
|
|
|
// otherwise use MHash
|
|
|
|
elseif (function_exists(mHash)) {
|
|
|
|
$salt = mhash_keygen_s2k(MHASH_SHA1, $password, substr(pack("h*", md5($_SESSION['ldap']->rand)), 0, 8), 4);
|
|
|
|
$hash = base64_encode(mHash(MHASH_SHA1, $password . $salt) . $salt);
|
|
|
|
$hash = "{SSHA}" . $hash;
|
|
|
|
}
|
|
|
|
// if SSHA is not possible use crypt()
|
|
|
|
else {
|
|
|
|
$hash = "{CRYPT}" . crypt($password);
|
|
|
|
}
|
2003-10-04 13:26:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// enable/disable password
|
2004-04-09 10:21:43 +00:00
|
|
|
if (! $enabled) return pwd_disable($hash);
|
2003-10-04 13:26:37 +00:00
|
|
|
else return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Marks an password hash as enabled and returns the new hash string
|
|
|
|
*
|
|
|
|
* @param string $hash hash value to enable
|
|
|
|
* @return string enabled password hash
|
|
|
|
*/
|
2003-10-04 13:26:37 +00:00
|
|
|
function pwd_enable($hash) {
|
2004-04-09 10:21:43 +00:00
|
|
|
// check if password is disabled (old wrong LAM method)
|
|
|
|
if ((substr($hash, 0, 2) == "!{") || ((substr($hash, 0, 2) == "*{"))) {
|
2003-10-04 13:26:37 +00:00
|
|
|
return substr($hash, 1, strlen($hash));
|
|
|
|
}
|
2004-04-09 10:21:43 +00:00
|
|
|
// check for "!" or "*" at beginning of password hash
|
2003-10-04 13:26:37 +00:00
|
|
|
else {
|
2004-04-09 10:21:43 +00:00
|
|
|
if (substr($hash, 0, 1) == "{") {
|
|
|
|
$pos = strpos($hash, "}");
|
|
|
|
if ((substr($hash, $pos + 1, 1) == "!") || (substr($hash, $pos + 1, 1) == "*")) {
|
|
|
|
// enable hash
|
|
|
|
return substr($hash, 0, $pos + 1) . substr($hash, $pos + 2, strlen($hash));
|
|
|
|
}
|
|
|
|
else return $hash; // not disabled
|
|
|
|
}
|
|
|
|
else return $hash; // password is plain text
|
2003-10-04 13:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Marks an password hash as disabled and returns the new hash string
|
|
|
|
*
|
|
|
|
* @param string $hash hash value to disable
|
|
|
|
* @return string disabled hash value
|
|
|
|
*/
|
2003-10-04 13:26:37 +00:00
|
|
|
function pwd_disable($hash) {
|
2004-04-09 10:21:43 +00:00
|
|
|
// check if password is disabled (old wrong LAM method)
|
|
|
|
if ((substr($hash, 0, 2) == "!{") || ((substr($hash, 0, 2) == "*{"))) {
|
2003-10-04 13:26:37 +00:00
|
|
|
return $hash;
|
|
|
|
}
|
2004-04-09 10:21:43 +00:00
|
|
|
// check for "!" or "*" at beginning of password hash
|
2003-10-04 13:26:37 +00:00
|
|
|
else {
|
2004-04-09 10:21:43 +00:00
|
|
|
if (substr($hash, 0, 1) == "{") {
|
|
|
|
$pos = strpos($hash, "}");
|
|
|
|
if ((substr($hash, $pos + 1, 1) == "!") || (substr($hash, $pos + 1, 1) == "*")) {
|
|
|
|
// hash already disabled
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
else return substr($hash, 0, $pos + 1) . "!" . substr($hash, $pos + 1, strlen($hash)); // not disabled
|
|
|
|
}
|
|
|
|
else return $hash; // password is plain text
|
2003-10-04 13:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Checks if a password hash is enabled/disabled
|
|
|
|
*
|
|
|
|
* @param string $hash password hash to check
|
|
|
|
* @return boolean true if the password is marked as enabled
|
|
|
|
*/
|
2003-10-04 13:26:37 +00:00
|
|
|
function pwd_is_enabled($hash) {
|
2004-04-09 10:21:43 +00:00
|
|
|
// disabled passwords have a "!" or "*" at the beginning (old wrong LAM method)
|
|
|
|
if ((substr($hash, 0, 2) == "!{") || ((substr($hash, 0, 2) == "*{"))) return false;
|
|
|
|
if (substr($hash, 0, 1) == "{") {
|
|
|
|
$pos = strrpos($hash, "}");
|
|
|
|
// check if hash starts with "!" or "*"
|
|
|
|
if ((substr($hash, $pos + 1, 1) == "!") || (substr($hash, $pos + 1, 1) == "*")) return false;
|
|
|
|
else return true;
|
|
|
|
}
|
2003-10-04 13:26:37 +00:00
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Ldap manages connection to LDAP and includes several helper functions.
|
|
|
|
*
|
|
|
|
* @package LDAP
|
|
|
|
*/
|
2003-03-05 16:05:23 +00:00
|
|
|
class Ldap{
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Object of Config to access preferences */
|
2003-07-30 20:46:07 +00:00
|
|
|
var $conf;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Server handle */
|
2003-07-30 20:46:07 +00:00
|
|
|
var $server;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** LDAP username used for bind */
|
2003-07-30 20:46:07 +00:00
|
|
|
var $username;
|
2004-05-31 14:04:00 +00:00
|
|
|
/** LDAP password used for bind */
|
2003-07-30 20:46:07 +00:00
|
|
|
var $password;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Array with all objectClass strings from the LDAP server */
|
2003-10-12 17:07:03 +00:00
|
|
|
var $objectClasses;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
// Capabilities of the LDAP server
|
|
|
|
/** Host attribute in inetOrgPerson */
|
|
|
|
var $supports_unix_hosts = false;
|
|
|
|
/** ObjectClass sambaAccount */
|
|
|
|
var $supports_samba2_schema = false;
|
|
|
|
/** ObjectClass sambaSamAccount */
|
|
|
|
var $supports_samba3_schema = false;
|
2003-10-12 17:07:03 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Random number (changes on every page request) */
|
2003-10-16 18:49:25 +00:00
|
|
|
var $rand;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* @param object $config an object of class Config
|
|
|
|
*/
|
2003-07-30 20:46:07 +00:00
|
|
|
function Ldap($config) {
|
2003-08-08 15:35:59 +00:00
|
|
|
setlanguage();
|
2003-07-30 20:46:07 +00:00
|
|
|
if (is_object($config)) $this->conf = $config;
|
|
|
|
else return false;
|
2003-10-16 18:49:25 +00:00
|
|
|
mt_srand((double)microtime()*1000000);
|
|
|
|
$this->rand = mt_rand();
|
2003-07-30 20:46:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Connects to the server using the given username and password
|
|
|
|
*
|
|
|
|
* @param string $user user name
|
|
|
|
* @param string $passwd password
|
|
|
|
* @return mixed if connect succeeds the server handle is returned, else false
|
|
|
|
*/
|
2003-07-30 20:46:07 +00:00
|
|
|
function connect($user, $passwd) {
|
|
|
|
// close any prior connection
|
|
|
|
@$this->close();
|
|
|
|
// do not allow anonymous bind
|
|
|
|
if ((!$user)||($user == "")||(!$passwd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// save password und username encrypted
|
2004-01-10 11:08:10 +00:00
|
|
|
$this->encrypt_login($user, $passwd);
|
2003-10-18 16:45:15 +00:00
|
|
|
$this->server = @ldap_connect($this->conf->get_ServerURL());
|
2003-07-30 20:46:07 +00:00
|
|
|
if ($this->server) {
|
|
|
|
// use LDAPv3
|
|
|
|
ldap_set_option($this->server, LDAP_OPT_PROTOCOL_VERSION, 3);
|
2003-08-16 08:30:05 +00:00
|
|
|
// start TLS if possible
|
2003-08-16 16:54:11 +00:00
|
|
|
if (function_exists('ldap_start_tls')) {
|
|
|
|
@ldap_start_tls($this->server);
|
|
|
|
// connect without TLS if it failed
|
2004-05-23 08:16:38 +00:00
|
|
|
if (ldap_errno($this->server) != 0) {
|
2003-08-16 16:54:11 +00:00
|
|
|
@ldap_close($this->server);
|
|
|
|
$this->server = @ldap_connect($this->conf->get_ServerURL());
|
|
|
|
ldap_set_option($this->server, LDAP_OPT_PROTOCOL_VERSION, 3);
|
|
|
|
}
|
|
|
|
}
|
2003-07-30 20:46:07 +00:00
|
|
|
$bind = @ldap_bind($this->server, $user, $passwd);
|
|
|
|
if ($bind) {
|
2003-10-12 17:07:03 +00:00
|
|
|
// read objectClasses from server and update capabilities if needed
|
|
|
|
if (! $this->objectClasses) {
|
|
|
|
$this->updateClasses();
|
|
|
|
$this->updateCapabilities();
|
|
|
|
}
|
2003-12-03 23:03:10 +00:00
|
|
|
// return success number
|
|
|
|
return ldap_errno($this->server);
|
2003-07-30 20:46:07 +00:00
|
|
|
}
|
2003-12-03 23:03:10 +00:00
|
|
|
// return error number
|
|
|
|
else return ldap_errno($this->server);
|
2003-07-30 20:46:07 +00:00
|
|
|
}
|
2003-12-03 23:03:10 +00:00
|
|
|
else return false;
|
2003-07-30 20:46:07 +00:00
|
|
|
}
|
2003-03-05 16:05:23 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Closes connection to server */
|
2003-07-26 12:37:31 +00:00
|
|
|
function close() {
|
2003-08-03 19:52:05 +00:00
|
|
|
@ldap_close($this->server);
|
2003-06-08 18:58:01 +00:00
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Searches LDAP for a specific user name and returns its DN entry
|
|
|
|
*
|
|
|
|
* @param string $name user name
|
|
|
|
* @return string DN
|
|
|
|
*/
|
2003-07-26 12:37:31 +00:00
|
|
|
function search_username($name) {
|
2003-09-21 19:32:15 +00:00
|
|
|
$filter = "(uid=$name)";
|
2003-07-26 12:37:31 +00:00
|
|
|
$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;
|
|
|
|
}
|
2003-05-13 15:54:37 +00:00
|
|
|
}
|
2003-04-21 15:45:40 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Returns an array with all organizational units under the given suffix
|
|
|
|
*
|
|
|
|
* @param string $suffix search suffix
|
|
|
|
* @return array DNs of organizational units
|
|
|
|
*/
|
2003-06-14 12:16:59 +00:00
|
|
|
function search_units($suffix) {
|
2003-07-26 12:37:31 +00:00
|
|
|
$ret = array();
|
2003-12-10 12:00:05 +00:00
|
|
|
$sr = @ldap_search($this->server(), $suffix, "objectClass=organizationalunit", array("DN"));
|
2003-07-26 12:37:31 +00:00
|
|
|
if ($sr) {
|
2003-12-10 12:00:05 +00:00
|
|
|
$units = ldap_get_entries($this->server, $sr);
|
2003-07-26 12:37:31 +00:00
|
|
|
// extract Dns
|
|
|
|
for ($i = 0; $i < sizeof($units); $i++) {
|
|
|
|
if ($units[$i]['dn']) $ret[] = $units[$i]['dn'];
|
|
|
|
}
|
2003-06-28 09:30:21 +00:00
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
// add root suffix if needed
|
2003-08-09 10:56:46 +00:00
|
|
|
$found = false;
|
2003-07-26 12:37:31 +00:00
|
|
|
for ($i = 0; $i < sizeof($ret); $i++) { // search suffix case-intensitive
|
|
|
|
if (strtolower($suffix) == strtolower($ret[$i])) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
2003-06-30 19:50:42 +00:00
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
if (!$found) {
|
|
|
|
$ret[] = $suffix;
|
|
|
|
}
|
|
|
|
usort($ret, array($this,"cmp_array"));
|
|
|
|
return $ret;
|
2003-06-30 19:50:42 +00:00
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Returns an array with all Samba 3 domain entries under the given suffix
|
|
|
|
*
|
|
|
|
* @param string $suffix search suffix
|
|
|
|
* @return array list of samba3domain objects
|
|
|
|
*/
|
2003-07-26 12:37:31 +00:00
|
|
|
function search_domains($suffix) {
|
|
|
|
$ret = array();
|
|
|
|
$attr = array("DN", "sambaDomainName", "sambaSID", "sambaNextRid", "sambaNextGroupRid",
|
|
|
|
"sambaNextUserRid", "sambaAlgorithmicRidBase");
|
2003-12-10 12:00:05 +00:00
|
|
|
$sr = @ldap_search($this->server(), $suffix, "objectClass=sambaDomain", $attr);
|
2003-07-26 12:37:31 +00:00
|
|
|
if ($sr) {
|
2003-12-10 12:00:05 +00:00
|
|
|
$units = ldap_get_entries($this->server, $sr);
|
2003-07-26 12:37:31 +00:00
|
|
|
// 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;
|
2003-06-14 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Reads the array of objectClasses from the LDAP server */
|
2003-10-12 17:07:03 +00:00
|
|
|
function updateClasses() {
|
|
|
|
// read from default cn
|
|
|
|
$sr = @ldap_read($this->server, 'cn=subschema', '(objectClass=*)', array('objectclasses'));
|
|
|
|
// if default was not correct check different cn
|
|
|
|
if (!$sr) $sr = @ldap_read($this->server, 'cn=schema', '(objectClass=*)', array('objectclasses'));
|
|
|
|
if ($sr) {
|
|
|
|
// get search result and save it
|
|
|
|
$info = @ldap_get_entries($this->server,$sr);
|
|
|
|
if ($info) {
|
|
|
|
$this->objectClasses = $info[0]['objectclasses'];
|
|
|
|
array_shift($this->objectClasses);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if search failed save empty result
|
|
|
|
$this->objectClasses = array();
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Updates the capabilities values (var $supports_*) */
|
2003-10-12 17:07:03 +00:00
|
|
|
function updateCapabilities() {
|
|
|
|
for ($i = 0; $i < sizeof($this->objectClasses); $i++) {
|
|
|
|
$line = $this->objectClasses[$i];
|
|
|
|
// search keywords
|
|
|
|
if (strpos($line, "NAME 'inetOrgPerson'") && strpos($line, " host ")) $this->supports_unix_hosts = true;
|
|
|
|
if (strpos($line, "NAME 'sambaAccount'")) $this->supports_samba2_schema = true;
|
|
|
|
if (strpos($line, "NAME 'sambaSamAccount'")) $this->supports_samba3_schema = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Returns the LDAP connection handle
|
|
|
|
*
|
|
|
|
* @return object connection handle
|
|
|
|
*/
|
2003-07-26 12:37:31 +00:00
|
|
|
function server() {
|
|
|
|
return $this->server;
|
|
|
|
}
|
2003-06-28 08:05:58 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Closes connection to LDAP server before serialization */
|
2003-07-26 12:37:31 +00:00
|
|
|
function __sleep() {
|
|
|
|
$this->close();
|
|
|
|
// define which attributes to save
|
2003-10-12 17:07:03 +00:00
|
|
|
return array("conf", "username", "password", "ldapUserAttributes", "ldapGroupAttributes",
|
|
|
|
"ldapHostAttributes", "objectClasses", "supports_unix_hosts", "supports_samba2_schema",
|
2003-10-16 18:49:25 +00:00
|
|
|
"supports_samba3_schema", "rand");
|
2003-07-26 12:37:31 +00:00
|
|
|
}
|
2003-06-28 08:05:58 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Reconnects to LDAP server when deserialized */
|
2003-07-26 12:37:31 +00:00
|
|
|
function __wakeup() {
|
2004-01-10 11:08:10 +00:00
|
|
|
$data = $this->decrypt_login();
|
2003-07-26 12:37:31 +00:00
|
|
|
$this->connect($data[0], $data[1]);
|
2003-10-16 18:49:25 +00:00
|
|
|
// change random number
|
|
|
|
mt_srand($this->rand + (microtime() * 1000000));
|
|
|
|
$this->rand = mt_rand();
|
2003-11-03 14:32:27 +00:00
|
|
|
// delete PDF files which are older than 10 min
|
|
|
|
if (isset($_SESSION['lampath'])) {
|
|
|
|
$relpath = $_SESSION['lampath'] . 'tmp/';
|
|
|
|
$time = time();
|
|
|
|
$dir = @opendir($relpath);
|
|
|
|
while ($file = @readdir($dir)) {
|
|
|
|
if (substr($file, -4) == '.pdf') {
|
|
|
|
$path = $relpath . $file;
|
|
|
|
if ($time - filemtime($path) > 600) {
|
|
|
|
@unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@closedir($h);
|
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
}
|
2003-06-28 08:05:58 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Calculates a new value for rand */
|
2004-01-03 18:19:21 +00:00
|
|
|
function new_rand() {
|
|
|
|
// change random number
|
|
|
|
mt_srand($this->rand + (microtime() * 1000000));
|
|
|
|
$this->rand = mt_rand();
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Encrypts a string
|
|
|
|
*
|
|
|
|
* @param string $data string to encrypt
|
|
|
|
* @return object encrypted string
|
|
|
|
*/
|
2004-01-10 11:08:10 +00:00
|
|
|
function encrypt($data) {
|
|
|
|
// use MCrypt if available
|
|
|
|
if (function_exists(mcrypt_create_iv)) {
|
|
|
|
// read key and iv from cookie
|
|
|
|
$iv = base64_decode($_COOKIE["IV"]);
|
|
|
|
$key = base64_decode($_COOKIE["Key"]);
|
|
|
|
// encrypt string
|
|
|
|
return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv);
|
|
|
|
}
|
|
|
|
// use Blowfish if MCrypt is not available
|
|
|
|
else {
|
|
|
|
// read key and iv from cookie
|
|
|
|
$iv = base64_decode($_COOKIE["IV"]);
|
|
|
|
$key = base64_decode($_COOKIE["Key"]);
|
|
|
|
$b_key = $iv . $key;
|
|
|
|
// encrypt string
|
|
|
|
$b_fish = new Cipher_blowfish();
|
|
|
|
return $b_fish->encrypt($data, $b_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Decrypts a string
|
|
|
|
*
|
|
|
|
* @param object $data string to decrypt
|
|
|
|
* @return string decrypted string
|
|
|
|
*/
|
2004-01-10 11:08:10 +00:00
|
|
|
function decrypt($data) {
|
|
|
|
// use MCrypt if available
|
|
|
|
if (function_exists(mcrypt_create_iv)) {
|
|
|
|
// read key and iv from cookie
|
|
|
|
$iv = base64_decode($_COOKIE["IV"]);
|
|
|
|
$key = base64_decode($_COOKIE["Key"]);
|
|
|
|
// decrypt string
|
|
|
|
$ret = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv);
|
|
|
|
$ret = str_replace(chr(00), "", $ret);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
// use Blowfish if MCrypt is not available
|
|
|
|
else {
|
|
|
|
// read key and iv from cookie
|
|
|
|
$iv = base64_decode($_COOKIE["IV"]);
|
|
|
|
$key = base64_decode($_COOKIE["Key"]);
|
|
|
|
$b_key = $iv . $key;
|
|
|
|
// decrypt string
|
|
|
|
$b_fish = new Cipher_blowfish();
|
|
|
|
return $b_fish->decrypt($data, $b_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Encrypts username and password
|
|
|
|
*
|
|
|
|
* @param string $username LDAP user name
|
|
|
|
* @param string $password LDAP password
|
|
|
|
*/
|
2004-01-10 11:08:10 +00:00
|
|
|
function encrypt_login($username, $password) {
|
2003-07-02 17:58:55 +00:00
|
|
|
// encrypt username and password
|
2004-01-10 11:08:10 +00:00
|
|
|
$this->username = base64_encode($this->encrypt($username));
|
|
|
|
$this->password = base64_encode($this->encrypt($password));
|
2003-07-02 17:58:55 +00:00
|
|
|
}
|
2003-03-19 18:39:09 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Decrypts username and password
|
|
|
|
*
|
|
|
|
* @return array array(user name, password)
|
|
|
|
*/
|
2004-01-10 11:08:10 +00:00
|
|
|
function decrypt_login() {
|
2003-07-02 17:58:55 +00:00
|
|
|
// decrypt username and password
|
2004-01-10 11:08:10 +00:00
|
|
|
$username = $this->decrypt(base64_decode($this->username));
|
|
|
|
$password = $this->decrypt(base64_decode($this->password));
|
2003-07-02 17:58:55 +00:00
|
|
|
$ret = array($username, $password);
|
|
|
|
return $ret;
|
|
|
|
}
|
2003-03-28 15:45:42 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Closes connection to LDAP server and deletes encrypted username/password */
|
2003-07-02 17:58:55 +00:00
|
|
|
function destroy() {
|
|
|
|
$this->close();
|
|
|
|
$this->username="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
$this->password="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
}
|
2003-03-19 18:39:09 +00:00
|
|
|
|
2003-04-01 14:17:32 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Helper function to sort the unit DNs
|
|
|
|
*
|
|
|
|
* @param string $a first argument to compare
|
|
|
|
* @param string $b second argument to compare
|
|
|
|
* @return integer 0 if equal, 1 if $a is greater, -1 if $b is greater
|
|
|
|
*/
|
2003-07-02 17:58:55 +00:00
|
|
|
function cmp_array($a, $b) {
|
|
|
|
// split DNs
|
|
|
|
$array_a = explode(",", $a);
|
|
|
|
$array_b = explode(",", $b);
|
|
|
|
$len_a = sizeof($array_a);
|
|
|
|
$len_b = sizeof($array_b);
|
|
|
|
// check how many parts to compare
|
|
|
|
$len = min($len_a, $len_b);
|
|
|
|
// compare from last part on
|
|
|
|
for ($i = 0; $i < $len; $i++) {
|
|
|
|
// get parts to compare
|
|
|
|
$part_a = strtolower($array_a[$len_a - $i - 1]);
|
|
|
|
$part_b = strtolower($array_b[$len_b - $i - 1]);
|
|
|
|
// compare parts
|
|
|
|
if ($part_a == $part_b) { // part is identical
|
|
|
|
if ($i == ($len - 1)) {
|
|
|
|
if ($len_a > $len_b) return 1;
|
|
|
|
elseif ($len_a < $len_b) return -1;
|
|
|
|
else return 0; // DNs are identical
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($part_a == max($part_a, $part_b)) return 1;
|
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Helper function to sort the domains
|
|
|
|
*
|
|
|
|
* @param string $a first argument to compare
|
|
|
|
* @param string $b second argument to compare
|
|
|
|
* @return integer 0 if equal, 1 if $a is greater, -1 if $b is greater
|
|
|
|
*/
|
2003-07-26 12:37:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-05 16:05:23 +00:00
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/**
|
|
|
|
* Represents a Samba 3 domain entry
|
|
|
|
*
|
|
|
|
* @package LDAP
|
|
|
|
*/
|
2003-07-26 12:37:31 +00:00
|
|
|
class samba3domain {
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** DN */
|
2003-07-26 12:37:31 +00:00
|
|
|
var $dn;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Domain name */
|
2003-07-26 12:37:31 +00:00
|
|
|
var $name;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Domain SID */
|
2003-07-26 12:37:31 +00:00
|
|
|
var $SID;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Next RID */
|
2003-07-26 12:37:31 +00:00
|
|
|
var $nextRID;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Next user RID */
|
2003-07-26 12:37:31 +00:00
|
|
|
var $nextUserRID;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Next group RID */
|
2003-07-26 12:37:31 +00:00
|
|
|
var $nextGroupRID;
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** RID base to calculate RIDs, default 1000 */
|
|
|
|
var $RIDbase = 1000;
|
2003-07-26 12:37:31 +00:00
|
|
|
}
|
2003-03-05 16:05:23 +00:00
|
|
|
|
2003-04-01 14:17:32 +00:00
|
|
|
?>
|