Improve LDAP: attribute checks, handling, user filtering (#493)

This commit is contained in:
ahaenggli
2024-10-28 20:43:25 +01:00
committed by GitHub
parent f051fbc333
commit b5c778fd7f

View File

@@ -14,6 +14,8 @@ use Filegator\Services\Auth\User;
use Filegator\Services\Auth\UsersCollection;
use Filegator\Services\Service;
use Filegator\Services\Session\SessionStorageInterface as Session;
use Filegator\Services\Logger\LoggerInterface;
use Monolog\Logger;
/**
* @codeCoverageIgnore
@@ -32,10 +34,12 @@ class LDAP implements Service, AuthInterface
protected $ldap_filter;
protected $ldap_attributes;
protected $ldap_userFieldMapping;
protected $logger;
public function __construct(Session $session)
public function __construct(Session $session, LoggerInterface $logger)
{
$this->session = $session;
$this->logger = $logger;
}
public function init(array $config = [])
@@ -43,10 +47,13 @@ class LDAP implements Service, AuthInterface
if (!isset($config['ldap_server']) || empty($config['ldap_server']))
throw new \Exception('config ldap_server missing');
if (!extension_loaded('ldap')) throw new \Exception('ldap extension missing');
if (!extension_loaded('ldap'))
throw new \Exception('ldap extension missing');
if ($connect = ldap_connect($config['ldap_server'])) {
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
@ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
@ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
$this->private_repos = $config['private_repos'];
$this->ldap_server = $config['ldap_server'];
$this->ldap_bindDN = $config['ldap_bindDN'];
@@ -71,14 +78,18 @@ class LDAP implements Service, AuthInterface
public function authenticate($username, $password): bool
{
// prevent anonymous binding
if(!isset($password) || empty($password)) return false;
if(!isset($username) || empty($username)) return false;
if (!isset($password) || empty($password))
return false;
if (!isset($username) || empty($username))
return false;
// remove (optional) domains from the username
if (!empty($this->ldap_userFieldMapping['username_RemoveDomains']) && is_array($this->ldap_userFieldMapping['username_RemoveDomains'])) {
$username = str_replace($this->ldap_userFieldMapping['username_RemoveDomains'], '', $username);
}
$all_users = $this->getUsers($username);
// add the domain to the username
if (!empty($this->ldap_userFieldMapping['username_AddDomain'])) {
if (strpos($username, $this->ldap_userFieldMapping['username_AddDomain']) === false) {
@@ -86,10 +97,8 @@ class LDAP implements Service, AuthInterface
}
}
$all_users = $this->getUsers();
foreach ($all_users as &$u) {
if ($u['username'] == $username && $this->verifyPassword($u['userDN'], $password)) {
if (strtolower($u['username']) == strtolower($username) && $this->verifyPassword($u['userDN'], $password)) {
$user = $this->mapToUserObject($u);
$this->store($user);
return true;
@@ -126,8 +135,8 @@ class LDAP implements Service, AuthInterface
public function find($username): ?User
{
foreach ($this->getUsers() as $user) {
if ($user['username'] == $username) {
foreach ($this->getUsers($username) as $user) {
if (strtolower($user['username']) == strtolower($username)) {
return $this->mapToUserObject($user);
}
}
@@ -174,24 +183,49 @@ class LDAP implements Service, AuthInterface
return $new;
}
protected function getUsers(): array
protected function getUsers(string $username = null): array
{
$ldapConn = @ldap_connect($this->ldap_server);
if (!$ldapConn) throw new \Exception('Cannot Connect to LDAP server');
if (!$ldapConn)
throw new \Exception('Cannot Connect to LDAP server');
@ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);
@ldap_set_option($ldapConn, LDAP_OPT_REFERRALS, 0);
$ldapBind = @ldap_bind($ldapConn, $this->ldap_bindDN, $this->ldap_bindPass);
if (!$ldapBind) throw new \Exception('Cannot Bind to LDAP server: Wrong credentials?');
if (!$ldapBind)
throw new \Exception('Cannot Bind to LDAP server: Wrong credentials?');
// search the LDAP server for users
$ldapSearch = @ldap_search($ldapConn, $this->ldap_baseDN, $this->ldap_filter, $this->ldap_attributes);
$filter = $this->ldap_filter;
if (!empty($username))
$filter = '(&' . $filter . '(' . $this->ldap_userFieldMapping['username'] . '=' . $username . '))';
$ldapSearch = @ldap_search($ldapConn, $this->ldap_baseDN, $filter, $this->ldap_attributes);
if (!$ldapSearch) {
$this->logger->log($filter);
throw new \Exception('Cannot search LDAP server: Wrong filter?');
}
$ldapResults = @ldap_get_entries($ldapConn, $ldapSearch);
@ldap_close($ldapConn);
$users = [];
for ($item = 0; $item < $ldapResults['count']; $item++)
{
for ($item = 0; $item < $ldapResults['count']; $item++) {
$missingAttributes = [];
// Check if all required attributes are present
foreach (['username', 'name', 'userDN'] as $attribute) {
if (!isset($ldapResults[$item][$this->ldap_userFieldMapping[$attribute]])) {
$missingAttributes[] = '`' . $this->ldap_userFieldMapping[$attribute] . '` as `' . $attribute . '`';
}
}
// if any required attribute is missing, log an info message
if (!empty($missingAttributes)) {
$this->logger->log('Missing LDAP attribues: ' . implode(', ', $missingAttributes) . '. Please check the spelling (including upper or lower case).', Logger::WARNING);
} else {
$user = [];
$user['username'] = $ldapResults[$item][$this->ldap_userFieldMapping['username']][0];
$user['name'] = $ldapResults[$item][$this->ldap_userFieldMapping['name']][0];
@@ -205,9 +239,9 @@ class LDAP implements Service, AuthInterface
$user['username'] .= $this->ldap_userFieldMapping['username_AddDomain'];
}
if(is_array($this->ldap_userFieldMapping['admin_usernames']))
{
if(in_array($user['username'], $this->ldap_userFieldMapping['admin_usernames'])) $user['role'] = 'admin';
if (is_array($this->ldap_userFieldMapping['admin_usernames'])) {
if (in_array($user['username'], $this->ldap_userFieldMapping['admin_usernames']))
$user['role'] = 'admin';
}
// private repositories for each user?
@@ -221,7 +255,9 @@ class LDAP implements Service, AuthInterface
$user['permissions'] = 'read|write|upload|download|batchdownload|zip|chmod';
}
if(is_array($user) && !empty($user)) $users[] = $user;
if (is_array($user) && !empty($user))
$users[] = $user;
}
}
// print_r($users); // uncomment this line to see all available ldap-login-users
return is_array($users) ? $users : [];
@@ -229,13 +265,16 @@ class LDAP implements Service, AuthInterface
private function verifyPassword($auth_user, $password)
{
if(!isset($this->ldap_server) || empty($this->ldap_server)) return false;
if(!extension_loaded('ldap')) return false;
if($connect=ldap_connect($this->ldap_server))
{
if (!isset($this->ldap_server) || empty($this->ldap_server))
return false;
if (!extension_loaded('ldap'))
return false;
$connect = @ldap_connect($this->ldap_server);
if ($connect) {
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
if($bind=ldap_bind($connect, $auth_user, $password)){
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
$bind = @ldap_bind($connect, $auth_user, $password);
if ($bind) {
@ldap_close($connect);
return true;
} else {