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
|
|
|
|
2015-04-21 12:51:31 +02:00
|
|
|
namespace Icinga\Authentication\User;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2015-06-01 12:23:16 +02:00
|
|
|
use DateTime;
|
|
|
|
use Icinga\Application\Logger;
|
2015-05-04 12:18:25 +02:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-06-02 15:52:58 +02:00
|
|
|
use Icinga\Exception\AuthenticationException;
|
2015-05-04 12:18:25 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
use Icinga\Repository\Repository;
|
|
|
|
use Icinga\Repository\RepositoryQuery;
|
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;
|
2015-05-04 12:18:25 +02:00
|
|
|
use Icinga\User;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
class LdapUserBackend extends Repository implements UserBackendInterface
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2015-05-04 12:18:25 +02:00
|
|
|
* The base DN to use for a query
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* @var string
|
2015-02-06 16:32:26 +01:00
|
|
|
*/
|
2014-10-09 10:10:09 +02:00
|
|
|
protected $baseDn;
|
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
/**
|
|
|
|
* The objectClass where look for users
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-03 17:21:17 +01:00
|
|
|
protected $userClass;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
/**
|
|
|
|
* The attribute name where to find a user's name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-03 17:21:17 +01:00
|
|
|
protected $userNameAttribute;
|
2013-08-28 10:16:18 +02:00
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
/**
|
|
|
|
* The custom LDAP filter to apply on search queries
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $filter;
|
|
|
|
|
2015-05-06 10:27:26 +02:00
|
|
|
/**
|
|
|
|
* The columns which are not permitted to be queried
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $filterColumns = array('user');
|
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
/**
|
|
|
|
* The default sort rules to be applied on a query
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $sortRules = array(
|
|
|
|
'user_name' => array(
|
2015-05-04 15:56:13 +02:00
|
|
|
'columns' => array(
|
2015-05-21 13:53:27 +02:00
|
|
|
'is_active desc',
|
|
|
|
'user_name'
|
2015-05-04 15:56:13 +02:00
|
|
|
)
|
2015-05-04 12:18:25 +02:00
|
|
|
)
|
|
|
|
);
|
2015-03-11 09:52:14 +01:00
|
|
|
|
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-05-04 12:18:25 +02:00
|
|
|
/**
|
|
|
|
* Set the base DN to use for a query
|
|
|
|
*
|
|
|
|
* @param string $baseDn
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setBaseDn($baseDn)
|
|
|
|
{
|
|
|
|
if (($baseDn = trim($baseDn))) {
|
|
|
|
$this->baseDn = $baseDn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the base DN to use for a query
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBaseDn()
|
|
|
|
{
|
|
|
|
return $this->baseDn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the objectClass where to look for users
|
|
|
|
*
|
|
|
|
* Sets also the base table name for the underlying repository.
|
|
|
|
*
|
|
|
|
* @param string $userClass
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUserClass($userClass)
|
|
|
|
{
|
|
|
|
$this->baseTable = $this->userClass = $this->getNormedAttribute($userClass);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the objectClass where to look for users
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserClass()
|
|
|
|
{
|
|
|
|
return $this->userClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the attribute name where to find a user's name
|
|
|
|
*
|
|
|
|
* @param string $userNameAttribute
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setUserNameAttribute($userNameAttribute)
|
|
|
|
{
|
2015-03-13 11:17:35 +01:00
|
|
|
$this->userNameAttribute = $this->getNormedAttribute($userNameAttribute);
|
2015-05-04 12:18:25 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the attribute name where to find a user's name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserNameAttribute()
|
|
|
|
{
|
|
|
|
return $this->userNameAttribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the custom LDAP filter to apply on search queries
|
|
|
|
*
|
|
|
|
* @param string $filter
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setFilter($filter)
|
|
|
|
{
|
|
|
|
if (($filter = trim($filter))) {
|
|
|
|
$this->filter = $filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the custom LDAP filter to apply on search queries
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilter()
|
|
|
|
{
|
|
|
|
return $this->filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setGroupOptions(array $options)
|
|
|
|
{
|
|
|
|
$this->groupOptions = $options;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGroupOptions()
|
|
|
|
{
|
|
|
|
return $this->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-05-04 12:18:25 +02:00
|
|
|
* Apply the given configuration to this backend
|
|
|
|
*
|
|
|
|
* @param ConfigObject $config
|
2015-02-06 16:32:26 +01:00
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* @return $this
|
2014-10-14 14:37:21 +02:00
|
|
|
*/
|
2015-05-04 12:18:25 +02:00
|
|
|
public function setConfig(ConfigObject $config)
|
2014-10-14 14:37:21 +02:00
|
|
|
{
|
2015-05-04 12:18:25 +02:00
|
|
|
return $this
|
|
|
|
->setBaseDn($config->base_dn)
|
|
|
|
->setUserClass($config->user_class)
|
|
|
|
->setUserNameAttribute($config->user_name_attribute)
|
|
|
|
->setFilter($config->filter);
|
|
|
|
}
|
2015-03-11 09:52:14 +01:00
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
/**
|
|
|
|
* Return a new query for the given columns
|
|
|
|
*
|
|
|
|
* @param array $columns The desired columns, if null all columns will be queried
|
|
|
|
*
|
|
|
|
* @return RepositoryQuery
|
|
|
|
*/
|
|
|
|
public function select(array $columns = null)
|
|
|
|
{
|
|
|
|
$query = parent::select($columns);
|
|
|
|
$query->getQuery()->setBase($this->baseDn);
|
|
|
|
if ($this->filter) {
|
|
|
|
$query->getQuery()->where(new Expression($this->filter));
|
2015-03-11 09:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
2014-10-14 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2015-05-04 12:18:25 +02:00
|
|
|
* Initialize this repository's query columns
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2015-05-07 08:28:32 +02:00
|
|
|
* @return array
|
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* @throws ProgrammingError In case either $this->userNameAttribute or $this->userClass has not been set yet
|
2015-02-06 16:32:26 +01:00
|
|
|
*/
|
2015-05-04 12:18:25 +02:00
|
|
|
protected function initializeQueryColumns()
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2015-05-07 08:28:32 +02:00
|
|
|
if ($this->userClass === null) {
|
|
|
|
throw new ProgrammingError('It is required to set the objectClass where to look for users first');
|
|
|
|
}
|
|
|
|
if ($this->userNameAttribute === null) {
|
|
|
|
throw new ProgrammingError('It is required to set a attribute name where to find a user\'s name first');
|
|
|
|
}
|
2015-05-04 12:18:25 +02:00
|
|
|
|
2015-06-01 12:23:16 +02:00
|
|
|
if ($this->ds->getCapabilities()->hasAdOid()) {
|
|
|
|
$isActiveAttribute = 'userAccountControl';
|
|
|
|
$createdAtAttribute = 'whenCreated';
|
|
|
|
$lastModifiedAttribute = 'whenChanged';
|
|
|
|
} else {
|
|
|
|
$isActiveAttribute = 'unknown';
|
|
|
|
$createdAtAttribute = 'unknown';
|
|
|
|
$lastModifiedAttribute = 'unknown';
|
|
|
|
}
|
|
|
|
|
2015-05-07 08:28:32 +02:00
|
|
|
return array(
|
|
|
|
$this->userClass => array(
|
2015-05-06 10:27:26 +02:00
|
|
|
'user' => $this->userNameAttribute,
|
2015-05-04 12:18:25 +02:00
|
|
|
'user_name' => $this->userNameAttribute,
|
2015-06-01 12:23:16 +02:00
|
|
|
'is_active' => $isActiveAttribute,
|
|
|
|
'created_at' => $createdAtAttribute,
|
|
|
|
'last_modified' => $lastModifiedAttribute
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this repository's conversion rules
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws ProgrammingError In case $this->userClass has not been set yet
|
|
|
|
*/
|
|
|
|
protected function initializeConversionRules()
|
|
|
|
{
|
|
|
|
if ($this->userClass === null) {
|
|
|
|
throw new ProgrammingError('It is required to set the objectClass where to look for users first');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->ds->getCapabilities()->hasAdOid()) {
|
|
|
|
$stateConverter = 'user_account_control';
|
|
|
|
$timeConverter = 'generalized_time';
|
|
|
|
} else {
|
|
|
|
$timeConverter = null;
|
|
|
|
$stateConverter = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$this->userClass => array(
|
|
|
|
'is_active' => $stateConverter,
|
|
|
|
'created_at' => $timeConverter,
|
|
|
|
'last_modified' => $timeConverter
|
2015-05-07 08:28:32 +02:00
|
|
|
)
|
|
|
|
);
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 12:23:16 +02:00
|
|
|
/**
|
|
|
|
* Return whether the given userAccountControl value defines that a user is permitted to login
|
|
|
|
*
|
|
|
|
* @param string|null $value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function retrieveUserAccountControl($value)
|
|
|
|
{
|
|
|
|
if ($value === null) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ADS_UF_ACCOUNTDISABLE = 2;
|
|
|
|
return ((int) $value & $ADS_UF_ACCOUNTDISABLE) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the given value based on the ASN.1 standard (GeneralizedTime) and return its timestamp representation
|
|
|
|
*
|
|
|
|
* @param string|null $value
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function retrieveGeneralizedTime($value)
|
|
|
|
{
|
|
|
|
if ($value === null) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
($dateTime = DateTime::createFromFormat('YmdHis.uO', $value)) !== false
|
|
|
|
|| ($dateTime = DateTime::createFromFormat('YmdHis.uZ', $value)) !== false
|
|
|
|
|| ($dateTime = DateTime::createFromFormat('YmdHis.u', $value)) !== false
|
|
|
|
) {
|
|
|
|
return $dateTime->getTimeStamp();
|
|
|
|
} else {
|
|
|
|
Logger::debug(sprintf(
|
|
|
|
'Failed to parse "%s" based on the ASN.1 standard (GeneralizedTime) for user backend "%s".',
|
|
|
|
$value,
|
|
|
|
$this->getName()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 14:22:52 +02:00
|
|
|
/**
|
|
|
|
* Probe the backend to test if authentication is possible
|
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* Try to bind to the backend and fetch a single user to check if:
|
2014-06-11 14:22:52 +02:00
|
|
|
* <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-05-04 12:18:25 +02:00
|
|
|
$result = $this->select()->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-05-04 12:18:25 +02:00
|
|
|
'No objects with objectClass "%s" in DN "%s" found. (Filter: %s)',
|
2014-08-22 10:59:52 +02:00
|
|
|
$this->userClass,
|
2015-05-04 12:18:25 +02:00
|
|
|
$this->baseDn ?: $this->ds->getDn(),
|
|
|
|
$this->filter ?: 'None'
|
2014-07-03 16:20:45 +02:00
|
|
|
);
|
2014-06-11 14:22:52 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
if (! isset($result->user_name)) {
|
2014-06-11 14:22:52 +02:00
|
|
|
throw new AuthenticationException(
|
2015-05-04 12:18:25 +02:00
|
|
|
'UserNameAttribute "%s" not existing in objectClass "%s"',
|
2014-08-22 10:59:52 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
$result = $this->ds->select()
|
2014-10-01 15:58:53 +02:00
|
|
|
->setBase($this->groupOptions['group_base_dn'])
|
|
|
|
->from(
|
|
|
|
$this->groupOptions['group_class'],
|
|
|
|
array($this->groupOptions['group_attribute'])
|
|
|
|
)
|
|
|
|
->where(
|
|
|
|
$this->groupOptions['group_member_attribute'],
|
|
|
|
$dn
|
2015-05-04 12:18:25 +02:00
|
|
|
)
|
|
|
|
->fetchAll();
|
2014-10-01 15:58:53 +02:00
|
|
|
|
|
|
|
$groups = array();
|
|
|
|
foreach ($result as $group) {
|
|
|
|
$groups[] = $group->{$this->groupOptions['group_attribute']};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2015-05-04 12:18:25 +02:00
|
|
|
* Authenticate the given user
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* @param User $user
|
|
|
|
* @param string $password
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* @return bool True on success, false on failure
|
2015-02-06 16:32:26 +01:00
|
|
|
*
|
2015-05-04 12:18:25 +02:00
|
|
|
* @throws AuthenticationException In case authentication is not possible due to an error
|
2013-08-28 10:16:18 +02:00
|
|
|
*/
|
2015-05-04 12:18:25 +02:00
|
|
|
public function authenticate(User $user, $password)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2014-03-28 14:45:03 +01:00
|
|
|
try {
|
2015-05-04 12:18:25 +02:00
|
|
|
$userDn = $this
|
|
|
|
->select()
|
|
|
|
->where('user_name', str_replace('*', '', $user->getUsername()))
|
|
|
|
->getQuery()
|
|
|
|
->setUsePagedResults(false)
|
|
|
|
->fetchDn();
|
|
|
|
|
2015-04-21 13:15:40 +02:00
|
|
|
if ($userDn === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-04 12:18:25 +02:00
|
|
|
$authenticated = $this->ds->testCredentials($userDn, $password);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|