2013-06-10 16:03:51 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-10 16:03:51 +02:00
|
|
|
|
|
|
|
namespace Icinga\Authentication\Backend;
|
|
|
|
|
2014-03-03 17:21:17 +01:00
|
|
|
use Icinga\User;
|
|
|
|
use Icinga\Authentication\UserBackend;
|
2015-02-06 16:32:26 +01:00
|
|
|
use Icinga\Protocol\Ldap\Query;
|
2014-03-03 17:21:17 +01:00
|
|
|
use Icinga\Protocol\Ldap\Connection;
|
2014-06-02 15:52:58 +02:00
|
|
|
use Icinga\Exception\AuthenticationException;
|
2014-06-25 12:38:31 +02:00
|
|
|
use Icinga\Protocol\Ldap\Exception as LdapException;
|
2015-03-11 09:52:14 +01:00
|
|
|
use Icinga\Protocol\Ldap\Expression;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2014-03-03 17:21:17 +01:00
|
|
|
class LdapUserBackend extends UserBackend
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2014-03-03 17:21:17 +01:00
|
|
|
* Connection to the LDAP server
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
|
|
|
* @var Connection
|
2015-02-06 16:32:26 +01:00
|
|
|
*/
|
2014-03-03 17:21:17 +01:00
|
|
|
protected $conn;
|
2013-07-12 15:37:36 +02:00
|
|
|
|
2014-10-09 10:10:09 +02:00
|
|
|
protected $baseDn;
|
|
|
|
|
2014-03-03 17:21:17 +01:00
|
|
|
protected $userClass;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2014-03-03 17:21:17 +01:00
|
|
|
protected $userNameAttribute;
|
2013-08-28 10:16:18 +02:00
|
|
|
|
2015-03-11 09:52:14 +01:00
|
|
|
protected $customFilter;
|
|
|
|
|
2014-10-01 15:58:53 +02:00
|
|
|
protected $groupOptions;
|
|
|
|
|
2015-03-13 11:17:35 +01:00
|
|
|
/**
|
|
|
|
* Normed attribute names based on known LDAP environments
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $normedAttributes = array(
|
|
|
|
'uid' => 'uid',
|
|
|
|
'user' => 'user',
|
|
|
|
'inetorgperson' => 'inetOrgPerson',
|
|
|
|
'samaccountname' => 'sAMAccountName'
|
|
|
|
);
|
|
|
|
|
2015-03-11 09:52:14 +01:00
|
|
|
public function __construct(
|
|
|
|
Connection $conn,
|
|
|
|
$userClass,
|
|
|
|
$userNameAttribute,
|
|
|
|
$baseDn,
|
|
|
|
$cutomFilter,
|
|
|
|
$groupOptions = null
|
|
|
|
) {
|
2014-03-03 17:21:17 +01:00
|
|
|
$this->conn = $conn;
|
2015-03-11 09:52:14 +01:00
|
|
|
$this->baseDn = trim($baseDn) ?: $conn->getDN();
|
2015-03-13 11:17:35 +01:00
|
|
|
$this->userClass = $this->getNormedAttribute($userClass);
|
|
|
|
$this->userNameAttribute = $this->getNormedAttribute($userNameAttribute);
|
2015-03-11 09:52:14 +01:00
|
|
|
$this->customFilter = trim($cutomFilter);
|
2014-10-01 15:58:53 +02:00
|
|
|
$this->groupOptions = $groupOptions;
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2015-03-13 11:17:35 +01:00
|
|
|
/**
|
|
|
|
* Return the given attribute name normed to known LDAP enviroments, if possible
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getNormedAttribute($name)
|
|
|
|
{
|
|
|
|
$loweredName = strtolower($name);
|
|
|
|
if (array_key_exists($loweredName, $this->normedAttributes)) {
|
|
|
|
return $this->normedAttributes[$loweredName];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2014-10-14 14:37:21 +02:00
|
|
|
/**
|
2015-02-06 16:32:26 +01:00
|
|
|
* Create a query to select all usernames
|
|
|
|
*
|
|
|
|
* @return Query
|
2014-10-14 14:37:21 +02:00
|
|
|
*/
|
|
|
|
protected function selectUsers()
|
|
|
|
{
|
2015-03-11 09:52:14 +01:00
|
|
|
$query = $this->conn->select()->setBase($this->baseDn)->from(
|
2014-10-14 14:37:21 +02:00
|
|
|
$this->userClass,
|
|
|
|
array(
|
|
|
|
$this->userNameAttribute
|
|
|
|
)
|
|
|
|
);
|
2015-03-11 09:52:14 +01:00
|
|
|
|
|
|
|
if ($this->customFilter) {
|
|
|
|
$query->addFilter(new Expression($this->customFilter));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
2014-10-14 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2015-02-06 16:32:26 +01:00
|
|
|
* Create a query filtered by the given username
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2015-02-06 16:32:26 +01:00
|
|
|
* @param string $username
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2015-02-06 16:32:26 +01:00
|
|
|
* @return Query
|
|
|
|
*/
|
2014-10-14 14:37:21 +02:00
|
|
|
protected function selectUser($username)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2015-02-06 16:32:26 +01:00
|
|
|
return $this->selectUsers()->setUsePagedResults(false)->where(
|
|
|
|
$this->userNameAttribute,
|
|
|
|
str_replace('*', '', $username)
|
|
|
|
);
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2014-06-11 14:22:52 +02:00
|
|
|
/**
|
|
|
|
* Probe the backend to test if authentication is possible
|
|
|
|
*
|
|
|
|
* Try to bind to the backend and query all available users to check if:
|
|
|
|
* <ul>
|
2015-02-06 16:32:26 +01:00
|
|
|
* <li>Connection credentials are correct and the bind is possible</li>
|
2014-06-11 14:22:52 +02:00
|
|
|
* <li>At least one user exists</li>
|
|
|
|
* <li>The specified userClass has the property specified by userNameAttribute</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
2015-02-06 16:32:26 +01:00
|
|
|
* @throws AuthenticationException When authentication is not possible
|
2014-06-11 14:22:52 +02:00
|
|
|
*/
|
2014-06-11 15:04:19 +02:00
|
|
|
public function assertAuthenticationPossible()
|
2014-06-11 14:22:52 +02:00
|
|
|
{
|
2014-11-04 12:35:41 +01:00
|
|
|
try {
|
2015-02-06 16:32:26 +01:00
|
|
|
$result = $this->selectUsers()->fetchRow();
|
2014-11-04 12:35:41 +01:00
|
|
|
} catch (LdapException $e) {
|
2014-11-06 16:32:43 +01:00
|
|
|
throw new AuthenticationException('Connection not possible.', $e);
|
2014-11-04 12:35:41 +01:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:32:26 +01:00
|
|
|
if ($result === null) {
|
2014-06-11 14:22:52 +02:00
|
|
|
throw new AuthenticationException(
|
2015-03-11 09:52:14 +01:00
|
|
|
'No objects with objectClass="%s" in DN="%s" found. (Filter: %s)',
|
2014-08-22 10:59:52 +02:00
|
|
|
$this->userClass,
|
2015-03-11 09:52:14 +01:00
|
|
|
$this->baseDn,
|
|
|
|
$this->customFilter ?: 'None'
|
2014-07-03 16:20:45 +02:00
|
|
|
);
|
2014-06-11 14:22:52 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 16:20:45 +02:00
|
|
|
if (! isset($result->{$this->userNameAttribute})) {
|
2014-06-11 14:22:52 +02:00
|
|
|
throw new AuthenticationException(
|
2014-08-22 10:59:52 +02:00
|
|
|
'UserNameAttribute "%s" not existing in objectClass="%s"',
|
|
|
|
$this->userNameAttribute,
|
|
|
|
$this->userClass
|
2014-07-03 16:20:45 +02:00
|
|
|
);
|
2014-06-11 14:22:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 15:58:53 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the user groups
|
|
|
|
*
|
2014-10-06 13:35:17 +02:00
|
|
|
* @TODO: Subject to change, see #7343
|
|
|
|
*
|
2014-10-01 15:58:53 +02:00
|
|
|
* @param string $dn
|
|
|
|
*
|
2014-10-09 10:14:42 +02:00
|
|
|
* @return array
|
2014-10-01 15:58:53 +02:00
|
|
|
*/
|
|
|
|
public function getGroups($dn)
|
|
|
|
{
|
2014-10-06 13:35:17 +02:00
|
|
|
if (empty($this->groupOptions) || ! isset($this->groupOptions['group_base_dn'])) {
|
2014-10-09 10:14:42 +02:00
|
|
|
return array();
|
2014-10-01 15:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$q = $this->conn->select()
|
|
|
|
->setBase($this->groupOptions['group_base_dn'])
|
|
|
|
->from(
|
|
|
|
$this->groupOptions['group_class'],
|
|
|
|
array($this->groupOptions['group_attribute'])
|
|
|
|
)
|
|
|
|
->where(
|
|
|
|
$this->groupOptions['group_member_attribute'],
|
|
|
|
$dn
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->conn->fetchAll($q);
|
|
|
|
|
|
|
|
$groups = array();
|
|
|
|
|
|
|
|
foreach ($result as $group) {
|
|
|
|
$groups[] = $group->{$this->groupOptions['group_attribute']};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2015-02-06 16:32:26 +01:00
|
|
|
* Return whether the given user exists
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2015-02-06 16:32:26 +01:00
|
|
|
* @param User $user
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2014-03-03 17:21:17 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasUser(User $user)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2014-03-03 17:21:17 +01:00
|
|
|
$username = $user->getUsername();
|
2015-02-06 16:32:26 +01:00
|
|
|
$entry = $this->selectUser($username)->fetchOne();
|
2015-02-03 10:15:54 +01:00
|
|
|
|
|
|
|
if (is_array($entry)) {
|
|
|
|
return in_array(strtolower($username), array_map('strtolower', $entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtolower($entry) === strtolower($username);
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2015-02-06 16:32:26 +01:00
|
|
|
* Return whether the given user credentials are valid
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2014-03-03 17:21:17 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $password
|
2015-02-06 16:32:26 +01:00
|
|
|
* @param boolean $healthCheck Assert that authentication is possible at all
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2015-02-06 16:32:26 +01:00
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws AuthenticationException In case an error occured or the health check has failed
|
2013-08-28 10:16:18 +02:00
|
|
|
*/
|
2015-02-09 15:29:52 +01:00
|
|
|
public function authenticate(User $user, $password, $healthCheck = false)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2014-06-11 14:22:52 +02:00
|
|
|
if ($healthCheck) {
|
|
|
|
try {
|
|
|
|
$this->assertAuthenticationPossible();
|
|
|
|
} catch (AuthenticationException $e) {
|
|
|
|
throw new AuthenticationException(
|
2014-11-06 16:32:43 +01:00
|
|
|
'Authentication against backend "%s" not possible.',
|
2014-08-22 10:59:52 +02:00
|
|
|
$this->getName(),
|
2014-06-11 14:22:52 +02:00
|
|
|
$e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-02-06 16:32:26 +01:00
|
|
|
|
2014-06-23 13:57:41 +02:00
|
|
|
if (! $this->hasUser($user)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-06 16:32:26 +01:00
|
|
|
|
2014-03-28 14:45:03 +01:00
|
|
|
try {
|
2014-10-14 14:37:21 +02:00
|
|
|
$userDn = $this->conn->fetchDN($this->selectUser($user->getUsername()));
|
2014-10-01 15:58:53 +02:00
|
|
|
$authenticated = $this->conn->testCredentials(
|
|
|
|
$userDn,
|
2014-06-23 13:57:41 +02:00
|
|
|
$password
|
|
|
|
);
|
2015-02-06 16:32:26 +01:00
|
|
|
|
2014-10-01 15:58:53 +02:00
|
|
|
if ($authenticated) {
|
2014-10-23 03:46:49 +02:00
|
|
|
$groups = $this->getGroups($userDn);
|
|
|
|
if ($groups !== null) {
|
|
|
|
$user->setGroups($groups);
|
|
|
|
}
|
2014-10-01 15:58:53 +02:00
|
|
|
}
|
2015-02-06 16:32:26 +01:00
|
|
|
|
2014-10-01 15:58:53 +02:00
|
|
|
return $authenticated;
|
2014-06-25 12:38:31 +02:00
|
|
|
} catch (LdapException $e) {
|
2014-06-02 15:52:58 +02:00
|
|
|
throw new AuthenticationException(
|
2014-08-22 10:59:52 +02:00
|
|
|
'Failed to authenticate user "%s" against backend "%s". An exception was thrown:',
|
|
|
|
$user->getUsername(),
|
|
|
|
$this->getName(),
|
2014-06-02 15:52:58 +02:00
|
|
|
$e
|
2014-03-28 14:45:03 +01:00
|
|
|
);
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-26 16:56:23 +02:00
|
|
|
|
2013-08-30 15:31:21 +02:00
|
|
|
/**
|
2014-03-03 17:21:17 +01:00
|
|
|
* Get the number of users available
|
2013-08-30 15:31:21 +02:00
|
|
|
*
|
2014-03-03 17:21:17 +01:00
|
|
|
* @return int
|
2013-08-30 15:31:21 +02:00
|
|
|
*/
|
2014-03-03 17:21:17 +01:00
|
|
|
public function count()
|
2013-08-27 14:37:22 +02:00
|
|
|
{
|
2015-01-29 15:53:15 +01:00
|
|
|
return $this->selectUsers()->count();
|
2013-08-26 16:56:23 +02:00
|
|
|
}
|
2014-09-29 11:21:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the names of all available users
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listUsers()
|
|
|
|
{
|
|
|
|
$users = array();
|
2014-10-14 14:37:21 +02:00
|
|
|
foreach ($this->selectUsers()->fetchAll() as $row) {
|
2015-02-03 10:15:54 +01:00
|
|
|
if (is_array($row->{$this->userNameAttribute})) {
|
|
|
|
foreach ($row->{$this->userNameAttribute} as $col) {
|
|
|
|
$users[] = $col;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$users[] = $row->{$this->userNameAttribute};
|
|
|
|
}
|
2014-09-29 11:21:40 +02:00
|
|
|
}
|
|
|
|
return $users;
|
|
|
|
}
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|