| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\PLUGINS\EXTRA_INVALID_CREDENTIALS; |
| 4 |
|
| 5 |
/* |
| 6 |
|
| 7 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 8 |
Copyright (C) 2020 - 2026 Roland Gruber |
| 9 |
|
| 10 |
This program is free software; you can redistribute it and/or modify |
| 11 |
it under the terms of the GNU General Public License as published by |
| 12 |
the Free Software Foundation; either version 2 of the License, or |
| 13 |
(at your option) any later version. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
|
| 24 |
*/ |
| 25 |
|
| 26 |
use LDAP\Connection; |
| 27 |
|
| 28 |
/** |
| 29 |
* Extra messages for invalid credentials. |
| 30 |
* |
| 31 |
* @author Roland Gruber |
| 32 |
*/ |
| 33 |
|
| 34 |
/** |
| 35 |
* Provides additional messages if login failed. |
| 36 |
* |
| 37 |
* @package LAM\PLUGINS\EXTRA_INVALID_CREDENTIALS |
| 38 |
*/ |
| 39 |
class ExtraInvalidCredentials { |
| 40 |
|
| 41 |
/** |
| 42 |
* Tries to get additional information why invalid credentials was returned. E.g. account is locked. |
| 43 |
* |
| 44 |
* @param Connection $ldap LDAP object to connect for getting extra data |
| 45 |
* @param string $userDn failed DN |
| 46 |
* @return null|string extra message |
| 47 |
*/ |
| 48 |
public function getExtraMessage($ldap, string $userDn) { |
| 49 |
$this->includeFiles(); |
| 50 |
$providers = $this->findProviders(); |
| 51 |
$attributes = $this->getAttributeNames($providers); |
| 52 |
$userData = $this->getLdapData($userDn, $attributes, $ldap); |
| 53 |
return $this->getMessageFromProviders($providers, $userData, $ldap); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Includes all plugin files. |
| 58 |
*/ |
| 59 |
protected function includeFiles(): void { |
| 60 |
$pluginDir = dir(__DIR__); |
| 61 |
while ($entry = $pluginDir->read()) { |
| 62 |
if ((str_starts_with($entry, '.')) || ($entry === basename(__FILE__))) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
include_once(__DIR__ . '/' . $entry); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns a list of ExtraInvalidCredentialsProvider objects. |
| 71 |
* |
| 72 |
* @return ExtraInvalidCredentialsProvider[] providers |
| 73 |
*/ |
| 74 |
protected function findProviders() { |
| 75 |
$providers = []; |
| 76 |
foreach (get_declared_classes() as $declaredClass) { |
| 77 |
if (in_array('LAM\PLUGINS\EXTRA_INVALID_CREDENTIALS\ExtraInvalidCredentialsProvider', class_implements($declaredClass))) { |
| 78 |
$providers[] = new $declaredClass(); |
| 79 |
} |
| 80 |
} |
| 81 |
return $providers; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the attribute names for the LDAP search. |
| 86 |
* |
| 87 |
* @param ExtraInvalidCredentialsProvider[] $providers |
| 88 |
* @return string[] attribute names |
| 89 |
*/ |
| 90 |
protected function getAttributeNames(array $providers): array { |
| 91 |
$attributeNames = []; |
| 92 |
foreach ($providers as $provider) { |
| 93 |
$attributeNames = array_merge($attributeNames, $provider->getAttributeNamesToRead()); |
| 94 |
} |
| 95 |
$attributeNames = array_unique($attributeNames); |
| 96 |
logNewMessage(LOG_DEBUG, 'Extra invalid credentials - attribute names: ' . implode(', ', $attributeNames)); |
| 97 |
return $attributeNames; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Reads the LDAP data. |
| 102 |
* |
| 103 |
* @param string $userDn user DN |
| 104 |
* @param string[] $attributes attribute names |
| 105 |
* @param Connection $ldap LDAP handle |
| 106 |
* @return array<string, string[]|string>|null attribute values |
| 107 |
*/ |
| 108 |
protected function getLdapData(string $userDn, array $attributes, $ldap) { |
| 109 |
$data = ldapGetDN($userDn, $attributes, $ldap); |
| 110 |
logNewMessage(LOG_DEBUG, 'Extra invalid credentials - LDAP data: ' . print_r($data, true)); |
| 111 |
if ($data === null) { |
| 112 |
$data = []; |
| 113 |
} |
| 114 |
return $data; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns the message from the providers. |
| 119 |
* |
| 120 |
* @param ExtraInvalidCredentialsProvider[] $providers providers |
| 121 |
* @param array<string, string[]|string> $userData LDAP data |
| 122 |
* @param Connection $ldap LDAP handle |
| 123 |
* @return null|string extra message |
| 124 |
*/ |
| 125 |
protected function getMessageFromProviders(array $providers, array $userData, $ldap) { |
| 126 |
foreach ($providers as $provider) { |
| 127 |
$message = $provider->getExtraMessage($userData, $ldap); |
| 128 |
if ($message !== null) { |
| 129 |
return $message; |
| 130 |
} |
| 131 |
} |
| 132 |
return null; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Interface for provides of extra messages. |
| 139 |
* |
| 140 |
* @package LAM\PLUGINS\EXTRA_INVALID_CREDENTIALS |
| 141 |
*/ |
| 142 |
interface ExtraInvalidCredentialsProvider { |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns the list of attribute names to read from LDAP. |
| 146 |
* |
| 147 |
* @return string[] attribute names |
| 148 |
*/ |
| 149 |
public function getAttributeNamesToRead(): array; |
| 150 |
|
| 151 |
/** |
| 152 |
* Returns an extra message if any. |
| 153 |
* |
| 154 |
* @param array<string, string[]|string> $attributes LDAP attributes |
| 155 |
* @param Connection $ldap LDAP handle |
| 156 |
* @return null|string message |
| 157 |
*/ |
| 158 |
public function getExtraMessage(array $attributes, $ldap); |
| 159 |
|
| 160 |
} |
| 161 |
|