2013-06-10 16:03:51 +02:00
|
|
|
<?php
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-28 16:47:30 +02:00
|
|
|
/**
|
2013-07-15 12:26:10 +02:00
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
2013-06-28 16:47:30 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
2013-07-15 12:26:10 +02:00
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
2013-06-28 16:47:30 +02:00
|
|
|
*/
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-10 16:03:51 +02:00
|
|
|
|
|
|
|
namespace Icinga\Authentication\Backend;
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
use Icinga\User;
|
2013-06-25 12:24:52 +02:00
|
|
|
use Icinga\Authentication\UserBackend;
|
|
|
|
use Icinga\Authentication\Credentials;
|
2013-06-10 16:03:51 +02:00
|
|
|
use Icinga\Protocol\Ldap;
|
2013-08-12 15:02:25 +02:00
|
|
|
use \Icinga\Application\Config as IcingaConfig;
|
2013-06-10 16:03:51 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* User authentication backend (@see Icinga\Authentication\UserBackend) for
|
2013-07-12 15:37:36 +02:00
|
|
|
* authentication of users via LDAP. The attributes and location of the
|
2013-06-27 15:18:24 +02:00
|
|
|
* user is configurable via the application.ini
|
|
|
|
*
|
|
|
|
* See the UserBackend class (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* usage information
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
class LdapUserBackend implements UserBackend
|
|
|
|
{
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* @var Ldap\Connection
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
protected $connection;
|
2013-07-12 15:37:36 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* The ldap connection information
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Authentication backend using the
|
|
|
|
* connection information provided in $config
|
|
|
|
*
|
|
|
|
* @param object $config The ldap connection information
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
public function __construct($config)
|
|
|
|
{
|
|
|
|
$this->connection = new Ldap\Connection($config);
|
2013-08-13 18:08:21 +02:00
|
|
|
$this->config = $config;
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* @see Icinga\Authentication\UserBackend::hasUsername
|
|
|
|
**/
|
2013-06-25 12:24:52 +02:00
|
|
|
public function hasUsername(Credentials $credential)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
|
|
|
return $this->connection->fetchOne(
|
2013-06-25 12:24:52 +02:00
|
|
|
$this->selectUsername($credential->getUsername())
|
|
|
|
) === $credential->getUsername();
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* Removes the '*' characted from $string
|
|
|
|
*
|
|
|
|
* @param String $string
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
protected function stripAsterisks($string)
|
|
|
|
{
|
|
|
|
return str_replace('*', '', $string);
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* Tries to fetch the username given in $username from
|
|
|
|
* the ldap connection, using the configuration parameters
|
|
|
|
* given in the Authentication configuration
|
|
|
|
*
|
|
|
|
* @param String $username The username to select
|
|
|
|
*
|
|
|
|
* @return object $result
|
|
|
|
**/
|
2013-06-10 16:03:51 +02:00
|
|
|
protected function selectUsername($username)
|
|
|
|
{
|
|
|
|
return $this->connection->select()
|
2013-07-26 15:58:16 +02:00
|
|
|
->from(
|
2013-08-13 18:08:21 +02:00
|
|
|
$this->config->user_class,
|
2013-07-26 15:58:16 +02:00
|
|
|
array(
|
2013-08-13 18:08:21 +02:00
|
|
|
$this->config->user_name_attribute
|
2013-07-26 15:58:16 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
->where(
|
2013-08-13 18:08:21 +02:00
|
|
|
$this->config->user_name_attribute,
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->stripAsterisks($username)
|
|
|
|
);
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* @see Icinga\Authentication\UserBackend::authenticate
|
|
|
|
**/
|
2013-06-25 12:24:52 +02:00
|
|
|
public function authenticate(Credentials $credentials)
|
2013-06-10 16:03:51 +02:00
|
|
|
{
|
2013-06-11 13:53:42 +02:00
|
|
|
if (!$this->connection->testCredentials(
|
2013-08-26 16:56:23 +02:00
|
|
|
$this->connection->fetchDN($this->selectUsername($credentials->getUsername())),
|
|
|
|
$credentials->getPassword()
|
|
|
|
)
|
2013-06-26 16:48:07 +02:00
|
|
|
) {
|
2013-06-10 16:03:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-25 12:24:52 +02:00
|
|
|
$user = new User($credentials->getUsername());
|
2013-06-11 13:53:42 +02:00
|
|
|
|
2013-06-10 16:03:51 +02:00
|
|
|
return $user;
|
|
|
|
}
|
2013-08-26 16:56:23 +02:00
|
|
|
|
|
|
|
public function getUserCount() {
|
|
|
|
return $this->connection->count(
|
|
|
|
$this->connection->select()->from(
|
|
|
|
$this->config->user_class,
|
|
|
|
array(
|
|
|
|
$this->config->user_name_attribute
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2013-06-10 16:03:51 +02:00
|
|
|
}
|