From 12b8042c5317d61f826e2f4695beff1b4106a95c Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Fri, 22 Feb 2019 14:26:58 +0100 Subject: [PATCH 1/3] Added server plugins in meta Former-commit-id: 7f69f55c3fad06c7b15408d755d088d5ac89b111 --- pandora_console/godmode/servers/plugin.php | 294 ++++++++++++++++++--- 1 file changed, 257 insertions(+), 37 deletions(-) diff --git a/pandora_console/godmode/servers/plugin.php b/pandora_console/godmode/servers/plugin.php index ff370a5c48..fcb34363f0 100644 --- a/pandora_console/godmode/servers/plugin.php +++ b/pandora_console/godmode/servers/plugin.php @@ -112,6 +112,7 @@ $edit_file = get_parameter('edit_file', false); $update_file = get_parameter('update_file', false); $plugin_command = get_parameter('plugin_command', ''); $tab = get_parameter('tab', ''); +$deploy_plugin = get_parameter('deploy_plugin', 0); if ($view != '') { $form_id = $view; @@ -192,9 +193,8 @@ if ($filemanager) { // If is win compatible and the compatibility must be unix if ($is_win_compatible !== false && $compatibility == 'unix') { $contentFile = str_replace("\r\n", "\n", $contentFile); - } - // If is unix compatible and the compatibility must be win - else if ($is_win_compatible === false && $compatibility == 'windows') { + } else if ($is_win_compatible === false && $compatibility == 'windows') { + // If is unix compatible and the compatibility must be win $contentFile = str_replace("\n", "\r\n", $contentFile); } @@ -268,12 +268,16 @@ if ($filemanager) { // ===================================================================== $sec = 'gservers'; -if (($create != '') or ($view != '')) { +if (($create != '') || ($view != '')) { enterprise_hook('open_meta_frame'); if (defined('METACONSOLE')) { components_meta_print_header(); $sec = 'advanced'; + $management_allowed = is_management_allowed(); + if (!$management_allowed) { + ui_print_warning_message(__('To manage plugin you must activate centralized management')); + } } else { if ($create != '') { ui_print_page_header( @@ -292,6 +296,16 @@ if (($create != '') or ($view != '')) { true ); } + + $management_allowed = !is_central_policies_on_node(); + if (!$management_allowed) { + ui_print_warning_message( + __( + 'This console is not manager of this environment, + please manage this feature from centralized manager console (Metaconsole).' + ) + ); + } } @@ -564,9 +578,53 @@ if (($create != '') or ($view != '')) { if (defined('METACONSOLE')) { components_meta_print_header(); $sec = 'advanced'; + $management_allowed = is_management_allowed(); + if (!$management_allowed) { + ui_print_warning_message(__('To manage plugin you must activate centralized management')); + } + + if (!$config['metaconsole_deploy_plugin_server'] && $management_allowed) { + $deploy_plugin_server = true; + + echo ''; + ?> + + '; @@ -709,12 +767,15 @@ if (($create != '') or ($view != '')) { $result = db_process_sql_delete('tplugin', ['id' => $plugin_id]); - if (! $result) { - ui_print_error_message(__('Problem deleting plugin')); - } else { - ui_print_success_message(__('Plugin deleted successfully')); + if (!is_metaconsole()) { + if (!$result) { + ui_print_error_message(__('Problem deleting plugin')); + } else { + ui_print_success_message(__('Plugin deleted successfully')); + } } + if ($plugin_id != 0) { // Delete all the modules with this plugin $plugin_modules = db_get_all_rows_filter( @@ -737,6 +798,153 @@ if (($create != '') or ($view != '')) { policies_change_delete_pending_module($policies_id['id']); } } + + if (is_metaconsole()) { + enterprise_include_once('include/functions_plugins.php'); + $result = plugins_delete_plugin($plugin_id); + if (!$result) { + ui_print_error_message(__('Problem deleting plugin')); + } else { + ui_print_success_message(__('Plugin deleted successfully')); + } + } + } + } + + if ($deploy_plugin) { + if (is_metaconsole()) { + enterprise_include_once('include/functions_plugins.php'); + $result = plugins_deploy_plugin($deploy_plugin); + if (!$result) { + ui_print_error_message(__('Problem deploying plugin')); + } else { + ui_print_success_message(__('Plugin deployed successfully')); + } + } + } + + if ($deploy_plugin_server) { + $setup = db_get_all_rows_in_table('tmetaconsole_setup'); + // recorremos todos los nodos. + foreach ($setup as $key => $value) { + // Obtenemos los plugins de la meta. + $all_plugin_meta = db_get_all_rows_in_table('tplugin'); + // Conectamos con el nodo. + if (metaconsole_connect($value) == NOERR) { + $values = []; + // Obtenemos los plugin del nodo. + $node_plugin_server = db_get_all_rows_in_table('tplugin'); + foreach ($node_plugin_server as $key2 => $plugin) { + // Comprobamos si el id esta meta y nodo al mismo tiempo. + $key_exists = array_search($plugin['id'], array_column($all_plugin_meta, 'id')); + if ($key_exists !== false) { + // Si el plugin tiene el mismo id pero diferentes datos. + if ($all_plugin_meta[$key_exists] != $plugin) { + $old_id = $plugin['id']; + $new_id = ($plugin['id'] + (1000 * $value['id'])); + + // El plugin del nodo pasa a tener otro id y otro nombre. + $plugin['id'] = $new_id; + $plugin['name'] = $plugin['name'].'_'.$value['server_name']; + $result_update = db_process_sql_update( + 'tplugin', + [ + 'id' => $new_id, + 'name' => $plugin['name'], + ], + ['id' => $old_id] + ); + + if ($result_update) { + db_process_sql_update( + 'tagente_modulo', + ['id_plugin' => $new_id], + ['id_plugin' => $old_id] + ); + + db_process_sql_update( + 'tnetwork_component', + ['id_plugin' => $new_id], + ['id_plugin' => $old_id] + ); + + db_process_sql_update( + 'tpolicy_modules', + ['id_plugin' => $new_id], + ['id_plugin' => $old_id] + ); + } + + // New plugins to insert in the metaconsole. + $values[$plugin['id']] = $plugin; + } + } else { + // Exists in the node, but does not exist in the metaconsole. + $values[$plugin['id']] = $plugin; + } + } + + // Restore to metaconsole. + metaconsole_restore_db(); + + // Insert in metaconsole. + if (!empty($values)) { + foreach ($values as $key2 => $val) { + // Insert into metaconsole. + $result_insert = db_process_sql_insert('tplugin', $val); + } + } + } + } + + $all_plugin_meta = db_get_all_rows_in_table('tplugin'); + + foreach ($setup as $key => $value) { + if (metaconsole_connect($value) == NOERR) { + $all_plugin_node = db_get_all_rows_in_table('tplugin'); + + $array_diff = array_diff(array_column($all_plugin_meta, 'id'), array_column($all_plugin_node, 'id')); + foreach ($array_diff as $key2 => $pluginid) { + $other = []; + $plugin_meta = $all_plugin_meta[$key2]; + + unset($plugin_meta['id']); + $other['name'] = urlencode($plugin_meta['name']); + $other['description'] = urlencode($plugin_meta['description']); + $other['max_timeout'] = $plugin_meta['max_timeout']; + $other['max_retries'] = $plugin_meta['max_retries']; + $other['execute'] = urlencode($plugin_meta['execute']); + $other['net_dst_opt'] = $plugin_meta['net_dst_opt']; + $other['net_port_opt'] = $plugin_meta['net_port_opt']; + $other['user_opt'] = $plugin_meta['user_opt']; + $other['pass_opt'] = $plugin_meta['pass_opt']; + $other['plugin_type'] = $plugin_meta['plugin_type']; + $other['macros'] = urlencode($plugin_meta['macros']); + $other['parameters'] = urlencode($plugin_meta['parameters']); + $other = implode('%7C', $other); + + $auth_token = json_decode($value['auth_token']); + $url = $value['server_url'].'include/api.php?op=set&op2=push_plugin'.'&id='.$pluginid.'&other_mode=url_encode_separator_%7C&other='.$other."&apipass=$auth_token->api_password"."&user=$auth_token->console_user&pass=$auth_token->console_password"; + $file_path = realpath($plugin_meta['execute']); + $post = ''; + if (file_exists($file_path)) { + $post = ['file' => curl_file_create($file_path)]; + } + + $curlObj = curl_init(); + curl_setopt($curlObj, CURLOPT_URL, $url); + curl_setopt($curlObj, CURLOPT_POST, 1); + curl_setopt($curlObj, CURLOPT_POSTFIELDS, $post); + curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); + + $api_result = curl_exec($curlObj); + curl_close($curlObj); + } + } + + // restore to metaconsole + metaconsole_restore_db(); } } @@ -753,7 +961,10 @@ if (($create != '') or ($view != '')) { echo ''.__('Name').''; echo ''.__('Type').''; echo ''.__('Command').''; - echo "".''.__('Op.').''.''; + if ($management_allowed) { + echo "".''.__('Op.').''.''; + } + $color = 0; foreach ($rows as $row) { @@ -780,31 +991,38 @@ if (($create != '') or ($view != '')) { echo ""; echo $row['execute']; echo ''; - echo ""; + if ($management_allowed) { + echo ""; - // Show it is locket - $modules_using_plugin = db_get_value_filter( - 'count(*)', - 'tagente_modulo', - [ - 'delete_pending' => 0, - 'id_plugin' => $row['id'], - ] - ); - $components_using_plugin = db_get_value_filter( - 'count(*)', - 'tnetwork_component', - ['id_plugin' => $row['id']] - ); - if (($components_using_plugin + $modules_using_plugin) > 0) { - echo ''; - html_print_image('images/lock.png'); - echo ''; + // Show it is locket + $modules_using_plugin = db_get_value_filter( + 'count(*)', + 'tagente_modulo', + [ + 'delete_pending' => 0, + 'id_plugin' => $row['id'], + ] + ); + $components_using_plugin = db_get_value_filter( + 'count(*)', + 'tnetwork_component', + ['id_plugin' => $row['id']] + ); + if (($components_using_plugin + $modules_using_plugin) > 0) { + echo ''; + html_print_image('images/lock.png'); + echo ''; + } + + echo "".html_print_image('images/config.png', true, ['title' => __('Edit')]).'  '; + echo "".html_print_image('images/cross.png', true, ['border' => '0']).''; + if (is_metaconsole()) { + echo "   ".html_print_image('images/deploy.png', true, ['title' => __('Deploy'), 'width' => '21 px']).'  '; + } + + echo ''; } - echo "".html_print_image('images/config.png', true, ['title' => __('Edit')]).'  '; - echo "".html_print_image('images/cross.png', true, ['border' => '0']).''; - echo ''; echo ''; } @@ -813,12 +1031,15 @@ if (($create != '') or ($view != '')) { ui_print_info_message(['no_close' => true, 'message' => __('There are no plugins in the system') ]); } - echo ""; + if ($management_allowed) { + echo "
"; - echo '
'; - echo "
"; - echo ""; - echo '
'; + echo ''; + echo ""; + echo ""; + echo ''; + echo '