OpenDNSSEC-enforcer 2.1.13
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"
36#include "longgetopt.h"
38
40
41static const char *module_str = "policy_export_cmd";
42
43/* TODO: add export to specific file */
44
45static void
46usage(int sockfd)
47{
48 client_printf(sockfd,
49 "policy export\n"
50 " --policy <policy> | --all aka -p | -a \n"
51 );
52}
53
54static void
55help(int sockfd)
56{
57 client_printf(sockfd,
58 "Export a specified policy or all of them from the database.\n"
59 "\nOptions:\n"
60 "policy|all limit the operation to a specified policy or all of them\n\n"
61 );
62}
63
64static int
65run(cmdhandler_ctx_type* context, int argc, char* argv[])
66{
67 int sockfd = context->sockfd;
68 struct longgetopt optctx;
69 int long_index = 0, opt = 0;
70 const char* policy_name = NULL;
71 int all = 0;
73 db_connection_t* dbconn = getconnectioncontext(context);;
74
75 static struct option long_options[] = {
76 {"policy", required_argument, 0, 'p'},
77 {"all", no_argument, 0, 'a'},
78 {0, 0, 0, 0}
79 };
80
81 for(opt = longgetopt(argc, argv, "p:a", long_options, &long_index, &optctx); opt != -1;
82 opt = longgetopt(argc, argv, NULL, long_options, &long_index, &optctx)) {
83 switch (opt) {
84 case 'p':
85 policy_name = optctx.optarg;
86 break;
87 case 'a':
88 all = 1;
89 break;
90 default:
91 client_printf_err(sockfd, "unknown arguments\n");
92 ods_log_error("[%s] unknown arguments for policy export command", module_str);
93 return -1;
94 }
95 }
96
97 if (!dbconn) {
98 return 1;
99 }
100
101 if (all) {
102 if (policy_export_all(sockfd, dbconn, NULL) != POLICY_EXPORT_OK) {
103 return 1;
104 }
105 }
106 else if (policy_name) {
107 if (!(policy = policy_new_get_by_name(dbconn, policy_name))) {
108 client_printf_err(sockfd, "Unable to find policy %s!\n", policy_name);
109 return 1;
110 }
111 if (policy_export(sockfd, policy, NULL) != POLICY_EXPORT_OK) {
113 return 1;
114 }
116 }
117 else {
118 client_printf_err(sockfd, "Either --all or --policy needs to be given!\n");
119 return 1;
120 }
121
122 return 0;
123}
124
125struct cmd_func_block policy_export_funcblock = {
126 "policy export", &usage, &help, NULL, NULL, &run, NULL
127};
db_connection_t * getconnectioncontext(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
struct cmd_func_block policy_export_funcblock