| 1 |
<?php |
| 2 |
/* |
| 3 |
|
| 4 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 5 |
Copyright (C) 2003 - 2026 Roland Gruber |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 2 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* ldap.inc provides basic functions to connect to the OpenLDAP server. |
| 25 |
* |
| 26 |
* @package LDAP |
| 27 |
* @author Roland Gruber |
| 28 |
*/ |
| 29 |
|
| 30 |
/** Access to configuration data */ |
| 31 |
|
| 32 |
use LDAP\Connection; |
| 33 |
|
| 34 |
include_once(__DIR__ . "/config.inc"); |
| 35 |
|
| 36 |
/** |
| 37 |
* Ldap manages connection to LDAP and includes several helper functions. |
| 38 |
* |
| 39 |
* @package LDAP |
| 40 |
*/ |
| 41 |
class Ldap { |
| 42 |
|
| 43 |
/** Object of Config to access preferences */ |
| 44 |
private ?LAMConfig $conf = null; |
| 45 |
|
| 46 |
/** Server handle */ |
| 47 |
private ?Connection $server = null; |
| 48 |
|
| 49 |
/** LDAP connection established */ |
| 50 |
private bool $is_connected = false; |
| 51 |
|
| 52 |
/** LDAP username used for bind */ |
| 53 |
private ?string $username = null; |
| 54 |
/** LDAP password used for bind */ |
| 55 |
private ?string $password = null; |
| 56 |
/** is Active Directory */ |
| 57 |
private ?bool $isActiveDirectory = null; |
| 58 |
|
| 59 |
/** |
| 60 |
* Creates a new LDAP object. |
| 61 |
* |
| 62 |
* @param LAMConfig|null $config configuration |
| 63 |
*/ |
| 64 |
public function __construct(?LAMConfig $config) { |
| 65 |
if ($config !== null) { |
| 66 |
$this->conf = $config; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Connects to the server using the given username and password |
| 72 |
* |
| 73 |
* @param string|null $user user name |
| 74 |
* @param string|null $passwd password |
| 75 |
* @param boolean $allowAnonymous specifies if anonymous binds are allowed |
| 76 |
* @throws LAMException unable to connect |
| 77 |
*/ |
| 78 |
public function connect($user, $passwd, $allowAnonymous = false): void { |
| 79 |
// close any prior connection |
| 80 |
@$this->close(); |
| 81 |
// do not allow anonymous bind |
| 82 |
if (!$allowAnonymous && (($user === null) || ($user === '') || ($passwd === null) || ($passwd === ''))) { |
| 83 |
throw new LAMException(_("Cannot connect to specified LDAP server. Please try again.")); |
| 84 |
} |
| 85 |
// save password und username encrypted |
| 86 |
$this->encrypt_login($user, $passwd); |
| 87 |
$startTLS = $this->conf->getUseTLS(); |
| 88 |
$startTLS = ($startTLS === 'yes'); |
| 89 |
$this->server = connectToLDAP($this->conf->get_ServerURL(), $startTLS); |
| 90 |
if ($this->server != null) { |
| 91 |
// referral following |
| 92 |
$followReferrals = ($this->conf->getFollowReferrals() === 'true') ? 1 : 0; |
| 93 |
ldap_set_option($this->server, LDAP_OPT_REFERRALS, $followReferrals); |
| 94 |
$bind = @ldap_bind($this->server, $user, $passwd); |
| 95 |
if ($bind) { |
| 96 |
$this->is_connected = true; |
| 97 |
return; |
| 98 |
} |
| 99 |
// return error number |
| 100 |
$errorNumber = ldap_errno($this->server); |
| 101 |
$clientSource = empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']; |
| 102 |
if ($errorNumber === 81) { |
| 103 |
// connection failed |
| 104 |
logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (LDAP error: ' . getDefaultLDAPErrorString($this->server) . ').'); |
| 105 |
throw new LAMException(_("Cannot connect to specified LDAP server. Please try again."), '', null, $errorNumber); |
| 106 |
} |
| 107 |
elseif ($errorNumber === 49) { |
| 108 |
// username/password invalid. Return to login page. |
| 109 |
logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (wrong password). ' . getDefaultLDAPErrorString($this->server)); |
| 110 |
throw new LAMException(_("Wrong password/user name combination. Please try again."), getDefaultLDAPErrorString($this->server), null, $errorNumber); |
| 111 |
} |
| 112 |
else { |
| 113 |
// other errors |
| 114 |
logNewMessage(LOG_ERR, 'User ' . $user . ' (' . $clientSource . ') failed to log in (LDAP error: ' . getDefaultLDAPErrorString($this->server) . ').'); |
| 115 |
throw new LAMException(_("Cannot connect to specified LDAP server. Please try again."), "($errorNumber) " . getDefaultLDAPErrorString($this->server), null, $errorNumber); |
| 116 |
} |
| 117 |
} |
| 118 |
throw new LAMException(_("Cannot connect to specified LDAP server. Please try again.")); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Checks if the given password is the new password to use for the current user. |
| 123 |
* |
| 124 |
* @param string $newPassword new password |
| 125 |
*/ |
| 126 |
public function tryAndApplyNewPassword(string $newPassword): void { |
| 127 |
logNewMessage(LOG_DEBUG, 'Check if new LDAP password needs to be used: ' . $this->getUserName() . ' - ' . $newPassword[0] . '...'); |
| 128 |
$bind = @ldap_bind($this->server, $this->getUserName(), $newPassword); |
| 129 |
if ($bind) { |
| 130 |
logNewMessage(LOG_DEBUG, 'Password updated'); |
| 131 |
$this->encrypt_login($this->getUserName(), $newPassword); |
| 132 |
} |
| 133 |
else { |
| 134 |
logNewMessage(LOG_DEBUG, 'Keeping old password as bind failed. ' . getDefaultLDAPErrorString($this->server)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** Closes connection to server */ |
| 139 |
public function close(): void { |
| 140 |
if ($this->server != null) { |
| 141 |
$this->is_connected = false; |
| 142 |
@ldap_close($this->server); |
| 143 |
$this->server = null; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the LDAP connection handle |
| 149 |
* |
| 150 |
* @return Connection|null connection handle |
| 151 |
*/ |
| 152 |
public function server() { |
| 153 |
if (!$this->is_connected) { |
| 154 |
try { |
| 155 |
$this->connect($this->getUserName(), $this->getPassword()); |
| 156 |
$this->is_connected = true; |
| 157 |
} |
| 158 |
catch (LAMException $e) { |
| 159 |
logNewMessage(LOG_ERR, $e->getTitle() . ' ' . $e->getMessage()); |
| 160 |
} |
| 161 |
} |
| 162 |
return $this->server; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Closes the connection to LDAP server before serialization. |
| 167 |
* |
| 168 |
* @return array<string, string|LAMConfig|bool> data |
| 169 |
*/ |
| 170 |
public function __serialize(): array { |
| 171 |
$this->close(); |
| 172 |
// define which attributes to save |
| 173 |
return [ |
| 174 |
'conf' => $this->conf, |
| 175 |
'username' => $this->username, |
| 176 |
'password' => $this->password, |
| 177 |
'isActiveDirectory' => $this->isActiveDirectory |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Reconnects to the LDAP server when deserialized. |
| 183 |
* |
| 184 |
* @param array<string, string|LAMConfig|bool> $data data |
| 185 |
*/ |
| 186 |
public function __unserialize(array $data): void { |
| 187 |
$this->is_connected = false; |
| 188 |
$this->conf = $data['conf']; |
| 189 |
$this->username = $data['username']; |
| 190 |
$this->password = $data['password']; |
| 191 |
$this->isActiveDirectory = $data['isActiveDirectory']; |
| 192 |
// delete PDF files and images which are older than 15 min |
| 193 |
$tmpDir = __DIR__ . '/../tmp/'; |
| 194 |
$time = time(); |
| 195 |
$dir = @opendir($tmpDir); |
| 196 |
$file = @readdir($dir); |
| 197 |
while ($file) { |
| 198 |
$path = $tmpDir . $file; |
| 199 |
if ((!str_starts_with($file, '.')) |
| 200 |
&& !is_dir($path) |
| 201 |
&& ($time - filemtime($path) > 900)) { |
| 202 |
@unlink($path); |
| 203 |
} |
| 204 |
$file = @readdir($dir); |
| 205 |
} |
| 206 |
@closedir($dir); |
| 207 |
// clean internal files that are older than 24 hours |
| 208 |
$tmpDir = __DIR__ . '/../tmp/internal/'; |
| 209 |
$time = time(); |
| 210 |
$dir = @opendir($tmpDir); |
| 211 |
$file = @readdir($dir); |
| 212 |
while ($file) { |
| 213 |
if (str_ends_with($file, '.tmp')) { |
| 214 |
$path = $tmpDir . $file; |
| 215 |
if ($time - filemtime($path) > (3600 * 24)) { |
| 216 |
@unlink($path); |
| 217 |
} |
| 218 |
} |
| 219 |
$file = @readdir($dir); |
| 220 |
} |
| 221 |
@closedir($dir); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Encrypts username and password |
| 226 |
* |
| 227 |
* @param string $username LDAP user name |
| 228 |
* @param string $password LDAP password |
| 229 |
*/ |
| 230 |
public function encrypt_login($username, $password): void { |
| 231 |
// encrypt username and password |
| 232 |
$this->username = base64_encode(lamEncrypt($username)); |
| 233 |
$this->password = base64_encode(lamEncrypt($password)); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns the LDAP username. |
| 238 |
* |
| 239 |
* @return string|null user name |
| 240 |
*/ |
| 241 |
public function getUserName(): ?string { |
| 242 |
if ($this->username === null) { |
| 243 |
return null; |
| 244 |
} |
| 245 |
return lamDecrypt(base64_decode($this->username)); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Returns the LDAP password. |
| 250 |
* |
| 251 |
* @return string|null password |
| 252 |
*/ |
| 253 |
public function getPassword(): ?string { |
| 254 |
if ($this->password === null) { |
| 255 |
return null; |
| 256 |
} |
| 257 |
return lamDecrypt(base64_decode($this->password)); |
| 258 |
} |
| 259 |
|
| 260 |
/** Closes connection to LDAP server and deletes encrypted username/password */ |
| 261 |
public function destroy(): void { |
| 262 |
$this->close(); |
| 263 |
$this->username = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 264 |
$this->password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Returns if the LDAP server is an Active Directory. |
| 269 |
* |
| 270 |
* @return bool is Active Directory |
| 271 |
*/ |
| 272 |
public function isActiveDirectory(): bool { |
| 273 |
if ($this->isActiveDirectory !== null) { |
| 274 |
return $this->isActiveDirectory; |
| 275 |
} |
| 276 |
$rootDse = ldapGetDN('', ['*']); |
| 277 |
logNewMessage(LOG_DEBUG, 'Root DSE: ' . print_r($rootDse, true)); |
| 278 |
$this->isActiveDirectory = isset($rootDse['domaincontrollerfunctionality']) || isset($rootDse['forestfunctionality']); |
| 279 |
return $this->isActiveDirectory; |
| 280 |
} |
| 281 |
|
| 282 |
} |