OpenDNSSEC-enforcer  2.1.7
policy_import_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 "policy/policy_import.h"
38 #include "enforcer/enforce_task.h"
39 
40 
42 
43 static const char *module_str = "policy_import_cmd";
44 
45 static void database_error_help(int sockfd) {
46  client_printf_err(sockfd,
47  "\nThe information in the database may have been changed during KASP update"
48  " and caused an update error, try rerunning policy import. If the problem persists"
49  " please check logs and database setup and after correcting the problem rerun policy import.\n"
50  );
51 }
52 
53 static void
54 usage(int sockfd)
55 {
56  client_printf(sockfd,
57  "policy import\n"
58  " [--remove-missing-policies] aka -r\n"
59  );
60 }
61 
62 static void
63 help(int sockfd)
64 {
65  client_printf(sockfd,
66  "Import policies from kasp.xml into the enforcer database.\n"
67  "\nOptions:\n"
68  "remove-missing-policies Remove any policies from database "
69  "that do not exist in the KASP file\n\n"
70  );
71 }
72 
73 
74 static int
75 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
76 {
77  #define NARGV 3
78  db_connection_t* dbconn = getconnectioncontext(context);;
79  engine_type* engine = getglobalcontext(context);
80 
81  int remove_missing_policies = 0, argc = 0;
82  int long_index = 0, opt = 0;
83  char buf[ODS_SE_MAXLINE];
84  char const *argv[NARGV];
85 
86  static struct option long_options[] = {
87  {"remove-missing-policies", no_argument, 0, 'r'},
88  {0, 0, 0, 0}
89  };
90 
91  if (!engine || !engine->config || !engine->config->policy_filename
92  || !dbconn)
93  {
94  return 1;
95  }
96 
97  ods_log_debug("[%s] %s command", module_str, policy_import_funcblock.cmdname);
98 
99  if (!cmd) return -1;
100 
101  strncpy(buf, cmd, sizeof(buf));
102  buf[sizeof(buf)-1] = '\0';
103 
104  /* separate the arguments*/
105  argc = ods_str_explode(buf, NARGV, argv);
106  if (argc == -1) {
107  ods_log_error("[%s] too many arguments for %s command",
108  module_str, policy_import_funcblock.cmdname);
109  client_printf_err(sockfd,"too many arguments\n");
110  return -1;
111  }
112 
113  optind = 0;
114  while ((opt = getopt_long(argc, (char* const*)argv, "r", long_options, &long_index)) != -1 ) {
115  switch (opt) {
116  case 'r':
117  remove_missing_policies = 1;
118  break;
119  default:
120  client_printf_err(sockfd, "unknown arguments\n");
121  ods_log_error("[%s] unknown arguments for %s command",
122  module_str, policy_import_funcblock.cmdname);
123  return -1;
124  }
125  }
126 
127  switch (policy_import(sockfd, engine, dbconn, remove_missing_policies)) {
128  case POLICY_IMPORT_OK:
129  /* only zones in policy, and force! */
130  enforce_task_flush_all(engine, dbconn);
131  (void)flush_resalt_task_all(engine, dbconn);
132  return 0;
133  break;
134 
138  break;
139 
141  database_error_help(sockfd);
142  break;
143 
144  default:
145  break;
146  }
147 
148  return 1;
149 }
150 
151 struct cmd_func_block policy_import_funcblock = {
152  "policy import", &usage, &help, NULL, &run
153 };
flush_resalt_task_all
int flush_resalt_task_all(engine_type *engine, db_connection_t *dbconn)
Definition: policy_resalt_task.c:177
policy_resalt_task.h
enforcercommands.h
POLICY_IMPORT_ERR_ARGS
#define POLICY_IMPORT_ERR_ARGS
Definition: policy_import.h:42
engineconfig_struct::policy_filename
const char * policy_filename
Definition: cfg.h:56
policy_import.h
enforce_task_flush_all
void enforce_task_flush_all(engine_type *engine, db_connection_t *dbconn)
Definition: enforce_task.c:179
POLICY_IMPORT_ERR_MEMORY
#define POLICY_IMPORT_ERR_MEMORY
Definition: policy_import.h:54
POLICY_IMPORT_OK
#define POLICY_IMPORT_OK
Definition: policy_import.h:38
getconnectioncontext
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
Definition: enforcercommands.c:110
POLICY_IMPORT_ERR_DATABASE
#define POLICY_IMPORT_ERR_DATABASE
Definition: policy_import.h:50
policy_import
int policy_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete)
Definition: policy_import.c:196
engine_struct
Definition: engine.h:47
engine_struct::config
engineconfig_type * config
Definition: engine.h:48
POLICY_IMPORT_ERR_XML
#define POLICY_IMPORT_ERR_XML
Definition: policy_import.h:46
engine.h
db_connection
Definition: db_connection.h:46
getglobalcontext
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
Definition: enforcercommands.c:104
enforce_task.h
policy_import_cmd.h
policy_import_funcblock
struct cmd_func_block policy_import_funcblock
Definition: policy_import_cmd.c:151
NARGV
#define NARGV