2003-03-05 16:05:23 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
|
2009-10-27 18:47:12 +00:00
|
|
|
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
|
2020-03-19 19:42:36 +00:00
|
|
|
Copyright (C) 2003 - 2020 Roland Gruber
|
2003-03-05 16:05:23 +00:00
|
|
|
|
|
|
|
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 */
|
2018-12-23 16:52:56 +00:00
|
|
|
include_once(__DIR__ . "/config.inc");
|
2003-03-05 16:05:23 +00:00
|
|
|
|
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 */
|
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;
|
2015-06-24 17:40:20 +00:00
|
|
|
|
2012-07-15 12:05:47 +00:00
|
|
|
/** LDAP connection established */
|
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
|
|
|
/**
|
2012-07-15 12:05:47 +00:00
|
|
|
* Creates a new LDAP object.
|
2015-06-24 17:40:20 +00:00
|
|
|
*
|
2004-05-31 14:04:00 +00:00
|
|
|
* @param object $config an object of class Config
|
|
|
|
*/
|
2018-12-29 20:06:27 +00:00
|
|
|
public function __construct($config) {
|
2013-07-21 11:34:31 +00:00
|
|
|
if (is_object($config)) {
|
|
|
|
$this->conf = $config;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
2015-06-12 18:28:25 +00:00
|
|
|
* @return mixed if connect succeeds the 0 is returned, else false or error number
|
2004-05-31 14:04:00 +00:00
|
|
|
*/
|
2018-12-29 20:06:27 +00:00
|
|
|
public 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);
|
2017-10-07 12:45:15 +00:00
|
|
|
$startTLS = $this->conf->getUseTLS();
|
|
|
|
$startTLS = ($startTLS === 'yes');
|
|
|
|
$this->server = connectToLDAP($this->conf->get_ServerURL(), $startTLS);
|
|
|
|
if ($this->server != null) {
|
2014-01-12 10:18:35 +00:00
|
|
|
// referral following
|
|
|
|
$followReferrals = ($this->conf->getFollowReferrals() === 'true') ? 1 : 0;
|
|
|
|
ldap_set_option($this->server,LDAP_OPT_REFERRALS, $followReferrals);
|
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
|
2019-08-05 19:56:06 +00:00
|
|
|
else {
|
|
|
|
return ldap_errno($this->server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 */
|
2018-12-29 20:06:27 +00:00
|
|
|
public function close() {
|
2012-02-05 10:38:59 +00:00
|
|
|
if ($this->server != null) {
|
|
|
|
@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
|
|
|
|
*/
|
2018-12-29 20:06:27 +00:00
|
|
|
public function server() {
|
2007-07-21 08:27:13 +00:00
|
|
|
if (!$this->is_connected) {
|
2019-08-05 19:56:06 +00:00
|
|
|
$this->connect($this->getUserName(), $this->getPassword());
|
2007-07-21 08:27:13 +00:00
|
|
|
$this->is_connected = true;
|
|
|
|
}
|
2003-07-26 12:37:31 +00:00
|
|
|
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 */
|
2018-12-29 20:06:27 +00:00
|
|
|
public function __sleep() {
|
2003-07-26 12:37:31 +00:00
|
|
|
$this->close();
|
|
|
|
// define which attributes to save
|
2013-07-21 11:34:31 +00:00
|
|
|
return array("conf", "username", "password");
|
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 */
|
2018-12-29 20:06:27 +00:00
|
|
|
public function __wakeup() {
|
2007-07-21 08:27:13 +00:00
|
|
|
$this->is_connected = false;
|
2013-01-28 21:14:26 +00:00
|
|
|
// delete PDF files and images which are older than 15 min
|
2010-04-01 18:12:07 +00:00
|
|
|
$tmpDir = dirname(__FILE__) . '/../tmp/';
|
|
|
|
$time = time();
|
|
|
|
$dir = @opendir($tmpDir);
|
|
|
|
$file = @readdir($dir);
|
|
|
|
while ($file) {
|
2013-01-28 21:14:26 +00:00
|
|
|
$path = $tmpDir . $file;
|
2020-03-19 19:42:36 +00:00
|
|
|
if ((substr($file, 0, 1) != '.')
|
|
|
|
&& !is_dir($path)
|
|
|
|
&& ($time - filemtime($path) > 900)) {
|
|
|
|
@unlink($path);
|
2003-11-03 14:32:27 +00:00
|
|
|
}
|
2010-04-01 18:12:07 +00:00
|
|
|
$file = @readdir($dir);
|
2003-11-03 14:32:27 +00:00
|
|
|
}
|
2010-04-01 18:12:07 +00:00
|
|
|
@closedir($dir);
|
2012-01-26 20:02:38 +00:00
|
|
|
// clean internal files that are older than 24 hours
|
|
|
|
$tmpDir = dirname(__FILE__) . '/../tmp/internal/';
|
|
|
|
$time = time();
|
|
|
|
$dir = @opendir($tmpDir);
|
|
|
|
$file = @readdir($dir);
|
|
|
|
while ($file) {
|
2018-12-28 10:19:15 +00:00
|
|
|
if (substr($file, -4) == '.tmp') {
|
2012-01-26 20:02:38 +00:00
|
|
|
$path = $tmpDir . $file;
|
|
|
|
if ($time - filemtime($path) > (3600 * 24)) {
|
|
|
|
@unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$file = @readdir($dir);
|
|
|
|
}
|
|
|
|
@closedir($dir);
|
2003-07-26 12:37:31 +00:00
|
|
|
}
|
2003-06-28 08:05:58 +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
|
|
|
|
*/
|
2018-12-29 20:06:27 +00:00
|
|
|
public function encrypt_login($username, $password) {
|
2003-07-02 17:58:55 +00:00
|
|
|
// encrypt username and password
|
2016-08-07 08:40:36 +00:00
|
|
|
$this->username = base64_encode(lamEncrypt($username));
|
|
|
|
$this->password = base64_encode(lamEncrypt($password));
|
2003-07-02 17:58:55 +00:00
|
|
|
}
|
2003-03-19 18:39:09 +00:00
|
|
|
|
2018-12-29 20:06:27 +00:00
|
|
|
/**
|
|
|
|
* Returns the LDAP user name.
|
|
|
|
*
|
|
|
|
* @return string user name
|
|
|
|
*/
|
|
|
|
public function getUserName() {
|
|
|
|
return lamDecrypt(base64_decode($this->username));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the LDAP password.
|
|
|
|
*
|
|
|
|
* @return string password
|
|
|
|
*/
|
|
|
|
public function getPassword() {
|
|
|
|
return lamDecrypt(base64_decode($this->password));
|
|
|
|
}
|
|
|
|
|
2004-05-31 14:04:00 +00:00
|
|
|
/** Closes connection to LDAP server and deletes encrypted username/password */
|
2018-12-29 20:06:27 +00:00
|
|
|
public function destroy() {
|
2003-07-02 17:58:55 +00:00
|
|
|
$this->close();
|
|
|
|
$this->username="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
$this->password="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
}
|
2003-03-19 18:39:09 +00:00
|
|
|
|
2003-04-01 14:17:32 +00:00
|
|
|
|
2003-07-26 12:37:31 +00:00
|
|
|
}
|
2003-03-05 16:05:23 +00:00
|
|
|
|
2003-04-01 14:17:32 +00:00
|
|
|
?>
|