OpenDNSSEC-enforcer  2.0.4
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 "daemon/engine.h"
30 #include "daemon/cmdhandler.h"
31 #include "log.h"
32 #include "str.h"
33 #include "clientpipe.h"
34 #include "policy/policy_import.h"
36 #include "enforcer/enforce_task.h"
37 
38 
40 
41 static const char *module_str = "policy_import_cmd";
42 
43 static void database_error_help(int sockfd) {
44  client_printf_err(sockfd,
45  "\nThe information in the database may have been changed during KASP update"
46  " and caused an update error, try rerunning policy import. If the problem persists"
47  " please check logs and database setup and after correcting the problem rerun policy import.\n"
48  );
49 }
50 
51 static void
52 usage(int sockfd)
53 {
54  client_printf(sockfd,
55  "policy import\n"
56  " [--remove-missing-policies] aka -r\n"
57  );
58 }
59 
60 static void
61 help(int sockfd)
62 {
63  client_printf(sockfd,
64  "Import policies from kasp.xml into the enforcer database.\n"
65  "\nOptions:\n"
66  "remove-missing-policies Remove any policies from database "
67  "that do not exist in the KASP file\n\n"
68  );
69 }
70 
71 
72 static int
73 handles(const char *cmd, ssize_t n)
74 {
75  return ods_check_command(cmd, n, policy_import_funcblock()->cmdname) ? 1 : 0;
76 }
77 
78 static int
79 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
80  db_connection_t *dbconn)
81 {
82  #define NARGV 8
83 
84  int remove_missing_policies, argc;
85  char buf[ODS_SE_MAXLINE];
86  char const *argv[NARGV];
87 
88  if (!engine || !engine->config || !engine->config->policy_filename
89  || !dbconn)
90  {
91  return 1;
92  }
93 
94  ods_log_debug("[%s] %s command", module_str, policy_import_funcblock()->cmdname);
95 
96  cmd = ods_check_command(cmd, n, policy_import_funcblock()->cmdname);
97  if (!cmd) return -1;
98  strncpy(buf, cmd, sizeof(buf));
99  buf[sizeof(buf)-1] = '\0';
100  /* separate the arguments*/
101  argc = ods_str_explode(buf, NARGV, argv);
102  if (argc > NARGV) {
103  ods_log_warning("[%s] too many arguments for %s command",
104  module_str, policy_import_funcblock()->cmdname);
105  client_printf(sockfd,"too many arguments\n");
106  return -1;
107  }
108  remove_missing_policies = (ods_find_arg(&argc, argv, "remove-missing-policies", "r") >= 0);
109  if (argc) {
110  ods_log_warning("[%s] unknown arguments for %s command",
111  module_str, policy_import_funcblock()->cmdname);
112  client_printf(sockfd,"unknown arguments\n");
113  return -1;
114  }
115 
116  switch (policy_import(sockfd, engine, dbconn, remove_missing_policies)) {
117  case POLICY_IMPORT_OK:
118  (void)flush_enforce_task(engine, 0);
119  (void)flush_resalt_task(engine);
120  return 0;
121  break;
122 
126  break;
127 
129  database_error_help(sockfd);
130  break;
131 
132  default:
133  break;
134  }
135 
136  return 1;
137 }
138 
139 static struct cmd_func_block funcblock = {
140  "policy import", &usage, &help, &handles, &run
141 };
142 
143 struct cmd_func_block*
145 {
146  return &funcblock;
147 }
void(* help)(int sockfd)
Definition: cmdhandler.h:64
void ods_log_debug(const char *format,...)
Definition: log.c:41
const char * policy_filename
Definition: cfg.h:56
#define POLICY_IMPORT_ERR_XML
Definition: policy_import.h:46
#define POLICY_IMPORT_OK
Definition: policy_import.h:38
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
struct cmd_func_block * policy_import_funcblock(void)
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
engineconfig_type * config
Definition: engine.h:53
#define POLICY_IMPORT_ERR_ARGS
Definition: policy_import.h:42
int flush_resalt_task(engine_type *engine)
int policy_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete)
#define POLICY_IMPORT_ERR_DATABASE
Definition: policy_import.h:50
#define POLICY_IMPORT_ERR_MEMORY
Definition: policy_import.h:54
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67
#define NARGV
void ods_log_warning(const char *format,...)
Definition: log.c:62
int flush_enforce_task(engine_type *engine, bool enforce_all)
Definition: enforce_task.c:323