2016-05-04 19:21:24 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2021-09-29 17:03:13 +02:00
|
|
|
* Pandora FMS- http://barivion.com
|
2016-05-04 19:21:24 +02:00
|
|
|
* ==================================================
|
2020-11-27 13:52:35 +01:00
|
|
|
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
|
2016-05-04 19:21:24 +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 for version 2.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Load global vars
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if (!$config['ehorus_enabled']) {
|
2019-01-30 16:18:44 +01:00
|
|
|
return;
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
check_login();
|
2016-06-22 14:50:57 +02:00
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
// Get the parameters
|
2016-05-04 19:21:24 +02:00
|
|
|
$agent_id = (int) get_parameter('id_agente');
|
2016-05-09 13:06:30 +02:00
|
|
|
$client_tab = (string) get_parameter('client_tab');
|
2016-05-04 19:21:24 +02:00
|
|
|
|
|
|
|
if (empty($agent_id)) {
|
2019-01-30 16:18:44 +01:00
|
|
|
ui_print_error_message(__('Missing agent id'));
|
|
|
|
return;
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 14:50:57 +02:00
|
|
|
$group_id = db_get_value('id_grupo', 'tagente', 'id_agente', $agent_id);
|
|
|
|
|
2016-06-22 15:28:32 +02:00
|
|
|
if ($group_id === false || (!check_acl($config['id_user'], $group_id, 'AW') && !is_user_admin($config['id_user']))) {
|
2019-01-30 16:18:44 +01:00
|
|
|
db_pandora_audit('ACL Violation', 'Trying to access eHorus');
|
|
|
|
include 'general/noaccess.php';
|
|
|
|
return;
|
2016-06-22 14:50:57 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
require_once $config['homedir'].'/include/functions_ui.php';
|
|
|
|
require_once $config['homedir'].'/include/functions_agents.php';
|
2016-06-22 14:50:57 +02:00
|
|
|
|
2016-05-05 18:32:34 +02:00
|
|
|
$ehorus_agent_id = agents_get_agent_custom_field($agent_id, $config['ehorus_custom_field']);
|
2016-05-04 19:21:24 +02:00
|
|
|
|
|
|
|
if (empty($ehorus_agent_id)) {
|
2019-01-30 16:18:44 +01:00
|
|
|
ui_print_error_message(__('Missing ehorus agent id'));
|
|
|
|
return;
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Directory data
|
|
|
|
$hostname = $config['ehorus_hostname'];
|
|
|
|
$port = $config['ehorus_port'];
|
|
|
|
$user = $config['ehorus_user'];
|
2018-11-14 12:16:59 +01:00
|
|
|
$password = io_safe_output(io_output_password($config['ehorus_pass']));
|
2016-05-04 19:21:24 +02:00
|
|
|
$curl_timeout = $config['ehorus_req_timeout'];
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$base_url = 'https://'.$hostname.':'.$port;
|
2017-11-13 12:03:01 +01:00
|
|
|
|
2019-05-13 15:00:29 +02:00
|
|
|
// If user login is enabled, fetch user and pass from user info
|
2019-05-28 13:14:42 +02:00
|
|
|
if ($config['ehorus_user_level_conf']) {
|
|
|
|
$user = $user_info['ehorus_user_level_user'];
|
|
|
|
$password = io_safe_output(io_output_password($user_info['ehorus_user_level_pass']));
|
2019-05-13 15:00:29 +02:00
|
|
|
}
|
|
|
|
|
2017-11-13 12:03:01 +01:00
|
|
|
// Get the login auth token
|
|
|
|
$login_path = '/api/login';
|
2019-01-30 16:18:44 +01:00
|
|
|
$body = [
|
|
|
|
'user' => $user,
|
|
|
|
'pass' => $password,
|
|
|
|
'exp' => 300,
|
|
|
|
// 5 minutes
|
|
|
|
];
|
2017-11-13 12:03:01 +01:00
|
|
|
$ch = curl_init();
|
2019-01-30 16:18:44 +01:00
|
|
|
curl_setopt($ch, CURLOPT_URL, $base_url.$login_path);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
|
2017-11-13 12:03:01 +01:00
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
|
|
|
|
|
|
|
|
$result_login = curl_exec($ch);
|
|
|
|
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
$error = false;
|
|
|
|
if ($result_login === false) {
|
2019-01-30 16:18:44 +01:00
|
|
|
$error = curl_error($ch);
|
2017-11-13 12:03:01 +01:00
|
|
|
}
|
2019-01-30 16:18:44 +01:00
|
|
|
|
2017-11-13 12:03:01 +01:00
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($error !== false || $http_status !== 200) {
|
2019-01-30 16:18:44 +01:00
|
|
|
if ($error !== false) {
|
|
|
|
// echo $error;
|
|
|
|
ui_print_error_message(__('There was an error retrieving an authorization token'));
|
|
|
|
} else {
|
|
|
|
ui_print_error_message($http_status.' '.$result_login);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2017-11-13 12:03:01 +01:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$response_auth = [];
|
2017-11-13 12:03:01 +01:00
|
|
|
try {
|
2019-01-30 16:18:44 +01:00
|
|
|
$response_auth = json_decode($result_login, true);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
ui_print_error_message(__('There was an error processing the response'));
|
2017-11-13 12:03:01 +01:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:21:24 +02:00
|
|
|
// Get the agent auth token
|
2019-01-30 16:18:44 +01:00
|
|
|
$token_path = '/api/agents/'.$ehorus_agent_id.'/token';
|
2016-05-04 19:21:24 +02:00
|
|
|
$ch = curl_init();
|
2019-01-30 16:18:44 +01:00
|
|
|
curl_setopt($ch, CURLOPT_URL, $base_url.$token_path);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
|
2016-05-04 19:21:24 +02:00
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout);
|
2019-01-30 16:18:44 +01:00
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: JWT '.$response_auth['token']]);
|
2016-05-04 19:21:24 +02:00
|
|
|
|
|
|
|
$result_token = curl_exec($ch);
|
|
|
|
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
$error = false;
|
|
|
|
if ($result_token === false) {
|
2019-01-30 16:18:44 +01:00
|
|
|
$error = curl_error($ch);
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
2019-01-30 16:18:44 +01:00
|
|
|
|
2016-05-04 19:21:24 +02:00
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($error !== false || $http_status !== 200) {
|
2019-01-30 16:18:44 +01:00
|
|
|
if ($error !== false) {
|
|
|
|
// echo $error;
|
|
|
|
ui_print_error_message(__('There was an error retrieving an authorization token'));
|
|
|
|
} else {
|
|
|
|
ui_print_error_message($http_status.' '.$result_token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$response_auth = [];
|
2016-05-04 19:21:24 +02:00
|
|
|
try {
|
2019-01-30 16:18:44 +01:00
|
|
|
$response_auth = json_decode($result_token, true);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
ui_print_error_message(__('There was an error processing the response'));
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get agent info
|
2019-01-30 16:18:44 +01:00
|
|
|
$agent_path = '/api/agents/'.$ehorus_agent_id;
|
2016-05-04 19:21:24 +02:00
|
|
|
$ch = curl_init();
|
2019-01-30 16:18:44 +01:00
|
|
|
curl_setopt($ch, CURLOPT_URL, $base_url.$agent_path);
|
2016-05-04 19:21:24 +02:00
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout);
|
2019-01-30 16:18:44 +01:00
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: JWT '.$response_auth['token']]);
|
2016-05-04 19:21:24 +02:00
|
|
|
|
|
|
|
$result_agent = curl_exec($ch);
|
|
|
|
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
$error = false;
|
|
|
|
if ($result_agent === false) {
|
2019-01-30 16:18:44 +01:00
|
|
|
$error = curl_error($ch);
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
2019-01-30 16:18:44 +01:00
|
|
|
|
2016-05-04 19:21:24 +02:00
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($error !== false || $http_status !== 200) {
|
2019-01-30 16:18:44 +01:00
|
|
|
if ($error !== false) {
|
|
|
|
// echo $error;
|
|
|
|
ui_print_error_message(__('There was an error retrieving the agent data'));
|
|
|
|
} else {
|
|
|
|
ui_print_error_message($http_status.' '.$result_agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$agent_data = [];
|
2016-05-04 19:21:24 +02:00
|
|
|
try {
|
2019-01-30 16:18:44 +01:00
|
|
|
$agent_data = json_decode($result_agent, true);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
ui_print_error_message(__('There was an error processing the response'));
|
2016-05-04 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:40:23 +01:00
|
|
|
echo '<table id="ehorus-client-run-info" class="databox w100p"><tr>';
|
2016-05-04 19:21:24 +02:00
|
|
|
echo '<td>';
|
|
|
|
echo __('Remote management of this agent with eHorus');
|
|
|
|
echo '</td><td>';
|
2019-01-30 16:18:44 +01:00
|
|
|
echo '<input type="button" id="run-ehorus-client" class="sub next" value="'.__('Launch').'" />';
|
2016-05-04 19:21:24 +02:00
|
|
|
echo '</td>';
|
|
|
|
echo '</tr></table>';
|
|
|
|
|
2021-03-11 15:40:23 +01:00
|
|
|
echo '<div id="expired_message" class="invisible">';
|
2016-05-09 16:22:01 +02:00
|
|
|
ui_print_error_message(
|
2019-01-30 16:18:44 +01:00
|
|
|
__('The connection was lost and the authorization token was expired').'. '.__('Reload the page to request a new authorization token').'. '
|
2016-05-09 16:22:01 +02:00
|
|
|
);
|
|
|
|
echo '</div>';
|
|
|
|
|
2016-05-09 13:06:30 +02:00
|
|
|
echo '<div id="ehorus-client-iframe"></div>';
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$query_data = [
|
|
|
|
'agent_id' => $ehorus_agent_id,
|
|
|
|
'hostname' => (string) $agent_data['serverAddress'],
|
|
|
|
'port' => (int) $agent_data['serverPort'],
|
|
|
|
'token' => (string) $response_auth['token'],
|
|
|
|
'expiration' => (int) $response_auth['exp'],
|
|
|
|
'is_busy' => (bool) $agent_data['isBusy'],
|
|
|
|
'last_connection' => (int) $agent_data['lastConnection'],
|
|
|
|
'section' => $client_tab,
|
|
|
|
];
|
2016-05-09 13:06:30 +02:00
|
|
|
$query = http_build_query($query_data);
|
2019-01-30 16:18:44 +01:00
|
|
|
$client_url = $config['homeurl'].'operation/agentes/ehorus_client.php?'.$query;
|
2016-05-04 19:21:24 +02:00
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
2019-01-30 16:18:44 +01:00
|
|
|
$(document).ready(function () {
|
|
|
|
var handleTabClick = function (section, messager) {
|
|
|
|
return function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
messager({
|
|
|
|
action: 'change_section',
|
|
|
|
payload: {
|
|
|
|
section: section
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var heightCorrection = 20;
|
|
|
|
|
|
|
|
var createIframe = function (node, src) {
|
|
|
|
var iframe = document.createElement('iframe');
|
|
|
|
iframe.src = src;
|
|
|
|
iframe.style.border = 'none';
|
|
|
|
iframe.style.position = 'relative';
|
|
|
|
iframe.style.top = '-' + heightCorrection + 'px';
|
|
|
|
iframe.style.border = 'none';
|
|
|
|
resizeIframe(iframe);
|
|
|
|
node.appendChild(iframe);
|
|
|
|
|
|
|
|
return iframe;
|
|
|
|
}
|
|
|
|
|
|
|
|
var getOptimalIframeSize = function () {
|
|
|
|
var $elem = $('div#ehorus-client-iframe');
|
|
|
|
return {
|
|
|
|
width: $elem.width(),
|
|
|
|
height: $(window).height() - $elem.offset().top + heightCorrection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var resizeIframe = function (iframe) {
|
|
|
|
var size = getOptimalIframeSize();
|
|
|
|
iframe.style.width = size.width + 'px';
|
|
|
|
iframe.style.height = size.height + 'px';
|
|
|
|
}
|
|
|
|
var handleResize = function (iframe) {
|
|
|
|
return function (event) {
|
|
|
|
resizeIframe(iframe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var handleMessage = function (iframe, actionHandlers) {
|
|
|
|
return function (event) {
|
|
|
|
// The message source should be the created iframe
|
|
|
|
if (event.origin === window.location.origin &&
|
|
|
|
event.source !== iframe.contentWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof actionHandlers === 'undefined') return;
|
|
|
|
|
|
|
|
if (event.data.action in actionHandlers) {
|
|
|
|
actionHandlers[event.data.action](event.data.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var messageToElement = function (elem, message) {
|
|
|
|
elem.postMessage(message, window.location.origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
var handleButtonClick = function (event) {
|
|
|
|
$('table#ehorus-client-run-info').remove();
|
|
|
|
|
|
|
|
// Init iframe
|
|
|
|
var clientURL = '<?php echo $client_url; ?>';
|
|
|
|
var iframe = createIframe(document.getElementById('ehorus-client-iframe'), clientURL);
|
|
|
|
|
|
|
|
var messageToIframe = function (message) {
|
|
|
|
return messageToElement(iframe.contentWindow, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
var actionHandlers = {
|
|
|
|
ready: function () {
|
|
|
|
$('a.ehorus_tab').click(handleTabClick('system', messageToIframe));
|
|
|
|
$('a.tab_terminal').click(handleTabClick('terminal', messageToIframe));
|
|
|
|
$('a.tab_display').click(handleTabClick('display', messageToIframe));
|
|
|
|
$('a.tab_processes').click(handleTabClick('processes', messageToIframe));
|
|
|
|
$('a.tab_services').click(handleTabClick('services', messageToIframe));
|
|
|
|
$('a.tab_files').click(handleTabClick('files', messageToIframe));
|
|
|
|
},
|
|
|
|
expired: function () {
|
|
|
|
$(iframe).remove();
|
|
|
|
$('a.ehorus_tab').unbind('click');
|
|
|
|
$('a.tab_terminal').unbind('click');
|
|
|
|
$('a.tab_display').unbind('click');
|
|
|
|
$('a.tab_processes').unbind('click');
|
|
|
|
$('a.tab_services').unbind('click');
|
|
|
|
$('a.tab_files').unbind('click');
|
|
|
|
iframe = null;
|
|
|
|
$('div#expired_message').show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for messages
|
|
|
|
window.addEventListener('message', handleMessage(iframe, actionHandlers));
|
|
|
|
// Listen for resize
|
|
|
|
window.addEventListener('resize', handleResize(iframe));
|
|
|
|
}
|
|
|
|
|
|
|
|
$('input#run-ehorus-client').click(handleButtonClick);
|
|
|
|
});
|
2018-11-14 12:16:59 +01:00
|
|
|
</script>
|