policy_purge_cmd.c
Go to the documentation of this file.
#include "daemon/engine.h"
#include "cmdhandler.h"
#include "daemon/enforcercommands.h"
#include "log.h"
#include "str.h"
#include "clientpipe.h"
#include "enforcer/enforce_task.h"
#include "db/policy.h"
#include "policy/policy_purge_cmd.h"
static const char *module_str = "policy_purge_cmd";
static void
usage(int sockfd)
{
client_printf(sockfd,
"policy purge\n"
);
}
static void
help(int sockfd)
{
client_printf(sockfd,
"This command will remove any policies from the database which have no\n"
"associated zones. Use with caution.\n\n"
);
}
static int
purge_policies(int sockfd, db_connection_t *dbconn)
{
policy_list_t* policy_list;
policy_t* policy;
zone_list_db_t* zonelist;
const char* name;
size_t listsize;
int result = 0;
client_printf(sockfd, "Purging policies\n");
policy_list = policy_list_new_get(dbconn);
if (!policy_list) return 1;
while ((policy = policy_list_get_next(policy_list))) {
name = policy_name(policy);
/*fetch zonelist from db, owned by policy*/
if (policy_retrieve_zone_list(policy)) {
result = 1;
client_printf(sockfd, "Error fetching zones\n");
break;
}
zonelist = policy_zone_list(policy);
listsize = zone_list_db_size(zonelist);
if (listsize == 0) {
ods_log_info("[%s] No zones on policy %s; purging...", module_str, name);
client_printf(sockfd, "No zones on policy %s; purging...\n", name);
if (policy_delete(policy)) {
ods_log_crit("[%s] Error while purging policy from database", module_str);
client_printf(sockfd, "Error while updating database\n", name);
result++;
}
}
policy_free(policy);
}
policy_list_free(policy_list);
return result;
}
static int
run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
{
db_connection_t* dbconn = getconnectioncontext(context);;
engine_type* engine = getglobalcontext(context);
(void) cmd;
ods_log_debug("[%s] %s command", module_str, policy_purge_funcblock.cmdname);
return purge_policies(sockfd, dbconn);
}
struct cmd_func_block policy_purge_funcblock = {
"policy purge", &usage, &help, NULL, &run
};
policy_list
Definition: policy.h:733
policy_list_free
void policy_list_free(policy_list_t *policy_list)
Definition: policy.c:2664
engine.h
policy_name
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
policy_delete
int policy_delete(policy_t *policy)
Definition: policy.c:2571
getglobalcontext
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
Definition: enforcercommands.c:102
policy_purge_funcblock
struct cmd_func_block policy_purge_funcblock
Definition: policy_purge_cmd.c:88
getconnectioncontext
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
Definition: enforcercommands.c:108
enforce_task.h
policy_list_get_next
policy_t * policy_list_get_next(policy_list_t *policy_list)
Definition: policy.c:3280
policy_free
void policy_free(policy_t *policy)
Definition: policy.c:518
policy.h
policy_retrieve_zone_list
int policy_retrieve_zone_list(policy_t *policy)
Definition: policy.c:1111
enforcercommands.h
zone_list_db
Definition: zone_db.h:466
policy_purge_cmd.h
policy_list_new_get
policy_list_t * policy_list_new_get(const db_connection_t *connection)
Definition: policy.c:3079
policy_zone_list
zone_list_db_t * policy_zone_list(policy_t *policy)
Definition: policy.c:1093
engine_struct
Definition: engine.h:47
policy
Definition: policy.h:60
db_connection
Definition: db_connection.h:46
zone_list_db_size
size_t zone_list_db_size(zone_list_db_t *zone_list)
Definition: zone_db.c:2705