pandorafms/pandora_console/include/class/ExternalTools.class.php

1146 lines
40 KiB
PHP
Raw Normal View History

2020-12-16 14:09:54 +01:00
<?php
/**
* External Tools view Class.
2020-12-16 14:09:54 +01:00
*
* @category Class
* @package Pandora FMS
* @subpackage Setup
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
2023-06-08 12:42:10 +02:00
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
2020-12-16 14:09:54 +01:00
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
2023-06-08 11:53:13 +02:00
* Copyright (c) 2005-2023 Pandora FMS
2023-06-08 13:19:01 +02:00
* Please see https://pandorafms.com/community/ for full contribution list
2020-12-16 14:09:54 +01: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.
* ============================================================================
*/
// Get global data.
global $config;
// Necessary classes for extends.
require_once $config['homedir'].'/include/class/HTML.class.php';
/**
* External Tools class
2020-12-16 14:09:54 +01:00
*/
class ExternalTools extends HTML
2020-12-16 14:09:54 +01:00
{
/**
2020-12-17 16:51:05 +01:00
* Constructor.
2020-12-17 13:04:21 +01:00
*
* @param string $origin Origin of the request.
2020-12-16 14:09:54 +01:00
*/
2020-12-17 13:04:21 +01:00
public function __construct(string $origin)
2020-12-16 14:09:54 +01:00
{
2020-12-17 13:04:21 +01:00
global $config;
// Check if the user can access here.
check_login();
// Setting the origin.
$this->origin = $origin;
if ($this->origin === 'agent') {
if (check_acl($config['id_user'], 0, 'AR') === false) {
db_pandora_audit(
2022-01-20 10:55:23 +01:00
AUDIT_LOG_ACL_VIOLATION,
2020-12-17 13:04:21 +01:00
'Trying to access Agent Management'
);
include 'general/noaccess.php';
return;
}
// Capture needed parameter for agent form.
2022-11-21 14:49:07 +01:00
$this->id_agente = (int) get_parameter('id_agente');
$this->operation = (int) get_parameter('operation');
2020-12-17 16:51:05 +01:00
$this->community = (string) get_parameter('community', 'public');
$this->ip = (string) get_parameter('select_ips');
$this->snmp_version = (string) get_parameter('select_version');
2020-12-17 13:04:21 +01:00
} else if ($this->origin === 'setup') {
if (check_acl($config['id_user'], 0, 'PM') === false) {
db_pandora_audit(
2022-01-20 10:55:23 +01:00
AUDIT_LOG_ACL_VIOLATION,
2020-12-17 13:04:21 +01:00
'Trying to access Profile Management'
);
include 'general/noaccess.php';
return;
}
// Capture needed parameters for setup form.
$this->updatePaths = (bool) get_parameter('update_paths', 0);
2020-12-17 13:04:21 +01:00
// Capture paths.
$this->pathTraceroute = (string) get_parameter('traceroute_path');
$this->pathPing = (string) get_parameter('ping_path');
$this->pathNmap = (string) get_parameter('nmap_path');
$this->pathDig = (string) get_parameter('dig_path');
$this->pathSnmpget = (string) get_parameter('snmpget_path');
2021-01-12 13:24:08 +01:00
// Capture custom commands.
$this->pathCustomComm = [];
foreach ($_REQUEST as $customKey => $customValue) {
if ((bool) preg_match('/command_custom_/', $customKey) === true) {
$temporaryCustomCommandId = explode('_', $customKey);
$customCommandId = $temporaryCustomCommandId[2];
// Define array for host the command/parameters pair data.
$this->pathCustomComm[$customValue] = [];
// Ensure the information.
$this->pathCustomComm[$customValue]['command_custom'] = (string) get_parameter(
'command_custom_'.$customCommandId
);
$this->pathCustomComm[$customValue]['params_custom'] = (string) get_parameter(
'params_custom_'.$customCommandId
);
2021-01-12 13:24:08 +01:00
}
}
2020-12-17 13:04:21 +01:00
}
return $this;
2020-12-16 14:09:54 +01:00
}
/**
2020-12-17 16:51:05 +01:00
* Run action.
2020-12-16 14:09:54 +01:00
*
* @return void
*/
2020-12-17 13:04:21 +01:00
public function run()
2020-12-16 14:09:54 +01:00
{
2020-12-17 13:04:21 +01:00
if ($this->origin === 'agent') {
// Print tool form.
$this->agentExternalToolsForm();
2020-12-17 13:04:21 +01:00
} else if ($this->origin === 'setup') {
// Print setup form.
$this->setupExternalToolsForm();
2020-12-16 14:09:54 +01:00
}
2020-12-17 13:04:21 +01:00
// Anyway, load JS.
$this->loadJS();
}
2020-12-16 14:09:54 +01:00
2020-12-17 13:04:21 +01:00
/**
* Print the form for setup the external tools.
2020-12-17 13:04:21 +01:00
*
* @return void
*/
private function setupExternalToolsForm()
2020-12-17 13:04:21 +01:00
{
2020-12-17 19:33:10 +01:00
global $config;
2021-03-10 16:20:45 +01:00
$i = 0;
$sounds = $this->getSounds();
2021-03-10 16:20:45 +01:00
2020-12-17 13:04:21 +01:00
if ($this->updatePaths === true) {
2021-01-12 13:24:08 +01:00
$external_tools_config = [];
$external_tools_config['traceroute_path'] = $this->pathTraceroute;
$external_tools_config['ping_path'] = $this->pathPing;
$external_tools_config['nmap_path'] = $this->pathNmap;
$external_tools_config['dig_path'] = $this->pathDig;
$external_tools_config['snmpget_path'] = $this->pathSnmpget;
2021-03-11 09:12:53 +01:00
$otherParameters = [];
$otherParameters['sound_alert'] = (string) get_parameter('sound_alert');
$otherParameters['sound_critical'] = (string) get_parameter('sound_critical');
$otherParameters['sound_warning'] = (string) get_parameter('sound_warning');
$otherParameters['graphviz_bin_dir'] = (string) get_parameter('graphviz_bin_dir');
2021-01-12 13:24:08 +01:00
if (empty($this->pathCustomComm) === false) {
$external_tools_config['custom_commands'] = $this->pathCustomComm;
}
2020-12-16 14:09:54 +01:00
2021-03-11 09:12:53 +01:00
foreach ($otherParameters as $keyParam => $valueParam) {
$result = config_update_value($keyParam, $valueParam);
if ($result === false) {
break;
}
}
if ($result === true) {
$result = config_update_value(
'external_tools_config',
io_safe_input(
json_encode($external_tools_config)
)
2021-03-11 09:12:53 +01:00
);
}
2020-12-16 14:09:54 +01:00
ui_print_result_message(
2021-03-11 09:12:53 +01:00
($result),
__('Changes successfully saved.'),
__('Changes not saved.')
2020-12-16 14:09:54 +01:00
);
} else {
2021-01-12 13:24:08 +01:00
if (isset($config['external_tools_config']) === true) {
$external_tools_config = json_decode(
io_safe_output(
$config['external_tools_config']
),
true
);
2020-12-17 13:04:21 +01:00
// Setting paths.
2021-01-12 13:24:08 +01:00
$this->pathTraceroute = $external_tools_config['traceroute_path'];
$this->pathPing = $external_tools_config['ping_path'];
$this->pathNmap = $external_tools_config['nmap_path'];
$this->pathDig = $external_tools_config['dig_path'];
$this->pathSnmpget = $external_tools_config['snmpget_path'];
$this->pathCustomComm = ($external_tools_config['custom_commands'] ?? ['a' => 'a']);
2020-12-16 14:09:54 +01:00
}
}
2023-03-16 17:38:51 +01:00
html_print_div(['id' => 'layer_sound_alert']);
html_print_div(['id' => 'layer_sound_critical']);
html_print_div(['id' => 'layer_sound_warning']);
2020-12-17 13:04:21 +01:00
// Make the table for show the form.
2020-12-16 14:09:54 +01:00
$table = new stdClass();
$table->width = '100%';
2023-03-16 17:38:51 +01:00
$table->class = 'filter-table-adv';
2021-01-11 18:00:24 +01:00
$table->id = 'commandsTable';
2020-12-16 14:09:54 +01:00
$table->data = [];
2023-03-16 17:38:51 +01:00
$table->data[$i][] = html_print_label_input_block(
__('Sound for Alert fired'),
html_print_div(
[
'class' => '',
'content' => html_print_select(
$sounds,
'sound_alert',
$config['sound_alert'],
'replaySound(\'alert\');',
'',
'',
true
).html_print_anchor(
[
'href' => 'javascript:toggleButton(\'alert\')',
'content' => html_print_image(
'images/change-active.svg',
true,
[
2023-03-22 16:42:03 +01:00
'id' => 'button_sound_alert',
2023-03-16 17:38:51 +01:00
'style' => 'vertical-align: middle; margin-left: 10px',
'width' => '16',
'title' => __('Play sound'),
'class' => 'invert_filter',
]
),
],
true
),
],
true
),
2021-03-10 16:20:45 +01:00
);
2021-06-04 13:03:35 +02:00
2023-03-16 17:38:51 +01:00
$table->data[$i++][] = html_print_label_input_block(
__('Sound for Monitor critical'),
html_print_div(
[
'class' => '',
'content' => html_print_select(
$sounds,
'sound_critical',
$config['sound_critical'],
'replaySound(\'critical\');',
'',
'',
true
).html_print_anchor(
[
'href' => 'javascript:toggleButton(\'critical\')',
'content' => html_print_image(
'images/change-active.svg',
true,
[
2023-03-22 16:42:03 +01:00
'id' => 'button_sound_critical',
2023-03-16 17:38:51 +01:00
'style' => 'vertical-align: middle; margin-left: 10px',
'width' => '16',
'title' => __('Play sound'),
'class' => 'invert_filter',
]
),
],
true
),
],
true
),
2021-03-10 16:20:45 +01:00
);
2023-03-16 17:38:51 +01:00
$table->data[$i++][] = html_print_label_input_block(
__('Sound for Monitor warning'),
html_print_div(
[
'class' => '',
'content' => html_print_select(
$sounds,
'sound_warning',
$config['sound_warning'],
'replaySound(\'warning\');',
'',
'',
true
).html_print_anchor(
[
'href' => 'javascript:toggleButton(\'warning\')',
'content' => html_print_image(
'images/change-active.svg',
true,
[
'id' => 'button_sound_warning',
'style' => 'vertical-align: middle; margin-left: 10px',
'width' => '16',
'title' => __('Play sound'),
'class' => 'invert_filter',
]
),
],
true
),
],
true
),
2021-03-10 16:20:45 +01:00
);
2021-06-04 13:03:35 +02:00
2023-03-16 17:38:51 +01:00
$table->data[$i][] = html_print_label_input_block(
__('Custom graphviz directory'),
html_print_input_text(
'graphviz_bin_dir',
$config['graphviz_bin_dir'],
'',
25,
255,
true
)
2021-03-10 16:20:45 +01:00
);
2023-03-16 17:38:51 +01:00
$table->data[$i++][] = html_print_label_input_block(
__('Snmpget path'),
html_print_input_text('snmpget_path', $this->pathSnmpget, '', 40, 255, true)
2021-03-10 16:20:45 +01:00
);
2021-06-04 13:03:35 +02:00
2023-03-16 17:38:51 +01:00
$table->data[$i][] = html_print_label_input_block(
__('Traceroute path'),
html_print_input_text('traceroute_path', $this->pathTraceroute, '', 40, 255, true)
2021-03-10 16:20:45 +01:00
);
2023-03-16 17:38:51 +01:00
$table->data[$i++][] = html_print_label_input_block(
__('Ping path'),
html_print_input_text('ping_path', $this->pathPing, '', 40, 255, true)
2021-03-10 16:20:45 +01:00
);
2023-03-16 17:38:51 +01:00
$table->data[$i][] = html_print_label_input_block(
__('Nmap path'),
html_print_input_text('nmap_path', $this->pathNmap, '', 40, 255, true)
);
$table->data[$i++][] = html_print_label_input_block(
__('Dig path'),
html_print_input_text('dig_path', $this->pathDig, '', 40, 255, true)
);
2020-12-16 14:09:54 +01:00
2021-03-10 16:20:45 +01:00
$table->data[$i][0] = html_print_div(
[
'class' => 'title_custom_commands bolder float-left',
2023-03-16 17:38:51 +01:00
'content' => '<label>'.__('Custom commands').'</label>',
],
true
);
2021-03-10 16:20:45 +01:00
$table->data[$i++][0] .= html_print_div(
[
'id' => 'add_button_custom_command',
'content' => html_print_image(
2023-03-16 17:38:51 +01:00
'images/plus@svg.svg',
true,
2021-01-11 18:00:24 +01:00
[
'title' => __('Add new custom command'),
'onclick' => 'manageCommandLines(event)',
'id' => 'img_add_button_custom_command',
2023-03-16 17:38:51 +01:00
'class' => 'main_menu_icon invert_filter',
2021-06-04 13:03:35 +02:00
2021-01-11 18:00:24 +01:00
]
2020-12-18 13:18:03 +01:00
),
],
true
);
2023-03-16 17:38:51 +01:00
$table->data[$i][0] = '<div><label>'.__('Command').'</label></div>';
$table->data[$i++][1] = '<div style="flex-direction: row;justify-content: flex-start;"><label>'.__('Parameters').'</label>'.ui_print_help_tip(__('Adding `_address_` macro will use agent\'s IP when perform the execution'), true, '', false, 'margin-top: 2px').'</div>';
2021-03-10 16:20:45 +01:00
$y = 1;
$iRow = $i;
2021-01-11 18:00:24 +01:00
if (empty($this->pathCustomComm) === true) {
2021-03-10 16:20:45 +01:00
$table->rowid[$iRow] = 'custom_row_'.$y;
2021-01-11 18:00:24 +01:00
2021-03-10 16:20:45 +01:00
$table->data[$iRow][0] = $this->customCommandPair('command', $y);
$table->data[$iRow][1] = $this->customCommandPair('params', $y);
$table->data[$iRow][2] = $this->customCommandPair('delete', $y);
2021-01-11 18:00:24 +01:00
} else {
foreach ($this->pathCustomComm as $command) {
// Fill the fields.
2021-01-12 13:24:08 +01:00
$customCommand = ($command['command_custom'] ?? '');
$customParams = ($command['params_custom'] ?? '');
2021-01-11 18:00:24 +01:00
// Attach the fields.
2021-03-10 16:20:45 +01:00
$table->rowid[$iRow] = 'custom_row_'.$y;
$table->data[$iRow][0] = $this->customCommandPair('command', $y, $customCommand);
$table->data[$iRow][1] = $this->customCommandPair('params', $y, $customParams);
$table->data[$iRow][2] = $this->customCommandPair('delete', $y);
2021-01-12 13:24:08 +01:00
// Add another command.
2021-03-10 16:20:45 +01:00
$y++;
2021-01-12 13:24:08 +01:00
$iRow++;
2021-01-11 18:00:24 +01:00
}
}
2023-03-16 17:38:51 +01:00
$form = '<form class="max_floating_element_size" id="form_setup" method="POST" >';
2020-12-16 14:26:32 +01:00
$form .= '<fieldset>';
$form .= '<legend>'.__('Options').'</legend>';
2020-12-17 13:04:21 +01:00
$form .= html_print_input_hidden('update_paths', 1, true);
$form .= html_print_table($table, true);
2020-12-16 14:26:32 +01:00
$form .= '</fieldset>';
$form .= '</form>';
2023-03-16 17:38:51 +01:00
html_print_action_buttons(
html_print_submit_button(
__('Update'),
'update_button',
false,
[
'icon' => 'update',
'form' => 'form_setup',
],
true
)
);
2020-12-16 14:26:32 +01:00
echo $form;
2020-12-16 14:09:54 +01:00
}
2021-01-11 18:00:24 +01:00
/**
* Prints the custom command fields.
*
* @param string $type Type of field.
* @param integer $index Control index.
* @param string $value Value of this field.
*
* @return string
*/
private function customCommandPair($type, $index=0, $value='')
{
$output = '';
switch ($type) {
case 'command':
case 'params':
$output = html_print_input_text(
$type.'_custom_'.$index,
$value,
'',
40,
255,
true
);
break;
case 'delete':
$output = html_print_div(
[
'id' => 'delete_button_custom_'.$index,
'content' => html_print_image(
2023-03-16 17:38:51 +01:00
'images/delete.svg',
2021-01-11 18:00:24 +01:00
true,
[
'title' => __('Delete this custom command'),
'onclick' => 'manageCommandLines(event)',
'id' => 'img_delete_button_custom_'.$index,
2023-03-16 17:38:51 +01:00
'class' => 'main_menu_icon invert_filter',
2021-01-11 18:00:24 +01:00
]
),
],
true
);
break;
default:
// Do none.
break;
}
return $output;
}
2020-12-17 13:04:21 +01:00
/**
* Print the form for use the external tools.
2020-12-17 13:04:21 +01:00
*
* @return void
*/
private function agentExternalToolsForm()
2020-12-17 13:04:21 +01:00
{
2021-01-12 13:24:08 +01:00
global $config;
2020-12-17 13:04:21 +01:00
$principal_ip = db_get_sql(
sprintf(
'SELECT direccion FROM tagente WHERE id_agente = %d',
$this->id_agente
)
);
$list_address = db_get_all_rows_sql(
sprintf(
'SELECT id_a FROM taddress_agent WHERE id_agent = %d',
$this->id_agente
)
);
foreach ($list_address as $address) {
$ids[] = join(',', $address);
}
// Must be an a IP at least for work.
if (empty($ids) === true) {
ui_print_message(__('The agent doesn`t have an IP yet'), 'error', true);
return;
}
2020-12-17 13:04:21 +01:00
$ips = db_get_all_rows_sql(
sprintf(
'SELECT ip FROM taddress WHERE id_a IN (%s)',
join(',', $ids)
)
);
// Make the data for show in table.
$ipsSelect = array_reduce(
$ips,
function ($carry, $item) {
$carry[$item['ip']] = $item['ip'];
return $carry;
}
);
2021-01-12 13:24:08 +01:00
// Get the list of available commands.
$commandList = [
COMMAND_TRACEROUTE => __('Traceroute'),
COMMAND_PING => __('Ping host & Latency'),
COMMAND_SNMP => __('SNMP Interface status'),
COMMAND_NMAP => __('Basic TCP Port Scan'),
COMMAND_DIGWHOIS => __('DiG/Whois Lookup'),
];
// Adding custom commands.
$tempCustomCommandsList = json_decode(io_safe_output($config['external_tools_config']), true);
$customCommandsList = $tempCustomCommandsList['custom_commands'];
foreach ($customCommandsList as $customCommandKey => $customCommandValue) {
$commandList[$customCommandKey] = $customCommandKey;
}
2020-12-17 13:04:21 +01:00
// Form table.
$table = new StdClass();
2023-03-15 15:58:28 +01:00
$table->class = 'fixed_filter_bar filter-table-adv pdd_15px';
$table->id = 'externalToolTable';
2023-03-15 15:58:28 +01:00
$table->size[0] = '25%';
$table->size[1] = '25%';
$table->size[2] = '25%';
$table->size[3] = '25%';
$table->colspan = [];
$table->colspan[1][0] = 4;
// $table->cellclass[0][2] = 'snmpcolumn';
// $table->cellclass[0][2] = 'snmpcolumn';
// $table->cellclass[0][3] = 'snmpcolumn';
// $table->cellclass[0][3] = 'snmpcolumn';
2020-12-17 13:04:21 +01:00
$table->data = [];
2023-03-15 15:58:28 +01:00
$table->data[0][0] = html_print_label_input_block(
__('Operation'),
html_print_select(
$commandList,
'operation',
$this->operation,
'mostrarColumns(this.value)',
__('Please select'),
0,
true,
false,
true,
'w100p',
false,
'width: 100%;'
)
2020-12-17 13:04:21 +01:00
);
2023-03-15 15:58:28 +01:00
$table->data[0][1] = html_print_label_input_block(
__('IP Adress'),
html_print_select(
$ipsSelect,
'select_ips',
$principal_ip,
'',
'',
0,
true,
false,
true,
'w100p',
false,
'width: 100%;'
)
2020-12-17 13:04:21 +01:00
);
2023-03-15 15:58:28 +01:00
$table->data[0][2] = html_print_label_input_block(
__('SNMP Version'),
html_print_select(
[
'1' => 'v1',
'2c' => 'v2c',
],
'select_version',
$this->snmp_version,
'',
'',
0,
true,
false,
true,
'w100p',
false,
'width: 100%;'
),
['div_class' => 'snmpcolumn']
2020-12-17 13:04:21 +01:00
);
2023-03-15 15:58:28 +01:00
$table->data[0][3] = html_print_label_input_block(
__('SNMP Community'),
html_print_input_text(
'community',
$this->community,
'',
50,
255,
true,
false,
false,
'',
'w100p'
),
['div_class' => 'snmpcolumn']
2020-12-17 13:04:21 +01:00
);
2023-03-15 15:58:28 +01:00
$table->data[1][0] = html_print_submit_button(
__('Execute'),
'submit',
false,
2022-10-25 13:13:52 +02:00
[
2023-03-15 15:58:28 +01:00
'icon' => 'cog',
'mode' => 'mini',
'class' => 'float-right mrgn_right_10px',
2022-10-25 13:13:52 +02:00
],
true
);
2020-12-17 13:04:21 +01:00
// Output string.
$output = '';
2020-12-17 16:51:05 +01:00
$output .= '<form name="actionbox" method="post">';
2020-12-17 13:04:21 +01:00
$output .= html_print_table($table, true);
$output .= '</form>';
html_print_div(
[
'class' => '',
'style' => 'width: 100%',
'content' => $output,
]
);
2020-12-17 16:51:05 +01:00
if ($this->operation !== 0) {
2020-12-17 13:04:21 +01:00
// Execute form.
echo $this->externalToolsExecution($this->operation, $this->ip, $this->community, $this->snmp_version);
2020-12-17 13:04:21 +01:00
}
}
/**
* Searchs for command.
*
* @param string $command Command.
*
* @return string Path.
*/
2020-12-17 16:51:05 +01:00
private function whereIsTheCommand(string $command)
2020-12-17 13:04:21 +01:00
{
global $config;
2021-01-12 13:24:08 +01:00
if (isset($config['external_tools_config']) === true) {
$external_tools_config = json_decode(io_safe_output($config['external_tools_config']), true);
$traceroute_path = $external_tools_config['traceroute_path'];
$ping_path = $external_tools_config['ping_path'];
$nmap_path = $external_tools_config['nmap_path'];
$dig_path = $external_tools_config['dig_path'];
$snmpget_path = $external_tools_config['snmpget_path'];
2020-12-17 13:04:21 +01:00
switch ($command) {
case 'traceroute':
2020-12-17 16:51:05 +01:00
if (empty($traceroute_path) === false) {
2020-12-17 13:04:21 +01:00
return $traceroute_path;
}
break;
case 'ping':
2020-12-17 16:51:05 +01:00
if (empty($ping_path) === false) {
2020-12-17 13:04:21 +01:00
return $ping_path;
}
break;
case 'nmap':
2020-12-17 16:51:05 +01:00
if (empty($nmap_path) === false) {
2020-12-17 13:04:21 +01:00
return $nmap_path;
}
break;
case 'dig':
2020-12-17 16:51:05 +01:00
if (empty($dig_path) === false) {
2020-12-17 13:04:21 +01:00
return $dig_path;
}
break;
case 'snmpget':
2020-12-17 16:51:05 +01:00
if (empty($snmpget_path) === false) {
2020-12-17 13:04:21 +01:00
return $snmpget_path;
}
break;
2022-09-06 10:10:41 +02:00
case 'whois':
if (empty($snmpget_path) === false) {
return $snmpget_path;
}
break;
2020-12-17 13:04:21 +01:00
default:
return null;
}
}
ob_start();
system('whereis '.$command);
$output = ob_get_clean();
$result = explode(':', $output);
$result = trim($result[1]);
2020-12-17 19:33:10 +01:00
if (empty($result) === true) {
2020-12-17 13:04:21 +01:00
return null;
}
$result = explode(' ', $result);
$fullpath = trim($result[0]);
2020-12-17 19:33:10 +01:00
if (file_exists($fullpath) === false) {
2020-12-17 13:04:21 +01:00
return null;
}
return $fullpath;
}
2020-12-17 16:51:05 +01:00
/**
* Create the output for show.
*
* @param string $command Command for execute.
* @param string $caption Description of the execution.
*
* @return void
*/
private function performExecution(string $command='', string $caption='')
{
2023-03-15 15:58:28 +01:00
$output = '<div class="white_box max_floating_element_size no_border">';
2020-12-17 16:51:05 +01:00
try {
// If caption is not added, don't show anything.
if (empty($caption) === false) {
$output .= sprintf('<h3 class="external_tools_title">%s</h3>', $caption);
2020-12-17 16:51:05 +01:00
}
2022-11-21 14:49:07 +01:00
$output .= '<pre class="external_tools_output">';
2020-12-17 16:51:05 +01:00
// Only perform an execution if command is passed. Avoid errors.
if (empty($command) === false) {
$resultCode = 0;
2020-12-17 16:51:05 +01:00
ob_start();
system(io_safe_output($command), $resultCode);
2020-12-17 16:51:05 +01:00
$output .= ob_get_clean();
} else {
$output .= __('Command not response');
2020-12-17 16:51:05 +01:00
}
2023-03-15 15:58:28 +01:00
$output .= '</pre></div>';
if ($resultCode !== 0) {
throw new Exception(
sprintf(
'%s %s',
__('The command failed and obtained the return code:'),
$resultCode
),
1
);
}
2020-12-17 16:51:05 +01:00
} catch (\Throwable $th) {
$output = sprintf(
'%s %s',
$th->getMessage(),
__('Something went wrong while perform the execution. Please check the configuration.')
);
2020-12-17 16:51:05 +01:00
}
echo $output;
}
2020-12-17 13:04:21 +01:00
/**
* Execute external tools action.
2020-12-17 13:04:21 +01:00
*
2021-01-12 13:24:08 +01:00
* @param mixed $operation Operation.
* @param string $ip Ip.
* @param string $community Community.
* @param string $snmp_version SNMP version.
2020-12-17 13:04:21 +01:00
*
* @return string String formed result of execution.
*/
2021-01-12 13:24:08 +01:00
public function externalToolsExecution($operation, string $ip, string $community, string $snmp_version)
2020-12-17 13:04:21 +01:00
{
$output = '';
2023-03-15 15:58:28 +01:00
echo '<div class="white_box max_floating_element_size no_border pdd_15px">';
2020-12-17 16:51:05 +01:00
if (validate_address($ip) === false) {
2020-12-17 13:04:21 +01:00
$output .= ui_print_error_message(
__('The ip or dns name entered cannot be resolved'),
'',
true
);
} else {
2021-07-02 13:29:00 +02:00
if ((int) $operation === COMMAND_SNMP) {
2020-12-17 16:51:05 +01:00
$snmp_obj = [
'ip_target' => $ip,
'snmp_version' => $snmp_version,
'snmp_community' => $community,
'format' => '-Oqn',
];
echo '<h3 class="external_tools_title">'.__('SNMP information for ').$ip.'</h3>';
2020-12-17 16:51:05 +01:00
$snmp_obj['base_oid'] = '.1.3.6.1.2.1.1.3.0';
$result = get_h_snmpwalk($snmp_obj);
if (empty($result) === true) {
ui_print_error_message(__('Target unreachable.'));
return null;
} else {
2020-12-17 13:04:21 +01:00
echo '<h4>'.__('Uptime').'</h4>';
echo '<pre>';
2020-12-17 16:51:05 +01:00
echo array_pop($result);
2020-12-17 13:04:21 +01:00
echo '</pre>';
2020-12-17 16:51:05 +01:00
}
$snmp_obj['base_oid'] = '.1.3.6.1.2.1.1.1.0';
$result = get_h_snmpwalk($snmp_obj);
if (empty($result) === true) {
ui_print_error_message(__('Target unreachable.'));
return null;
} else {
2020-12-17 13:04:21 +01:00
echo '<h4>'.__('Device info').'</h4>';
echo '<pre>';
2020-12-17 16:51:05 +01:00
echo array_pop($result);
2020-12-17 13:04:21 +01:00
echo '</pre>';
2020-12-17 16:51:05 +01:00
}
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
echo '<h4>Interface Information</h4>';
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$table = new StdClass();
$table->class = 'databox';
$table->head = [];
$table->head[] = __('Interface');
$table->head[] = __('Status');
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$i = 0;
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$base_oid = '.1.3.6.1.2.1.2.2.1';
$idx_oids = '.1';
$names_oids = '.2';
$status_oids = '.8';
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$snmp_obj['base_oid'] = $base_oid.$idx_oids;
$idx = get_h_snmpwalk($snmp_obj);
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$snmp_obj['base_oid'] = $base_oid.$names_oids;
$names = get_h_snmpwalk($snmp_obj);
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$snmp_obj['base_oid'] = $base_oid.$status_oids;
$statuses = get_h_snmpwalk($snmp_obj);
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
foreach ($idx as $k => $v) {
$index = str_replace($base_oid.$idx_oids, '', $k);
$name = $names[$base_oid.$names_oids.$index];
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$status = $statuses[$base_oid.$status_oids.$index];
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
$table->data[$i][0] = $name;
$table->data[$i++][1] = $status;
}
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
html_print_table($table);
2021-07-02 13:29:00 +02:00
} else if ((int) $operation === COMMAND_DIGWHOIS) {
echo '<h3 class="external_tools_title">'.__('Domain and IP information for ').$ip.'</h3>';
2020-12-17 13:04:21 +01:00
2020-12-17 16:51:05 +01:00
// Dig execution.
$dig = $this->whereIsTheCommand('dig');
if (empty($dig) === true) {
ui_print_error_message(__('Dig executable does not exist.'));
} else {
2022-09-06 10:10:41 +02:00
$dig .= ' '.$ip;
2020-12-17 16:51:05 +01:00
$this->performExecution($dig);
}
// Whois execution.
$whois = $this->whereIsTheCommand('whois');
if (empty($whois) === true) {
ui_print_error_message(__('Whois executable does not exist.'));
} else {
2022-09-06 10:10:41 +02:00
$whois .= ' '.$ip;
2020-12-17 16:51:05 +01:00
$this->performExecution($whois);
}
return;
} else {
switch ($operation) {
case COMMAND_TRACEROUTE:
$command = $this->whereIsTheCommand('traceroute');
if (empty($command) === true) {
ui_print_error_message(__('Traceroute executable does not exist.'));
return;
} else {
$stringCommand = __('Traceroute to %s', $ip);
$executeCommand = sprintf('%s %s', $command, $ip);
}
break;
case COMMAND_PING:
$command = $this->whereIsTheCommand('ping');
if (empty($command) === true) {
ui_print_error_message(__('Ping executable does not exist.'));
return;
} else {
$stringCommand = __('Ping to %s', $ip);
$executeCommand = sprintf('%s -c 5 %s', $command, $ip);
}
break;
case COMMAND_NMAP:
$command = $this->whereIsTheCommand('nmap');
if (empty($command) === true) {
ui_print_error_message(__('Nmap executable does not exist.'));
return;
} else {
$stringCommand = __('Basic TCP Scan on %s', $ip);
$executeCommand = sprintf('%s -F %s', $command, $ip);
}
break;
default:
2021-01-12 13:24:08 +01:00
global $config;
$tempCustomCommandsList = json_decode(io_safe_output($config['external_tools_config']), true);
$customCommandsList = $tempCustomCommandsList['custom_commands'];
// If the selected operation exists or not.
if (isset($customCommandsList[$operation]) === true) {
// Setting custom commands.
$customCommand = $customCommandsList[$operation]['command_custom'];
$customParams = $customCommandsList[$operation]['params_custom'];
// If '_address_' macro is setted, attach to execution.
if ((bool) preg_match('/_address_/', $customParams) === true) {
$customParams = preg_replace('/_address_/', $ip, $customParams);
$stringCommand = __('Performing %s execution on %s', $customCommand, $ip);
} else {
$stringCommand = __('Performing %s execution', $customCommand);
}
$executeCommand = sprintf('%s %s', $customCommand, $customParams);
} else {
// Nothing to do.
$stringCommand = '';
$executeCommand = '';
}
2020-12-17 16:51:05 +01:00
break;
}
$this->performExecution($executeCommand, $stringCommand);
2020-12-17 13:04:21 +01:00
}
}
2023-03-15 15:58:28 +01:00
echo '</div>';
2020-12-17 13:04:21 +01:00
return $output;
}
2021-03-10 16:20:45 +01:00
/**
* Return sounds path.
*
* @return string Path.
*/
private function getSounds()
2021-03-10 16:20:45 +01:00
{
global $config;
$return = [];
$files = scandir($config['homedir'].'/include/sounds');
foreach ($files as $file) {
if (strstr($file, 'wav') !== false) {
$return['include/sounds/'.$file] = $file;
}
}
return $return;
}
2020-12-17 13:04:21 +01:00
/**
* Load the JS and attach
*
* @return string Formed script string.
*/
private function loadJS()
{
$str = '';
ob_start();
?>
<script type='text/javascript'>
$(document).ready(function(){
let custom_command = $('#add_button_custom_command');
2020-12-17 13:04:21 +01:00
mostrarColumns($('#operation :selected').val());
});
2020-12-18 13:18:03 +01:00
// Manage network component oid field generation.
2021-01-11 18:00:24 +01:00
function manageCommandLines(event) {
let buttonId = event.target.id;
let parentId = event.target.parentElement.id;
let action = parentId.split('_');
action = action[0];
if (action === 'add') {
let fieldLines = $("tr[id*=custom_row_]").length;
let fieldLinesAdded = fieldLines + 1;
// Ensure the first erase button is clickable
$("#img_delete_button_custom_1")
.attr("style", "opacity: 1;")
.addClass("clickable");
$("#custom_row_" + fieldLines).after(
$("#custom_row_" + fieldLines)
.clone()
.attr("id", 'custom_row_'+fieldLinesAdded)
2020-12-18 13:18:03 +01:00
);
2021-01-11 18:00:24 +01:00
let rowCommand = Array.from($("#custom_row_"+fieldLinesAdded).children());
rowCommand.forEach(function(value, index){
let thisId = $(value).attr("id");
let separatedId = thisId.split("-");
let fieldLinesAddedForNewId = parseInt(separatedId[1]) + 1;
let thisNewId = separatedId[0] + "-" + fieldLinesAddedForNewId + "-" + separatedId[2];
// Assignation of new Id for this cell
$(value).attr("id", thisNewId);
// Children text fields.
if (parseInt(separatedId[2]) === 0) {
$("#text-command_custom_"+fieldLines, "#"+thisNewId)
.attr("name", "command_custom_"+fieldLinesAdded)
.attr("id", "text-command_custom_"+fieldLinesAdded)
.val('');
2021-01-11 18:00:24 +01:00
} else if (parseInt(separatedId[2]) === 1) {
$("#text-params_custom_"+fieldLines, "#"+thisNewId)
.attr("id", "text-params_custom_"+fieldLinesAdded)
.attr("name", "params_custom_"+fieldLinesAdded)
.val('');
2021-01-11 18:00:24 +01:00
} else if (parseInt(separatedId[2]) === 2) {
$("#img_delete_button_custom_"+fieldLines, "#"+thisNewId)
.attr("id", "img_delete_button_custom_"+fieldLinesAdded);
}
});
} else if (action === 'delete') {
let buttonIdDivided = buttonId.split("_");
let lineNumber = buttonIdDivided[buttonIdDivided.length-1];
let lineCount = parseInt($("tr[id*=custom_row_]").length);
if (parseInt(lineNumber) >= 1 && lineCount > 1) {
$("#custom_row_" + lineNumber).remove();
2021-06-14 08:16:35 +02:00
} else if (lineCount === 1) {
$("#custom_row_" + lineNumber).find('input').each(function() {
$(this).val('');
});
2020-12-18 13:18:03 +01:00
}
2021-01-11 18:00:24 +01:00
if (lineCount === 1) {
$("[id*=img_delete_button_custom_]")
.attr("style", "opacity: 0.5;")
.removeClass("clickable");
2020-12-18 13:18:03 +01:00
}
}
}
2020-12-17 13:04:21 +01:00
function mostrarColumns(value) {
2021-01-12 13:24:08 +01:00
if (parseInt(value) === 3) {
2020-12-17 13:04:21 +01:00
$('.snmpcolumn').show();
}
else {
$('.snmpcolumn').hide();
}
}
2021-03-10 16:20:45 +01:00
function toggleButton(type) {
if ($("#button_sound_" + type).attr('src') == 'images/control_pause_col.png') {
$("#button_sound_" + type).attr('src', 'images/control_play_col.png');
$('#layer_sound_' + type).html("");
}
else {
$("#button_sound_" + type).attr('src', 'images/control_pause_col.png');
$('#layer_sound_' + type).html("<audio src='" + $("#sound_" + type).val() + "' autoplay='true' hidden='true' loop='true'>");
}
}
function replaySound(type) {
if ($("#button_sound_" + type).attr('src') == 'images/control_pause_col.png') {
$('#layer_sound_' + type).html("");
$('#layer_sound_' + type).html("<audio src='" + $("#sound_" + type).val() + "' autoplay='true' hidden='true' loop='true'>");
}
}
2020-12-17 13:04:21 +01:00
</script>
<?php
// Get the JS script.
$str = ob_get_clean();
// Return the loaded JS.
echo $str;
return $str;
}
2020-12-16 14:09:54 +01:00
}