2014-09-02 15:39:21 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-09-02 15:39:21 +02:00
|
|
|
|
2014-11-14 10:57:14 +01:00
|
|
|
namespace Icinga\Forms\Config\Resource;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Icinga\Web\Form;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-09-02 15:39:21 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2015-03-12 15:17:19 +01:00
|
|
|
use Icinga\Protocol\Ldap\Connection;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form class for adding/modifying ldap resources
|
|
|
|
*/
|
|
|
|
class LdapResourceForm extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->setName('form_config_resource_ldap');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Form::createElements()
|
|
|
|
*/
|
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
2014-09-29 11:20:39 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Resource Name'),
|
2015-03-11 08:00:20 +01:00
|
|
|
'description' => $this->translate('The unique name of this resource')
|
2014-09-29 11:20:39 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'hostname',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Host'),
|
|
|
|
'description' => $this->translate(
|
2015-03-11 08:00:20 +01:00
|
|
|
'The hostname or address of the LDAP server to use for authentication'
|
2015-01-19 11:26:23 +01:00
|
|
|
),
|
2015-03-11 08:00:20 +01:00
|
|
|
'value' => 'localhost'
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
2014-11-14 10:15:11 +01:00
|
|
|
'number',
|
|
|
|
'port',
|
|
|
|
array(
|
2015-03-11 08:00:20 +01:00
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Port'),
|
2015-03-11 08:00:20 +01:00
|
|
|
'description' => $this->translate('The port of the LDAP server to use for authentication'),
|
2014-11-14 10:15:11 +01:00
|
|
|
'value' => 389
|
2014-09-02 15:39:21 +02:00
|
|
|
)
|
|
|
|
);
|
2015-03-12 15:17:19 +01:00
|
|
|
$this->addElement(
|
|
|
|
'select',
|
2015-03-13 00:02:18 +01:00
|
|
|
'encryption',
|
2015-03-12 15:17:19 +01:00
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'autosubmit' => true,
|
2015-03-13 00:02:18 +01:00
|
|
|
'label' => $this->translate('Encryption'),
|
2015-03-12 15:17:19 +01:00
|
|
|
'description' => $this->translate(
|
2015-03-13 00:00:21 +01:00
|
|
|
'Whether to encrypt communication. Choose STARTTLS or LDAPS for encrypted communication or'
|
2015-03-13 00:47:23 +01:00
|
|
|
. ' none for unencrypted communication'
|
2015-03-12 15:17:19 +01:00
|
|
|
),
|
|
|
|
'multiOptions' => array(
|
2015-03-13 08:53:08 +01:00
|
|
|
'none' => $this->translate('None', 'resource.ldap.encryption'),
|
2015-03-13 00:03:01 +01:00
|
|
|
Connection::STARTTLS => 'STARTTLS',
|
|
|
|
Connection::LDAPS => 'LDAPS'
|
2015-03-12 15:17:19 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-03-13 00:47:23 +01:00
|
|
|
if (isset($formData['encryption']) && $formData['encryption'] !== 'none') {
|
2015-03-12 15:17:19 +01:00
|
|
|
// TODO(jom): Do not show this checkbox unless the connection is actually failing due to certificate errors
|
|
|
|
$this->addElement(
|
|
|
|
'checkbox',
|
|
|
|
'reqcert',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'label' => $this->translate('Require Certificate'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'When checked, the LDAP server must provide a valid and known (trusted) certificate.'
|
|
|
|
),
|
|
|
|
'value' => 1
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'root_dn',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Root DN'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Only the root and its child nodes will be accessible on this resource.'
|
|
|
|
)
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'bind_dn',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Bind DN'),
|
2015-03-11 08:00:20 +01:00
|
|
|
'description' => $this->translate('The user dn to use for querying the ldap server')
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'password',
|
|
|
|
'bind_pw',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'renderPassword' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Bind Password'),
|
2015-03-11 08:00:20 +01:00
|
|
|
'description' => $this->translate('The password to use for querying the ldap server')
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that the current configuration points to a valid resource
|
|
|
|
*
|
|
|
|
* @see Form::onSuccess()
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onSuccess()
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
2014-09-29 11:02:45 +02:00
|
|
|
if (false === static::isValidResource($this)) {
|
2014-09-02 15:39:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the resource configuration by trying to connect with it
|
|
|
|
*
|
|
|
|
* @param Form $form The form to fetch the configuration values from
|
|
|
|
*
|
|
|
|
* @return bool Whether validation succeeded or not
|
|
|
|
*/
|
2014-09-29 11:02:45 +02:00
|
|
|
public static function isValidResource(Form $form)
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
|
|
|
try {
|
2014-11-18 13:11:52 +01:00
|
|
|
$resource = ResourceFactory::createResource(new ConfigObject($form->getValues()));
|
2014-09-29 11:22:43 +02:00
|
|
|
if (false === $resource->testCredentials(
|
|
|
|
$form->getElement('bind_dn')->getValue(),
|
|
|
|
$form->getElement('bind_pw')->getValue()
|
|
|
|
)
|
|
|
|
) {
|
2015-03-12 14:45:16 +01:00
|
|
|
throw new Exception(); // TODO: Get the exact error message
|
2014-09-29 11:22:43 +02:00
|
|
|
}
|
2014-09-02 15:39:21 +02:00
|
|
|
} catch (Exception $e) {
|
2015-03-12 14:45:16 +01:00
|
|
|
$msg = $form->translate('Connectivity validation failed, connection to the given resource not possible.');
|
|
|
|
if (($error = $e->getMessage())) {
|
|
|
|
$msg .= ' (' . $error . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->addError($msg);
|
2014-09-02 15:39:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|