Merge branch 'ent-9500-cli-en-la-metaconsola-no-se-sicnroniza-con-los-nodos' into 'develop'

synch cli with nodes pandora_enterprise#9500

pandora_enterprise#9500

See merge request artica/pandorafms!5198
This commit is contained in:
Jimmy Olano 2022-10-28 01:30:22 +00:00
commit 4594fb17e2
2 changed files with 273 additions and 33 deletions

View File

@ -113,6 +113,10 @@ our @EXPORT = qw(
get_agentmodule_status_str
get_agentmodule_data
set_ssl_opts
db_synch_insert
db_synch_update
db_synch_delete
db_synch
$RDBMS
$RDBMS_QUOTE
$RDBMS_QUOTE_STRING
@ -1672,6 +1676,78 @@ sub set_ssl_opts($) {
}
}
########################################################################
## Synch insert query with nodes.
########################################################################
sub db_synch_insert ($$$$$@) {
my ($dbh, $pa_config, $table, $query, $result, @values) = @_;
my $substr = "\"\%s\"";
$query =~ s/\?/$substr/g;
my $query_string = sprintf($query, @values);
db_synch($dbh, $pa_config, 'INSERT INTO', $table, $query_string, $result);
}
########################################################################
## Synch update query with nodes.
########################################################################
sub db_synch_update ($$$$$@) {
my ($dbh, $pa_config, $table, $query, $result, @values) = @_;
my $substr = "\"\%s\"";
$query =~ s/\?/$substr/g;
my $query_string = sprintf($query, @values);
db_synch($dbh, $pa_config, 'UPDATE', $table, $query_string, $result);
}
########################################################################
## Synch delete query with nodes.
########################################################################
sub db_synch_delete ($$$$@) {
my ($dbh, $pa_config, $table, $result, @parameters) = @_;
#Build query string.
my $query = $dbh->{Statement};
my $substr = "\"\%s\"";
$query =~ s/\?/$substr/g;
my $query_string = sprintf($query, @parameters);
db_synch($dbh, $pa_config, 'DELETE FROM', $table, $query_string, $result);
}
########################################################################
## Synch queries with nodes.
########################################################################
sub db_synch ($$$$$$) {
my ($dbh, $pa_config, $type, $table, $query, $result) = @_;
my @nodes = get_db_rows($dbh, 'SELECT * FROM tmetaconsole_setup');
foreach my $node (@nodes) {
eval {
local $SIG{__DIE__};
my @values_queue = (
safe_input($query),
$node->{'id'},
time(),
$type,
$table,
'',
$result
);
my $query_queue = 'INSERT INTO tsync_queue (`sql`, `target`, `utimestamp`, `operation`, `table`, `error`, `result`) VALUES (?, ?, ?, ?, ?, ?, ?)';
db_insert ($dbh, 'id', $query_queue, @values_queue);
};
if ($@) {
logger($pa_config, "Error add sync_queue: $@", 10);
return;
}
}
}
# End of function declaration
# End of defined Code

View File

@ -539,8 +539,27 @@ sub pandora_add_profile_to_user ($$$;$) {
$group_id = 0 unless defined($group_id);
db_do ($dbh, 'INSERT INTO tusuario_perfil (id_usuario, id_perfil, id_grupo)
VALUES (?, ?, ?)', safe_input($user_id), $profile_id, $group_id);
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
my $query = 'INSERT INTO tusuario_perfil (id_usuario, id_perfil, id_grupo) VALUES (?, ?, ?)';
my @values = (
safe_input($user_id),
$profile_id,
$group_id
);
my $res = db_do ($dbh, $query, @values);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_insert($dbh, $conf, 'tusuario_perfil', $query, $res, @values);
}
return $res;
}
@ -568,13 +587,29 @@ sub cli_create_snmp_trap ($$) {
sub pandora_create_user ($$$$$) {
my ($dbh, $name, $password, $is_admin, $comments) = @_;
if(is_metaconsole($conf) != 1 && pandora_get_tconfig_token ($dbh, 'centralized_management', '')) {
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
my $query = 'INSERT INTO tusuario (id_user, fullname, password, comments, is_admin) VALUES (?, ?, ?, ?, ?)';
my @values = (
safe_input($name),
safe_input($name),
$password,
decode_entities($comments),
$is_admin ? '1' : '0'
);
return db_insert ($dbh, 'id_user', 'INSERT INTO tusuario (id_user, fullname, password, is_admin, comments)
VALUES (?, ?, ?, ?, ?)', safe_input($name), safe_input($name), $password, $is_admin ? '1' : '0', decode_entities($comments));
my $res = db_insert($dbh, 'id_user', $query, @values);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_insert($dbh, $conf, 'tusuario', $query, $res, @values);
}
return $res;
}
##########################################################################
@ -583,17 +618,27 @@ sub pandora_create_user ($$$$$) {
sub pandora_delete_user ($$) {
my ($dbh, $name) = @_;
if(is_metaconsole($conf) != 1 && pandora_get_tconfig_token ($dbh, 'centralized_management', '')) {
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To delete a user go to metaconsole. \n\n";
exit;
}
# Delete user profiles
db_do ($dbh, 'DELETE FROM tusuario_perfil WHERE id_usuario = ?', $name);
my $result_profile = db_do ($dbh, 'DELETE FROM tusuario_perfil WHERE id_usuario = ?', $name);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_delete($dbh, $conf, 'tusuario_perfil', $result_profile, $name);
}
# Delete the user
my $return = db_do ($dbh, 'DELETE FROM tusuario WHERE id_user = ?', $name);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_delete($dbh, $conf, 'tusuario', $return, $name);
}
if($return eq '0E0') {
return -1;
}
@ -623,25 +668,79 @@ else {
## Assign a profile to the given user/group.
##########################################################################
sub pandora_create_user_profile ($$$$) {
my ($dbh, $user_id, $profile_id, $group_id) = @_;
return db_insert ($dbh, 'id_up', 'INSERT INTO tusuario_perfil (id_usuario, id_perfil, id_grupo) VALUES (?, ?, ?)', $user_id, $profile_id, $group_id);
my ($dbh, $user_id, $profile_id, $group_id) = @_;
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
my $query = 'INSERT INTO tusuario_perfil (id_usuario, id_perfil, id_grupo) VALUES (?, ?, ?)';
my @values = (
safe_input($user_id),
$profile_id,
$group_id
);
my $res = db_insert ($dbh, 'id_up', $query, @values);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_insert($dbh, $conf, 'tusuario_perfil', $query, $res, @values);
}
return $res;
}
##########################################################################
## Create profile.
##########################################################################
sub pandora_create_profile ($$$$$$$$$$$$$$$$$$$$$$) {
my ($dbh, $profile_name, $agent_view,
my ($dbh, $profile_name, $agent_view,
$agent_edit, $agent_disable, $alert_edit, $alert_management, $user_management, $db_management,
$event_view, $event_edit, $event_management, $report_view, $report_edit, $report_management,
$map_view, $map_edit, $map_management, $vconsole_view, $vconsole_edit, $vconsole_management, $pandora_management) = @_;
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
return db_insert ($dbh, 'id_up', 'INSERT INTO tperfil (name,agent_view,agent_edit,agent_disable,alert_edit,alert_management,user_management,db_management,event_view,event_edit,event_management,report_view,report_edit,report_management,map_view,map_edit,map_management,vconsole_view,vconsole_edit,vconsole_management,pandora_management) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
safe_input($profile_name), $agent_view,
$agent_edit, $agent_disable, $alert_edit, $alert_management, $user_management, $db_management,
$event_view, $event_edit, $event_management, $report_view, $report_edit, $report_management,
$map_view, $map_edit, $map_management, $vconsole_view, $vconsole_edit, $vconsole_management, $pandora_management);
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
my $query = 'INSERT INTO tperfil (name,agent_view,agent_edit,agent_disable,alert_edit,alert_management,user_management,db_management,event_view,event_edit,event_management,report_view,report_edit,report_management,map_view,map_edit,map_management,vconsole_view,vconsole_edit,vconsole_management,pandora_management) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
my @values = (
safe_input($profile_name),
$agent_view,
$agent_edit,
$agent_disable,
$alert_edit,
$alert_management,
$user_management,
$db_management,
$event_view,
$event_edit,
$event_management,
$report_view,
$report_edit,
$report_management,
$map_view,
$map_edit,
$map_management,
$vconsole_view,
$vconsole_edit,
$vconsole_management,
$pandora_management
);
my $res = db_insert ($dbh, 'id_perfil', $query, @values);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_insert($dbh, $conf, 'tperfil', $query, $res, @values);
}
return $res;
}
##########################################################################
@ -653,11 +752,33 @@ sub pandora_update_profile ($$$$$$$$$$$$$$$$$$$$$$) {
$event_view, $event_edit, $event_management, $report_view, $report_edit, $report_management,
$map_view, $map_edit, $map_management, $vconsole_view, $vconsole_edit, $vconsole_management, $pandora_management) = @_;
return db_update ($dbh, 'UPDATE tperfil SET agent_view = ?, agent_edit = ?, agent_disable = ?, alert_edit = ?, alert_management = ?, user_management = ?, db_management = ?, event_view = ?, event_edit = ?, event_management = ?, report_view = ?, report_edit = ?, report_management = ?, map_view = ?, map_edit = ?, map_management = ?, vconsole_view = ?, vconsole_edit = ?, vconsole_management = ?, pandora_management = ? WHERE name=?;',
$agent_view,
$agent_edit, $agent_disable, $alert_edit, $alert_management, $user_management, $db_management,
$event_view, $event_edit, $event_management, $report_view, $report_edit, $report_management,
$map_view, $map_edit, $map_management, $vconsole_view, $vconsole_edit, $vconsole_management, $pandora_management, safe_input($profile_name));
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
my @parameters = (
$agent_view, $agent_edit, $agent_disable,
$alert_edit, $alert_management,
$user_management, $db_management,
$event_view, $event_edit, $event_management,
$report_view, $report_edit, $report_management,
$map_view, $map_edit, $map_management,
$vconsole_view, $vconsole_edit, $vconsole_management,
$pandora_management, safe_input($profile_name)
);
my $query = 'UPDATE tperfil SET agent_view = ?, agent_edit = ?, agent_disable = ?, alert_edit = ?, alert_management = ?, user_management = ?, db_management = ?, event_view = ?, event_edit = ?, event_management = ?, report_view = ?, report_edit = ?, report_management = ?, map_view = ?, map_edit = ?, map_management = ?, vconsole_view = ?, vconsole_edit = ?, vconsole_management = ?, pandora_management = ? WHERE name=?;';
my $result = db_update ($dbh, $query, @parameters);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_update($dbh, $conf, 'tperfil', $query, $result, @parameters);
}
return $result;
}
##########################################################################
@ -665,8 +786,28 @@ sub pandora_update_profile ($$$$$$$$$$$$$$$$$$$$$$) {
##########################################################################
sub pandora_delete_user_profile ($$$$) {
my ($dbh, $user_id, $profile_id, $group_id) = @_;
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To delete a user go to metaconsole. \n\n";
exit;
}
my @parameters = (
$user_id,
$profile_id,
$group_id
);
# Delete the user
my $return = db_do ($dbh, 'DELETE FROM tusuario_perfil WHERE id_usuario=? AND id_perfil=? AND id_grupo=?', @parameters);
if(is_metaconsole($conf) == 1 && $centralized) {
db_synch_delete($dbh, $conf, 'tusuario_perfil', $return, @parameters);
}
return db_do ($dbh, 'DELETE FROM tusuario_perfil WHERE id_usuario=? AND id_perfil=? AND id_grupo=?', $user_id, $profile_id, $group_id);
return $return;
}
##########################################################################
@ -816,9 +957,18 @@ sub pandora_validate_event_id ($$$) {
##########################################################################
sub pandora_update_user_from_hash ($$$$) {
my ($parameters, $where_column, $where_value, $dbh) = @_;
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
my $result = db_process_update($dbh, 'tusuario', $parameters, {$where_column => $where_value});
if(is_metaconsole($conf) == 1 && $centralized) {
my @values = (
values %$parameters,
$where_value
);
my $user_id = db_process_update($dbh, 'tusuario', $parameters, {$where_column => $where_value});
return $user_id;
db_synch_update($dbh, $conf, 'tusuario', $dbh->{Statement}, $result, @values);
}
return $result;
}
##########################################################################
@ -6235,8 +6385,10 @@ sub cli_disable_double_auth () {
sub cli_user_enable () {
my $user_id = @ARGV[2];
if(is_metaconsole($conf) != 1 && pandora_get_tconfig_token ($dbh, 'centralized_management', '')) {
print_log "[ERROR] This node is configured with centralized mode. To enable a user go to metaconsole. \n\n";
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
@ -6253,9 +6405,14 @@ sub cli_user_enable () {
$user_id = safe_input($user_id);
db_do ($dbh, "UPDATE tusuario SET disabled = '0' WHERE id_user = '$user_id'");
exit;
my $result = db_do ($dbh, "UPDATE tusuario SET disabled = '0' WHERE id_user = '$user_id'");
if(is_metaconsole($conf) == 1 && $centralized) {
my @values;
db_synch_update($dbh, $conf, 'tusuario', $dbh->{Statement}, $result, @values);
}
exit;
}
###############################################################################
@ -6265,8 +6422,10 @@ sub cli_user_enable () {
sub cli_user_disable () {
my $user_id = @ARGV[2];
if(is_metaconsole($conf) != 1 && pandora_get_tconfig_token ($dbh, 'centralized_management', '')) {
print_log "[ERROR] This node is configured with centralized mode. To disable a user go to metaconsole. \n\n";
my $centralized = pandora_get_tconfig_token ($dbh, 'centralized_management', '');
if(is_metaconsole($conf) != 1 && $centralized) {
print_log "[ERROR] This node is configured with centralized mode. To create a user go to metaconsole. \n\n";
exit;
}
@ -6283,9 +6442,14 @@ sub cli_user_disable () {
$user_id = safe_input($user_id);
db_do ($dbh, "UPDATE tusuario SET disabled = '1' WHERE id_user = '$user_id'");
my $result = db_do ($dbh, "UPDATE tusuario SET disabled = '1' WHERE id_user = '$user_id'");
if(is_metaconsole($conf) == 1 && $centralized) {
my @values;
db_synch_update($dbh, $conf, 'tusuario', $dbh->{Statement}, $result, @values);
}
exit;
exit;
}
###############################################################################