OpenDNSSEC-enforcer 2.1.12
policy_export_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
3 * Copyright (c) 2014 OpenDNSSEC AB (svb)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include <getopt.h>
30#include "daemon/engine.h"
31#include "cmdhandler.h"
33#include "log.h"
34#include "str.h"
35#include "clientpipe.h"
37
39
40static const char *module_str = "policy_export_cmd";
41
42/* TODO: add export to specific file */
43
44static void
45usage(int sockfd)
46{
47 client_printf(sockfd,
48 "policy export\n"
49 " --policy <policy> | --all aka -p | -a \n"
50 );
51}
52
53static void
54help(int sockfd)
55{
56 client_printf(sockfd,
57 "Export a specified policy or all of them from the database.\n"
58 "\nOptions:\n"
59 "policy|all limit the operation to a specified policy or all of them\n\n"
60 );
61}
62
63static int
64run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
65{
66 #define NARGV 4
67 char* buf;
68 const char* argv[NARGV];
69 int returnCode;
70 int argc = 0, long_index = 0, opt = 0;
71 const char* policy_name = NULL;
72 int all = 0;
74 db_connection_t* dbconn = getconnectioncontext(context);;
75 engine_type* engine = getglobalcontext(context);
76
77 static struct option long_options[] = {
78 {"policy", required_argument, 0, 'p'},
79 {"all", no_argument, 0, 'a'},
80 {0, 0, 0, 0}
81 };
82
83 ods_log_debug("[%s] %s command", module_str, policy_export_funcblock.cmdname);
84
85 if (!cmd || !(buf = strdup(cmd))) {
86 client_printf_err(sockfd, "memory error\n");
87 return -1;
88 }
89
90 argc = ods_str_explode(buf, NARGV, argv);
91 if (argc == -1) {
92 client_printf_err(sockfd, "too many arguments\n");
93 ods_log_error("[%s] too many arguments for %s command",
94 module_str, policy_export_funcblock.cmdname);
95 free(buf);
96 return -1;
97 }
98
99 optind = 0;
100 while ((opt = getopt_long(argc, (char* const*)argv, "p:a", long_options, &long_index)) != -1) {
101 switch (opt) {
102 case 'p':
103 policy_name = optarg;
104 break;
105 case 'a':
106 all = 1;
107 break;
108 default:
109 client_printf_err(sockfd, "unknown arguments\n");
110 ods_log_error("[%s] unknown arguments for %s command",
111 module_str, policy_export_funcblock.cmdname);
112 free(buf);
113 return -1;
114 }
115 }
116
117 if (!dbconn) {
118 free(buf);
119 return 1;
120 }
121
122 if (all) {
123 if (policy_export_all(sockfd, dbconn, NULL) != POLICY_EXPORT_OK) {
124 free(buf);
125 return 1;
126 }
127 }
128 else if (policy_name) {
129 if (!(policy = policy_new_get_by_name(dbconn, policy_name))) {
130 client_printf_err(sockfd, "Unable to find policy %s!\n", policy_name);
131 free(buf);
132 return 1;
133 }
134 if (policy_export(sockfd, policy, NULL) != POLICY_EXPORT_OK) {
136 free(buf);
137 return 1;
138 }
140 }
141 else {
142 client_printf_err(sockfd, "Either --all or --policy needs to be given!\n");
143 free(buf);
144 return 1;
145 }
146
147 free(buf);
148 return 0;
149}
150
151struct cmd_func_block policy_export_funcblock = {
152 "policy export", &usage, &help, NULL, &run
153};
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
policy_t * policy_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: policy.c:2090
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
void policy_free(policy_t *policy)
Definition: policy.c:518
int policy_export(int sockfd, const policy_t *policy, const char *filename)
int policy_export_all(int sockfd, const db_connection_t *connection, const char *filename)
#define POLICY_EXPORT_OK
Definition: policy_export.h:38
struct cmd_func_block policy_export_funcblock
#define NARGV
Definition: policy.h:60