policy_export_cmd.c
Go to the documentation of this file.
/*
* Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
* Copyright (c) 2014 OpenDNSSEC AB (svb)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <getopt.h>
#include "daemon/engine.h"
#include "cmdhandler.h"
#include "daemon/enforcercommands.h"
#include "log.h"
#include "str.h"
#include "clientpipe.h"
#include "policy/policy_export.h"
#include "policy/policy_export_cmd.h"
static const char *module_str = "policy_export_cmd";
/* TODO: add export to specific file */
static void
usage(int sockfd)
{
client_printf(sockfd,
"policy export\n"
" --policy <policy> | --all aka -p | -a \n"
);
}
static void
help(int sockfd)
{
client_printf(sockfd,
"Export a specified policy or all of them from the database.\n"
"\nOptions:\n"
"policy|all limit the operation to a specified policy or all of them\n\n"
);
}
static int
run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
{
#define NARGV 4
char* buf;
const char* argv[NARGV];
int returnCode;
int argc = 0, long_index = 0, opt = 0;
const char* policy_name = NULL;
int all = 0;
policy_t* policy;
db_connection_t* dbconn = getconnectioncontext(context);;
engine_type* engine = getglobalcontext(context);
static struct option long_options[] = {
{"policy", required_argument, 0, 'p'},
{"all", no_argument, 0, 'a'},
{0, 0, 0, 0}
};
ods_log_debug("[%s] %s command", module_str, policy_export_funcblock.cmdname);
if (!cmd || !(buf = strdup(cmd))) {
client_printf_err(sockfd, "memory error\n");
return -1;
}
argc = ods_str_explode(buf, NARGV, argv);
if (argc == -1) {
client_printf_err(sockfd, "too many arguments\n");
ods_log_error("[%s] too many arguments for %s command",
module_str, policy_export_funcblock.cmdname);
free(buf);
return -1;
}
optind = 0;
while ((opt = getopt_long(argc, (char* const*)argv, "p:a", long_options, &long_index)) != -1) {
switch (opt) {
case 'p':
policy_name = optarg;
break;
case 'a':
all = 1;
break;
default:
client_printf_err(sockfd, "unknown arguments\n");
ods_log_error("[%s] unknown arguments for %s command",
module_str, policy_export_funcblock.cmdname);
free(buf);
return -1;
}
}
if (!dbconn) {
free(buf);
return 1;
}
if (all) {
if (policy_export_all(sockfd, dbconn, NULL) != POLICY_EXPORT_OK) {
free(buf);
return 1;
}
}
else if (policy_name) {
if (!(policy = policy_new_get_by_name(dbconn, policy_name))) {
client_printf_err(sockfd, "Unable to find policy %s!\n", policy_name);
free(buf);
return 1;
}
if (policy_export(sockfd, policy, NULL) != POLICY_EXPORT_OK) {
policy_free(policy);
free(buf);
return 1;
}
policy_free(policy);
}
else {
client_printf_err(sockfd, "Either --all or --policy needs to be given!\n");
free(buf);
return 1;
}
free(buf);
return 0;
}
struct cmd_func_block policy_export_funcblock = {
"policy export", &usage, &help, NULL, &run
};
POLICY_EXPORT_OK
#define POLICY_EXPORT_OK
Definition: policy_export.h:38
engine.h
policy_new_get_by_name
policy_t * policy_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: policy.c:2090
policy_name
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
policy_export_cmd.h
policy_export.h
getglobalcontext
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
Definition: enforcercommands.c:102
getconnectioncontext
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
Definition: enforcercommands.c:108
policy_free
void policy_free(policy_t *policy)
Definition: policy.c:518
policy_export_all
int policy_export_all(int sockfd, const db_connection_t *connection, const char *filename)
Definition: policy_export.c:413
enforcercommands.h
NARGV
#define NARGV
engine_struct
Definition: engine.h:47
policy_export
int policy_export(int sockfd, const policy_t *policy, const char *filename)
Definition: policy_export.c:532
policy
Definition: policy.h:60
db_connection
Definition: db_connection.h:46
policy_export_funcblock
struct cmd_func_block policy_export_funcblock
Definition: policy_export_cmd.c:151