OpenDNSSEC-enforcer  2.0.4
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 "daemon/engine.h"
30 #include "daemon/cmdhandler.h"
31 #include "log.h"
32 #include "str.h"
33 #include "clientpipe.h"
34 #include "policy/policy_export.h"
35 
37 
38 static const char *module_str = "policy_export_cmd";
39 
40 /* TODO: add export to specific file */
41 
42 static void
43 usage(int sockfd)
44 {
45  client_printf(sockfd,
46  "policy export\n"
47  " --policy <policy> | --all aka -p | -a \n"
48  );
49 }
50 
51 static void
52 help(int sockfd)
53 {
54  client_printf(sockfd,
55  "Export a specified policy or all of them from the database.\n"
56  "\nOptions:\n"
57  "policy|all limit the operation to a specified policy or all of them\n\n"
58  );
59 }
60 
61 static int
62 handles(const char *cmd, ssize_t n)
63 {
64  return ods_check_command(cmd, n, policy_export_funcblock()->cmdname) ? 1 : 0;
65 }
66 
67 static int
68 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
69  db_connection_t *dbconn)
70 {
71  char* buf;
72  const char* argv[2];
73  int argc;
74  const char* policy_name = NULL;
75  int all = 0;
77  (void)engine; (void)cmd; (void)n;
78 
79  ods_log_debug("[%s] %s command", module_str, policy_export_funcblock()->cmdname);
80  cmd = ods_check_command(cmd, n, policy_export_funcblock()->cmdname);
81 
82  if (!(buf = strdup(cmd))) {
83  client_printf_err(sockfd, "memory error\n");
84  return -1;
85  }
86 
87  argc = ods_str_explode(buf, 2, argv);
88  if (argc > 2) {
89  client_printf_err(sockfd, "too many arguments\n");
90  free(buf);
91  return -1;
92  }
93 
94  ods_find_arg_and_param(&argc, argv, "policy", "p", &policy_name);
95  all = ods_find_arg(&argc, argv, "all", "a") > -1 ? 1 : 0;
96 
97  if (argc) {
98  client_printf_err(sockfd, "unknown arguments\n");
99  free(buf);
100  return -1;
101  }
102 
103  if (!dbconn) {
104  free(buf);
105  return 1;
106  }
107 
108  if (all) {
109  if (policy_export_all(sockfd, dbconn, NULL) != POLICY_EXPORT_OK) {
110  free(buf);
111  return 1;
112  }
113  }
114  else if (policy_name) {
115  if (!(policy = policy_new_get_by_name(dbconn, policy_name))) {
116  client_printf_err(sockfd, "Unable to find policy %s!\n", policy_name);
117  free(buf);
118  return 1;
119  }
120  if (policy_export(sockfd, policy, NULL) != POLICY_EXPORT_OK) {
121  policy_free(policy);
122  free(buf);
123  return 1;
124  }
125  policy_free(policy);
126  }
127  else {
128  client_printf_err(sockfd, "Either --all or --policy needs to be given!\n");
129  free(buf);
130  return 1;
131  }
132 
133  free(buf);
134  return 0;
135 }
136 
137 static struct cmd_func_block funcblock = {
138  "policy export", &usage, &help, &handles, &run
139 };
140 
141 struct cmd_func_block*
143 {
144  return &funcblock;
145 }
#define POLICY_EXPORT_OK
Definition: policy_export.h:38
void(* help)(int sockfd)
Definition: cmdhandler.h:64
void ods_log_debug(const char *format,...)
Definition: log.c:41
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
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
void policy_free(policy_t *policy)
Definition: policy.c:518
int policy_export_all(int sockfd, const db_connection_t *connection, const char *filename)
struct cmd_func_block * policy_export_funcblock(void)
int policy_export(int sockfd, const policy_t *policy, const char *filename)
Definition: policy.h:60
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67