2013-06-28 19:00:30 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-06-28 19:00:30 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-06-28 19:00:30 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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-10-23 15:10:33 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Authentication\Backend;
|
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
use \Exception;
|
|
|
|
use \stdClass;
|
|
|
|
use \Zend_Config;
|
2013-08-26 17:23:31 +02:00
|
|
|
use \Zend_Db;
|
2013-08-28 15:56:33 +02:00
|
|
|
use \Zend_Db_Adapter_Abstract;
|
2013-11-06 10:20:15 +01:00
|
|
|
use \Icinga\Data\ResourceFactory;
|
2013-08-15 14:16:34 +02:00
|
|
|
use \Icinga\User;
|
|
|
|
use \Icinga\Authentication\UserBackend;
|
2013-08-30 10:24:05 +02:00
|
|
|
use \Icinga\Authentication\Credential;
|
2013-08-15 14:16:34 +02:00
|
|
|
use \Icinga\Authentication;
|
|
|
|
use \Icinga\Application\Logger;
|
2013-11-06 10:20:15 +01:00
|
|
|
use \Icinga\Exception\ProgrammingError;
|
2013-08-28 10:16:18 +02:00
|
|
|
use \Icinga\Exception\ConfigurationError;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-29 11:42:31 +02:00
|
|
|
* User authentication backend (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* authentication of users via an SQL database. The credentials needed to access
|
|
|
|
* the database are configurable via the application.ini
|
|
|
|
*
|
|
|
|
* See the UserBackend class (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* usage information
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-08-15 14:16:34 +02:00
|
|
|
class DbUserBackend implements UserBackend
|
|
|
|
{
|
2013-08-15 14:58:08 +02:00
|
|
|
/**
|
2013-11-27 10:58:43 +01:00
|
|
|
* The database connection that will be used for fetching users
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2013-11-27 10:58:43 +01:00
|
|
|
* @var Zend_Db
|
2013-08-15 14:58:08 +02:00
|
|
|
*/
|
2013-11-27 10:58:43 +01:00
|
|
|
private $db;
|
2013-08-15 14:58:08 +02:00
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
2013-11-27 10:58:43 +01:00
|
|
|
* The name of the user table
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2013-11-27 10:58:43 +01:00
|
|
|
* @var String
|
2013-08-28 10:16:18 +02:00
|
|
|
*/
|
2013-11-27 10:58:43 +01:00
|
|
|
private $userTable = 'account';
|
2013-08-15 14:58:08 +02:00
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
2013-11-27 10:58:43 +01:00
|
|
|
* Column name to identify active users
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-11-27 10:58:43 +01:00
|
|
|
private $activeColumnName = 'active';
|
2013-08-15 14:58:08 +02:00
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
2013-11-27 10:58:43 +01:00
|
|
|
* Column name to fetch the password
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-11-27 10:58:43 +01:00
|
|
|
private $passwordColumnName = 'password';
|
2013-08-15 14:58:08 +02:00
|
|
|
|
2013-07-26 15:01:52 +02:00
|
|
|
/**
|
2013-11-27 10:58:43 +01:00
|
|
|
* Column name for password salt
|
2013-07-26 15:01:52 +02:00
|
|
|
*
|
2013-11-27 10:58:43 +01:00
|
|
|
* @var string
|
2013-07-26 15:01:52 +02:00
|
|
|
*/
|
2013-11-27 10:58:43 +01:00
|
|
|
private $saltColumnName = 'salt';
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-26 15:01:52 +02:00
|
|
|
/**
|
2013-11-27 10:58:43 +01:00
|
|
|
* Column name for user name
|
2013-07-26 15:01:52 +02:00
|
|
|
*
|
2013-11-27 10:58:43 +01:00
|
|
|
* @var string
|
2013-07-26 15:01:52 +02:00
|
|
|
*/
|
2013-11-27 10:58:43 +01:00
|
|
|
private $userColumnName = 'username';
|
2013-08-28 10:16:18 +02:00
|
|
|
|
2013-11-28 17:22:53 +01:00
|
|
|
/**
|
|
|
|
* Column name of email
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $emailColumnName = null;
|
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
|
|
|
* Name of the backend
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $name;
|
2013-08-23 15:04:00 +02:00
|
|
|
|
2013-06-28 19:00:30 +02:00
|
|
|
/**
|
2013-11-06 10:20:15 +01:00
|
|
|
* Create a new DbUserBackend
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config The configuration for this authentication backend.
|
2013-11-06 14:49:16 +01:00
|
|
|
* 'resource' => The name of the resource to use, or an actual
|
|
|
|
* instance of Zend_Db_Adapter_Abstract
|
|
|
|
* 'name' => The name of this authentication backend
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2014-01-22 11:38:47 +01:00
|
|
|
* @throws ConfigurationError When the given resource does not exist.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-11-06 14:49:16 +01:00
|
|
|
public function __construct(Zend_Config $config)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-11-06 14:49:16 +01:00
|
|
|
if (!isset($config->resource)) {
|
|
|
|
throw new ConfigurationError('An authentication backend must provide a resource.');
|
|
|
|
}
|
2013-08-28 10:16:18 +02:00
|
|
|
$this->name = $config->name;
|
2013-08-28 15:56:33 +02:00
|
|
|
if ($config->resource instanceof Zend_Db_Adapter_Abstract) {
|
|
|
|
$this->db = $config->resource;
|
|
|
|
} else {
|
2013-11-06 14:49:16 +01:00
|
|
|
$resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($config->resource));
|
2013-11-06 10:20:15 +01:00
|
|
|
$this->db = $resource->getConnection();
|
2013-08-28 15:56:33 +02:00
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 10:58:43 +01:00
|
|
|
/**
|
|
|
|
* Setter for password column
|
|
|
|
*
|
|
|
|
* @param string $passwordColumnName
|
|
|
|
*/
|
|
|
|
public function setPasswordColumnName($passwordColumnName)
|
|
|
|
{
|
|
|
|
$this->passwordColumnName = $passwordColumnName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for password salt column
|
|
|
|
*
|
|
|
|
* @param string $saltColumnName
|
|
|
|
*/
|
|
|
|
public function setSaltColumnName($saltColumnName)
|
|
|
|
{
|
|
|
|
$this->saltColumnName = $saltColumnName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for usernamea column
|
|
|
|
*
|
|
|
|
* @param string $userColumnName
|
|
|
|
*/
|
|
|
|
public function setUserColumnName($userColumnName)
|
|
|
|
{
|
|
|
|
$this->userColumnName = $userColumnName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for database table
|
|
|
|
*
|
|
|
|
* @param String $userTable
|
|
|
|
*/
|
|
|
|
public function setUserTable($userTable)
|
|
|
|
{
|
|
|
|
$this->userTable = $userTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setter for column identifying an active user
|
|
|
|
*
|
|
|
|
* Set this to null if no active column exists.
|
|
|
|
*
|
|
|
|
* @param string $activeColumnName
|
|
|
|
*/
|
|
|
|
public function setActiveColumnName($activeColumnName)
|
|
|
|
{
|
|
|
|
$this->activeColumnName = $activeColumnName;
|
|
|
|
}
|
|
|
|
|
2013-11-28 17:22:53 +01:00
|
|
|
/**
|
|
|
|
* Setter for email column
|
|
|
|
*
|
|
|
|
* Set to null if not needed
|
|
|
|
*
|
|
|
|
* @param string $emailColumnName
|
|
|
|
*/
|
|
|
|
public function setEmailColumnName($emailColumnName)
|
|
|
|
{
|
|
|
|
$this->emailColumnName = $emailColumnName;
|
|
|
|
}
|
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
|
|
|
* Name of the backend
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2013-06-28 19:00:30 +02:00
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Check if the user identified by the given credentials is available
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-30 10:30:19 +02:00
|
|
|
* @param Credential $credential Credential to find a user in the database
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2013-08-30 10:30:19 +02:00
|
|
|
* @return boolean True when the username is known and currently active.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-08-30 10:24:05 +02:00
|
|
|
public function hasUsername(Credential $credential)
|
2013-06-28 19:00:30 +02:00
|
|
|
{
|
|
|
|
$user = $this->getUserByName($credential->getUsername());
|
2013-08-23 15:04:00 +02:00
|
|
|
return isset($user);
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Authenticate a user with the given credentials
|
|
|
|
*
|
2013-08-30 10:30:19 +02:00
|
|
|
* @param Credential $credential Credential to authenticate
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @return User|null The authenticated user or Null.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-08-30 10:24:05 +02:00
|
|
|
public function authenticate(Credential $credential)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-08-23 15:04:00 +02:00
|
|
|
try {
|
|
|
|
$salt = $this->getUserSalt($credential->getUsername());
|
2013-08-28 10:16:18 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Logger::error(
|
2013-08-30 10:30:19 +02:00
|
|
|
'Could not fetch salt from database for user %s. Exception was thrown: %s',
|
2013-08-28 10:16:18 +02:00
|
|
|
$credential->getUsername(),
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
2013-08-23 15:04:00 +02:00
|
|
|
return null;
|
|
|
|
}
|
2013-11-27 10:58:43 +01:00
|
|
|
$sth = $this->db
|
2013-06-28 19:00:30 +02:00
|
|
|
->select()->from($this->userTable)
|
2013-11-27 10:58:43 +01:00
|
|
|
->where($this->userColumnName . ' = ?', $credential->getUsername())
|
|
|
|
->where(
|
|
|
|
$this->passwordColumnName . ' = ?',
|
|
|
|
$this->createPasswordHash($credential->getPassword(), $salt)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->activeColumnName !== null) {
|
|
|
|
$sth->where($this->activeColumnName . ' = ?', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $sth->query()->fetch();
|
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
if ($res !== false) {
|
2013-06-28 19:00:30 +02:00
|
|
|
return $this->createUserFromResult($res);
|
|
|
|
}
|
2013-08-28 10:16:18 +02:00
|
|
|
|
|
|
|
return null;
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Fetch the users salt from the database
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @param string$username The user whose salt should be fetched
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @return string|null Return the salt-string or null, when the user does not exist
|
|
|
|
* @throws ProgrammingError
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function getUserSalt($username)
|
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
$res = $this->db->select()
|
2013-11-27 10:58:43 +01:00
|
|
|
->from($this->userTable, $this->saltColumnName)
|
|
|
|
->where($this->userColumnName . ' = ?', $username)
|
2013-06-28 19:00:30 +02:00
|
|
|
->query()->fetch();
|
2013-08-23 15:04:00 +02:00
|
|
|
if ($res !== false) {
|
2013-11-27 10:58:43 +01:00
|
|
|
return $res->{$this->saltColumnName};
|
2013-08-23 15:04:00 +02:00
|
|
|
} else {
|
2013-08-28 10:16:18 +02:00
|
|
|
throw new ProgrammingError('No Salt found for user "' . $username . '"');
|
2013-08-23 15:04:00 +02:00
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 10:58:43 +01:00
|
|
|
/**
|
|
|
|
* Create password hash at this place
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @param string $salt
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function createPasswordHash($password, $salt) {
|
|
|
|
return hash_hmac('sha256', $password, $salt);
|
|
|
|
}
|
|
|
|
|
2013-06-28 19:00:30 +02:00
|
|
|
/**
|
2013-07-26 12:58:21 +02:00
|
|
|
* Fetch the user information from the database
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @param string $username The name of the user
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @return User|null Returns the user object, or null when the user does not exist
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function getUserByName($username)
|
|
|
|
{
|
2013-08-30 12:17:12 +02:00
|
|
|
$this->db->getConnection();
|
2013-11-27 10:58:43 +01:00
|
|
|
$sth = $this->db->select()
|
|
|
|
->from($this->userTable)
|
|
|
|
->where($this->userColumnName .' = ?', $username);
|
|
|
|
|
|
|
|
if ($this->activeColumnName !== null) {
|
|
|
|
$sth->where($this->activeColumnName .' = ?', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $sth->query()->fetch();
|
|
|
|
|
2013-08-30 12:17:12 +02:00
|
|
|
if ($res !== false) {
|
|
|
|
return $this->createUserFromResult($res);
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
2013-08-30 12:17:12 +02:00
|
|
|
return null;
|
|
|
|
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Create a new instance of User from a query result
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-30 10:30:19 +02:00
|
|
|
* @param stdClass $resultRow Result object from database
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2013-08-28 10:16:18 +02:00
|
|
|
* @return User The created instance of User.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-11-28 17:22:53 +01:00
|
|
|
protected function createUserFromResult(stdClass $resultRow)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
$usr = new User(
|
2013-11-28 17:22:53 +01:00
|
|
|
$resultRow->{$this->userColumnName},
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
(isset($resultRow->{$this->emailColumnName})) ? $resultRow->{$this->emailColumnName} : null
|
2013-08-15 14:16:34 +02:00
|
|
|
);
|
2013-06-28 19:00:30 +02:00
|
|
|
return $usr;
|
|
|
|
}
|
2013-08-26 17:23:31 +02:00
|
|
|
|
2013-08-27 14:37:22 +02:00
|
|
|
/**
|
|
|
|
* Return the number of users in this database connection
|
|
|
|
*
|
|
|
|
* This class is mainly used for determining whether the authentication backend is valid or not
|
|
|
|
*
|
2013-08-30 15:31:21 +02:00
|
|
|
* @return int The number of users set in this backend
|
|
|
|
* @see UserBackend::getUserCount
|
2013-08-27 14:37:22 +02:00
|
|
|
*/
|
2013-08-26 17:23:31 +02:00
|
|
|
public function getUserCount()
|
|
|
|
{
|
|
|
|
$query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query();
|
|
|
|
return $query->fetch()->count;
|
|
|
|
}
|
2014-01-22 11:38:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to connect to the underlying database.
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError When the backend is not reachable with the given configuration.
|
|
|
|
*/
|
|
|
|
public function connect()
|
|
|
|
{
|
|
|
|
$this->db->getConnection();
|
|
|
|
}
|
2013-07-26 15:57:37 +02:00
|
|
|
}
|