From 57f6ad097a6c6d5b17571706932e849022c4ef66 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Tue, 11 Jul 2017 10:10:48 +0200 Subject: [PATCH 1/3] update --- pandora_console/include/functions_html.php | 17 ++++++++++++++++- pandora_console/pandoradb.sql | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index d868f68f48..797660534c 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -385,13 +385,28 @@ function html_print_select_groups($id_user = false, $privilege = "AR", $nothing = '', $nothing_value = 0, $return = false, $multiple = false, $sort = true, $class = '', $disabled = false, $style = false, $option_style = false, $id_group = false, - $keys_field = 'id_grupo', $strict_user = false) { + $keys_field = 'id_grupo', $strict_user = false, $delete_groups = false , $include_groups = false) { global $config; $fields = users_get_groups_for_select($id_user, $privilege, $returnAllGroup, true, $id_group, $keys_field); + + if ($delete_groups && is_array($delete_groups)){ + foreach ($delete_groups as $value) { + unset($fields[$value]); + } + } + + if (is_array($include_groups)){ + $field = array(); + foreach ($include_groups as $value) { + $field[$value] = $fields[$value]; + } + $fields = $field; + } + if ($strict_user) { $fields = users_get_strict_mode_groups($config['id_user'], $returnAllGroup); } diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 1ef3c1f7e8..471b64e048 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -2167,6 +2167,20 @@ CREATE TABLE IF NOT EXISTS `tpolicy_agents` ( UNIQUE (`id_policy`, `id_agent`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-- ----------------------------------------------------- +-- Table `tpolicy_groups` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `tpolicy_groups` ( + `id` int(10) unsigned NOT NULL auto_increment, + `id_policy` int(10) unsigned default '0', + `id_group` int(10) unsigned default '0', + `policy_applied` tinyint(1) unsigned default '0', + `pending_delete` tinyint(1) unsigned default '0', + `last_apply_utimestamp` int(10) unsigned NOT NULL default 0, + PRIMARY KEY (`id`), + UNIQUE (`id_policy`, `id_group`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- --------------------------------------------------------------------- -- Table `tdashboard` -- --------------------------------------------------------------------- @@ -2468,6 +2482,7 @@ CREATE TABLE IF NOT EXISTS `tpolicy_queue` ( `id` int(10) unsigned NOT NULL auto_increment, `id_policy` int(10) unsigned NOT NULL default '0', `id_agent` int(10) unsigned NOT NULL default '0', + `id_group` int(10) unsigned NOT NULL default '0', `operation` varchar(15) default '', `progress` int(10) unsigned NOT NULL default '0', `end_utimestamp` int(10) unsigned NOT NULL default 0, From c13644951f4e14b74b0ae50e1da9dc382052589d Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Mon, 17 Jul 2017 09:22:28 +0200 Subject: [PATCH 2/3] update agent --- .../godmode/agentes/configurar_agente.php | 49 +++++++++++++++++ .../godmode/massive/massive_edit_agents.php | 52 +++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 4484e6a1ba..5e965d2348 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -239,6 +239,14 @@ if ($create_agent) { $agent_created_ok = true; + $tpolicy_group = db_get_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$grupo); + + if($tpolicy_group){ + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $tpolicy_group, 'id_agent' => $id_agente)); + } + $info = 'Name: ' . $nombre_agente . ' IP: ' . $direccion_agente . ' Group: ' . $grupo . @@ -787,6 +795,47 @@ if ($update_agent) { // if modified some agent paramenter if ($old_interval != $intervalo) { enterprise_hook('config_agents_update_config_interval', array($id_agente, $intervalo)); } + + if($grupo){ + $group_old = db_get_sql("SELECT id_grupo FROM tagente WHERE id_agente =" .$id_agente); + + $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$group_old); + + if($tpolicy_group_old){ + foreach ($tpolicy_group_old as $key => $value) { + $tpolicy_agents_old= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$value['id_policy'] . " AND id_agent =" .$id_agente); + + if($tpolicy_agents_old){ + $result2 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 1), + array ('id_agent' => $id_agente, 'id_policy' => $value['id_policy'])); + } + } + } + } + + //pruebassssss + $tpolicy_group = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$grupo); + + if($tpolicy_group){ + foreach ($tpolicy_group as $key => $value) { + $tpolicy_agents= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$value['id_policy'] . " AND id_agent =" .$id_agente); + + if(!$tpolicy_agents){ + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $value['id_policy'], 'id_agent' => $id_agente)); + } else { + $result3 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 0), + array ('id_agent' => $id_agente, 'id_policy' => $value['id_policy'])); + } + } + + } $info = 'Group: ' . $grupo . ' Interval: ' . $intervalo . ' Comments: ' . $comentarios . ' Mode: ' . $modo . diff --git a/pandora_console/godmode/massive/massive_edit_agents.php b/pandora_console/godmode/massive/massive_edit_agents.php index facdf18b54..647b1f3226 100755 --- a/pandora_console/godmode/massive/massive_edit_agents.php +++ b/pandora_console/godmode/massive/massive_edit_agents.php @@ -139,9 +139,61 @@ if ($update_agents) { isset($values['id_grupo'])) { $values['update_module_count'] = 1; // Force an update of the agent cache. } + $group_old = false; + if($values['id_grupo']){ + $group_old = db_get_sql("SELECT id_grupo FROM tagente WHERE id_agente =" .$id_agent); + } + $result = db_process_sql_update ('tagente', $values, array ('id_agente' => $id_agent)); + + if($group_old || $result){ + $tpolicy_group_old = db_get_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$group_old); + + if($tpolicy_group_old){ + $tpolicy_agents_old= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$tpolicy_group_old . " AND id_agent =" .$id_agent); + + if($tpolicy_agents_old){ + $result2 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 1), + array ('id_agent' => $id_agent, 'id_policy' => $tpolicy_group_old)); + } + + } + + $tpolicy_group_new = db_get_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$values['id_grupo']); + + if($tpolicy_group_new){ + $tpolicy_agents_new= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$tpolicy_group_new . " AND id_agent =" .$id_agent); + + if($tpolicy_agents_new){ + $result3 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 0), + array ('id_agent' => $id_agent, 'id_policy' => $tpolicy_group_new)); + } else { + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $tpolicy_group_new, 'id_agent' => $id_agent)); + } + } + + } + + // if($values['id_grupo'] || $result){ + // $tpolicy_agents= db_get_sql("SELECT * FROM tpolicy_agents + // WHERE id_policy = ".$tpolicy_group . " AND id_agent =" .$id_agente); + // + // $tpolicy_group = db_get_sql("SELECT id_policy FROM tpolicy_groups + // WHERE id_group = ".$values['id_grupo']); + // if ($tpolicy_group){ + // + // } + // } + } // Update Custom Fields From f0e43e252c171164c31ecfd41b1a2d471d219e2b Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Tue, 18 Jul 2017 12:06:57 +0200 Subject: [PATCH 3/3] New feature: Add groups to policies --- pandora_console/extras/mr/4.sql | 17 ++++++ .../pandoradb_migrate_6.0_to_7.0.mysql.sql | 14 +++++ .../godmode/agentes/configurar_agente.php | 45 ++++++++-------- .../godmode/massive/massive_edit_agents.php | 52 ++++++++---------- pandora_console/include/functions_api.php | 54 ++++++++++++++++++- pandora_console/include/functions_html.php | 2 +- pandora_console/pandoradb.sql | 1 - 7 files changed, 126 insertions(+), 59 deletions(-) create mode 100644 pandora_console/extras/mr/4.sql diff --git a/pandora_console/extras/mr/4.sql b/pandora_console/extras/mr/4.sql new file mode 100644 index 0000000000..828b770c90 --- /dev/null +++ b/pandora_console/extras/mr/4.sql @@ -0,0 +1,17 @@ +START TRANSACTION; + +alter table tusuario add autorefresh_white_list text not null default ''; +ALTER TABLE tserver_export MODIFY name varchar(600) BINARY NOT NULL default ''; + +CREATE TABLE IF NOT EXISTS `tpolicy_groups` ( + `id` int(10) unsigned NOT NULL auto_increment, + `id_policy` int(10) unsigned default '0', + `id_group` int(10) unsigned default '0', + `policy_applied` tinyint(1) unsigned default '0', + `pending_delete` tinyint(1) unsigned default '0', + `last_apply_utimestamp` int(10) unsigned NOT NULL default 0, + PRIMARY KEY (`id`), + UNIQUE (`id_policy`, `id_group`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +COMMIT; diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index 588a68da48..6b2577e051 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -181,6 +181,20 @@ CREATE TABLE IF NOT EXISTS `tpolicy_agents` ( UNIQUE (`id_policy`, `id_agent`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +-- ----------------------------------------------------- +-- Table `tpolicy_groups` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `tpolicy_groups` ( + `id` int(10) unsigned NOT NULL auto_increment, + `id_policy` int(10) unsigned default '0', + `id_group` int(10) unsigned default '0', + `policy_applied` tinyint(1) unsigned default '0', + `pending_delete` tinyint(1) unsigned default '0', + `last_apply_utimestamp` int(10) unsigned NOT NULL default 0, + PRIMARY KEY (`id`), + UNIQUE (`id_policy`, `id_group`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + -- --------------------------------------------------------------------- -- Table `tdashboard` -- --------------------------------------------------------------------- diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php index 5e965d2348..f86ba0849e 100644 --- a/pandora_console/godmode/agentes/configurar_agente.php +++ b/pandora_console/godmode/agentes/configurar_agente.php @@ -239,12 +239,14 @@ if ($create_agent) { $agent_created_ok = true; - $tpolicy_group = db_get_sql("SELECT id_policy FROM tpolicy_groups + $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups WHERE id_group = ".$grupo); - - if($tpolicy_group){ - db_process_sql_insert ('tpolicy_agents', - array('id_policy' => $tpolicy_group, 'id_agent' => $id_agente)); + + if($tpolicy_group_old){ + foreach ($tpolicy_group_old as $key => $old_group) { + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $old_group['id_policy'], 'id_agent' => $id_agente)); + } } $info = 'Name: ' . $nombre_agente . @@ -782,6 +784,10 @@ if ($update_agent) { // if modified some agent paramenter $values['update_module_count'] = 1; // Force an update of the agent cache. } + $group_old = db_get_sql("SELECT id_grupo FROM tagente WHERE id_agente =" .$id_agente); + $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$group_old); + $result = db_process_sql_update ('tagente', $values, array ('id_agente' => $id_agente)); if ($result === false) { ui_print_error_message( @@ -795,28 +801,20 @@ if ($update_agent) { // if modified some agent paramenter if ($old_interval != $intervalo) { enterprise_hook('config_agents_update_config_interval', array($id_agente, $intervalo)); } - - if($grupo){ - $group_old = db_get_sql("SELECT id_grupo FROM tagente WHERE id_agente =" .$id_agente); - - $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups - WHERE id_group = ".$group_old); - - if($tpolicy_group_old){ - foreach ($tpolicy_group_old as $key => $value) { - $tpolicy_agents_old= db_get_sql("SELECT * FROM tpolicy_agents - WHERE id_policy = ".$value['id_policy'] . " AND id_agent =" .$id_agente); + + if($tpolicy_group_old){ + foreach ($tpolicy_group_old as $key => $value) { + $tpolicy_agents_old= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$value['id_policy'] . " AND id_agent = " .$id_agente); - if($tpolicy_agents_old){ - $result2 = db_process_sql_update ('tpolicy_agents', - array('pending_delete' => 1), - array ('id_agent' => $id_agente, 'id_policy' => $value['id_policy'])); - } + if($tpolicy_agents_old){ + $result2 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 1), + array ('id_agent' => $id_agente, 'id_policy' => $value['id_policy'])); } } - } + } - //pruebassssss $tpolicy_group = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups WHERE id_group = ".$grupo); @@ -834,7 +832,6 @@ if ($update_agent) { // if modified some agent paramenter array ('id_agent' => $id_agente, 'id_policy' => $value['id_policy'])); } } - } $info = 'Group: ' . $grupo . ' Interval: ' . $intervalo . diff --git a/pandora_console/godmode/massive/massive_edit_agents.php b/pandora_console/godmode/massive/massive_edit_agents.php index 647b1f3226..802dc67e6b 100755 --- a/pandora_console/godmode/massive/massive_edit_agents.php +++ b/pandora_console/godmode/massive/massive_edit_agents.php @@ -149,51 +149,41 @@ if ($update_agents) { array ('id_agente' => $id_agent)); if($group_old || $result){ - $tpolicy_group_old = db_get_sql("SELECT id_policy FROM tpolicy_groups - WHERE id_group = ".$group_old); + $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$group_old); - if($tpolicy_group_old){ + if($tpolicy_group_old){ + foreach ($tpolicy_group_old as $key => $value) { $tpolicy_agents_old= db_get_sql("SELECT * FROM tpolicy_agents - WHERE id_policy = ".$tpolicy_group_old . " AND id_agent =" .$id_agent); - + WHERE id_policy = ".$value['id_policy'] . " AND id_agent = " .$id_agent); + if($tpolicy_agents_old){ $result2 = db_process_sql_update ('tpolicy_agents', array('pending_delete' => 1), - array ('id_agent' => $id_agent, 'id_policy' => $tpolicy_group_old)); + array ('id_agent' => $id_agent, 'id_policy' => $value['id_policy'])); } - } - - $tpolicy_group_new = db_get_sql("SELECT id_policy FROM tpolicy_groups - WHERE id_group = ".$values['id_grupo']); + } + + $tpolicy_group_new = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$values['id_grupo']); - if($tpolicy_group_new){ + if($tpolicy_group_new){ + foreach ($tpolicy_group_new as $key => $value) { $tpolicy_agents_new= db_get_sql("SELECT * FROM tpolicy_agents - WHERE id_policy = ".$tpolicy_group_new . " AND id_agent =" .$id_agent); - - if($tpolicy_agents_new){ + WHERE id_policy = ".$value['id_policy'] . " AND id_agent =" .$id_agent); + + if(!$tpolicy_agents_new){ + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $value['id_policy'], 'id_agent' => $id_agent)); + } else { $result3 = db_process_sql_update ('tpolicy_agents', array('pending_delete' => 0), - array ('id_agent' => $id_agent, 'id_policy' => $tpolicy_group_new)); - } else { - db_process_sql_insert ('tpolicy_agents', - array('id_policy' => $tpolicy_group_new, 'id_agent' => $id_agent)); + array ('id_agent' => $id_agent, 'id_policy' => $value['id_policy'])); } } - + } } - - // if($values['id_grupo'] || $result){ - // $tpolicy_agents= db_get_sql("SELECT * FROM tpolicy_agents - // WHERE id_policy = ".$tpolicy_group . " AND id_agent =" .$id_agente); - // - // $tpolicy_group = db_get_sql("SELECT id_policy FROM tpolicy_groups - // WHERE id_group = ".$values['id_grupo']); - // if ($tpolicy_group){ - // - // } - // } - } // Update Custom Fields diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 89fb8843da..657807e533 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -1195,7 +1195,11 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) { else { $cascadeProtectionModule = 0; } - + + $group_old = db_get_sql("SELECT id_grupo FROM tagente WHERE id_agente =" .$id_agent); + $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$group_old); + $return = db_process_sql_update('tagente', array('alias' => $alias, 'direccion' => $ip, @@ -1216,6 +1220,40 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) { // register ip for this agent in 'taddress' agents_add_address ($id_agent, $ip); } + + if($return){ + if($tpolicy_group_old){ + foreach ($tpolicy_group_old as $key => $value) { + $tpolicy_agents_old= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$value['id_policy'] . " AND id_agent = " .$id_agent); + + if($tpolicy_agents_old){ + $result2 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 1), + array ('id_agent' => $id_agent, 'id_policy' => $value['id_policy'])); + } + } + } + + $tpolicy_group = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$idGroup); + + if($tpolicy_group){ + foreach ($tpolicy_group as $key => $value) { + $tpolicy_agents= db_get_sql("SELECT * FROM tpolicy_agents + WHERE id_policy = ".$value['id_policy'] . " AND id_agent =" .$id_agent); + + if(!$tpolicy_agents){ + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $value['id_policy'], 'id_agent' => $id_agent)); + } else { + $result3 = db_process_sql_update ('tpolicy_agents', + array('pending_delete' => 0), + array ('id_agent' => $id_agent, 'id_policy' => $value['id_policy'])); + } + } + } + } returnData('string', array('type' => 'string', 'data' => (int)((bool)$return))); @@ -1339,7 +1377,19 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) { // register ip for this agent in 'taddress' agents_add_address ($idAgente, $ip); } - + + if($idGroup && !empty($idAgente)){ + $tpolicy_group_old = db_get_all_rows_sql("SELECT id_policy FROM tpolicy_groups + WHERE id_group = ".$idGroup); + + if($tpolicy_group_old){ + foreach ($tpolicy_group_old as $key => $old_group) { + db_process_sql_insert ('tpolicy_agents', + array('id_policy' => $old_group['id_policy'], 'id_agent' => $idAgente)); + } + } + } + returnData('string', array('type' => 'string', 'data' => $idAgente)); } diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 797660534c..c374170c36 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -404,7 +404,7 @@ function html_print_select_groups($id_user = false, $privilege = "AR", foreach ($include_groups as $value) { $field[$value] = $fields[$value]; } - $fields = $field; + $fields = array_intersect($fields, $field); } if ($strict_user) { diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 471b64e048..d8749dc075 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -2482,7 +2482,6 @@ CREATE TABLE IF NOT EXISTS `tpolicy_queue` ( `id` int(10) unsigned NOT NULL auto_increment, `id_policy` int(10) unsigned NOT NULL default '0', `id_agent` int(10) unsigned NOT NULL default '0', - `id_group` int(10) unsigned NOT NULL default '0', `operation` varchar(15) default '', `progress` int(10) unsigned NOT NULL default '0', `end_utimestamp` int(10) unsigned NOT NULL default 0,