LDAPAccountManager/lam/lib/ldap.inc

277 lines
7.4 KiB
PHP
Raw Normal View History

<?php
/*
$Id$
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2011-06-26 10:33:06 +00:00
Copyright (C) 2003 - 2011 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.
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.
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
/**
* Converts a HEX string to a binary value
*
* @param string $value HEX string
* @return binary result binary
*/
function hex2bin($value) {
return pack("H*", $value);
}
2003-08-08 15:35:59 +00:00
2003-10-04 13:26:37 +00:00
2004-05-31 14:04:00 +00:00
/**
* Ldap manages connection to LDAP and includes several helper functions.
*
* @package LDAP
*/
class Ldap{
2004-05-31 14:04:00 +00:00
/** Object of Config to access preferences */
2007-07-08 11:33:31 +00:00
private $conf;
2003-07-30 20:46:07 +00:00
2004-05-31 14:04:00 +00:00
/** Server handle */
2007-07-08 11:33:31 +00:00
private $server;
2007-07-21 08:27:13 +00:00
private $is_connected = false;
2003-07-30 20:46:07 +00:00
2004-05-31 14:04:00 +00:00
/** LDAP username used for bind */
2007-07-08 11:33:31 +00:00
private $username;
2004-05-31 14:04:00 +00:00
/** LDAP password used for bind */
2007-07-08 11:33:31 +00:00
private $password;
2003-07-30 20:46:07 +00:00
2004-05-31 14:04:00 +00:00
/** Random number (changes on every page request) */
2007-07-08 11:33:31 +00:00
private $rand;
2003-10-16 18:49:25 +00:00
2004-05-31 14:04:00 +00:00
/**
* @param object $config an object of class Config
*/
2007-12-28 16:08:56 +00:00
function __construct($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
2009-03-07 18:15:27 +00:00
* @param boolean $allowAnonymous specifies if anonymous binds are allowed
2004-05-31 14:04:00 +00:00
* @return mixed if connect succeeds the server handle is returned, else false
*/
2009-03-07 18:15:27 +00:00
function connect($user, $passwd, $allowAnonymous=false) {
2003-07-30 20:46:07 +00:00
// close any prior connection
@$this->close();
// do not allow anonymous bind
2009-03-07 18:15:27 +00:00
if (!$allowAnonymous && ((!$user)||($user == "")||(!$passwd))) {
2003-07-30 20:46:07 +00:00
return false;
}
// save password und username encrypted
2004-01-10 11:08:10 +00:00
$this->encrypt_login($user, $passwd);
$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);
2009-05-03 17:31:39 +00:00
// start TLS if specified
$useTLS = $this->conf->getUseTLS();
if (isset($useTLS) && ($useTLS == "yes")) {
@ldap_start_tls($this->server);
2004-05-23 08:16:38 +00:00
if (ldap_errno($this->server) != 0) {
2011-10-24 15:24:35 +00:00
logNewMessage(LOG_ERR, 'Unable to start TLS encryption. Please check if your server certificate is valid and if the LDAP server supports TLS at all.');
2009-05-03 17:31:39 +00:00
return ldap_errno($this->server);
}
}
2003-07-30 20:46:07 +00:00
$bind = @ldap_bind($this->server, $user, $passwd);
if ($bind) {
2006-02-02 19:56:41 +00:00
$return = ldap_errno($this->server);
2009-11-03 20:57:53 +00:00
$this->is_connected = true;
2003-12-03 23:03:10 +00:00
// return success number
2006-02-02 19:56:41 +00:00
return $return;
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
}
2004-05-31 14:04:00 +00:00
/** Closes connection to server */
2003-07-26 12:37:31 +00:00
function close() {
@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
/**
* Returns the LDAP connection handle
*
* @return object connection handle
*/
2003-07-26 12:37:31 +00:00
function server() {
2007-07-21 08:27:13 +00:00
if (!$this->is_connected) {
$data = $this->decrypt_login();
$this->connect($data[0], $data[1]);
$this->is_connected = true;
}
2003-07-26 12:37:31 +00:00
return $this->server;
}
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
2007-11-14 13:07:11 +00:00
return array("conf", "username", "password", "rand");
2003-07-26 12:37:31 +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() {
2007-07-21 08:27:13 +00:00
$this->is_connected = false;
2003-10-16 18:49:25 +00:00
// change random number
mt_srand($this->rand + (microtime() * 1000000));
$this->rand = mt_rand();
2010-04-01 18:12:07 +00:00
// delete PDF files and images which are older than 10 min
$tmpDir = dirname(__FILE__) . '/../tmp/';
$time = time();
$dir = @opendir($tmpDir);
$file = @readdir($dir);
while ($file) {
if ((substr($file, -4) == '.pdf') || (substr($file, -4) == '.jpg')) {
$path = $tmpDir . $file;
if ($time - filemtime($path) > 600) {
@unlink($path);
}
}
2010-04-01 18:12:07 +00:00
$file = @readdir($dir);
}
2010-04-01 18:12:07 +00:00
@closedir($dir);
2003-07-26 12:37:31 +00:00
}
2005-02-27 12:40:06 +00:00
/**
* Calculates a new value for rand
*
* @return int New random value
*/
function new_rand() {
// change random number
mt_srand($this->rand + (microtime() * 1000000));
2005-02-27 12:40:06 +00:00
$r = mt_rand();
$this->rand = $r;
return $r;
}
2004-05-31 14:04:00 +00:00
/**
* Encrypts a string
*
* @param string $data string to encrypt
2011-08-21 18:35:59 +00:00
* @param string $prefix prefix for cookie names
2004-05-31 14:04:00 +00:00
* @return object encrypted string
*/
2011-08-21 18:35:59 +00:00
public static function encrypt($data, $prefix='') {
2004-01-10 11:08:10 +00:00
// use MCrypt if available
if (function_exists('mcrypt_create_iv')) {
// MCrypt may have been enabled in a running session
2011-08-21 18:35:59 +00:00
if (!isset($_COOKIE[$prefix . "IV"]) || ($_COOKIE[$prefix . "IV"] == '')) return $data;
if ($_COOKIE[$prefix . "IV"] == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") {
2007-11-17 14:18:59 +00:00
return $data;
}
2004-01-10 11:08:10 +00:00
// read key and iv from cookie
2011-08-21 18:35:59 +00:00
$iv = base64_decode($_COOKIE[$prefix . "IV"]);
$key = base64_decode($_COOKIE[$prefix . "Key"]);
2004-01-10 11:08:10 +00:00
// encrypt string
return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, base64_encode($data), MCRYPT_MODE_ECB, $iv);
2004-01-10 11:08:10 +00:00
}
// otherwise do not encrypt
2004-01-10 11:08:10 +00:00
else {
return $data;
2004-01-10 11:08:10 +00:00
}
}
2004-05-31 14:04:00 +00:00
/**
* Decrypts a string
*
* @param object $data string to decrypt
2011-08-21 18:35:59 +00:00
* @param string $prefix prefix for cookie names
2004-05-31 14:04:00 +00:00
* @return string decrypted string
*/
2011-08-21 18:35:59 +00:00
public static function decrypt($data, $prefix='') {
2004-01-10 11:08:10 +00:00
// use MCrypt if available
if (function_exists('mcrypt_create_iv')) {
// MCrypt may have been enabled in a running session
2011-08-21 18:35:59 +00:00
if (!isset($_COOKIE[$prefix . "IV"]) || ($_COOKIE[$prefix . "IV"] == '')) return $data;
if ($_COOKIE[$prefix . "IV"] == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") {
2007-11-17 14:18:59 +00:00
return $data;
}
2004-01-10 11:08:10 +00:00
// read key and iv from cookie
2011-08-21 18:35:59 +00:00
$iv = base64_decode($_COOKIE[$prefix . "IV"]);
$key = base64_decode($_COOKIE[$prefix . "Key"]);
2004-01-10 11:08:10 +00:00
// decrypt string
$ret = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv);
$ret = base64_decode(str_replace(chr(00), "", $ret));
2004-01-10 11:08:10 +00:00
return $ret;
}
// otherwise do not decrypt
2004-01-10 11:08:10 +00:00
else {
return $data;
2004-01-10 11:08:10 +00:00
}
}
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
}
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;
}
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-07-26 12:37:31 +00:00
}
?>